@@ -244,28 +244,47 @@ int
244244boot_read_enc_key (const struct flash_area * fap , uint8_t slot , struct boot_status * bs )
245245{
246246 uint32_t off ;
247- #if MCUBOOT_SWAP_SAVE_ENCTLV
248247 uint32_t i ;
249- #endif
250248 int rc ;
249+ uint8_t * read_dst ;
250+ uint32_t read_size ;
251251
252- off = boot_enc_key_off (fap , slot );
253252#if MCUBOOT_SWAP_SAVE_ENCTLV
254- rc = flash_area_read (fap , off , bs -> enctlv [slot ], BOOT_ENC_TLV_ALIGN_SIZE );
253+ /* In this case we have stored entire encryted TLV in swap-state and bs->enckey
254+ * will be decrypted from the TLV.
255+ */
256+ BOOT_LOG_DBG ("boot_read_enc_key: TLV" );
257+ read_dst = bs -> enctlv [slot ];
258+ read_size = BOOT_ENC_TLV_ALIGN_SIZE ;
259+ #else
260+ BOOT_LOG_DBG ("boot_read_enc_key: RAW key" );
261+ read_dst = bs -> enckey [slot ];
262+ read_size = BOOT_ENC_KEY_ALIGN_SIZE ;
263+ #endif
264+
265+ off = boot_enc_key_off (fap , slot );
266+
267+ rc = flash_area_read (fap , off , read_dst , read_size );
255268 if (rc == 0 ) {
256- for (i = 0 ; i < BOOT_ENC_TLV_ALIGN_SIZE ; i ++ ) {
257- if (bs -> enctlv [ slot ] [i ] != 0xff ) {
269+ for (i = 0 ; i < read_size ; i ++ ) {
270+ if (read_dst [i ] != 0xff ) {
258271 break ;
259272 }
260273 }
261- /* Only try to decrypt non-erased TLV metadata */
262- if (i != BOOT_ENC_TLV_ALIGN_SIZE ) {
274+
275+ if (i == read_size ) {
276+ BOOT_LOG_ERR ("boot_read_enc_key: No key, read all 0xFF" );
277+ rc = 1 ;
278+ }
279+ #if MCUBOOT_SWAP_SAVE_ENCTLV
280+ else {
281+ /* read_dst is the same as bs->enctlv[slot], and serves as a source
282+ * of the encrypted key.
283+ */
263284 rc = boot_decrypt_key (bs -> enctlv [slot ], bs -> enckey [slot ]);
264285 }
265- }
266- #else
267- rc = flash_area_read (fap , off , bs -> enckey [slot ], BOOT_ENC_KEY_ALIGN_SIZE );
268286#endif
287+ }
269288
270289 return rc ;
271290}
0 commit comments