Skip to content

Commit 20a3085

Browse files
committed
bootutil: Provide support for embedded AES keys
Commit provides support for MCUBOOT_BUILTIN_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 9f0677c commit 20a3085

File tree

7 files changed

+46
-8
lines changed

7 files changed

+46
-8
lines changed

boot/boot_serial/src/boot_serial_encryption.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,11 @@ decrypt_image_inplace(const struct flash_area *fa_p,
235235
#endif
236236
memset(&boot_data, 0, sizeof(struct boot_loader_state));
237237
/* Load the encryption keys into cache */
238+
#if !defined(MCUBOOT_BUILTIN_ENC_KEY)
238239
rc = boot_enc_load(state, BOOT_SLOT_PRIMARY, hdr, fa_p, bs);
240+
#else
241+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_PRIMARY], BOOT_CURR_IMG(state), BOOT_SLOT_PRIMARY);
242+
#endif
239243
if (rc < 0) {
240244
FIH_RET(fih_rc);
241245
}

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+
#if !defined(MCUBOOT_BUILTIN_ENC_KEY)
182183
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY, hdr, fap, bs);
184+
#else
185+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
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_BUILTIN_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_BUILTIN_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_BUILTIN_ENC_KEY) */
567569

570+
#if !defined(MCUBOOT_BUILTIN_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_BUILTIN_ENC_KEY */
628632

629633
int
630634
boot_enc_init(struct enc_key_data *enc_state)

boot/bootutil/src/loader.c

Lines changed: 29 additions & 7 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
}
@@ -1007,8 +1008,12 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
10071008
#ifdef MCUBOOT_ENC_IMAGES
10081009
if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SLOT_SECONDARY))) {
10091010
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY,
1011+
#if !defined(MCUBOOT_BUILTIN_ENC_KEY)
10101012
boot_img_hdr(state, BOOT_SLOT_SECONDARY),
10111013
fap_secondary_slot, bs);
1014+
#else
1015+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
1016+
#endif
10121017

10131018
if (rc < 0) {
10141019
return BOOT_EBADIMAGE;
@@ -1104,8 +1109,10 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11041109
struct image_header *hdr;
11051110
const struct flash_area *fap;
11061111
#ifdef MCUBOOT_ENC_IMAGES
1112+
#ifndef MCUBOOT_BUILTIN_ENC_KEY
1113+
int i;
1114+
#endif
11071115
uint8_t slot;
1108-
uint8_t i;
11091116
#endif
11101117
uint32_t size;
11111118
uint32_t copy_size;
@@ -1131,7 +1138,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11311138
#ifdef MCUBOOT_ENC_IMAGES
11321139
if (IS_ENCRYPTED(hdr)) {
11331140
fap = BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY);
1141+
#if !defined(MCUBOOT_BUILTIN_ENC_KEY)
11341142
rc = boot_enc_load(state, BOOT_SLOT_PRIMARY, hdr, fap, bs);
1143+
#else
1144+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_PRIMARY], BOOT_CURR_IMG(state), BOOT_SLOT_PRIMARY);
1145+
#endif
11351146
assert(rc >= 0);
11361147

11371148
if (rc == 0) {
@@ -1155,7 +1166,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11551166
hdr = boot_img_hdr(state, BOOT_SLOT_SECONDARY);
11561167
if (IS_ENCRYPTED(hdr)) {
11571168
fap = BOOT_IMG_AREA(state, BOOT_SLOT_SECONDARY);
1169+
#if !defined(MCUBOOT_BUILTIN_ENC_KEY)
11581170
rc = boot_enc_load(state, BOOT_SLOT_SECONDARY, hdr, fap, bs);
1171+
#else
1172+
rc = boot_take_enc_key(bs->enckey[BOOT_SLOT_SECONDARY], BOOT_CURR_IMG(state), BOOT_SLOT_SECONDARY);
1173+
#endif
11591174
assert(rc >= 0);
11601175

11611176
if (rc == 0) {
@@ -1192,6 +1207,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
11921207

11931208
boot_enc_init(BOOT_CURR_ENC_SLOT(state, slot));
11941209

1210+
#ifndef MCUBOOT_BUILTIN_ENC_KEY
11951211
rc = boot_read_enc_key(fap, slot, bs);
11961212
assert(rc == 0);
11971213

@@ -1201,9 +1217,15 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
12011217
}
12021218
}
12031219

1204-
if (i != BOOT_ENC_KEY_SIZE) {
1205-
boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, slot), bs->enckey[slot]);
1220+
if (i == BOOT_ENC_KEY_SIZE) {
1221+
/* Invalid key */
1222+
continue;
12061223
}
1224+
#else
1225+
rc = boot_take_enc_key(bs->enckey[slot], image_index, slot);
1226+
assert(rc == 0);
1227+
#endif
1228+
boot_enc_set_key(BOOT_CURR_ENC_SLOT(state, slot), bs->enckey[slot]);
12071229
}
12081230
#endif
12091231
flash_area_close(fap);

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)