diff --git a/makefile.mingw b/makefile.mingw index df39d6fbc..ab1874c40 100644 --- a/makefile.mingw +++ b/makefile.mingw @@ -244,12 +244,12 @@ src/stream/sosemanuk/sosemanuk_memory.o src/stream/sosemanuk/sosemanuk_test.o TOBJECTS=tests/argon2_test.o tests/base16_test.o tests/base32_test.o tests/base64_test.o \ tests/bcrypt_test.o tests/cipher_hash_test.o tests/common.o tests/deprecated_test.o tests/der_test.o \ tests/dh_test.o tests/dsa_test.o tests/ecc_test.o tests/ed25519_test.o tests/ed448_test.o \ -tests/file_test.o tests/mac_test.o tests/misc_test.o tests/modes_test.o tests/mpi_test.o \ -tests/multi_test.o tests/no_null_termination_check_test.o tests/no_prng.o tests/padding_test.o \ -tests/pem_test.o tests/pk_oid_test.o tests/pkcs_1_eme_test.o tests/pkcs_1_emsa_test.o \ -tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o tests/prng_test.o \ -tests/rotate_test.o tests/rsa_test.o tests/scrypt_test.o tests/siv_wycheproof_test.o tests/ssh_test.o \ -tests/store_test.o tests/test.o tests/x25519_test.o tests/x448_test.o +tests/file_test.o tests/hash_state_test.o tests/mac_test.o tests/misc_test.o tests/modes_test.o \ +tests/mpi_test.o tests/multi_test.o tests/no_null_termination_check_test.o tests/no_prng.o \ +tests/padding_test.o tests/pem_test.o tests/pk_oid_test.o tests/pkcs_1_eme_test.o \ +tests/pkcs_1_emsa_test.o tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o \ +tests/prng_test.o tests/rotate_test.o tests/rsa_test.o tests/scrypt_test.o tests/siv_wycheproof_test.o \ +tests/ssh_test.o tests/store_test.o tests/test.o tests/x25519_test.o tests/x448_test.o #The following headers will be installed by "make install" HEADERS_PUB=src/headers/tomcrypt.h src/headers/tomcrypt_argchk.h src/headers/tomcrypt_cfg.h \ diff --git a/makefile.msvc b/makefile.msvc index e9028e8ab..7d4f8dae5 100644 --- a/makefile.msvc +++ b/makefile.msvc @@ -237,12 +237,12 @@ src/stream/sosemanuk/sosemanuk_memory.obj src/stream/sosemanuk/sosemanuk_test.ob TOBJECTS=tests/argon2_test.obj tests/base16_test.obj tests/base32_test.obj tests/base64_test.obj \ tests/bcrypt_test.obj tests/cipher_hash_test.obj tests/common.obj tests/deprecated_test.obj tests/der_test.obj \ tests/dh_test.obj tests/dsa_test.obj tests/ecc_test.obj tests/ed25519_test.obj tests/ed448_test.obj \ -tests/file_test.obj tests/mac_test.obj tests/misc_test.obj tests/modes_test.obj tests/mpi_test.obj \ -tests/multi_test.obj tests/no_null_termination_check_test.obj tests/no_prng.obj tests/padding_test.obj \ -tests/pem_test.obj tests/pk_oid_test.obj tests/pkcs_1_eme_test.obj tests/pkcs_1_emsa_test.obj \ -tests/pkcs_1_oaep_test.obj tests/pkcs_1_pss_test.obj tests/pkcs_1_test.obj tests/prng_test.obj \ -tests/rotate_test.obj tests/rsa_test.obj tests/scrypt_test.obj tests/siv_wycheproof_test.obj tests/ssh_test.obj \ -tests/store_test.obj tests/test.obj tests/x25519_test.obj tests/x448_test.obj +tests/file_test.obj tests/hash_state_test.obj tests/mac_test.obj tests/misc_test.obj tests/modes_test.obj \ +tests/mpi_test.obj tests/multi_test.obj tests/no_null_termination_check_test.obj tests/no_prng.obj \ +tests/padding_test.obj tests/pem_test.obj tests/pk_oid_test.obj tests/pkcs_1_eme_test.obj \ +tests/pkcs_1_emsa_test.obj tests/pkcs_1_oaep_test.obj tests/pkcs_1_pss_test.obj tests/pkcs_1_test.obj \ +tests/prng_test.obj tests/rotate_test.obj tests/rsa_test.obj tests/scrypt_test.obj tests/siv_wycheproof_test.obj \ +tests/ssh_test.obj tests/store_test.obj tests/test.obj tests/x25519_test.obj tests/x448_test.obj #The following headers will be installed by "make install" HEADERS_PUB=src/headers/tomcrypt.h src/headers/tomcrypt_argchk.h src/headers/tomcrypt_cfg.h \ diff --git a/makefile.unix b/makefile.unix index b07b43b8c..2d8db2d98 100644 --- a/makefile.unix +++ b/makefile.unix @@ -258,12 +258,12 @@ src/stream/sosemanuk/sosemanuk_memory.o src/stream/sosemanuk/sosemanuk_test.o TOBJECTS=tests/argon2_test.o tests/base16_test.o tests/base32_test.o tests/base64_test.o \ tests/bcrypt_test.o tests/cipher_hash_test.o tests/common.o tests/deprecated_test.o tests/der_test.o \ tests/dh_test.o tests/dsa_test.o tests/ecc_test.o tests/ed25519_test.o tests/ed448_test.o \ -tests/file_test.o tests/mac_test.o tests/misc_test.o tests/modes_test.o tests/mpi_test.o \ -tests/multi_test.o tests/no_null_termination_check_test.o tests/no_prng.o tests/padding_test.o \ -tests/pem_test.o tests/pk_oid_test.o tests/pkcs_1_eme_test.o tests/pkcs_1_emsa_test.o \ -tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o tests/prng_test.o \ -tests/rotate_test.o tests/rsa_test.o tests/scrypt_test.o tests/siv_wycheproof_test.o tests/ssh_test.o \ -tests/store_test.o tests/test.o tests/x25519_test.o tests/x448_test.o +tests/file_test.o tests/hash_state_test.o tests/mac_test.o tests/misc_test.o tests/modes_test.o \ +tests/mpi_test.o tests/multi_test.o tests/no_null_termination_check_test.o tests/no_prng.o \ +tests/padding_test.o tests/pem_test.o tests/pk_oid_test.o tests/pkcs_1_eme_test.o \ +tests/pkcs_1_emsa_test.o tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o \ +tests/prng_test.o tests/rotate_test.o tests/rsa_test.o tests/scrypt_test.o tests/siv_wycheproof_test.o \ +tests/ssh_test.o tests/store_test.o tests/test.o tests/x25519_test.o tests/x448_test.o #The following headers will be installed by "make install" HEADERS_PUB=src/headers/tomcrypt.h src/headers/tomcrypt_argchk.h src/headers/tomcrypt_cfg.h \ diff --git a/makefile_include.mk b/makefile_include.mk index 0577b5bda..b37973155 100644 --- a/makefile_include.mk +++ b/makefile_include.mk @@ -434,12 +434,12 @@ endif TOBJECTS=tests/argon2_test.o tests/base16_test.o tests/base32_test.o tests/base64_test.o \ tests/bcrypt_test.o tests/cipher_hash_test.o tests/common.o tests/deprecated_test.o tests/der_test.o \ tests/dh_test.o tests/dsa_test.o tests/ecc_test.o tests/ed25519_test.o tests/ed448_test.o \ -tests/file_test.o tests/mac_test.o tests/misc_test.o tests/modes_test.o tests/mpi_test.o \ -tests/multi_test.o tests/no_null_termination_check_test.o tests/no_prng.o tests/padding_test.o \ -tests/pem_test.o tests/pk_oid_test.o tests/pkcs_1_eme_test.o tests/pkcs_1_emsa_test.o \ -tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o tests/prng_test.o \ -tests/rotate_test.o tests/rsa_test.o tests/scrypt_test.o tests/siv_wycheproof_test.o tests/ssh_test.o \ -tests/store_test.o tests/test.o tests/x25519_test.o tests/x448_test.o +tests/file_test.o tests/hash_state_test.o tests/mac_test.o tests/misc_test.o tests/modes_test.o \ +tests/mpi_test.o tests/multi_test.o tests/no_null_termination_check_test.o tests/no_prng.o \ +tests/padding_test.o tests/pem_test.o tests/pk_oid_test.o tests/pkcs_1_eme_test.o \ +tests/pkcs_1_emsa_test.o tests/pkcs_1_oaep_test.o tests/pkcs_1_pss_test.o tests/pkcs_1_test.o \ +tests/prng_test.o tests/rotate_test.o tests/rsa_test.o tests/scrypt_test.o tests/siv_wycheproof_test.o \ +tests/ssh_test.o tests/store_test.o tests/test.o tests/x25519_test.o tests/x448_test.o # The following headers will be installed by "make install" HEADERS_PUB=src/headers/tomcrypt.h src/headers/tomcrypt_argchk.h src/headers/tomcrypt_cfg.h \ diff --git a/tests/hash_state_test.c b/tests/hash_state_test.c new file mode 100644 index 000000000..ac39e1209 --- /dev/null +++ b/tests/hash_state_test.c @@ -0,0 +1,126 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +#include + +#include + + +static int s_hash_state_test_sha1(void) +{ +#ifdef LTC_SHA1 + struct ugly_struct + { + unsigned char uchar; + struct sha1_state state; + }; + typedef struct ugly_struct ugly_struct; + + const unsigned char digest_baseline[] = { 0xa4, 0x9b, 0x24, 0x46, 0xa0, 0x2c, 0x64, 0x5b, 0xf4, 0x19, 0xf9, 0x95, 0xb6, 0x70, 0x91, 0x25, 0x3a, 0x04, 0xa2, 0x59 }; + + ugly_struct* md_a; + int err; + ugly_struct* md_b; + unsigned char digest_computed[sizeof(digest_baseline)]; + int cmp; + + md_a = (ugly_struct*)malloc(sizeof(*md_a)); + err = sha1_init((hash_state*)&md_a->state); if(err != CRYPT_OK){ return err; } + err = sha1_process((hash_state*)&md_a->state, (const unsigned char*)"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 112); if(err != CRYPT_OK){ return err; } + md_b = (ugly_struct*)malloc(sizeof(*md_b)); + *md_b = *md_a; + XMEMSET(md_a, 0xff, sizeof(*md_a)); + free(md_a); + err = sha1_done((hash_state*)&md_b->state, &digest_computed[0]); if(err != CRYPT_OK){ return err; } + free(md_b); + cmp = ltc_compare_testvector(&digest_computed[0], sizeof(digest_computed), &digest_baseline[0], sizeof(digest_baseline), "SHA-1", 0); + if(cmp != 0) + { + return CRYPT_FAIL_TESTVECTOR; + } +#endif + return CRYPT_OK; +} + +static int s_hash_state_test_sha224(void) +{ +#ifdef LTC_SHA224 + struct ugly_struct + { + unsigned char uchar; + struct sha256_state state; + }; + typedef struct ugly_struct ugly_struct; + + const unsigned char digest_baseline[] = { 0xc9, 0x7c, 0xa9, 0xa5, 0x59, 0x85, 0x0c, 0xe9, 0x7a, 0x04, 0xa9, 0x6d, 0xef, 0x6d, 0x99, 0xa9, 0xe0, 0xe0, 0xe2, 0xab, 0x14, 0xe6, 0xb8, 0xdf, 0x26, 0x5f, 0xc0, 0xb3 }; + + ugly_struct* md_a; + int err; + ugly_struct* md_b; + unsigned char digest_computed[sizeof(digest_baseline)]; + int cmp; + + md_a = (ugly_struct*)malloc(sizeof(*md_a)); + err = sha224_init((hash_state*)&md_a->state); if(err != CRYPT_OK){ return err; } + err = sha224_process((hash_state*)&md_a->state, (const unsigned char*)"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 112); if(err != CRYPT_OK){ return err; } + md_b = (ugly_struct*)malloc(sizeof(*md_b)); + *md_b = *md_a; + XMEMSET(md_a, 0xff, sizeof(*md_a)); + free(md_a); + err = sha224_done((hash_state*)&md_b->state, &digest_computed[0]); if(err != CRYPT_OK){ return err; } + free(md_b); + cmp = ltc_compare_testvector(&digest_computed[0], sizeof(digest_computed), &digest_baseline[0], sizeof(digest_baseline), "SHA-224", 0); + if(cmp != 0) + { + return CRYPT_FAIL_TESTVECTOR; + } +#endif + return CRYPT_OK; +} + +static int s_hash_state_test_sha256(void) +{ +#ifdef LTC_SHA256 + struct ugly_struct + { + unsigned char uchar; + struct sha256_state state; + }; + typedef struct ugly_struct ugly_struct; + + const unsigned char digest_baseline[] = { 0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80, 0x03, 0x6c, 0xe5, 0x9e, 0x7b, 0x04, 0x92, 0x37, 0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51, 0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1 }; + + ugly_struct* md_a; + int err; + ugly_struct* md_b; + unsigned char digest_computed[sizeof(digest_baseline)]; + int cmp; + + md_a = (ugly_struct*)malloc(sizeof(*md_a)); + err = sha256_init((hash_state*)&md_a->state); if(err != CRYPT_OK){ return err; } + err = sha256_process((hash_state*)&md_a->state, (const unsigned char*)"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 112); if(err != CRYPT_OK){ return err; } + md_b = (ugly_struct*)malloc(sizeof(*md_b)); + *md_b = *md_a; + XMEMSET(md_a, 0xff, sizeof(*md_a)); + free(md_a); + err = sha256_done((hash_state*)&md_b->state, &digest_computed[0]); if(err != CRYPT_OK){ return err; } + free(md_b); + cmp = ltc_compare_testvector(&digest_computed[0], sizeof(digest_computed), &digest_baseline[0], sizeof(digest_baseline), "SHA-256", 0); + if(cmp != 0) + { + return CRYPT_FAIL_TESTVECTOR; + } +#endif + return CRYPT_OK; +} + + +int hash_state_test(void) +{ + int err; + + err = s_hash_state_test_sha1(); if(err != CRYPT_OK){ return err; } + err = s_hash_state_test_sha224(); if(err != CRYPT_OK){ return err; } + err = s_hash_state_test_sha256(); if(err != CRYPT_OK){ return err; } + return CRYPT_OK; +} diff --git a/tests/misc_test.c b/tests/misc_test.c index d3d2945dc..5a0c105e0 100644 --- a/tests/misc_test.c +++ b/tests/misc_test.c @@ -40,6 +40,7 @@ int misc_test(void) #ifdef LTC_SSH ssh_test(); #endif + DO(hash_state_test()); pk_oid_test(); no_null_termination_check_test(); return 0; diff --git a/tests/sources.cmake b/tests/sources.cmake index a24ae5d6c..66c7fa2f4 100644 --- a/tests/sources.cmake +++ b/tests/sources.cmake @@ -14,6 +14,7 @@ ecc_test.c ed25519_test.c ed448_test.c file_test.c +hash_state_test.c mac_test.c misc_test.c modes_test.c diff --git a/tests/tomcrypt_test.h b/tests/tomcrypt_test.h index 86d39af77..029cf409c 100644 --- a/tests/tomcrypt_test.h +++ b/tests/tomcrypt_test.h @@ -56,6 +56,7 @@ int no_null_termination_check_test(void); int pk_oid_test(void); int deprecated_test(void); int nop_test(void); +int hash_state_test(void); #ifdef LTC_PKCS_1 struct ltc_prng_descriptor* no_prng_desc_get(void);