2424
2525/* Option to enable sign tool debugging */
2626/* Must also define DEBUG_WOLFSSL in user_settings.h */
27+ #if !defined(WOLFBOOT_HASH_SHA256 ) && !defined(WOLFBOOT_HASH_SHA384 ) && \
28+ !defined(WOLFBOOT_HASH_SHA3_384 )
2729#define WOLFBOOT_HASH_SHA256
30+ #endif
2831#define EXT_FLASH
2932#define PART_UPDATE_EXT
3033#define NVM_FLASH_WRITEONCE
@@ -86,7 +89,7 @@ uint8_t *wolfBoot_get_self_header(void)
8689 return NULL ;
8790}
8891
89- #if defined(WOLFBOOT_SIGN_ECC256 )
92+ #if defined(WOLFBOOT_SIGN_ECC256 ) && defined( WOLFBOOT_HASH_SHA256 )
9093static const unsigned char pubkey_digest [SHA256_DIGEST_SIZE ] = {
9194 0x17 , 0x20 , 0xa5 , 0x9b , 0xe0 , 0x9b , 0x80 , 0x0c , 0xaa , 0xc4 , 0xf5 , 0x3f ,
9295 0xae , 0xe5 , 0x72 , 0x4f , 0xf2 , 0x1f , 0x33 , 0x53 , 0xd1 , 0xd4 , 0xcd , 0x8b ,
@@ -187,12 +190,12 @@ static void patch_pubkey_hint(uint8_t *img, uint32_t img_len)
187190{
188191 uint8_t * ptr = NULL ;
189192 uint16_t len ;
190- uint8_t hash [SHA256_DIGEST_SIZE ];
193+ uint8_t hash [WOLFBOOT_SHA_DIGEST_SIZE ];
191194
192195 (void )img_len ;
193196 len = _find_header (img + IMAGE_HEADER_OFFSET , HDR_PUBKEY , & ptr );
194197 ck_assert_int_eq (len , WOLFBOOT_SHA_DIGEST_SIZE );
195- key_sha256 (0 , hash );
198+ key_hash (0 , hash );
196199 memcpy (ptr , hash , WOLFBOOT_SHA_DIGEST_SIZE );
197200}
198201
@@ -388,15 +391,31 @@ END_TEST
388391START_TEST (test_keyslot_id_by_sha_scans_all_slots )
389392{
390393 int id ;
394+ uint8_t digest [WOLFBOOT_SHA_DIGEST_SIZE ];
391395
396+ key_hash (0 , digest );
392397 unit_keystore_reset_counters ();
393- id = keyslot_id_by_sha (pubkey_digest );
398+ id = keyslot_id_by_sha (digest );
394399
395400 ck_assert_int_eq (id , 0 );
396401 ck_assert_int_eq (unit_keystore_get_buffer_calls (), keystore_num_pubkeys ());
397402 ck_assert_int_eq (unit_keystore_get_size_calls (), keystore_num_pubkeys ());
398403}
399404END_TEST
405+
406+ START_TEST (test_key_hash_zeroes_output_on_invalid_slot )
407+ {
408+ uint8_t hash [WOLFBOOT_SHA_DIGEST_SIZE ];
409+ size_t i ;
410+
411+ memset (hash , 0xA5 , sizeof (hash ));
412+ key_hash (0xFF , hash );
413+
414+ for (i = 0 ; i < sizeof (hash ); i ++ ) {
415+ ck_assert_uint_eq (hash [i ], 0 );
416+ }
417+ }
418+ END_TEST
400419#endif
401420
402421#if defined(WOLFBOOT_SIGN_RSA2048 ) || defined(WOLFBOOT_SIGN_RSA3072 ) || \
@@ -455,7 +474,7 @@ END_TEST
455474
456475START_TEST (test_sha_ops )
457476{
458- uint8_t hash [SHA256_DIGEST_SIZE ];
477+ uint8_t hash [WOLFBOOT_SHA_DIGEST_SIZE ];
459478 static uint8_t FlashImg [32 * 1024 ];
460479 uint8_t * retp = NULL ;
461480 struct wolfBoot_image test_img ;
@@ -499,15 +518,15 @@ START_TEST(test_sha_ops)
499518 ck_assert_ptr_eq (retp , ext_hash_block );
500519 ck_assert_uint_eq (sz , WOLFBOOT_SHA_BLOCK_SIZE );
501520
502- /* Test image_sha256 */
521+ /* Test image hash */
503522
504523 /* NULL img */
505- ck_assert_int_lt (image_sha256 (NULL , hash ), 0 );
524+ ck_assert_int_lt (image_hash (NULL , hash ), 0 );
506525
507526 /* Too short, internal partition field */
508527 test_img .part = PART_BOOT ;
509528 test_img .fw_size = 0x1000 ;
510- ck_assert_int_lt (image_sha256 (& test_img , hash ), 0 );
529+ ck_assert_int_lt (image_hash (& test_img , hash ), 0 );
511530
512531 /* Ext partition with a valid SHA */
513532 find_header_mocked = 0 ;
@@ -518,14 +537,14 @@ START_TEST(test_sha_ops)
518537 test_img .part = PART_UPDATE ;
519538 test_img .fw_base = 0 ;
520539 test_img .fw_size = test_img_len ;
521- ck_assert_int_eq (image_sha256 (& test_img , hash ), 0 );
540+ ck_assert_int_eq (image_hash (& test_img , hash ), 0 );
522541
523- /* key_sha256 */
524- key_sha256 (0 , hash );
525- #if defined(WOLFBOOT_SIGN_ECC256 )
542+ /* key hash */
543+ key_hash (0 , hash );
544+ #if defined(WOLFBOOT_SIGN_ECC256 ) && defined( WOLFBOOT_HASH_SHA256 )
526545 ck_assert_mem_eq (hash , pubkey_digest , SHA256_DIGEST_SIZE );
527546#else
528- /* For non- ECC256 configurations we do not have a fixed expected digest. */
547+ /* Only the SHA-256 ECC256 fixture has a fixed expected digest here . */
529548 (void )hash ;
530549#endif
531550}
@@ -775,11 +794,20 @@ Suite *wolfboot_suite(void)
775794 /* Suite initialization */
776795 Suite * s = suite_create ("wolfBoot" );
777796
797+ #ifdef UNIT_IMAGE_KEYHASH_ONLY
798+ TCase * tcase_key_hash = tcase_create ("key_hash" );
799+ tcase_set_timeout (tcase_key_hash , 20 );
800+ tcase_add_test (tcase_key_hash , test_key_hash_zeroes_output_on_invalid_slot );
801+ suite_add_tcase (s , tcase_key_hash );
802+ return s ;
803+ #endif
804+
778805#if defined(WOLFBOOT_SIGN_ECC256 )
779806 TCase * tcase_verify_signature = tcase_create ("verify_signature" );
780807 tcase_set_timeout (tcase_verify_signature , 20 );
781808 tcase_add_test (tcase_verify_signature , test_verify_signature );
782809 tcase_add_test (tcase_verify_signature , test_keyslot_id_by_sha_scans_all_slots );
810+ tcase_add_test (tcase_verify_signature , test_key_hash_zeroes_output_on_invalid_slot );
783811 suite_add_tcase (s , tcase_verify_signature );
784812#endif
785813
0 commit comments