@@ -380,35 +380,26 @@ static int fake_rng(void *p_rng, unsigned char *output, size_t len)
380380int
381381boot_decrypt_key (const uint8_t * buf , uint8_t * enckey )
382382{
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
397383#if defined(MCUBOOT_ENCRYPT_EC256 ) || defined(MCUBOOT_ENCRYPT_X25519 )
398384 bootutil_hmac_sha256_context hmac ;
399385 bootutil_aes_ctr_context aes_ctr ;
400386 uint8_t tag [BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE ];
401387 uint8_t shared [EC_SHARED_LEN ];
402388 uint8_t derived_key [BOOT_ENC_KEY_SIZE + BOOTUTIL_CRYPTO_SHA256_DIGEST_SIZE ];
403- uint8_t * cp ;
404- uint8_t * cpend ;
405389 uint8_t private_key [EC_PRIVK_LEN ];
406390 uint8_t counter [BOOT_ENC_BLOCK_SIZE ];
391+ #endif
392+ #if !defined(MCUBOOT_ENCRYPT_KW )
393+ bootutil_key_exchange_ctx pk_ctx ;
394+ uint8_t * cp ;
395+ uint8_t * cpend ;
407396 uint16_t len ;
408397#endif
409398 struct bootutil_key * bootutil_enc_key = NULL ;
410399 int rc = -1 ;
411400
401+ BOOT_LOG_DBG ("boot_decrypt_key" );
402+
412403 rc = boot_enc_retrieve_private_key (& bootutil_enc_key );
413404 if (rc ) {
414405 return rc ;
@@ -418,21 +409,23 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
418409 return rc ;
419410 }
420411
421- #if defined(MCUBOOT_ENCRYPT_RSA )
422-
423- bootutil_rsa_init (& rsa );
412+ #if !defined(MCUBOOT_ENCRYPT_KW )
424413 cp = (uint8_t * )bootutil_enc_key -> key ;
425414 cpend = cp + * bootutil_enc_key -> len ;
415+ #endif
416+
417+ #if defined(MCUBOOT_ENCRYPT_RSA )
418+ bootutil_rsa_init (& pk_ctx );
426419
427420 /* 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 );
429422 if (rc ) {
430- bootutil_rsa_drop (& rsa );
423+ bootutil_rsa_drop (& pk_ctx );
431424 return rc ;
432425 }
433426
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 );
436429 if (rc ) {
437430 return rc ;
438431 }
@@ -447,10 +440,6 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
447440#endif /* defined(MCUBOOT_ENCRYPT_KW) */
448441
449442#if defined(MCUBOOT_ENCRYPT_EC256 )
450-
451- cp = (uint8_t * )bootutil_enc_key -> key ;
452- cpend = cp + * bootutil_enc_key -> len ;
453-
454443 /*
455444 * Load the stored EC256 decryption private key
456445 */
@@ -463,21 +452,17 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
463452 /*
464453 * First "element" in the TLV is the curve point (public key)
465454 */
466- bootutil_ecdh_p256_init (& ecdh_p256 );
455+ bootutil_ecdh_p256_init (& pk_ctx );
467456
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 );
470459 if (rc != 0 ) {
471460 return -1 ;
472461 }
473462
474463#endif /* defined(MCUBOOT_ENCRYPT_EC256) */
475464
476465#if defined(MCUBOOT_ENCRYPT_X25519 )
477-
478- cp = (uint8_t * )bootutil_enc_key -> key ;
479- cpend = cp + * bootutil_enc_key -> len ;
480-
481466 /*
482467 * Load the stored X25519 decryption private key
483468 */
@@ -491,10 +476,10 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
491476 * First "element" in the TLV is the curve point (public key)
492477 */
493478
494- bootutil_ecdh_x25519_init (& ecdh_x25519 );
479+ bootutil_ecdh_x25519_init (& pk_ctx );
495480
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 );
498483 if (!rc ) {
499484 return -1 ;
500485 }
0 commit comments