41 while ((opt = getopt(argc, argv,
"hvV:")) != EOF) {
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))
55 fprintf(stderr,
"Unknown log level %s\n", optarg);
60 fprintf(stderr,
"ex_digest\n");
64 fprintf(stderr,
"usage: %s [-V LOG_LEVEL]\n", argv[0]);
71static void expect_hex(
const unsigned char* digest,
size_t len,
const char* want,
const char* label) {
78 if (strcmp(hex->
data, want) != 0) {
86 const unsigned char* abc = (
const unsigned char*)
"abc";
93 expect_hex(md5,
sizeof(md5),
"900150983cd24fb0d6963f7d28e17f72",
"md5(abc)");
99 expect_hex(sha1,
sizeof(sha1),
"a9993e364706816aba3e25717850c26c9cd0d89d",
"sha1(abc)");
105 expect_hex(sha256,
sizeof(sha256),
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
"sha256(abc)");
112 "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
120 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
127#if N_DIGEST_HAVE_BLAKE2B512
133 "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923",
136 n_log(
LOG_NOTICE,
"blake2b512: skipped, this OpenSSL does not provide it");
142 if (!h || strcmp(h->
data,
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") != 0) {
150 if (!h || strcmp(h->
data,
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") != 0) {
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";
166 expect_hex(out,
sizeof(out),
"f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8",
"hmac-sha256");
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);
179 expect_hex(o1,
sizeof(o1),
"de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9",
"hmac-sha1");
186 "d7f4727e2c0b39ae0f1e40cc96f60242d5b7801841cea6fc592c5d3e1ae50700582a96cf35e1e554995fe4e03381c237",
194 "b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe86653c73dd248fb82f948a549f7b791a5b41915ee4d1ec3935357e4e2317250d0372afa2ebeeb3a",
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};
213 unsigned char out[40];
216 if (
n_pbkdf2_hmac_sha1((
const unsigned char*)
"password", 8, (
const unsigned char*)
"salt", 4, 1, out, 20) != 0)
219 expect_hex(out, 20,
"0c60c80f961f0e71f3a9b524af6012062fe037a6",
"pbkdf2-sha1 c=1");
222 if (
n_pbkdf2_hmac_sha1((
const unsigned char*)
"password", 8, (
const unsigned char*)
"salt", 4, 4096, out, 20) != 0)
225 expect_hex(out, 20,
"4b007901b765489abead49d926f721d065a429c1",
"pbkdf2-sha1 c=4096");
228 if (
n_pbkdf2_hmac_sha256((
const unsigned char*)
"password", 8, (
const unsigned char*)
"salt", 4, 1, out, 32) != 0)
231 expect_hex(out, 32,
"120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b",
"pbkdf2-sha256 c=1");
234 if (
n_pbkdf2_hmac_sha1((
const unsigned char*)
"p", 1, (
const unsigned char*)
"s", 1, 0, out, 20) == 0)
236 if (
n_pbkdf2_hmac_sha1((
const unsigned char*)
"p", 1, (
const unsigned char*)
"s", 1, 1, NULL, 20) == 0)
243 unsigned char iv[12] = {0};
244 unsigned char pt[16] = {0};
245 unsigned char ct[16];
247 unsigned char back[16];
253 expect_hex(ct,
sizeof(ct),
"cea7403d4d606b6e074ec5d3baf39d18",
"aes256gcm ct");
254 expect_hex(tag,
sizeof(tag),
"d0d1c8a799996bf0265b98b5d48ab919",
"aes256gcm tag");
259 const unsigned char msg[] =
"hello, world!";
260 size_t mlen =
sizeof(msg) - 1;
262 unsigned char riv[12];
263 unsigned char rct[16];
265 unsigned char rback[16];
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);
273 else if (
n_aead_aes256gcm_decrypt(rk, riv,
sizeof(riv), (
const unsigned char*)
"aad", 3, rct, mlen, rtag, rback) != 0)
275 else if (memcmp(msg, rback, mlen) != 0)
286 else if (memcmp(pt, back,
sizeof(pt)) != 0)
291#if !N_AEAD_HAVE_CHACHA20POLY1305
293 n_log(
LOG_NOTICE,
"chacha20poly1305: skipped, this OpenSSL does not provide it");
296 unsigned char iv[12] = {0};
297 unsigned char pt[16] = {0};
298 unsigned char ct[16];
300 unsigned char back[16];
306 expect_hex(ct,
sizeof(ct),
"9f07e7be5551387a98ba977c732d080d",
"chacha20poly1305 ct");
307 expect_hex(tag,
sizeof(tag),
"c34a88047320f52aa2c6683ef8084d2f",
"chacha20poly1305 tag");
312 else if (memcmp(pt, back,
sizeof(pt)) != 0)
318 const unsigned char msg[] =
"hello, chacha!";
319 size_t mlen =
sizeof(msg) - 1;
321 unsigned char riv[12];
322 unsigned char rct[16];
324 unsigned char rback[16];
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);
334 else if (memcmp(msg, rback, mlen) != 0)
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");
365#if !N_KDF_HAVE_EVP_KDF
367 n_log(
LOG_NOTICE,
"hkdf/scrypt: skipped, this OpenSSL does not provide EVP_KDF");
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)
378 "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865",
379 "hkdf-sha256 (rfc5869 tc1)");
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)
387 "77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906",
392int main(
int argc,
char** argv) {
void process_args(int argc, char **argv)
static void test_hmac_variants(void)
static void test_pbkdf2(void)
static void test_kdf(void)
static void test_hmac(void)
static void test_aead_chacha(void)
static void test_aead(void)
static void test_consttime(void)
static void expect_hex(const unsigned char *digest, size_t len, const char *want, const char *label)
static void test_digests(void)
static void test_errors(void)
static void test_sha256_hex(void)
volatile char key[227]
TRUE while the key is held.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_NOTICE
normal but significant condition
#define LOG_NULL
no log output
#define LOG_INFO
informational
#define N_DIGEST_MD5_LEN
output size in bytes of an MD5 digest
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_AEAD_AES256GCM_KEY_LEN
key size in bytes for AES-256-GCM
#define N_AEAD_CHACHA20POLY1305_KEY_LEN
key size in bytes for ChaCha20-Poly1305
#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).
#define N_DIGEST_BLAKE2B512_LEN
BLAKE2b-512 digest length in 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.
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
A box including a string and his lenght.
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.
N_STR and string function declaration.