Skip to content

Commit

Permalink
Don't fail if the image contains no base sha
Browse files Browse the repository at this point in the history
+ Added --no-base-sha option to sign
  • Loading branch information
danielinux committed Nov 22, 2024
1 parent adb0204 commit 3a69b0e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 30 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/test-powerfail-simulator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ jobs:
run: |
tools/scripts/sim-update-powerfail-resume.sh
- name: Rebuild without SHA of base image to test compatibility
run: |
make clean && make test-sim-internal-flash-with-delta-update-no-base-sha
- name: Run sunny day update test (DELTA with no-base-sha)
run: |
tools/scripts/sim-sunnyday-update.sh
- name: Rebuild with wrong delta base version
run: |
make clean && make test-sim-internal-flash-with-wrong-delta-update
Expand Down
9 changes: 9 additions & 0 deletions docs/Signing.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ result is stored in a file ending in `_signed_diff.bin`.

The compression scheme used is Bentley–McIlroy.

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


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

Expand Down
17 changes: 11 additions & 6 deletions src/update_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,15 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
delta_base_v = wolfBoot_get_diffbase_version(PART_UPDATE);

if (delta_base_hash_sz != WOLFBOOT_SHA_DIGEST_SIZE) {
wolfBoot_printf("Delta update: Base hash size mismatch"
" (size: %x expected %x)\n", delta_base_hash_sz,
WOLFBOOT_SHA_DIGEST_SIZE);
return -1;
if (delta_base_hash_sz == 0) {
wolfBoot_printf("Warning: delta update: Base hash not found in image\n");
delta_base_hash = NULL;
} else {
wolfBoot_printf("Error: delta update: Base hash size mismatch"
" (size: %x expected %x)\n", delta_base_hash_sz,
WOLFBOOT_SHA_DIGEST_SIZE);
return -1;
}
}

