Skip to content

Commit 73315f7

Browse files
taltenbachd3zd3z
authored andcommitted
bootutil: Fix memory leak in HKDF implementation
The bootutil_hmac_sha256_set_key routine performs some dynamic memory allocations when mbedTLS is used. To properly free the allocated memory, bootutil_hmac_sha256_drop must be called before reinitializing the HMAC context using bootutil_hmac_sha256_init. However, in the hkdf routine, the HMAC context was freed only once even though it was initialized multiple times. Signed-off-by: Thomas Altenbach <[email protected]>
1 parent 453096b commit 73315f7

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

boot/bootutil/src/encrypted.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ hkdf(uint8_t *ikm, uint16_t ikm_len, uint8_t *info, uint16_t info_len,
276276
goto error;
277277
}
278278

279+
bootutil_hmac_sha256_drop(&hmac);
280+
279281
/*
280282
* Expand
281283
*/
@@ -315,6 +317,8 @@ hkdf(uint8_t *ikm, uint16_t ikm_len, uint8_t *info, uint16_t info_len,
315317
goto error;
316318
}
317319

320+
bootutil_hmac_sha256_drop(&hmac);
321+
318322
if (len > BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE) {
319323
memcpy(&okm[off], T, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE);
320324
len -= BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE;
@@ -324,7 +328,6 @@ hkdf(uint8_t *ikm, uint16_t ikm_len, uint8_t *info, uint16_t info_len,
324328
}
325329
}
326330

327-
bootutil_hmac_sha256_drop(&hmac);
328331
return 0;
329332

330333
error:

0 commit comments

Comments
 (0)