Skip to content

Commit 34bf2a0

Browse files
committed
boot_decrypt_key: Move ECIES AES key decryption out
Signed-off-by: Dominik Ermel <[email protected]>
1 parent c792284 commit 34bf2a0

File tree

1 file changed

+77
-75
lines changed

1 file changed

+77
-75
lines changed

boot/bootutil/src/encrypted.c

Lines changed: 77 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,80 @@ static int fake_rng(void *p_rng, unsigned char *output, size_t len)
396396
#endif /* (MCUBOOT_ENCRYPT_RSA && MCUBOOT_USE_MBED_TLS && !MCUBOOT_USE_PSA_CRYPTO) ||
397397
(MCUBOOT_ENCRYPT_EC256 && MCUBOOT_USE_MBED_TLS) */
398398

399+
#if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519)
400+
static int extract_aes(const uint8_t *encrypted, const uint8_t *mac, const uint8_t *key, uint8_t *decrypted)
401+
{
402+
bootutil_hmac_sha256_context hmac;
403+
bootutil_aes_ctr_context aes_ctr;
404+
uint8_t tag[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
405+
uint8_t derived_key[BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
406+
uint8_t counter[BOOT_ENC_BLOCK_SIZE];
407+
int rc;
408+
size_t len;
409+
410+
len = BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE;
411+
rc = hkdf(key, SHARED_KEY_LEN, (uint8_t *)"MCUBoot_ECIES_v1", 16,
412+
derived_key, &len);
413+
if (rc != 0 || len != (BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE)) {
414+
return -1;
415+
}
416+
417+
/*
418+
* HMAC the key and check that our received MAC matches the generated tag
419+
*/
420+
bootutil_hmac_sha256_init(&hmac);
421+
422+
rc = bootutil_hmac_sha256_set_key(&hmac, &derived_key[BOOT_ENC_KEY_SIZE], 32);
423+
if (rc != 0) {
424+
(void)bootutil_hmac_sha256_drop(&hmac);
425+
return -1;
426+
}
427+
428+
rc = bootutil_hmac_sha256_update(&hmac, encrypted, BOOT_ENC_KEY_SIZE);
429+
if (rc != 0) {
430+
(void)bootutil_hmac_sha256_drop(&hmac);
431+
return -1;
432+
}
433+
434+
/* Assumes the tag buffer is at least sizeof(hmac_tag_size(state)) bytes */
435+
rc = bootutil_hmac_sha256_finish(&hmac, tag, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE);
436+
(void)bootutil_hmac_sha256_drop(&hmac);
437+
if (rc != 0) {
438+
return -1;
439+
}
440+
441+
if (bootutil_constant_time_compare(tag, mac, 32) != 0) {
442+
return -1;
443+
}
444+
445+
446+
/*
447+
* Finally decrypt the received ciphered key
448+
*/
449+
bootutil_aes_ctr_init(&aes_ctr);
450+
if (rc != 0) {
451+
bootutil_aes_ctr_drop(&aes_ctr);
452+
return -1;
453+
}
454+
455+
rc = bootutil_aes_ctr_set_key(&aes_ctr, key);
456+
if (rc != 0) {
457+
bootutil_aes_ctr_drop(&aes_ctr);
458+
return -1;
459+
}
460+
461+
memset(counter, 0, BOOT_ENC_BLOCK_SIZE);
462+
rc = bootutil_aes_ctr_decrypt(&aes_ctr, counter, encrypted, BOOT_ENC_KEY_SIZE, 0, decrypted);
463+
if (rc != 0) {
464+
bootutil_aes_ctr_drop(&aes_ctr);
465+
return -1;
466+
}
467+
468+
bootutil_aes_ctr_drop(&aes_ctr);
469+
470+
return rc;
471+
}
472+
#endif /* MCUBOOT_ENCRYPT_EC256 || MCUBOOT_ENCRYPT_X25519 */
399473
/*
400474
* Decrypt an encryption key TLV.
401475
*
@@ -415,13 +489,9 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
415489
bootutil_ecdh_x25519_context pk_ctx;
416490
#endif
417491
#if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519)
418-
bootutil_hmac_sha256_context hmac;
419-
bootutil_aes_ctr_context aes_ctr;
420-
uint8_t tag[BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
421-
uint8_t shared[SHARED_KEY_LEN];
422-
uint8_t derived_key[BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE];
423492
uint8_t private_key[PRIV_KEY_LEN];
424-
uint8_t counter[BOOT_ENC_BLOCK_SIZE];
493+
uint8_t shared[SHARED_KEY_LEN];
494+
size_t len;
425495
#endif
426496
#if !defined(MCUBOOT_ENCRYPT_KW)
427497
uint8_t *cp;
@@ -519,75 +589,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
519589

520590
#if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519)
521591

522-
/*
523-
* Expand shared secret to create keys for AES-128-CTR + HMAC-SHA256
524-
*/
525-
526-
len = BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE;
527-
rc = hkdf(shared, SHARED_KEY_LEN, (uint8_t *)"MCUBoot_ECIES_v1", 16,
528-
derived_key, &len);
529-
if (rc != 0 || len != (BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE)) {
530-
return -1;
531-
}
532-
533-
/*
534-
* HMAC the key and check that our received MAC matches the generated tag
535-
*/
536-
537-
bootutil_hmac_sha256_init(&hmac);
538-
539-
rc = bootutil_hmac_sha256_set_key(&hmac, &derived_key[BOOT_ENC_KEY_SIZE], 32);
540-
if (rc != 0) {
541-
(void)bootutil_hmac_sha256_drop(&hmac);
542-
return -1;
543-
}
544-
545-
rc = bootutil_hmac_sha256_update(&hmac, &buf[EC_CIPHERKEY_INDEX], BOOT_ENC_KEY_SIZE);
546-
if (rc != 0) {
547-
(void)bootutil_hmac_sha256_drop(&hmac);
548-
return -1;
549-
}
550-
551-
/* Assumes the tag buffer is at least sizeof(hmac_tag_size(state)) bytes */
552-
rc = bootutil_hmac_sha256_finish(&hmac, tag, BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE);
553-
if (rc != 0) {
554-
(void)bootutil_hmac_sha256_drop(&hmac);
555-
return -1;
556-
}
557-
558-
if (bootutil_constant_time_compare(tag, &buf[EC_TAG_INDEX], 32) != 0) {
559-
(void)bootutil_hmac_sha256_drop(&hmac);
560-
return -1;
561-
}
562-
563-
bootutil_hmac_sha256_drop(&hmac);
564-
565-
/*
566-
* Finally decrypt the received ciphered key
567-
*/
568-
569-
bootutil_aes_ctr_init(&aes_ctr);
570-
if (rc != 0) {
571-
bootutil_aes_ctr_drop(&aes_ctr);
572-
return -1;
573-
}
574-
575-
rc = bootutil_aes_ctr_set_key(&aes_ctr, derived_key);
576-
if (rc != 0) {
577-
bootutil_aes_ctr_drop(&aes_ctr);
578-
return -1;
579-
}
580-
581-
memset(counter, 0, BOOT_ENC_BLOCK_SIZE);
582-
rc = bootutil_aes_ctr_decrypt(&aes_ctr, counter, &buf[EC_CIPHERKEY_INDEX], BOOT_ENC_KEY_SIZE, 0, enckey);
583-
if (rc != 0) {
584-
bootutil_aes_ctr_drop(&aes_ctr);
585-
return -1;
586-
}
587-
588-
bootutil_aes_ctr_drop(&aes_ctr);
589-
590-
rc = 0;
592+
rc = extract_aes(&buf[EC_CIPHERKEY_INDEX], &buf[EC_TAG_INDEX], &derived_key[BOOT_ENC_KEY_SIZE], enckey);
591593

592594
#endif /* defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519) */
593595

0 commit comments

Comments
 (0)