Unit test the full length of SHA256 and SHA512 digests

Bugfix on a tor version before the refactoring in git commit
cea1225199 (23 Sep 2009). Patch by "teor".
This commit is contained in:
teor (Tim Wilson-Brown) 2015-11-27 02:25:31 +11:00
parent 09e0ae0588
commit fc264975b1
2 changed files with 25 additions and 19 deletions

5
changes/sha-unit-tests Normal file
View File

@ -0,0 +1,5 @@
o Minor bugfixes (unit tests):
- Check the full results of SHA256 and SHA512 digests in the
unit tests.
Bugfix on a tor version before the refactoring in git commit
cea12251995d (23 Sep 2009). Patch by "teor".

View File

@ -284,10 +284,11 @@ test_crypto_sha(void *arg)
{ {
crypto_digest_t *d1 = NULL, *d2 = NULL; crypto_digest_t *d1 = NULL, *d2 = NULL;
int i; int i;
char key[160]; #define RFC_4231_MAX_KEY_SIZE 131
char digest[32]; char key[RFC_4231_MAX_KEY_SIZE];
char data[50]; char digest[DIGEST256_LEN];
char d_out1[DIGEST_LEN], d_out2[DIGEST512_LEN]; char data[DIGEST512_LEN];
char d_out1[DIGEST512_LEN], d_out2[DIGEST512_LEN];
char *mem_op_hex_tmp=NULL; char *mem_op_hex_tmp=NULL;
/* Test SHA-1 with a test vector from the specification. */ /* Test SHA-1 with a test vector from the specification. */
@ -385,15 +386,15 @@ test_crypto_sha(void *arg)
d2 = crypto_digest_dup(d1); d2 = crypto_digest_dup(d1);
tt_assert(d2); tt_assert(d2);
crypto_digest_add_bytes(d2, "ghijkl", 6); crypto_digest_add_bytes(d2, "ghijkl", 6);
crypto_digest_get_digest(d2, d_out1, sizeof(d_out1)); crypto_digest_get_digest(d2, d_out1, DIGEST_LEN);
crypto_digest(d_out2, "abcdefghijkl", 12); crypto_digest(d_out2, "abcdefghijkl", 12);
tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN); tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN);
crypto_digest_assign(d2, d1); crypto_digest_assign(d2, d1);
crypto_digest_add_bytes(d2, "mno", 3); crypto_digest_add_bytes(d2, "mno", 3);
crypto_digest_get_digest(d2, d_out1, sizeof(d_out1)); crypto_digest_get_digest(d2, d_out1, DIGEST_LEN);
crypto_digest(d_out2, "abcdefmno", 9); crypto_digest(d_out2, "abcdefmno", 9);
tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN); tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN);
crypto_digest_get_digest(d1, d_out1, sizeof(d_out1)); crypto_digest_get_digest(d1, d_out1, DIGEST_LEN);
crypto_digest(d_out2, "abcdef", 6); crypto_digest(d_out2, "abcdef", 6);
tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN); tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN);
crypto_digest_free(d1); crypto_digest_free(d1);
@ -406,17 +407,17 @@ test_crypto_sha(void *arg)
d2 = crypto_digest_dup(d1); d2 = crypto_digest_dup(d1);
tt_assert(d2); tt_assert(d2);
crypto_digest_add_bytes(d2, "ghijkl", 6); crypto_digest_add_bytes(d2, "ghijkl", 6);
crypto_digest_get_digest(d2, d_out1, sizeof(d_out1)); crypto_digest_get_digest(d2, d_out1, DIGEST256_LEN);
crypto_digest256(d_out2, "abcdefghijkl", 12, DIGEST_SHA256); crypto_digest256(d_out2, "abcdefghijkl", 12, DIGEST_SHA256);
tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN); tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
crypto_digest_assign(d2, d1); crypto_digest_assign(d2, d1);
crypto_digest_add_bytes(d2, "mno", 3); crypto_digest_add_bytes(d2, "mno", 3);
crypto_digest_get_digest(d2, d_out1, sizeof(d_out1)); crypto_digest_get_digest(d2, d_out1, DIGEST256_LEN);
crypto_digest256(d_out2, "abcdefmno", 9, DIGEST_SHA256); crypto_digest256(d_out2, "abcdefmno", 9, DIGEST_SHA256);
tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN); tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
crypto_digest_get_digest(d1, d_out1, sizeof(d_out1)); crypto_digest_get_digest(d1, d_out1, DIGEST256_LEN);
crypto_digest256(d_out2, "abcdef", 6, DIGEST_SHA256); crypto_digest256(d_out2, "abcdef", 6, DIGEST_SHA256);
tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN); tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
crypto_digest_free(d1); crypto_digest_free(d1);
crypto_digest_free(d2); crypto_digest_free(d2);
@ -427,17 +428,17 @@ test_crypto_sha(void *arg)
d2 = crypto_digest_dup(d1); d2 = crypto_digest_dup(d1);
tt_assert(d2); tt_assert(d2);
crypto_digest_add_bytes(d2, "ghijkl", 6); crypto_digest_add_bytes(d2, "ghijkl", 6);
crypto_digest_get_digest(d2, d_out1, sizeof(d_out1)); crypto_digest_get_digest(d2, d_out1, DIGEST512_LEN);
crypto_digest512(d_out2, "abcdefghijkl", 12, DIGEST_SHA512); crypto_digest512(d_out2, "abcdefghijkl", 12, DIGEST_SHA512);
tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN); tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
crypto_digest_assign(d2, d1); crypto_digest_assign(d2, d1);
crypto_digest_add_bytes(d2, "mno", 3); crypto_digest_add_bytes(d2, "mno", 3);
crypto_digest_get_digest(d2, d_out1, sizeof(d_out1)); crypto_digest_get_digest(d2, d_out1, DIGEST512_LEN);
crypto_digest512(d_out2, "abcdefmno", 9, DIGEST_SHA512); crypto_digest512(d_out2, "abcdefmno", 9, DIGEST_SHA512);
tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN); tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
crypto_digest_get_digest(d1, d_out1, sizeof(d_out1)); crypto_digest_get_digest(d1, d_out1, DIGEST512_LEN);
crypto_digest512(d_out2, "abcdef", 6, DIGEST_SHA512); crypto_digest512(d_out2, "abcdef", 6, DIGEST_SHA512);
tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN); tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
done: done:
if (d1) if (d1)