@@ -924,6 +924,10 @@ boot_copy_region(struct boot_loader_state *state,
924
924
uint8_t image_index ;
925
925
#endif
926
926
927
+ #ifdef MCUBOOT_ENC_SCRATCH
928
+ size_t scratch_blk_off ;
929
+ #endif
930
+
927
931
TARGET_STATIC uint8_t buf [BOOT_SWAP_BUF_SIZE ] __attribute__((aligned (4 )));
928
932
929
933
#if !defined(MCUBOOT_ENC_IMAGES )
@@ -942,6 +946,15 @@ boot_copy_region(struct boot_loader_state *state,
942
946
if (rc != 0 ) {
943
947
return BOOT_EFLASH ;
944
948
}
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
945
958
946
959
#ifdef MCUBOOT_ENC_IMAGES
947
960
image_index = BOOT_CURR_IMG (state );
@@ -1002,6 +1015,15 @@ boot_copy_region(struct boot_loader_state *state,
1002
1015
}
1003
1016
#endif
1004
1017
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
+
1005
1027
rc = flash_area_write (fap_dst , off_dst + bytes_copied , buf , chunk_sz );
1006
1028
if (rc != 0 ) {
1007
1029
return BOOT_EFLASH ;
@@ -1257,6 +1279,15 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
1257
1279
} else {
1258
1280
rc = 0 ;
1259
1281
}
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
+
1260
1291
} else {
1261
1292
memset (bs -> enckey [1 ], 0xff , BOOT_ENC_KEY_SIZE );
1262
1293
}
@@ -1290,6 +1321,11 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
1290
1321
1291
1322
if (i != BOOT_ENC_KEY_SIZE ) {
1292
1323
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
1293
1329
}
1294
1330
}
1295
1331
#endif
0 commit comments