Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_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
26
27#include "nilorea/n_common.h"
28#include "nilorea/n_digest.h"
29#include "nilorea/n_hex.h"
30#include "nilorea/n_log.h"
31#include "nilorea/n_str.h"
32
33#include <stdio.h>
34#include <string.h>
35
36static int failures = 0;
37
38/* Parse the -V LOG_LEVEL verbosity argument. */
39void process_args(int argc, char** argv) {
40 int opt = 0;
41 while ((opt = getopt(argc, argv, "hvV:")) != EOF) {
42 switch (opt) {
43 case 'V':
44 if (!strncmp("LOG_NULL", optarg, 8))
46 else if (!strncmp("LOG_NOTICE", optarg, 10))
48 else if (!strncmp("LOG_INFO", optarg, 8))
50 else if (!strncmp("LOG_ERR", optarg, 7))
52 else if (!strncmp("LOG_DEBUG", optarg, 9))
54 else {
55 fprintf(stderr, "Unknown log level %s\n", optarg);
56 exit(1);
57 }
58 break;
59 case 'v':
60 fprintf(stderr, "ex_digest\n");
61 exit(1);
62 case 'h':
63 default:
64 fprintf(stderr, "usage: %s [-V LOG_LEVEL]\n", argv[0]);
65 break;
66 }
67 }
68}
69
70/* Hex-encode a raw digest and compare it to the expected lowercase hex string. */
71static void expect_hex(const unsigned char* digest, size_t len, const char* want, const char* label) {
72 N_STR* hex = n_hex_encode(digest, len);
73 if (!hex) {
74 n_log(LOG_ERR, "%s: hex encode failed", label);
75 failures++;
76 return;
77 }
78 if (strcmp(hex->data, want) != 0) {
79 n_log(LOG_ERR, "%s: got '%s' expected '%s'", label, hex->data, want);
80 failures++;
81 }
82 free_nstr(&hex);
83}
84
85static void test_digests(void) {
86 const unsigned char* abc = (const unsigned char*)"abc";
87 size_t n = 3;
88
89 unsigned char md5[N_DIGEST_MD5_LEN];
90 if (n_digest_md5(abc, n, md5) != 0)
91 failures++;
92 else
93 expect_hex(md5, sizeof(md5), "900150983cd24fb0d6963f7d28e17f72", "md5(abc)");
94
95 unsigned char sha1[N_DIGEST_SHA1_LEN];
96 if (n_digest_sha1(abc, n, sha1) != 0)
97 failures++;
98 else
99 expect_hex(sha1, sizeof(sha1), "a9993e364706816aba3e25717850c26c9cd0d89d", "sha1(abc)");
100
101 unsigned char sha256[N_DIGEST_SHA256_LEN];
102 if (n_digest_sha256(abc, n, sha256) != 0)
103 failures++;
104 else
105 expect_hex(sha256, sizeof(sha256), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", "sha256(abc)");
106
107 unsigned char sha384[N_DIGEST_SHA384_LEN];
108 if (n_digest_sha384(abc, n, sha384) != 0)
109 failures++;
110 else
111 expect_hex(sha384, sizeof(sha384),
112 "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
113 "sha384(abc)");
114
115 unsigned char sha512[N_DIGEST_SHA512_LEN];
116 if (n_digest_sha512(abc, n, sha512) != 0)
117 failures++;
118 else
119 expect_hex(sha512, sizeof(sha512),
120 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
121 "sha512(abc)");
122
123/* BLAKE2b-512 needs OpenSSL 1.1.0. On the OpenSSL 1.0.2 this library still
124 supports, the entry point is a logged failure, so check the vector only where
125 the primitive exists. The guard is a #if rather than an if so the skipped
126 builds do not carry the vectors as unreachable code. */
127#if N_DIGEST_HAVE_BLAKE2B512
128 unsigned char b2b[N_DIGEST_BLAKE2B512_LEN];
129 if (n_digest_blake2b512(abc, n, b2b) != 0)
130 failures++;
131 else
132 expect_hex(b2b, sizeof(b2b),
133 "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923",
134 "blake2b512(abc)");
135#else
136 n_log(LOG_NOTICE, "blake2b512: skipped, this OpenSSL does not provide it");
137#endif
138}
139
140static void test_sha256_hex(void) {
141 N_STR* h = n_digest_sha256_hex((const unsigned char*)"abc", 3);
142 if (!h || strcmp(h->data, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") != 0) {
143 n_log(LOG_ERR, "sha256_hex(abc): got '%s'", h ? h->data : "(null)");
144 failures++;
145 }
146 free_nstr(&h);
147
148 /* the empty input has a well-known SHA-256 digest */
149 h = n_digest_sha256_hex((const unsigned char*)"", 0);
150 if (!h || strcmp(h->data, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") != 0) {
151 n_log(LOG_ERR, "sha256_hex(empty): got '%s'", h ? h->data : "(null)");
152 failures++;
153 }
154 free_nstr(&h);
155}
156
157static void test_hmac(void) {
158 const unsigned char* key = (const unsigned char*)"key";
159 const unsigned char* msg = (const unsigned char*)"The quick brown fox jumps over the lazy dog";
160 unsigned char out[N_DIGEST_SHA256_LEN];
161 if (n_hmac_sha256(key, 3, msg, strlen((const char*)msg), out) != 0) {
162 n_log(LOG_ERR, "hmac_sha256 failed");
163 failures++;
164 return;
165 }
166 expect_hex(out, sizeof(out), "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8", "hmac-sha256");
167}
168
169/* HMAC-SHA1/384/512 known-answer vectors (key="key", the pangram message). */
170static void test_hmac_variants(void) {
171 const unsigned char* key = (const unsigned char*)"key";
172 const unsigned char* msg = (const unsigned char*)"The quick brown fox jumps over the lazy dog";
173 size_t klen = 3, mlen = strlen((const char*)msg);
174
175 unsigned char o1[N_DIGEST_SHA1_LEN];
176 if (n_hmac_sha1(key, klen, msg, mlen, o1) != 0)
177 failures++;
178 else
179 expect_hex(o1, sizeof(o1), "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9", "hmac-sha1");
180
181 unsigned char o384[N_DIGEST_SHA384_LEN];
182 if (n_hmac_sha384(key, klen, msg, mlen, o384) != 0)
183 failures++;
184 else
185 expect_hex(o384, sizeof(o384),
186 "d7f4727e2c0b39ae0f1e40cc96f60242d5b7801841cea6fc592c5d3e1ae50700582a96cf35e1e554995fe4e03381c237",
187 "hmac-sha384");
188
189 unsigned char o512[N_DIGEST_SHA512_LEN];
190 if (n_hmac_sha512(key, klen, msg, mlen, o512) != 0)
191 failures++;
192 else
193 expect_hex(o512, sizeof(o512),
194 "b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe86653c73dd248fb82f948a549f7b791a5b41915ee4d1ec3935357e4e2317250d0372afa2ebeeb3a",
195 "hmac-sha512");
196}
197
198/* Constant-time comparison: equal buffers match, a one-byte diff does not. */
199static void test_consttime(void) {
200 const unsigned char a[] = {1, 2, 3, 4, 5};
201 const unsigned char b[] = {1, 2, 3, 4, 5};
202 const unsigned char c[] = {1, 2, 3, 4, 6};
203 if (n_digest_consttime_equal(a, b, sizeof(a)) != 1)
204 failures++;
205 if (n_digest_consttime_equal(a, c, sizeof(a)) != 0)
206 failures++;
207 if (n_digest_consttime_equal(NULL, b, sizeof(a)) != 0) /* NULL never matches */
208 failures++;
209}
210
211/* PBKDF2-HMAC-SHA1 vectors from RFC 6070 and a SHA256 vector (RFC 7914 salt). */
212static void test_pbkdf2(void) {
213 unsigned char out[40];
214
215 /* RFC 6070: P="password" S="salt" c=1 dkLen=20 */
216 if (n_pbkdf2_hmac_sha1((const unsigned char*)"password", 8, (const unsigned char*)"salt", 4, 1, out, 20) != 0)
217 failures++;
218 else
219 expect_hex(out, 20, "0c60c80f961f0e71f3a9b524af6012062fe037a6", "pbkdf2-sha1 c=1");
220
221 /* RFC 6070: same but c=4096 */
222 if (n_pbkdf2_hmac_sha1((const unsigned char*)"password", 8, (const unsigned char*)"salt", 4, 4096, out, 20) != 0)
223 failures++;
224 else
225 expect_hex(out, 20, "4b007901b765489abead49d926f721d065a429c1", "pbkdf2-sha1 c=4096");
226
227 /* PBKDF2-HMAC-SHA256: P="password" S="salt" c=1 dkLen=32 */
228 if (n_pbkdf2_hmac_sha256((const unsigned char*)"password", 8, (const unsigned char*)"salt", 4, 1, out, 32) != 0)
229 failures++;
230 else
231 expect_hex(out, 32, "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b", "pbkdf2-sha256 c=1");
232
233 /* invalid arguments must fail */
234 if (n_pbkdf2_hmac_sha1((const unsigned char*)"p", 1, (const unsigned char*)"s", 1, 0, out, 20) == 0) /* zero iterations */
235 failures++;
236 if (n_pbkdf2_hmac_sha1((const unsigned char*)"p", 1, (const unsigned char*)"s", 1, 1, NULL, 20) == 0) /* NULL out */
237 failures++;
238}
239
240/* AES-256-GCM: NIST test vector (Test Case 14), a round-trip, and tamper detection. */
241static void test_aead(void) {
242 unsigned char key[N_AEAD_AES256GCM_KEY_LEN] = {0};
243 unsigned char iv[12] = {0};
244 unsigned char pt[16] = {0};
245 unsigned char ct[16];
246 unsigned char tag[N_AEAD_AES256GCM_TAG_LEN];
247 unsigned char back[16];
248
249 /* Test Case 14: all-zero key/iv/plaintext */
250 if (n_aead_aes256gcm_encrypt(key, iv, sizeof(iv), NULL, 0, pt, sizeof(pt), ct, tag) != 0) {
251 failures++;
252 } else {
253 expect_hex(ct, sizeof(ct), "cea7403d4d606b6e074ec5d3baf39d18", "aes256gcm ct");
254 expect_hex(tag, sizeof(tag), "d0d1c8a799996bf0265b98b5d48ab919", "aes256gcm tag");
255 }
256
257 /* round-trip with real data and AAD recovers the plaintext */
258 {
259 const unsigned char msg[] = "hello, world!";
260 size_t mlen = sizeof(msg) - 1; /* drop the terminating NUL */
261 unsigned char rk[N_AEAD_AES256GCM_KEY_LEN];
262 unsigned char riv[12];
263 unsigned char rct[16];
264 unsigned char rtag[N_AEAD_AES256GCM_TAG_LEN];
265 unsigned char rback[16];
266 size_t i;
267 for (i = 0; i < sizeof(rk); i++)
268 rk[i] = (unsigned char)(i + 1);
269 for (i = 0; i < sizeof(riv); i++)
270 riv[i] = (unsigned char)(0x40 + i);
271 if (n_aead_aes256gcm_encrypt(rk, riv, sizeof(riv), (const unsigned char*)"aad", 3, msg, mlen, rct, rtag) != 0)
272 failures++;
273 else if (n_aead_aes256gcm_decrypt(rk, riv, sizeof(riv), (const unsigned char*)"aad", 3, rct, mlen, rtag, rback) != 0)
274 failures++;
275 else if (memcmp(msg, rback, mlen) != 0)
276 failures++;
277 /* a flipped tag byte must fail authentication */
278 rtag[0] ^= 0x01;
279 if (n_aead_aes256gcm_decrypt(rk, riv, sizeof(riv), (const unsigned char*)"aad", 3, rct, mlen, rtag, rback) == 0)
280 failures++;
281 }
282
283 /* decrypting Test Case 14 back returns the zero plaintext */
284 if (n_aead_aes256gcm_decrypt(key, iv, sizeof(iv), NULL, 0, ct, sizeof(ct), tag, back) != 0)
285 failures++;
286 else if (memcmp(pt, back, sizeof(pt)) != 0)
287 failures++;
288}
289
290static void test_aead_chacha(void) {
291#if !N_AEAD_HAVE_CHACHA20POLY1305
292 /* ChaCha20-Poly1305 needs OpenSSL 1.1.0 */
293 n_log(LOG_NOTICE, "chacha20poly1305: skipped, this OpenSSL does not provide it");
294#else
295 unsigned char key[N_AEAD_CHACHA20POLY1305_KEY_LEN] = {0};
296 unsigned char iv[12] = {0};
297 unsigned char pt[16] = {0};
298 unsigned char ct[16];
299 unsigned char tag[N_AEAD_CHACHA20POLY1305_TAG_LEN];
300 unsigned char back[16];
301
302 /* all-zero key/iv/plaintext, no AAD (RFC 8439 cipher) */
303 if (n_aead_chacha20poly1305_encrypt(key, iv, sizeof(iv), NULL, 0, pt, sizeof(pt), ct, tag) != 0) {
304 failures++;
305 } else {
306 expect_hex(ct, sizeof(ct), "9f07e7be5551387a98ba977c732d080d", "chacha20poly1305 ct");
307 expect_hex(tag, sizeof(tag), "c34a88047320f52aa2c6683ef8084d2f", "chacha20poly1305 tag");
308
309 /* decrypting the same vector back returns the zero plaintext */
310 if (n_aead_chacha20poly1305_decrypt(key, iv, sizeof(iv), NULL, 0, ct, sizeof(ct), tag, back) != 0)
311 failures++;
312 else if (memcmp(pt, back, sizeof(pt)) != 0)
313 failures++;
314 }
315
316 /* round-trip with AAD recovers the plaintext; a flipped tag fails auth */
317 {
318 const unsigned char msg[] = "hello, chacha!";
319 size_t mlen = sizeof(msg) - 1;
320 unsigned char rk[N_AEAD_CHACHA20POLY1305_KEY_LEN];
321 unsigned char riv[12];
322 unsigned char rct[16];
323 unsigned char rtag[N_AEAD_CHACHA20POLY1305_TAG_LEN];
324 unsigned char rback[16];
325 size_t i;
326 for (i = 0; i < sizeof(rk); i++)
327 rk[i] = (unsigned char)(i + 1);
328 for (i = 0; i < sizeof(riv); i++)
329 riv[i] = (unsigned char)(0x40 + i);
330 if (n_aead_chacha20poly1305_encrypt(rk, riv, sizeof(riv), (const unsigned char*)"aad", 3, msg, mlen, rct, rtag) != 0)
331 failures++;
332 else if (n_aead_chacha20poly1305_decrypt(rk, riv, sizeof(riv), (const unsigned char*)"aad", 3, rct, mlen, rtag, rback) != 0)
333 failures++;
334 else if (memcmp(msg, rback, mlen) != 0)
335 failures++;
336 rtag[0] ^= 0x01;
337 if (n_aead_chacha20poly1305_decrypt(rk, riv, sizeof(riv), (const unsigned char*)"aad", 3, rct, mlen, rtag, rback) == 0)
338 failures++;
339 }
340#endif
341}
342
343static void test_errors(void) {
344 unsigned char out[N_DIGEST_SHA256_LEN];
345 if (n_digest_sha256(NULL, 4, out) == 0) { /* NULL data with len>0 must fail */
346 n_log(LOG_ERR, "sha256(NULL,4) should fail");
347 failures++;
348 }
349 if (n_digest_sha256((const unsigned char*)"x", 1, NULL) == 0) { /* NULL out must fail */
350 n_log(LOG_ERR, "sha256 with NULL out should fail");
351 failures++;
352 }
353 if (n_hmac_sha256((const unsigned char*)"k", 1, NULL, 4, out) == 0) {
354 n_log(LOG_ERR, "hmac with NULL data,len>0 should fail");
355 failures++;
356 }
357 /* the digest of the empty input is well defined */
358 if (n_digest_sha256(NULL, 0, out) != 0) {
359 n_log(LOG_ERR, "sha256(NULL,0) should succeed");
360 failures++;
361 }
362}
363
364static void test_kdf(void) {
365#if !N_KDF_HAVE_EVP_KDF
366 /* HKDF and scrypt go through the EVP_KDF provider API, OpenSSL 3.0 and up */
367 n_log(LOG_NOTICE, "hkdf/scrypt: skipped, this OpenSSL does not provide EVP_KDF");
368#else
369 /* RFC 5869 HKDF-SHA256 test case 1 */
370 unsigned char ikm[22], salt[13], info[10], okm[42];
371 memset(ikm, 0x0b, sizeof(ikm));
372 for (size_t i = 0; i < sizeof(salt); i++) salt[i] = (unsigned char)i;
373 for (size_t i = 0; i < sizeof(info); i++) info[i] = (unsigned char)(0xf0 + i);
374 if (n_kdf_hkdf_sha256(ikm, sizeof(ikm), salt, sizeof(salt), info, sizeof(info), okm, sizeof(okm)) != 0)
375 failures++;
376 else
377 expect_hex(okm, sizeof(okm),
378 "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865",
379 "hkdf-sha256 (rfc5869 tc1)");
380
381 /* RFC 7914 scrypt(P="", S="", N=16, r=1, p=1, dkLen=64) */
382 unsigned char dk[64];
383 if (n_kdf_scrypt((const unsigned char*)"", 0, (const unsigned char*)"", 0, 16, 1, 1, (uint64_t)1 << 24, dk, sizeof(dk)) != 0)
384 failures++;
385 else
386 expect_hex(dk, sizeof(dk),
387 "77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906",
388 "scrypt (rfc7914)");
389#endif
390}
391
392int main(int argc, char** argv) {
394 process_args(argc, argv);
395
396 test_digests();
398 test_hmac();
401 test_pbkdf2();
402 test_kdf();
403 test_aead();
405 test_errors();
406
407 if (failures) {
408 n_log(LOG_ERR, "ex_digest: %d failure(s)", failures);
409 return 1;
410 }
411 n_log(LOG_NOTICE, "ex_digest: all checks passed");
412 return 0;
413}
void process_args(int argc, char **argv)
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void test_hmac_variants(void)
Definition ex_digest.c:170
static void test_pbkdf2(void)
Definition ex_digest.c:212
static void test_kdf(void)
Definition ex_digest.c:364
static void test_hmac(void)
Definition ex_digest.c:157
static void test_aead_chacha(void)
Definition ex_digest.c:290
static void test_aead(void)
Definition ex_digest.c:241
static void test_consttime(void)
Definition ex_digest.c:199
static void expect_hex(const unsigned char *digest, size_t len, const char *want, const char *label)
Definition ex_digest.c:71
static void test_digests(void)
Definition ex_digest.c:85
static void test_errors(void)
Definition ex_digest.c:343
static void test_sha256_hex(void)
Definition ex_digest.c:140
volatile char key[227]
TRUE while the key is held.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_DEBUG
debug-level messages
Definition n_log.h:108
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
#define LOG_NULL
no log output
Definition n_log.h:70
#define LOG_INFO
informational
Definition n_log.h:106
#define N_DIGEST_MD5_LEN
output size in bytes of an MD5 digest
Definition n_digest.h:65
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
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),...
Definition n_digest.c:452
#define N_AEAD_AES256GCM_KEY_LEN
key size in bytes for AES-256-GCM
Definition n_digest.h:111
#define N_AEAD_CHACHA20POLY1305_KEY_LEN
key size in bytes for ChaCha20-Poly1305
Definition n_digest.h:121
#define N_DIGEST_SHA512_LEN
output size in bytes of a SHA-512 digest
Definition n_digest.h:73
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.
Definition n_digest.c:339
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
#define N_DIGEST_BLAKE2B512_LEN
BLAKE2b-512 digest length in bytes.
Definition n_digest.h:75
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...
Definition n_digest.c:440
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:166
#define N_DIGEST_SHA384_LEN
output size in bytes of a SHA-384 digest
Definition n_digest.h:71
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:129
#define N_AEAD_AES256GCM_TAG_LEN
authentication tag size in bytes for AES-256-GCM
Definition n_digest.h:113
#define N_AEAD_CHACHA20POLY1305_TAG_LEN
authentication tag size in bytes for ChaCha20-Poly1305
Definition n_digest.h:123
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:174
#define N_DIGEST_SHA256_LEN
output size in bytes of a SHA-256 digest
Definition n_digest.h:69
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:125
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:67
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:170
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:141
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:137
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:133
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:212
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
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.
Definition n_digest.c:353
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:95
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).
Definition n_digest.c:86
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
char * data
the string
Definition n_str.h:63
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:203
A box including a string and his lenght.
Definition n_str.h:61
Common headers and low-level functions & define.
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.