@@ -396,6 +396,80 @@ 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 (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 */
399
473
/*
400
474
* Decrypt an encryption key TLV.
401
475
*
@@ -415,13 +489,9 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
415
489
bootutil_ecdh_x25519_context pk_ctx ;
416
490
#endif
417
491
#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
492
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 ;
425
495
#endif
426
496
#if !defined(MCUBOOT_ENCRYPT_KW )
427
497
uint8_t * cp ;
@@ -519,75 +589,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
519
589
520
590
#if defined(MCUBOOT_ENCRYPT_EC256 ) || defined(MCUBOOT_ENCRYPT_X25519 )
521
591
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 );
591
593
592
594
#endif /* defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519) */
593
595
0 commit comments