Skip to content

Commit 07222c1

Browse files
taltenbachnordicjm
authored andcommitted
boot_serial: Avoid re-initializing state in boot_image_validate_encrypted
A valid bootloader state was needed to validate encrypted images, so the boot_image_validate_encrypted (only called from bs_list and bs_set) was allocating and initializing a minimal state with the required content. Now bs_list and bs_set have a valid bootloader state, the latter can be given to boot_image_validate_encrypted, avoiding two bootloader state allocations. Signed-off-by: Thomas Altenbach <[email protected]>
1 parent a18f635 commit 07222c1

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

boot/boot_serial/include/boot_serial/boot_serial_encryption.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
/**
1212
* Validate hash of a primary boot image doing on the fly decryption as well
1313
*
14+
* @param[in] state bootloader state
1415
* @param[in] fa_p flash area pointer
1516
* @param[in] hdr boot image header pointer
1617
* @param[in] buf buffer which is used for validating data
@@ -20,7 +21,8 @@
2021
* @return FIH_SUCCESS on success, error code otherwise
2122
*/
2223
fih_ret
23-
boot_image_validate_encrypted(const struct flash_area *fa_p,
24+
boot_image_validate_encrypted(struct boot_loader_state *state,
25+
const struct flash_area *fa_p,
2426
struct image_header *hdr, uint8_t *buf,
2527
uint16_t buf_size
2628
);

boot/boot_serial/src/boot_serial.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ bs_list(struct boot_loader_state *state, char *buf, int len)
352352
#if defined(MCUBOOT_ENC_IMAGES)
353353
#if !defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
354354
if (IS_ENCRYPTED(&hdr) && MUST_DECRYPT(fap, image_index, &hdr)) {
355-
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
355+
FIH_CALL(boot_image_validate_encrypted, fih_rc, state, fap,
356356
&hdr, tmpbuf, sizeof(tmpbuf));
357357
} else {
358358
#endif
@@ -573,7 +573,7 @@ bs_set(struct boot_loader_state *state, char *buf, int len)
573573
{
574574
#ifdef MCUBOOT_ENC_IMAGES
575575
if (IS_ENCRYPTED(&hdr)) {
576-
FIH_CALL(boot_image_validate_encrypted, fih_rc, fap,
576+
FIH_CALL(boot_image_validate_encrypted, fih_rc, state, fap,
577577
&hdr, tmpbuf, sizeof(tmpbuf));
578578
} else {
579579
#endif

boot/boot_serial/src/boot_serial_encryption.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,18 @@
1919
BOOT_LOG_MODULE_DECLARE(serial_encryption);
2020

2121
fih_ret
22-
boot_image_validate_encrypted(const struct flash_area *fa_p,
22+
boot_image_validate_encrypted(struct boot_loader_state *state,
23+
const struct flash_area *fa_p,
2324
struct image_header *hdr, uint8_t *buf,
2425
uint16_t buf_size)
2526
{
2627
FIH_DECLARE(fih_rc, FIH_FAILURE);
2728

28-
struct boot_loader_state boot_data;
29-
struct boot_loader_state *state = &boot_data;
3029
struct boot_status _bs;
3130
struct boot_status *bs = &_bs;
3231
int rc;
3332

34-
memset(&boot_data, 0, sizeof(struct boot_loader_state));
35-
if(IS_ENCRYPTED(hdr)) {
33+
if (MUST_DECRYPT(fa_p, BOOT_CURR_IMG(state), hdr)) {
3634
rc = boot_enc_load(state, 1, hdr, fa_p, bs);
3735
if (rc < 0) {
3836
FIH_RET(fih_rc);
@@ -46,6 +44,8 @@ boot_image_validate_encrypted(const struct flash_area *fa_p,
4644
FIH_CALL(bootutil_img_validate, fih_rc, state,
4745
hdr, fa_p, buf, buf_size, NULL, 0, NULL);
4846

47+
boot_enc_zeroize(BOOT_CURR_ENC(state));
48+
4949
FIH_RET(fih_rc);
5050
}
5151

@@ -228,7 +228,7 @@ decrypt_image_inplace(const struct flash_area *fa_p,
228228
#if 0 //Skip this step?, the image will just not boot if it's not decrypted properly
229229
static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
230230
/* First check if the encrypted image is a good image before decrypting */
231-
FIH_CALL(boot_image_validate_encrypted,fih_rc,fa_p,&_hdr,tmpbuf,BOOT_TMPBUF_SZ);
231+
FIH_CALL(boot_image_validate_encrypted, fih_rc, state, fa_p, &_hdr, tmpbuf, BOOT_TMPBUF_SZ);
232232
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
233233
FIH_RET(fih_rc);
234234
}

0 commit comments

Comments
 (0)