Skip to content

Commit ca3110c

Browse files
committed
Improved image, keygen, sign, test lib messages. Polish
1 parent a8afa74 commit ca3110c

File tree

4 files changed

+286
-112
lines changed

4 files changed

+286
-112
lines changed

hal/library.c

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,17 @@ int wolfBoot_start(void)
124124
os_image.hdr = (uint8_t*)gImage;
125125

126126
if ((ret = wolfBoot_open_image_address(&os_image, (uint8_t*)gImage)) < 0) {
127+
wolfBoot_printf("Failed to open image address.\n");
127128
goto exit;
128129
}
129130

130131
if ((ret = wolfBoot_verify_integrity(&os_image)) < 0) {
132+
wolfBoot_printf("Failed to verify integrity.\n");
131133
goto exit;
132134
}
133135

134136
if ((ret = wolfBoot_verify_authenticity(&os_image)) < 0) {
137+
wolfBoot_printf("Failed to verify authenticity.\n");
135138
goto exit;
136139
}
137140

@@ -142,11 +145,13 @@ int wolfBoot_start(void)
142145
exit:
143146
if (ret < 0) {
144147
wolfBoot_printf("Failure %d: Hdr %d, Hash %d, Sig %d\n", ret,
145-
(int)os_image.hdr_ok, (int)os_image.sha_ok,
146-
(int)os_image.signature_ok);
148+
os_image.hdr_ok, os_image.sha_ok, os_image.signature_ok);
149+
return -1;
150+
}
151+
else {
152+
return 0;
147153
}
148154

149-
return 0;
150155
}
151156

152157

@@ -155,39 +160,62 @@ int main(int argc, const char* argv[])
155160
int ret = 0;
156161

