@@ -111,7 +111,7 @@ read_image_size(const struct flash_area *fa_p,
111111 * @return 0 on success; nonzero on failure.
112112 */
113113static int
114- decrypt_region_inplace (struct boot_loader_state * state ,
114+ decrypt_region_inplace (struct enc_data * enc_data ,
115115 const struct flash_area * fap ,
116116 struct image_header * hdr ,
117117 uint32_t off , uint32_t sz )
@@ -123,11 +123,8 @@ decrypt_region_inplace(struct boot_loader_state *state,
123123 size_t blk_off ;
124124 uint16_t idx ;
125125 uint32_t blk_sz ;
126- int slot = flash_area_id_to_multi_image_slot (BOOT_CURR_IMG (state ),
127- flash_area_get_id (fap ));
128126 uint8_t buf [sz ] __attribute__((aligned ));
129127 assert (sz <= sizeof buf );
130- assert (slot >= 0 );
131128
132129 bytes_copied = 0 ;
133130 while (bytes_copied < sz ) {
@@ -169,7 +166,7 @@ decrypt_region_inplace(struct boot_loader_state *state,
169166 blk_sz = tlv_off - (off + bytes_copied );
170167 }
171168 }
172- boot_enc_decrypt (BOOT_CURR_ENC_SLOT ( state , slot ) ,
169+ boot_enc_decrypt (enc_data ,
173170 (off + bytes_copied + idx ) - hdr -> ih_hdr_size , blk_sz ,
174171 blk_off , & buf [idx ]);
175172 }
@@ -217,6 +214,7 @@ decrypt_image_inplace(const struct flash_area *fa_p,
217214 size_t sect_count ;
218215 size_t sect ;
219216 struct flash_sector sector ;
217+ struct enc_key_data enc_data ;
220218
221219 boot_state_clear (state );
222220 memset (& _bs , 0 , sizeof (struct boot_status ));
@@ -236,22 +234,25 @@ decrypt_image_inplace(const struct flash_area *fa_p,
236234 /* Load the encryption keys into cache */
237235 rc = boot_enc_load (state , BOOT_SLOT_PRIMARY , hdr , fa_p , bs );
238236 if (rc < 0 ) {
239- FIH_RET ( fih_rc ) ;
237+ goto total_out ;
240238 }
241- if (rc == 0 && boot_enc_set_key (BOOT_CURR_ENC_SLOT (state , BOOT_SLOT_PRIMARY ), bs -> enckey [BOOT_SLOT_PRIMARY ])) {
242- FIH_RET (fih_rc );
239+
240+ boot_enc_init (& enc_data );
241+
242+ if (rc == 0 && boot_enc_set_key (& enc_data , bs -> enckey [BOOT_SLOT_PRIMARY ])) {
243+ goto total_out ;
243244 }
244245 }
245246 else
246247 {
247248 /* Expected encrypted image! */
248- FIH_RET ( fih_rc ) ;
249+ goto total_out ;
249250 }
250251
251252 uint32_t src_size = 0 ;
252253 rc = read_image_size (fa_p ,hdr , & src_size );
253254 if (rc != 0 ) {
254- FIH_RET ( fih_rc ) ;
255+ goto total_out ;
255256 }
256257
257258 /* TODO: This assumes every sector has an equal size, should instead use
@@ -261,14 +262,16 @@ decrypt_image_inplace(const struct flash_area *fa_p,
261262 sect_size = sector .fs_size ;
262263 sect_count = fa_p -> fa_size / sect_size ;
263264 for (sect = 0 , size = 0 ; size < src_size && sect < sect_count ; sect ++ ) {
264- rc = decrypt_region_inplace (state , fa_p ,hdr , size , sect_size );
265+ rc = decrypt_region_inplace (enc_data , fa_p , hdr , size , sect_size );
265266 if (rc != 0 ) {
266267 FIH_RET (fih_rc );
267268 }
268269 size += sect_size ;
269270 }
270271
271272 fih_rc = FIH_SUCCESS ;
273+ total_out :
274+ boot_enc_deinit (& enc_data );
272275 FIH_RET (fih_rc );
273276}
274277
0 commit comments