36#include <openssl/crypto.h>
37#include <openssl/evp.h>
38#include <openssl/hmac.h>
45static int n_digest_oneshot(
const EVP_MD* md,
const unsigned char* data,
size_t len,
unsigned char* out,
const char* name) {
46 if (!out || (!data && len > 0)) {
50 const unsigned char* in = data ? data : (
const unsigned char*)
"";
51 if (EVP_Digest(in, len, out, NULL, md, NULL) != 1) {
58int n_digest_md5(
const unsigned char* data,
size_t len,
unsigned char* out) {
62int n_digest_sha1(
const unsigned char* data,
size_t len,
unsigned char* out) {
81#if N_DIGEST_HAVE_BLAKE2B512
90 n_log(
LOG_ERR,
"n_digest_blake2b512: requires OpenSSL 1.1.0+ (EVP_blake2b512)");
105static int n_hmac_oneshot(
const EVP_MD* md,
unsigned int expect_len,
const unsigned char*
key,
size_t key_len,
const unsigned char* data,
size_t data_len,
unsigned char* out,
const char* name) {
106 if (!out || (!
key && key_len > 0) || (!data && data_len > 0)) {
110 if (key_len > INT_MAX) {
111 n_log(
LOG_ERR,
"n_hmac_%s: key length %zu too large", name, key_len);
114 const unsigned char* k =
key ?
key : (
const unsigned char*)
"";
115 const unsigned char* in = data ? data : (
const unsigned char*)
"";
116 unsigned int out_len = 0;
117 const unsigned char* r = HMAC(md, k, (
int)key_len, in, data_len, out, &out_len);
118 if (!r || out_len != expect_len) {
119 n_log(
LOG_ERR,
"n_hmac_%s: HMAC failed (out_len %u)", name, out_len);
125int n_hmac_sha1(
const unsigned char*
key,
size_t key_len,
const unsigned char* data,
size_t data_len,
unsigned char* out) {
129int n_hmac_sha256(
const unsigned char*
key,
size_t key_len,
const unsigned char* data,
size_t data_len,
unsigned char* out) {
133int n_hmac_sha384(
const unsigned char*
key,
size_t key_len,
const unsigned char* data,
size_t data_len,
unsigned char* out) {
137int n_hmac_sha512(
const unsigned char*
key,
size_t key_len,
const unsigned char* data,
size_t data_len,
unsigned char* out) {
144 return CRYPTO_memcmp(a, b, len) == 0 ? 1 : 0;
148static int n_pbkdf2_oneshot(
const EVP_MD* md,
const unsigned char* pass,
size_t pass_len,
const unsigned char* salt,
size_t salt_len,
unsigned int iterations,
unsigned char* out,
size_t out_len,
const char* name) {
149 if (!out || out_len == 0 || iterations == 0 || (!pass && pass_len > 0) || (!salt && salt_len > 0)) {
153 if (pass_len > INT_MAX || salt_len > INT_MAX || out_len > INT_MAX || iterations > (
unsigned int)INT_MAX) {
154 n_log(
LOG_ERR,
"n_pbkdf2_%s: argument too large", name);
157 const char* p = pass ? (
const char*)pass :
"";
158 const unsigned char* s = salt ? salt : (
const unsigned char*)
"";
159 if (PKCS5_PBKDF2_HMAC(p, (
int)pass_len, s, (
int)salt_len, (
int)iterations, md, (
int)out_len, out) != 1) {
160 n_log(
LOG_ERR,
"n_pbkdf2_%s: PKCS5_PBKDF2_HMAC failed", name);
166int n_pbkdf2_hmac_sha1(
const unsigned char* pass,
size_t pass_len,
const unsigned char* salt,
size_t salt_len,
unsigned int iterations,
unsigned char* out,
size_t out_len) {
167 return n_pbkdf2_oneshot(EVP_sha1(), pass, pass_len, salt, salt_len, iterations, out, out_len,
"sha1");
170int n_pbkdf2_hmac_sha256(
const unsigned char* pass,
size_t pass_len,
const unsigned char* salt,
size_t salt_len,
unsigned int iterations,
unsigned char* out,
size_t out_len) {
171 return n_pbkdf2_oneshot(EVP_sha256(), pass, pass_len, salt, salt_len, iterations, out, out_len,
"sha256");
174int n_aead_aes256gcm_encrypt(
const unsigned char*
key,
const unsigned char* iv,
size_t iv_len,
const unsigned char* aad,
size_t aad_len,
const unsigned char* pt,
size_t pt_len,
unsigned char* out_ct,
unsigned char* out_tag) {
178 if (!
key || !iv || iv_len == 0 || !out_ct || !out_tag || (!pt && pt_len > 0) || (!aad && aad_len > 0) || iv_len > INT_MAX || pt_len > INT_MAX || aad_len > INT_MAX) {
179 n_log(
LOG_ERR,
"n_aead_aes256gcm_encrypt: invalid arguments");
182 ctx = EVP_CIPHER_CTX_new();
184 n_log(
LOG_ERR,
"n_aead_aes256gcm_encrypt: EVP_CIPHER_CTX_new failed");
187 if (EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL) != 1 ||
188 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (
int)iv_len, NULL) != 1 ||
189 EVP_EncryptInit_ex(ctx, NULL, NULL,
key, iv) != 1) {
190 n_log(
LOG_ERR,
"n_aead_aes256gcm_encrypt: init failed");
193 if (aad_len > 0 && EVP_EncryptUpdate(ctx, NULL, &outl, aad, (
int)aad_len) != 1) {
197 if (pt_len > 0 && EVP_EncryptUpdate(ctx, out_ct, &outl, pt, (
int)pt_len) != 1) {
198 n_log(
LOG_ERR,
"n_aead_aes256gcm_encrypt: encrypt failed");
201 if (EVP_EncryptFinal_ex(ctx, out_ct + outl, &outl) != 1 ||
203 n_log(
LOG_ERR,
"n_aead_aes256gcm_encrypt: finalize failed");
208 EVP_CIPHER_CTX_free(ctx);
212int n_aead_aes256gcm_decrypt(
const unsigned char*
key,
const unsigned char* iv,
size_t iv_len,
const unsigned char* aad,
size_t aad_len,
const unsigned char* ct,
size_t ct_len,
const unsigned char* tag,
unsigned char* out_pt) {
217 if (!
key || !iv || iv_len == 0 || !tag || !out_pt || (!ct && ct_len > 0) || (!aad && aad_len > 0) || iv_len > INT_MAX || ct_len > INT_MAX || aad_len > INT_MAX) {
218 n_log(
LOG_ERR,
"n_aead_aes256gcm_decrypt: invalid arguments");
221 ctx = EVP_CIPHER_CTX_new();
223 n_log(
LOG_ERR,
"n_aead_aes256gcm_decrypt: EVP_CIPHER_CTX_new failed");
226 if (EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL) != 1 ||
227 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (
int)iv_len, NULL) != 1 ||
228 EVP_DecryptInit_ex(ctx, NULL, NULL,
key, iv) != 1) {
229 n_log(
LOG_ERR,
"n_aead_aes256gcm_decrypt: init failed");
232 if (aad_len > 0 && EVP_DecryptUpdate(ctx, NULL, &outl, aad, (
int)aad_len) != 1) {
236 if (ct_len > 0 && EVP_DecryptUpdate(ctx, out_pt, &outl, ct, (
int)ct_len) != 1) {
237 n_log(
LOG_ERR,
"n_aead_aes256gcm_decrypt: decrypt failed");
242 n_log(
LOG_ERR,
"n_aead_aes256gcm_decrypt: set tag failed");
246 if (EVP_DecryptFinal_ex(ctx, out_pt + outl, &outl) > 0)
249 EVP_CIPHER_CTX_free(ctx);
259#if N_AEAD_HAVE_CHACHA20POLY1305
260int n_aead_chacha20poly1305_encrypt(
const unsigned char*
key,
const unsigned char* iv,
size_t iv_len,
const unsigned char* aad,
size_t aad_len,
const unsigned char* pt,
size_t pt_len,
unsigned char* out_ct,
unsigned char* out_tag) {
264 if (!
key || !iv || iv_len == 0 || !out_ct || !out_tag || (!pt && pt_len > 0) || (!aad && aad_len > 0) || iv_len > INT_MAX || pt_len > INT_MAX || aad_len > INT_MAX) {
265 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_encrypt: invalid arguments");
268 ctx = EVP_CIPHER_CTX_new();
270 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_encrypt: EVP_CIPHER_CTX_new failed");
273 if (EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL, NULL) != 1 ||
274 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, (
int)iv_len, NULL) != 1 ||
275 EVP_EncryptInit_ex(ctx, NULL, NULL,
key, iv) != 1) {
276 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_encrypt: init failed");
279 if (aad_len > 0 && EVP_EncryptUpdate(ctx, NULL, &outl, aad, (
int)aad_len) != 1) {
280 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_encrypt: AAD failed");
283 if (pt_len > 0 && EVP_EncryptUpdate(ctx, out_ct, &outl, pt, (
int)pt_len) != 1) {
284 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_encrypt: encrypt failed");
287 if (EVP_EncryptFinal_ex(ctx, out_ct + outl, &outl) != 1 ||
289 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_encrypt: finalize failed");
294 EVP_CIPHER_CTX_free(ctx);
298int n_aead_chacha20poly1305_decrypt(
const unsigned char*
key,
const unsigned char* iv,
size_t iv_len,
const unsigned char* aad,
size_t aad_len,
const unsigned char* ct,
size_t ct_len,
const unsigned char* tag,
unsigned char* out_pt) {
303 if (!
key || !iv || iv_len == 0 || !tag || !out_pt || (!ct && ct_len > 0) || (!aad && aad_len > 0) || iv_len > INT_MAX || ct_len > INT_MAX || aad_len > INT_MAX) {
304 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_decrypt: invalid arguments");
307 ctx = EVP_CIPHER_CTX_new();
309 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_decrypt: EVP_CIPHER_CTX_new failed");
312 if (EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL, NULL) != 1 ||
313 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, (
int)iv_len, NULL) != 1 ||
314 EVP_DecryptInit_ex(ctx, NULL, NULL,
key, iv) != 1) {
315 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_decrypt: init failed");
318 if (aad_len > 0 && EVP_DecryptUpdate(ctx, NULL, &outl, aad, (
int)aad_len) != 1) {
319 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_decrypt: AAD failed");
322 if (ct_len > 0 && EVP_DecryptUpdate(ctx, out_pt, &outl, ct, (
int)ct_len) != 1) {
323 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_decrypt: decrypt failed");
328 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_decrypt: set tag failed");
332 if (EVP_DecryptFinal_ex(ctx, out_pt + outl, &outl) > 0)
335 EVP_CIPHER_CTX_free(ctx);
339int n_aead_chacha20poly1305_encrypt(
const unsigned char*
key,
const unsigned char* iv,
size_t iv_len,
const unsigned char* aad,
size_t aad_len,
const unsigned char* pt,
size_t pt_len,
unsigned char* out_ct,
unsigned char* out_tag) {
349 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_encrypt: requires OpenSSL 1.1.0+ (EVP_chacha20_poly1305)");
353int n_aead_chacha20poly1305_decrypt(
const unsigned char*
key,
const unsigned char* iv,
size_t iv_len,
const unsigned char* aad,
size_t aad_len,
const unsigned char* ct,
size_t ct_len,
const unsigned char* tag,
unsigned char* out_pt) {
363 n_log(
LOG_ERR,
"n_aead_chacha20poly1305_decrypt: requires OpenSSL 1.1.0+ (EVP_chacha20_poly1305)");
370#if N_KDF_HAVE_EVP_KDF
371#include <openssl/core_names.h>
372#include <openssl/kdf.h>
373#include <openssl/params.h>
375int n_kdf_hkdf_sha256(
const unsigned char* ikm,
size_t ikm_len,
const unsigned char* salt,
size_t salt_len,
const unsigned char* info,
size_t info_len,
unsigned char* out,
size_t out_len) {
376 if (!out || out_len == 0 || (!ikm && ikm_len > 0) || (!salt && salt_len > 0) || (!info && info_len > 0)) {
381 EVP_KDF* kdf = EVP_KDF_fetch(NULL,
"HKDF", NULL);
382 EVP_KDF_CTX* kctx = NULL;
383 OSSL_PARAM params[5];
385 char digest[] =
"SHA256";
387 n_log(
LOG_ERR,
"n_kdf_hkdf_sha256: EVP_KDF_fetch(HKDF) failed");
390 kctx = EVP_KDF_CTX_new(kdf);
391 if (!kctx)
goto done;
392 params[i++] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digest, 0);
393 params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, (
void*)ikm, ikm_len);
394 params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, (
void*)salt, salt_len);
395 params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, (
void*)info, info_len);
396 params[i] = OSSL_PARAM_construct_end();
397 if (EVP_KDF_derive(kctx, out, out_len, params) == 1)
400 n_log(
LOG_ERR,
"n_kdf_hkdf_sha256: EVP_KDF_derive failed");
402 EVP_KDF_CTX_free(kctx);
407int n_kdf_scrypt(
const unsigned char* pass,
size_t pass_len,
const unsigned char* salt,
size_t salt_len, uint64_t n, uint32_t r, uint32_t p, uint64_t maxmem,
unsigned char* out,
size_t out_len) {
408 if (!out || out_len == 0 || (!pass && pass_len > 0) || (!salt && salt_len > 0)) {
413 EVP_KDF* kdf = EVP_KDF_fetch(NULL,
"SCRYPT", NULL);
414 EVP_KDF_CTX* kctx = NULL;
415 OSSL_PARAM params[7];
418 n_log(
LOG_ERR,
"n_kdf_scrypt: EVP_KDF_fetch(SCRYPT) failed");
421 kctx = EVP_KDF_CTX_new(kdf);
422 if (!kctx)
goto done;
423 params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, (
void*)pass, pass_len);
424 params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, (
void*)salt, salt_len);
425 params[i++] = OSSL_PARAM_construct_uint64(OSSL_KDF_PARAM_SCRYPT_N, &n);
426 params[i++] = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_SCRYPT_R, &r);
427 params[i++] = OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_SCRYPT_P, &p);
428 params[i++] = OSSL_PARAM_construct_uint64(OSSL_KDF_PARAM_SCRYPT_MAXMEM, &maxmem);
429 params[i] = OSSL_PARAM_construct_end();
430 if (EVP_KDF_derive(kctx, out, out_len, params) == 1)
435 EVP_KDF_CTX_free(kctx);
440int n_kdf_hkdf_sha256(
const unsigned char* ikm,
size_t ikm_len,
const unsigned char* salt,
size_t salt_len,
const unsigned char* info,
size_t info_len,
unsigned char* out,
size_t out_len) {
449 n_log(
LOG_ERR,
"n_kdf_hkdf_sha256: requires OpenSSL 3.0+ (EVP_KDF)");
452int n_kdf_scrypt(
const unsigned char* pass,
size_t pass_len,
const unsigned char* salt,
size_t salt_len, uint64_t n, uint32_t r, uint32_t p, uint64_t maxmem,
unsigned char* out,
size_t out_len) {
463 n_log(
LOG_ERR,
"n_kdf_scrypt: requires OpenSSL 3.0+ (EVP_KDF)");
volatile sig_atomic_t done
volatile char key[227]
TRUE while the key is held.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
int n_digest_sha256(const unsigned char *data, size_t len, unsigned char *out)
compute the SHA-256 digest of len bytes of data into out (N_DIGEST_SHA256_LEN bytes).
int n_kdf_scrypt(const unsigned char *pass, size_t pass_len, const unsigned char *salt, size_t salt_len, uint64_t n, uint32_t r, uint32_t p, uint64_t maxmem, unsigned char *out, size_t out_len)
scrypt (RFC 7914) of pass with salt and cost parameters n (CPU/memory, a power of two),...
#define N_DIGEST_SHA512_LEN
output size in bytes of a SHA-512 digest
int n_aead_chacha20poly1305_encrypt(const unsigned char *key, const unsigned char *iv, size_t iv_len, const unsigned char *aad, size_t aad_len, const unsigned char *pt, size_t pt_len, unsigned char *out_ct, unsigned char *out_tag)
ChaCha20-Poly1305 (RFC 8439) authenticated encryption.
int n_digest_sha512(const unsigned char *data, size_t len, unsigned char *out)
compute the SHA-512 digest of len bytes of data into out (N_DIGEST_SHA512_LEN bytes).
int n_kdf_hkdf_sha256(const unsigned char *ikm, size_t ikm_len, const unsigned char *salt, size_t salt_len, const unsigned char *info, size_t info_len, unsigned char *out, size_t out_len)
HKDF-SHA256 (RFC 5869 extract-then-expand) of ikm with the given salt and info into out (out_len byte...
int n_pbkdf2_hmac_sha1(const unsigned char *pass, size_t pass_len, const unsigned char *salt, size_t salt_len, unsigned int iterations, unsigned char *out, size_t out_len)
derive out_len key bytes from pass and salt with PBKDF2-HMAC-SHA1 over iterations rounds (RFC 2898).
#define N_DIGEST_SHA384_LEN
output size in bytes of a SHA-384 digest
int n_hmac_sha256(const unsigned char *key, size_t key_len, const unsigned char *data, size_t data_len, unsigned char *out)
compute HMAC-SHA256 of data under key into out (N_DIGEST_SHA256_LEN bytes).
#define N_AEAD_AES256GCM_TAG_LEN
authentication tag size in bytes for AES-256-GCM
#define N_AEAD_CHACHA20POLY1305_TAG_LEN
authentication tag size in bytes for ChaCha20-Poly1305
int n_aead_aes256gcm_encrypt(const unsigned char *key, const unsigned char *iv, size_t iv_len, const unsigned char *aad, size_t aad_len, const unsigned char *pt, size_t pt_len, unsigned char *out_ct, unsigned char *out_tag)
AES-256-GCM authenticated encryption.
#define N_DIGEST_SHA256_LEN
output size in bytes of a SHA-256 digest
int n_hmac_sha1(const unsigned char *key, size_t key_len, const unsigned char *data, size_t data_len, unsigned char *out)
compute HMAC-SHA1 of data under key into out (N_DIGEST_SHA1_LEN bytes).
int n_digest_sha1(const unsigned char *data, size_t len, unsigned char *out)
compute the SHA-1 digest of len bytes of data into out (N_DIGEST_SHA1_LEN bytes).
#define N_DIGEST_SHA1_LEN
output size in bytes of a SHA-1 digest
int n_pbkdf2_hmac_sha256(const unsigned char *pass, size_t pass_len, const unsigned char *salt, size_t salt_len, unsigned int iterations, unsigned char *out, size_t out_len)
derive out_len key bytes from pass and salt with PBKDF2-HMAC-SHA256 over iterations rounds (RFC 2898)...
int n_digest_consttime_equal(const unsigned char *a, const unsigned char *b, size_t len)
constant-time comparison of two equal-length byte buffers (for verifying MACs/signatures without a ti...
int n_hmac_sha512(const unsigned char *key, size_t key_len, const unsigned char *data, size_t data_len, unsigned char *out)
compute HMAC-SHA512 of data under key into out (N_DIGEST_SHA512_LEN bytes).
int n_digest_sha384(const unsigned char *data, size_t len, unsigned char *out)
compute the SHA-384 digest of len bytes of data into out (N_DIGEST_SHA384_LEN bytes).
int n_hmac_sha384(const unsigned char *key, size_t key_len, const unsigned char *data, size_t data_len, unsigned char *out)
compute HMAC-SHA384 of data under key into out (N_DIGEST_SHA384_LEN bytes).
int n_aead_aes256gcm_decrypt(const unsigned char *key, const unsigned char *iv, size_t iv_len, const unsigned char *aad, size_t aad_len, const unsigned char *ct, size_t ct_len, const unsigned char *tag, unsigned char *out_pt)
AES-256-GCM authenticated decryption.
int n_digest_md5(const unsigned char *data, size_t len, unsigned char *out)
compute the MD5 digest of len bytes of data into out (N_DIGEST_MD5_LEN bytes).
int n_aead_chacha20poly1305_decrypt(const unsigned char *key, const unsigned char *iv, size_t iv_len, const unsigned char *aad, size_t aad_len, const unsigned char *ct, size_t ct_len, const unsigned char *tag, unsigned char *out_pt)
ChaCha20-Poly1305 authenticated decryption.
N_STR * n_digest_sha256_hex(const unsigned char *data, size_t len)
compute the SHA-256 digest of data and return it as a lowercase hex N_STR; free with free_nstr.
int n_digest_blake2b512(const unsigned char *data, size_t len, unsigned char *out)
compute the BLAKE2b-512 digest of len bytes of data into out (N_DIGEST_BLAKE2B512_LEN bytes).
N_STR * n_hex_encode(const unsigned char *data, size_t len)
encode len bytes of data as a lowercase hex N_STR (2*len characters); free with free_nstr.
A box including a string and his lenght.
Common headers and low-level functions & define.
static int n_hmac_oneshot(const EVP_MD *md, unsigned int expect_len, const unsigned char *key, size_t key_len, const unsigned char *data, size_t data_len, unsigned char *out, const char *name)
static int n_digest_oneshot(const EVP_MD *md, const unsigned char *data, size_t len, unsigned char *out, const char *name)
static int n_pbkdf2_oneshot(const EVP_MD *md, const unsigned char *pass, size_t pass_len, const unsigned char *salt, size_t salt_len, unsigned int iterations, unsigned char *out, size_t out_len, const char *name)
Message digests, HMAC, PBKDF2, and AES-256-GCM AEAD over the OpenSSL EVP API.
Hexadecimal encode/decode helpers.
N_STR and string function declaration.