Skip to content

Commit 30391a6

Browse files
committed
Encrypt scratch data before writing to flash and decrypt when reading
1 parent ca0ece6 commit 30391a6

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

boot/bootutil/src/bootutil_priv.h

+4
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,12 @@ struct boot_loader_state {
216216
uint32_t write_sz;
217217

218218
#if defined(MCUBOOT_ENC_IMAGES)
219+
#ifdef MCUBOOT_ENC_SCRATCH
220+
struct enc_key_data enc[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS + 1];
221+
#else
219222
struct enc_key_data enc[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
220223
#endif
224+
#endif
221225

222226
#if (BOOT_IMAGE_NUMBER > 1)
223227
uint8_t curr_img_idx;

boot/bootutil/src/loader.c

+36
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,10 @@ boot_copy_region(struct boot_loader_state *state,
924924
uint8_t image_index;
925925
#endif
926926

927+
#ifdef MCUBOOT_ENC_SCRATCH
928+
size_t scratch_blk_off;
929+
#endif
930+
927931
TARGET_STATIC uint8_t buf[BOOT_SWAP_BUF_SIZE] __attribute__((aligned(4)));
928932

929933
#if !defined(MCUBOOT_ENC_IMAGES)
@@ -942,6 +946,15 @@ boot_copy_region(struct boot_loader_state *state,
942946
if (rc != 0) {
943947
return BOOT_EFLASH;
944948
}
949+
#ifdef MCUBOOT_ENC_SCRATCH
950+
scratch_blk_off = 0;
951+
if (flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SCRATCH &&
952+
boot_enc_valid(BOOT_CURR_ENC(state), image_index, fap_src)) {
953+
boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
954+
(off_src + bytes_copied), chunk_sz,
955+
scratch_blk_off, buf);
956+
}
957+
#endif
945958

946959
#ifdef MCUBOOT_ENC_IMAGES
947960
image_index = BOOT_CURR_IMG(state);
@@ -1002,6 +1015,15 @@ boot_copy_region(struct boot_loader_state *state,
10021015
}
10031016
#endif
10041017

1018+
#ifdef MCUBOOT_ENC_SCRATCH
1019+
if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SCRATCH &&
1020+
boot_enc_valid(BOOT_CURR_ENC(state), image_index, fap_dst)) {
1021+
boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_dst,
1022+
(off_dst + bytes_copied), chunk_sz,
1023+
scratch_blk_off, buf);
1024+
}
1025+
#endif
1026+
10051027
rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
10061028
if (rc != 0) {
10071029
return BOOT_EFLASH;
@@ -1257,6 +1279,15 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
12571279
} else {
12581280
rc = 0;
12591281
}
1282+
1283+
#ifdef MCUBOOT_ENC_SCRATCH
1284+
rc = boot_enc_init(BOOT_CURR_ENC(state), 2);
1285+
assert(rc == 0);
1286+
1287+
rc = boot_enc_set_key(BOOT_CURR_ENC(state), 2, bs);
1288+
assert(rc == 0);
1289+
#endif
1290+
12601291
} else {
12611292
memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
12621293
}
@@ -1290,6 +1321,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
12901321

12911322
if (i != BOOT_ENC_KEY_SIZE) {
12921323
boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
1324+
#ifdef MCUBOOT_ENC_SCRATCH
1325+
if(slot == BOOT_SECONDARY_SLOT) {
1326+
boot_enc_set_key(BOOT_CURR_ENC(state), 2, bs);
1327+
}
1328+
#endif
12931329
}
12941330
}
12951331
#endif

0 commit comments

Comments
 (0)