@@ -941,7 +941,7 @@ static int header_sha256(wc_Sha256 *sha256_ctx, struct wolfBoot_image *img)
941941 while (p < end_sha ) {
942942 blksz = WOLFBOOT_SHA_BLOCK_SIZE ;
943943 if (end_sha - p < blksz )
944- blksz = end_sha - p ;
944+ blksz = ( int )( end_sha - p ) ;
945945 wc_Sha256Update (sha256_ctx , p , blksz );
946946 p += blksz ;
947947 }
@@ -988,20 +988,32 @@ static int image_sha256(struct wolfBoot_image *img, uint8_t *hash)
988988 * @param key_slot The key slot ID to calculate the hash for.
989989 * @param hash A pointer to store the resulting SHA256 hash.
990990 */
991- static void key_sha256 (uint8_t key_slot , uint8_t * hash )
991+ static int key_sha256 (uint8_t key_slot , uint8_t * hash )
992992{
993993 uint8_t * pubkey = keystore_get_buffer (key_slot );
994994 int pubkey_sz = keystore_get_size (key_slot );
995995 wc_Sha256 sha256_ctx ;
996+ int ret = 0 ;
997+
998+ if (!pubkey || (pubkey_sz < 0 )) {
999+ return -1 ;
1000+ }
1001+ #ifndef WOLFBOOT_KEYHASH_HAS_RESULT
1002+ wolfBoot_printf ("This hash result must define WOLFBOOT_KEYHASH_HAS_RESULT" );
1003+ return -1 ;
1004+ #endif
9961005
9971006 memset (hash , 0 , SHA256_DIGEST_SIZE );
998- if (!pubkey || (pubkey_sz < 0 ))
999- return ;
10001007
1001- wc_InitSha256 (& sha256_ctx );
1002- wc_Sha256Update (& sha256_ctx , pubkey , (word32 )pubkey_sz );
1003- wc_Sha256Final (& sha256_ctx , hash );
1008+ ret = wc_InitSha256 (& sha256_ctx );
1009+ if (ret == 0 ) {
1010+ ret = wc_Sha256Update (& sha256_ctx , pubkey , (word32 )pubkey_sz );
1011+ }
1012+ if (ret == 0 ) {
1013+ ret = wc_Sha256Final (& sha256_ctx , hash );
1014+ }
10041015 wc_Sha256Free (& sha256_ctx );
1016+ return ret ;
10051017}
10061018#endif /* WOLFBOOT_NO_SIGN */
10071019#endif /* SHA2-256 */
@@ -2192,6 +2204,17 @@ uint8_t* wolfBoot_peek_image(struct wolfBoot_image *img, uint32_t offset,
21922204
21932205#if !defined(WOLFBOOT_NO_SIGN ) && !defined(WOLFBOOT_RENESAS_SCEPROTECT )
21942206
2207+ /* Normalize the call so we always get an int status: 0 == OK, <0 == error */
2208+ static inline int key_hash_ok (int id , uint8_t * digest )
2209+ {
2210+ #ifdef WOLFBOOT_KEYHASH_HAS_RET
2211+ return key_hash (id , digest );
2212+ #else
2213+ key_hash (id , digest );
2214+ return 0 ;
2215+ #endif
2216+ }
2217+
21952218/**
21962219 * @brief Get the key slot ID by SHA hash.
21972220 *
@@ -2200,18 +2223,37 @@ uint8_t* wolfBoot_peek_image(struct wolfBoot_image *img, uint32_t offset,
22002223 *
22012224 * @param hint The SHA hash of the public key to search for.
22022225 * @return The key slot ID if found, -1 if the key was not found.
2226+ * Other negative values if the key_hash function failed.
22032227 */
2228+
22042229int keyslot_id_by_sha (const uint8_t * hint )
22052230{
22062231 int id ;
2232+ int ret = -1 ;
2233+ int ct = 0 ;
2234+ if (hint == NULL ) {
2235+ return -1 ;
2236+ }
2237+ if (WOLFBOOT_SHA_DIGEST_SIZE <= 0 ) {
2238+ return -1 ;
2239+ }
22072240
22082241 for (id = 0 ; id < keystore_num_pubkeys (); id ++ ) {
2209- key_hash (id , digest );
2210- if (memcmp (digest , hint , WOLFBOOT_SHA_DIGEST_SIZE ) == 0 ) {
2242+ ct ++ ;
2243+ ret = key_hash_ok (id , digest );
2244+ if ((ret == 0 ) && memcmp (digest , hint , WOLFBOOT_SHA_DIGEST_SIZE ) == 0 ) {
2245+ wolfBoot_printf ("Found matching digest in slot %d\n" , id );
22112246 return id ;
22122247 }
22132248 }
2214- return -1 ;
2249+
2250+ if (ret == 0 ) {
2251+ /* Calls to key_hash were successful, but we did not find one. Fail: */
2252+ wolfBoot_printf ("No matching key hash found. Looked at %d slot(s)" , ct );
2253+ ret = -1 ;
2254+ }
2255+ /* Reminder: zero based slot array. */
2256+ return ret ;
22152257}
22162258#endif /* !WOLFBOOT_NO_SIGN && !WOLFBOOT_RENESAS_SCEPROTECT */
22172259
0 commit comments