Skip to content

Commit f98e33a

Browse files
committed
bootutil: Provide support for embedded AES keys
Commit provides support for MCUBOOT_EMBEDDED_ENC_KEY config option, that allows to compile code with embedded key. When this option is enabled, compilation requires definition of boot_take_enc_key function to be provided by user; prototype for the function is provided. The boot_take_enc_key function is supposed to provide encryption AES key to be used for image encryption and decryption. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 7603613 commit f98e33a

File tree

7 files changed

+43
-6
lines changed

7 files changed

+43
-6
lines changed

boot/boot_serial/src/boot_serial_encryption.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ boot_image_validate_encrypted(struct boot_loader_state *state,
3131
int rc;
3232

3333
if (MUST_DECRYPT(fa_p, BOOT_CURR_IMG(state), hdr)) {
34+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
35+
rc = boot_en_take_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CUR_IMG(state), BOOT_SLOT_SECONDARY);
36+
#else
3437
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY, hdr, fa_p, bs);
38+
#endif
3539
if (rc < 0) {
3640
FIH_RET(fih_rc);
3741
}
@@ -232,7 +236,11 @@ decrypt_image_inplace(const struct flash_area *fa_p,
232236
}
233237
#endif
234238
/* Load the encryption keys into cache */
239+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
240+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_PRIMARY], BOOT_CURR_IMG(state), BOOT_SLOT_PRIMARY);
241+
#else
235242
rc = boot_enc_load(state, BOOT_SLOT_PRIMARY, hdr, fa_p, bs);
243+
#endif
236244
if (rc < 0) {
237245
goto total_out;
238246
}

boot/bootutil/include/bootutil/enc_key.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ void boot_enc_decrypt(struct enc_key_data *enc_state,
7575
/* Note that boot_enc_zeorize takes BOOT_CURR_ENC, not BOOT_CURR_ENC_SLOT */
7676
void boot_enc_zeroize(struct enc_key_data *enc_state);
7777

78+
/* Retrieve key for a slot */
79+
int boot_take_enc_key(uint8_t *key, int image, int slot);
80+
7881
#ifdef __cplusplus
7982
}
8083
#endif

boot/bootutil/src/bootutil_loader.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ boot_check_image(struct boot_loader_state *state, struct boot_status *bs, int sl
179179
*/
180180
#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
181181
if (MUST_DECRYPT(fap, BOOT_CURR_IMG(state), hdr)) {
182+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
183+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
184+
#else
182185
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY, hdr, fap, bs);
186+
#endif
183187
if (rc < 0) {
184188
FIH_RET(fih_rc);
185189
}

boot/bootutil/src/bootutil_misc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ boot_read_unprotected_tlv_sizes(const struct flash_area *fap, uint16_t *tlv_size
239239
}
240240
#endif
241241

242-
#ifdef MCUBOOT_ENC_IMAGES
242+
#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_EMBEDDED_ENC_KEY)
243243
int
244244
boot_read_enc_key(const struct flash_area *fap, uint8_t slot, struct boot_status *bs)
245245
{

boot/bootutil/src/encrypted.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ static int fake_rng(void *p_rng, unsigned char *output, size_t len)
370370
#endif /* (MCUBOOT_ENCRYPT_RSA && MCUBOOT_USE_MBED_TLS && !MCUBOOT_USE_PSA_CRYPTO) ||
371371
(MCUBOOT_ENCRYPT_EC256 && MCUBOOT_USE_MBED_TLS) */
372372

373+
#if !defined(MCUBOOT_EMBEDDED_ENC_KEY)
373374
/*
374375
* Decrypt an encryption key TLV.
375376
*
@@ -564,7 +565,9 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
564565
return rc;
565566
}
566567
#endif /* CONFIG_BOOT_ED25519_PSA && CONFIG_BOOT_ECDSA_PSA */
568+
#endif /* defined(MCUBOOT_EMBEDDED_ENC_KEY) */
567569

570+
#if !defined(MCUBOOT_EMBEDDED_ENC_KEY)
568571
/*
569572
* Load encryption key.
570573
*/
@@ -625,6 +628,7 @@ boot_enc_load(struct boot_loader_state *state, int slot,
625628

626629
return boot_decrypt_key(buf, bs->enckey[slot]);
627630
}
631+
#endif /* defined(MCUBOOT_EMBEDDED_ENC_KEY */
628632

629633
int
630634
boot_enc_init(struct enc_key_data *enc_state)

boot/bootutil/src/loader.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
632632
}
633633
#endif
634634
if (!boot_check_header_valid(state, slot)) {
635+
BOOT_LOG_DBG("boot_validate_slot: header validation failed %d", slot);
635636
fih_rc = FIH_FAILURE;
636637
} else {
637638
BOOT_HOOK_CALL_FIH(boot_image_check_hook, FIH_BOOT_HOOK_REGULAR,
@@ -644,16 +645,16 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
644645
check_validity:
645646
#endif
646647
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
648+
#if !defined(__BOOTSIM__)
649+
BOOT_LOG_ERR("Image in the %s slot is not valid!",
650+
(slot == BOOT_SLOT_PRIMARY) ? "primary" : "secondary");
651+
#endif
647652
if ((slot != BOOT_SLOT_PRIMARY) || ARE_SLOTS_EQUIVALENT()) {
648653
boot_scramble_slot(fap, slot);
649654
/* Image is invalid, erase it to prevent further unnecessary
650655
* attempts to validate and boot it.
651656
*/
652657
}
653-
#if !defined(__BOOTSIM__)
654-
BOOT_LOG_ERR("Image in the %s slot is not valid!",
655-
(slot == BOOT_SLOT_PRIMARY) ? "primary" : "secondary");
656-
#endif
657658
fih_rc = FIH_NO_BOOTABLE_IMAGE;
658659
goto out;
659660
}
@@ -1006,9 +1007,13 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
10061007

10071008
#ifdef MCUBOOT_ENC_IMAGES
10081009
if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SLOT_SECONDARY))) {
1010+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
1011+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
1012+
#else
10091013
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY,
10101014
boot_img_hdr(state, BOOT_SLOT_SECONDARY),
10111015
fap_secondary_slot, bs);
1016+
#endif /* MCUBOOT_EMBEDDED_ENC_KEY */
10121017

