Skip to content

Commit 842ef0c

Browse files
committed
Add checks to crypto operation calls in MCUboot
Signed-off-by: Karambite <[email protected]>
1 parent 9fa0c6b commit 842ef0c

File tree

3 files changed

+69
-20
lines changed

3 files changed

+69
-20
lines changed

boot/bootutil/src/encrypted.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,10 @@ boot_enc_encrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
726726
nonce[15] = (uint8_t)off;
727727

728728
assert(enc->valid == 1);
729-
bootutil_aes_ctr_encrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);
729+
if (bootutil_aes_ctr_encrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);) {
730+
bootutil_aes_ctr_drop(&aes_ctr);
731+
return -1;
732+
}
730733
}
731734

732735
void
@@ -749,7 +752,10 @@ boot_enc_decrypt(struct enc_key_data *enc_state, int slot, uint32_t off,
749752
nonce[15] = (uint8_t)off;
750753

751754
assert(enc->valid == 1);
752-
bootutil_aes_ctr_decrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf);
755+
if (bootutil_aes_ctr_decrypt(&enc->aes_ctr, nonce, buf, sz, blk_off, buf)) {
756+
bootutil_aes_ctr_drop(&aes_ctr);
757+
return -1;
758+
}
753759
}
754760

755761
/**

boot/bootutil/src/image_rsa.c

+23-8
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,15 @@ pss_mgf1(uint8_t *mask, const uint8_t *hash)
9494

9595
while (count > 0) {
9696
bootutil_sha_init(&ctx);
97-
bootutil_sha_update(&ctx, hash, PSS_HLEN);
98-
bootutil_sha_update(&ctx, counter, 4);
99-
bootutil_sha_finish(&ctx, htmp);
97+
if (bootutil_sha_update(&ctx, hash, PSS_HLEN)) {
98+
goto out;
99+
}
100+
if (bootutil_sha_update(&ctx, counter, 4)) {
101+
goto out;
102+
}
103+
if(bootutil_sha_finish(&ctx, htmp)){
104+
goto out;
105+
}
100106

101107
counter[3]++;
102108

@@ -109,6 +115,7 @@ pss_mgf1(uint8_t *mask, const uint8_t *hash)
109115
count -= bytes;
110116
}
111117

118+
out:
112119
bootutil_sha_drop(&ctx);
113120
}
114121

@@ -222,17 +229,25 @@ bootutil_cmp_rsasig(bootutil_rsa_context *ctx, uint8_t *hash, uint32_t hlen,
222229

223230
/* Step 13. Let H' = Hash(M') */
224231
bootutil_sha_init(&shactx);
225-
bootutil_sha_update(&shactx, pss_zeros, 8);
226-
bootutil_sha_update(&shactx, hash, PSS_HLEN);
227-
bootutil_sha_update(&shactx, &db_mask[PSS_MASK_SALT_POS], PSS_SLEN);
228-
bootutil_sha_finish(&shactx, h2);
229-
bootutil_sha_drop(&shactx);
232+
if (bootutil_sha_update(&shactx, pss_zeros, 8)) {
233+
goto out;
234+
}
235+
if (bootutil_sha_update(&shactx, hash, PSS_HLEN)) {
236+
goto out;
237+
}
238+
if (bootutil_sha_update(&shactx, &db_mask[PSS_MASK_SALT_POS], PSS_SLEN)) {
239+
goto out;
240+
}
241+
if (bootutil_sha_finish(&shactx, h2)) {
242+
goto out;
243+
}
230244

231245
/* Step 14. If H = H', output "consistent". Otherwise, output
232246
* "inconsistent". */
233247
FIH_CALL(boot_fih_memequal, fih_rc, h2, &em[PSS_HASH_OFFSET], PSS_HLEN);
234248

235249
out:
250+
bootutil_sha_drop(&shactx);
236251
FIH_RET(fih_rc);
237252
}
238253

boot/bootutil/src/image_validate.c

