Skip to content

Commit eb1d2d5

Browse files
committed
boot: Replace boot_encrypt by boot_enc_encrypt and boot_enc_decrypt
To be able to implement encryption with API that requires different calls for encryption and encryption, the boot_encrypt needs to be replaced with encryption/decryption specific functions. Signed-off-by: Dominik Ermel <[email protected]>
1 parent f763c5f commit eb1d2d5

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

boot/boot_serial/src/boot_serial_encryption.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ decrypt_region_inplace(struct boot_loader_state *state,
171171
blk_sz = tlv_off - (off + bytes_copied);
172172
}
173173
}
174-
boot_encrypt(BOOT_CURR_ENC(state), slot,
174+
boot_enc_decrypt(BOOT_CURR_ENC(state), slot,
175175
(off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
176176
blk_off, &buf[idx]);
177177
}

boot/bootutil/include/bootutil/enc_key.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ int boot_enc_load(struct enc_key_data *enc_state, int slot,
7171
struct boot_status *bs);
7272
bool boot_enc_valid(struct enc_key_data *enc_state, int image_index,
7373
const struct flash_area *fap);
74-
void boot_encrypt(struct enc_key_data *enc_state, int slot,
74+
void boot_enc_encrypt(struct enc_key_data *enc_state, int slot,
75+
uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);
76+
void boot_enc_decrypt(struct enc_key_data *enc_state, int slot,
7577
uint32_t off, uint32_t sz, uint32_t blk_off, uint8_t *buf);
7678
void boot_enc_zeroize(struct enc_key_data *enc_state);
7779

boot/bootutil/src/encrypted.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -698,14 +698,13 @@ boot_enc_valid(struct enc_key_data *enc_state, int image_index,
698698
}
699699

700700
void
701-
boot_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
701+
boot_enc_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
702702
uint32_t sz, uint32_t blk_off, uint8_t *buf)
703703
{
704-
struct enc_key_data *enc;
704+
struct enc_key_data *enc = &enc_state[slot];
705705
uint8_t nonce[16];
706706

707-
/* boot_copy_region will call boot_encrypt with sz = 0 when skipping over
708-
the TLVs. */
707+
/* Nothing to do with size == 0 */
709708
if (sz == 0) {
710709
return;
711710
}
@@ -717,11 +716,33 @@ boot_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
717716
nonce[14] = (uint8_t)(off >> 8);
718717
nonce[15] = (uint8_t)off;
719718

720-
enc = &enc_state[slot];
721719
assert(enc->valid == 1);
722720
bootutil_aes_ctr_encrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);
723721
}
724722

723+
void
724+
boot_enc_decrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
725+
uint32_t sz, uint32_t blk_off, uint8_t *buf)
726+
{
727+
struct enc_key_data *enc = &enc_state[slot];
728+
uint8_t nonce[16];
729+
730+
/* Nothing to do with size == 0 */
731+
if (sz == 0) {
732+
return;
733+
}
734+
735+
memset(nonce, 0, 12);
736+
off >>= 4;
737+
nonce[12] = (uint8_t)(off >> 24);
738+
nonce[13] = (uint8_t)(off >> 16);
739+
nonce[14] = (uint8_t)(off >> 8);
740+
nonce[15] = (uint8_t)off;
741+
742+
assert(enc->valid == 1);
743+
bootutil_aes_ctr_decrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);
744+
}
745+
725746
/**
726747
* Clears encrypted state after use.
727748
*/

boot/bootutil/src/loader.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,15 @@ boot_copy_region(struct boot_loader_state *state,
12961296
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
12971297
blk_off, &buf[idx]);
12981298
}
1299+
if (source_slot == 0) {
1300+
boot_enc_encrypt(BOOT_CURR_ENC(state), source_slot,
1301+
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
1302+
blk_off, &buf[idx]);
1303+
} else {
1304+
boot_enc_decrypt(BOOT_CURR_ENC(state), source_slot,
1305+
(abs_off + idx) - hdr->ih_hdr_size, blk_sz,
1306+
blk_off, &buf[idx]);
1307+
}
12991308
}
13001309
}
13011310
#endif
@@ -2773,10 +2782,15 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
27732782
* Part of the chunk is encrypted payload */
27742783
blk_sz = tlv_off - (bytes_copied);
27752784
}
2776-
boot_encrypt(BOOT_CURR_ENC(state), slot,
2777-
(bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2778-
blk_off, cur_dst);
2779-
2785+
if (slot == 0) {
2786+
boot_enc_encrypt(BOOT_CURR_ENC(state), slot,
2787+
(bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2788+
blk_off, cur_dst);
2789+
} else {
2790+
boot_enc_decrypt(BOOT_CURR_ENC(state), slot,
2791+
(bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2792+
blk_off, cur_dst);
2793+
}
27802794
bytes_copied += chunk_sz;
27812795
}
27822796
rc = 0;

0 commit comments

Comments
 (0)