Skip to content

Commit ae4344b

Browse files
committed
[nrf noup] boot: bootutil: Add required signature decoding
The CC310 and bl_crypto require decoded signature instead of raw ASN.1 Signed-off-by: Dominik Ermel <[email protected]> (cherry picked from commit 51afa7a) (cherry picked from commit 9da6438)
1 parent a49b83e commit ae4344b

File tree

1 file changed

+13
-7
lines changed
  • boot/bootutil/include/bootutil/crypto

1 file changed

+13
-7
lines changed

boot/bootutil/include/bootutil/crypto/ecdsa.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ static int bootutil_import_key(uint8_t **cp, uint8_t *end)
132132
}
133133
#endif /* MCUBOOT_USE_TINYCRYPT || MCUBOOT_USE_MBED_TLS || MCUBOOT_USE_CC310 */
134134

135-
#if defined(MCUBOOT_USE_TINYCRYPT)
136-
#ifndef MCUBOOT_ECDSA_NEED_ASN1_SIG
137135
/*
138136
* cp points to ASN1 string containing an integer.
139137
* Verify the tag, and that the length is 32 bytes. Helper function.
@@ -183,8 +181,8 @@ static int bootutil_decode_sig(uint8_t signature[NUM_ECC_BYTES * 2], uint8_t *cp
183181
}
184182
return 0;
185183
}
186-
#endif /* not MCUBOOT_ECDSA_NEED_ASN1_SIG */
187184

185+
#if defined(MCUBOOT_USE_TINYCRYPT)
188186
typedef uintptr_t bootutil_ecdsa_context;
189187
static inline void bootutil_ecdsa_init(bootutil_ecdsa_context *ctx)
190188
{
@@ -253,16 +251,20 @@ static inline int bootutil_ecdsa_verify(bootutil_ecdsa_context *ctx,
253251
{
254252
(void)ctx;
255253
(void)pk_len;
256-
(void)sig_len;
257254
(void)hash_len;
255+
uint8_t dsig[2 * NUM_ECC_BYTES];
256+
257+
if (bootutil_decode_sig(dsig, sig, sig + sig_len)) {
258+
return -1;
259+
}
258260

259261
/* Only support uncompressed keys. */
260262
if (pk[0] != 0x04) {
261263
return -1;
262264
}
263265
pk++;
264266

265-
return cc310_ecdsa_verify_secp256r1(hash, pk, sig, BOOTUTIL_CRYPTO_ECDSA_P256_HASH_SIZE);
267+
return cc310_ecdsa_verify_secp256r1(hash, pk, dsig, BOOTUTIL_CRYPTO_ECDSA_P256_HASH_SIZE);
266268
}
267269

268270
static inline int bootutil_ecdsa_parse_public_key(bootutil_ecdsa_context *ctx,
@@ -619,7 +621,11 @@ static inline int bootutil_ecdsa_p256_verify(bootutil_ecdsa_p256_context *ctx,
619621
{
620622
(void)ctx;
621623
(void)pk_len;
622-
(void)sig_len;
624+
uint8_t dsig[2 * NUM_ECC_BYTES];
625+
626+
if (bootutil_decode_sig(dsig, sig, sig + sig_len)) {
627+
return -1;
628+
}
623629

624630
/* As described on the compact representation in IETF protocols,
625631
* the first byte of the key defines if the ECC points are
@@ -632,7 +638,7 @@ static inline int bootutil_ecdsa_p256_verify(bootutil_ecdsa_p256_context *ctx,
632638
pk++;
633639

634640
return bl_secp256r1_validate(hash, BOOTUTIL_CRYPTO_ECDSA_P256_HASH_SIZE,
635-
pk, sig);
641+
pk, dsig);
636642
}
637643
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */
638644

0 commit comments

Comments
 (0)