@@ -380,35 +380,26 @@ static int fake_rng(void *p_rng, unsigned char *output, size_t len)
380
380
int
381
381
boot_decrypt_key (const uint8_t * buf , uint8_t * enckey )
382
382
{
383
- #if defined(MCUBOOT_ENCRYPT_RSA )
384
- bootutil_rsa_context rsa ;
385
- uint8_t * cp ;
386
- uint8_t * cpend ;
387
- size_t olen ;
388
- #endif
389
-
390
- BOOT_LOG_DBG ("boot_decrypt_key" );
391
- #if defined(MCUBOOT_ENCRYPT_EC256 )
392
- bootutil_ecdh_p256_context ecdh_p256 ;
393
- #endif
394
- #if defined(MCUBOOT_ENCRYPT_X25519 )
395
- bootutil_ecdh_x25519_context ecdh_x25519 ;
396
- #endif
397
383
#if defined(MCUBOOT_ENCRYPT_EC256 ) || defined(MCUBOOT_ENCRYPT_X25519 )
398
384
bootutil_hmac_sha256_context hmac ;
399
385
bootutil_aes_ctr_context aes_ctr ;
400
386
uint8_t tag [BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE ];
401
387
uint8_t shared [EC_SHARED_LEN ];
402
388
uint8_t derived_key [BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE ];
403
- uint8_t * cp ;
404
- uint8_t * cpend ;
405
389
uint8_t private_key [EC_PRIVK_LEN ];
406
390
uint8_t counter [BOOT_ENC_BLOCK_SIZE ];
391
+ #endif
392
+ #if !defined(MCUBOOT_ENCRYPT_KW )
393
+ bootutil_key_xchange_ctx pk_ctx ;
394
+ uint8_t * cp ;
395
+ uint8_t * cpend ;
407
396
uint16_t len ;
408
397
#endif
409
398
struct bootutil_key * bootutil_enc_key = NULL ;
410
399
int rc = -1 ;
411
400
401
+ BOOT_LOG_DBG ("boot_decrypt_key" );
402
+
412
403
rc = boot_enc_retrieve_private_key (& bootutil_enc_key );
413
404
if (rc ) {
414
405
return rc ;
@@ -418,21 +409,23 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
418
409
return rc ;
419
410
}
420
411
421
- #if defined(MCUBOOT_ENCRYPT_RSA )
422
-
423
- bootutil_rsa_init (& rsa );
412
+ #if !defined(MCUBOOT_ENCRYPT_KW )
424
413
cp = (uint8_t * )bootutil_enc_key -> key ;
425
414
cpend = cp + * bootutil_enc_key -> len ;
415
+ #endif
416
+
417
+ #if defined(MCUBOOT_ENCRYPT_RSA )
418
+ bootutil_rsa_init (& pk_ctx );
426
419
427
420
/* The enckey is encrypted through RSA so for decryption we need the private key */
428
- rc = bootutil_rsa_parse_private_key (& rsa , & cp , cpend );
421
+ rc = bootutil_rsa_parse_private_key (& pk_ctx , & cp , cpend );
429
422
if (rc ) {
430
- bootutil_rsa_drop (& rsa );
423
+ bootutil_rsa_drop (& pk_ctx );
431
424
return rc ;
432
425
}
433
426
434
- rc = bootutil_rsa_oaep_decrypt (& rsa , & olen , buf , enckey , BOOT_ENC_KEY_SIZE );
435
- bootutil_rsa_drop (& rsa );
427
+ rc = bootutil_rsa_oaep_decrypt (& pk_ctx , & len , buf , enckey , BOOT_ENC_KEY_SIZE );
428
+ bootutil_rsa_drop (& pk_ctx );
436
429
if (rc ) {
437
430
return rc ;
438
431
}
@@ -447,10 +440,6 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
447
440
#endif /* defined(MCUBOOT_ENCRYPT_KW) */
448
441
449
442
#if defined(MCUBOOT_ENCRYPT_EC256 )
450
-
451
- cp = (uint8_t * )bootutil_enc_key -> key ;
452
- cpend = cp + * bootutil_enc_key -> len ;
453
-
454
443
/*
455
444
* Load the stored EC256 decryption private key
456
445
*/
@@ -463,21 +452,17 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
463
452
/*
464
453
* First "element" in the TLV is the curve point (public key)
465
454
*/
466
- bootutil_ecdh_p256_init (& ecdh_p256 );
455
+ bootutil_ecdh_p256_init (& pk_ctx );
467
456
468
- rc = bootutil_ecdh_p256_shared_secret (& ecdh_p256 , & buf [EC_PUBK_INDEX ], private_key , shared );
469
- bootutil_ecdh_p256_drop (& ecdh_p256 );
457
+ rc = bootutil_ecdh_p256_shared_secret (& pk_ctx , & buf [EC_PUBK_INDEX ], private_key , shared );
458
+ bootutil_ecdh_p256_drop (& pk_ctx );
470
459
if (rc != 0 ) {
471
460
return -1 ;
472
461
}
473
462
474
463
#endif /* defined(MCUBOOT_ENCRYPT_EC256) */
475
464
476
465
#if defined(MCUBOOT_ENCRYPT_X25519 )
477
-
478
- cp = (uint8_t * )bootutil_enc_key -> key ;
479
- cpend = cp + * bootutil_enc_key -> len ;
480
-
481
466
/*
482
467
* Load the stored X25519 decryption private key
483
468
*/
@@ -491,10 +476,10 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
491
476
* First "element" in the TLV is the curve point (public key)
492
477
*/
493
478
494
- bootutil_ecdh_x25519_init (& ecdh_x25519 );
479
+ bootutil_ecdh_x25519_init (& pk_ctx );
495
480
496
- rc = bootutil_ecdh_x25519_shared_secret (& ecdh_x25519 , & buf [EC_PUBK_INDEX ], private_key , shared );
497
- bootutil_ecdh_x25519_drop (& ecdh_x25519 );
481
+ rc = bootutil_ecdh_x25519_shared_secret (& pk_ctx , & buf [EC_PUBK_INDEX ], private_key , shared );
482
+ bootutil_ecdh_x25519_drop (& pk_ctx );
498
483
if (!rc ) {
499
484
return -1 ;
500
485
}
0 commit comments