From bd69a3083ac264b2c614f18d97734d5aa99887f2 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sun, 17 Sep 2017 21:48:16 +0200 Subject: [PATCH] metamorphic tests for onetimeauth --- test/default/metamorphic.c | 48 +++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/test/default/metamorphic.c b/test/default/metamorphic.c index 96cf71e0..74e0fcd7 100644 --- a/test/default/metamorphic.c +++ b/test/default/metamorphic.c @@ -6,7 +6,7 @@ #define MAX_ITER 1000 static void -mm_1(void) +mm_generichash(void) { crypto_generichash_state st; unsigned char *h, *h2; @@ -31,8 +31,9 @@ mm_1(void) h = (unsigned char *) sodium_malloc(hlen); h2 = (unsigned char *) sodium_malloc(hlen); + randombytes_buf(k, klen); randombytes_buf(m, mlen); - + crypto_generichash_init(&st, k, klen, hlen); l1 = randombytes_uniform(mlen); l2 = randombytes_uniform(mlen - l1); @@ -52,10 +53,51 @@ mm_1(void) } } +static void +mm_onetimeauth(void) +{ + crypto_onetimeauth_state st; + unsigned char *h, *h2; + unsigned char *k; + unsigned char *m; + size_t mlen; + size_t l1, l2; + int i; + + for (i = 0; i < MAX_ITER; i++) { + mlen = randombytes_uniform(MAXLEN); + m = (unsigned char *) sodium_malloc(mlen); + k = (unsigned char *) sodium_malloc(crypto_onetimeauth_KEYBYTES); + h = (unsigned char *) sodium_malloc(crypto_onetimeauth_BYTES); + h2 = (unsigned char *) sodium_malloc(crypto_onetimeauth_BYTES); + + crypto_onetimeauth_keygen(k); + randombytes_buf(m, mlen); + + crypto_onetimeauth_init(&st, k); + l1 = randombytes_uniform(mlen); + l2 = randombytes_uniform(mlen - l1); + crypto_onetimeauth_update(&st, m, l1); + crypto_onetimeauth_update(&st, m + l1, l2); + crypto_onetimeauth_update(&st, m + l1 + l2, mlen - l1 - l2); + crypto_onetimeauth_final(&st, h); + + crypto_onetimeauth(h2, m, mlen, k); + + assert(memcmp(h, h2, crypto_onetimeauth_BYTES) == 0); + + sodium_free(h2); + sodium_free(h); + sodium_free(k); + sodium_free(m); + } +} + int main(void) { - mm_1(); + mm_generichash(); + mm_onetimeauth(); printf("OK\n");