From 30f8aaad8c33be5730d3df2c5ebfca71dc7b3ca7 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Sun, 14 May 2017 21:56:58 -0400 Subject: [PATCH] replace XMEMCMP with compare_testvector, other consistency cleanup in testing code. --- src/hashes/blake2b.c | 12 +++-- src/hashes/blake2s.c | 12 +++-- src/hashes/chc/chc.c | 14 +++--- src/hashes/md2.c | 9 ++-- src/hashes/md4.c | 19 ++++---- src/hashes/md5.c | 2 +- src/hashes/rmd128.c | 18 ++++---- src/hashes/rmd160.c | 18 ++++---- src/hashes/rmd256.c | 18 ++++---- src/hashes/rmd320.c | 18 ++++---- src/hashes/sha1.c | 2 +- src/hashes/sha2/sha224.c | 2 +- src/hashes/sha2/sha256.c | 2 +- src/hashes/sha2/sha384.c | 2 +- src/hashes/sha2/sha512.c | 2 +- src/hashes/sha2/sha512_224.c | 2 +- src/hashes/sha2/sha512_256.c | 2 +- src/hashes/sha3_test.c | 87 +++++++++++++----------------------- src/hashes/tiger.c | 2 +- src/hashes/whirl/whirl.c | 9 +--- 20 files changed, 112 insertions(+), 140 deletions(-) diff --git a/src/hashes/blake2b.c b/src/hashes/blake2b.c index b01f63e..51c1ee4 100644 --- a/src/hashes/blake2b.c +++ b/src/hashes/blake2b.c @@ -432,8 +432,9 @@ int blake2b_512_test(void) blake2b_512_init(&md); blake2b_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); blake2b_done(&md, tmp); - if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2B_512", i)) + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2B_512", i)) { return CRYPT_FAIL_TESTVECTOR; + } } return CRYPT_OK; #endif @@ -478,8 +479,9 @@ int blake2b_384_test(void) blake2b_384_init(&md); blake2b_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); blake2b_done(&md, tmp); - if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2B_384", i)) + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2B_384", i)) { return CRYPT_FAIL_TESTVECTOR; + } } return CRYPT_OK; #endif @@ -530,8 +532,9 @@ int blake2b_256_test(void) blake2b_256_init(&md); blake2b_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); blake2b_done(&md, tmp); - if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2B_256", i)) + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2B_256", i)) { return CRYPT_FAIL_TESTVECTOR; + } } return CRYPT_OK; #endif @@ -570,8 +573,9 @@ int blake2b_160_test(void) blake2b_160_init(&md); blake2b_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); blake2b_done(&md, tmp); - if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2B_160", i)) + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2B_160", i)) { return CRYPT_FAIL_TESTVECTOR; + } } return CRYPT_OK; #endif diff --git a/src/hashes/blake2s.c b/src/hashes/blake2s.c index daa45a5..6a7b24f 100644 --- a/src/hashes/blake2s.c +++ b/src/hashes/blake2s.c @@ -422,8 +422,9 @@ int blake2s_256_test(void) blake2s_256_init(&md); blake2s_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); blake2s_done(&md, tmp); - if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2S_256", i)) + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2S_256", i)) { return CRYPT_FAIL_TESTVECTOR; + } } return CRYPT_OK; @@ -465,8 +466,9 @@ int blake2s_224_test(void) blake2s_224_init(&md); blake2s_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); blake2s_done(&md, tmp); - if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2S_224", i)) + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2S_224", i)) { return CRYPT_FAIL_TESTVECTOR; + } } return CRYPT_OK; @@ -506,8 +508,9 @@ int blake2s_160_test(void) blake2s_160_init(&md); blake2s_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); blake2s_done(&md, tmp); - if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2S_160", i)) + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2S_160", i)) { return CRYPT_FAIL_TESTVECTOR; + } } return CRYPT_OK; @@ -545,8 +548,9 @@ int blake2s_128_test(void) blake2s_128_init(&md); blake2s_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); blake2s_done(&md, tmp); - if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2S_128", i)) + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "BLAKE2S_128", i)) { return CRYPT_FAIL_TESTVECTOR; + } } return CRYPT_OK; #endif diff --git a/src/hashes/chc/chc.c b/src/hashes/chc/chc.c index bff4d80..889291e 100644 --- a/src/hashes/chc/chc.c +++ b/src/hashes/chc/chc.c @@ -256,7 +256,7 @@ int chc_test(void) #else static const struct { unsigned char *msg, - md[MAXBLOCKSIZE]; + hash[MAXBLOCKSIZE]; int len; } tests[] = { { @@ -266,8 +266,8 @@ int chc_test(void) 16 } }; - int x, oldhashidx, idx; - unsigned char out[MAXBLOCKSIZE]; + int i, oldhashidx, idx; + unsigned char tmp[MAXBLOCKSIZE]; hash_state md; /* AES can be under rijndael or aes... try to find it */ @@ -279,11 +279,11 @@ int chc_test(void) oldhashidx = cipher_idx; chc_register(idx); - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { chc_init(&md); - chc_process(&md, tests[x].msg, strlen((char *)tests[x].msg)); - chc_done(&md, out); - if (XMEMCMP(out, tests[x].md, tests[x].len)) { + chc_process(&md, tests[i].msg, strlen((char *)tests[i].msg)); + chc_done(&md, tmp); + if (compare_testvector(tmp, tests[i].len, tests[i].hash, tests[i].len, "CHC", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/md2.c b/src/hashes/md2.c index 0410923..e5ce4bb 100644 --- a/src/hashes/md2.c +++ b/src/hashes/md2.c @@ -194,7 +194,7 @@ int md2_test(void) #else static const struct { char *msg; - unsigned char md[16]; + unsigned char hash[16]; } tests[] = { { "", {0x83,0x50,0xe5,0xa3,0xe2,0x4c,0x15,0x3d, @@ -227,15 +227,16 @@ int md2_test(void) } } }; + int i; + unsigned char tmp[16]; hash_state md; - unsigned char buf[16]; for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { md2_init(&md); md2_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); - md2_done(&md, buf); - if (XMEMCMP(buf, tests[i].md, 16) != 0) { + md2_done(&md, tmp); + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "MD2", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/md4.c b/src/hashes/md4.c index b2527b5..be4b129 100644 --- a/src/hashes/md4.c +++ b/src/hashes/md4.c @@ -257,8 +257,8 @@ int md4_test(void) #else static const struct md4_test_case { char *input; - unsigned char digest[16]; - } cases[] = { + unsigned char hash[16]; + } tests[] = { { "", {0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31, 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0} }, @@ -281,15 +281,16 @@ int md4_test(void) {0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19, 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36} }, }; - int i; - hash_state md; - unsigned char digest[16]; - for(i = 0; i < (int)(sizeof(cases) / sizeof(cases[0])); i++) { + int i; + unsigned char tmp[16]; + hash_state md; + + for(i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { md4_init(&md); - md4_process(&md, (unsigned char *)cases[i].input, (unsigned long)strlen(cases[i].input)); - md4_done(&md, digest); - if (XMEMCMP(digest, cases[i].digest, 16) != 0) { + md4_process(&md, (unsigned char *)tests[i].input, (unsigned long)strlen(tests[i].input)); + md4_done(&md, tmp); + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "MD4", i)) { return CRYPT_FAIL_TESTVECTOR; } diff --git a/src/hashes/md5.c b/src/hashes/md5.c index 1d0ec92..c76e76b 100644 --- a/src/hashes/md5.c +++ b/src/hashes/md5.c @@ -351,7 +351,7 @@ int md5_test(void) md5_init(&md); md5_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); md5_done(&md, tmp); - if (XMEMCMP(tmp, tests[i].hash, 16) != 0) { + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "MD5", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/rmd128.c b/src/hashes/rmd128.c index af16f1f..135d83e 100644 --- a/src/hashes/rmd128.c +++ b/src/hashes/rmd128.c @@ -356,7 +356,7 @@ int rmd128_test(void) #else static const struct { char *msg; - unsigned char md[16]; + unsigned char hash[16]; } tests[] = { { "", { 0xcd, 0xf2, 0x62, 0x13, 0xa1, 0x50, 0xdc, 0x3e, @@ -383,18 +383,16 @@ int rmd128_test(void) 0xae, 0xa4, 0x62, 0x4c, 0x60, 0xc5, 0xc7, 0x02 } } }; - int x; - unsigned char buf[16]; + + int i; + unsigned char tmp[16]; hash_state md; - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { rmd128_init(&md); - rmd128_process(&md, (unsigned char *)tests[x].msg, strlen(tests[x].msg)); - rmd128_done(&md, buf); - if (XMEMCMP(buf, tests[x].md, 16) != 0) { - #if 0 - printf("Failed test %d\n", x); - #endif + rmd128_process(&md, (unsigned char *)tests[i].msg, strlen(tests[i].msg)); + rmd128_done(&md, tmp); + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "RIPEMD128", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/rmd160.c b/src/hashes/rmd160.c index ac41e5b..133c75c 100644 --- a/src/hashes/rmd160.c +++ b/src/hashes/rmd160.c @@ -415,7 +415,7 @@ int rmd160_test(void) #else static const struct { char *msg; - unsigned char md[20]; + unsigned char hash[20]; } tests[] = { { "", { 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28, @@ -442,18 +442,16 @@ int rmd160_test(void) 0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, 0xda, 0x62, 0xeb, 0x2b } } }; - int x; - unsigned char buf[20]; + + int i; + unsigned char tmp[20]; hash_state md; - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { rmd160_init(&md); - rmd160_process(&md, (unsigned char *)tests[x].msg, strlen(tests[x].msg)); - rmd160_done(&md, buf); - if (XMEMCMP(buf, tests[x].md, 20) != 0) { -#if 0 - printf("Failed test %d\n", x); -#endif + rmd160_process(&md, (unsigned char *)tests[i].msg, strlen(tests[i].msg)); + rmd160_done(&md, tmp); + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "RIPEMD160", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/rmd256.c b/src/hashes/rmd256.c index cbfadcc..fa90c09 100644 --- a/src/hashes/rmd256.c +++ b/src/hashes/rmd256.c @@ -369,7 +369,7 @@ int rmd256_test(void) #else static const struct { char *msg; - unsigned char md[32]; + unsigned char hash[32]; } tests[] = { { "", { 0x02, 0xba, 0x4c, 0x4e, 0x5f, 0x8e, 0xcd, 0x18, @@ -408,18 +408,16 @@ int rmd256_test(void) 0xa8, 0x9f, 0x7e, 0xa6, 0xde, 0x77, 0xa0, 0xb8 } } }; - int x; - unsigned char buf[32]; + + int i; + unsigned char tmp[32]; hash_state md; - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { rmd256_init(&md); - rmd256_process(&md, (unsigned char *)tests[x].msg, strlen(tests[x].msg)); - rmd256_done(&md, buf); - if (XMEMCMP(buf, tests[x].md, 32) != 0) { - #if 0 - printf("Failed test %d\n", x); - #endif + rmd256_process(&md, (unsigned char *)tests[i].msg, strlen(tests[i].msg)); + rmd256_done(&md, tmp); + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "RIPEMD256", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/rmd320.c b/src/hashes/rmd320.c index 26119f9..05b2dec 100644 --- a/src/hashes/rmd320.c +++ b/src/hashes/rmd320.c @@ -434,7 +434,7 @@ int rmd320_test(void) #else static const struct { char *msg; - unsigned char md[40]; + unsigned char hash[40]; } tests[] = { { "", { 0x22, 0xd6, 0x5d, 0x56, 0x61, 0x53, 0x6c, 0xdc, 0x75, 0xc1, @@ -473,18 +473,16 @@ int rmd320_test(void) 0xbc, 0x74, 0x70, 0xa9, 0x69, 0xc9, 0xd0, 0x72, 0xa1, 0xac } } }; - int x; - unsigned char buf[40]; + + int i; + unsigned char tmp[40]; hash_state md; - for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) { + for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { rmd320_init(&md); - rmd320_process(&md, (unsigned char *)tests[x].msg, strlen(tests[x].msg)); - rmd320_done(&md, buf); - if (XMEMCMP(buf, tests[x].md, 40) != 0) { -#if 0 - printf("Failed test %d\n", x); -#endif + rmd320_process(&md, (unsigned char *)tests[i].msg, strlen(tests[i].msg)); + rmd320_done(&md, tmp); + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "RIPEMD320", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/sha1.c b/src/hashes/sha1.c index 96c3b93..b4324b1 100644 --- a/src/hashes/sha1.c +++ b/src/hashes/sha1.c @@ -271,7 +271,7 @@ int sha1_test(void) sha1_init(&md); sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); sha1_done(&md, tmp); - if (XMEMCMP(tmp, tests[i].hash, 20) != 0) { + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA1", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/sha2/sha224.c b/src/hashes/sha2/sha224.c index 2240aaf..d19eb1f 100644 --- a/src/hashes/sha2/sha224.c +++ b/src/hashes/sha2/sha224.c @@ -115,7 +115,7 @@ int sha224_test(void) sha224_init(&md); sha224_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); sha224_done(&md, tmp); - if (XMEMCMP(tmp, tests[i].hash, 28) != 0) { + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA224", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/sha2/sha256.c b/src/hashes/sha2/sha256.c index 13ec9e6..02fae83 100644 --- a/src/hashes/sha2/sha256.c +++ b/src/hashes/sha2/sha256.c @@ -319,7 +319,7 @@ int sha256_test(void) sha256_init(&md); sha256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); sha256_done(&md, tmp); - if (XMEMCMP(tmp, tests[i].hash, 32) != 0) { + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA256", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/sha2/sha384.c b/src/hashes/sha2/sha384.c index 483784b..c7fb02a 100644 --- a/src/hashes/sha2/sha384.c +++ b/src/hashes/sha2/sha384.c @@ -121,7 +121,7 @@ int sha384_test(void) sha384_init(&md); sha384_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); sha384_done(&md, tmp); - if (XMEMCMP(tmp, tests[i].hash, 48) != 0) { + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA384", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/sha2/sha512.c b/src/hashes/sha2/sha512.c index fbf14de..951865e 100644 --- a/src/hashes/sha2/sha512.c +++ b/src/hashes/sha2/sha512.c @@ -297,7 +297,7 @@ int sha512_test(void) sha512_init(&md); sha512_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); sha512_done(&md, tmp); - if (XMEMCMP(tmp, tests[i].hash, 64) != 0) { + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA512", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/sha2/sha512_224.c b/src/hashes/sha2/sha512_224.c index 98fba3a..b485cf5 100644 --- a/src/hashes/sha2/sha512_224.c +++ b/src/hashes/sha2/sha512_224.c @@ -117,7 +117,7 @@ int sha512_224_test(void) sha512_224_init(&md); sha512_224_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); sha512_224_done(&md, tmp); - if (XMEMCMP(tmp, tests[i].hash, 28) != 0) { + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA512-224", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/sha2/sha512_256.c b/src/hashes/sha2/sha512_256.c index 86e4bac..8bfbdcc 100644 --- a/src/hashes/sha2/sha512_256.c +++ b/src/hashes/sha2/sha512_256.c @@ -117,7 +117,7 @@ int sha512_256_test(void) sha512_256_init(&md); sha512_256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); sha512_256_done(&md, tmp); - if (XMEMCMP(tmp, tests[i].hash, 32) != 0) { + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA512-265", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/sha3_test.c b/src/hashes/sha3_test.c index b4b3d8d..8804a6a 100644 --- a/src/hashes/sha3_test.c +++ b/src/hashes/sha3_test.c @@ -18,7 +18,7 @@ int sha3_224_test(void) #ifndef LTC_TEST return CRYPT_NOP; #else - unsigned char buf[200], hash[200]; + unsigned char buf[200], hash[224 / 8]; int i; hash_state c; const unsigned char c1 = 0xa3; @@ -42,8 +42,7 @@ int sha3_224_test(void) /* SHA3-224 on an empty buffer */ sha3_224_init(&c); sha3_done(&c, hash); - if(XMEMCMP(sha3_224_empty, hash, sizeof(sha3_224_empty)) != 0) { - printf("SHA3-224() failed\n"); + if (compare_testvector(hash, sizeof(hash), sha3_224_empty, sizeof(sha3_224_empty), "SHA3-224", 1)) { return CRYPT_FAIL_TESTVECTOR; } @@ -52,8 +51,7 @@ int sha3_224_test(void) sha3_process(&c, buf, sizeof(buf) / 2); sha3_process(&c, buf + sizeof(buf) / 2, sizeof(buf) / 2); sha3_done(&c, hash); - if(XMEMCMP(sha3_224_0xa3_200_times, hash, sizeof(sha3_224_0xa3_200_times)) != 0) { - printf("SHA3-224( 0xa3 ... [200 times] ) failed (2 steps)\n"); + if (compare_testvector(hash, sizeof(hash), sha3_224_0xa3_200_times, sizeof(sha3_224_0xa3_200_times), "SHA3-224", 1)) { return CRYPT_FAIL_TESTVECTOR; } @@ -64,8 +62,7 @@ int sha3_224_test(void) sha3_process(&c, &c1, 1); } sha3_done(&c, hash); - if(XMEMCMP(sha3_224_0xa3_200_times, hash, sizeof(sha3_224_0xa3_200_times)) != 0) { - printf("SHA3-224( 0xa3 ... [200 times] ) failed (200 steps)\n"); + if (compare_testvector(hash, sizeof(hash), sha3_224_0xa3_200_times, sizeof(sha3_224_0xa3_200_times), "SHA3-224", 2)) { return CRYPT_FAIL_TESTVECTOR; } @@ -78,7 +75,7 @@ int sha3_256_test(void) #ifndef LTC_TEST return CRYPT_NOP; #else - unsigned char buf[200], hash[200]; + unsigned char buf[200], hash[256 / 8]; int i; hash_state c; const unsigned char c1 = 0xa3; @@ -101,8 +98,7 @@ int sha3_256_test(void) /* SHA3-256 on an empty buffer */ sha3_256_init(&c); sha3_done(&c, hash); - if(XMEMCMP(sha3_256_empty, hash, sizeof(sha3_256_empty)) != 0) { - printf("SHA3-256() failed\n"); + if (compare_testvector(hash, sizeof(hash), sha3_256_empty, sizeof(sha3_256_empty), "SHA3-256", 1)) { return CRYPT_FAIL_TESTVECTOR; } @@ -110,8 +106,7 @@ int sha3_256_test(void) sha3_256_init(&c); sha3_process(&c, buf, sizeof(buf)); sha3_done(&c, hash); - if(XMEMCMP(sha3_256_0xa3_200_times, hash, sizeof(sha3_256_0xa3_200_times)) != 0) { - printf("SHA3-256( 0xa3 ... [200 times] ) failed (1 buffer)\n"); + if (compare_testvector(hash, sizeof(hash), sha3_256_0xa3_200_times, sizeof(sha3_256_0xa3_200_times), "SHA3-256", 2)) { return CRYPT_FAIL_TESTVECTOR; } @@ -120,8 +115,7 @@ int sha3_256_test(void) sha3_process(&c, buf, sizeof(buf) / 2); sha3_process(&c, buf + sizeof(buf) / 2, sizeof(buf) / 2); sha3_done(&c, hash); - if(XMEMCMP(sha3_256_0xa3_200_times, hash, sizeof(sha3_256_0xa3_200_times)) != 0) { - printf("SHA3-256( 0xa3 ... [200 times] ) failed (2 steps)\n"); + if (compare_testvector(hash, sizeof(hash), sha3_256_0xa3_200_times, sizeof(sha3_256_0xa3_200_times), "SHA3-224", 3)) { return CRYPT_FAIL_TESTVECTOR; } @@ -132,8 +126,7 @@ int sha3_256_test(void) sha3_process(&c, &c1, 1); } sha3_done(&c, hash); - if(XMEMCMP(sha3_256_0xa3_200_times, hash, sizeof(sha3_256_0xa3_200_times)) != 0) { - printf("SHA3-256( 0xa3 ... [200 times] ) failed (200 steps)\n"); + if (compare_testvector(hash, sizeof(hash), sha3_256_0xa3_200_times, sizeof(sha3_256_0xa3_200_times), "SHA3-224", 4)) { return CRYPT_FAIL_TESTVECTOR; } @@ -159,11 +152,11 @@ int sha3_256_test(void) "\x76\x3d\x52\xdb\x98\xd9\x49\xd3" "\xb0\xfe\xd6\xa8\x05\x2f\xbb", 1080 / 8); sha3_done(&c, hash); - if(XMEMCMP(hash, "\xa1\x9e\xee\x92\xbb\x20\x97\xb6" - "\x4e\x82\x3d\x59\x77\x98\xaa\x18" - "\xbe\x9b\x7c\x73\x6b\x80\x59\xab" - "\xfd\x67\x79\xac\x35\xac\x81\xb5", 256 / 8) != 0) { - printf("SHA3-256( b771 ... ) doesn't match the known answer\n"); + if(compare_testvector(hash, sizeof(hash), + "\xa1\x9e\xee\x92\xbb\x20\x97\xb6" + "\x4e\x82\x3d\x59\x77\x98\xaa\x18" + "\xbe\x9b\x7c\x73\x6b\x80\x59\xab" + "\xfd\x67\x79\xac\x35\xac\x81\xb5", 256 / 8, "SHA2-256", 5)) { return CRYPT_FAIL_TESTVECTOR; } @@ -176,7 +169,7 @@ int sha3_384_test(void) #ifndef LTC_TEST return CRYPT_NOP; #else - unsigned char buf[200], hash[200]; + unsigned char buf[200], hash[384 / 8]; int i; hash_state c; const unsigned char c1 = 0xa3; @@ -196,8 +189,7 @@ int sha3_384_test(void) sha3_384_init(&c); sha3_process(&c, buf, sizeof(buf)); sha3_done(&c, hash); - if(XMEMCMP(sha3_384_0xa3_200_times, hash, sizeof(sha3_384_0xa3_200_times)) != 0) { - printf("SHA3-384( 0xa3 ... [200 times] ) failed (1 buffer)\n"); + if (compare_testvector(hash, sizeof(hash), sha3_384_0xa3_200_times, sizeof(sha3_384_0xa3_200_times), "SHA3-384", 0)) { return CRYPT_FAIL_TESTVECTOR; } @@ -206,8 +198,7 @@ int sha3_384_test(void) sha3_process(&c, buf, sizeof(buf) / 2); sha3_process(&c, buf + sizeof(buf) / 2, sizeof(buf) / 2); sha3_done(&c, hash); - if(XMEMCMP(sha3_384_0xa3_200_times, hash, sizeof(sha3_384_0xa3_200_times)) != 0) { - printf("SHA3-384( 0xa3 ... [200 times] ) failed (2 steps)\n"); + if (compare_testvector(hash, sizeof(hash), sha3_384_0xa3_200_times, sizeof(sha3_384_0xa3_200_times), "SHA3-384", 1)) { return CRYPT_FAIL_TESTVECTOR; } @@ -218,8 +209,7 @@ int sha3_384_test(void) sha3_process(&c, &c1, 1); } sha3_done(&c, hash); - if(XMEMCMP(sha3_384_0xa3_200_times, hash, sizeof(sha3_384_0xa3_200_times)) != 0) { - printf("SHA3-384( 0xa3 ... [200 times] ) failed (200 steps)\n"); + if (compare_testvector(hash, sizeof(hash), sha3_384_0xa3_200_times, sizeof(sha3_384_0xa3_200_times), "SHA3-384", 2)) { return CRYPT_FAIL_TESTVECTOR; } @@ -232,7 +222,7 @@ int sha3_512_test(void) #ifndef LTC_TEST return CRYPT_NOP; #else - unsigned char buf[200], hash[200]; + unsigned char buf[200], hash[512 / 8]; int i; hash_state c; const unsigned char c1 = 0xa3; @@ -254,8 +244,7 @@ int sha3_512_test(void) sha3_512_init(&c); sha3_process(&c, buf, sizeof(buf)); sha3_done(&c, hash); - if(XMEMCMP(sha3_512_0xa3_200_times, hash, sizeof(sha3_512_0xa3_200_times)) != 0) { - printf("SHA3-512( 0xa3 ... [200 times] ) failed (1 buffer)\n"); + if (compare_testvector(hash, sizeof(hash), sha3_512_0xa3_200_times, sizeof(sha3_512_0xa3_200_times), "SHA3-512", 0)) { return CRYPT_FAIL_TESTVECTOR; } @@ -264,8 +253,7 @@ int sha3_512_test(void) sha3_process(&c, buf, sizeof(buf) / 2); sha3_process(&c, buf + sizeof(buf) / 2, sizeof(buf) / 2); sha3_done(&c, hash); - if(XMEMCMP(sha3_512_0xa3_200_times, hash, sizeof(sha3_512_0xa3_200_times)) != 0) { - printf("SHA3-512( 0xa3 ... [200 times] ) failed (2 steps)\n"); + if (compare_testvector(hash, sizeof(hash), sha3_512_0xa3_200_times, sizeof(sha3_512_0xa3_200_times), "SHA3-512", 1)) { return CRYPT_FAIL_TESTVECTOR; } @@ -276,8 +264,7 @@ int sha3_512_test(void) sha3_process(&c, &c1, 1); } sha3_done(&c, hash); - if(XMEMCMP(sha3_512_0xa3_200_times, hash, sizeof(sha3_512_0xa3_200_times)) != 0) { - printf("SHA3-512( 0xa3 ... [200 times] ) failed (200 steps)\n"); + if (compare_testvector(hash, sizeof(hash), sha3_512_0xa3_200_times, sizeof(sha3_512_0xa3_200_times), "SHA3-512", 2)) { return CRYPT_FAIL_TESTVECTOR; } @@ -326,16 +313,14 @@ int sha3_shake_test(void) /* SHAKE256 on an empty buffer */ sha3_shake_init(&c, 256); for (i = 0; i < 16; i++) sha3_shake_done(&c, hash, 32); /* get 512 bytes, keep in hash the last 32 */ - if(XMEMCMP(shake256_empty, hash, sizeof(shake256_empty)) != 0) { - printf("SHAKE256('') failed\n"); + if (compare_testvector(hash, sizeof(shake256_empty), shake256_empty, sizeof(shake256_empty), "SHAKE256", 0)) { return CRYPT_FAIL_TESTVECTOR; } /* SHAKE256 via sha3_shake_memory [FIPS 202] */ len = 512; sha3_shake_memory(256, buf, sizeof(buf), hash, &len); - if(XMEMCMP(shake256_0xa3_200_times, hash + 480, sizeof(shake256_0xa3_200_times)) != 0) { - printf("SHAKE256( 0xa3 ... [200 times] ) failed (sha3_shake_memory)\n"); + if (compare_testvector(hash + 480, sizeof(shake256_0xa3_200_times), shake256_0xa3_200_times, sizeof(shake256_0xa3_200_times), "SHAKE256", 1)) { return CRYPT_FAIL_TESTVECTOR; } @@ -343,8 +328,7 @@ int sha3_shake_test(void) sha3_shake_init(&c, 256); sha3_shake_process(&c, buf, sizeof(buf)); for (i = 0; i < 16; i++) sha3_shake_done(&c, hash, 32); /* get 512 bytes, keep in hash the last 32 */ - if(XMEMCMP(shake256_0xa3_200_times, hash, sizeof(shake256_0xa3_200_times)) != 0) { - printf("SHAKE256( 0xa3 ... [200 times] ) failed (1 buffer)\n"); + if (compare_testvector(hash, sizeof(shake256_0xa3_200_times), shake256_0xa3_200_times, sizeof(shake256_0xa3_200_times), "SHAKE256", 2)) { return CRYPT_FAIL_TESTVECTOR; } @@ -353,8 +337,7 @@ int sha3_shake_test(void) sha3_shake_process(&c, buf, sizeof(buf) / 2); sha3_shake_process(&c, buf + sizeof(buf) / 2, sizeof(buf) / 2); for (i = 0; i < 16; i++) sha3_shake_done(&c, hash, 32); /* get 512 bytes, keep in hash the last 32 */ - if(XMEMCMP(shake256_0xa3_200_times, hash, sizeof(shake256_0xa3_200_times)) != 0) { - printf("SHAKE256( 0xa3 ... [200 times] ) failed (2 steps)\n"); + if (compare_testvector(hash, sizeof(shake256_0xa3_200_times), shake256_0xa3_200_times, sizeof(shake256_0xa3_200_times), "SHAKE256", 3)) { return CRYPT_FAIL_TESTVECTOR; } @@ -363,24 +346,21 @@ int sha3_shake_test(void) sha3_shake_init(&c, 256); while (i--) sha3_shake_process(&c, &c1, 1); for (i = 0; i < 16; i++) sha3_shake_done(&c, hash, 32); /* get 512 bytes, keep in hash the last 32 */ - if(XMEMCMP(shake256_0xa3_200_times, hash, sizeof(shake256_0xa3_200_times)) != 0) { - printf("SHAKE256( 0xa3 ... [200 times] ) failed (200 steps)\n"); + if (compare_testvector(hash, sizeof(shake256_0xa3_200_times), shake256_0xa3_200_times, sizeof(shake256_0xa3_200_times), "SHAKE256", 3)) { return CRYPT_FAIL_TESTVECTOR; } /* SHAKE128 on an empty buffer */ sha3_shake_init(&c, 128); for (i = 0; i < 16; i++) sha3_shake_done(&c, hash, 32); /* get 512 bytes, keep in hash the last 32 */ - if(XMEMCMP(shake128_empty, hash, sizeof(shake128_empty)) != 0) { - printf("SHAKE128() failed\n"); + if (compare_testvector(hash, sizeof(shake128_empty), shake128_empty, sizeof(shake128_empty), "SHAKE128", 0)) { return CRYPT_FAIL_TESTVECTOR; } /* SHAKE128 via sha3_shake_memory [FIPS 202] */ len = 512; sha3_shake_memory(128, buf, sizeof(buf), hash, &len); - if(XMEMCMP(shake128_0xa3_200_times, hash + 480, sizeof(shake128_0xa3_200_times)) != 0) { - printf("SHAKE128( 0xa3 ... [200 times] ) failed (sha3_shake_memory)\n"); + if (compare_testvector(hash + 480, sizeof(shake128_0xa3_200_times), shake128_0xa3_200_times, sizeof(shake128_0xa3_200_times), "SHAKE128", 1)) { return CRYPT_FAIL_TESTVECTOR; } @@ -388,8 +368,7 @@ int sha3_shake_test(void) sha3_shake_init(&c, 128); sha3_shake_process(&c, buf, sizeof(buf)); for (i = 0; i < 16; i++) sha3_shake_done(&c, hash, 32); /* get 512 bytes, keep in hash the last 32 */ - if(XMEMCMP(shake128_0xa3_200_times, hash, sizeof(shake128_0xa3_200_times)) != 0) { - printf("SHAKE128( 0xa3 ... [200 times] ) failed (1 buffer)\n"); + if (compare_testvector(hash, sizeof(shake128_0xa3_200_times), shake128_0xa3_200_times, sizeof(shake128_0xa3_200_times), "SHAKE128", 2)) { return CRYPT_FAIL_TESTVECTOR; } @@ -398,8 +377,7 @@ int sha3_shake_test(void) sha3_shake_process(&c, buf, sizeof(buf) / 2); sha3_shake_process(&c, buf + sizeof(buf) / 2, sizeof(buf) / 2); for (i = 0; i < 16; i++) sha3_shake_done(&c, hash, 32); /* get 512 bytes, keep in hash the last 32 */ - if(XMEMCMP(shake128_0xa3_200_times, hash, sizeof(shake128_0xa3_200_times)) != 0) { - printf("SHAKE128( 0xa3 ... [200 times] ) failed (2 steps)\n"); + if (compare_testvector(hash, sizeof(shake128_0xa3_200_times), shake128_0xa3_200_times, sizeof(shake128_0xa3_200_times), "SHAKE128", 3)) { return CRYPT_FAIL_TESTVECTOR; } @@ -408,8 +386,7 @@ int sha3_shake_test(void) sha3_shake_init(&c, 128); while (i--) sha3_shake_process(&c, &c1, 1); for (i = 0; i < 16; i++) sha3_shake_done(&c, hash, 32); /* get 512 bytes, keep in hash the last 32 */ - if(XMEMCMP(shake128_0xa3_200_times, hash, sizeof(shake128_0xa3_200_times)) != 0) { - printf("SHAKE128( 0xa3 ... [200 times] ) failed (200 steps)\n"); + if (compare_testvector(hash, sizeof(shake128_0xa3_200_times), shake128_0xa3_200_times, sizeof(shake128_0xa3_200_times), "SHAKE128", 4)) { return CRYPT_FAIL_TESTVECTOR; } diff --git a/src/hashes/tiger.c b/src/hashes/tiger.c index dcacb64..e117e77 100644 --- a/src/hashes/tiger.c +++ b/src/hashes/tiger.c @@ -775,7 +775,7 @@ int tiger_test(void) tiger_init(&md); tiger_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); tiger_done(&md, tmp); - if (XMEMCMP(tmp, tests[i].hash, 24) != 0) { + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "TIGER", i)) { return CRYPT_FAIL_TESTVECTOR; } } diff --git a/src/hashes/whirl/whirl.c b/src/hashes/whirl/whirl.c index 525d75b..5e0b231 100644 --- a/src/hashes/whirl/whirl.c +++ b/src/hashes/whirl/whirl.c @@ -291,14 +291,7 @@ int whirlpool_test(void) whirlpool_init(&md); whirlpool_process(&md, (unsigned char *)tests[i].msg, tests[i].len); whirlpool_done(&md, tmp); - if (XMEMCMP(tmp, tests[i].hash, 64) != 0) { -#if 0 - printf("\nFailed test %d\n", i); - for (i = 0; i < 64; ) { - printf("%02x ", tmp[i]); - if (!(++i & 15)) printf("\n"); - } -#endif + if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "WHIRLPOOL", i)) { return CRYPT_FAIL_TESTVECTOR; } }