// crypto.c --- cryptography utilities // // Copyright (c) 2026 Martin Michalec // // SPDX-License-Identifier: MIT // // Commentary: // // Code: #include "crypto.h" #include #include #include #include #include #include #include "common.h" static EVP_PKEY *pkey_local = NULL; static EVP_PKEY_CTX *pkey_derive_ctx = NULL; uint8_t local_pubkey[X25519_PUBKEY_LEN]; static EVP_MD *md_sha1 = NULL; static EVP_MD_CTX *md_sha1_ctx = NULL; static EVP_KDF *hkdf = NULL; static EVP_KDF_CTX *hkdf_ctx = NULL; static EVP_CIPHER *cipher_aes_cbc = NULL; static EVP_CIPHER_CTX *cipher_ctx = NULL; bool crypto_init (void) { EVP_PKEY_CTX *pkey_gen_ctx = EVP_PKEY_CTX_new_from_name (NULL, "X25519", NULL); if (pkey_gen_ctx == NULL) goto error; if (EVP_PKEY_keygen_init (pkey_gen_ctx) <= 0) { EVP_PKEY_CTX_free (pkey_gen_ctx); goto error; } if (EVP_PKEY_keygen (pkey_gen_ctx, &pkey_local) <= 0) { EVP_PKEY_CTX_free (pkey_gen_ctx); goto error; } EVP_PKEY_CTX_free (pkey_gen_ctx); pkey_derive_ctx = EVP_PKEY_CTX_new_from_pkey (NULL, pkey_local, NULL); if (pkey_derive_ctx == NULL) goto error; size_t len; if (EVP_PKEY_get_raw_public_key (pkey_local, local_pubkey, &len) <= 0) goto error; if (len != X25519_PUBKEY_LEN) goto error_pkey_derive_ctx; md_sha1 = EVP_MD_fetch (NULL, "SHA-1", NULL); if (md_sha1 == NULL) goto error_pkey_derive_ctx; md_sha1_ctx = EVP_MD_CTX_new (); if (md_sha1_ctx == NULL) goto error_md_sha1; if (!EVP_DigestInit_ex2 (md_sha1_ctx, md_sha1, NULL)) goto error_md_sha1_ctx; hkdf = EVP_KDF_fetch (NULL, "HKDF", NULL); if (!hkdf) goto error_md_sha1_ctx; hkdf_ctx = EVP_KDF_CTX_new (hkdf); if (!hkdf_ctx) goto error_hkdf; cipher_aes_cbc = EVP_CIPHER_fetch (NULL, "AES-256-CBC", NULL); if (cipher_aes_cbc == NULL) goto error_hkdf_ctx; cipher_ctx = EVP_CIPHER_CTX_new (); if (cipher_ctx == NULL) goto error_cipher_aes_cbc; return true; error_cipher_aes_cbc: EVP_CIPHER_free (cipher_aes_cbc); error_hkdf_ctx: EVP_KDF_CTX_free (hkdf_ctx); error_hkdf: EVP_KDF_free (hkdf); error_pkey_derive_ctx: EVP_PKEY_CTX_free (pkey_derive_ctx); error_md_sha1_ctx: EVP_MD_CTX_free (md_sha1_ctx); error_md_sha1: EVP_MD_free (md_sha1); error: ERR_print_errors_fp (stderr); return false; } ssize_t sha1_hash (uint8_t out[SHA_DIGEST_SIZE], struct memory_view in) { unsigned int length; if (EVP_DigestInit_ex2 (md_sha1_ctx, NULL, NULL) != 1) goto error; if (EVP_DigestUpdate (md_sha1_ctx, in.data, in.size) != 1) goto error; if (EVP_DigestFinal_ex (md_sha1_ctx, out, &length) != 1) goto error; return length; error: ERR_reason_error_string (ERR_get_error ()); return -1; } bool hkdf_sha256 (const uint8_t peer_pubkey[X25519_PUBKEY_LEN], aes_256_key out) { EVP_PKEY *peer_pkey = EVP_PKEY_new_raw_public_key_ex (NULL, "X25519", NULL, peer_pubkey, X25519_PUBKEY_LEN); if (peer_pkey == NULL) goto error; if (EVP_PKEY_derive_init (pkey_derive_ctx) <= 0) goto error; if (EVP_PKEY_derive_set_peer (pkey_derive_ctx, peer_pkey) <= 0) goto error; size_t ikm_len = 0; if (EVP_PKEY_derive (pkey_derive_ctx, NULL, &ikm_len) <= 0) goto error; uint8_t *ikm = OPENSSL_malloc (ikm_len); if (!ikm) goto error; if (EVP_PKEY_derive (pkey_derive_ctx, ikm, &ikm_len) <= 0) goto error; EVP_PKEY_free (peer_pkey); uint8_t salt[] = { 0xcd }; size_t salt_len = sizeof (salt); uint8_t info[] = { 0xcd }; size_t info_len = sizeof (info); OSSL_PARAM params[] = { OSSL_PARAM_construct_utf8_string (OSSL_KDF_PARAM_DIGEST, "SHA256", 0), OSSL_PARAM_construct_octet_string (OSSL_KDF_PARAM_KEY, (void *) ikm, ikm_len), OSSL_PARAM_construct_octet_string (OSSL_KDF_PARAM_SALT, (void *) salt, salt_len), OSSL_PARAM_construct_octet_string (OSSL_KDF_PARAM_INFO, (void *) info, info_len), OSSL_PARAM_construct_end (), }; EVP_KDF_CTX_reset (hkdf_ctx); if (EVP_KDF_derive (hkdf_ctx, out, AES_256_KEY_SIZE, params) <= 0) goto error; OPENSSL_free (ikm); return true; error: EVP_PKEY_free (peer_pkey); OPENSSL_free (ikm); ERR_print_errors_fp (stderr); return false; } ssize_t aes_256_cbc_encrypt (struct memory_span out, struct memory_view in, const aes_256_key key, bool salt) { ASSERT (out.size >= AES_IV_SIZE + in.size + AES_BLOCK_SIZE - 1); uint8_t *head = out.data; uint8_t *iv = NULL; if (salt) { iv = head; if (RAND_bytes (iv, AES_IV_SIZE) != 1) goto error; head += AES_IV_SIZE; } if (!EVP_CIPHER_CTX_reset (cipher_ctx)) goto error; if (EVP_EncryptInit_ex2 (cipher_ctx, cipher_aes_cbc, key, iv, NULL) != 1) goto error; int bytes_written = 0; if (EVP_EncryptUpdate (cipher_ctx, head, &bytes_written, in.data, in.size) != 1) goto error; head += bytes_written; if (EVP_EncryptFinal_ex (cipher_ctx, head, &bytes_written) != 1) goto error; head += bytes_written; return head - out.data; error: ERR_print_errors_fp (stderr); return -1; } ssize_t aes_256_cbc_decrypt (struct memory_span out, struct memory_view in, const aes_256_key key, bool salt) { uint8_t *head = out.data; const uint8_t *iv = NULL; if (salt) { const uint8_t *iv = in.data; in.data += AES_IV_SIZE; in.size -= AES_IV_SIZE; } if (!EVP_CIPHER_CTX_reset (cipher_ctx)) goto error; if (EVP_DecryptInit_ex2 (cipher_ctx, cipher_aes_cbc, key, iv, NULL) != 1) goto error; int bytes_written = 0; if (EVP_DecryptUpdate (cipher_ctx, head, &bytes_written, in.data, in.size) != 1) goto error; head += bytes_written; if (EVP_DecryptFinal_ex (cipher_ctx, head, &bytes_written) != 1) goto error; head += bytes_written; return head - out.data; error: // ERR_print_errors_fp (stderr); return -1; } // crypto.c ends here