From a7b75a2d7d7a38024d07218294a894e5f781837f Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sun, 17 Sep 2017 21:41:32 +0200 Subject: [PATCH] + simple metamorphic tests for crypto_generichash() This needs to be extended to other APIs with a streaming interface --- .gitignore | 1 + test/default/Makefile.am | 8 +++++ test/default/metamorphic.c | 63 ++++++++++++++++++++++++++++++++++++ test/default/metamorphic.exp | 1 + 4 files changed, 73 insertions(+) create mode 100644 test/default/metamorphic.c create mode 100644 test/default/metamorphic.exp diff --git a/.gitignore b/.gitignore index 526277f9..b8eb2a13 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/test/default/Makefile.am b/test/default/Makefile.am index 4e1d79d3..835ad74c 100644 --- a/test/default/Makefile.am +++ b/test/default/Makefile.am @@ -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) diff --git a/test/default/metamorphic.c b/test/default/metamorphic.c new file mode 100644 index 00000000..96cf71e0 --- /dev/null +++ b/test/default/metamorphic.c @@ -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; +} diff --git a/test/default/metamorphic.exp b/test/default/metamorphic.exp new file mode 100644 index 00000000..d86bac9d --- /dev/null +++ b/test/default/metamorphic.exp @@ -0,0 +1 @@ +OK