diff options
Diffstat (limited to '05/src/crypto.c')
| -rw-r--r-- | 05/src/crypto.c | 239 |
1 files changed, 239 insertions, 0 deletions
diff --git a/05/src/crypto.c b/05/src/crypto.c new file mode 100644 index 0000000..eb60ba9 --- /dev/null +++ b/05/src/crypto.c | |||
| @@ -0,0 +1,239 @@ | |||
| 1 | // crypto.c --- cryptography utilities | ||
| 2 | // | ||
| 3 | // Copyright (c) 2026 Martin Michalec <martin@michalec.dev> | ||
| 4 | // | ||
| 5 | // SPDX-License-Identifier: MIT | ||
| 6 | // | ||
| 7 | // Commentary: | ||
| 8 | // | ||
| 9 | // Code: | ||
| 10 | |||
| 11 | #include "crypto.h" | ||
| 12 | |||
| 13 | #include <openssl/evp.h> | ||
| 14 | #include <openssl/kdf.h> | ||
| 15 | #include <openssl/err.h> | ||
| 16 | #include <openssl/rand.h> | ||
| 17 | #include <openssl/crypto.h> | ||
| 18 | #include <openssl/core_names.h> | ||
| 19 | |||
| 20 | #include "common.h" | ||
| 21 | |||
| 22 | static EVP_PKEY *pkey_local = NULL; | ||
| 23 | static EVP_PKEY_CTX *pkey_derive_ctx = NULL; | ||
| 24 | |||
| 25 | uint8_t local_pubkey[X25519_PUBKEY_LEN]; | ||
| 26 | |||
| 27 | static EVP_MD *md_sha1 = NULL; | ||
| 28 | static EVP_MD_CTX *md_sha1_ctx = NULL; | ||
| 29 | |||
| 30 | static EVP_KDF *hkdf = NULL; | ||
| 31 | static EVP_KDF_CTX *hkdf_ctx = NULL; | ||
| 32 | |||
| 33 | static EVP_CIPHER *cipher_aes_cbc = NULL; | ||
| 34 | static EVP_CIPHER_CTX *cipher_ctx = NULL; | ||
| 35 | |||
| 36 | bool | ||
| 37 | crypto_init (void) | ||
| 38 | { | ||
| 39 | EVP_PKEY_CTX *pkey_gen_ctx = | ||
| 40 | EVP_PKEY_CTX_new_from_name (NULL, "X25519", NULL); | ||
| 41 | if (pkey_gen_ctx == NULL) goto error; | ||
| 42 | |||
| 43 | if (EVP_PKEY_keygen_init (pkey_gen_ctx) <= 0) { | ||
| 44 | EVP_PKEY_CTX_free (pkey_gen_ctx); | ||
| 45 | goto error; | ||
| 46 | } | ||
| 47 | |||
| 48 | if (EVP_PKEY_keygen (pkey_gen_ctx, &pkey_local) <= 0) { | ||
| 49 | EVP_PKEY_CTX_free (pkey_gen_ctx); | ||
| 50 | goto error; | ||
| 51 | } EVP_PKEY_CTX_free (pkey_gen_ctx); | ||
| 52 | |||
| 53 | pkey_derive_ctx = EVP_PKEY_CTX_new_from_pkey (NULL, pkey_local, NULL); | ||
| 54 | if (pkey_derive_ctx == NULL) goto error; | ||
| 55 | |||
| 56 | size_t len; | ||
| 57 | if (EVP_PKEY_get_raw_public_key (pkey_local, local_pubkey, &len) <= 0) | ||
| 58 | goto error; | ||
| 59 | if (len != X25519_PUBKEY_LEN) goto error_pkey_derive_ctx; | ||
| 60 | |||
| 61 | md_sha1 = EVP_MD_fetch (NULL, "SHA-1", NULL); | ||
| 62 | if (md_sha1 == NULL) goto error_pkey_derive_ctx; | ||
| 63 | |||
| 64 | md_sha1_ctx = EVP_MD_CTX_new (); | ||
| 65 | if (md_sha1_ctx == NULL) goto error_md_sha1; | ||
| 66 | |||
| 67 | if (!EVP_DigestInit_ex2 (md_sha1_ctx, md_sha1, NULL)) | ||
| 68 | goto error_md_sha1_ctx; | ||
| 69 | |||
| 70 | hkdf = EVP_KDF_fetch (NULL, "HKDF", NULL); | ||
| 71 | if (!hkdf) goto error_md_sha1_ctx; | ||
| 72 | |||
| 73 | hkdf_ctx = EVP_KDF_CTX_new (hkdf); | ||
| 74 | if (!hkdf_ctx) goto error_hkdf; | ||
| 75 | |||
| 76 | cipher_aes_cbc = EVP_CIPHER_fetch (NULL, "AES-256-CBC", NULL); | ||
| 77 | if (cipher_aes_cbc == NULL) goto error_hkdf_ctx; | ||
| 78 | |||
| 79 | cipher_ctx = EVP_CIPHER_CTX_new (); | ||
| 80 | if (cipher_ctx == NULL) goto error_cipher_aes_cbc; | ||
| 81 | |||
| 82 | return true; | ||
| 83 | |||
| 84 | error_cipher_aes_cbc: | ||
| 85 | EVP_CIPHER_free (cipher_aes_cbc); | ||
| 86 | error_hkdf_ctx: | ||
| 87 | EVP_KDF_CTX_free (hkdf_ctx); | ||
| 88 | error_hkdf: | ||
| 89 | EVP_KDF_free (hkdf); | ||
| 90 | error_pkey_derive_ctx: | ||
| 91 | EVP_PKEY_CTX_free (pkey_derive_ctx); | ||
| 92 | error_md_sha1_ctx: | ||
| 93 | EVP_MD_CTX_free (md_sha1_ctx); | ||
| 94 | error_md_sha1: | ||
| 95 | EVP_MD_free (md_sha1); | ||
| 96 | error: | ||
| 97 | ERR_print_errors_fp (stderr); | ||
| 98 | return false; | ||
| 99 | } | ||
| 100 | |||
| 101 | ssize_t | ||
| 102 | sha1_hash (uint8_t out[SHA_DIGEST_SIZE], struct memory_view in) | ||
| 103 | { | ||
| 104 | unsigned int length; | ||
| 105 | |||
| 106 | if (EVP_DigestInit_ex2 (md_sha1_ctx, NULL, NULL) != 1) goto error; | ||
| 107 | if (EVP_DigestUpdate (md_sha1_ctx, in.data, in.size) != 1) goto error; | ||
| 108 | if (EVP_DigestFinal_ex (md_sha1_ctx, out, &length) != 1) goto error; | ||
| 109 | |||
| 110 | return length; | ||
| 111 | |||
| 112 | error: | ||
| 113 | ERR_reason_error_string (ERR_get_error ()); | ||
| 114 | return -1; | ||
| 115 | } | ||
| 116 | |||
| 117 | bool | ||
| 118 | hkdf_sha256 (const uint8_t peer_pubkey[X25519_PUBKEY_LEN], aes_256_key out) | ||
| 119 | { | ||
| 120 | EVP_PKEY *peer_pkey = | ||
| 121 | EVP_PKEY_new_raw_public_key_ex (NULL, "X25519", NULL, | ||
| 122 | peer_pubkey, X25519_PUBKEY_LEN); | ||
| 123 | if (peer_pkey == NULL) goto error; | ||
| 124 | |||
| 125 | if (EVP_PKEY_derive_init (pkey_derive_ctx) <= 0) | ||
| 126 | goto error; | ||
| 127 | if (EVP_PKEY_derive_set_peer (pkey_derive_ctx, peer_pkey) <= 0) | ||
| 128 | goto error; | ||
| 129 | |||
| 130 | size_t ikm_len = 0; | ||
| 131 | if (EVP_PKEY_derive (pkey_derive_ctx, NULL, &ikm_len) <= 0) goto error; | ||
| 132 | |||
| 133 | uint8_t *ikm = OPENSSL_malloc (ikm_len); | ||
| 134 | if (!ikm) goto error; | ||
| 135 | |||
| 136 | if (EVP_PKEY_derive (pkey_derive_ctx, ikm, &ikm_len) <= 0) goto error; | ||
| 137 | EVP_PKEY_free (peer_pkey); | ||
| 138 | |||
| 139 | uint8_t salt[] = { 0xcd }; | ||
| 140 | size_t salt_len = sizeof (salt); | ||
| 141 | uint8_t info[] = { 0xcd }; | ||
| 142 | size_t info_len = sizeof (info); | ||
| 143 | |||
| 144 | OSSL_PARAM params[] = { | ||
| 145 | OSSL_PARAM_construct_utf8_string (OSSL_KDF_PARAM_DIGEST, "SHA256", 0), | ||
| 146 | OSSL_PARAM_construct_octet_string (OSSL_KDF_PARAM_KEY, (void *) ikm, ikm_len), | ||
| 147 | OSSL_PARAM_construct_octet_string (OSSL_KDF_PARAM_SALT, (void *) salt, salt_len), | ||
| 148 | OSSL_PARAM_construct_octet_string (OSSL_KDF_PARAM_INFO, (void *) info, info_len), | ||
| 149 | OSSL_PARAM_construct_end (), | ||
| 150 | }; | ||
| 151 | |||
| 152 | EVP_KDF_CTX_reset (hkdf_ctx); | ||
| 153 | if (EVP_KDF_derive (hkdf_ctx, out, AES_256_KEY_SIZE, params) <= 0) | ||
| 154 | goto error; | ||
| 155 | OPENSSL_free (ikm); | ||
| 156 | |||
| 157 | return true; | ||
| 158 | |||
| 159 | error: | ||
| 160 | EVP_PKEY_free (peer_pkey); | ||
| 161 | OPENSSL_free (ikm); | ||
| 162 | ERR_print_errors_fp (stderr); | ||
| 163 | return false; | ||
| 164 | } | ||
| 165 | |||
| 166 | ssize_t | ||
| 167 | aes_256_cbc_encrypt (struct memory_span out, | ||
| 168 | struct memory_view in, const aes_256_key key, bool salt) | ||
| 169 | { | ||
| 170 | ASSERT (out.size >= AES_IV_SIZE + in.size + AES_BLOCK_SIZE - 1); | ||
| 171 | |||
| 172 | uint8_t *head = out.data; | ||
| 173 | |||
| 174 | uint8_t *iv = NULL; | ||
| 175 | if (salt) { | ||
| 176 | iv = head; | ||
| 177 | if (RAND_bytes (iv, AES_IV_SIZE) != 1) | ||
| 178 | goto error; | ||
| 179 | head += AES_IV_SIZE; | ||
| 180 | } | ||
| 181 | |||
| 182 | if (!EVP_CIPHER_CTX_reset (cipher_ctx)) | ||
| 183 | goto error; | ||
| 184 | |||
| 185 | if (EVP_EncryptInit_ex2 (cipher_ctx, cipher_aes_cbc, key, iv, NULL) != 1) | ||
| 186 | goto error; | ||
| 187 | |||
| 188 | int bytes_written = 0; | ||
| 189 | if (EVP_EncryptUpdate (cipher_ctx, head, &bytes_written, in.data, in.size) != 1) | ||
| 190 | goto error; | ||
| 191 | head += bytes_written; | ||
| 192 | |||
| 193 | if (EVP_EncryptFinal_ex (cipher_ctx, head, &bytes_written) != 1) | ||
| 194 | goto error; | ||
| 195 | head += bytes_written; | ||
| 196 | |||
| 197 | return head - out.data; | ||
| 198 | |||
| 199 | error: | ||
| 200 | ERR_print_errors_fp (stderr); | ||
| 201 | return -1; | ||
| 202 | } | ||
| 203 | |||
| 204 | ssize_t | ||
| 205 | aes_256_cbc_decrypt (struct memory_span out, | ||
| 206 | struct memory_view in, const aes_256_key key, bool salt) | ||
| 207 | { | ||
| 208 | uint8_t *head = out.data; | ||
| 209 | |||
| 210 | const uint8_t *iv = NULL; | ||
| 211 | if (salt) { | ||
| 212 | const uint8_t *iv = in.data; | ||
| 213 | in.data += AES_IV_SIZE; | ||
| 214 | in.size -= AES_IV_SIZE; | ||
| 215 | } | ||
| 216 | |||
| 217 | if (!EVP_CIPHER_CTX_reset (cipher_ctx)) | ||
| 218 | goto error; | ||
| 219 | |||
| 220 | if (EVP_DecryptInit_ex2 (cipher_ctx, cipher_aes_cbc, key, iv, NULL) != 1) | ||
| 221 | goto error; | ||
| 222 | |||
| 223 | int bytes_written = 0; | ||
| 224 | if (EVP_DecryptUpdate (cipher_ctx, head, &bytes_written, in.data, in.size) != 1) | ||
| 225 | goto error; | ||
| 226 | head += bytes_written; | ||
| 227 | |||
| 228 | if (EVP_DecryptFinal_ex (cipher_ctx, head, &bytes_written) != 1) | ||
| 229 | goto error; | ||
| 230 | head += bytes_written; | ||
| 231 | |||
| 232 | return head - out.data; | ||
| 233 | |||
| 234 | error: | ||
| 235 | // ERR_print_errors_fp (stderr); | ||
| 236 | return -1; | ||
| 237 | } | ||
| 238 | |||
| 239 | // crypto.c ends here | ||