#if defined(WOLFBOOT_HASH_SHA256)
Expand Down Expand Up @@ -375,8 +380,8 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
wolfBoot_printf("Delta Base 0x%x != Cur 0x%x\n",
cur_v, delta_base_v);
ret = -1;

} else if (!resume && memcmp(base_hash, delta_base_hash, base_hash_sz) != 0) {
} else if (!resume && delta_base_hash &&
memcmp(base_hash, delta_base_hash, base_hash_sz) != 0) {
/* Wrong base image digest, cannot apply delta patch */
wolfBoot_printf("Delta Base hash mismatch\n");
ret = -1;
Expand Down
53 changes: 29 additions & 24 deletions tools/keytools/sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ struct cmd_options {
const char *policy_file;
const char *encrypt_key_file;
const char *delta_base_file;
int no_base_sha;
char output_image_file[PATH_MAX];
char output_diff_file[PATH_MAX];
char output_encrypted_image_file[PATH_MAX];
Expand Down Expand Up @@ -1201,33 +1202,35 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
header_append_tag(header, &header_idx, HDR_IMG_DELTA_INVERSE_SIZE, 4,
&patch_inv_len);

/* Append pad bytes, so base hash is 8-byte aligned */
ALIGN_8(header_idx);
if (!base_hash) {
fprintf(stderr, "Base hash for delta image not found.\n");
exit(1);
}
if (CMD.hash_algo == HASH_SHA256) {
if (base_hash_sz != HDR_SHA256_LEN) {
fprintf(stderr, "Invalid base hash size for SHA256.\n");
exit(1);
}
header_append_tag(header, &header_idx, HDR_IMG_DELTA_BASE_HASH,
HDR_SHA256_LEN, base_hash);
} else if (CMD.hash_algo == HASH_SHA384) {
if (base_hash_sz != HDR_SHA384_LEN) {
fprintf(stderr, "Invalid base hash size for SHA384.\n");
if (!CMD.no_base_sha) {
/* Append pad bytes, so base hash is 8-byte aligned */
ALIGN_8(header_idx);
if (!base_hash) {
fprintf(stderr, "Base hash for delta image not found.\n");
exit(1);
}
header_append_tag(header, &header_idx, HDR_IMG_DELTA_BASE_HASH,
HDR_SHA384_LEN, base_hash);
} else if (CMD.hash_algo == HASH_SHA3) {
if (base_hash_sz != HDR_SHA3_384_LEN) {
fprintf(stderr, "Invalid base hash size for SHA3-384.\n");
exit(1);
if (CMD.hash_algo == HASH_SHA256) {
if (base_hash_sz != HDR_SHA256_LEN) {
fprintf(stderr, "Invalid base hash size for SHA256.\n");
exit(1);
}
header_append_tag(header, &header_idx, HDR_IMG_DELTA_BASE_HASH,
HDR_SHA256_LEN, base_hash);
} else if (CMD.hash_algo == HASH_SHA384) {
if (base_hash_sz != HDR_SHA384_LEN) {
fprintf(stderr, "Invalid base hash size for SHA384.\n");
exit(1);
}
header_append_tag(header, &header_idx, HDR_IMG_DELTA_BASE_HASH,
HDR_SHA384_LEN, base_hash);
} else if (CMD.hash_algo == HASH_SHA3) {
if (base_hash_sz != HDR_SHA3_384_LEN) {
fprintf(stderr, "Invalid base hash size for SHA3-384.\n");
exit(1);
}
header_append_tag(header, &header_idx, HDR_IMG_DELTA_BASE_HASH,
HDR_SHA3_384_LEN, base_hash);
}
header_append_tag(header, &header_idx, HDR_IMG_DELTA_BASE_HASH,
HDR_SHA3_384_LEN, base_hash);
}
}

Expand Down Expand Up @@ -2490,6 +2493,8 @@ int main(int argc, char** argv)
else if (strcmp(argv[i], "--delta") == 0) {
CMD.delta = 1;
CMD.delta_base_file = argv[++i];
} else if (strcmp(argv[i], "--no-base-sha") == 0) {
CMD.no_base_sha = 1;
}
else if (strcmp(argv[i], "--no-ts") == 0) {
CMD.no_ts = 1;
Expand Down
8 changes: 8 additions & 0 deletions tools/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ test-sim-internal-flash-with-delta-update:
$$(($(WOLFBOOT_PARTITION_UPDATE_ADDRESS)-$(ARCH_FLASH_OFFSET))) test-app/image_v$(TEST_UPDATE_VERSION)_signed_diff.bin \
$$(($(WOLFBOOT_PARTITION_SWAP_ADDRESS)-$(ARCH_FLASH_OFFSET))) erased_sec.dd

test-sim-internal-flash-with-delta-update-no-base-sha:
make test-sim-internal-flash-with-update DELTA_UPDATE_OPTIONS="--no-base-sha --delta test-app/image_v1_signed.bin"
$(Q)$(BINASSEMBLE) internal_flash.dd \
0 wolfboot.bin \
$$(($(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLASH_OFFSET))) test-app/image_v1_signed.bin \
$$(($(WOLFBOOT_PARTITION_UPDATE_ADDRESS)-$(ARCH_FLASH_OFFSET))) test-app/image_v$(TEST_UPDATE_VERSION)_signed_diff.bin \
$$(($(WOLFBOOT_PARTITION_SWAP_ADDRESS)-$(ARCH_FLASH_OFFSET))) erased_sec.dd

test-sim-internal-flash-with-wrong-delta-update:
make test-sim-internal-flash-with-update DELTA_UPDATE_OPTIONS="--delta test-app/image_v1_signed.bin"
make test-sim-internal-flash-with-update DELTA_UPDATE_OPTIONS="--delta test-app/image_v2_signed.bin" TEST_UPDATE_VERSION=3
Expand Down

0 comments on commit 3a69b0e

Please sign in to comment.