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
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
124static void test_sha256_hex(void) {
125 N_STR* h = n_digest_sha256_hex((const unsigned char*)"abc", 3);
126 if (!h || strcmp(h->data, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") != 0) {
127 n_log(LOG_ERR, "sha256_hex(abc): got '%s'", h ? h->data : "(null)");
128 failures++;
129 }
130 free_nstr(&h);
131
132 /* the empty input has a well-known SHA-256 digest */
133 h = n_digest_sha256_hex((const unsigned char*)"", 0);
134 if (!h || strcmp(h->data, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") != 0) {
135 n_log(LOG_ERR, "sha256_hex(empty): got '%s'", h ? h->data : "(null)");
136 failures++;
137 }
138 free_nstr(&h);
139}
140
141static void test_hmac(void) {
142 const unsigned char* key = (const unsigned char*)"key";
143 const unsigned char* msg = (const unsigned char*)"The quick brown fox jumps over the lazy dog";
144 unsigned char out[N_DIGEST_SHA256_LEN];
145 if (n_hmac_sha256(key, 3, msg, strlen((const char*)msg), out) != 0) {
146 n_log(LOG_ERR, "hmac_sha256 failed");
147 failures++;
148 return;
149 }
150 expect_hex(out, sizeof(out), "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8", "hmac-sha256");
151}
152
153/* HMAC-SHA1/384/512 known-answer vectors (key="key", the pangram message). */
154static void test_hmac_variants(void) {
155 const unsigned char* key = (const unsigned char*)"key";
156 const unsigned char* msg = (const unsigned char*)"The quick brown fox jumps over the lazy dog";
157 size_t klen = 3, mlen = strlen((const char*)msg);
158
159 unsigned char o1[N_DIGEST_SHA1_LEN];
160 if (n_hmac_sha1(key, klen, msg, mlen, o1) != 0)
161 failures++;
162 else
163 expect_hex(o1, sizeof(o1), "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9", "hmac-sha1");
164
165 unsigned char o384[N_DIGEST_SHA384_LEN];
166 if (n_hmac_sha384(key, klen, msg, mlen, o384) != 0)
167 failures++;
168 else
169 expect_hex(o384, sizeof(o384),
170 "d7f4727e2c0b39ae0f1e40cc96f60242d5b7801841cea6fc592c5d3e1ae50700582a96cf35e1e554995fe4e03381c237",
171 "hmac-sha384");
172
173 unsigned char o512[N_DIGEST_SHA512_LEN];
174 if (n_hmac_sha512(key, klen, msg, mlen, o512) != 0)
175 failures++;
176 else
177 expect_hex(o512, sizeof(o512),
178 "b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe86653c73dd248fb82f948a549f7b791a5b41915ee4d1ec3935357e4e2317250d0372afa2ebeeb3a",
179 "hmac-sha512");
180}
181
182/* Constant-time comparison: equal buffers match, a one-byte diff does not. */
183static void test_consttime(void) {
184 const unsigned char a[] = {1, 2, 3, 4, 5};
185 const unsigned char b[] = {1, 2, 3, 4, 5};
186 const unsigned char c[] = {1, 2, 3, 4, 6};
187 if (n_digest_consttime_equal(a, b, sizeof(a)) != 1)
188 failures++;
189 if (n_digest_consttime_equal(a, c, sizeof(a)) != 0)
190 failures++;
191 if (n_digest_consttime_equal(NULL, b, sizeof(a)) != 0) /* NULL never matches */
192 failures++;
193}
194
195/* PBKDF2-HMAC-SHA1 vectors from RFC 6070 and a SHA256 vector (RFC 7914 salt). */
196static void test_pbkdf2(void) {
197 unsigned char out[40];
198
199 /* RFC 6070: P="password" S="salt" c=1 dkLen=20 */
200 if (n_pbkdf2_hmac_sha1((const unsigned char*)"password", 8, (const unsigned char*)"salt", 4, 1, out, 20) != 0)
201 failures++;
202 else
203 expect_hex(out, 20, "0c60c80f961f0e71f3a9b524af6012062fe037a6", "pbkdf2-sha1 c=1");
204
205 /* RFC 6070: same but c=4096 */
206 if (n_pbkdf2_hmac_sha1((const unsigned char*)"password", 8, (const unsigned char*)"salt", 4, 4096, out, 20) != 0)
207 failures++;
208 else
209 expect_hex(out, 20, "4b007901b765489abead49d926f721d065a429c1", "pbkdf2-sha1 c=4096");
210
211 /* PBKDF2-HMAC-SHA256: P="password" S="salt" c=1 dkLen=32 */
212 if (n_pbkdf2_hmac_sha256((const unsigned char*)"password", 8, (const unsigned char*)"salt", 4, 1, out, 32) != 0)
213 failures++;
214 else
215 expect_hex(out, 32, "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b", "pbkdf2-sha256 c=1");
216
217 /* invalid arguments must fail */
218 if (n_pbkdf2_hmac_sha1((const unsigned char*)"p", 1, (const unsigned char*)"s", 1, 0, out, 20) == 0) /* zero iterations */
219 failures++;
220 if (n_pbkdf2_hmac_sha1((const unsigned char*)"p", 1, (const unsigned char*)"s", 1, 1, NULL, 20) == 0) /* NULL out */
221 failures++;
222}
223
224/* AES-256-GCM: NIST test vector (Test Case 14), a round-trip, and tamper detection. */
225static void test_aead(void) {
226 unsigned char key[N_AEAD_AES256GCM_KEY_LEN] = {0};
227 unsigned char iv[12] = {0};
228 unsigned char pt[16] = {0};
229 unsigned char ct[16];
230 unsigned char tag[N_AEAD_AES256GCM_TAG_LEN];
231 unsigned char back[16];
232
233 /* Test Case 14: all-zero key/iv/plaintext */
234 if (n_aead_aes256gcm_encrypt(key, iv, sizeof(iv), NULL, 0, pt, sizeof(pt), ct, tag) != 0) {
235 failures++;
236 } else {
237 expect_hex(ct, sizeof(ct), "cea7403d4d606b6e074ec5d3baf39d18", "aes256gcm ct");
238 expect_hex(tag, sizeof(tag), "d0d1c8a799996bf0265b98b5d48ab919", "aes256gcm tag");
239 }
240
241 /* round-trip with real data and AAD recovers the plaintext */
242 {
243 const unsigned char msg[] = "hello, world!";
244 size_t mlen = sizeof(msg) - 1; /* drop the terminating NUL */
245 unsigned char rk[N_AEAD_AES256GCM_KEY_LEN];
246 unsigned char riv[12];
247 unsigned char rct[16];
248 unsigned char rtag[N_AEAD_AES256GCM_TAG_LEN];
249 unsigned char rback[16];
250 size_t i;
251 for (i = 0; i < sizeof(rk); i++)
252 rk[i] = (unsigned char)(i + 1);
253 for (i = 0; i < sizeof(riv); i++)
254 riv[i] = (unsigned char)(0x40 + i);
255 if (n_aead_aes256gcm_encrypt(rk, riv, sizeof(riv), (const unsigned char*)"aad", 3, msg, mlen, rct, rtag) != 0)
256 failures++;
257 else if (n_aead_aes256gcm_decrypt(rk, riv, sizeof(riv), (const unsigned char*)"aad", 3, rct, mlen, rtag, rback) != 0)
258 failures++;
259 else if (memcmp(msg, rback, mlen) != 0)
260 failures++;
261 /* a flipped tag byte must fail authentication */
262 rtag[0] ^= 0x01;
263 if (n_aead_aes256gcm_decrypt(rk, riv, sizeof(riv), (const unsigned char*)"aad", 3, rct, mlen, rtag, rback) == 0)
264 failures++;
265 }
266
267 /* decrypting Test Case 14 back returns the zero plaintext */
268 if (n_aead_aes256gcm_decrypt(key, iv, sizeof(iv), NULL, 0, ct, sizeof(ct), tag, back) != 0)
269 failures++;
270 else if (memcmp(pt, back, sizeof(pt)) != 0)
271 failures++;
272}
273
274static void test_errors(void) {
275 unsigned char out[N_DIGEST_SHA256_LEN];
276 if (n_digest_sha256(NULL, 4, out) == 0) { /* NULL data with len>0 must fail */
277 n_log(LOG_ERR, "sha256(NULL,4) should fail");
278 failures++;
279 }
280 if (n_digest_sha256((const unsigned char*)"x", 1, NULL) == 0) { /* NULL out must fail */
281 n_log(LOG_ERR, "sha256 with NULL out should fail");
282 failures++;
283 }
284 if (n_hmac_sha256((const unsigned char*)"k", 1, NULL, 4, out) == 0) {
285 n_log(LOG_ERR, "hmac with NULL data,len>0 should fail");
286 failures++;
287 }
288 /* the digest of the empty input is well defined */
289 if (n_digest_sha256(NULL, 0, out) != 0) {
290 n_log(LOG_ERR, "sha256(NULL,0) should succeed");
291 failures++;
292 }
293}
294
295int main(int argc, char** argv) {
297 process_args(argc, argv);
298
299 test_digests();
301 test_hmac();
304 test_pbkdf2();
305 test_aead();
306 test_errors();
307
308 if (failures) {
309 n_log(LOG_ERR, "ex_digest: %d failure(s)", failures);
310 return 1;
311 }
312 n_log(LOG_NOTICE, "ex_digest: all checks passed");
313 return 0;
314}
static int failures
int main(void)
void process_args(int argc, char **argv)
Definition ex_common.c:48
static void test_hmac_variants(void)
Definition ex_digest.c:154
static void test_pbkdf2(void)
Definition ex_digest.c:196
static void test_hmac(void)
Definition ex_digest.c:141
static void test_aead(void)
Definition ex_digest.c:225
static void test_consttime(void)
Definition ex_digest.c:183
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:274
static void test_sha256_hex(void)
Definition ex_digest.c:124
char * key
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_NULL
no log output
Definition n_log.h:46
#define LOG_INFO
informational
Definition n_log.h:82
#define N_DIGEST_MD5_LEN
output size in bytes of an MD5 digest
Definition n_digest.h:46
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_AEAD_AES256GCM_KEY_LEN
key size in bytes for AES-256-GCM
Definition n_digest.h:88
#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
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.