Skip to content

Commit 2f045a2

Browse files
romanjoed3zd3z
authored andcommitted
bootutil: Align mbedtls_ecdsa signature verification with cypress mbedtls hw accel implementation
Signed-off-by: Roman Okhrimenko <roman.okhrimenko@cypress.com> Signed-off-by: Roman Okhrimenko <roman.okhrimenko@infineon.com>
1 parent 26edaf3 commit 2f045a2

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

boot/bootutil/src/image_ec256.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,53 @@ static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1;
5151
/*
5252
* Parse the public key used for signing.
5353
*/
54+
#ifdef CY_MBEDTLS_HW_ACCELERATION
55+
static int
56+
bootutil_parse_eckey(mbedtls_ecdsa_context *ctx, uint8_t **p, uint8_t *end)
57+
{
58+
size_t len;
59+
mbedtls_asn1_buf alg;
60+
mbedtls_asn1_buf param;
61+
62+
if (mbedtls_asn1_get_tag(p, end, &len,
63+
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
64+
return -1;
65+
}
66+
end = *p + len;
67+
68+
if (mbedtls_asn1_get_alg(p, end, &alg, &param)) {
69+
return -2;
70+
}
71+
if (alg.len != sizeof(ec_pubkey_oid) - 1 ||
72+
memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) {
73+
return -3;
74+
}
75+
if (param.len != sizeof(ec_secp256r1_oid) - 1||
76+
memcmp(param.p, ec_secp256r1_oid, sizeof(ec_secp256r1_oid) - 1)) {
77+
return -4;
78+
}
79+
80+
if (mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP256R1)) {
81+
return -5;
82+
}
83+
84+
if (mbedtls_asn1_get_bitstring_null(p, end, &len)) {
85+
return -6;
86+
}
87+
if (*p + len != end) {
88+
return -7;
89+
}
90+
91+
if (mbedtls_ecp_point_read_binary(&ctx->grp, &ctx->Q, *p, end - *p)) {
92+
return -8;
93+
}
94+
95+
if (mbedtls_ecp_check_pubkey(&ctx->grp, &ctx->Q)) {
96+
return -9;
97+
}
98+
return 0;
99+
}
100+
#endif /* CY_MBEDTLS_HW_ACCELERATION */
54101
static int
55102
bootutil_import_key(uint8_t **cp, uint8_t *end)
56103
{
@@ -163,7 +210,12 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
163210
pubkey = (uint8_t *)bootutil_keys[key_id].key;
164211
end = pubkey + *bootutil_keys[key_id].len;
165212

213+
#ifdef CY_MBEDTLS_HW_ACCELERATION
214+
mbedtls_ecdsa_init(&ctx);
215+
rc = bootutil_parse_eckey(&ctx, &pubkey, end);
216+
#else
166217
rc = bootutil_import_key(&pubkey, end);
218+
#endif
167219
if (rc) {
168220
return -1;
169221
}
@@ -178,6 +230,13 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
178230
/*
179231
* This is simplified, as the hash length is also 32 bytes.
180232
*/
233+
#ifdef CY_MBEDTLS_HW_ACCELERATION
234+
while (sig[slen - 1] == '\0') {
235+
slen--;
236+
}
237+
rc = mbedtls_ecdsa_read_signature(&ctx, hash, hlen, sig, slen);
238+
239+
#else /* CY_MBEDTLS_HW_ACCELERATION */
181240
if (hlen != NUM_ECC_BYTES) {
182241
return -1;
183242
}
@@ -189,7 +248,10 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
189248
rc = bootutil_ecdsa_p256_verify(&ctx, pubkey, end - pubkey, hash, signature,
190249
2 * NUM_ECC_BYTES);
191250
#endif
251+
#endif /* CY_MBEDTLS_HW_ACCELERATION */
252+
192253
bootutil_ecdsa_p256_drop(&ctx);
254+
193255
return rc;
194256
}
195257

0 commit comments

Comments
 (0)