+38-10
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ bootutil_img_hash(struct boot_loader_state *state,
140140
/* in some cases (split image) the hash is seeded with data from
141141
* the loader image */
142142
if (seed && (seed_len > 0)) {
143-
bootutil_sha_update(&sha_ctx, seed, seed_len);
143+
rc = bootutil_sha_update(&sha_ctx, seed, seed_len);
144+
if (rc){
145+
bootutil_sha_drop(&sha_ctx);
146+
return rc;
147+
}
144148
}
145149

146150
/* Hash is computed over image header and image itself. */
@@ -155,12 +159,21 @@ bootutil_img_hash(struct boot_loader_state *state,
155159
/* No chunk loading, storage is mapped to address space and can
156160
* be directly given to hashing function.
157161
*/
158-
bootutil_sha_update(&sha_ctx, (void *)flash_area_get_off(fap), size);
162+
rc = bootutil_sha_update(&sha_ctx, (void *)flash_area_get_off(fap), size);
163+
if (rc){
164+
bootutil_sha_drop(&sha_ctx);
165+
return rc;
166+
}
159167
#else /* MCUBOOT_HASH_STORAGE_DIRECTLY */
160168
#ifdef MCUBOOT_RAM_LOAD
161-
bootutil_sha_update(&sha_ctx,
169+
rc = bootutil_sha_update(&sha_ctx,
162170
(void*)(IMAGE_RAM_BASE + hdr->ih_load_addr),
163171
size);
172+
if (rc){
173+
bootutil_sha_drop(&sha_ctx);
174+
return rc;
175+
}
176+
164177
#else
165178
for (off = 0; off < size; off += blk_sz) {
166179
blk_sz = size - off;
@@ -202,14 +215,18 @@ bootutil_img_hash(struct boot_loader_state *state,
202215
}
203216
}
204217
#endif
205-
bootutil_sha_update(&sha_ctx, tmp_buf, blk_sz);
218+
rc = bootutil_sha_update(&sha_ctx, tmp_buf, blk_sz);
219+
if (rc){
220+
bootutil_sha_drop(&sha_ctx);
221+
return rc;
222+
}
206223
}
207224
#endif /* MCUBOOT_RAM_LOAD */
208225
#endif /* MCUBOOT_HASH_STORAGE_DIRECTLY */
209-
bootutil_sha_finish(&sha_ctx, hash_result);
226+
rc = bootutil_sha_finish(&sha_ctx, hash_result);
210227
bootutil_sha_drop(&sha_ctx);
211228

212-
return 0;
229+
return rc;
213230
}
214231
#endif
215232

@@ -287,8 +304,12 @@ bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
287304
for (i = 0; i < bootutil_key_cnt; i++) {
288305
key = &bootutil_keys[i];
289306
bootutil_sha_init(&sha_ctx);
290-
bootutil_sha_update(&sha_ctx, key->key, *key->len);
291-
bootutil_sha_finish(&sha_ctx, hash);
307+
if (bootutil_sha_update(&sha_ctx, key->key, *key->len)){
308+
break;
309+
}
310+
if (bootutil_sha_finish(&sha_ctx, hash)){
311+
break;
312+
}
292313
if (!memcmp(hash, keyhash, keyhash_len)) {
293314
bootutil_sha_drop(&sha_ctx);
294315
return i;
@@ -310,9 +331,16 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
310331
FIH_DECLARE(fih_rc, FIH_FAILURE);
311332

312333
bootutil_sha_init(&sha_ctx);
313-
bootutil_sha_update(&sha_ctx, key, key_len);
314-
bootutil_sha_finish(&sha_ctx, hash);
334+
rc = bootutil_sha_update(&sha_ctx, key, key_len);
335+
if (rc){
336+
bootutil_sha_drop(&sha_ctx);
337+
return rc;
338+
}
339+
rc = bootutil_sha_finish(&sha_ctx, hash);
315340
bootutil_sha_drop(&sha_ctx);
341+
if (rc){
342+
return rc;
343+
}
316344

317345
rc = boot_retrieve_public_key_hash(image_index, key_hash, &key_hash_size);
318346
if (rc) {

0 commit comments

Comments
 (0)