From a8cc1634f492b9c8802ee13d7020b0673a905728 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 4 Aug 2017 22:59:06 +0200 Subject: [PATCH 1/6] Indent --- test/default/aead_xchacha20poly1305.c | 32 +++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test/default/aead_xchacha20poly1305.c b/test/default/aead_xchacha20poly1305.c index 7924038d..0927ce6c 100644 --- a/test/default/aead_xchacha20poly1305.c +++ b/test/default/aead_xchacha20poly1305.c @@ -40,8 +40,8 @@ tv(void) assert(sizeof MESSAGE - 1U == MLEN); memcpy(m, MESSAGE, MLEN); crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN, - ad, ADLEN, - NULL, nonce, firstkey); + ad, ADLEN, + NULL, nonce, firstkey); if (found_clen != MLEN + crypto_aead_xchacha20poly1305_ietf_abytes()) { printf("found_clen is not properly set\n"); } @@ -53,10 +53,10 @@ tv(void) } printf("\n"); crypto_aead_xchacha20poly1305_ietf_encrypt_detached(detached_c, - mac, &found_maclen, - m, MLEN, - ad, ADLEN, - NULL, nonce, firstkey); + mac, &found_maclen, + m, MLEN, + ad, ADLEN, + NULL, nonce, firstkey); if (found_maclen != crypto_aead_xchacha20poly1305_ietf_abytes()) { printf("found_maclen is not properly set\n"); } @@ -65,7 +65,7 @@ tv(void) } if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, CLEN, ad, - ADLEN, nonce, firstkey) != 0) { + ADLEN, nonce, firstkey) != 0) { printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed\n"); } if (m2len != MLEN) { @@ -76,9 +76,9 @@ tv(void) } memset(m2, 0, m2len); if (crypto_aead_xchacha20poly1305_ietf_decrypt_detached(m2, NULL, - c, MLEN, mac, - ad, ADLEN, - nonce, firstkey) != 0) { + c, MLEN, mac, + ad, ADLEN, + nonce, firstkey) != 0) { printf("crypto_aead_xchacha20poly1305_ietf_decrypt_detached() failed\n"); } if (memcmp(m, m2, MLEN) != 0) { @@ -88,14 +88,14 @@ tv(void) for (i = 0U; i < CLEN; i++) { c[i] ^= (i + 1U); if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, NULL, NULL, c, CLEN, - ad, ADLEN, nonce, firstkey) + ad, ADLEN, nonce, firstkey) == 0 || memcmp(m, m2, MLEN) == 0) { printf("message can be forged\n"); } c[i] ^= (i + 1U); } crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN, - NULL, 0U, NULL, nonce, firstkey); + NULL, 0U, NULL, nonce, firstkey); if (found_clen != CLEN) { printf("clen is not properly set (adlen=0)\n"); } @@ -107,7 +107,7 @@ tv(void) } printf("\n"); if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, CLEN, - NULL, 0U, nonce, firstkey) != 0) { + NULL, 0U, nonce, firstkey) != 0) { printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed (adlen=0)\n"); } if (m2len != MLEN) { @@ -139,7 +139,7 @@ tv(void) memcpy(c, m, MLEN); crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, c, MLEN, - NULL, 0U, NULL, nonce, firstkey); + NULL, 0U, NULL, nonce, firstkey); if (found_clen != CLEN) { printf("clen is not properly set (adlen=0)\n"); } @@ -152,7 +152,7 @@ tv(void) printf("\n"); if (crypto_aead_xchacha20poly1305_ietf_decrypt(c, &m2len, NULL, c, CLEN, - NULL, 0U, nonce, firstkey) != 0) { + NULL, 0U, nonce, firstkey) != 0) { printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed (adlen=0)\n"); } if (m2len != MLEN) { @@ -164,7 +164,7 @@ tv(void) crypto_aead_xchacha20poly1305_ietf_keygen(key2); if (crypto_aead_xchacha20poly1305_ietf_decrypt(c, &m2len, NULL, c, CLEN, - NULL, 0U, nonce, key2) == 0) { + NULL, 0U, nonce, key2) == 0) { printf("crypto_aead_xchacha20poly1305_ietf_decrypt() with a wrong key should have failed\n"); } From 8813c36fff7e0e03e5887a1d6344e1f3449aa319 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 4 Aug 2017 23:01:16 +0200 Subject: [PATCH 2/6] randombytes test: restore the salsa20-based rng at the end, for benchmarks --- test/default/randombytes.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/default/randombytes.c b/test/default/randombytes.c index 83de344f..d4127145 100644 --- a/test/default/randombytes.c +++ b/test/default/randombytes.c @@ -39,12 +39,14 @@ randombytes_tests(void) unsigned int i; uint32_t n; -#ifdef __EMSCRIPTEN__ +#ifndef BENCHMARKS +# ifdef __EMSCRIPTEN__ assert(strcmp(randombytes_implementation_name(), "js") == 0); -#elif defined(__native_client__) +# elif defined(__native_client__) assert(strcmp(randombytes_implementation_name(), "nativeclient") == 0); -#else +# else assert(strcmp(randombytes_implementation_name(), "sysrandom") == 0); +# endif #endif randombytes(x, 1U); do { @@ -158,5 +160,7 @@ main(void) #endif printf("OK\n"); + randombytes_set_implementation(&randombytes_salsa20_implementation); + return 0; } From 5b4db091df123f7c7d42be8d3b787d07c0c61f3e Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 4 Aug 2017 23:08:22 +0200 Subject: [PATCH 3/6] Add a benchmark mode --- test/default/cmptest.h | 46 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/test/default/cmptest.h b/test/default/cmptest.h index 96a0ad76..bf3ddb68 100644 --- a/test/default/cmptest.h +++ b/test/default/cmptest.h @@ -30,7 +30,51 @@ int xmain(void); -#ifndef BROWSER_TESTS +#ifdef BENCHMARKS + +# include + +# define ITERATIONS 256 + +static unsigned long long now(void) +{ + struct timeval tp; + unsigned long long now; + + if (gettimeofday(&tp, NULL) != 0) { + abort(); + } + now = ((unsigned long long) tp.tv_sec * 1000000ULL) + + (unsigned long long) tp.tv_usec; + + return now; +} + +int main(void) +{ + unsigned long long ts_start; + unsigned long long ts_end; + unsigned int i; + + if (sodium_init() != 0) { + return 99; + } + randombytes_set_implementation(&randombytes_salsa20_implementation); + ts_start = now(); + for (i = 0; i < ITERATIONS; i++) { + if (xmain() != 0) { + abort(); + } + } + ts_end = now(); + printf("%llu\n", ts_end - ts_start); + + return 0; +} + +#define printf(...) do { } while(0) + +#elif !defined(BROWSER_TESTS) FILE *fp_res; From 28a1e6886e4b44371858fa58af4766db2da1f20b Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 4 Aug 2017 23:13:29 +0200 Subject: [PATCH 4/6] Add an interesting test case for a custom randombytes_uniform implementation --- test/default/randombytes.c | 1 + 1 file changed, 1 insertion(+) diff --git a/test/default/randombytes.c b/test/default/randombytes.c index d4127145..551afaf4 100644 --- a/test/default/randombytes.c +++ b/test/default/randombytes.c @@ -139,6 +139,7 @@ impl_tests(void) impl.uniform = randombytes_uniform_impl; randombytes_close(); randombytes_set_implementation(&impl); + assert(randombytes_uniform(1) == 1); assert(randombytes_uniform(v) == v); assert(randombytes_uniform(v) == v); assert(randombytes_uniform(v) == v); From 6d59a5897def3d34a8b4ceea368b1a0614a4bbd9 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 4 Aug 2017 23:16:42 +0200 Subject: [PATCH 5/6] Make the number of iterations configurable; reduce the default --- test/default/cmptest.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/default/cmptest.h b/test/default/cmptest.h index bf3ddb68..6df1c133 100644 --- a/test/default/cmptest.h +++ b/test/default/cmptest.h @@ -34,7 +34,9 @@ int xmain(void); # include -# define ITERATIONS 256 +# ifndef ITERATIONS +# define ITERATIONS 128 +# endif static unsigned long long now(void) { From cde31281d1c9068a32d6aaf235d22a2e29475247 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Fri, 4 Aug 2017 23:30:30 +0200 Subject: [PATCH 6/6] Bench: don't tie the printed result to the number of iterations --- test/default/cmptest.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/default/cmptest.h b/test/default/cmptest.h index 6df1c133..d1b9bafc 100644 --- a/test/default/cmptest.h +++ b/test/default/cmptest.h @@ -69,7 +69,7 @@ int main(void) } } ts_end = now(); - printf("%llu\n", ts_end - ts_start); + printf("%llu\n", 1000000ULL * (ts_end - ts_start) / ITERATIONS); return 0; }