+ simple metamorphic tests for crypto_generichash()
This needs to be extended to other APIs with a streaming interface
This commit is contained in:
parent
a029b352af
commit
a7b75a2d7d
1
.gitignore
vendored
1
.gitignore
vendored
@ -114,6 +114,7 @@ test/default/hash3
|
||||
test/default/kdf
|
||||
test/default/keygen
|
||||
test/default/kx
|
||||
test/default/metamorphic
|
||||
test/default/misuse
|
||||
test/default/onetimeauth
|
||||
test/default/onetimeauth2
|
||||
|
@ -37,6 +37,7 @@ EXTRA_DIST = \
|
||||
kdf.exp \
|
||||
keygen.exp \
|
||||
kx.exp \
|
||||
metamorphic.exp \
|
||||
misuse.exp \
|
||||
onetimeauth.exp \
|
||||
onetimeauth2.exp \
|
||||
@ -108,6 +109,7 @@ DISTCLEANFILES = \
|
||||
kdf.res \
|
||||
keygen.res \
|
||||
kx.res \
|
||||
metamorphic.res \
|
||||
misuse.res \
|
||||
onetimeauth.res \
|
||||
onetimeauth2.res \
|
||||
@ -180,6 +182,7 @@ CLEANFILES = \
|
||||
kdf.final \
|
||||
keygen.final \
|
||||
kx.final \
|
||||
metamorphic.final \
|
||||
misuse.final \
|
||||
onetimeauth.final \
|
||||
onetimeauth2.final \
|
||||
@ -247,6 +250,7 @@ CLEANFILES = \
|
||||
kdf.nexe \
|
||||
keygen.nexe \
|
||||
kx.nexe \
|
||||
metamorphic.nexe \
|
||||
misuse.nexe \
|
||||
onetimeauth.nexe \
|
||||
onetimeauth2.nexe \
|
||||
@ -326,6 +330,7 @@ TESTS_TARGETS = \
|
||||
kdf \
|
||||
keygen \
|
||||
kx \
|
||||
metamorphic \
|
||||
misuse \
|
||||
onetimeauth \
|
||||
onetimeauth2 \
|
||||
@ -470,6 +475,9 @@ keygen_LDADD = $(TESTS_LDADD)
|
||||
kx_SOURCE = cmptest.h kx.c
|
||||
kx_LDADD = $(TESTS_LDADD)
|
||||
|
||||
metamorphic_SOURCE = cmptest.h metamorphic.c
|
||||
metamorphic_LDADD = $(TESTS_LDADD)
|
||||
|
||||
misuse_SOURCE = cmptest.h misuse.c
|
||||
misuse_LDADD = $(TESTS_LDADD)
|
||||
|
||||
|
63
test/default/metamorphic.c
Normal file
63
test/default/metamorphic.c
Normal file
@ -0,0 +1,63 @@
|
||||
|
||||
#define TEST_NAME "metamorphic"
|
||||
#include "cmptest.h"
|
||||
|
||||
#define MAXLEN 512
|
||||
#define MAX_ITER 1000
|
||||
|
||||
static void
|
||||
mm_1(void)
|
||||
{
|
||||
crypto_generichash_state st;
|
||||
unsigned char *h, *h2;
|
||||
unsigned char *k;
|
||||
unsigned char *m;
|
||||
size_t hlen;
|
||||
size_t klen;
|
||||
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);
|
||||
klen = randombytes_uniform(crypto_generichash_KEYBYTES_MAX -
|
||||
crypto_generichash_KEYBYTES_MIN + 1U)
|
||||
+ crypto_generichash_KEYBYTES_MIN;
|
||||
k = (unsigned char *) sodium_malloc(klen);
|
||||
hlen = randombytes_uniform(crypto_generichash_BYTES_MAX -
|
||||
crypto_generichash_BYTES_MIN + 1U)
|
||||
+ crypto_generichash_BYTES_MIN;
|
||||
h = (unsigned char *) sodium_malloc(hlen);
|
||||
h2 = (unsigned char *) sodium_malloc(hlen);
|
||||
|
||||
randombytes_buf(m, mlen);
|
||||
|
||||
crypto_generichash_init(&st, k, klen, hlen);
|
||||
l1 = randombytes_uniform(mlen);
|
||||
l2 = randombytes_uniform(mlen - l1);
|
||||
crypto_generichash_update(&st, m, l1);
|
||||
crypto_generichash_update(&st, m + l1, l2);
|
||||
crypto_generichash_update(&st, m + l1 + l2, mlen - l1 - l2);
|
||||
crypto_generichash_final(&st, h, hlen);
|
||||
|
||||
crypto_generichash(h2, hlen, m, mlen, k, klen);
|
||||
|
||||
assert(memcmp(h, h2, hlen) == 0);
|
||||
|
||||
sodium_free(h2);
|
||||
sodium_free(h);
|
||||
sodium_free(k);
|
||||
sodium_free(m);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
mm_1();
|
||||
|
||||
printf("OK\n");
|
||||
|
||||
return 0;
|
||||
}
|
1
test/default/metamorphic.exp
Normal file
1
test/default/metamorphic.exp
Normal file
@ -0,0 +1 @@
|
||||
OK
|
Loading…
Reference in New Issue
Block a user