Skip to content

Commit 5899fac

Browse files
Roland Mikheldavidvincze
Roland Mikhel
authored andcommitted
sim: PSA Crypto ECDSA enablement
This commit enables ECDSA signature verification using PSA Crypto API. Signed-off-by: Roland Mikhel <roland.mikhel@arm.com> Change-Id: I33f559ecdd59b1ce41c6a2d5f315212300d585e3
1 parent 274547c commit 5899fac

11 files changed

+151
-17
lines changed

root-ec-p384-pkcs8.pem

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIG2AgEAMBAGByqGSM49AgEGBSuBBAAiBIGeMIGbAgEBBDC8ZQWjooCCaLQJ9DJN
3+
KMyPoUoFcqGluXGu13Zf526RX6TdRhnkExtL1T7fC13n32ChZANiAAQMdsqucjql
4+
6PDU8Ra1Au93oRuTYXjACSZ7O0Cc7kmF4MlP5/K6l2zzgmUULPUMczNNMueb00LM
5+
lVrl4vX0bkXg7SA1XK9SNYHU3JzjniI++z8iENpwAzetqPJI/jpgaaU=
6+
-----END PRIVATE KEY-----

root-ec-p384.pem

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
MIGkAgEBBDC8ZQWjooCCaLQJ9DJNKMyPoUoFcqGluXGu13Zf526RX6TdRhnkExtL
3+
1T7fC13n32CgBwYFK4EEACKhZANiAAQMdsqucjql6PDU8Ra1Au93oRuTYXjACSZ7
4+
O0Cc7kmF4MlP5/K6l2zzgmUULPUMczNNMueb00LMlVrl4vX0bkXg7SA1XK9SNYHU
5+
3JzjniI++z8iENpwAzetqPJI/jpgaaU=
6+
-----END EC PRIVATE KEY-----

sim/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ sig-rsa = ["mcuboot-sys/sig-rsa"]
1111
sig-rsa3072 = ["mcuboot-sys/sig-rsa3072"]
1212
sig-ecdsa = ["mcuboot-sys/sig-ecdsa"]
1313
sig-ecdsa-mbedtls = ["mcuboot-sys/sig-ecdsa-mbedtls"]
14+
sig-ecdsa-psa = ["mcuboot-sys/sig-ecdsa-psa", "mcuboot-sys/psa-crypto-api"]
15+
sig-p384 = ["mcuboot-sys/sig-p384"]
1416
sig-ed25519 = ["mcuboot-sys/sig-ed25519"]
1517
overwrite-only = ["mcuboot-sys/overwrite-only"]
1618
swap-move = ["mcuboot-sys/swap-move"]
@@ -31,7 +33,6 @@ direct-xip = ["mcuboot-sys/direct-xip"]
3133
downgrade-prevention = ["mcuboot-sys/downgrade-prevention"]
3234
max-align-32 = ["mcuboot-sys/max-align-32"]
3335
hw-rollback-protection = ["mcuboot-sys/hw-rollback-protection"]
34-
psa-crypto-api = ["mcuboot-sys/psa-crypto-api"]
3536

3637
[dependencies]
3738
byteorder = "1.4"

sim/mcuboot-sys/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ sig-ecdsa = []
2424
# Verify ECDSA (secp256r1) signatures using mbed TLS
2525
sig-ecdsa-mbedtls = []
2626

27+
# Verify ECDSA (p256 or p384) signatures using PSA Crypto API
28+
sig-ecdsa-psa = []
29+
30+
# Enable P384 Curve support (instead of P256) for PSA Crypto
31+
sig-p384 = []
32+
2733
# Verify ED25519 signatures.
2834
sig-ed25519 = []
2935

