From 1c8e34577fd609015d570190640eb7c724717c43 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 25 Nov 2015 14:10:36 +0100 Subject: [PATCH] Less deterministic crypto_verify_*() tests --- test/default/verify1.c | 73 +++++++++++++++++++++++++++++++--------- test/default/verify1.exp | 8 ++--- 2 files changed, 60 insertions(+), 21 deletions(-) diff --git a/test/default/verify1.c b/test/default/verify1.c index 5baec865..59f88259 100644 --- a/test/default/verify1.c +++ b/test/default/verify1.c @@ -8,29 +8,72 @@ static unsigned char v64[64], v64x[64]; int main(void) { - randombytes_buf(v16, sizeof v16); - randombytes_buf(v32, sizeof v32); - randombytes_buf(v64, sizeof v64); + unsigned char *v16, *v16x; + unsigned char *v32, *v32x; + unsigned char *v64, *v64x; + uint32_t r; + uint8_t o; + int i; - memcpy(v16x, v16, sizeof v16); - memcpy(v32x, v32, sizeof v32); - memcpy(v64x, v64, sizeof v64); + v16 = (unsigned char *) sodium_malloc(16); + v16x = (unsigned char *) sodium_malloc(16); + v32 = (unsigned char *) sodium_malloc(32); + v32x = (unsigned char *) sodium_malloc(32); + v64 = (unsigned char *) sodium_malloc(64); + v64x = (unsigned char *) sodium_malloc(64); + for (i = 0; i < 100000; i++) { + randombytes_buf(v16, 16); + randombytes_buf(v32, 32); + randombytes_buf(v64, 64); - printf("%d\n", crypto_verify_16(v16, v16x)); - printf("%d\n", crypto_verify_32(v32, v32x)); - printf("%d\n", crypto_verify_64(v64, v64x)); + memcpy(v16x, v16, 16); + memcpy(v32x, v32, 32); + memcpy(v64x, v64, 64); - v16x[randombytes_random() & 15U]++; - v32x[randombytes_random() & 31U]++; - v64x[randombytes_random() & 63U]++; + if (crypto_verify_16(v16, v16x) != 0 || + crypto_verify_32(v32, v32x) != 0 || + crypto_verify_64(v64, v64x) != 0 || + sodium_memcmp(v16, v16x, 16) != 0 || + sodium_memcmp(v32, v32x, 32) != 0 || + sodium_memcmp(v64, v64x, 64) != 0) { + printf("Failed\n"); + } + } + printf("OK\n"); - printf("%d\n", crypto_verify_16(v16, v16x)); - printf("%d\n", crypto_verify_32(v32, v32x)); - printf("%d\n", crypto_verify_64(v64, v64x)); + for (i = 0; i < 100000; i++) { + r = randombytes_random(); + o = (uint8_t) randombytes_random(); + if (o == 0) { + continue; + } + v16x[r & 15U] ^= o; + v32x[r & 31U] ^= o; + v64x[r & 63U] ^= o; + if (crypto_verify_16(v16, v16x) != -1 || + crypto_verify_32(v32, v32x) != -1 || + crypto_verify_64(v64, v64x) != -1 || + sodium_memcmp(v16, v16x, 16) != -1 || + sodium_memcmp(v32, v32x, 32) != -1 || + sodium_memcmp(v64, v64x, 64) != -1) { + printf("Failed\n"); + } + v16x[r & 15U] ^= o; + v32x[r & 31U] ^= o; + v64x[r & 63U] ^= o; + } + printf("OK\n"); assert(crypto_verify_16_bytes() == 16U); assert(crypto_verify_32_bytes() == 32U); assert(crypto_verify_64_bytes() == 64U); + sodium_free(v16); + sodium_free(v16x); + sodium_free(v32); + sodium_free(v32x); + sodium_free(v64); + sodium_free(v64x); + return 0; } diff --git a/test/default/verify1.exp b/test/default/verify1.exp index 2b845004..2c94e483 100644 --- a/test/default/verify1.exp +++ b/test/default/verify1.exp @@ -1,6 +1,2 @@ -0 -0 -0 --1 --1 --1 +OK +OK