From 4c1836af879d2e05d287caf2c32a491754efeaf3 Mon Sep 17 00:00:00 2001 From: Loup Vaillant Date: Sat, 21 Apr 2018 19:48:38 +0200 Subject: [PATCH] use RANDOM_INPUT macro everywhere --- tests/speed-sodium.c | 56 +++++++++++++++++++------------------- tests/speed-tweetnacl.c | 32 +++++++++++----------- tests/speed.c | 59 +++++++++++++++++++++-------------------- tests/test.c | 51 +++++++++++++++++------------------ 4 files changed, 99 insertions(+), 99 deletions(-) diff --git a/tests/speed-sodium.c b/tests/speed-sodium.c index 1da86b6..a1c50ab 100644 --- a/tests/speed-sodium.c +++ b/tests/speed-sodium.c @@ -3,10 +3,10 @@ static u64 chacha20(void) { - static u8 in [SIZE]; p_random(in , SIZE); - static u8 key [ 32]; p_random(key , 32); - static u8 nonce[ 8]; p_random(nonce, 8); - static u8 out [SIZE]; + u8 out[SIZE]; + RANDOM_INPUT(in , SIZE); + RANDOM_INPUT(key , 32); + RANDOM_INPUT(nonce, 8); TIMING_START { crypto_stream_chacha20_xor(out, in, SIZE, nonce, key); @@ -16,9 +16,9 @@ static u64 chacha20(void) static u64 poly1305(void) { - static u8 in [SIZE]; p_random(in , SIZE); - static u8 key[ 32]; p_random(key , 32); - static u8 out[ 16]; + u8 out[16]; + RANDOM_INPUT(in , SIZE); + RANDOM_INPUT(key, 32); TIMING_START { crypto_onetimeauth(out, in, SIZE, key); @@ -28,11 +28,12 @@ static u64 poly1305(void) static u64 authenticated(void) { - static u8 in [SIZE]; p_random(in , SIZE); - static u8 key [ 32]; p_random(key , 32); - static u8 nonce[ 8]; p_random(nonce, 8); - static u8 out [SIZE]; - static u8 mac [crypto_aead_xchacha20poly1305_ietf_ABYTES]; + u8 out[SIZE]; + u8 mac[crypto_aead_xchacha20poly1305_ietf_ABYTES]; + RANDOM_INPUT(in , SIZE); + RANDOM_INPUT(key , 32); + RANDOM_INPUT(nonce, 8); + TIMING_START { crypto_aead_xchacha20poly1305_ietf_encrypt_detached( out, mac, 0, in, SIZE, 0, 0, 0, nonce, key); @@ -42,9 +43,9 @@ static u64 authenticated(void) static u64 blake2b(void) { - static u8 in [SIZE]; p_random(in , SIZE); - static u8 key [ 32]; p_random(key, 32); - static u8 hash[ 64]; + u8 hash[64]; + RANDOM_INPUT(in , SIZE); + RANDOM_INPUT(key, 32); TIMING_START { crypto_generichash(hash, 64, in, SIZE, key, 32); @@ -54,8 +55,8 @@ static u64 blake2b(void) static u64 sha512(void) { - static u8 in [SIZE]; p_random(in , SIZE); - static u8 hash[ 64]; + u8 hash[64]; + RANDOM_INPUT(in, SIZE); TIMING_START { crypto_hash_sha512(hash, in, SIZE); @@ -65,9 +66,9 @@ static u64 sha512(void) static u64 argon2i(void) { - static u8 password [ 16]; p_random(password, 16); - static u8 salt [ 16]; p_random(salt , 16); - static u8 hash [ 32]; + u8 hash [32]; + RANDOM_INPUT(password, 16); + RANDOM_INPUT(salt , 16); TIMING_START { if (crypto_pwhash(hash, 32, (char*)password, 16, salt, @@ -93,10 +94,11 @@ static u64 x25519(void) static u64 edDSA_sign(void) { - u8 sk [64]; p_random(sk, 32); - u8 pk [32]; crypto_sign_keypair(pk, sk); - u8 message [64]; p_random(message, 64); + u8 sk [64]; + u8 pk [32]; u8 signature[64]; + RANDOM_INPUT(message, 64); + crypto_sign_keypair(pk, sk); TIMING_START { crypto_sign_detached(signature, 0, message, 64, sk); @@ -106,11 +108,11 @@ static u64 edDSA_sign(void) static u64 edDSA_check(void) { - u8 sk [64]; p_random(sk, 32); - u8 pk [32]; crypto_sign_keypair(pk, sk); - u8 message [64]; p_random(message, 64); + u8 sk [64]; + u8 pk [32]; u8 signature[64]; - + RANDOM_INPUT(message, 64); + crypto_sign_keypair(pk, sk); crypto_sign_detached(signature, 0, message, 64, sk); TIMING_START { diff --git a/tests/speed-tweetnacl.c b/tests/speed-tweetnacl.c index a785541..5607ad1 100644 --- a/tests/speed-tweetnacl.c +++ b/tests/speed-tweetnacl.c @@ -4,10 +4,10 @@ static u64 salsa20(void) { - static u8 in [SIZE]; p_random(in , SIZE); - static u8 key [ 32]; p_random(key , 32); - static u8 nonce[ 8]; p_random(nonce, 8); - static u8 out [SIZE]; + u8 out[SIZE]; + RANDOM_INPUT(in , SIZE); + RANDOM_INPUT(key , 32); + RANDOM_INPUT(nonce, 8); TIMING_START { crypto_stream_salsa20_xor(out, in, SIZE, nonce, key); @@ -17,9 +17,9 @@ static u64 salsa20(void) static u64 poly1305(void) { - static u8 in [SIZE]; p_random(in , SIZE); - static u8 key[ 32]; p_random(key , 32); - static u8 out[ 16]; + u8 out[16]; + RANDOM_INPUT(in , SIZE); + RANDOM_INPUT(key, 32); TIMING_START { crypto_onetimeauth(out, in, SIZE, key); @@ -29,10 +29,10 @@ static u64 poly1305(void) static u64 authenticated(void) { - static u8 in [SIZE + 32]; p_random(in , SIZE); - static u8 key [ 32]; p_random(key , 32); - static u8 nonce[ 8]; p_random(nonce, 8); - static u8 out [SIZE + 32]; + u8 out[SIZE + 32]; + RANDOM_INPUT(in , SIZE + 32); + RANDOM_INPUT(key , 32); + RANDOM_INPUT(nonce, 8); TIMING_START { crypto_secretbox(out, in, SIZE + 32, nonce, key); @@ -42,8 +42,8 @@ static u64 authenticated(void) static u64 sha512(void) { - static u8 in [SIZE]; p_random(in , SIZE); - static u8 hash[ 64]; + u8 hash[64]; + RANDOM_INPUT(in, SIZE); TIMING_START { crypto_hash(hash, in, SIZE); @@ -66,10 +66,9 @@ static u64 edDSA_sign(void) { u8 sk [ 64]; u8 pk [ 32]; - u8 message [ 64]; p_random(message, 64); u8 signed_msg[128]; unsigned long long sig_size; - + RANDOM_INPUT(message, 64); crypto_sign_keypair(pk, sk); TIMING_START { @@ -85,12 +84,11 @@ static u64 edDSA_check(void) { u8 sk [ 64]; u8 pk [ 32]; - u8 message [ 64]; p_random(message, 64); u8 signed_msg[128]; u8 out_msg [128]; unsigned long long sig_size; unsigned long long msg_size; - + RANDOM_INPUT(message, 64); crypto_sign_keypair(pk, sk); crypto_sign(signed_msg, &sig_size, message, 64, sk); diff --git a/tests/speed.c b/tests/speed.c index a5b8635..7cb1056 100644 --- a/tests/speed.c +++ b/tests/speed.c @@ -5,10 +5,10 @@ static u64 chacha20(void) { - static u8 in [SIZE]; p_random(in , SIZE); - static u8 key [ 32]; p_random(key , 32); - static u8 nonce[ 8]; p_random(nonce, 8); - static u8 out [SIZE]; + u8 out[SIZE]; + RANDOM_INPUT(in , SIZE); + RANDOM_INPUT(key , 32); + RANDOM_INPUT(nonce, 8); TIMING_START { crypto_chacha_ctx ctx; @@ -20,9 +20,9 @@ static u64 chacha20(void) static u64 poly1305(void) { - static u8 in [SIZE]; p_random(in , SIZE); - static u8 key[ 32]; p_random(key , 32); - static u8 out[ 16]; + u8 out[16]; + RANDOM_INPUT(in , SIZE); + RANDOM_INPUT(key, 32); TIMING_START { crypto_poly1305(out, in, SIZE, key); @@ -32,11 +32,11 @@ static u64 poly1305(void) static u64 authenticated(void) { - static u8 in [SIZE]; p_random(in , SIZE); - static u8 key [ 32]; p_random(key , 32); - static u8 nonce[ 8]; p_random(nonce, 8); - static u8 out [SIZE]; - static u8 mac [ 16]; + u8 out[SIZE]; + u8 mac[ 16]; + RANDOM_INPUT(in , SIZE); + RANDOM_INPUT(key , 32); + RANDOM_INPUT(nonce, 8); TIMING_START { crypto_lock(mac, out, key, nonce, in, SIZE); @@ -46,9 +46,9 @@ static u64 authenticated(void) static u64 blake2b(void) { - static u8 in [SIZE]; p_random(in , SIZE); - static u8 key [ 32]; p_random(key, 32); - static u8 hash[ 64]; + u8 hash[64]; + RANDOM_INPUT(in , SIZE); + RANDOM_INPUT(key, 32); TIMING_START { crypto_blake2b_general(hash, 64, key, 32, in, SIZE); @@ -58,8 +58,8 @@ static u64 blake2b(void) static u64 sha512(void) { - static u8 in [SIZE]; p_random(in , SIZE); - static u8 hash[ 64]; + u8 hash[64]; + RANDOM_INPUT(in, SIZE); TIMING_START { crypto_sha512(hash, in, SIZE); @@ -69,11 +69,11 @@ static u64 sha512(void) static u64 argon2i(void) { - size_t nb_blocks = SIZE / 1024; - static u8 work_area[SIZE]; - static u8 password [ 16]; p_random(password, 16); - static u8 salt [ 16]; p_random(salt , 16); - static u8 hash [ 32]; + u64 work_area[SIZE / 8]; + u8 hash [32]; + size_t nb_blocks = SIZE / 1024; + RANDOM_INPUT(password, 16); + RANDOM_INPUT(salt , 16); TIMING_START { crypto_argon2i(hash, 32, work_area, nb_blocks, 3, @@ -97,10 +97,11 @@ static u64 x25519(void) static u64 edDSA_sign(void) { - u8 sk [32]; p_random(sk, 32); - u8 pk [32]; crypto_sign_public_key(pk, sk); - u8 message [64]; p_random(message, 64); + u8 pk [32]; u8 signature[64]; + RANDOM_INPUT(sk , 32); + RANDOM_INPUT(message, 64); + crypto_sign_public_key(pk, sk); TIMING_START { crypto_sign(signature, sk, pk, message, 64); @@ -110,11 +111,11 @@ static u64 edDSA_sign(void) static u64 edDSA_check(void) { - u8 sk [32]; p_random(sk, 32); - u8 pk [32]; crypto_sign_public_key(pk, sk); - u8 message [64]; p_random(message, 64); + u8 pk [32]; u8 signature[64]; - + RANDOM_INPUT(sk , 32); + RANDOM_INPUT(message, 64); + crypto_sign_public_key(pk, sk); crypto_sign(signature, sk, pk, message, 64); TIMING_START { diff --git a/tests/test.c b/tests/test.c index 590126f..aed4b26 100644 --- a/tests/test.c +++ b/tests/test.c @@ -289,9 +289,9 @@ static int p_chacha20() u8 output_chunk[INPUT_SIZE]; u8 output_whole[INPUT_SIZE]; // inputs - u8 input [INPUT_SIZE]; p_random(input, INPUT_SIZE); - u8 key [32]; p_random(key , 32); - u8 nonce [8]; p_random(nonce, 8); + RANDOM_INPUT(input, INPUT_SIZE); + RANDOM_INPUT(key , 32); + RANDOM_INPUT(nonce, 8); // Encrypt in chunks crypto_chacha_ctx ctx; @@ -322,10 +322,10 @@ static int p_chacha20() static int p_chacha20_same_ptr() { int status = 0; - u8 input [INPUT_SIZE]; p_random(input, INPUT_SIZE); - u8 key [32]; p_random(key , 32); - u8 nonce [8]; p_random(nonce, 8); - u8 output [INPUT_SIZE]; + u8 output[INPUT_SIZE]; + RANDOM_INPUT(input, INPUT_SIZE); + RANDOM_INPUT(key , 32); + RANDOM_INPUT(nonce, 8); crypto_chacha_ctx ctx; crypto_chacha20_init (&ctx, key, nonce); crypto_chacha20_encrypt(&ctx, output, input, INPUT_SIZE); @@ -344,8 +344,8 @@ static int p_chacha20_set_ctr() u8 output_part[STREAM_SIZE ]; u8 output_all [STREAM_SIZE ]; u8 output_more[STREAM_SIZE * 2]; - u8 key [32]; p_random(key , 32); - u8 nonce [8]; p_random(nonce, 8 ); + RANDOM_INPUT(key , 32); + RANDOM_INPUT(nonce, 8); u64 ctr = rand64() % CHACHA_NB_BLOCKS; size_t limit = ctr * CHACHA_BLOCK_SIZE; // Encrypt all at once @@ -385,8 +385,8 @@ static int p_poly1305() u8 mac_chunk[16]; u8 mac_whole[16]; // inputs - u8 input[INPUT_SIZE]; p_random(input, INPUT_SIZE); - u8 key [32]; p_random(key , 32); + RANDOM_INPUT(input, INPUT_SIZE); + RANDOM_INPUT(key , 32); // Authenticate bit by bit crypto_poly1305_ctx ctx; @@ -412,8 +412,8 @@ static int p_poly1305_overlap() #define INPUT_SIZE (POLY1305_BLOCK_SIZE + (2 * 16)) // total input size int status = 0; FOR (i, 0, POLY1305_BLOCK_SIZE + 16) { - u8 input[INPUT_SIZE]; p_random(input, INPUT_SIZE); - u8 key [32]; p_random(key , 32); + RANDOM_INPUT(input, INPUT_SIZE); + RANDOM_INPUT(key , 32); u8 mac [16]; crypto_poly1305(mac , input + 16, POLY1305_BLOCK_SIZE, key); crypto_poly1305(input+i, input + 16, POLY1305_BLOCK_SIZE, key); @@ -437,7 +437,7 @@ static int p_blake2b() u8 hash_chunk[64]; u8 hash_whole[64]; // inputs - u8 input[INPUT_SIZE]; p_random(input, INPUT_SIZE); + RANDOM_INPUT(input, INPUT_SIZE); // Authenticate bit by bit crypto_blake2b_ctx ctx; @@ -463,8 +463,8 @@ static int p_blake2b_overlap() #define INPUT_SIZE (BLAKE2B_BLOCK_SIZE + (2 * 64)) // total input size int status = 0; FOR (i, 0, BLAKE2B_BLOCK_SIZE + 64) { - u8 input[INPUT_SIZE]; p_random(input, INPUT_SIZE); u8 hash [64]; + RANDOM_INPUT(input, INPUT_SIZE); crypto_blake2b(hash , input + 64, BLAKE2B_BLOCK_SIZE); crypto_blake2b(input+i, input + 64, BLAKE2B_BLOCK_SIZE); status |= memcmp(hash, input + i, 64); @@ -485,7 +485,7 @@ static int p_sha512() u8 hash_chunk[64]; u8 hash_whole[64]; // inputs - u8 input[INPUT_SIZE]; p_random(input, INPUT_SIZE); + RANDOM_INPUT(input, INPUT_SIZE); // Authenticate bit by bit crypto_sha512_ctx ctx; @@ -511,8 +511,8 @@ static int p_sha512_overlap() #define INPUT_SIZE (SHA_512_BLOCK_SIZE + (2 * 64)) // total input size int status = 0; FOR (i, 0, SHA_512_BLOCK_SIZE + 64) { - u8 input[INPUT_SIZE]; p_random(input, INPUT_SIZE); u8 hash [64]; + RANDOM_INPUT(input, INPUT_SIZE); crypto_sha512(hash , input + 64, SHA_512_BLOCK_SIZE); crypto_sha512(input+i, input + 64, SHA_512_BLOCK_SIZE); status |= memcmp(hash, input + i, 64); @@ -594,7 +594,7 @@ static int p_eddsa_roundtrip() static int p_eddsa_random() { int status = 0; - u8 message[MESSAGE_SIZE]; p_random(message, 32); + RANDOM_INPUT(message, MESSAGE_SIZE); FOR (i, 0, 1000) { RANDOM_INPUT(pk, 32); RANDOM_INPUT(signature , 64); @@ -626,7 +626,6 @@ static int p_eddsa_overlap() static int p_eddsa_incremental() { int status = 0; - u8 message[MESSAGE_SIZE]; p_random(message, 32); FOR (i, 0, MESSAGE_SIZE) { RANDOM_INPUT(message, MESSAGE_SIZE); RANDOM_INPUT(sk, 32); @@ -661,10 +660,10 @@ static int p_aead() { int status = 0; FOR (i, 0, 1000) { - u8 key [32]; p_random(key , 32); - u8 nonce [24]; p_random(nonce , 24); - u8 ad [ 4]; p_random(ad , 4); - u8 plaintext[ 8]; p_random(plaintext, 8); + RANDOM_INPUT(key , 32); + RANDOM_INPUT(nonce , 24); + RANDOM_INPUT(ad , 4); + RANDOM_INPUT(plaintext, 8); u8 box[24], box2[24]; u8 out[8]; // AEAD roundtrip @@ -763,9 +762,9 @@ static int p_auth() { int status = 0; FOR (i, 0, 128) { - u8 key [ 32]; p_random(key , 32); - u8 nonce [ 24]; p_random(nonce , 24); - u8 ad [128]; p_random(ad , i); + RANDOM_INPUT(key , 32); + RANDOM_INPUT(nonce , 24); + RANDOM_INPUT(ad , 128); u8 mac1[16]; u8 mac2[16]; // roundtrip -- 2.47.3