diff --git a/src/hashes/sha2/sha224.c b/src/hashes/sha2/sha224.c index 156ba176..5e8acc98 100644 --- a/src/hashes/sha2/sha224.c +++ b/src/hashes/sha2/sha224.c @@ -9,7 +9,7 @@ #if defined(LTC_SHA224) && defined(LTC_SHA256) -const struct ltc_hash_descriptor sha224_c_desc = +const struct ltc_hash_descriptor sha224_portable_desc = { "sha224", 10, @@ -21,7 +21,7 @@ const struct ltc_hash_descriptor sha224_c_desc = 9, &sha224_c_init, - &sha256_process, + &sha256_c_process, &sha224_c_done, &sha224_test, NULL @@ -78,41 +78,7 @@ int sha224_c_done(hash_state * md, unsigned char *out) */ int sha224_c_test(void) { - #ifndef LTC_TEST - return CRYPT_NOP; - #else - static const struct { - const char *msg; - unsigned char hash[28]; - } tests[] = { - { "abc", - { 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, - 0x22, 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, - 0x55, 0xb3, 0x2a, 0xad, 0xbc, 0xe4, 0xbd, - 0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d, 0xa7 } - }, - { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - { 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76, - 0xcc, 0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89, - 0x01, 0x50, 0xb0, 0xc6, 0x45, 0x5c, 0xb4, - 0xf5, 0x8b, 0x19, 0x52, 0x52, 0x25, 0x25 } - }, - }; - - int i; - unsigned char tmp[28]; - hash_state md; - - for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { - sha224_c_init(&md); - sha224_c_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg)); - sha224_c_done(&md, tmp); - if (ltc_compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA224", i)) { - return CRYPT_FAIL_TESTVECTOR; - } - } - return CRYPT_OK; - #endif + return sha224_test_desc(&sha224_portable_desc, "SHA224 portable"); } #endif /* defined(LTC_SHA224) && defined(LTC_SHA256) */ diff --git a/src/hashes/sha2/sha256.c b/src/hashes/sha2/sha256.c index ea17b9dc..77f47aa7 100644 --- a/src/hashes/sha2/sha256.c +++ b/src/hashes/sha2/sha256.c @@ -9,7 +9,7 @@ #ifdef LTC_SHA256 -const struct ltc_hash_descriptor sha256_c_desc = +const struct ltc_hash_descriptor sha256_portable_desc = { "sha256", 0, @@ -318,41 +318,7 @@ int sha256_c_done(hash_state * md, unsigned char *out) */ int sha256_c_test(void) { - #ifndef LTC_TEST - return CRYPT_NOP; - #else - static const struct { - const char *msg; - unsigned char hash[32]; - } tests[] = { - { "abc", - { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, - 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, - 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, - 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad } - }, - { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", - { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, - 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, - 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, - 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 } - }, - }; - - int i; - unsigned char tmp[32]; - hash_state md; - - for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { - sha256_c_init(&md); - sha256_c_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg)); - sha256_c_done(&md, tmp); - if (ltc_compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA256", i)) { - return CRYPT_FAIL_TESTVECTOR; - } - } - return CRYPT_OK; - #endif + return sha256_test_desc(&sha256_portable_desc, "SHA256 portable"); } #undef Ch diff --git a/src/hashes/sha2/sha256_x86.c b/src/hashes/sha2/sha256_x86.c index d3eb70fa..de4249af 100644 --- a/src/hashes/sha2/sha256_x86.c +++ b/src/hashes/sha2/sha256_x86.c @@ -43,6 +43,7 @@ const struct ltc_hash_descriptor sha256_x86_desc = }; /* the K array */ +#define K sha256_x86_K #pragma pack(push) #pragma pack(16) /* todo #pragma pack might not work */ static const ulong32 K[64] = { @@ -253,6 +254,7 @@ static int ltc_attribute_sha256 s_sha256_x86_compress(hash_state * md, const uns _mm_store_si128(((__m128i*)(&md->sha256_x86.state[4])), state_1); return CRYPT_OK; } +#undef K #ifdef LTC_CLEAN_STACK static int s_sha256_compress(hash_state * md, const unsigned char *buf) diff --git a/src/hashes/sha2/sha512.c b/src/hashes/sha2/sha512.c index d4a16a36..5086a823 100644 --- a/src/hashes/sha2/sha512.c +++ b/src/hashes/sha2/sha512.c @@ -28,6 +28,7 @@ const struct ltc_hash_descriptor sha512_desc = }; /* the K array */ +#define K sha512_K static const ulong64 K[80] = { CONST64(0x428a2f98d728ae22), CONST64(0x7137449123ef65cd), CONST64(0xb5c0fbcfec4d3b2f), CONST64(0xe9b5dba58189dbbc), @@ -186,6 +187,7 @@ static int s_sha512_compress(hash_state * md, const unsigned char *buf) return CRYPT_OK; } +#undef K /* compress 1024-bits */ #ifdef LTC_CLEAN_STACK diff --git a/src/headers/tomcrypt_hash.h b/src/headers/tomcrypt_hash.h index fc0547ec..23ff9d1d 100644 --- a/src/headers/tomcrypt_hash.h +++ b/src/headers/tomcrypt_hash.h @@ -413,7 +413,7 @@ int sha256_c_init(hash_state * md); int sha256_c_process(hash_state * md, const unsigned char *in, unsigned long inlen); int sha256_c_done(hash_state * md, unsigned char *out); int sha256_c_test(void); -extern const struct ltc_hash_descriptor sha256_c_desc; +extern const struct ltc_hash_descriptor sha256_portable_desc; #ifdef LTC_SHA256_X86 int sha256_x86_init(hash_state * md); @@ -437,7 +437,7 @@ int sha224_c_init(hash_state * md); #define sha224_c_process sha256_c_process int sha224_c_done(hash_state * md, unsigned char *out); int sha224_c_test(void); -extern const struct ltc_hash_descriptor sha224_c_desc; +extern const struct ltc_hash_descriptor sha224_portable_desc; #ifdef LTC_SHA224_X86 int sha224_x86_init(hash_state * md); diff --git a/tests/test.c b/tests/test.c index 9fc38794..8eb7d7d0 100644 --- a/tests/test.c +++ b/tests/test.c @@ -204,24 +204,26 @@ static void s_unregister_all(void) unregister_hash(&md5_desc); #endif #ifdef LTC_SHA1 + /* `register_all_hashes()` does not register + * - `sha1_portable_desc` + * - `sha1_x86_desc` + * so we don't have to unregister them */ unregister_hash(&sha1_desc); - unregister_hash(&sha1_portable_desc); -#endif -#ifdef LTC_SHA1_X86 - unregister_hash(&sha1_x86_desc); #endif #ifdef LTC_SHA224 + /* `register_all_hashes()` does not register + * - `sha224_portable_desc` + * - `sha224_x86_desc` + * so we don't have to unregister them */ unregister_hash(&sha224_desc); #endif -#ifdef LTC_SHA224_X86 - unregister_hash(&sha224_x86_desc); -#endif #ifdef LTC_SHA256 + /* `register_all_hashes()` does not register + * - `sha256_portable_desc` + * - `sha256_x86_desc` + * so we don't have to unregister them */ unregister_hash(&sha256_desc); #endif -#ifdef LTC_SHA256_X86 - unregister_hash(&sha256_x86_desc); -#endif #ifdef LTC_SHA384 unregister_hash(&sha384_desc); #endif