sim/mcuboot-sys/build.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ fn main() {
1515
let sig_rsa3072 = env::var("CARGO_FEATURE_SIG_RSA3072").is_ok();
1616
let sig_ecdsa = env::var("CARGO_FEATURE_SIG_ECDSA").is_ok();
1717
let sig_ecdsa_mbedtls = env::var("CARGO_FEATURE_SIG_ECDSA_MBEDTLS").is_ok();
18+
let sig_ecdsa_psa = env::var("CARGO_FEATURE_SIG_ECDSA_PSA").is_ok();
19+
let sig_p384 = env::var("CARGO_FEATURE_SIG_P384").is_ok();
1820
let sig_ed25519 = env::var("CARGO_FEATURE_SIG_ED25519").is_ok();
1921
let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok();
2022
let swap_move = env::var("CARGO_FEATURE_SWAP_MOVE").is_ok();
@@ -205,6 +207,24 @@ fn main() {
205207
conf.file("../../ext/mbedtls/library/ecp_curves.c");
206208
conf.file("../../ext/mbedtls/library/platform.c");
207209
conf.file("../../ext/mbedtls/library/platform_util.c");
210+
} else if sig_ecdsa_psa {
211+
conf.conf.include("../../ext/mbedtls/include");
212+
213+
if sig_p384 {
214+
conf.conf.define("MCUBOOT_SIGN_EC384", None);
215+
conf.file("../../ext/mbedtls/library/sha512.c");
216+
} else {
217+
conf.conf.define("MCUBOOT_SIGN_EC256", None);
218+
conf.file("../../ext/mbedtls/library/sha256.c");
219+
}
220+
221+
conf.file("csupport/keys.c");
222+
conf.file("../../ext/mbedtls/library/asn1parse.c");
223+
conf.file("../../ext/mbedtls/library/bignum.c");
224+
conf.file("../../ext/mbedtls/library/ecp.c");
225+
conf.file("../../ext/mbedtls/library/ecp_curves.c");
226+
conf.file("../../ext/mbedtls/library/platform.c");
227+
conf.file("../../ext/mbedtls/library/platform_util.c");
208228
} else if sig_ed25519 {
209229
conf.conf.define("MCUBOOT_SIGN_ED25519", None);
210230
conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
@@ -421,17 +441,19 @@ fn main() {
421441
conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
422442
} else if enc_aes256_x25519 {
423443
conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
444+
} else if sig_ecdsa_psa {
445+
conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ec-psa.h>"));
424446
}
425447

426448
conf.file("../../boot/bootutil/src/image_validate.c");
427449
if sig_rsa || sig_rsa3072 {
428450
conf.file("../../boot/bootutil/src/image_rsa.c");
429-
} else if sig_ecdsa || sig_ecdsa_mbedtls {
430-
conf.conf.include("../../ext/mbedtls/include");
451+
} else if sig_ecdsa || sig_ecdsa_mbedtls || sig_ecdsa_psa {
431452
conf.file("../../boot/bootutil/src/image_ecdsa.c");
432453
} else if sig_ed25519 {
433454
conf.file("../../boot/bootutil/src/image_ed25519.c");
434455
}
456+
435457
conf.file("../../boot/bootutil/src/loader.c");
436458
conf.file("../../boot/bootutil/src/swap_misc.c");
437459
conf.file("../../boot/bootutil/src/swap_scratch.c");
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* Copyright (c) 2023 Arm Limited
5+
*/
6+
7+
#ifndef MCUBOOT_PSA_CRYPTO_CONFIG_ECDSA
8+
#define MCUBOOT_PSA_CRYPTO_CONFIG_ECDSA
9+
10+
#if defined(MCUBOOT_USE_PSA_CRYPTO)
11+
#include "config-add-psa-crypto.h"
12+
#endif
13+
14+
#define MBEDTLS_ECP_C
15+
#define MBEDTLS_ECP_NIST_OPTIM
16+
#define MBEDTLS_ECDSA_C
17+
18+
/* mbed TLS modules */
19+
#define MBEDTLS_ASN1_PARSE_C
20+
#define MBEDTLS_ASN1_WRITE_C
21+
#define MBEDTLS_AES_C
22+
#define MBEDTLS_BIGNUM_C
23+
#define MBEDTLS_MD_C
24+
#define MBEDTLS_OID_C
25+
#if defined(MCUBOOT_SIGN_EC384)
26+
#define MBEDTLS_SHA384_C
27+
#define MBEDTLS_SHA512_C
28+
#define MBEDTLS_ECP_DP_SECP384R1_ENABLED
29+
#else
30+
#define MBEDTLS_SHA256_C
31+
#define MBEDTLS_SHA224_C
32+
#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
33+
#endif /* MCUBOOT_SIGN_EC384 */
34+
35+
#include "mbedtls/check_config.h"
36+
37+
#endif /* MCUBOOT_PSA_CRYPTO_CONFIG_ECDSA */

sim/mcuboot-sys/csupport/keys.c

+23-1
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ const unsigned char root_pub_der[] = {
106106
};
107107
const unsigned int root_pub_der_len = 398;
108108
#endif
109-
#elif defined(MCUBOOT_SIGN_EC256)
109+
#elif defined(MCUBOOT_SIGN_EC256) || \
110+
defined(MCUBOOT_SIGN_EC384)
110111
#define HAVE_KEYS
112+
#ifndef MCUBOOT_SIGN_EC384
111113
const unsigned char root_pub_der[] = {
112114
0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
113115
0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
@@ -122,6 +124,26 @@ const unsigned char root_pub_der[] = {
122124
0x8b, 0x68, 0x34, 0xcc, 0x3a, 0x6a, 0xfc, 0x53,
123125
0x8e, 0xfa, 0xc1, };
124126
const unsigned int root_pub_der_len = 91;
127+
#else /* MCUBOOT_SIGN_EC384 */
128+
const unsigned char root_pub_der[] = {
129+
0x30, 0x76, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86,
130+
0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b,
131+
0x81, 0x04, 0x00, 0x22, 0x03, 0x62, 0x00, 0x04,
132+
0x0c, 0x76, 0xca, 0xae, 0x72, 0x3a, 0xa5, 0xe8,
133+
0xf0, 0xd4, 0xf1, 0x16, 0xb5, 0x02, 0xef, 0x77,
134+
0xa1, 0x1b, 0x93, 0x61, 0x78, 0xc0, 0x09, 0x26,
135+
0x7b, 0x3b, 0x40, 0x9c, 0xee, 0x49, 0x85, 0xe0,
136+
0xc9, 0x4f, 0xe7, 0xf2, 0xba, 0x97, 0x6c, 0xf3,
137+
0x82, 0x65, 0x14, 0x2c, 0xf5, 0x0c, 0x73, 0x33,
138+
0x4d, 0x32, 0xe7, 0x9b, 0xd3, 0x42, 0xcc, 0x95,
139+
0x5a, 0xe5, 0xe2, 0xf5, 0xf4, 0x6e, 0x45, 0xe0,
140+
0xed, 0x20, 0x35, 0x5c, 0xaf, 0x52, 0x35, 0x81,
141+
0xd4, 0xdc, 0x9c, 0xe3, 0x9e, 0x22, 0x3e, 0xfb,
142+
0x3f, 0x22, 0x10, 0xda, 0x70, 0x03, 0x37, 0xad,
143+
0xa8, 0xf2, 0x48, 0xfe, 0x3a, 0x60, 0x69, 0xa5,
144+
};
145+
const unsigned int root_pub_der_len = 120;
146+
#endif /* MCUBOOT_SIGN_EC384 */
125147
#elif defined(MCUBOOT_SIGN_ED25519)
126148
#define HAVE_KEYS
127149
const unsigned char root_pub_der[] = {

sim/src/caps.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub enum Caps {
2929
RamLoad = (1 << 16),
3030
DirectXip = (1 << 17),
3131
HwRollbackProtection = (1 << 18),
32+
EcdsaP384 = (1 << 19),
3233
}
3334

3435
impl Caps {
@@ -39,7 +40,7 @@ impl Caps {
3940

4041
/// Does this build have ECDSA of some type enabled for signatures.
4142
pub fn has_ecdsa() -> bool {
42-
Caps::EcdsaP256.present()
43+
Caps::EcdsaP256.present() || Caps::EcdsaP384.present()
4344
}
4445

4546
/// Query for the number of images that have been configured into this

sim/src/ecdsa_pub_key-rs.txt

+18
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,21 @@ static ECDSA256_PUB_KEY: &[u8] = &[
1212
0x8b, 0x68, 0x34, 0xcc, 0x3a, 0x6a, 0xfc, 0x53,
1313
0x8e, 0xfa, 0xc1,
1414
];
15+
16+
static ECDSAP384_PUB_KEY: &[u8] = &[
17+
0x30, 0x76, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86,
18+
0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b,
19+
0x81, 0x04, 0x00, 0x22, 0x03, 0x62, 0x00, 0x04,
20+
0x0c, 0x76, 0xca, 0xae, 0x72, 0x3a, 0xa5, 0xe8,
21+
0xf0, 0xd4, 0xf1, 0x16, 0xb5, 0x02, 0xef, 0x77,
22+
0xa1, 0x1b, 0x93, 0x61, 0x78, 0xc0, 0x09, 0x26,
23+
0x7b, 0x3b, 0x40, 0x9c, 0xee, 0x49, 0x85, 0xe0,
24+
0xc9, 0x4f, 0xe7, 0xf2, 0xba, 0x97, 0x6c, 0xf3,
25+
0x82, 0x65, 0x14, 0x2c, 0xf5, 0x0c, 0x73, 0x33,
26+
0x4d, 0x32, 0xe7, 0x9b, 0xd3, 0x42, 0xcc, 0x95,
27+
0x5a, 0xe5, 0xe2, 0xf5, 0xf4, 0x6e, 0x45, 0xe0,
28+
0xed, 0x20, 0x35, 0x5c, 0xaf, 0x52, 0x35, 0x81,
29+
0xd4, 0xdc, 0x9c, 0xe3, 0x9e, 0x22, 0x3e, 0xfb,
30+
0x3f, 0x22, 0x10, 0xda, 0x70, 0x03, 0x37, 0xad,
31+
0xa8, 0xf2, 0x48, 0xfe, 0x3a, 0x60, 0x69, 0xa5,
32+
];

sim/src/image.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1999,7 +1999,7 @@ fn make_tlv() -> TlvGen {
19991999
TlvGen::new_rsa_pss()
20002000
} else if Caps::RSA3072.present() {
20012001
TlvGen::new_rsa3072_pss()
2002-
} else if Caps::EcdsaP256.present() {
2002+
} else if Caps::EcdsaP256.present() || Caps::EcdsaP384.present() {
20032003
TlvGen::new_ecdsa()
20042004
} else if Caps::Ed25519.present() {
20052005
TlvGen::new_ed25519()

sim/src/tlv.rs

+26-11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use ring::signature::{
2929
EcdsaKeyPair,
3030
ECDSA_P256_SHA256_ASN1_SIGNING,
3131
Ed25519KeyPair,
32+
ECDSA_P384_SHA384_ASN1_SIGNING,
3233
};
3334
use aes::{
3435
Aes128,
@@ -385,12 +386,17 @@ impl ManifestGen for TlvGen {
385386
estimate += 4 + 64; // ED25519 signature.
386387
}
387388
if self.kinds.contains(&TlvKinds::ECDSASIG) {
388-
estimate += 4 + 32; // keyhash
389-
390-
// ECDSA signatures are encoded as ASN.1 with the x and y values stored as signed
391-
// integers. As such, the size can vary by 2 bytes, if the 256-bit value has the high
392-
// bit, it takes an extra 0 byte to avoid it being seen as a negative number.
393-
estimate += 4 + 72; // ECDSA256 (varies)
389+
// ECDSA signatures are encoded as ASN.1 with the x and y values
390+
// stored as signed integers. As such, the size can vary by 2 bytes,
391+
// if for example the 256-bit value has the high bit, it takes an
392+
// extra 0 byte to avoid it being seen as a negative number.
393+
if cfg!(feature = "use-p384-curve") {
394+
estimate += 4 + 48; // keyhash
395+
estimate += 4 + 104; // ECDSA384 (varies)
396+
} else {
397+
estimate += 4 + 32; // keyhash
398+
estimate += 4 + 72; // ECDSA256 (varies)
399+
}
394400
}
395401

396402
// Estimate encryption.
@@ -559,11 +565,19 @@ impl ManifestGen for TlvGen {
559565

560566
if self.kinds.contains(&TlvKinds::ECDSASIG) {
561567
let rng = rand::SystemRandom::new();
562-
let keyhash = digest::digest(&digest::SHA256, ECDSA256_PUB_KEY);
563-
let key_bytes = pem::parse(include_bytes!("../../root-ec-p256-pkcs8.pem").as_ref()).unwrap();
564-
let sign_algo = &ECDSA_P256_SHA256_ASN1_SIGNING;
565-
let key_pair = EcdsaKeyPair::from_pkcs8(sign_algo, &key_bytes.contents).unwrap();
566-
let signature = key_pair.sign(&rng,&sig_payload).unwrap();
568+
let (signature, keyhash) = if cfg!(feature = "use-p384-curve") {
569+
let keyhash = digest::digest(&digest::SHA384, ECDSAP384_PUB_KEY);
570+
let key_bytes = pem::parse(include_bytes!("../../root-ec-p384-pkcs8.pem").as_ref()).unwrap();
571+
let sign_algo = &ECDSA_P384_SHA384_ASN1_SIGNING;
572+
let key_pair = EcdsaKeyPair::from_pkcs8(sign_algo, &key_bytes.contents).unwrap();
573+
(key_pair.sign(&rng, &sig_payload).unwrap(), keyhash)
574+
} else {
575+
let keyhash = digest::digest(&digest::SHA256, ECDSA256_PUB_KEY);
576+
let key_bytes = pem::parse(include_bytes!("../../root-ec-p256-pkcs8.pem").as_ref()).unwrap();
577+
let sign_algo = &ECDSA_P256_SHA256_ASN1_SIGNING;
578+
let key_pair = EcdsaKeyPair::from_pkcs8(sign_algo, &key_bytes.contents).unwrap();
579+
(key_pair.sign(&rng, &sig_payload).unwrap(), keyhash)
580+
};
567581

568582
// Write public key
569583
let keyhash_slice = keyhash.as_ref();
@@ -578,6 +592,7 @@ impl ManifestGen for TlvGen {
578592
result.write_u16::<LittleEndian>(signature.len() as u16).unwrap();
579593
result.extend_from_slice(&signature);
580594
}
595+
581596
if self.kinds.contains(&TlvKinds::ED25519) {
582597
let keyhash = digest::digest(&digest::SHA256, ED25519_PUB_KEY);
583598
let keyhash = keyhash.as_ref();

0 commit comments

Comments
 (0)