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",
126 if (!h || strcmp(h->
data,
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") != 0) {
134 if (!h || strcmp(h->
data,
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") != 0) {
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";
150 expect_hex(out,
sizeof(out),
"f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8",
"hmac-sha256");
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);
163 expect_hex(o1,
sizeof(o1),
"de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9",
"hmac-sha1");
170 "d7f4727e2c0b39ae0f1e40cc96f60242d5b7801841cea6fc592c5d3e1ae50700582a96cf35e1e554995fe4e03381c237",
178 "b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe86653c73dd248fb82f948a549f7b791a5b41915ee4d1ec3935357e4e2317250d0372afa2ebeeb3a",
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};
197 unsigned char out[40];
200 if (
n_pbkdf2_hmac_sha1((
const unsigned char*)
"password", 8, (
const unsigned char*)
"salt", 4, 1, out, 20) != 0)
203 expect_hex(out, 20,
"0c60c80f961f0e71f3a9b524af6012062fe037a6",
"pbkdf2-sha1 c=1");
206 if (
n_pbkdf2_hmac_sha1((
const unsigned char*)
"password", 8, (
const unsigned char*)
"salt", 4, 4096, out, 20) != 0)
209 expect_hex(out, 20,
"4b007901b765489abead49d926f721d065a429c1",
"pbkdf2-sha1 c=4096");
212 if (
n_pbkdf2_hmac_sha256((
const unsigned char*)
"password", 8, (
const unsigned char*)
"salt", 4, 1, out, 32) != 0)
215 expect_hex(out, 32,
"120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b",
"pbkdf2-sha256 c=1");
218 if (
n_pbkdf2_hmac_sha1((
const unsigned char*)
"p", 1, (
const unsigned char*)
"s", 1, 0, out, 20) == 0)
220 if (
n_pbkdf2_hmac_sha1((
const unsigned char*)
"p", 1, (
const unsigned char*)
"s", 1, 1, NULL, 20) == 0)
227 unsigned char iv[12] = {0};
228 unsigned char pt[16] = {0};
229 unsigned char ct[16];
231 unsigned char back[16];
237 expect_hex(ct,
sizeof(ct),
"cea7403d4d606b6e074ec5d3baf39d18",
"aes256gcm ct");
238 expect_hex(tag,
sizeof(tag),
"d0d1c8a799996bf0265b98b5d48ab919",
"aes256gcm tag");
243 const unsigned char msg[] =
"hello, world!";
244 size_t mlen =
sizeof(msg) - 1;
246 unsigned char riv[12];
247 unsigned char rct[16];
249 unsigned char rback[16];
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);
257 else if (
n_aead_aes256gcm_decrypt(rk, riv,
sizeof(riv), (
const unsigned char*)
"aad", 3, rct, mlen, rtag, rback) != 0)
259 else if (memcmp(msg, rback, mlen) != 0)
270 else if (memcmp(pt, back,
sizeof(pt)) != 0)
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");
295int 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_hmac(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)
#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).
#define N_AEAD_AES256GCM_KEY_LEN
key size in bytes for AES-256-GCM
#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.
#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.