Skip to content

Commit 3a69b0e

Browse files
committed
Don't fail if the image contains no base sha
+ Added --no-base-sha option to sign
1 parent adb0204 commit 3a69b0e

File tree

5 files changed

+65
-30
lines changed

5 files changed

+65
-30
lines changed

.github/workflows/test-powerfail-simulator.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ jobs:
235235
run: |
236236
tools/scripts/sim-update-powerfail-resume.sh
237237
238+
- name: Rebuild without SHA of base image to test compatibility
239+
run: |
240+
make clean && make test-sim-internal-flash-with-delta-update-no-base-sha
241+
242+
- name: Run sunny day update test (DELTA with no-base-sha)
243+
run: |
244+
tools/scripts/sim-sunnyday-update.sh
245+
238246
- name: Rebuild with wrong delta base version
239247
run: |
240248
make clean && make test-sim-internal-flash-with-wrong-delta-update

docs/Signing.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ result is stored in a file ending in `_signed_diff.bin`.
180180

181181
The compression scheme used is Bentley–McIlroy.
182182

183+
Options:
184+
* `--no-base-sha` : Avoid adding the sha of the base image to the manifest header.
185+
By default, the sign tool appends the sha of the base image to the manifest header,
186+
so wolfBoot will refuse to start a delta update if the sha does not match the
187+
one of the existing image. However, this takes up 32 to 48 bytes extra in the
188+
manifest header, so this option is available to provide compatibility on
189+
existing installations without this feature, where the header size does not
190+
allow to accommodate the field
191+
183192

184193
#### Policy signing (for sealing/unsealing with a TPM)
185194

src/update_flash.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,15 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
340340
delta_base_v = wolfBoot_get_diffbase_version(PART_UPDATE);
341341

342342
if (delta_base_hash_sz != WOLFBOOT_SHA_DIGEST_SIZE) {
343-
wolfBoot_printf("Delta update: Base hash size mismatch"
344-
" (size: %x expected %x)\n", delta_base_hash_sz,
345-
WOLFBOOT_SHA_DIGEST_SIZE);
346-
return -1;
343+
if (delta_base_hash_sz == 0) {
344+
wolfBoot_printf("Warning: delta update: Base hash not found in image\n");
345+
delta_base_hash = NULL;
346+
} else {
347+
wolfBoot_printf("Error: delta update: Base hash size mismatch"
348+
" (size: %x expected %x)\n", delta_base_hash_sz,
349+
WOLFBOOT_SHA_DIGEST_SIZE);
350+
return -1;
351+
}
347352
}
348353

