Skip to content

Commit 1b28c6b

Browse files
committed
[nrf fromlist] boot: Enable Encryption with PSA + ECDSA
This configuration was not supported until now. Upstream PR #: 2435 Signed-off-by: Artur Hadasz <[email protected]>
1 parent e1f2ab3 commit 1b28c6b

File tree

3 files changed

+103
-14
lines changed

3 files changed

+103
-14
lines changed

boot/bootutil/src/encrypted.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
#include "bootutil/crypto/aes_kw.h"
2323
#endif
2424

25+
#if !defined(MCUBOOT_USE_PSA_CRYPTO)
2526
#if defined(MCUBOOT_ENCRYPT_EC256)
2627
#include "bootutil/crypto/ecdh_p256.h"
2728
#endif
2829

29-
#if !defined(MCUBOOT_USE_PSA_CRYPTO)
3030
#if defined(MCUBOOT_ENCRYPT_X25519)
3131
#include "bootutil/crypto/ecdh_x25519.h"
3232
#endif
@@ -50,7 +50,7 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
5050
#include "bootutil_priv.h"
5151

5252
/* NOUP Fixme: */
53-
#if !defined(CONFIG_BOOT_ED25519_PSA)
53+
#if !defined(CONFIG_BOOT_ED25519_PSA) && !defined(CONFIG_BOOT_ECDSA_PSA)
5454
#if defined(MCUBOOT_ENCRYPT_EC256) || defined(MCUBOOT_ENCRYPT_X25519)
5555
#if defined(_compare)
5656
static inline int bootutil_constant_time_compare(const uint8_t *a, const uint8_t *b, size_t size)
@@ -105,7 +105,7 @@ static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1;
105105
* curve keypair. See RFC5208 and RFC5915.
106106
*/
107107
static int
108-
parse_ec256_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key)
108+
parse_priv_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key)
109109
{
110110
int rc;
111111
size_t len;
@@ -180,7 +180,7 @@ static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG \
180180
MBEDTLS_OID_ORG_GOV X25519_OID;
181181

182182
static int
183-
parse_x25519_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key)
183+
parse_priv_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key)
184184
{
185185
size_t len;
186186
int version;
@@ -455,7 +455,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
455455
* Load the stored EC256 decryption private key
456456
*/
457457

458-
rc = parse_ec256_enckey(&cp, cpend, private_key);
458+
rc = parse_priv_enckey(&cp, cpend, private_key);
459459
if (rc) {
460460
return rc;
461461
}
@@ -482,7 +482,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
482482
* Load the stored X25519 decryption private key
483483
*/
484484

485-
rc = parse_x25519_enckey(&cp, cpend, private_key);
485+
rc = parse_priv_enckey(&cp, cpend, private_key);
486486
if (rc) {
487487
return rc;
488488
}
@@ -580,7 +580,7 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
580580

581581
return rc;
582582
}
583-
#endif /* CONFIG_BOOT_ED25519_PSA */
583+
#endif /* CONFIG_BOOT_ED25519_PSA && CONFIG_BOOT_ECDSA_PSA */
584584

585585
/*
586586
* Load encryption key.

boot/bootutil/src/encrypted_psa.c

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,18 @@ BOOT_LOG_MODULE_DECLARE(mcuboot_psa_enc);
3333
#define PSA_HMAC_HKDF_SHA PSA_ALG_SHA_256
3434
#endif
3535

36+
#if defined(MCUBOOT_ENCRYPT_EC256)
37+
#define NUM_ECC_BYTES (256 / 8)
38+
static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED;
39+
static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1;
40+
#define ECC_FAMILY PSA_ECC_FAMILY_SECP_R1
41+
#endif /* defined(MCUBOOT_ENCRYPT_EC256) */
42+
#if defined(MCUBOOT_ENCRYPT_X25519)
3643
#define X25519_OID "\x6e"
3744
static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG \
3845
MBEDTLS_OID_ORG_GOV X25519_OID;
46+
#define ECC_FAMILY PSA_ECC_FAMILY_MONTGOMERY
47+
#endif /* defined(MCUBOOT_ENCRYPT_X25519) */
3948

4049
/* Partitioning of HKDF derived material, from the exchange derived key */
4150
/* AES key encryption key */
@@ -51,9 +60,87 @@ static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_ISO_IDENTIFIED_ORG \
5160
/* Total size */
5261
#define HKDF_SIZE (HKDF_AES_KEY_SIZE + HKDF_MAC_FEED_SIZE)
5362

