37
37
#endif
38
38
#define DECOMP_BUF_ALLOC_SIZE (DECOMP_BUF_SIZE + DECOMP_BUF_EXTRA_SIZE)
39
39
40
+ #define DECRYPTION_BLOCK_SIZE_AES128 16
41
+ #define DECRYPTION_BLOCK_SIZE_AES256 32
42
+
40
43
/* Number of times that consumed data by decompression system can be 0 in a row before aborting */
41
44
#define OFFSET_ZERO_CHECK_TIMES 3
42
45
@@ -187,6 +190,7 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
187
190
struct enc_key_data * enc_state ;
188
191
int image_index ;
189
192
uint32_t comp_size = 0 ;
193
+ uint8_t decryption_block_size = 0 ;
190
194
191
195
rc = bootutil_get_img_decrypted_comp_size (hdr , fap , & comp_size );
192
196
@@ -209,6 +213,18 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
209
213
!boot_enc_valid (enc_state , 1 )) {
210
214
return -1 ;
211
215
}
216
+
217
+ if (MUST_DECRYPT (fap , image_index , hdr )) {
218
+ if (hdr -> ih_flags & IMAGE_F_ENCRYPTED_AES128 ) {
219
+ decryption_block_size = DECRYPTION_BLOCK_SIZE_AES128 ;
220
+ } else if (hdr -> ih_flags & IMAGE_F_ENCRYPTED_AES256 ) {
221
+ decryption_block_size = DECRYPTION_BLOCK_SIZE_AES256 ;
222
+ } else {
223
+ LOG_ERR ("Unknown decryption block size" );
224
+ rc = BOOT_EBADIMAGE ;
225
+ goto finish_end ;
226
+ }
227
+ }
212
228
#endif
213
229
214
230
bootutil_sha_init (& sha_ctx );
@@ -319,11 +335,17 @@ int bootutil_img_hash_decompress(struct boot_loader_state *state, struct image_h
319
335
}
320
336
321
337
#ifdef MCUBOOT_ENC_IMAGES
322
- if (MUST_DECRYPT (fap , image_index , hdr )) {
323
- boot_enc_decrypt (enc_state , 1 , read_pos ,
324
- copy_size , (read_pos & 0xf ),
325
- tmp_buf );
326
- }
338
+ if (MUST_DECRYPT (fap , image_index , hdr )) {
339
+ uint8_t dummy_bytes = 0 ;
340
+
341
+ if ((copy_size % decryption_block_size )) {
342
+ dummy_bytes = decryption_block_size - (copy_size % decryption_block_size );
343
+ memset (& tmp_buf [copy_size ], 0x00 , dummy_bytes );
344
+ }
345
+
346
+ boot_enc_decrypt (enc_state , 1 , read_pos , (copy_size + dummy_bytes ), (read_pos & 0xf ),
347
+ tmp_buf );
348
+ }
327
349
#endif
328
350
329
351
/* Decompress data in chunks, writing it back with a larger write offset of the primary
@@ -990,6 +1012,7 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
990
1012
991
1013
#ifdef MCUBOOT_ENC_IMAGES
992
1014
uint32_t comp_size = 0 ;
1015
+ uint8_t decryption_block_size = 0 ;
993
1016
#endif
994
1017
995
1018
hdr = boot_img_hdr (state , BOOT_SECONDARY_SLOT );
@@ -1002,6 +1025,14 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
1002
1025
rc = BOOT_EBADIMAGE ;
1003
1026
goto finish ;
1004
1027
}
1028
+
1029
+ if (IS_ENCRYPTED (hdr )) {
1030
+ if (hdr -> ih_flags & IMAGE_F_ENCRYPTED_AES128 ) {
1031
+ decryption_block_size = DECRYPTION_BLOCK_SIZE_AES128 ;
1032
+ } else if (hdr -> ih_flags & IMAGE_F_ENCRYPTED_AES256 ) {
1033
+ decryption_block_size = DECRYPTION_BLOCK_SIZE_AES256 ;
1034
+ }
1035
+ }
1005
1036
#endif
1006
1037
1007
1038
/* Setup decompression system */
@@ -1107,7 +1138,14 @@ int boot_copy_region_decompress(struct boot_loader_state *state, const struct fl
1107
1138
1108
1139
#ifdef MCUBOOT_ENC_IMAGES
1109
1140
if (IS_ENCRYPTED (hdr )) {
1110
- boot_enc_decrypt (BOOT_CURR_ENC (state ), 1 , pos , copy_size , (pos & 0xf ), buf );
1141
+ uint8_t dummy_bytes = 0 ;
1142
+
1143
+ if ((copy_size % decryption_block_size )) {
1144
+ dummy_bytes = decryption_block_size - (copy_size % decryption_block_size );
1145
+ memset (& buf [copy_size ], 0x00 , dummy_bytes );
1146
+ }
1147
+
1148
+ boot_enc_decrypt (BOOT_CURR_ENC (state ), 1 , pos , (copy_size + dummy_bytes ), (pos & 0xf ), buf );
1111
1149
}
1112
1150
#endif
1113
1151
0 commit comments