add software ecdsa support
Subsystems (2)
v7 cover add software ecdsa support
This series adds the support of ecdsa with software using mbedtls. So boards without ecdsa hardware may also use signature with ecdsa.
To add the support of ecdsa with mbedtls, I have:
- enabled ecdsa in mbedtls
- add a function sw_ecdsa_verify that uses mbedtls
- add a driver sw_ecdsa that call sw_ecdsa_verify
I have tested this code with sandbox, and I have followed those steps:
0) build u-boot using sandbox_defconfig and adding those options:
CONFIG_ECDSA_MBEDTLS=yCONFIG_ECDSA=yCONFIG_ECDSA_VERIFY=y
1) add a signature node to an its file
signature-256 {
algo = "sha256,ecdsa256";
key-name-hint = "private-key-256";
};2) generate an ecdsa key
openssl ecparam -name prime256v1 -genkey -noout -out private-key-256.pem
3) create the itb file./tools/mkimage -f <file.its> -k . -K arch/sandbox/dts/test.dtb <file.itb>
4) launch sandbox u-boot
./u-boot -d arch/sandbox/dts/test.dtb
5) on sandbox u-boot prompt, load the itb and launch bootm on it
=> host load hostfs - 1000000 uboot-ecdsa.itb
4628674 bytes read in 1 ms (4.3 GiB/s)
=> bootm 1000000
...
...
Verifying Hash Integrity ... sha256,ecdsa256:private-key-256+ OK
Changes in v2:
- move ECDSA_MBEDTLS to MBEDTLS_LIB_X509
- rename lib/mbedtls/sw_ecdsa.c to lib/mbedtls/ecdsa.c
- enhance dependencies for ECDSA_MBEDTLS
- fix support of ecdsa521/secp521r1
- add vboot test using ecdsa
Changes in v3:
- do not use _MBEDTLS in mbedtls_def_config.h
- check returns and remove mem leak in lib/mbedtls/ecdsa.c
- remove useless field *k in struct ecdsa_test_vector_s
- check returns in test/lib/ecdsa.c
- fix third parameter when calling sha*_csum_wd()
- add support of ecdsa in pre-load header
Changes in v4:
- change some dependencies to enable ecc
- use ECDSA_MBEDTLS to build the ecdsa driver
- support ecdsa521 and secp521r1
- add a test calling ecdsa_verify for the ecdsa class
- merge patch 10 and 11 (support ecdsa pre-load for binman and tests)
- several code cleanup
Change in v5:
- fix ecdsa 521 in the first patch (instead of the 5th patch)
- mbedtls: ecdsa is only compiled when ECDSA_MBEDTLS is enabled
- fit_image_setup_sig: required_keynode is initiazed to -1 instead of 0
- avoid hardcoded value for ecdsa sig and point size
- check pointer before using them
- free ecdsa keys when key are not found/used
Change in v6:
- fix dependencies for ecdsa
- ecdsa is no more mandatory for tls
- remove include u-boot/ecdsa.h in ecdsa-sw.c
- do not use fdt_stderror with -1
-
Change in v7:
- change uint8_t to u8 in test/dm/ecdsa.c
- change info->required_keynode >= 0 in lib/ecdsa/ecdsa-verify.c
- set required_keynode to -1 in test/dm/ecdsa.c
Philippe Reynes (15):
ecdsa: fix support of secp521r1
mbedtls: enable support of ecc
ecdsa: initial support of ecdsa using mbedtls
test: lib: ecdsa: add initial test
drivers: crypto: add software ecdsa support
test: dm: ecdsa.c: clean this test as software ecdsa is now
implemented
test: py: vboot: prepare integration test for ecdsa
test: vboot: add test for ecdsa
tools: fit_image_setup_sig: set required_keynode to -1
tools: mkimage: pre-load: add support of ecdsa
tools: binman: pre-load: add support of ecdsa
boot: pre-load: add support of ecdsa
tools: preload_check_sign: add support of ecdsa
test: py: vboot: prepare test for global signature with ecdsa
test: py: vboot: add test for global signature with ecdsa
boot/image-pre-load.c | 53 +-
configs/sandbox_defconfig | 2 +
drivers/crypto/Makefile | 1 +
drivers/crypto/ecdsa/Makefile | 6 +
drivers/crypto/ecdsa/ecdsa-sw.c | 32 ++
include/crypto/ecdsa-uclass.h | 15 +-
include/crypto/internal/ecdsa.h | 39 ++
lib/ecdsa/Kconfig | 1 +
lib/ecdsa/ecdsa-libcrypto.c | 94 +++-
lib/ecdsa/ecdsa-verify.c | 67 ++-
lib/fdt-libcrypto.c | 2 +-
lib/mbedtls/Kconfig | 24 +
lib/mbedtls/Makefile | 20 +-
lib/mbedtls/ecdsa.c | 146 ++++++
lib/mbedtls/mbedtls_def_config.h | 41 +-
test/dm/ecdsa.c | 109 ++++-
test/lib/Makefile | 1 +
test/lib/ecdsa.c | 456 ++++++++++++++++++
test/py/tests/test_fit_ecdsa.py | 2 +-
test/py/tests/test_vboot.py | 143 +++---
.../tests/vboot/sandbox-binman-ecdsa256.dts | 24 +
.../tests/vboot/sandbox-binman-ecdsa384.dts | 24 +
.../tests/vboot/sandbox-binman-ecdsa521.dts | 24 +
...pss.dts => sandbox-binman-rsa2048-pss.dts} | 0
...-binman.dts => sandbox-binman-rsa2048.dts} | 0
.../vboot/sandbox-u-boot-global-ecdsa256.dts | 27 ++
.../vboot/sandbox-u-boot-global-ecdsa384.dts | 27 ++
.../vboot/sandbox-u-boot-global-ecdsa521.dts | 27 ++
... => sandbox-u-boot-global-rsa2048-pss.dts} | 0
....dts => sandbox-u-boot-global-rsa2048.dts} | 0
....its => sign-configs-sha1-rsa2048-pss.its} | 0
...sha1.its => sign-configs-sha1-rsa2048.its} | 0
.../vboot/sign-configs-sha256-ecdsa256.its | 45 ++
.../vboot/sign-configs-sha256-ecdsa384.its | 45 ++
.../vboot/sign-configs-sha256-ecdsa521.its | 45 ++
... sign-configs-sha256-rsa2048-pss-prod.its} | 0
...ts => sign-configs-sha256-rsa2048-pss.its} | 0
...56.its => sign-configs-sha256-rsa2048.its} | 0
...84.its => sign-configs-sha384-rsa3072.its} | 0
...s.its => sign-images-sha1-rsa2048-pss.its} | 0
...-sha1.its => sign-images-sha1-rsa2048.its} | 0
.../vboot/sign-images-sha256-ecdsa256.its | 42 ++
.../vboot/sign-images-sha256-ecdsa384.its | 42 ++
.../vboot/sign-images-sha256-ecdsa521.its | 42 ++
...its => sign-images-sha256-rsa2048-pss.its} | 0
...256.its => sign-images-sha256-rsa2048.its} | 0
...384.its => sign-images-sha384-rsa3072.its} | 0
tools/binman/etype/pre_load.py | 78 ++-
tools/binman/ftest.py | 50 ++
tools/binman/test/ecdsa521.pem | 7 +
tools/binman/test/security/pre_load_ecdsa.dts | 22 +
.../security/pre_load_ecdsa_invalid_algo.dts | 22 +
.../security/pre_load_ecdsa_invalid_key.dts | 22 +
.../security/pre_load_ecdsa_invalid_sha.dts | 22 +
tools/image-host.c | 93 +++-
tools/image-sig-host.c | 7 +
tools/preload_check_sign.c | 30 ++
57 files changed, 1873 insertions(+), 148 deletions(-)
create mode 100644 drivers/crypto/ecdsa/Makefile
create mode 100644 drivers/crypto/ecdsa/ecdsa-sw.c
create mode 100644 include/crypto/internal/ecdsa.h
create mode 100644 lib/mbedtls/ecdsa.c
create mode 100644 test/lib/ecdsa.c
create mode 100644 test/py/tests/vboot/sandbox-binman-ecdsa256.dts
create mode 100644 test/py/tests/vboot/sandbox-binman-ecdsa384.dts
create mode 100644 test/py/tests/vboot/sandbox-binman-ecdsa521.dts
rename test/py/tests/vboot/{sandbox-binman-pss.dts => sandbox-binman-rsa2048-pss.dts} (100%)
rename test/py/tests/vboot/{sandbox-binman.dts => sandbox-binman-rsa2048.dts} (100%)
create mode 100644 test/py/tests/vboot/sandbox-u-boot-global-ecdsa256.dts
create mode 100644 test/py/tests/vboot/sandbox-u-boot-global-ecdsa384.dts
create mode 100644 test/py/tests/vboot/sandbox-u-boot-global-ecdsa521.dts
rename test/py/tests/vboot/{sandbox-u-boot-global-pss.dts => sandbox-u-boot-global-rsa2048-pss.dts} (100%)
rename test/py/tests/vboot/{sandbox-u-boot-global.dts => sandbox-u-boot-global-rsa2048.dts} (100%)
rename test/py/tests/vboot/{sign-configs-sha1-pss.its => sign-configs-sha1-rsa2048-pss.its} (100%)
rename test/py/tests/vboot/{sign-configs-sha1.its => sign-configs-sha1-rsa2048.its} (100%)
create mode 100644 test/py/tests/vboot/sign-configs-sha256-ecdsa256.its
create mode 100644 test/py/tests/vboot/sign-configs-sha256-ecdsa384.its
create mode 100644 test/py/tests/vboot/sign-configs-sha256-ecdsa521.its
rename test/py/tests/vboot/{sign-configs-sha256-pss-prod.its => sign-configs-sha256-rsa2048-pss-prod.its} (100%)
rename test/py/tests/vboot/{sign-configs-sha256-pss.its => sign-configs-sha256-rsa2048-pss.its} (100%)
rename test/py/tests/vboot/{sign-configs-sha256.its => sign-configs-sha256-rsa2048.its} (100%)
rename test/py/tests/vboot/{sign-configs-sha384.its => sign-configs-sha384-rsa3072.its} (100%)
rename test/py/tests/vboot/{sign-images-sha1-pss.its => sign-images-sha1-rsa2048-pss.its} (100%)
rename test/py/tests/vboot/{sign-images-sha1.its => sign-images-sha1-rsa2048.its} (100%)
create mode 100644 test/py/tests/vboot/sign-images-sha256-ecdsa256.its
create mode 100644 test/py/tests/vboot/sign-images-sha256-ecdsa384.its
create mode 100644 test/py/tests/vboot/sign-images-sha256-ecdsa521.its
rename test/py/tests/vboot/{sign-images-sha256-pss.its => sign-images-sha256-rsa2048-pss.its} (100%)
rename test/py/tests/vboot/{sign-images-sha256.its => sign-images-sha256-rsa2048.its} (100%)
rename test/py/tests/vboot/{sign-images-sha384.its => sign-images-sha384-rsa3072.its} (100%)
create mode 100644 tools/binman/test/ecdsa521.pem
create mode 100644 tools/binman/test/security/pre_load_ecdsa.dts
create mode 100644 tools/binman/test/security/pre_load_ecdsa_invalid_algo.dts
create mode 100644 tools/binman/test/security/pre_load_ecdsa_invalid_key.dts
create mode 100644 tools/binman/test/security/pre_load_ecdsa_invalid_sha.dtsSignature · 1 line
2.43.0
PATCH v7 1/15 ecdsa: fix support of secp521r1
Current implementation of ecdsa only supports key len aligned on 8 bits. But the curve secp521r1 uses a key of 521 bits which is not aligned on 8 bits. In this commit, we update the keys management for ecdsa to support keys that are not aligned on 8 bits.
---
v2:
- intitial version
v3:
- fix typo in comments
v4:
- fix commit message
- clean code with DIV_ROUND_UP-
- duplicate data before shifting
- support ecdsa521 and secp521r1
- clean code
v5:
- check that x and y are not null before using them
- free keys when keys not found
-v6:
- free x and y if x and y are not null
- do not free key when an error has occured v7:
- no change
lib/ecdsa/ecdsa-libcrypto.c | 65 +++++++++++++++++++++++++++++++++++-- lib/ecdsa/ecdsa-verify.c | 65 ++++++++++++++++++++++++++++++++++--- lib/fdt-libcrypto.c | 2 +- tools/image-sig-host.c | 7 ++++ 4 files changed, 132 insertions(+), 7 deletions(-)
lib/ecdsa/ecdsa-libcrypto.c +63 -2
@@ -26,6 +26,8 @@#include <openssl/ec.h> #include <openssl/bn.h> +#define DIV_ROUND_UP (n, d) (((n) + (d) - 1) / (d)) +/* Image signing context for openssl-libcrypto */ struct signer {EVP_PKEY *evp_key;/* Pointer to EVP_PKEY object */
@@ -41,10 +43,26 @@ struct ecdsa_public_key {int size_bits; }; +static char *memdup (char *buf,size_t size) +{ +char *dup; + + dup =malloc (size); +if (dup) +memcpy (dup, buf, size); + +return dup; +} +static int fdt_get_key (struct ecdsa_public_key *key,const void *fdt,int node) { +const char *x; +const char *y;int x_len;int y_len; +int expected_len; + +memset (key,0 ,sizeof (*key)); key->curve_name =fdt_getprop (fdt, node,"ecdsa,curve" ,NULL );if (!key->curve_name)
@@ -54,6 +72,8 @@ static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node) key->size_bits =256 ; else if (!strcmp(key->curve_name, "secp384r1")) key->size_bits =384 ; + else if (!strcmp(key->curve_name, "secp521r1")) + key->size_bits =521 ; else return -EINVAL;
@@ -63,12 +83,45 @@ static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node)if (!key->x || !key->y)return -EINVAL ; -if (x_len != key->size_bits /8 || y_len != key->size_bits /8 ) +/* +* The public key is stored as an array of u32, so if the key size is +* not a multiple of 32 (for example 521), we may have extra bytes. +* To avoid any issue later, we shift the x and y pointer to the first +* useful byte. +*/ + expected_len =DIV_ROUND_UP (key->size_bits,8 ); + +if (x_len < expected_len || y_len < expected_len)return -EINVAL ; + x =memdup ((char *)key->x + (x_len - expected_len), expected_len); +if (!x) { +fprintf (stderr,"Cannot allocate memory for point X" ); +return -ENOMEM ; + } + key->x = (const uint8_t *)x; + + y =memdup ((char *)key->y + (y_len - expected_len), expected_len); +if (!y) { +fprintf (stderr,"Cannot allocate memory for point Y" ); +free ((char *)x); +return -ENOMEM ; + } + key->y = (const uint8_t *)y; +return 0 ; } +static void fdt_free_key (struct ecdsa_public_key *key) +{ +if (!key) +return ; +if (key->x) +free ((char *)key->x); +if (key->y) +free ((char *)key->y); +} +static int read_key_from_fdt (struct signer *ctx,const void *fdt,int node) {struct ecdsa_public_key pubkey;
@@ -89,8 +142,11 @@ static int read_key_from_fdt(struct signer *ctx, const void *fdt, int node) nid = NID_X9_62_prime256v1; }else if (!strcmp (pubkey.curve_name,"secp384r1" )) { nid = NID_secp384r1; + }else if (!strcmp (pubkey.curve_name,"secp521r1" )) { + nid = NID_secp521r1; }else {fprintf (stderr,"Unsupported curve name: '%s'\n" , pubkey.curve_name); +fdt_free_key (&pubkey);return -EINVAL ; }
@@ -100,6 +156,7 @@ static int read_key_from_fdt(struct signer *ctx, const void *fdt, int node) ec_key =EC_KEY_new_by_curve_name (nid);if (!ec_key) {fprintf (stderr,"Failed to allocate EC_KEY for curve %s\n" , pubkey.curve_name); +fdt_free_key (&pubkey);return -ENOMEM ; }
@@ -108,10 +165,11 @@ static int read_key_from_fdt(struct signer *ctx, const void *fdt, int node)if (!point) {fprintf (stderr,"Failed to allocate EC_POINT\n" );EC_KEY_free (ec_key); +fdt_free_key (&pubkey);return -ENOMEM ; } - len = pubkey.size_bits /8 ; + len =DIV_ROUND_UP (pubkey.size_bits,8 );uint8_t buf[1 + len *2 ];
@@ -123,6 +181,7 @@ static int read_key_from_fdt(struct signer *ctx, const void *fdt, int node)fprintf (stderr,"Failed to convert (x,y) point to EC_POINT\n" );EC_POINT_free (point);EC_KEY_free (ec_key); +fdt_free_key (&pubkey);return -EINVAL ; }
@@ -130,11 +189,13 @@ static int read_key_from_fdt(struct signer *ctx, const void *fdt, int node)fprintf (stderr,"Failed to set EC_POINT as public key\n" );EC_POINT_free (point);EC_KEY_free (ec_key); +fdt_free_key (&pubkey);return -EINVAL ; }fprintf (stderr,"Successfully loaded ECDSA key from FDT node %d\n" , node);EC_POINT_free (point); +fdt_free_key (&pubkey); ctx->ecdsa_key = ec_key;return 0 ;
lib/ecdsa/ecdsa-verify.c +61 -4
115 lines not on this page · raw message
lib/fdt-libcrypto.c +1 -1
@@ -10,7 +10,7 @@int fdt_add_bignum (void *blob,int noffset,const char *prop_name,BIGNUM *num,int num_bits) { -int nwords = num_bits /32 ; +int nwords = (num_bits +31 ) /32 ;int size;uint32_t *buf, *ptr;BIGNUM *tmp, *big2, *big32, *big2_32;
tools/image-sig-host.c +7 -0
@@ -83,6 +83,13 @@ struct crypto_algo crypto_algos[] = { .add_verify_data = ecdsa_add_verify_data, .verify = ecdsa_verify, }, + { + .name = "ecdsa521", + .key_len = ECDSA521_BYTES, + .sign = ecdsa_sign, + .add_verify_data = ecdsa_add_verify_data, + .verify = ecdsa_verify, + }, { .name = "secp521r1", .key_len = ECDSA521_BYTES,
Signature · 1 line
2.43.0
Re: [PATCH v7 01/15] ecdsa: fix support of secp521r1
Hi Philippe,
Quoted 7 lines · Philippe Reynes
ecdsa: fix support of secp521r1
Current implementation of ecdsa only supports key len aligned on 8 bits. But the curve secp521r1 uses a key of 521 bits which is not aligned on 8 bits. In this commit, we update the keys management for ecdsa to support keys that are not aligned on 8 bits.
Quoted 8 lines
Reviewed-by Raymond Mao <[email protected]>Signed-off-by Philippe Reynes <[email protected]>
lib/ecdsa/ecdsa-libcrypto.c| 65 +++++++++++++++++++++++++++++++++++++++++++--lib/ecdsa/ecdsa-verify.c | 65 ++++++++++++++++++++++++++++++++++++++++++--- lib/fdt-libcrypto.c | 2 +- tools/image-sig-host.c | 7 +++++ 4 files changed, 132 insertions(+), 7 deletions(-)
questions / nits below
lib/ecdsa/ecdsa-libcrypto.c struct ecdsa_public_key { · line 52
Quoted 12 lines
lib/ecdsa/ecdsa-libcrypto.c +10
-0@@ -41,10 +43,26 @@ struct ecdsa_public_key { +static char *memdup (char *buf,size_t size) +{ +char *dup; + + dup =malloc (size); +if (dup) +memcpy (dup, buf, size); + +return dup; +}
Please match the U-Boot signature: void *memdup(const void *src, size_t len) (see include/linux/string.h). Making buf const lets the call sites lose the (char *) casts. Also note that fdt_get_key() now hands back malloc'd buffers - please spell out the caller's free responsibility in a function comment.
Quoted 6 lines
lib/ecdsa/ecdsa-verify.c +1
-0@@ -135,6 +180,18 @@ U_BOOT_CRYPTO_ALGO(ecdsa384) = { .verify = ecdsa_verify, }; +U_BOOT_CRYPTO_ALGO (ecdsa521) = {
lib/ecdsa/ecdsa-verify.c U_BOOT_CRYPTO_ALGO(ecdsa384) = { · line 193
Quoted 10 lines
lib/ecdsa/ecdsa-verify.c +10
-0+ .name = 'ecdsa521', + .key_len = ECDSA521_BYTES, + .verify = ecdsa_verify, +}; + +U_BOOT_CRYPTO_ALGO (secp521r1) = { + .name ='secp521r1' , + .key_len =ECDSA521_BYTES , + .verify = ecdsa_verify, +};
I'm not entirely clear why there are two entries here?
Quoted 6 lines
lib/ecdsa/ecdsa-verify.c +4
-0@@ -50,15 +57,48 @@ static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node) + x =memdup ((char *)key->x + (x_len - expected_len), expected_len); +if (!x) { +debug ("Cannot allocate memory for point X" ); +return -ENOMEM ;
lib/ecdsa/ecdsa-verify.c static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node) · line 70
Quoted 10 lines
lib/ecdsa/ecdsa-verify.c +10
-0+ } + key->x = (const uint8_t *)x; + + y =memdup ((char *)key->y + (y_len - expected_len), expected_len); +if (!y) { +debug ("Cannot allocate memory for point Y" ); +free ((char *)x); +return -ENOMEM ; + } + key->y = (const uint8_t *)y;
A few things:
1. You should be able to drop the char * casts for memdup()
2. When the y allocation fails you free x but leave key->x pointing at the freed buffer, so a later fdt_free_key() will double-free. Please set key->x = NULL after the free.
3. The debug() messages are missing a trailing newline.
lib/ecdsa/ecdsa-verify.c static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node) · line 65
Quoted 12 lines
lib/ecdsa/ecdsa-verify.c +9
-1@@ -50,15 +57,48 @@ static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node) -if (x_len != (key->size_bits /8 ) || y_len != (key->size_bits /8 )) { +/* +* The public key is stored as an array of u32, so if the key size is +* not a multiple of 32 (for example 521), we may have extra bytes. +* To avoid any issue later, we shift the x and y pointer to the first +* useful byte. +*/ + expected_len =DIV_ROUND_UP (key->size_bits,8 ); + +if (x_len < expected_len || y_len < expected_len) {
The previous check was !=; you have loosened it to <
The padded length is known exactly, I think - isn't it DIV_ROUND_UP(key->size_bits, 32) * 4. ? So could we check the exact value to avoid silently accepting (and truncating) over-sized properties for prime256v1/secp384r1 ?
lib/ecdsa/ecdsa-libcrypto.c static int read_key_from_fdt(struct signer *ctx, const void *fdt, int node) · line 194
Quoted 8 lines
lib/ecdsa/ecdsa-libcrypto.c +1
-0@@ -130,11 +189,13 @@ static int read_key_from_fdt(struct signer *ctx, const void *fdt, int node)fprintf (stderr,"Failed to set EC_POINT as public key\n" );EC_POINT_free (point);EC_KEY_free (ec_key); +fdt_free_key (&pubkey);return -EINVAL ; }
The repeated EC_POINT_free(point); EC_KEY_free(ec_key); fdt_free_key(&pubkey); return -EINVAL; across five exit paths is begging for a goto-style cleanup at some point in the future :-)
Regards,
Simon
Re: [PATCH v7 01/15] ecdsa: fix support of secp521r1
Quoted 33 lines · Simon Glass
Hi Philippe,
Quoted 7 lines · Philippe Reynes
ecdsa: fix support of secp521r1
Current implementation of ecdsa only supports key len aligned on 8 bits. But the curve secp521r1 uses a key of 521 bits which is not aligned on 8 bits. In this commit, we update the keys management for ecdsa to support keys that are not aligned on 8 bits.
Quoted 8 lines
Reviewed-by Raymond Mao <[email protected]>Signed-off-by Philippe Reynes <[email protected]>
lib/ecdsa/ecdsa-libcrypto.c| 65 +++++++++++++++++++++++++++++++++++++++++++--lib/ecdsa/ecdsa-verify.c | 65 ++++++++++++++++++++++++++++++++++++++++++--- lib/fdt-libcrypto.c | 2 +- tools/image-sig-host.c | 7 +++++ 4 files changed, 132 insertions(+), 7 deletions(-)Reviewed-by Simon Glass <[email protected]>questions / nits below
Quoted 10 lines
lib/ecdsa/ecdsa-libcrypto.c +8
-0@@ -41,10 +43,26 @@ struct ecdsa_public_key { +static char *memdup (char *buf,size_t size) +{ +char *dup; + + dup =malloc (size); +if (dup) +memcpy (dup, buf, size); +
Quoted 8 lines
lib/ecdsa/ecdsa-libcrypto.c struct ecdsa_public_key { · line 55
Quoted 2 lines
lib/ecdsa/ecdsa-libcrypto.c +2
-0+return dup; +}Please match the U-Boot signature: void *memdup(const void *src,
size_tlen) (seeinclude/linux/string.h). Making buf const lets the call sites lose the (char *) casts. Also note thatfdt_get_key()now hands back malloc'd buffers - please spell out the caller's free responsibility in a function comment.
Simon, I've noticed of late that you're providing a lot of Reviewed-by tags in the same email that you provide a lot of substantial and non-trivial feedback. This would normally negate the Reviewed-by tag, so can you please refrain from providing both the tag and feedback in the same email? Thanks.
Signature · 1 line
Tom
Re: [PATCH v7 01/15] ecdsa: fix support of secp521r1
Hi Tom,
Quoted 44 lines · Tom Rini
Quoted 33 lines · Simon Glass
Hi Philippe,
Quoted 7 lines · Philippe Reynes
ecdsa: fix support of secp521r1
Current implementation of ecdsa only supports key len aligned on 8 bits. But the curve secp521r1 uses a key of 521 bits which is not aligned on 8 bits. In this commit, we update the keys management for ecdsa to support keys that are not aligned on 8 bits.
Quoted 8 lines
Reviewed-by Raymond Mao <[email protected]>Signed-off-by Philippe Reynes <[email protected]>
lib/ecdsa/ecdsa-libcrypto.c| 65 +++++++++++++++++++++++++++++++++++++++++++--lib/ecdsa/ecdsa-verify.c | 65 ++++++++++++++++++++++++++++++++++++++++++--- lib/fdt-libcrypto.c | 2 +- tools/image-sig-host.c | 7 +++++ 4 files changed, 132 insertions(+), 7 deletions(-)Reviewed-by Simon Glass <[email protected]>questions / nits below
Quoted 10 lines
lib/ecdsa/ecdsa-libcrypto.c +8
-0@@ -41,10 +43,26 @@ struct ecdsa_public_key { +static char *memdup (char *buf,size_t size) +{ +char *dup; + + dup =malloc (size); +if (dup) +memcpy (dup, buf, size); +Quoted 8 lines
lib/ecdsa/ecdsa-libcrypto.c struct ecdsa_public_key { · line 55
Quoted 2 lines
lib/ecdsa/ecdsa-libcrypto.c +2
-0+return dup; +}Please match the U-Boot signature: void *memdup(const void *src,
size_tlen) (seeinclude/linux/string.h). Making buf const lets the call sites lose the (char *) casts. Also note thatfdt_get_key()now hands back malloc'd buffers - please spell out the caller's free responsibility in a function comment.
Quoted 5 lines
Simon, I've noticed of late that you're providing a lot of Reviewed-by tags in the same email that you provide a lot of substantial and non-trivial feedback. This would normally negate the Reviewed-by tag, so can you please refrain from providing both the tag and feedback in the same email? Thanks.
I add the tag when the changes I am requested are not critical, particularly when we are already several versions in. The honest answer is that I don't really want to look at a series again :-) I am already spending a lot of time on reviews, to help code land.
I will see if I can only add the tag when I have minor feedback.
Regards,
Simon
Re: [PATCH v7 01/15] ecdsa: fix support of secp521r1
In reply to Simon GlassHi Simon,
Le 29/05/2026 à 09:16, Simon Glass a écrit :
Quoted 47 lines
This Mail comes from Outside of SoftAtHome: Do not answer, click links or open attachments unless you recognize the sender and know the content is safe.
Hi Philippe,
Quoted 7 lines · Philippe Reynes
ecdsa: fix support of secp521r1
Current implementation of ecdsa only supports key len aligned on 8 bits. But the curve secp521r1 uses a key of 521 bits which is not aligned on 8 bits. In this commit, we update the keys management for ecdsa to support keys that are not aligned on 8 bits.
Quoted 8 lines
Reviewed-by Raymond Mao <[email protected]>Signed-off-by Philippe Reynes <[email protected]>
lib/ecdsa/ecdsa-libcrypto.c| 65 +++++++++++++++++++++++++++++++++++++++++++--lib/ecdsa/ecdsa-verify.c | 65 ++++++++++++++++++++++++++++++++++++++++++--- lib/fdt-libcrypto.c | 2 +- tools/image-sig-host.c | 7 +++++ 4 files changed, 132 insertions(+), 7 deletions(-)Reviewed-by Simon Glass <[email protected]>questions / nits below
lib/ecdsa/ecdsa-libcrypto.c struct ecdsa_public_key { · line 52
Quoted 12 lines
lib/ecdsa/ecdsa-libcrypto.c +10
-0@@ -41,10 +43,26 @@ struct ecdsa_public_key { +static char *memdup (char *buf,size_t size) +{ +char *dup; + + dup =malloc (size); +if (dup) +memcpy (dup, buf, size); + +return dup; +}Please match the U-Boot signature: void *memdup(const void *src,
size_tlen) (seeinclude/linux/string.h). Making buf const lets the call sites lose the (char *) casts. Also note thatfdt_get_key()now hands back malloc'd buffers - please spell out the caller's free responsibility in a function comment.Quoted 5 lines
lib/ecdsa/ecdsa-verify.c +0
-0@@ -135,6 +180,18 @@ U_BOOT_CRYPTO_ALGO(ecdsa384) = { .verify = ecdsa_verify, };
Quoted 12 lines
lib/ecdsa/ecdsa-verify.c U_BOOT_CRYPTO_ALGO(ecdsa384) = { · line 193
Quoted 11 lines
lib/ecdsa/ecdsa-verify.c +11
-0+U_BOOT_CRYPTO_ALGO (ecdsa521) = { + .name ='ecdsa521' , + .key_len =ECDSA521_BYTES , + .verify = ecdsa_verify, +}; + +U_BOOT_CRYPTO_ALGO (secp521r1) = { + .name ='secp521r1' , + .key_len =ECDSA521_BYTES , + .verify = ecdsa_verify, +};I'm not entirely clear why there are two entries here?
Without this serie, there is an initial (broken) support of ecdsa 521.
It was added only to tools/image-sig-host.c, but with the name if "secp521r1" instead of "ecdsa521".
In my first proposal, I have changed this "secp521r1" to "ecdsa521". Then, in the review, You and Raymond
were worried about breaking existing code (see: https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/).
So so avoid any issue, I have added the support of secp521r1 and ecdsa521.
Best regards,
Philippe
Quoted 58 lines
Quoted 7 lines
lib/ecdsa/ecdsa-verify.c +4
-0@@ -50,15 +57,48 @@ static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node) + x =memdup ((char *)key->x + (x_len - expected_len), expected_len); +if (!x) { +debug ("Cannot allocate memory for point X" ); +return -ENOMEM ;lib/ecdsa/ecdsa-verify.c static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node) · line 70
Quoted 10 lines
lib/ecdsa/ecdsa-verify.c +10
-0+ } + key->x = (const uint8_t *)x; + + y =memdup ((char *)key->y + (y_len - expected_len), expected_len); +if (!y) { +debug ("Cannot allocate memory for point Y" ); +free ((char *)x); +return -ENOMEM ; + } + key->y = (const uint8_t *)y;A few things:
1. You should be able to drop the char * casts for
memdup()
2. When the y allocation fails you free x but leavekey->xpointing at the freed buffer, so a laterfdt_free_key()will double-free. Please setkey->x= NULL after the free.
3. Thedebug()messages are missing a trailing newline.lib/ecdsa/ecdsa-verify.c static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node) · line 65
Quoted 12 lines
lib/ecdsa/ecdsa-verify.c +9
-1@@ -50,15 +57,48 @@ static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node) -if (x_len != (key->size_bits /8 ) || y_len != (key->size_bits /8 )) { +/* +* The public key is stored as an array of u32, so if the key size is +* not a multiple of 32 (for example 521), we may have extra bytes. +* To avoid any issue later, we shift the x and y pointer to the first +* useful byte. +*/ + expected_len =DIV_ROUND_UP (key->size_bits,8 ); + +if (x_len < expected_len || y_len < expected_len) {The previous check was !=; you have loosened it to <
The padded length is known exactly, I think - isn't it
DIV_ROUND_UP(key->size_bits, 32)* 4. ? So could we check the exact value to avoid silently accepting (and truncating) over-sized properties for prime256v1/secp384r1 ?lib/ecdsa/ecdsa-libcrypto.c static int read_key_from_fdt(struct signer *ctx, const void *fdt, int node) · line 194
Quoted 8 lines
lib/ecdsa/ecdsa-libcrypto.c +1
-0@@ -130,11 +189,13 @@ static int read_key_from_fdt(struct signer *ctx, const void *fdt, int node)fprintf (stderr,"Failed to set EC_POINT as public key\n" );EC_POINT_free (point);EC_KEY_free (ec_key); +fdt_free_key (&pubkey);return -EINVAL ; }The repeated
EC_POINT_free(point);EC_KEY_free(ec_key);fdt_free_key(&pubkey); return-EINVAL; across five exit paths is begging for a goto-style cleanup at some point in the future :-)Regards,
Simon
PATCH v7 2/15 mbedtls: enable support of ecc
In reply to Philippe ReynesEnables the support of ecc in mbedtls.
---
v2:
- move ecdsa to MBEDTLS_LIB_X509
- enhance depencendies
v3:
- do not use _MBEDTLS in mbedtls_def_config.h v4:
- do not select ECDSA on some configs
- remove duplicated MBEDTLS_ECP_DP_SECP256K1_ENABLED
- change dependencies for ECDSA configs
v5:
- MBEDTLS_LIB_TLS depends en ECDSA_MBEDTLS
- update some configs according to previous change
- build of ecdsa is conditionned by ECDSA_MBEDTLS v6:
- fix dependencies for ecdsa and ecc on mbedtls
- ecdsa is no longer mandatory for tls
v7:
- no change
configs/sandbox_defconfig | 1 + lib/ecdsa/Kconfig | 1 + lib/mbedtls/Kconfig | 17 +++++++++++++ lib/mbedtls/Makefile | 17 +++++++------ lib/mbedtls/mbedtls_def_config.h | 41 +++++++++++++++++++++----------- 5 files changed, 56 insertions(+), 21 deletions(-)
configs/sandbox_defconfig +1 -0
@@ -381,6 +381,7 @@ CONFIG_PANIC_HANG=y CONFIG_CMD_DHRYSTONE=y CONFIG_MBEDTLS_LIB=y CONFIG_HKDF_MBEDTLS=y +CONFIG_ECDSA_MBEDTLS=y CONFIG_ECDSA=y CONFIG_ECDSA_VERIFY=y CONFIG_RSASSA_PSS=y
lib/ecdsa/Kconfig +1 -0
@@ -1,6 +1,7 @@ config ECDSA bool "Enable ECDSA support" depends on DM + select ASN1_DECODER help This enables the ECDSA (elliptic curve signature) algorithm for FIT image verification in U-Boot. The ECDSA algorithm is implemented
lib/mbedtls/Kconfig +17 -0
@@ -247,6 +247,12 @@ config MBEDTLS_LIB_X509 if MBEDTLS_LIB_X509 +config BIGNUM_MBEDTLS + bool + +config ECC_MBEDTLS + bool + config ASN1_DECODER_MBEDTLS bool "ASN1 decoder with MbedTLS certificate library" depends on MBEDTLS_LIB_X509 && ASN1_DECODER
@@ -264,6 +270,7 @@ config RSA_PUBLIC_KEY_PARSER_MBEDTLS bool "RSA public key parser with MbedTLS certificate library" depends on ASYMMETRIC_PUBLIC_KEY_MBEDTLS select ASN1_DECODER_MBEDTLS + select BIGNUM_MBEDTLS help This option chooses MbedTLS certificate library for RSA public key parser.
@@ -292,6 +299,15 @@ config MSCODE_PARSER_MBEDTLS This option chooses MbedTLS certificate library for MS authenticode parser. +config ECDSA_MBEDTLS + bool "Enable ECDSA support with MbedTLS certificate library" + depends on MBEDTLS_LIB_X509 && ASN1_DECODER_MBEDTLS + select BIGNUM_MBEDTLS + select ECC_MBEDTLS + help + This option enables support of ECDSA with the MbedTLS certificate + library. + endif # MBEDTLS_LIB_X509 config MBEDTLS_LIB_TLS
@@ -301,6 +317,7 @@ config MBEDTLS_LIB_TLS depends on ASYMMETRIC_PUBLIC_KEY_MBEDTLS depends on ASN1_DECODER_MBEDTLS depends on MBEDTLS_LIB + select ECC_MBEDTLS help Enable MbedTLS TLS library. Required for HTTPs support in wget
lib/mbedtls/Makefile +10 -7
@@ -39,13 +39,21 @@ mbedtls_lib_crypto-$(CONFIG_$(PHASE_)HKDF_MBEDTLS) += \ # MbedTLS X509 library obj-$(CONFIG_$(XPL_)MBEDTLS_LIB_X509) += mbedtls_lib_x509.o mbedtls_lib_x509-y := $(MBEDTLS_LIB_DIR)/x509.o +mbedtls_lib_x509-$(CONFIG_$(PHASE_)ECC_MBEDTLS) += \ + $(MBEDTLS_LIB_DIR)/ecp.o \ + $(MBEDTLS_LIB_DIR)/ecp_curves.o \ + $(MBEDTLS_LIB_DIR)/ecp_curves_new.o \ + $(MBEDTLS_LIB_DIR)/pk_ecc.o +mbedtls_lib_x509-$(CONFIG_$(PHASE_)ECDSA_MBEDTLS) += \ + $(MBEDTLS_LIB_DIR)/ecdsa.o +mbedtls_lib_x509-$(CONFIG_$(PHASE_)BIGNUM_MBEDTLS) += \ + $(MBEDTLS_LIB_DIR)/bignum.o \ + $(MBEDTLS_LIB_DIR)/bignum_core.o mbedtls_lib_x509-$(CONFIG_$(PHASE_)ASN1_DECODER_MBEDTLS) += \ $(MBEDTLS_LIB_DIR)/asn1parse.o \ $(MBEDTLS_LIB_DIR)/asn1write.o \ $(MBEDTLS_LIB_DIR)/oid.o mbedtls_lib_x509-$(CONFIG_$(PHASE_)RSA_PUBLIC_KEY_PARSER_MBEDTLS) += \ - $(MBEDTLS_LIB_DIR)/bignum.o \ - $(MBEDTLS_LIB_DIR)/bignum_core.o \ $(MBEDTLS_LIB_DIR)/rsa.o \ $(MBEDTLS_LIB_DIR)/rsa_alt_helpers.o mbedtls_lib_x509-$(CONFIG_$(PHASE_)ASYMMETRIC_PUBLIC_KEY_MBEDTLS) += \
@@ -64,7 +72,6 @@ mbedtls_lib_tls-y := \ $(MBEDTLS_LIB_DIR)/mps_reader.o \ $(MBEDTLS_LIB_DIR)/mps_trace.o \ $(MBEDTLS_LIB_DIR)/net_sockets.o \ - $(MBEDTLS_LIB_DIR)/pk_ecc.o \ $(MBEDTLS_LIB_DIR)/ssl_cache.o \ $(MBEDTLS_LIB_DIR)/ssl_ciphersuites.o \ $(MBEDTLS_LIB_DIR)/ssl_client.o \
@@ -82,8 +89,4 @@ mbedtls_lib_tls-y := \ $(MBEDTLS_LIB_DIR)/cipher.o \ $(MBEDTLS_LIB_DIR)/cipher_wrap.o \ $(MBEDTLS_LIB_DIR)/ecdh.o \ - $(MBEDTLS_LIB_DIR)/ecdsa.o \ - $(MBEDTLS_LIB_DIR)/ecp.o \ - $(MBEDTLS_LIB_DIR)/ecp_curves.o \ - $(MBEDTLS_LIB_DIR)/ecp_curves_new.o \ $(MBEDTLS_LIB_DIR)/gcm.o \
lib/mbedtls/mbedtls_def_config.h +27 -14
77 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v7 02/15] mbedtls: enable support of ecc
Hi Philippe,
Quoted 7 lines · Philippe Reynes
mbedtls: enable support of ecc
Enables the support of ecc in mbedtls.
Reviewed-by Raymond Mao <[email protected]>Signed-off-by Philippe Reynes <[email protected]>
Quoted 6 lines
configs/sandbox_defconfig | 1 +
lib/ecdsa/Kconfig | 1 + lib/mbedtls/Kconfig | 17 +++++++++++++++++ lib/mbedtls/Makefile | 17 ++++++++++------- lib/mbedtls/mbedtls_def_config.h | 41 ++++++++++++++++++++++++++-------------- 5 files changed, 56 insertions(+), 21 deletions(-)
thoughts below
lib/mbedtls/mbedtls_def_config.h line 152
Quoted 11 lines
lib/mbedtls/mbedtls_def_config.h +2
-0@@ -122,7 +144,9 @@/* ECDSA */ #if CONFIG_IS_ENABLED (ASN1_DECODER ) +#if CONFIG_IS_ENABLED (ECDSA_MBEDTLS )#define MBEDTLS_ECDSA_C +#endif // #if CONFIG_IS_ENABLED(ECDSA_MBEDTLS) #define MBEDTLS_ECP_C #define MBEDTLS_ECDH_C #endif
Please use /* ... */ rather than // here and on the matching #endif below, like the rest of this file
lib/mbedtls/mbedtls_def_config.h line 70
Quoted 11 lines
lib/mbedtls/mbedtls_def_config.h +6
-0@@ -62,6 +62,25 @@#if CONFIG_IS_ENABLED (MBEDTLS_LIB_X509 ) +#if CONFIG_IS_ENABLED (BIGNUM_MBEDTLS ) +#define MBEDTLS_BIGNUM_C +#endif + +#if CONFIG_IS_ENABLED (ECC_MBEDTLS ) +#define MBEDTLS_ECP_C
The commit message is very thin for a patch that splits BIGNUM/ECC out so they can be enabled without TLS, introduces new hidden Kconfig symbols, moves several object files, and changes lib/ecdsa/Kconfig. Please expand it to explain the motivation (preparing for ECDSA-verify via mbedtls) and the structural change.
Regards,
Simon
PATCH v7 3/15 ecdsa: initial support of ecdsa using mbedtls
In reply to Philippe ReynesAdds an initial support of ecdsa verify using mbedtls.
---
v2:
- rename sw_ecdsa.c to ecdsa.c
v3:
- rename sw_ecdsa_verify to ecdsa_hash_verify
- stop on first group found
- check signature len
- use debug instead of printf
- check function returns
- fix memleaks in ecdsa_hash_verify
v4:
- move struct ecdsa_public_key from ecdsa-u-class.h to internal/ecdsa.h
- use DIV_ROUND_UP
- some code cleanup
v5:
- add kerneldoc header for ecdsa_hash_verify
- read error when setting Q.Z
v6:
- add ECDSA_VERIFY_MBEDTLS
- fix kerneldoc: doc not add a space before :
v7:
- no change
configs/sandbox_defconfig | 1 + include/crypto/ecdsa-uclass.h | 15 +--- include/crypto/internal/ecdsa.h | 39 +++++++++ lib/mbedtls/Kconfig | 7 ++ lib/mbedtls/Makefile | 3 + lib/mbedtls/ecdsa.c | 146 ++++++++++++++++++++++++++++++++ 6 files changed, 197 insertions(+), 14 deletions(-) create mode 100644 include/crypto/internal/ecdsa.h create mode 100644 lib/mbedtls/ecdsa.c
configs/sandbox_defconfig +1 -0
@@ -382,6 +382,7 @@ CONFIG_CMD_DHRYSTONE=y CONFIG_MBEDTLS_LIB=y CONFIG_HKDF_MBEDTLS=y CONFIG_ECDSA_MBEDTLS=y +CONFIG_ECDSA_VERIFY_MBEDTLS=y CONFIG_ECDSA=y CONFIG_ECDSA_VERIFY=y CONFIG_RSASSA_PSS=y
include/crypto/ecdsa-uclass.h +1 -14
@@ -4,20 +4,7 @@*/ #include <dm/device.h> - -/** -* struct ecdsa_public_key - ECDSA public key properties -* -* The struct has pointers to the (x, y) curve coordinates to an ECDSA public -* key, as well as the name of the ECDSA curve. The size of the key is inferred -* from the 'curve_name' -*/ -struct ecdsa_public_key { -const char *curve_name;/* Name of curve, e.g. "prime256v1" */ -const void *x;/* x coordinate of public key */ -const void *y;/* y coordinate of public key */ -unsigned int size_bits;/* key size in bits, derived from curve name */ -}; +#include <crypto/internal/ecdsa.h> struct ecdsa_ops {/**
include/crypto/internal/ecdsa.h new file +39 -0
@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* +* Copyright (c) 2026, Philippe Reynes <[email protected]> +*/ +#ifndef _ECDSA_HELPER_ +#define _ECDSA_HELPER_ + +#include <linux/types.h> + +/** +* struct ecdsa_public_key - ECDSA public key properties +* +* The struct has pointers to the (x, y) curve coordinates to an ECDSA public +* key, as well as the name of the ECDSA curve. The size of the key is inferred +* from the 'curve_name' +*/ +struct ecdsa_public_key { +const char *curve_name;/* Name of curve, e.g. "prime256v1" */ +const void *x;/* x coordinate of public key */ +const void *y;/* y coordinate of public key */ +unsigned int size_bits;/* key size in bits, derived from curve name */ +}; + +/** +* +* ecdsa_hash_verify() - Verify the ecdsa signature of a hash +* +* @pubkey: ecdsa public key +* @hash: Hash +* @hash_len: Size of the hash +* @signature: Signature +* @sig_len: Size of the signature +* Return: 0 if all verified ok, <0 on error +*/ +int ecdsa_hash_verify (const struct ecdsa_public_key *pubkey, +const void *hash,size_t hash_len, +const void *signature,size_t sig_len); + +#endif
lib/mbedtls/Kconfig +7 -0
@@ -308,6 +308,13 @@ config ECDSA_MBEDTLS This option enables support of ECDSA with the MbedTLS certificate library. +config ECDSA_VERIFY_MBEDTLS + bool "Enable ECDSA verify" + depends on ECDSA_MBEDTLS && ECDSA_VERIFY + help + This option enables support of ECDSA signature check with + MbedTLS certificate library. + endif # MBEDTLS_LIB_X509 config MBEDTLS_LIB_TLS
lib/mbedtls/Makefile +3 -0
@@ -11,6 +11,9 @@ obj-$(CONFIG_$(PHASE_)SHA1_MBEDTLS) += sha1.o obj-$(CONFIG_$(PHASE_)SHA256_MBEDTLS) += sha256.o obj-$(CONFIG_$(PHASE_)SHA512_MBEDTLS) += sha512.o +# shim layer for ecdsa verify +obj-$(CONFIG_$(PHASE_)ECDSA_VERIFY_MBEDTLS) += ecdsa.o + # x509 libraries obj-$(CONFIG_$(PHASE_)ASYMMETRIC_PUBLIC_KEY_MBEDTLS) += \ public_key.o
lib/mbedtls/ecdsa.c new file +146 -0
146 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v7 03/15] ecdsa: initial support of ecdsa using mbedtls
Hi Philippe,
Quoted 7 lines · Philippe Reynes
ecdsa: initial support of ecdsa using mbedtls
Adds an initial support of ecdsa verify using mbedtls.
Reviewed-by Raymond Mao <[email protected]>Signed-off-by Philippe Reynes <[email protected]>
Quoted 7 lines
configs/sandbox_defconfig | 1 + include/crypto/ecdsa-uclass.h | 15 +---- include/crypto/internal/ecdsa.h | 39 +++++++++++ lib/mbedtls/Kconfig | 7 ++ lib/mbedtls/Makefile | 3 + lib/mbedtls/ecdsa.c | 146 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 197 insertions(+), 14 deletions(-)
nits below
include/crypto/internal/ecdsa.h line 11
Quoted 13 lines
include/crypto/internal/ecdsa.h +11
-0@@ -0,0 +1,39 @@ +/** +* +* ecdsa_hash_verify() - Verify the ecdsa signature of a hash +* +* @pubkey: ecdsa public key +* @hash: Hash +* @hash_len: Size of the hash +* @signature: Signature +* @sig_len: Size of the signature +* Return: 0 if all verified ok, <0 on error +*/
Please drop the stray blank line at the start - the function description should be on the first line after /**. Also a blank line is missing before Return: so it gets folded into the @sig_len description.
lib/mbedtls/ecdsa.c line 10
Quoted 12 lines
lib/mbedtls/ecdsa.c +10
-0@@ -0,0 +1,146 @@ +static mbedtls_ecp_group_id ecdsa_search_group_id (const char *curve_name) +{ +mbedtls_ecp_group_id grp_id =MBEDTLS_ECP_DP_NONE ; +const mbedtls_ecp_curve_info *info; + +if (!curve_name) + goto out; + +if (!strcmp (curve_name,'prime256v1' )) +return MBEDTLS_ECP_DP_SECP256R1 ;
A brief comment would help here it is not obvious why prime256v1 is singled out. My understanding is that it is the OpenSSL alias for secp256r1 (which is what FIT keys carry), while mbedtls_ecp_curve_list() only exposes the secp256r1 name. Worth saying so.
Also, since the only function of the local grp_id is to return MBEDTLS_ECP_DP_NONE on the !curve_name path, this could be simplified to an early return MBEDTLS_ECP_DP_NONE; and dropping the out: label entirely.
lib/mbedtls/ecdsa.c line 5
Quoted 7 lines
lib/mbedtls/ecdsa.c +5
-0@@ -0,0 +1,146 @@ +mbedtls_mpi r, s; +int key_len; +int err =-1 ; + + key_len =DIV_ROUND_UP (pubkey->size_bits,8 );
The err = -1 initialiser is dead - every path to out: sets err explicitly first. Please drop it, or use a meaningful -Exxx value as a safety default.
Quoted 4 lines
lib/mbedtls/ecdsa.c +2
-0@@ -0,0 +1,146 @@ + out4: +mbedtls_mpi_free (&s);
lib/mbedtls/ecdsa.c line 12
Quoted 10 lines
lib/mbedtls/ecdsa.c +10
-0+ out3: +mbedtls_mpi_free (&r); + out2: +mbedtls_ecp_point_free (&Q ); + out1: +mbedtls_ecp_group_free (&grp); + out: + +return err; +}
Please name the labels after what they do rather than counting them - e.g. free_s, free_r, free_q, free_grp, out. As-is, every edit has to renumber them, and a reader has to scroll up to figure out what out3 protects. Also drop the blank line between out: and return err;.
Regards,
Simon
PATCH v7 4/15 test: lib: ecdsa: add initial test
In reply to Philippe ReynesAdds tests to check that the function ecdsa_hash_verify using mbedtls is valid.
---
v2:
- little change to only use test with sha enabled
v3:
- remove useless field *k in struct ecdsa_test_vector_s
- change expected value on error (don't use mbedtls error)
- manage function returns
- update third parameter of sha*_csum_wd
v4:
- define SHA256_DEF_CHUNK_SZ, SHA384_DEF_CHUNK_SZ and SHA512_DEF_CHUNK_SZ
- report skipped tests
- add const on some variables
- code cleanup
v5:
- do not define SHA256_DEF_CHUNK_SZ, SHA384_DEF_CHUNK_SZ and SHA512_DEF_CHUNK_SZ
- use CHUNKSZ_SHA256, CHUNKSZ_SHA384 and CHUNKSZ_SHA521
- do not use 528 to compule ecdsa 521 sig size but compute it
- use ut_reportf instead of ut_failf
v6:
- no change
v7:
- no change
test/lib/Makefile | 1 + test/lib/ecdsa.c | 456 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 457 insertions(+) create mode 100644 test/lib/ecdsa.c
test/lib/Makefile +1 -0
@@ -8,6 +8,7 @@ obj-$(CONFIG_$(PHASE_)UT_COMPRESSION) += compression.o ifeq ($(CONFIG_XPL_BUILD),) obj-y += abuf.o obj-y += alist.o +obj-$(CONFIG_ECDSA_VERIFY_MBEDTLS) += ecdsa.o obj-$(CONFIG_EFI_LOADER) += efi_device_path.o efi_memory.o obj-$(CONFIG_EFI_SECURE_BOOT) += efi_image_region.o ifdef CONFIG_RISCV
test/lib/ecdsa.c new file +456 -0
456 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v7 5/15 drivers: crypto: add software ecdsa support
In reply to Philippe ReynesAdd a software ecdsa driver so it is
now possible to use ecdsa signature on
board without ecdsa hardware support.
---
v2:
- no change
v3:
- add depends on ECDSA_VERIFY to ECDSA_SW
- change sw_ecdsa_verify to ecdsa_hash_verify
- v4
- use ECDSA_MBEDTLS to build the driver
- clean include (change order)
v5:
- use $(PHASE_) in the Makefile for the driver
v6:
- remove include u-boot/ecdsa.h from ecdsa-sw.c
v7:
- no change
drivers/crypto/Makefile | 1 + drivers/crypto/ecdsa/Makefile | 6 ++++++ drivers/crypto/ecdsa/ecdsa-sw.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 drivers/crypto/ecdsa/Makefile create mode 100644 drivers/crypto/ecdsa/ecdsa-sw.c
drivers/crypto/Makefile +1 -0
@@ -4,6 +4,7 @@ # http://www.samsung.com obj-$(CONFIG_EXYNOS_ACE_SHA) += ace_sha.o +obj-$(CONFIG_ECDSA) += ecdsa/ obj-y += aes/ obj-y += rsa_mod_exp/ obj-y += fsl/
drivers/crypto/ecdsa/Makefile new file +6 -0
@@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright (C) 2026 Philippe Reynes <[email protected]> +# + +obj-$(CONFIG_$(PHASE_)ECDSA_VERIFY_MBEDTLS) += ecdsa-sw.o
drivers/crypto/ecdsa/ecdsa-sw.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (C) 2026 Philippe Reynes <[email protected]> +*/ +#include <crypto/ecdsa-uclass.h> +#include <crypto/internal/ecdsa.h> +#include <dm.h> +#include <linux/types.h> + +static int ops_sw_ecdsa_verify (__always_unused struct udevice *dev, +const struct ecdsa_public_key *pubkey, +const void *hash,size_t hash_len, +const void *signature,size_t sig_len) +{ +return ecdsa_hash_verify (pubkey, hash, hash_len, signature, sig_len); +} + +static const struct ecdsa_ops sw_ecdsa_ops = { + .verify = ops_sw_ecdsa_verify, +}; + +U_BOOT_DRIVER (sw_ecdsa) = { + .name ="sw_ecdsa" , + .id =UCLASS_ECDSA , + .ops = &sw_ecdsa_ops, + .flags =DM_FLAG_PRE_RELOC , +}; + +U_BOOT_DRVINFO (sw_ecdsa) = { + .name ="sw_ecdsa" , +}; +
Signature · 1 line
2.43.0
PATCH v7 6/15 test: dm: ecdsa.c: clean this test as software ecdsa is now implemented
In reply to Philippe ReynesThe test ecdsa was done when ecdsa was only supported by hardware. So it wasn't possible to test ecdsa on sandbox, and there is a test to check that ecdsa is not supported on sandbox.
Now, there is a software implementation of ecdsa. So we add a test to verify that ecdsa_verify may be used on sandbox.
---
v2:
- initial version
v3:
- no change
v4:
- add a test that use ecdsa_verify
v5:
- change secp256r1 to ecdsa256
- check that checksum and crypto are not null v6:
- update commit message
- remove fdt_strerror in macro CHECK
- return ENOMEM when malloc fails
- add a define FDT_MAX_SIZE to avoid hardcoded value v7:
- change uint8_t to u8
- set required_keynode to -1
test/dm/ecdsa.c | 109 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 99 insertions(+), 10 deletions(-)
test/dm/ecdsa.c +99 -10
@@ -3,36 +3,125 @@#include <crypto/ecdsa-uclass.h> #include <dm.h> #include <dm/test.h> +#include <malloc.h> #include <test/ut.h> #include <u-boot/ecdsa.h> +#define FDT_MAX_SIZE 512 + +#define CHECK (op) ({ \ + int err = op; \ + if (err < 0) { \ + printf("%s: function %s returns an error (%d)\n", \ + __func__, #op, err); \ + return err; \ + } \ + \ + err; \ + }) + +static int set_fdt_ecdsa_point (char *fdt,char *name,const char *data,size_t len) +{ +char *value =NULL ; +int ret =0 ; + +if (!fdt || !name || !data) { + ret = -EINVAL ; + goto out; + } + + value =malloc (len /2 ); +if (!value) { + ret = -ENOMEM ; + goto out; + } + + ret =hex2bin (value, data, len /2 ); +if (ret) + goto out; + + ret =fdt_property (fdt, name, value, len /2 ); +if (ret) + goto out; + + out: +free (value); +return ret; +} + +static int create_fdt_with_ecdsa_key (char *fdt,size_t size, +const char *name,const char *curve, +const char *x,const char *y) +{ +CHECK (fdt_create (fdt, size)); +CHECK (fdt_finish_reservemap (fdt)); +CHECK (fdt_begin_node (fdt,"" )); +CHECK (fdt_begin_node (fdt,"signature" )); +CHECK (fdt_begin_node (fdt, name)); +CHECK (fdt_property_string (fdt,"algo" ,"sha256,ecdsa256" )); +CHECK (set_fdt_ecdsa_point (fdt,"ecdsa,y-point" , y,strlen (y))); +CHECK (set_fdt_ecdsa_point (fdt,"ecdsa,x-point" , x,strlen (x))); +CHECK (fdt_property_string (fdt,"ecdsa,curve" , curve)); +CHECK (fdt_property_string (fdt,"key-name-hint" , name)); +CHECK (fdt_end_node (fdt));/* name */ +CHECK (fdt_end_node (fdt));/* "signature" */ +CHECK (fdt_end_node (fdt));/* "" */ +CHECK (fdt_finish (fdt)); +CHECK (fdt_pack (fdt)); + +return 0 ; +} +/* * Basic test of the ECDSA uclass and ecdsa_verify() * -* ECDSA implementations in u-boot are hardware-dependent. Until we have a -* software implementation that can be compiled into the sandbox, all we can -* test is the uclass support. +* ECDSA software implementation is tested in another test, +* so we only check that the class UCLASS_ECDSA may be used. * -* The uclass_get() test is redundant since ecdsa_verify() would also fail. We -* run both functions in order to isolate the cause more clearly. i.e. is -* ecdsa_verify() failing because the UCLASS is absent/broken? +* The data used in this test come from RFC6979 */ static int dm_test_ecdsa_verify (struct unit_test_state *uts) {struct uclass *ucp; +const char *full_name ="sha256,ecdsa256" ; +const char *name ="key-ecdsa-256" ; +const char *curve ="prime256v1" ; +const char *x ="60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6" ; +const char *y ="7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299" ; +const char *r ="EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716" ; +const char *s ="F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8" ; +u8 sig[64 ]; +char fdt[FDT_MAX_SIZE ]; -struct checksum_algo algo = { - .checksum_len =256 , +struct image_region region[] = { + { + .data ="sample" , + .size =strlen ("sample" ), + }, };struct image_sign_info info = { - .checksum = &algo, + .checksum =image_get_checksum_algo (full_name), + .crypto =image_get_crypto_algo (full_name), + .required_keynode =-1 , + .fdt_blob = fdt, }; +ut_assertnonnull (info.checksum); +ut_assertnonnull (info.crypto); + +/* create a fdt with the public key */ +ut_asserteq (0 ,create_fdt_with_ecdsa_key (fdt,sizeof (fdt), name, curve, x, y)); + +/* prepare the signature */ +ut_asserteq (0 ,hex2bin (sig +0 , r,strlen (r) /2 )); +ut_asserteq (0 ,hex2bin (sig +32 , s,strlen (s) /2 )); +ut_assertok (uclass_get (UCLASS_ECDSA , &ucp));ut_assertnonnull (ucp); -ut_asserteq (-ENODEV ,ecdsa_verify (&info,NULL ,0 ,NULL ,0 )); +ut_asserteq (0 ,ecdsa_verify (&info, region,1 , sig,sizeof (sig)));return 0 ; } +DM_TEST (dm_test_ecdsa_verify,UTF_SCAN_PDATA |UTF_SCAN_FDT );
Signature · 1 line
2.43.0
Re: [PATCH v7 06/15] test: dm: ecdsa.c: clean this test as software ecdsa is now implemented
Hi Philippe,
Quoted 8 lines · Philippe Reynes
test: dm:
ecdsa.c: clean this test as software ecdsa is now implementedThe test ecdsa was done when ecdsa was only supported by hardware. So it wasn't possible to test ecdsa on sandbox, and there is a test to check that ecdsa is not supported on sandbox.
Now, there is a software implementation of ecdsa. So we add a test to verify thatecdsa_verifymay be used on sandbox.
Quoted 4 lines
Signed-off-by Philippe Reynes <[email protected]>
test/dm/ecdsa.c| 109 ++++++++++++++++++++++++++++++++++++++++++++++++++------1 file changed, 99 insertions(+), 10 deletions(-)
Thanks for the test!
nits / thoughts below
test/dm/ecdsa.c line 12
Quoted 12 lines
test/dm/ecdsa.c +10
-0@@ -3,36 +3,125 @@ +#define CHECK (op) ({ \ + int err = op; \ + if (err < 0) { \ + printf("%s: function %s returns an error (%d)\n", \ + __func__, #op, err); \ + return err; \ + } \ + \ + err; \ + })
We normally handle this short of thing just with a ut_assertok() call. It reports the error number, line number and the code that fails so it should be good enough.
In other words, instead of:
+ CHECK(fdt_create(fdt, size));
you can do:
ut_assertok(fdt_create(fdt, size));
Also this:
+ /* create a fdt with the public key */
+ ut_asserteq(0, create_fdt_with_ecdsa_key(fdt, sizeof(fdt),
name, curve, x, y));
+
should really use utt_assertok() - the asserteq is for when we are checking for a particular integer value. Same with ecdsa_verify()
test/dm/ecdsa.c line 11
Quoted 11 lines
test/dm/ecdsa.c +9
-0@@ -3,36 +3,125 @@ +static int set_fdt_ecdsa_point (char *fdt,char *name,const char *data,size_t len) +{ +char *value =NULL ; +int ret =0 ; + +if (!fdt || !name || !data) { + ret = -EINVAL ; + goto out; + }
name should be const char * since every caller passes a string literal. The !fdt / !name / !data check is dead code but OK if you want to keep it.
test/dm/ecdsa.c line 9
Quoted 9 lines
test/dm/ecdsa.c +7
-0@@ -3,36 +3,125 @@ + value =malloc (len /2 ); +if (!value) { + ret = -ENOMEM ; + goto out; + } + + ret =hex2bin (value, data, len /2 );
Passing the hex-string length and dividing by two inside the function is an odd interface - the binary length is what actually matters. I'd rather you took the binary length (or just took data and called strlen() internally) so the /2 doesn't appear three times. What do you think?
test/dm/ecdsa.c line 9
Quoted 9 lines
test/dm/ecdsa.c +7
-0@@ -3,36 +3,125 @@ +if (ret) + goto out; + + out: +free (value); +return ret; +}
Label should sit in column 1, i.e. with no space before
test/dm/ecdsa.c line 6
Quoted 6 lines
test/dm/ecdsa.c +4
-0@@ -3,36 +3,125 @@ +const char *r ="EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716" ; +const char *s ="F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8" ; +u8 sig[64 ]; +char fdt[FDT_MAX_SIZE ];
You could put those in a shared header? Also please use lower-case hex if you can.
test/dm/ecdsa.c line 5
Quoted 5 lines
test/dm/ecdsa.c +3
-0@@ -3,36 +3,125 @@ +/* prepare the signature */ +ut_asserteq (0 ,hex2bin (sig +0 , r,strlen (r) /2 )); +ut_asserteq (0 ,hex2bin (sig +32 , s,strlen (s) /2 ));
Stray double space in sig + 0
Quoted 5 lines
test/dm/ecdsa.c +0
-0@@ -3,36 +3,125 @@/* * Basic test of the ECDSA uclass and ecdsa_verify() *
test/dm/ecdsa.c line 9
Quoted 10 lines
test/dm/ecdsa.c +3
-6-* ECDSA implementations in u-boot are hardware-dependent. Until we have a -* software implementation that can be compiled into the sandbox, all we can -* test is the uclass support. +* ECDSA software implementation is tested in another test, +* so we only check that the class UCLASS_ECDSA may be used. * -* The uclass_get() test is redundant since ecdsa_verify() would also fail. We -* run both functions in order to isolate the cause more clearly. i.e. is -* ecdsa_verify() failing because the UCLASS is absent/broken? +* The data used in this test come from RFC6979
Nit: 'the class UCLASS_ECDSA' reads oddly - 'the UCLASS_ECDSA uclass' is the usual phrasing. Also please mention which curve/vector (P-256, SHA-256 sample) so a future reader knows where the magic hex came from without chasing the RFC.
Regards,
Simon
Re: [PATCH v7 06/15] test: dm: ecdsa.c: clean this test as software ecdsa is now implemented
In reply to Philippe ReynesHi Philippe,
Quoted 27 lines · Philippe Reynes
The test ecdsa was done when ecdsa was only supported by hardware. So it wasn't possible to test ecdsa on sandbox, and there is a test to check that ecdsa is not supported on sandbox.
Now, there is a software implementation of ecdsa. So we add a test to verify thatecdsa_verifymay be used on sandbox.Signed-off-by Philippe Reynes <[email protected]>---
v2:
- initial version
v3:
- no change
v4:
- add a test that useecdsa_verify
v5:
- change secp256r1 to ecdsa256
- check that checksum and crypto are not null v6:
- update commit message
- removefdt_strerrorin macro CHECK
- return ENOMEM when malloc fails
- add a defineFDT_MAX_SIZEto avoid hardcoded value v7:
- changeuint8_tto u8
- setrequired_keynodeto -1
Quoted 3 lines
test/dm/ecdsa.c | 109 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 99 insertions(+), 10 deletions(-)
Thanks and regards,
Raymond
Quoted 143 lines
test/dm/ecdsa.c +99
-11@@ -3,36 +3,125 @@#include <crypto/ecdsa-uclass.h> #include <dm.h> #include <dm/test.h> +#include <malloc.h> #include <test/ut.h> #include <u-boot/ecdsa.h> +#define FDT_MAX_SIZE 512 + +#define CHECK (op) ({ \ + int err = op; \ + if (err < 0) { \ + printf("%s: function %s returns an error (%d)\n", \ + __func__, #op, err); \ + return err; \ + } \ + \ + err; \ + }) + +static int set_fdt_ecdsa_point (char *fdt,char *name,const char *data,size_t len) +{ +char *value =NULL ; +int ret =0 ; + +if (!fdt || !name || !data) { + ret = -EINVAL ; + goto out; + } + + value =malloc (len /2 ); +if (!value) { + ret = -ENOMEM ; + goto out; + } + + ret =hex2bin (value, data, len /2 ); +if (ret) + goto out; + + ret =fdt_property (fdt, name, value, len /2 ); +if (ret) + goto out; + + out: +free (value); +return ret; +} + +static int create_fdt_with_ecdsa_key (char *fdt,size_t size, +const char *name,const char *curve, +const char *x,const char *y) +{ +CHECK (fdt_create (fdt, size)); +CHECK (fdt_finish_reservemap (fdt)); +CHECK (fdt_begin_node (fdt,"" )); +CHECK (fdt_begin_node (fdt,"signature" )); +CHECK (fdt_begin_node (fdt, name)); +CHECK (fdt_property_string (fdt,"algo" ,"sha256,ecdsa256" )); +CHECK (set_fdt_ecdsa_point (fdt,"ecdsa,y-point" , y,strlen (y))); +CHECK (set_fdt_ecdsa_point (fdt,"ecdsa,x-point" , x,strlen (x))); +CHECK (fdt_property_string (fdt,"ecdsa,curve" , curve)); +CHECK (fdt_property_string (fdt,"key-name-hint" , name)); +CHECK (fdt_end_node (fdt));/* name */ +CHECK (fdt_end_node (fdt));/* "signature" */ +CHECK (fdt_end_node (fdt));/* "" */ +CHECK (fdt_finish (fdt)); +CHECK (fdt_pack (fdt)); + +return 0 ; +} +/* * Basic test of the ECDSA uclass and ecdsa_verify() * -* ECDSA implementations in u-boot are hardware-dependent. Until we have a -* software implementation that can be compiled into the sandbox, all we can -* test is the uclass support. +* ECDSA software implementation is tested in another test, +* so we only check that the class UCLASS_ECDSA may be used. * -* The uclass_get() test is redundant since ecdsa_verify() would also fail. We -* run both functions in order to isolate the cause more clearly. i.e. is -* ecdsa_verify() failing because the UCLASS is absent/broken? +* The data used in this test come from RFC6979 */ static int dm_test_ecdsa_verify (struct unit_test_state *uts) {struct uclass *ucp; +const char *full_name ="sha256,ecdsa256" ; +const char *name ="key-ecdsa-256" ; +const char *curve ="prime256v1" ; +const char *x ="60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6" ; +const char *y ="7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299" ; +const char *r ="EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716" ; +const char *s ="F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8" ; +u8 sig[64 ]; +char fdt[FDT_MAX_SIZE ]; -struct checksum_algo algo = { - .checksum_len =256 , +struct image_region region[] = { + { + .data ="sample" , + .size =strlen ("sample" ), + }, };struct image_sign_info info = { - .checksum = &algo, + .checksum =image_get_checksum_algo (full_name), + .crypto =image_get_crypto_algo (full_name), + .required_keynode =-1 , + .fdt_blob = fdt, }; +ut_assertnonnull (info.checksum); +ut_assertnonnull (info.crypto); + +/* create a fdt with the public key */ +ut_asserteq (0 ,create_fdt_with_ecdsa_key (fdt,sizeof (fdt), name, curve, x, y)); + +/* prepare the signature */ +ut_asserteq (0 ,hex2bin (sig +0 , r,strlen (r) /2 )); +ut_asserteq (0 ,hex2bin (sig +32 , s,strlen (s) /2 )); +ut_assertok (uclass_get (UCLASS_ECDSA , &ucp));ut_assertnonnull (ucp); -ut_asserteq (-ENODEV ,ecdsa_verify (&info,NULL ,0 ,NULL ,0 )); +ut_asserteq (0 ,ecdsa_verify (&info, region,1 , sig,sizeof (sig)));return 0 ; } +DM_TEST (dm_test_ecdsa_verify,UTF_SCAN_PDATA |UTF_SCAN_FDT ); --2.43.0
PATCH v7 7/15 test: py: vboot: prepare integration test for ecdsa
In reply to Philippe ReynesThe vboot tests only consider rsa algo for signature. To prepare the integration of ecdsa test, the signature algo is now explicit.
---
v2:
- initial version
v3:
- no change
v4:
- no change
v5:
- no change
v6:
- no change
v7:
- no change
test/py/tests/test_fit_ecdsa.py | 2 +-
test/py/tests/test_vboot.py | 99 ++++++++++---------
....its => sign-configs-sha1-rsa2048-pss.its} | 0
...sha1.its => sign-configs-sha1-rsa2048.its} | 0
... sign-configs-sha256-rsa2048-pss-prod.its} | 0
...ts => sign-configs-sha256-rsa2048-pss.its} | 0
...56.its => sign-configs-sha256-rsa2048.its} | 0
...84.its => sign-configs-sha384-rsa3072.its} | 0
...s.its => sign-images-sha1-rsa2048-pss.its} | 0
...-sha1.its => sign-images-sha1-rsa2048.its} | 0
...its => sign-images-sha256-rsa2048-pss.its} | 0
...256.its => sign-images-sha256-rsa2048.its} | 0
...384.its => sign-images-sha384-rsa3072.its} | 0
13 files changed, 51 insertions(+), 50 deletions(-)
rename test/py/tests/vboot/{sign-configs-sha1-pss.its => sign-configs-sha1-rsa2048-pss.its} (100%)
rename test/py/tests/vboot/{sign-configs-sha1.its => sign-configs-sha1-rsa2048.its} (100%)
rename test/py/tests/vboot/{sign-configs-sha256-pss-prod.its => sign-configs-sha256-rsa2048-pss-prod.its} (100%)
rename test/py/tests/vboot/{sign-configs-sha256-pss.its => sign-configs-sha256-rsa2048-pss.its} (100%)
rename test/py/tests/vboot/{sign-configs-sha256.its => sign-configs-sha256-rsa2048.its} (100%)
rename test/py/tests/vboot/{sign-configs-sha384.its => sign-configs-sha384-rsa3072.its} (100%)
rename test/py/tests/vboot/{sign-images-sha1-pss.its => sign-images-sha1-rsa2048-pss.its} (100%)
rename test/py/tests/vboot/{sign-images-sha1.its => sign-images-sha1-rsa2048.its} (100%)
rename test/py/tests/vboot/{sign-images-sha256-pss.its => sign-images-sha256-rsa2048-pss.its} (100%)
rename test/py/tests/vboot/{sign-images-sha256.its => sign-images-sha256-rsa2048.its} (100%)
rename test/py/tests/vboot/{sign-images-sha384.its => sign-images-sha384-rsa3072.its} (100%)test/py/tests/test_fit_ecdsa.py +1 -1
@@ -102,7 +102,7 @@ def test_fit_ecdsa(ubman): with open(key_file, 'w') as f: f.write(key.export_key(format='PEM')) - assemble_fit_image(fit_file, f'{datadir}/sign-images-sha256.its', tempdir) + assemble_fit_image(fit_file, f'{datadir}/sign-images-sha256-rsa2048.its', tempdir) fit = SignableFitImage(ubman, fit_file) nodes = fit.find_signable_image_nodes()
test/py/tests/test_vboot.py +50 -49
198 lines not on this page · raw message
test/py/tests/vboot/sign-configs-sha1-rsa2048-pss.its renamed from test/py/tests/vboot/sign-configs-sha1-pss.its +0 -0
test/py/tests/vboot/sign-configs-sha1-rsa2048.its renamed from test/py/tests/vboot/sign-configs-sha1.its +0 -0
test/py/tests/vboot/sign-configs-sha256-rsa2048-pss-prod.its renamed from test/py/tests/vboot/sign-configs-sha256-pss-prod.its +0 -0
test/py/tests/vboot/sign-configs-sha256-rsa2048-pss.its renamed from test/py/tests/vboot/sign-configs-sha256-pss.its +0 -0
test/py/tests/vboot/sign-configs-sha256-rsa2048.its renamed from test/py/tests/vboot/sign-configs-sha256.its +0 -0
test/py/tests/vboot/sign-configs-sha384-rsa3072.its renamed from test/py/tests/vboot/sign-configs-sha384.its +0 -0
test/py/tests/vboot/sign-images-sha1-rsa2048-pss.its renamed from test/py/tests/vboot/sign-images-sha1-pss.its +0 -0
test/py/tests/vboot/sign-images-sha1-rsa2048.its renamed from test/py/tests/vboot/sign-images-sha1.its +0 -0
test/py/tests/vboot/sign-images-sha256-rsa2048-pss.its renamed from test/py/tests/vboot/sign-images-sha256-pss.its +0 -0
test/py/tests/vboot/sign-images-sha256-rsa2048.its renamed from test/py/tests/vboot/sign-images-sha256.its +0 -0
test/py/tests/vboot/sign-images-sha384-rsa3072.its renamed from test/py/tests/vboot/sign-images-sha384.its +0 -0
Signature · 1 line
2.43.0
PATCH v7 8/15 test: vboot: add test for ecdsa
In reply to Philippe ReynesThis commit adds test case for ecdsa on fit, but not (yet) for the global image signature (preload).
---
v2:
- initial version
v3:
- no change
v4:
- no change
v5:
- no change
v6:
- no change
v7:
- no change
test/py/tests/test_vboot.py | 29 ++++++++++++ .../vboot/sign-configs-sha256-ecdsa256.its | 45 +++++++++++++++++++ .../vboot/sign-configs-sha256-ecdsa384.its | 45 +++++++++++++++++++ .../vboot/sign-configs-sha256-ecdsa521.its | 45 +++++++++++++++++++ .../vboot/sign-images-sha256-ecdsa256.its | 42 +++++++++++++++++ .../vboot/sign-images-sha256-ecdsa384.its | 42 +++++++++++++++++ .../vboot/sign-images-sha256-ecdsa521.its | 42 +++++++++++++++++ 7 files changed, 290 insertions(+) create mode 100644 test/py/tests/vboot/sign-configs-sha256-ecdsa256.its create mode 100644 test/py/tests/vboot/sign-configs-sha256-ecdsa384.its create mode 100644 test/py/tests/vboot/sign-configs-sha256-ecdsa521.its create mode 100644 test/py/tests/vboot/sign-images-sha256-ecdsa256.its create mode 100644 test/py/tests/vboot/sign-images-sha256-ecdsa384.its create mode 100644 test/py/tests/vboot/sign-images-sha256-ecdsa521.its
test/py/tests/test_vboot.py +29 -0
@@ -94,6 +94,9 @@ TESTDATA_IN = [ ['sha256-pss-pad', 'sha256', '-rsa2048', '-pss', '-E -p 0x10000', False, False, False, False], ['sha256-pss-required', 'sha256', '-rsa2048', '-pss', None, True, False, False, False], ['sha256-pss-pad-required', 'sha256', '-rsa2048', '-pss', '-E -p 0x10000', True, True, False, False], + ['sha256-basic-ecdsa256', 'sha256', '-ecdsa256', '', None, False, False, False, False], + ['sha256-basic-ecdsa384', 'sha256', '-ecdsa384', '', None, False, False, False, False], + ['sha256-basic-ecdsa521', 'sha256', '-ecdsa521', '', None, False, False, False, False], ['sha384-basic', 'sha384', '-rsa3072', '', None, False, False, False, False], ['sha384-pad', 'sha384', '-rsa3072', '', '-E -p 0x10000', False, False, False, False], ['algo-arg', 'algo-arg', '', '', '-o sha256,rsa2048', False, False, True, False],
@@ -287,6 +290,29 @@ def test_vboot(ubman, name, sha_algo, sig_algo, padding, sign_options, required, utils.run_and_log(ubman, 'openssl req -batch -new -x509 -key %s%s.key ' '-out %s%s.crt' % (tmpdir, name, tmpdir, name)) + def create_ecdsa_pair(name): + """Generate a new ECDSA key pair + + Args: + name: Name of the key (e.g. 'dev') + """ + + if sig_algo == "-ecdsa256": + curve_name = "secp256r1" + elif sig_algo == "-ecdsa384": + curve_name = "secp384r1" + elif sig_algo == "-ecdsa521": + curve_name = "secp521r1" + else: + curve_name = "unknownCurve" + + utils.run_and_log(ubman, 'openssl ecparam -name %s -genkey -noout -out %s%s.pem' % + (curve_name, tmpdir, name)) + + # Create a certificate containing the public key + utils.run_and_log(ubman, 'openssl req -batch -new -x509 -key %s%s.pem ' + '-out %s%s.crt' % (tmpdir, name, tmpdir, name)) + def test_with_algo(sha_algo, sig_algo, padding, sign_options): """Test verified boot with the given hash algorithm.
@@ -537,6 +563,9 @@ def test_vboot(ubman, name, sha_algo, sig_algo, padding, sign_options, required, if sig_algo == "-rsa2048" or sig_algo == "-rsa3072" or sig_algo == "": create_rsa_pair('dev') create_rsa_pair('prod') + elif sig_algo == "-ecdsa256" or sig_algo == "-ecdsa384" or sig_algo == "-ecdsa521": + create_ecdsa_pair('dev') + create_ecdsa_pair('prod') # Create a number kernel image with zeroes with open('%stest-kernel.bin' % tmpdir, 'wb') as fd:
test/py/tests/vboot/sign-configs-sha256-ecdsa256.its new file +45 -0
45 lines not on this page · raw message
test/py/tests/vboot/sign-configs-sha256-ecdsa384.its new file +45 -0
45 lines not on this page · raw message
test/py/tests/vboot/sign-configs-sha256-ecdsa521.its new file +45 -0
45 lines not on this page · raw message
test/py/tests/vboot/sign-images-sha256-ecdsa256.its new file +42 -0
42 lines not on this page · raw message
test/py/tests/vboot/sign-images-sha256-ecdsa384.its new file +42 -0
42 lines not on this page · raw message
test/py/tests/vboot/sign-images-sha256-ecdsa521.its new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v7 9/15 tools: fit_image_setup_sig: set required_keynode to -1
In reply to Philippe ReynesWhen initializing info about a signature, set required_keynode to -1 instead of 0, as 0 is a valid keynode offset.
---
v5:
- initial version
v6:
- no change
v7:
- change info->required_keynode > 0 to info->required_keynode >= 0 in ecdsa_verify_hash
lib/ecdsa/ecdsa-verify.c | 2 +- tools/image-host.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
lib/ecdsa/ecdsa-verify.c +1 -1
@@ -111,7 +111,7 @@ static int ecdsa_verify_hash(struct udevice *dev,if (!ops || !ops->verify)return -ENODEV ; -if (info->required_keynode >0 ) { +if (info->required_keynode >=0 ) { ret =fdt_get_key (&key, info->fdt_blob, info->required_keynode);if (ret <0 )return ret;
tools/image-host.c +1 -0
@@ -198,6 +198,7 @@ static int fit_image_setup_sig(struct image_sign_info *info, info->checksum =image_get_checksum_algo (algo_name); info->crypto =image_get_crypto_algo (algo_name); info->padding =image_get_padding_algo (padding_name); + info->required_keynode =-1 ; info->require_keys = require_keys; info->engine_id = engine_id;if (!info->checksum || !info->crypto) {
Signature · 1 line
2.43.0
Re: [PATCH v7 09/15] tools: fit_image_setup_sig: set required_keynode to -1
Hi Philippe,
Quoted 15 lines · Philippe Reynes
When initializing info about a signature, set
required_keynodeto -1 instead of 0, as 0 is a valid keynode offset.Reviewed-by Simon Glass <[email protected]>Signed-off-by Philippe Reynes <[email protected]>---
v5:
- initial version
v6:
- no change
v7:
- changeinfo->required_keynode> 0 toinfo->required_keynode>= 0 inecdsa_verify_hash
Quoted 4 lines
lib/ecdsa/ecdsa-verify.c | 2 +- tools/image-host.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
Thanks and regards,
Raymond
Quoted 28 lines
lib/ecdsa/ecdsa-verify.c +1
-1@@ -111,7 +111,7 @@ static int ecdsa_verify_hash(struct udevice *dev,if (!ops || !ops->verify)return -ENODEV ; -if (info->required_keynode >0 ) { +if (info->required_keynode >=0 ) { ret =fdt_get_key (&key, info->fdt_blob, info->required_keynode);if (ret <0 )return ret;tools/image-host.c +1
-1@@ -198,6 +198,7 @@ static int fit_image_setup_sig(struct image_sign_info *info, info->checksum =image_get_checksum_algo (algo_name); info->crypto =image_get_crypto_algo (algo_name); info->padding =image_get_padding_algo (padding_name); + info->required_keynode =-1 ; info->require_keys = require_keys; info->engine_id = engine_id;if (!info->checksum || !info->crypto) { --2.43.0
PATCH v7 10/15 tools: mkimage: pre-load: add support of ecdsa
In reply to Philippe ReynesRight now, mkimage can only create pre-load header using rsa. We add the support of ecdsa.
---
v3:
- initial version
v4:
- use pre_load_noffset (do not compute it again)
- release memory allocated with strdup
- clean code
v5:
- check checksum and algo before using them v6:
- no change
v6:
- no change
lib/ecdsa/ecdsa-libcrypto.c | 29 +++++++++--- tools/image-host.c | 92 ++++++++++++++++++++++++++++++++----- 2 files changed, 103 insertions(+), 18 deletions(-)
lib/ecdsa/ecdsa-libcrypto.c +22 -7
@@ -507,14 +507,9 @@ int ecdsa_verify(struct image_sign_info *info,return ret; } -static int do_add (struct signer *ctx,void *fdt,const char *key_node_name, -struct image_sign_info *info) +static int search_key_node (void *fdt,const char *key_node_name) { -int signature_node, key_node, ret, key_bits; -const char *curve_name; -const EC_GROUP *group; -const EC_POINT *point; -BIGNUM *x, *y; +int signature_node, key_node; signature_node =fdt_subnode_offset (fdt,0 ,FIT_SIG_NODENAME );if (signature_node == -FDT_ERR_NOTFOUND ) {
@@ -549,6 +544,26 @@ static int do_add(struct signer *ctx, void *fdt, const char *key_node_name,return key_node; } +return key_node; +} + +static int do_add (struct signer *ctx,void *fdt,const char *key_node_name, +struct image_sign_info *info) +{ +int key_node, ret, key_bits; +const char *curve_name; +const EC_GROUP *group; +const EC_POINT *point; +BIGNUM *x, *y; + +if (info->required_keynode >=0 ) { + key_node = info->required_keynode; + }else { + key_node =search_key_node (fdt, key_node_name); +if (key_node <0 ) +return key_node; + } + group =EC_KEY_get0_group (ctx->ecdsa_key); key_bits =EC_GROUP_order_bits (group); curve_name =OBJ_nid2sn (EC_GROUP_get_curve_name (group));
tools/image-host.c +81 -11
117 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v7 11/15 tools: binman: pre-load: add support of ecdsa
In reply to Philippe ReynesRight now, binman can only create pre-load header
using rsa. We add the support of ecdsa.
---
v3:
- initial version
v4:
- merge patch 11 that was adding test for ecdsa pre-load
- add key size check
- use exc instead of simply e
- rename dts filaneme
- add a test to check key size
v5:
- compute ecdsa521 sig instead of using hardcoded value 132
- fix english: don't -> doesn't
- avoid line too long
v6:
- no change
v7:
- no change
tools/binman/etype/pre_load.py | 78 ++++++++++++++++--- tools/binman/ftest.py | 50 ++++++++++++ tools/binman/test/ecdsa521.pem | 7 ++ tools/binman/test/security/pre_load_ecdsa.dts | 22 ++++++ .../security/pre_load_ecdsa_invalid_algo.dts | 22 ++++++ .../security/pre_load_ecdsa_invalid_key.dts | 22 ++++++ .../security/pre_load_ecdsa_invalid_sha.dts | 22 ++++++ 7 files changed, 214 insertions(+), 9 deletions(-) create mode 100644 tools/binman/test/ecdsa521.pem create mode 100644 tools/binman/test/security/pre_load_ecdsa.dts create mode 100644 tools/binman/test/security/pre_load_ecdsa_invalid_algo.dts create mode 100644 tools/binman/test/security/pre_load_ecdsa_invalid_key.dts create mode 100644 tools/binman/test/security/pre_load_ecdsa_invalid_sha.dts
tools/binman/etype/pre_load.py +69 -9
@@ -16,8 +16,10 @@ from binman.entry import EntryArg from Cryptodome.Hash import SHA256, SHA384, SHA512 from Cryptodome.PublicKey import RSA +from Cryptodome.PublicKey import ECC from Cryptodome.Signature import pkcs1_15 from Cryptodome.Signature import pss +from Cryptodome.Signature import DSS PRE_LOAD_MAGIC = b'UBSH'
@@ -27,6 +29,12 @@ RSAS = { 'rsa4096': 4096 / 8 } +ECDSAS = { + 'ecdsa256': 256 / 8 * 2, + 'ecdsa384': 384 / 8 * 2, + 'ecdsa521': (521 + 7) / 8 * 2 +} + SHAS = { 'sha256': SHA256, 'sha384': SHA384,
@@ -86,24 +94,17 @@ class Entry_pre_load(Entry_collection): if self.key_path is None: self.key_path = '' - def _CreateHeader(self): - """Create a pre load header""" - hash_name, sign_name = self.algo_name.split(',') - padding_name = self.padding_name - key_name = os.path.join(self.key_path, self.key_name) - + def _CreateHeaderRsa(self, hash_name, sign_name, padding_name, key_name): # Check hash and signature name/type if hash_name not in SHAS: self.Raise(hash_name + " is not supported") - if sign_name not in RSAS: - self.Raise(sign_name + " is not supported") # Read the key key = RSA.import_key(tools.read_file(key_name)) # Check if the key has the expected size if key.size_in_bytes() != RSAS[sign_name]: - self.Raise("The key " + self.key_name + " don't have the expected size") + self.Raise("The key " + self.key_name + " doesn't have the expected size") # Compute the hash hash_image = SHAS[hash_name].new()
@@ -151,6 +152,65 @@ class Entry_pre_load(Entry_collection): return data + pad + def _CreateHeaderEcdsa(self, hash_name, sign_name, key_name): + # Check hash and signature name/type + if hash_name not in SHAS: + self.Raise(hash_name + " is not supported") + + # Read the key + key = ECC.import_key(tools.read_file(key_name)) + + # Check if the key has the expected size + if key.pointQ.size_in_bytes() * 2 != ECDSAS[sign_name]: + self.Raise("The key " + self.key_name + " doesn't have the expected size") + + # Compute the hash + hash_image = SHAS[hash_name].new() + hash_image.update(self.image) + + # Compute the signature + signer = DSS.new(key, 'fips-186-3') + sig = signer.sign(hash_image) + + hash_sig = SHA256.new() + hash_sig.update(sig) + + version = self.version + header_size = self.header_size + image_size = len(self.image) + ofs_img_sig = 64 + len(sig) + flags = 0 + reserved0 = 0 + reserved1 = 0 + + first_header = struct.pack('>4sIIIIIII32s', PRE_LOAD_MAGIC, + version, header_size, image_size, + ofs_img_sig, flags, reserved0, + reserved1, hash_sig.digest()) + + hash_first_header = SHAS[hash_name].new() + hash_first_header.update(first_header) + sig_first_header = signer.sign(hash_first_header) + + data = first_header + sig_first_header + sig + pad = bytearray(self.header_size - len(data)) + + return data + pad + + def _CreateHeader(self): + """Create a pre load header""" + hash_name, sign_name = self.algo_name.split(',') + padding_name = self.padding_name + key_name = os.path.join(self.key_path, self.key_name) + + if sign_name in RSAS: + return self._CreateHeaderRsa(hash_name, sign_name, padding_name, key_name) + + if sign_name in ECDSAS: + return self._CreateHeaderEcdsa(hash_name, sign_name, key_name) + + self.Raise(sign_name + " is not supported") + def ObtainContents(self): """Create a placeholder for the header""" self.SetContents(tools.get_bytes(0, self.header_size))
tools/binman/ftest.py +50 -0
62 lines not on this page · raw message
tools/binman/test/ecdsa521.pem new file +7 -0
@@ -0,0 +1,7 @@ +-----BEGIN EC PRIVATE KEY----- +MIHcAgEBBEIBM+CNnraGci2/mw1wPq44l2HccHnoBbdP3DiU6zqsBOq8IR8uegz2 +FLzWsjxcW7hwROCdEm6tW99wqsyPE25RZ3egBwYFK4EEACOhgYkDgYYABABu5bWV +aQ4EgnXFjojX9df3gBEBipphEEFAoG87GuoWBdlimFC8UEEXiKNU37w0wlJn4bG0 +8uOKwDqBk3uF+DrmZwB45lCSKkjdRWsJeDt+iEuFe2O/mbXoL4p5D8MM2OsDV5GT +srUbxhXq+T/i5lV7XXm2+tT/7zU8ZQce6WRufbd9KQ== +-----END EC PRIVATE KEY-----
tools/binman/test/security/pre_load_ecdsa.dts new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + pre-load { + content = <&image>; + algo-name = "sha256,ecdsa521"; + key-name = "ecdsa521.pem"; + header-size = <4096>; + version = <0x11223344>; + }; + + image: blob-ext { + filename = "refcode.bin"; + }; + }; +};
tools/binman/test/security/pre_load_ecdsa_invalid_algo.dts new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + pre-load { + content = <&image>; + algo-name = "sha256,ecdsa5210"; + key-name = "ecdsa521.pem"; + header-size = <4096>; + version = <0x11223344>; + }; + + image: blob-ext { + filename = "refcode.bin"; + }; + }; +};
tools/binman/test/security/pre_load_ecdsa_invalid_key.dts new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + pre-load { + content = <&image>; + algo-name = "sha256,ecdsa384"; + key-name = "ecdsa521.pem"; + header-size = <4096>; + version = <0x11223344>; + }; + + image: blob-ext { + filename = "refcode.bin"; + }; + }; +};
tools/binman/test/security/pre_load_ecdsa_invalid_sha.dts new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + pre-load { + content = <&image>; + algo-name = "sha2560,ecdsa521"; + key-name = "ecdsa521.pem"; + header-size = <4096>; + version = <0x11223344>; + }; + + image: blob-ext { + filename = "refcode.bin"; + }; + }; +};
Signature · 1 line
2.43.0
PATCH v7 12/15 boot: pre-load: add support of ecdsa
In reply to Philippe ReynesRight now, u-boot can only boot image with a pre-load header with rsa. We add the support of ecdsa.
---
v3:
- initial version
v4:
- avoid calling image_get_crypto_algo several times
v5:
- simplify the function is_ecdsa
- re-use already computed crypto in function image_pre_load_sig_setup v6:
- no change
v7:
- no change
boot/image-pre-load.c | 53 +++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 12 deletions(-)
boot/image-pre-load.c +41 -12
@@ -70,6 +70,12 @@ static int image_pre_load_sig_setup(struct image_sig_info *info) return 0; }#else + +static int is_ecdsa (struct crypto_algo *crypto) +{ +return crypto && !strncmp (crypto->name,"ecdsa" ,5 ); +} +/* * This function gathers information about the signature check * that could be done before launching the image.
@@ -86,6 +92,7 @@ static int image_pre_load_sig_setup(struct image_sig_info *info)int key_len;int node, ret =0 ;char *sig_info_path =NULL ; +struct crypto_algo *crypto;if (!info) {log_err ("ERROR: info is NULL for image pre-load sig check\n" );
@@ -114,11 +121,24 @@ static int image_pre_load_sig_setup(struct image_sig_info *info) goto out; } - padding_name =fdt_getprop (gd_fdt_blob (), node, -IMAGE_PRE_LOAD_PROP_PADDING_NAME ,NULL ); -if (!padding_name) { -log_info ("INFO: no padding_name provided, so using pkcs-1.5\n" ); - padding_name ="pkcs-1.5" ; + crypto =image_get_crypto_algo (algo_name); +if (!crypto) { +printf ("ERROR: can't find a valid crypto algo from %s\n" , + (char *)algo_name); + ret = -EINVAL ; + goto out; + } + +if (is_ecdsa (crypto)) { + padding_name =NULL ; + }else { + padding_name =fdt_getprop (gd_fdt_blob (), node, +IMAGE_PRE_LOAD_PROP_PADDING_NAME , +NULL ); +if (!padding_name) { +log_info ("INFO: no padding_name provided, so using pkcs-1.5\n" ); + padding_name ="pkcs-1.5" ; + } } sig_size = fdt_getprop(gd_fdt_blob(), node,
@@ -129,12 +149,17 @@ static int image_pre_load_sig_setup(struct image_sig_info *info) goto out; } - key =fdt_getprop (gd_fdt_blob (), node, -IMAGE_PRE_LOAD_PROP_PUBLIC_KEY , &key_len); -if (!key) { -log_err ("ERROR: no key for image pre-load sig check\n" ); - ret = -EINVAL ; - goto out; +if (is_ecdsa (crypto)) { + key =NULL ; + key_len =0 ; + }else { + key =fdt_getprop (gd_fdt_blob (), node, +IMAGE_PRE_LOAD_PROP_PUBLIC_KEY , &key_len); +if (!key) { +log_err ("ERROR: no key for image pre-load sig check\n" ); + ret = -EINVAL ; + goto out; + } } info->algo_name = (char *)algo_name;
@@ -152,9 +177,13 @@ static int image_pre_load_sig_setup(struct image_sig_info *info) info->sig_info.name = info->algo_name; info->sig_info.padding =image_get_padding_algo (info->padding_name); info->sig_info.checksum =image_get_checksum_algo (info->sig_info.name); - info->sig_info.crypto =image_get_crypto_algo (info->sig_info.name); + info->sig_info.crypto = crypto; info->sig_info.key = info->key; info->sig_info.keylen = info->key_len; +if (is_ecdsa (crypto)) { + info->sig_info.required_keynode = node; + info->sig_info.fdt_blob =gd_fdt_blob (); + } out:return ret;
Signature · 1 line
2.43.0
PATCH v7 13/15 tools: preload_check_sign: add support of ecdsa
In reply to Philippe Reynesright now, the tool preload_check_sign may only checks an image with a pre-load header with rsa. We add the support of pre-load header with ecdsa.
---
v3:
- initial version
v4:
- free key to avoid mem leak
- fix error management (set ret before goto out) v5:
- add include ec.h, evp.h, err.h and image.h v6:
- no change
v7:
- no change
tools/preload_check_sign.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
tools/preload_check_sign.c +30 -0
@@ -8,9 +8,13 @@* complete file. The tool preload_check_sign allows to verify and authenticate * a file starting with a preload header. */ + +#define OPENSSL_API_COMPAT 0x10101000L +#include <stdio.h> #include <unistd.h> #include <openssl/pem.h> +#include <openssl/ec.h> #include <openssl/evp.h> #include <openssl/err.h> #include <image.h>
@@ -144,6 +148,32 @@ int main(int argc, char **argv) info.sig_info.key = info.key; info.sig_info.keylen = info.key_len; +/* For ecdsa key, we have to update some values */ +if (EVP_PKEY_id (pkey) ==EVP_PKEY_EC ) { +EC_KEY *ecdsa_key; +const EC_GROUP *group; + + ecdsa_key =EVP_PKEY_get1_EC_KEY (pkey); +if (!ecdsa_key) { +fprintf (stderr,"Can not extract ECDSA key\n" ); + ret =EXIT_FAILURE ; + goto out; + } + + group =EC_KEY_get0_group (ecdsa_key); +if (!group) { +fprintf (stderr,"Can not extract ECDSA group\n" ); +EC_KEY_free (ecdsa_key); + ret =EXIT_FAILURE ; + goto out; + } + + info.sig_info.keyfile = keyfile; + info.sig_size = (EC_GROUP_order_bits (group) +7 ) /8 *2 ; + +EC_KEY_free (ecdsa_key); + } +/* Check the signature */ image_pre_load_sig_set_info (&info); ret =image_pre_load_sig ((ulong )buffer);
Signature · 1 line
2.43.0
PATCH v7 14/15 test: py: vboot: prepare test for global signature with ecdsa
In reply to Philippe ReynesThe vboot tests only consider rsa for test with global signature. To prepare the integration of test with ecdsa test, the signature algo is now explicit.
---
v3:
- initial version
v4:
- no change
v5:
- no change
v6:
- no change
v7:
- no change
test/py/tests/test_vboot.py | 12 ++++++------
...binman-pss.dts => sandbox-binman-rsa2048-pss.dts} | 0
...sandbox-binman.dts => sandbox-binman-rsa2048.dts} | 0
...pss.dts => sandbox-u-boot-global-rsa2048-pss.dts} | 0
...-global.dts => sandbox-u-boot-global-rsa2048.dts} | 0
5 files changed, 6 insertions(+), 6 deletions(-)
rename test/py/tests/vboot/{sandbox-binman-pss.dts => sandbox-binman-rsa2048-pss.dts} (100%)
rename test/py/tests/vboot/{sandbox-binman.dts => sandbox-binman-rsa2048.dts} (100%)
rename test/py/tests/vboot/{sandbox-u-boot-global-pss.dts => sandbox-u-boot-global-rsa2048-pss.dts} (100%)
rename test/py/tests/vboot/{sandbox-u-boot-global.dts => sandbox-u-boot-global-rsa2048.dts} (100%)test/py/tests/test_vboot.py +6 -6
@@ -503,7 +503,7 @@ def test_vboot(ubman, name, sha_algo, sig_algo, padding, sign_options, required, dtb) run_bootm(sha_algo, 'multi required key', '', False) - def test_global_sign(sha_algo, padding, sign_options): + def test_global_sign(sha_algo, sig_algo, padding, sign_options): """Test global image signature with the given hash algorithm and padding. Args:
@@ -512,14 +512,14 @@ def test_vboot(ubman, name, sha_algo, sig_algo, padding, sign_options, required, rsa signature algorithm. """ - dtb = '%ssandbox-u-boot-global%s.dtb' % (tmpdir, padding) + dtb = '%ssandbox-u-boot-global%s%s.dtb' % (tmpdir, sig_algo, padding) ubman.config.dtb = dtb # Compile our device tree files for kernel and U-Boot. These are # regenerated here since mkimage will modify them (by adding a # public key) below. dtc('sandbox-kernel.dts', ubman, dtc_args, datadir, tmpdir, dtb) - dtc_options('sandbox-u-boot-global%s.dts' % padding, '-p 1024') + dtc_options('sandbox-u-boot-global%s%s.dts' % (sig_algo, padding), '-p 1024') # Build the FIT with dev key (keys NOT required). This adds the # signature into sandbox-u-boot.dtb, NOT marked 'required'.
@@ -528,11 +528,11 @@ def test_vboot(ubman, name, sha_algo, sig_algo, padding, sign_options, required, # Build the dtb for binman that define the pre-load header # with the global sigature. - dtc('sandbox-binman%s.dts' % padding, ubman, dtc_args, datadir, tmpdir, dtb) + dtc('sandbox-binman%s%s.dts' % (sig_algo, padding), ubman, dtc_args, datadir, tmpdir, dtb) # Run binman to create the final image with the not signed fit # and the pre-load header that contains the global signature. - run_binman('sandbox-binman%s.dtb' % padding) + run_binman('sandbox-binman%s%s.dtb' % (sig_algo, padding)) # Check that the signature is correctly verified by u-boot run_bootm(sha_algo, 'global image signature',
@@ -582,7 +582,7 @@ def test_vboot(ubman, name, sha_algo, sig_algo, padding, sign_options, required, try: ubman.config.dtb = dtb if global_sign: - test_global_sign(sha_algo, padding, sign_options) + test_global_sign(sha_algo, sig_algo, padding, sign_options) elif required: test_required_key(sha_algo, sig_algo, padding, sign_options) else:
test/py/tests/vboot/sandbox-binman-rsa2048-pss.dts renamed from test/py/tests/vboot/sandbox-binman-pss.dts +0 -0
test/py/tests/vboot/sandbox-binman-rsa2048.dts renamed from test/py/tests/vboot/sandbox-binman.dts +0 -0
test/py/tests/vboot/sandbox-u-boot-global-rsa2048-pss.dts renamed from test/py/tests/vboot/sandbox-u-boot-global-pss.dts +0 -0
test/py/tests/vboot/sandbox-u-boot-global-rsa2048.dts renamed from test/py/tests/vboot/sandbox-u-boot-global.dts +0 -0
Signature · 1 line
2.43.0
PATCH v7 15/15 test: py: vboot: add test for global signature with ecdsa
In reply to Philippe ReynesAs now u-boot supports pre-load header with ecdsa, we add some tests to vboot with ecdsa pre-load header.
---
v3:
- initial version
v4:
- no change
v5:
- no change
v6:
- no change
v7:
- no change
test/py/tests/test_vboot.py | 3 +++ .../tests/vboot/sandbox-binman-ecdsa256.dts | 24 +++++++++++++++++ .../tests/vboot/sandbox-binman-ecdsa384.dts | 24 +++++++++++++++++ .../tests/vboot/sandbox-binman-ecdsa521.dts | 24 +++++++++++++++++ .../vboot/sandbox-u-boot-global-ecdsa256.dts | 27 +++++++++++++++++++ .../vboot/sandbox-u-boot-global-ecdsa384.dts | 27 +++++++++++++++++++ .../vboot/sandbox-u-boot-global-ecdsa521.dts | 27 +++++++++++++++++++ 7 files changed, 156 insertions(+) create mode 100644 test/py/tests/vboot/sandbox-binman-ecdsa256.dts create mode 100644 test/py/tests/vboot/sandbox-binman-ecdsa384.dts create mode 100644 test/py/tests/vboot/sandbox-binman-ecdsa521.dts create mode 100644 test/py/tests/vboot/sandbox-u-boot-global-ecdsa256.dts create mode 100644 test/py/tests/vboot/sandbox-u-boot-global-ecdsa384.dts create mode 100644 test/py/tests/vboot/sandbox-u-boot-global-ecdsa521.dts
test/py/tests/test_vboot.py +3 -0
@@ -102,6 +102,9 @@ TESTDATA_IN = [ ['algo-arg', 'algo-arg', '', '', '-o sha256,rsa2048', False, False, True, False], ['sha256-global-sign', 'sha256', '-rsa2048', '', '', False, False, False, True], ['sha256-global-sign-pss', 'sha256', '-rsa2048', '-pss', '', False, False, False, True], + ['sha256-global-sign-ecdsa256', 'sha256', '-ecdsa256', '', '', False, False, False, True], + ['sha256-global-sign-ecdsa384', 'sha256', '-ecdsa384', '', '', False, False, False, True], + ['sha256-global-sign-ecdsa521', 'sha256', '-ecdsa521', '', '', False, False, False, True], ] # Mark all but the first test as slow, so they are not run with '-k not slow'
test/py/tests/vboot/sandbox-binman-ecdsa256.dts new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + filename = "sandbox.img"; + + pre-load { + content = <&image>; + algo-name = "sha256,ecdsa256"; + key-name = "dev.pem"; + header-size = <4096>; + version = <1>; + }; + + image: blob-ext { + filename = "test.fit"; + }; + }; +};
test/py/tests/vboot/sandbox-binman-ecdsa384.dts new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + filename = "sandbox.img"; + + pre-load { + content = <&image>; + algo-name = "sha256,ecdsa384"; + key-name = "dev.pem"; + header-size = <4096>; + version = <1>; + }; + + image: blob-ext { + filename = "test.fit"; + }; + }; +};
test/py/tests/vboot/sandbox-binman-ecdsa521.dts new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + filename = "sandbox.img"; + + pre-load { + content = <&image>; + algo-name = "sha256,ecdsa521"; + key-name = "dev.pem"; + header-size = <4096>; + version = <1>; + }; + + image: blob-ext { + filename = "test.fit"; + }; + }; +};
test/py/tests/vboot/sandbox-u-boot-global-ecdsa256.dts new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/dts-v1/; + +/ { + model = "Sandbox Verified Boot Test"; + compatible = "sandbox"; + + binman { + }; + + reset@0 { + compatible = "sandbox,reset"; + }; + + image { + pre-load { + sig { + algo-name = "sha256,ecdsa256"; + signature-size = <64>; + mandatory = "yes"; + + key-name = "dev"; + }; + }; + }; +};
test/py/tests/vboot/sandbox-u-boot-global-ecdsa384.dts new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/dts-v1/; + +/ { + model = "Sandbox Verified Boot Test"; + compatible = "sandbox"; + + binman { + }; + + reset@0 { + compatible = "sandbox,reset"; + }; + + image { + pre-load { + sig { + algo-name = "sha256,ecdsa384"; + signature-size = <96>; + mandatory = "yes"; + + key-name = "dev"; + }; + }; + }; +};
test/py/tests/vboot/sandbox-u-boot-global-ecdsa521.dts new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/dts-v1/; + +/ { + model = "Sandbox Verified Boot Test"; + compatible = "sandbox"; + + binman { + }; + + reset@0 { + compatible = "sandbox,reset"; + }; + + image { + pre-load { + sig { + algo-name = "sha256,ecdsa521"; + signature-size = <132>; + mandatory = "yes"; + + key-name = "dev"; + }; + }; + }; +};
Signature · 1 line
2.43.0