aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_crypto.c
diff options
context:
space:
mode:
authorYawning Angel <yawning@schwanenlied.me>2015-12-20 07:11:20 +0000
committerYawning Angel <yawning@schwanenlied.me>2015-12-20 07:11:20 +0000
commit081b159abc6cc89942dc11bd5745ec0fd5cc25bb (patch)
treecb281928f88a3a336ed74e913dc82a4247486dd7 /src/test/test_crypto.c
parent9467485517b69a99fb42e71416b856a2ef18a729 (diff)
downloadtor-081b159abc6cc89942dc11bd5745ec0fd5cc25bb.tar.gz
tor-081b159abc6cc89942dc11bd5745ec0fd5cc25bb.zip
Add the randomized large buffer test for SHA-3 incremental hashing.
This creates a random 100 KiB buffer, and incrementally hashes (SHA3-512) between 1 and 5 * Rate bytes in a loop, comparing the running digest with the equivalent one shot call from the start of the buffer.
Diffstat (limited to 'src/test/test_crypto.c')
-rw-r--r--src/test/test_crypto.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/test/test_crypto.c b/src/test/test_crypto.c
index a8677f3367..b6398c5f35 100644
--- a/src/test/test_crypto.c
+++ b/src/test/test_crypto.c
@@ -456,6 +456,7 @@ test_crypto_sha3(void *arg)
char data[DIGEST512_LEN];
char d_out1[DIGEST512_LEN], d_out2[DIGEST512_LEN];
char *mem_op_hex_tmp=NULL;
+ char *large = NULL;
(void)arg;
@@ -712,12 +713,46 @@ test_crypto_sha3(void *arg)
crypto_digest_get_digest(d1, d_out1, DIGEST512_LEN);
crypto_digest512(d_out2, "abcdef", 6, DIGEST_SHA3_512);
tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
+ crypto_digest_free(d1);
+
+ /* Attempt to exercise the incremental hashing code by creating a randomized
+ * 100 KiB buffer, and hashing rand[1, 5 * Rate] bytes at a time. SHA3-512
+ * is used because it has a lowest rate of the family (the code is common,
+ * but the slower rate exercises more of it).
+ */
+ const size_t bufsz = 100 * 1024;
+ size_t j = 0;
+ large = tor_malloc(bufsz);
+ crypto_rand(large, bufsz);
+ d1 = crypto_digest512_new(DIGEST_SHA3_512); /* Running digest. */
+ while (j < bufsz) {
+ /* Pick how much data to add to the running digest. */
+ size_t incr = (size_t)crypto_rand_int_range(1, 72 * 5);
+ incr = MIN(bufsz - j, incr);
+
+ /* Add the data, and calculate the hash. */
+ crypto_digest_add_bytes(d1, large + j, incr);
+ crypto_digest_get_digest(d1, d_out1, DIGEST512_LEN);
+
+ /* One-shot hash the buffer up to the data that was just added,
+ * and ensure that the values match up.
+ *
+ * XXX/yawning: If this actually fails, it'll be rather difficult to
+ * reproduce. Improvements welcome.
+ */
+ i = crypto_digest512(d_out2, large, j + incr, DIGEST_SHA3_512);
+ tt_int_op(i, OP_EQ, 0);
+ tt_mem_op(d_out1, OP_EQ, d_out2, DIGEST512_LEN);
+
+ j += incr;
+ }
done:
if (d1)
crypto_digest_free(d1);
if (d2)
crypto_digest_free(d2);
+ tor_free(large);
tor_free(mem_op_hex_tmp);
}