Skip to content

Commit 397b896

Browse files
committed
key_sha384: zero hash buffer to cover for early error
F/370
1 parent ced5517 commit 397b896

3 files changed

Lines changed: 56 additions & 14 deletions

File tree

src/image.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,7 @@ static void key_sha384(uint8_t key_slot, uint8_t *hash)
11431143
int pubkey_sz = keystore_get_size(key_slot);
11441144
wc_Sha384 sha384_ctx;
11451145

1146+
memset(hash, 0, SHA384_DIGEST_SIZE);
11461147
if (!pubkey || (pubkey_sz < 0))
11471148
return;
11481149

@@ -1237,6 +1238,7 @@ static void key_sha3_384(uint8_t key_slot, uint8_t *hash)
12371238
int pubkey_sz = keystore_get_size(key_slot);
12381239
wc_Sha3 sha3_ctx;
12391240

1241+
memset(hash, 0, WC_SHA3_384_DIGEST_SIZE);
12401242
if (!pubkey || (pubkey_sz < 0))
12411243
return;
12421244
wc_InitSha3_384(&sha3_ctx, NULL, INVALID_DEVID);

tools/unit-tests/Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ TESTS:=unit-parser unit-extflash unit-string unit-spi-flash unit-aes128 \
4848
unit-enc-nvm-flagshome unit-delta unit-update-flash \
4949
unit-update-flash-enc unit-update-ram unit-pkcs11_store unit-psa_store unit-disk \
5050
unit-multiboot unit-boot-x86-fsp unit-qspi-flash unit-tpm-rsa-exp \
51-
unit-image-nopart \
51+
unit-image-nopart unit-image-sha384 unit-image-sha3-384 \
5252
unit-tpm-blob
5353

5454
all: $(TESTS)
@@ -166,6 +166,18 @@ unit-image-nopart: ../../include/target.h unit-image.c unit-common.c $(WOLFCRYPT
166166
$(CFLAGS) $(WOLFCRYPT_CFLAGS) -DWOLFBOOT_NO_PARTITIONS -DMOCK_PARTITIONS \
167167
-DWOLFBOOT_RAMBOOT_MAX_SIZE=0x1000 $(LDFLAGS)
168168

169+
unit-image-sha384: ../../include/target.h unit-image.c unit-common.c
170+
gcc -o $@ unit-image.c unit-common.c $(WOLFCRYPT_SRC) \
171+
$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/sha512.c \
172+
$(CFLAGS) $(WOLFCRYPT_CFLAGS) -DUNIT_IMAGE_KEYHASH_ONLY \
173+
-DWOLFBOOT_HASH_SHA384 $(LDFLAGS)
174+
175+
unit-image-sha3-384: ../../include/target.h unit-image.c unit-common.c
176+
gcc -o $@ unit-image.c unit-common.c $(WOLFCRYPT_SRC) \
177+
$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/sha3.c \
178+
$(CFLAGS) $(WOLFCRYPT_CFLAGS) -DUNIT_IMAGE_KEYHASH_ONLY \
179+
-DWOLFBOOT_HASH_SHA3_384 $(LDFLAGS)
180+
169181
unit-image-rsa: CFLAGS += -DWOLFBOOT_SIGN_RSA2048
170182
unit-image-rsa: ../../include/target.h unit-image.c unit-common.c
171183
gcc -o $@ unit-image.c unit-common.c $(WOLFCRYPT_SRC) \

tools/unit-tests/unit-image.c

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
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)
9093
static 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
388391
START_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
}
399404
END_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

456475
START_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

Comments
 (0)