63+
#if defined(MCUBOOT_ENCRYPT_EC256)
64+
/* Fixme: This duplicates code from encrypted.c and depends on mbedtls */
65+
66+
/*
67+
* Parses the output of `imgtool keygen`, which produces a PKCS#8 elliptic
68+
* curve keypair. See RFC5208 and RFC5915.
69+
*/
70+
static int
71+
parse_priv_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key)
72+
{
73+
int rc;
74+
size_t len;
75+
int version;
76+
mbedtls_asn1_buf alg;
77+
mbedtls_asn1_buf param;
78+
79+
if ((rc = mbedtls_asn1_get_tag(p, end, &len,
80+
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
81+
return -1;
82+
}
83+
84+
if (*p + len != end) {
85+
return -2;
86+
}
87+
88+
version = 0;
89+
if (mbedtls_asn1_get_int(p, end, &version) || version != 0) {
90+
return -3;
91+
}
92+
93+
if ((rc = mbedtls_asn1_get_alg(p, end, &alg, &param)) != 0) {
94+
return -5;
95+
}
96+
97+
if (alg.ASN1_CONTEXT_MEMBER(len) != sizeof(ec_pubkey_oid) - 1 ||
98+
memcmp(alg.ASN1_CONTEXT_MEMBER(p), ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) {
99+
return -6;
100+
}
101+
if (param.ASN1_CONTEXT_MEMBER(len) != sizeof(ec_secp256r1_oid) - 1 ||
102+
memcmp(param.ASN1_CONTEXT_MEMBER(p), ec_secp256r1_oid, sizeof(ec_secp256r1_oid) - 1)) {
103+
return -7;
104+
}
105+
106+
if ((rc = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
107+
return -8;
108+
}
109+
110+
/* RFC5915 - ECPrivateKey */
111+
112+
if ((rc = mbedtls_asn1_get_tag(p, end, &len,
113+
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
114+
return -9;
115+
}
116+
117+
version = 0;
118+
if (mbedtls_asn1_get_int(p, end, &version) || version != 1) {
119+
return -10;
120+
}
121+
122+
/* privateKey */
123+
124+
if ((rc = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) {
125+
return -11;
126+
}
127+
128+
if (len != NUM_ECC_BYTES) {
129+
return -12;
130+
}
131+
132+
memcpy(private_key, *p, len);
133+
134+
/* publicKey usually follows but is not parsed here */
135+
136+
return 0;
137+
}
138+
#endif /* defined(MCUBOOT_ENCRYPT_EC256) */
139+
140+
#if defined(MCUBOOT_ENCRYPT_X25519)
54141
/* Fixme: This duplicates code from encrypted.c and depends on mbedtls */
55142
static int
56-
parse_x25519_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key)
143+
parse_priv_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key)
57144
{
58145
size_t len;
59146
int version;
@@ -98,6 +185,7 @@ parse_x25519_enckey(uint8_t **p, uint8_t *end, uint8_t *private_key)
98185
memcpy(private_key, *p, EC_PRIVK_LEN);
99186
return 0;
100187
}
188+
#endif /* defined(MCUBOOT_ENCRYPT_X25519) */
101189

102190
void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx)
103191
{
@@ -153,14 +241,14 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
153241
}
154242

155243
/*
156-
* Load the stored X25519 decryption private key
244+
* * Load the stored decryption private key
157245
*/
158-
rc = parse_x25519_enckey(&cp, cpend, private_key);
246+
rc = parse_priv_enckey(&cp, cpend, private_key);
159247
if (rc) {
160248
return rc;
161249
}
162250

163-
psa_set_key_type(&kattr, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_MONTGOMERY));
251+
psa_set_key_type(&kattr, PSA_KEY_TYPE_ECC_KEY_PAIR(ECC_FAMILY));
164252
psa_set_key_usage_flags(&kattr, PSA_KEY_USAGE_DERIVE);
165253
psa_set_key_algorithm(&kattr, PSA_ALG_ECDH);
166254

boot/zephyr/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ zephyr_library_sources(
123123
${BOOT_DIR}/bootutil/src/fault_injection_hardening.c
124124
)
125125

126-
if(DEFINED CONFIG_BOOT_ENCRYPT_X25519 AND DEFINED CONFIG_BOOT_ED25519_PSA)
126+
if((DEFINED CONFIG_BOOT_ENCRYPT_X25519 AND DEFINED CONFIG_BOOT_ED25519_PSA)
127+
OR (CONFIG_BOOT_ENCRYPT_EC256 AND DEFINED CONFIG_BOOT_ECDSA_PSA))
127128
zephyr_library_sources(${BOOT_DIR}/bootutil/src/encrypted_psa.c)
128129
endif()
129130

@@ -321,7 +322,7 @@ elseif(CONFIG_BOOT_SIGNATURE_TYPE_ED25519 OR CONFIG_BOOT_ENCRYPT_X25519)
321322
endif()
322323
endif()
323324

324-
if(NOT CONFIG_BOOT_ED25519_PSA)
325+
if(NOT CONFIG_BOOT_ED25519_PSA AND NOT CONFIG_BOOT_ECDSA_PSA)
325326
if(CONFIG_BOOT_ENCRYPT_EC256 OR CONFIG_BOOT_ENCRYPT_X25519)
326327
zephyr_library_sources(
327328
${TINYCRYPT_DIR}/source/aes_encrypt.c
@@ -333,7 +334,7 @@ if(NOT CONFIG_BOOT_ED25519_PSA)
333334
endif()
334335
endif()
335336

336-
if(CONFIG_BOOT_ENCRYPT_EC256)
337+
if(CONFIG_BOOT_ENCRYPT_EC256 AND NOT CONFIG_BOOT_ECDSA_PSA)
337338
zephyr_library_sources(
338339
${TINYCRYPT_DIR}/source/ecc_dh.c
339340
)

0 commit comments

Comments
 (0)