@@ -396,6 +396,82 @@ static int fake_rng(void *p_rng, unsigned char *output, size_t len)
396
396
#endif /* (MCUBOOT_ENCRYPT_RSA && MCUBOOT_USE_MBED_TLS && !MCUBOOT_USE_PSA_CRYPTO) ||
397
397
(MCUBOOT_ENCRYPT_EC256 && MCUBOOT_USE_MBED_TLS) */
398
398
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 (shared , 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
+
421
+ bootutil_hmac_sha256_init (& hmac );
422
+
423
+ rc = bootutil_hmac_sha256_set_key (& hmac , & derived_key [BOOT_ENC_KEY_SIZE ], 32 );
424
+ if (rc != 0 ) {
425
+ (void )bootutil_hmac_sha256_drop (& hmac );
426
+ return -1 ;
427
+ }
428
+
429
+ rc = bootutil_hmac_sha256_update (& hmac , key , BOOT_ENC_KEY_SIZE );
430
+ if (rc != 0 ) {
431
+ (void )bootutil_hmac_sha256_drop (& hmac );
432
+ return -1 ;
433
+ }
434
+
435
+ /* Assumes the tag buffer is at least sizeof(hmac_tag_size(state)) bytes */
436
+ rc = bootutil_hmac_sha256_finish (& hmac , tag , BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE );
437
+ (void )bootutil_hmac_sha256_drop (& hmac );
438
+ if (rc != 0 ) {
439
+ return -1 ;
440
+ }
441
+
442
+ if (bootutil_constant_time_compare (tag , mac , 32 ) != 0 ) {
443
+ return -1 ;
444
+ }
445
+
446
+
447
+ /*
448
+ * Finally decrypt the received ciphered key
449
+ */
450
+
451
+ bootutil_aes_ctr_init (& aes_ctr );
452
+ if (rc != 0 ) {
453
+ bootutil_aes_ctr_drop (& aes_ctr );
454
+ return -1 ;
455
+ }
456
+
457
+ rc = bootutil_aes_ctr_set_key (& aes_ctr , key );
458
+ if (rc != 0 ) {
459
+ bootutil_aes_ctr_drop (& aes_ctr );
460
+ return -1 ;
461
+ }
462
+
463
+ memset (counter , 0 , BOOT_ENC_BLOCK_SIZE );
464
+ rc = bootutil_aes_ctr_decrypt (& aes_ctr , counter , encrypted , BOOT_ENC_KEY_SIZE , 0 , decrypted );
465
+ if (rc != 0 ) {
466
+ bootutil_aes_ctr_drop (& aes_ctr );
467
+ return -1 ;
468
+ }
469
+
470
+ bootutil_aes_ctr_drop (& aes_ctr );
471
+
472
+ return rc ;
473
+ }
474
+ #endif /* MCUBOOT_ENCRYPT_EC256 || MCUBOOT_ENCRYPT_X25519 */
399
475
/*
400
476
* Decrypt an encryption key TLV.
401
477
*
@@ -415,13 +491,9 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
415
491
bootutil_ecdh_x25519_context pk_ctx ;
416
492
#endif
417
493
#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 ];
423
494
uint8_t private_key [PRIV_KEY_LEN ];
424
- uint8_t counter [BOOT_ENC_BLOCK_SIZE ];
495
+ uint8_t shared [SHARED_KEY_LEN ];
496
+ size_t len ;
425
497
#endif
426
498
#if !defined(MCUBOOT_ENCRYPT_KW )
427
499
uint8_t * cp ;
@@ -519,75 +591,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
519
591
520
592
#if defined(MCUBOOT_ENCRYPT_EC256 ) || defined(MCUBOOT_ENCRYPT_X25519 )
521
593
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 ;
594
+ rc = extract_aes (& buf [EC_CIPHERKEY_INDEX ], & buf [EC_TAG_INDEX ], & derived_key [BOOT_ENC_KEY_SIZE ], enckey );
591
595
592
596
#endif /* defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519) */
593
597
0 commit comments