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) {
88static 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) {
89 if (!out || (!
key && key_len > 0) || (!data && data_len > 0)) {
93 if (key_len > INT_MAX) {
94 n_log(
LOG_ERR,
"n_hmac_%s: key length %zu too large", name, key_len);
97 const unsigned char* k =
key ?
key : (
const unsigned char*)
"";
98 const unsigned char* in = data ? data : (
const unsigned char*)
"";
99 unsigned int out_len = 0;
100 const unsigned char* r = HMAC(md, k, (
int)key_len, in, data_len, out, &out_len);
101 if (!r || out_len != expect_len) {
102 n_log(
LOG_ERR,
"n_hmac_%s: HMAC failed (out_len %u)", name, out_len);
108int n_hmac_sha1(
const unsigned char*
key,
size_t key_len,
const unsigned char* data,
size_t data_len,
unsigned char* out) {
112int n_hmac_sha256(
const unsigned char*
key,
size_t key_len,
const unsigned char* data,
size_t data_len,
unsigned char* out) {
116int n_hmac_sha384(
const unsigned char*
key,
size_t key_len,
const unsigned char* data,
size_t data_len,
unsigned char* out) {
120int n_hmac_sha512(
const unsigned char*
key,
size_t key_len,
const unsigned char* data,
size_t data_len,
unsigned char* out) {
127 return CRYPTO_memcmp(a, b, len) == 0 ? 1 : 0;
131static 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) {
132 if (!out || out_len == 0 || iterations == 0 || (!pass && pass_len > 0) || (!salt && salt_len > 0)) {
136 if (pass_len > INT_MAX || salt_len > INT_MAX || out_len > INT_MAX || iterations > (
unsigned int)INT_MAX) {
137 n_log(
LOG_ERR,
"n_pbkdf2_%s: argument too large", name);
140 const char* p = pass ? (
const char*)pass :
"";
141 const unsigned char* s = salt ? salt : (
const unsigned char*)
"";
142 if (PKCS5_PBKDF2_HMAC(p, (
int)pass_len, s, (
int)salt_len, (
int)iterations, md, (
int)out_len, out) != 1) {
143 n_log(
LOG_ERR,
"n_pbkdf2_%s: PKCS5_PBKDF2_HMAC failed", name);
149int 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) {
150 return n_pbkdf2_oneshot(EVP_sha1(), pass, pass_len, salt, salt_len, iterations, out, out_len,
"sha1");
153int 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) {
154 return n_pbkdf2_oneshot(EVP_sha256(), pass, pass_len, salt, salt_len, iterations, out, out_len,
"sha256");
157int 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) {
161 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) {
162 n_log(
LOG_ERR,
"n_aead_aes256gcm_encrypt: invalid arguments");
165 ctx = EVP_CIPHER_CTX_new();
167 n_log(
LOG_ERR,
"n_aead_aes256gcm_encrypt: EVP_CIPHER_CTX_new failed");
170 if (EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL) != 1 ||
171 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (
int)iv_len, NULL) != 1 ||
172 EVP_EncryptInit_ex(ctx, NULL, NULL,
key, iv) != 1) {
173 n_log(
LOG_ERR,
"n_aead_aes256gcm_encrypt: init failed");
176 if (aad_len > 0 && EVP_EncryptUpdate(ctx, NULL, &outl, aad, (
int)aad_len) != 1) {
180 if (pt_len > 0 && EVP_EncryptUpdate(ctx, out_ct, &outl, pt, (
int)pt_len) != 1) {
181 n_log(
LOG_ERR,
"n_aead_aes256gcm_encrypt: encrypt failed");
184 if (EVP_EncryptFinal_ex(ctx, out_ct + outl, &outl) != 1 ||
186 n_log(
LOG_ERR,
"n_aead_aes256gcm_encrypt: finalize failed");
191 EVP_CIPHER_CTX_free(ctx);
195int 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) {
200 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) {
201 n_log(
LOG_ERR,
"n_aead_aes256gcm_decrypt: invalid arguments");
204 ctx = EVP_CIPHER_CTX_new();
206 n_log(
LOG_ERR,
"n_aead_aes256gcm_decrypt: EVP_CIPHER_CTX_new failed");
209 if (EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL) != 1 ||
210 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (
int)iv_len, NULL) != 1 ||
211 EVP_DecryptInit_ex(ctx, NULL, NULL,
key, iv) != 1) {
212 n_log(
LOG_ERR,
"n_aead_aes256gcm_decrypt: init failed");
215 if (aad_len > 0 && EVP_DecryptUpdate(ctx, NULL, &outl, aad, (
int)aad_len) != 1) {
219 if (ct_len > 0 && EVP_DecryptUpdate(ctx, out_pt, &outl, ct, (
int)ct_len) != 1) {
220 n_log(
LOG_ERR,
"n_aead_aes256gcm_decrypt: decrypt failed");
225 n_log(
LOG_ERR,
"n_aead_aes256gcm_decrypt: set tag failed");
229 if (EVP_DecryptFinal_ex(ctx, out_pt + outl, &outl) > 0)
232 EVP_CIPHER_CTX_free(ctx);
#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).
#define N_DIGEST_SHA512_LEN
output size in bytes of a SHA-512 digest
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_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
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).
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.
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.