Skip to content

Commit 4992f07

Browse files
committed
bootutil: Use common type name for key exchange context
The commit uses typedef to define common name for key exchange in order to reduce number of local definitions and #ifdef in code. Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
1 parent c136f4a commit 4992f07

File tree

4 files changed

+27
-37
lines changed

4 files changed

+27
-37
lines changed

boot/bootutil/include/bootutil/crypto/ecdh_p256.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extern "C" {
3535

3636
#if defined(MCUBOOT_USE_TINYCRYPT)
3737
typedef uintptr_t bootutil_ecdh_p256_context;
38+
typedef bootutil_ecdh_p256_context bootutil_key_xchange_ctx;
3839
static inline void bootutil_ecdh_p256_init(bootutil_ecdh_p256_context *ctx)
3940
{
4041
(void)ctx;
@@ -80,6 +81,7 @@ typedef struct bootutil_ecdh_p256_context {
8081
mbedtls_mpi z;
8182
mbedtls_mpi d;
8283
} bootutil_ecdh_p256_context;
84+
typedef bootutil_ecdh_p256_context bootutil_key_xchange_ctx;
8385

8486
static inline void bootutil_ecdh_p256_init(bootutil_ecdh_p256_context *ctx)
8587
{

boot/bootutil/include/bootutil/crypto/ecdh_x25519.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
2626
const uint8_t peer_public_value[32]);
2727

2828
typedef uintptr_t bootutil_ecdh_x25519_context;
29+
typedef bootutil_ecdh_x25519_context bootutil_key_xchange_ctx;
2930
static inline void bootutil_ecdh_x25519_init(bootutil_ecdh_x25519_context *ctx)
3031
{
3132
(void)ctx;

boot/bootutil/include/bootutil/crypto/rsa.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ extern "C" {
6868
typedef struct {
6969
psa_key_id_t key_id;
7070
} bootutil_rsa_context;
71+
typedef bootutil_rsa_context bootutil_key_xchange_ctx;
7172

7273
static inline void bootutil_rsa_init(bootutil_rsa_context *ctx)
7374
{
@@ -176,6 +177,7 @@ static inline int bootutil_rsassa_pss_verify(const bootutil_rsa_context *ctx,
176177
#elif defined(MCUBOOT_USE_MBED_TLS)
177178

178179
typedef mbedtls_rsa_context bootutil_rsa_context;
180+
typedef bootutil_rsa_context bootutil_key_xchange_ctx;
179181

180182
static inline void bootutil_rsa_init(bootutil_rsa_context *ctx)
181183
{

boot/bootutil/src/encrypted.c

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -380,35 +380,26 @@ static int fake_rng(void *p_rng, unsigned char *output, size_t len)
380380
int
381381
boot_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_xchange_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

Comments
 (0)