Skip to content

Commit 2516adb

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 <dominik.ermel@nordicsemi.no>
1 parent 8a3f3bb commit 2516adb

File tree

7 files changed

+38
-2
lines changed

7 files changed

+38
-2
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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,9 +1006,13 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
10061006

10071007
#ifdef MCUBOOT_ENC_IMAGES
10081008
if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SLOT_SECONDARY))) {
1009+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
1010+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
1011+
#else
10091012
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY,
10101013
boot_img_hdr(state, BOOT_SLOT_SECONDARY),
10111014
fap_secondary_slot, bs);
1015+
#endif /* MCUBOOT_EMBEDDED_ENC_KEY */
10121016

10131017
if (rc < 0) {
10141018
return BOOT_EBADIMAGE;
@@ -1130,7 +1134,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11301134
#ifdef MCUBOOT_ENC_IMAGES
11311135
if (IS_ENCRYPTED(hdr)) {
11321136
fap = BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY);
1137+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
1138+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_PRIMARY], BOOT_CURR_IMG(state), BOOT_SLOT_PRIMARY);
1139+
#else
11331140
rc = boot_enc_load(state, BOOT_SLOT_PRIMARY, hdr, fap, bs);
1141+
#endif /* MCUBOOT_EMBEDDED_ENC_KEY */
11341142
assert(rc >= 0);
11351143

11361144
if (rc == 0) {
@@ -1154,7 +1162,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11541162
hdr = boot_img_hdr(state, BOOT_SLOT_SECONDARY);
11551163
if (IS_ENCRYPTED(hdr)) {
11561164
fap = BOOT_IMG_AREA(state, BOOT_SLOT_SECONDARY);
1165+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
1166+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
1167+
#else
11571168
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY, hdr, fap, bs);
1169+
#endif /* MCUBOOT_EMBEDDED_ENC_KEY */
11581170
assert(rc >= 0);
11591171

11601172
if (rc == 0) {
@@ -1191,15 +1203,19 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11911203

11921204
boot_enc_init(BOOT_CURR_ENC_SLOT(state, slot));
11931205

1206+
#ifdef MCUBOOT_EMBEDDED_ENC_KEY
1207+
rc = boot_take_enc_key(bs->enckey[slot], image_index, slot);
1208+
#else
11941209
rc = boot_read_enc_key(fap, slot, bs);
1210+
#endif /* MCUBOOT_EMBEDDED_ENC_KEY */
11951211
if (rc) {
11961212
BOOT_LOG_DBG("boot_swap_image: Failed loading key (%d, %d)",
11971213
image_index, slot);
11981214
} else {
11991215
boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, slot), bs->enckey[slot]);
12001216
}
12011217
}
1202-
#endif
1218+
#endif /* MCUBOOT_ENC_IMAGES */
12031219
flash_area_close(fap);
12041220
}
12051221

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)