1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-19 18:15:18 -07:00

Merge branch 'master' of github.com:jedisct1/libsodium

* 'master' of github.com:jedisct1/libsodium:
  Bench: don't tie the printed result to the number of iterations
  Make the number of iterations configurable; reduce the default
  Add an interesting test case for a custom randombytes_uniform implementation
  Add a benchmark mode
  randombytes test: restore the salsa20-based rng at the end, for benchmarks
  Indent
This commit is contained in:
Frank Denis 2017-08-05 20:58:23 +02:00
commit 8f0953b31f
3 changed files with 71 additions and 20 deletions

View File

@ -40,8 +40,8 @@ tv(void)
assert(sizeof MESSAGE - 1U == MLEN); assert(sizeof MESSAGE - 1U == MLEN);
memcpy(m, MESSAGE, MLEN); memcpy(m, MESSAGE, MLEN);
crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN, crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN,
ad, ADLEN, ad, ADLEN,
NULL, nonce, firstkey); NULL, nonce, firstkey);
if (found_clen != MLEN + crypto_aead_xchacha20poly1305_ietf_abytes()) { if (found_clen != MLEN + crypto_aead_xchacha20poly1305_ietf_abytes()) {
printf("found_clen is not properly set\n"); printf("found_clen is not properly set\n");
} }
@ -53,10 +53,10 @@ tv(void)
} }
printf("\n"); printf("\n");
crypto_aead_xchacha20poly1305_ietf_encrypt_detached(detached_c, crypto_aead_xchacha20poly1305_ietf_encrypt_detached(detached_c,
mac, &found_maclen, mac, &found_maclen,
m, MLEN, m, MLEN,
ad, ADLEN, ad, ADLEN,
NULL, nonce, firstkey); NULL, nonce, firstkey);
if (found_maclen != crypto_aead_xchacha20poly1305_ietf_abytes()) { if (found_maclen != crypto_aead_xchacha20poly1305_ietf_abytes()) {
printf("found_maclen is not properly set\n"); 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, 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"); printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed\n");
} }
if (m2len != MLEN) { if (m2len != MLEN) {
@ -76,9 +76,9 @@ tv(void)
} }
memset(m2, 0, m2len); memset(m2, 0, m2len);
if (crypto_aead_xchacha20poly1305_ietf_decrypt_detached(m2, NULL, if (crypto_aead_xchacha20poly1305_ietf_decrypt_detached(m2, NULL,
c, MLEN, mac, c, MLEN, mac,
ad, ADLEN, ad, ADLEN,
nonce, firstkey) != 0) { nonce, firstkey) != 0) {
printf("crypto_aead_xchacha20poly1305_ietf_decrypt_detached() failed\n"); printf("crypto_aead_xchacha20poly1305_ietf_decrypt_detached() failed\n");
} }
if (memcmp(m, m2, MLEN) != 0) { if (memcmp(m, m2, MLEN) != 0) {
@ -88,14 +88,14 @@ tv(void)
for (i = 0U; i < CLEN; i++) { for (i = 0U; i < CLEN; i++) {
c[i] ^= (i + 1U); c[i] ^= (i + 1U);
if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, NULL, NULL, c, CLEN, 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) { == 0 || memcmp(m, m2, MLEN) == 0) {
printf("message can be forged\n"); printf("message can be forged\n");
} }
c[i] ^= (i + 1U); c[i] ^= (i + 1U);
} }
crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN, crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN,
NULL, 0U, NULL, nonce, firstkey); NULL, 0U, NULL, nonce, firstkey);
if (found_clen != CLEN) { if (found_clen != CLEN) {
printf("clen is not properly set (adlen=0)\n"); printf("clen is not properly set (adlen=0)\n");
} }
@ -107,7 +107,7 @@ tv(void)
} }
printf("\n"); printf("\n");
if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, CLEN, 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"); printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed (adlen=0)\n");
} }
if (m2len != MLEN) { if (m2len != MLEN) {
@ -139,7 +139,7 @@ tv(void)
memcpy(c, m, MLEN); memcpy(c, m, MLEN);
crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, c, 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) { if (found_clen != CLEN) {
printf("clen is not properly set (adlen=0)\n"); printf("clen is not properly set (adlen=0)\n");
} }
@ -152,7 +152,7 @@ tv(void)
printf("\n"); printf("\n");
if (crypto_aead_xchacha20poly1305_ietf_decrypt(c, &m2len, NULL, c, CLEN, 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"); printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed (adlen=0)\n");
} }
if (m2len != MLEN) { if (m2len != MLEN) {
@ -164,7 +164,7 @@ tv(void)
crypto_aead_xchacha20poly1305_ietf_keygen(key2); crypto_aead_xchacha20poly1305_ietf_keygen(key2);
if (crypto_aead_xchacha20poly1305_ietf_decrypt(c, &m2len, NULL, c, CLEN, 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"); printf("crypto_aead_xchacha20poly1305_ietf_decrypt() with a wrong key should have failed\n");
} }

View File

@ -30,7 +30,53 @@
int xmain(void); int xmain(void);
#ifndef BROWSER_TESTS #ifdef BENCHMARKS
# include <sys/time.h>
# ifndef ITERATIONS
# define ITERATIONS 128
# endif
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", 1000000ULL * (ts_end - ts_start) / ITERATIONS);
return 0;
}
#define printf(...) do { } while(0)
#elif !defined(BROWSER_TESTS)
FILE *fp_res; FILE *fp_res;

View File

@ -39,12 +39,14 @@ randombytes_tests(void)
unsigned int i; unsigned int i;
uint32_t n; uint32_t n;
#ifdef __EMSCRIPTEN__ #ifndef BENCHMARKS
# ifdef __EMSCRIPTEN__
assert(strcmp(randombytes_implementation_name(), "js") == 0); assert(strcmp(randombytes_implementation_name(), "js") == 0);
#elif defined(__native_client__) # elif defined(__native_client__)
assert(strcmp(randombytes_implementation_name(), "nativeclient") == 0); assert(strcmp(randombytes_implementation_name(), "nativeclient") == 0);
#else # else
assert(strcmp(randombytes_implementation_name(), "sysrandom") == 0); assert(strcmp(randombytes_implementation_name(), "sysrandom") == 0);
# endif
#endif #endif
randombytes(x, 1U); randombytes(x, 1U);
do { do {
@ -137,6 +139,7 @@ impl_tests(void)
impl.uniform = randombytes_uniform_impl; impl.uniform = randombytes_uniform_impl;
randombytes_close(); randombytes_close();
randombytes_set_implementation(&impl); randombytes_set_implementation(&impl);
assert(randombytes_uniform(1) == 1);
assert(randombytes_uniform(v) == v); assert(randombytes_uniform(v) == v);
assert(randombytes_uniform(v) == v); assert(randombytes_uniform(v) == v);
assert(randombytes_uniform(v) == v); assert(randombytes_uniform(v) == v);
@ -158,5 +161,7 @@ main(void)
#endif #endif
printf("OK\n"); printf("OK\n");
randombytes_set_implementation(&randombytes_salsa20_implementation);
return 0; return 0;
} }