157162
#ifdef NO_FILESYSTEM
163+
wolfBoot_printf("NO_FILESYSTEM is defined, looking at test_img");
158164
gImage = (uintptr_t)test_img;
159165
#else
160-
if (argc > 1) {
161-
size_t sz = 0, bread;
166+
if (argc == 2) {
167+
size_t sz = 0, bread = 0;
162168
FILE* img = fopen(argv[1], "rb");
163169
if (img == NULL) {
164-
wolfBoot_printf("failed to open %s!\n", argv[1]);
170+
wolfBoot_printf("Failed to open file: %s!\n\n", argv[1]);
171+
wolfBoot_printf("Usage: %s image_file.bin\n", argv[0]);
165172
return -3;
166173
}
167-
fseek(img, 0, SEEK_END);
168-
sz = ftell(img);
169-
fseek(img, 0, SEEK_SET);
174+
else {
175+
wolfBoot_printf("Looking at image file: %s\n", argv[1]);
176+
fseek(img, 0, SEEK_END);
177+
sz = ftell(img);
178+
fseek(img, 0, SEEK_SET);
179+
180+
gImage = (uintptr_t)malloc(sz);
181+
}
170182

171-
gImage = (uintptr_t)malloc(sz);
172183
if (((void*)gImage) == NULL) {
173-
wolfBoot_printf("failed to malloc %zu bytes for image\n", sz);
184+
wolfBoot_printf("Failed to malloc %zu bytes for image.\n", sz);
174185
ret = -1;
175186
}
187+
else {
188+
/* check the image */
189+
bread = fread((void*)gImage, 1, sz, img);
190+
}
176191

177-
bread = fread((void*)gImage, 1, sz, img);
178-
if (bread != sz) {
192+
if (bread == sz) {
193+
wolfBoot_printf("Confirmed expected size: %zu bytes.\n", bread);
194+
}
195+
else {
179196
ret = -2;
180-
wolfBoot_printf("read %zu of %zu bytes from %s\n", bread, sz, argv[1]);
197+
wolfBoot_printf("Read %zu of %zu bytes from %s\n", bread, sz, argv[1]);
181198
}
182199
fclose(img);
183-
} else {
200+
}
201+
else {
184202
wolfBoot_printf("usage: %s image_file.bin\n", argv[0]);
185203
ret = 255;
186204
}
187205
#endif
188206
if (ret == 0) {
207+
wolfBoot_printf("Checking image... ");
189208
ret = wolfBoot_start();
190209
}
210+
if (ret == 0) {
211+
wolfBoot_printf("Success!\n");
212+
}
213+
else {
214+
if (ret != 255) {
215+
/* Only show error if we actually processed file, not missing params */
216+
wolfBoot_printf("Failed to verify with wolfBoot_start\n");
217+
}
218+
}
191219

192220
#ifndef NO_FILESYSTEM
193221
if ((void*)gImage != NULL)

src/image.c

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
22042229
int 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

tools/keytools/keygen.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -503,10 +503,10 @@ void keystore_add(uint32_t ktype, uint8_t *key, uint32_t sz, const char *keyfile
503503
}
504504
fprintf(fpub, Pubkey_footer);
505505
fprintf(fpub, Slot_footer);
506-
printf("Associated key file: %s\n", keyfile);
506+
printf("Associated key file: %s\n", keyfile);
507507
printf("Partition ids mask: %08x\n", id_mask);
508-
printf("Key type : %s\n", KName[ktype]);
509-
printf("Public key slot: %u\n", id_slot);
508+
printf("Key type: %s\n", KName[ktype]);
509+
printf("Public key slot: %u\n", id_slot);
510510
if (noLocalKeys) {
511511
printf("WARNING: --nolocalkeys flag used, keystore.c public key is zeroed\n");
512512
}
@@ -1089,7 +1089,7 @@ static void keygen_ml_dsa(const char *priv_fname, uint32_t id_mask)
10891089
if (exportPubKey) {
10901090
if (saveAsDer) {
10911091
uint8_t* pubDer;
1092-
size_t pubDerSz;
1092+
word32 pubDerSz;
10931093
int pubOutLen;
10941094
const int WITH_ALG_SPKI = 1;
10951095

@@ -1349,10 +1349,11 @@ int main(int argc, char** argv)
13491349
uint32_t n_pubkeys = 0;
13501350
uint32_t part_id_mask = 0xFFFFFFFF; /* Default: key verify all */
13511351

1352+
printf("wolfBoot KeyGen\n");
13521353
#ifdef DEBUG_SIGNTOOL
13531354
wolfSSL_Debugging_ON();
13541355
#endif
1355-
printf("Keystore size: %lu\n", (unsigned long)sizeof(struct keystore_slot));
1356+
printf("Keystore size: %lu\n", (unsigned long)sizeof(struct keystore_slot));
13561357

13571358
/* Check arguments and print usage */
13581359
if (argc < 2)
@@ -1436,6 +1437,7 @@ int main(int argc, char** argv)
14361437
i++;
14371438
sprintf(pubkeyfile,"%s%s", argv[i], "/keystore.c");
14381439
sprintf(pubkeyimg, "%s%s", argv[i], "/keystore.der");
1440+
printf("Saving keystore file: %s\n", pubkeyfile);
14391441
i++;
14401442
continue;
14411443
}
@@ -1453,20 +1455,23 @@ int main(int argc, char** argv)
14531455
usage(argv[0]);
14541456
}
14551457
}
1456-
printf("Keytype: %s\n", KName[keytype]);
1457-
if (keytype == 0)
1458+
printf("Selected Keytype: %s\n", KName[keytype]);
1459+
if (keytype == 0) {
1460+
fprintf(stderr, "No keytype, exiting...");
14581461
exit(0);
1462+
}
14591463
fpub = fopen(pubkeyfile, "rb");
14601464
if (!force && (fpub != NULL)) {
14611465
char reply[40];
14621466
int replySz;
1463-
printf("** Warning: keystore already exists! Are you sure you want to generate a new key and overwrite the existing key? [Type 'Yes']: ");
1467+
printf("** Warning: keystore file already exists! %s\n", pubkeyfile);
1468+
printf("Are you sure you want to generate a new key and overwrite the existing key ? [Type 'Yes'] : ");
14641469
fflush(stdout);
14651470
replySz = scanf("%s", reply);
14661471
printf("Reply is [%s]\n", reply);
14671472
fclose(fpub);
14681473
if (replySz < 0 || strcmp(reply, "Yes") != 0) {
1469-
printf("Operation aborted by user.");
1474+
printf("Operation aborted by user.\n");
14701475
exit(5);
14711476
} else {
14721477
unlink(pubkeyfile);

0 commit comments

Comments
 (0)