349354
#if defined(WOLFBOOT_HASH_SHA256)
@@ -375,8 +380,8 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
375380
wolfBoot_printf("Delta Base 0x%x != Cur 0x%x\n",
376381
cur_v, delta_base_v);
377382
ret = -1;
378-
379-
} else if (!resume && memcmp(base_hash, delta_base_hash, base_hash_sz) != 0) {
383+
} else if (!resume && delta_base_hash &&
384+
memcmp(base_hash, delta_base_hash, base_hash_sz) != 0) {
380385
/* Wrong base image digest, cannot apply delta patch */
381386
wolfBoot_printf("Delta Base hash mismatch\n");
382387
ret = -1;

tools/keytools/sign.c

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ struct cmd_options {
291291
const char *policy_file;
292292
const char *encrypt_key_file;
293293
const char *delta_base_file;
294+
int no_base_sha;
294295
char output_image_file[PATH_MAX];
295296
char output_diff_file[PATH_MAX];
296297
char output_encrypted_image_file[PATH_MAX];
@@ -1201,33 +1202,35 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
12011202
header_append_tag(header, &header_idx, HDR_IMG_DELTA_INVERSE_SIZE, 4,
12021203
&patch_inv_len);
12031204

1204-
/* Append pad bytes, so base hash is 8-byte aligned */
1205-
ALIGN_8(header_idx);
1206-
if (!base_hash) {
1207-
fprintf(stderr, "Base hash for delta image not found.\n");
1208-
exit(1);
1209-
}
1210-
if (CMD.hash_algo == HASH_SHA256) {
1211-
if (base_hash_sz != HDR_SHA256_LEN) {
1212-
fprintf(stderr, "Invalid base hash size for SHA256.\n");
1213-
exit(1);
1214-
}
1215-
header_append_tag(header, &header_idx, HDR_IMG_DELTA_BASE_HASH,
1216-
HDR_SHA256_LEN, base_hash);
1217-
} else if (CMD.hash_algo == HASH_SHA384) {
1218-
if (base_hash_sz != HDR_SHA384_LEN) {
1219-
fprintf(stderr, "Invalid base hash size for SHA384.\n");
1205+
if (!CMD.no_base_sha) {
1206+
/* Append pad bytes, so base hash is 8-byte aligned */
1207+
ALIGN_8(header_idx);
1208+
if (!base_hash) {
1209+
fprintf(stderr, "Base hash for delta image not found.\n");
12201210
exit(1);
12211211
}
1222-
header_append_tag(header, &header_idx, HDR_IMG_DELTA_BASE_HASH,
1223-
HDR_SHA384_LEN, base_hash);
1224-
} else if (CMD.hash_algo == HASH_SHA3) {
1225-
if (base_hash_sz != HDR_SHA3_384_LEN) {
1226-
fprintf(stderr, "Invalid base hash size for SHA3-384.\n");
1227-
exit(1);
1212+
if (CMD.hash_algo == HASH_SHA256) {
1213+
if (base_hash_sz != HDR_SHA256_LEN) {
1214+
fprintf(stderr, "Invalid base hash size for SHA256.\n");
1215+
exit(1);
1216+
}
1217+
header_append_tag(header, &header_idx, HDR_IMG_DELTA_BASE_HASH,
1218+
HDR_SHA256_LEN, base_hash);
1219+
} else if (CMD.hash_algo == HASH_SHA384) {
1220+
if (base_hash_sz != HDR_SHA384_LEN) {
1221+
fprintf(stderr, "Invalid base hash size for SHA384.\n");
1222+
exit(1);
1223+
}
1224+
header_append_tag(header, &header_idx, HDR_IMG_DELTA_BASE_HASH,
1225+
HDR_SHA384_LEN, base_hash);
1226+
} else if (CMD.hash_algo == HASH_SHA3) {
1227+
if (base_hash_sz != HDR_SHA3_384_LEN) {
1228+
fprintf(stderr, "Invalid base hash size for SHA3-384.\n");
1229+
exit(1);
1230+
}
1231+
header_append_tag(header, &header_idx, HDR_IMG_DELTA_BASE_HASH,
1232+
HDR_SHA3_384_LEN, base_hash);
12281233
}
1229-
header_append_tag(header, &header_idx, HDR_IMG_DELTA_BASE_HASH,
1230-
HDR_SHA3_384_LEN, base_hash);
12311234
}
12321235
}
12331236

@@ -2490,6 +2493,8 @@ int main(int argc, char** argv)
24902493
else if (strcmp(argv[i], "--delta") == 0) {
24912494
CMD.delta = 1;
24922495
CMD.delta_base_file = argv[++i];
2496+
} else if (strcmp(argv[i], "--no-base-sha") == 0) {
2497+
CMD.no_base_sha = 1;
24932498
}
24942499
else if (strcmp(argv[i], "--no-ts") == 0) {
24952500
CMD.no_ts = 1;

tools/test.mk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ test-sim-internal-flash-with-delta-update:
246246
$$(($(WOLFBOOT_PARTITION_UPDATE_ADDRESS)-$(ARCH_FLASH_OFFSET))) test-app/image_v$(TEST_UPDATE_VERSION)_signed_diff.bin \
247247
$$(($(WOLFBOOT_PARTITION_SWAP_ADDRESS)-$(ARCH_FLASH_OFFSET))) erased_sec.dd
248248

249+
test-sim-internal-flash-with-delta-update-no-base-sha:
250+
make test-sim-internal-flash-with-update DELTA_UPDATE_OPTIONS="--no-base-sha --delta test-app/image_v1_signed.bin"
251+
$(Q)$(BINASSEMBLE) internal_flash.dd \
252+
0 wolfboot.bin \
253+
$$(($(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLASH_OFFSET))) test-app/image_v1_signed.bin \
254+
$$(($(WOLFBOOT_PARTITION_UPDATE_ADDRESS)-$(ARCH_FLASH_OFFSET))) test-app/image_v$(TEST_UPDATE_VERSION)_signed_diff.bin \
255+
$$(($(WOLFBOOT_PARTITION_SWAP_ADDRESS)-$(ARCH_FLASH_OFFSET))) erased_sec.dd
256+
249257
test-sim-internal-flash-with-wrong-delta-update:
250258
make test-sim-internal-flash-with-update DELTA_UPDATE_OPTIONS="--delta test-app/image_v1_signed.bin"
251259
make test-sim-internal-flash-with-update DELTA_UPDATE_OPTIONS="--delta test-app/image_v2_signed.bin" TEST_UPDATE_VERSION=3

0 commit comments

Comments
 (0)