10131018
if (rc < 0) {
10141019
return BOOT_EBADIMAGE;
@@ -1130,7 +1135,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11301135
#ifdef MCUBOOT_ENC_IMAGES
11311136
if (IS_ENCRYPTED(hdr)) {
11321137
fap = BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY);
1138+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
1139+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_PRIMARY], BOOT_CURR_IMG(state), BOOT_SLOT_PRIMARY);
1140+
#else
11331141
rc = boot_enc_load(state, BOOT_SLOT_PRIMARY, hdr, fap, bs);
1142+
#endif /* MCUBOOT_EMBEDDED_ENC_KEY */
11341143
assert(rc >= 0);
11351144

11361145
if (rc == 0) {
@@ -1154,7 +1163,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11541163
hdr = boot_img_hdr(state, BOOT_SLOT_SECONDARY);
11551164
if (IS_ENCRYPTED(hdr)) {
11561165
fap = BOOT_IMG_AREA(state, BOOT_SLOT_SECONDARY);
1166+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
1167+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
1168+
#else
11571169
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY, hdr, fap, bs);
1170+
#endif /* MCUBOOT_EMBEDDED_ENC_KEY */
11581171
assert(rc >= 0);
11591172

11601173
if (rc == 0) {
@@ -1191,15 +1204,19 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11911204

11921205
boot_enc_init(BOOT_CURR_ENC_SLOT(state, slot));
11931206

1207+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
1208+
rc = boot_take_enc_key(bs->enckey[slot], image_index, slot);
1209+
#else
11941210
rc = boot_read_enc_key(fap, slot, bs);
1211+
#endif /* MCUBOOT_EMBEDDED_ENC_KEY */
11951212
if (rc) {
11961213
BOOT_LOG_DBG("boot_swap_image: Failed loading key (%d, %d)",
11971214
image_index, slot);
11981215
} else {
11991216
boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, slot), bs->enckey[slot]);
12001217
}
12011218
}
1202-
#endif
1219+
#endif /* MCUBOOT_ENC_IMAGES */
12031220
flash_area_close(fap);
12041221
}
12051222

boot/mynewt/src/single_loader.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ boot_image_validate(const struct flash_area *fa_p,
4949
* was performed. We will try to validate the image, and if still
5050
* encrypted the validation will fail, and go in panic mode
5151
*/
52+
BOOT_LOG_DBG("boot_image_validate: clearing encryption flags");
5253
hdr->ih_flags &= ~(ENCRYPTIONFLAGS);
5354
}
5455
FIH_CALL(bootutil_img_validate, fih_rc, NULL, hdr, fa_p, tmpbuf,

0 commit comments

Comments
 (0)