Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_digest.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
27#include "nilorea/n_digest.h"
28
29#ifdef HAVE_OPENSSL
30
31#include "nilorea/n_common.h"
32#include "nilorea/n_hex.h"
33#include "nilorea/n_log.h"
34#include "nilorea/n_str.h"
35
36#include <openssl/crypto.h>
37#include <openssl/evp.h>
38#include <openssl/hmac.h>
39
40#include <limits.h>
41#include <string.h>
42
43/* One-shot EVP digest shared by the public wrappers. A NULL data pointer is
44 * accepted only with len 0 (the digest of the empty input). */
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)) {
47 n_log(LOG_ERR, "n_digest_%s: invalid arguments", name);
48 return -1;
49 }
50 const unsigned char* in = data ? data : (const unsigned char*)"";
51 if (EVP_Digest(in, len, out, NULL, md, NULL) != 1) {
52 n_log(LOG_ERR, "n_digest_%s: EVP_Digest failed", name);
53 return -1;
54 }
55 return 0;
56}
57
58int n_digest_md5(const unsigned char* data, size_t len, unsigned char* out) {
59 return n_digest_oneshot(EVP_md5(), data, len, out, "md5");
60}
61
62int n_digest_sha1(const unsigned char* data, size_t len, unsigned char* out) {
63 return n_digest_oneshot(EVP_sha1(), data, len, out, "sha1");
64}
65
66int n_digest_sha256(const unsigned char* data, size_t len, unsigned char* out) {
67 return n_digest_oneshot(EVP_sha256(), data, len, out, "sha256");
68}
69
70int n_digest_sha384(const unsigned char* data, size_t len, unsigned char* out) {
71 return n_digest_oneshot(EVP_sha384(), data, len, out, "sha384");
72}
73
74int n_digest_sha512(const unsigned char* data, size_t len, unsigned char* out) {
75 return n_digest_oneshot(EVP_sha512(), data, len, out, "sha512");
76}
77
78N_STR* n_digest_sha256_hex(const unsigned char* data, size_t len) {
79 unsigned char md[N_DIGEST_SHA256_LEN];
80 if (n_digest_sha256(data, len, md) != 0) {
81 return NULL;
82 }
83 return n_hex_encode(md, sizeof(md));
84}
85
86/* Shared HMAC one-shot: compute HMAC(md) of data under key into out, validating
87 the produced length against the digest size. Returns 0 on success, -1 on error. */
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)) {
90 n_log(LOG_ERR, "n_hmac_%s: invalid arguments", name);
91 return -1;
92 }
93 if (key_len > INT_MAX) {
94 n_log(LOG_ERR, "n_hmac_%s: key length %zu too large", name, key_len);
95 return -1;
96 }
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);
103 return -1;
104 }
105 return 0;
106}
107
108int n_hmac_sha1(const unsigned char* key, size_t key_len, const unsigned char* data, size_t data_len, unsigned char* out) {
109 return n_hmac_oneshot(EVP_sha1(), N_DIGEST_SHA1_LEN, key, key_len, data, data_len, out, "sha1");
110}
111
112int n_hmac_sha256(const unsigned char* key, size_t key_len, const unsigned char* data, size_t data_len, unsigned char* out) {
113 return n_hmac_oneshot(EVP_sha256(), N_DIGEST_SHA256_LEN, key, key_len, data, data_len, out, "sha256");
114}
115
116int n_hmac_sha384(const unsigned char* key, size_t key_len, const unsigned char* data, size_t data_len, unsigned char* out) {
117 return n_hmac_oneshot(EVP_sha384(), N_DIGEST_SHA384_LEN, key, key_len, data, data_len, out, "sha384");
118}
119
120int n_hmac_sha512(const unsigned char* key, size_t key_len, const unsigned char* data, size_t data_len, unsigned char* out) {
121 return n_hmac_oneshot(EVP_sha512(), N_DIGEST_SHA512_LEN, key, key_len, data, data_len, out, "sha512");
122}
123
124int n_digest_consttime_equal(const unsigned char* a, const unsigned char* b, size_t len) {
125 if (!a || !b)
126 return 0;
127 return CRYPTO_memcmp(a, b, len) == 0 ? 1 : 0;
128}
129
130/* Shared PBKDF2 one-shot over the OpenSSL PKCS5 API, validating argument ranges. */
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)) {
133 n_log(LOG_ERR, "n_pbkdf2_%s: invalid arguments", name);
134 return -1;
135 }
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);
138 return -1;
139 }
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);
144 return -1;
145 }
146 return 0;
147}
148
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");
151}
152
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");
155}
156
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) {
158 EVP_CIPHER_CTX* ctx;
159 int outl = 0;
160 int rc = -1;
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");
163 return -1;
164 }
165 ctx = EVP_CIPHER_CTX_new();
166 if (!ctx) {
167 n_log(LOG_ERR, "n_aead_aes256gcm_encrypt: EVP_CIPHER_CTX_new failed");
168 return -1;
169 }
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");
174 goto end;
175 }
176 if (aad_len > 0 && EVP_EncryptUpdate(ctx, NULL, &outl, aad, (int)aad_len) != 1) {
177 n_log(LOG_ERR, "n_aead_aes256gcm_encrypt: AAD failed");
178 goto end;
179 }
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");
182 goto end;
183 }
184 if (EVP_EncryptFinal_ex(ctx, out_ct + outl, &outl) != 1 ||
185 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, N_AEAD_AES256GCM_TAG_LEN, out_tag) != 1) {
186 n_log(LOG_ERR, "n_aead_aes256gcm_encrypt: finalize failed");
187 goto end;
188 }
189 rc = 0;
190end:
191 EVP_CIPHER_CTX_free(ctx);
192 return rc;
193}
194
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) {
196 EVP_CIPHER_CTX* ctx;
197 unsigned char tagbuf[N_AEAD_AES256GCM_TAG_LEN];
198 int outl = 0;
199 int rc = -1;
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");
202 return -1;
203 }
204 ctx = EVP_CIPHER_CTX_new();
205 if (!ctx) {
206 n_log(LOG_ERR, "n_aead_aes256gcm_decrypt: EVP_CIPHER_CTX_new failed");
207 return -1;
208 }
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");
213 goto end;
214 }
215 if (aad_len > 0 && EVP_DecryptUpdate(ctx, NULL, &outl, aad, (int)aad_len) != 1) {
216 n_log(LOG_ERR, "n_aead_aes256gcm_decrypt: AAD failed");
217 goto end;
218 }
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");
221 goto end;
222 }
223 memcpy(tagbuf, tag, N_AEAD_AES256GCM_TAG_LEN); /* SET_TAG wants a non-const buffer */
224 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, N_AEAD_AES256GCM_TAG_LEN, tagbuf) != 1) {
225 n_log(LOG_ERR, "n_aead_aes256gcm_decrypt: set tag failed");
226 goto end;
227 }
228 /* EVP_DecryptFinal_ex returns >0 only when the authentication tag verifies */
229 if (EVP_DecryptFinal_ex(ctx, out_pt + outl, &outl) > 0)
230 rc = 0;
231end:
232 EVP_CIPHER_CTX_free(ctx);
233 return rc;
234}
235
236#endif /* HAVE_OPENSSL */
char * key
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_ERR
error conditions
Definition n_log.h:76
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).
Definition n_digest.c:66
#define N_DIGEST_SHA512_LEN
output size in bytes of a SHA-512 digest
Definition n_digest.h:54
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).
Definition n_digest.c:74
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).
Definition n_digest.c:149
#define N_DIGEST_SHA384_LEN
output size in bytes of a SHA-384 digest
Definition n_digest.h:52
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).
Definition n_digest.c:112
#define N_AEAD_AES256GCM_TAG_LEN
authentication tag size in bytes for AES-256-GCM
Definition n_digest.h:90
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.
Definition n_digest.c:157
#define N_DIGEST_SHA256_LEN
output size in bytes of a SHA-256 digest
Definition n_digest.h:50
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).
Definition n_digest.c:108
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).
Definition n_digest.c:62
#define N_DIGEST_SHA1_LEN
output size in bytes of a SHA-1 digest
Definition n_digest.h:48
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)...
Definition n_digest.c:153
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...
Definition n_digest.c:124
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).
Definition n_digest.c:120
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).
Definition n_digest.c:70
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).
Definition n_digest.c:116
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.
Definition n_digest.c:195
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).
Definition n_digest.c:58
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.
Definition n_digest.c:78
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.
Definition n_hex.c:48
A box including a string and his lenght.
Definition n_str.h:61
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)
Definition n_digest.c:88
static int n_digest_oneshot(const EVP_MD *md, const unsigned char *data, size_t len, unsigned char *out, const char *name)
Definition n_digest.c:45
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)
Definition n_digest.c:131
Message digests, HMAC, PBKDF2, and AES-256-GCM AEAD over the OpenSSL EVP API.
Hexadecimal encode/decode helpers.
Generic log system.
N_STR and string function declaration.