@@ -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"
3744static 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 */
55142static 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
102190void 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
0 commit comments