metamorphic tests for onetimeauth

This commit is contained in:
Frank Denis 2017-09-17 21:48:16 +02:00
parent a7b75a2d7d
commit bd69a3083a

View File

@ -6,7 +6,7 @@
#define MAX_ITER 1000 #define MAX_ITER 1000
static void static void
mm_1(void) mm_generichash(void)
{ {
crypto_generichash_state st; crypto_generichash_state st;
unsigned char *h, *h2; unsigned char *h, *h2;
@ -31,8 +31,9 @@ mm_1(void)
h = (unsigned char *) sodium_malloc(hlen); h = (unsigned char *) sodium_malloc(hlen);
h2 = (unsigned char *) sodium_malloc(hlen); h2 = (unsigned char *) sodium_malloc(hlen);
randombytes_buf(k, klen);
randombytes_buf(m, mlen); randombytes_buf(m, mlen);
crypto_generichash_init(&st, k, klen, hlen); crypto_generichash_init(&st, k, klen, hlen);
l1 = randombytes_uniform(mlen); l1 = randombytes_uniform(mlen);
l2 = randombytes_uniform(mlen - l1); 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 int
main(void) main(void)
{ {
mm_1(); mm_generichash();
mm_onetimeauth();
printf("OK\n"); printf("OK\n");