|
34 | 34 |
|
35 | 35 | #if (defined(MCUBOOT_USE_TINYCRYPT) + \
|
36 | 36 | defined(MCUBOOT_USE_CC310) + \
|
| 37 | + defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO) + \ |
37 | 38 | defined(MCUBOOT_USE_PSA_OR_MBED_TLS)) != 1
|
38 | 39 | #error "One crypto backend must be defined: either CC310/TINYCRYPT/MBED_TLS/PSA_CRYPTO"
|
39 | 40 | #endif
|
|
70 | 71 | #include "bootutil/sign_key.h"
|
71 | 72 | #include "common.h"
|
72 | 73 |
|
| 74 | +#if defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO) |
| 75 | + #include <bl_crypto.h> |
| 76 | + #define NUM_ECC_BYTES (256 / 8) |
| 77 | +#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */ |
| 78 | + |
73 | 79 | #ifdef __cplusplus
|
74 | 80 | extern "C" {
|
75 | 81 | #endif
|
76 | 82 |
|
77 | 83 | #if (defined(MCUBOOT_USE_TINYCRYPT) || defined(MCUBOOT_USE_MBED_TLS) || \
|
78 |
| - defined(MCUBOOT_USE_CC310)) && !defined(MCUBOOT_USE_PSA_CRYPTO) |
| 84 | + defined(MCUBOOT_USE_CC310) || defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO)) \ |
| 85 | + && !defined(MCUBOOT_USE_PSA_CRYPTO) |
79 | 86 | /*
|
80 | 87 | * Declaring these like this adds NULL termination.
|
81 | 88 | */
|
@@ -127,8 +134,6 @@ static int bootutil_import_key(uint8_t **cp, uint8_t *end)
|
127 | 134 | }
|
128 | 135 | #endif /* (MCUBOOT_USE_TINYCRYPT || MCUBOOT_USE_MBED_TLS || MCUBOOT_USE_CC310) && !MCUBOOT_USE_PSA_CRYPTO */
|
129 | 136 |
|
130 |
| -#if defined(MCUBOOT_USE_TINYCRYPT) |
131 |
| -#ifndef MCUBOOT_ECDSA_NEED_ASN1_SIG |
132 | 137 | /*
|
133 | 138 | * cp points to ASN1 string containing an integer.
|
134 | 139 | * Verify the tag, and that the length is 32 bytes. Helper function.
|
@@ -178,8 +183,8 @@ static int bootutil_decode_sig(uint8_t signature[NUM_ECC_BYTES * 2], uint8_t *cp
|
178 | 183 | }
|
179 | 184 | return 0;
|
180 | 185 | }
|
181 |
| -#endif /* not MCUBOOT_ECDSA_NEED_ASN1_SIG */ |
182 | 186 |
|
| 187 | +#if defined(MCUBOOT_USE_TINYCRYPT) |
183 | 188 | typedef uintptr_t bootutil_ecdsa_context;
|
184 | 189 | static inline void bootutil_ecdsa_init(bootutil_ecdsa_context *ctx)
|
185 | 190 | {
|
@@ -248,16 +253,20 @@ static inline int bootutil_ecdsa_verify(bootutil_ecdsa_context *ctx,
|
248 | 253 | {
|
249 | 254 | (void)ctx;
|
250 | 255 | (void)pk_len;
|
251 |
| - (void)sig_len; |
252 | 256 | (void)hash_len;
|
| 257 | + uint8_t dsig[2 * NUM_ECC_BYTES]; |
| 258 | + |
| 259 | + if (bootutil_decode_sig(dsig, sig, sig + sig_len)) { |
| 260 | + return -1; |
| 261 | + } |
253 | 262 |
|
254 | 263 | /* Only support uncompressed keys. */
|
255 | 264 | if (pk[0] != 0x04) {
|
256 | 265 | return -1;
|
257 | 266 | }
|
258 | 267 | pk++;
|
259 | 268 |
|
260 |
| - return cc310_ecdsa_verify_secp256r1(hash, pk, sig, BOOTUTIL_CRYPTO_ECDSA_P256_HASH_SIZE); |
| 269 | + return cc310_ecdsa_verify_secp256r1(hash, pk, dsig, BOOTUTIL_CRYPTO_ECDSA_P256_HASH_SIZE); |
261 | 270 | }
|
262 | 271 |
|
263 | 272 | static inline int bootutil_ecdsa_parse_public_key(bootutil_ecdsa_context *ctx,
|
@@ -613,6 +622,49 @@ static inline int bootutil_ecdsa_parse_public_key(bootutil_ecdsa_context *ctx,
|
613 | 622 |
|
614 | 623 | #endif /* MCUBOOT_USE_MBED_TLS */
|
615 | 624 |
|
| 625 | +#if defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO) |
| 626 | +typedef uintptr_t bootutil_ecdsa_context; |
| 627 | +static inline void bootutil_ecdsa_init(bootutil_ecdsa_context *ctx) |
| 628 | +{ |
| 629 | + (void)ctx; |
| 630 | +} |
| 631 | + |
| 632 | +static inline void bootutil_ecdsa_drop(bootutil_ecdsa_context *ctx) |
| 633 | +{ |
| 634 | + (void)ctx; |
| 635 | +} |
| 636 | + |
| 637 | +static inline int bootutil_ecdsa_verify(bootutil_ecdsa_context *ctx, |
| 638 | + uint8_t *pk, size_t pk_len, |
| 639 | + uint8_t *hash, size_t hash_len, |
| 640 | + uint8_t *sig, size_t sig_len) |
| 641 | +{ |
| 642 | + (void)ctx; |
| 643 | + (void)pk_len; |
| 644 | + (void)hash_len; |
| 645 | + uint8_t dsig[2 * NUM_ECC_BYTES]; |
| 646 | + |
| 647 | + if (bootutil_decode_sig(dsig, sig, sig + sig_len)) { |
| 648 | + return -1; |
| 649 | + } |
| 650 | + |
| 651 | + /* Only support uncompressed keys. */ |
| 652 | + if (pk[0] != 0x04) { |
| 653 | + return -1; |
| 654 | + } |
| 655 | + pk++; |
| 656 | + |
| 657 | + return bl_secp256r1_validate(hash, BOOTUTIL_CRYPTO_ECDSA_P256_HASH_SIZE, pk, dsig); |
| 658 | +} |
| 659 | + |
| 660 | +static inline int bootutil_ecdsa_parse_public_key(bootutil_ecdsa_context *ctx, |
| 661 | + uint8_t **cp,uint8_t *end) |
| 662 | +{ |
| 663 | + (void)ctx; |
| 664 | + return bootutil_import_key(cp, end); |
| 665 | +} |
| 666 | +#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */ |
| 667 | + |
616 | 668 | #ifdef __cplusplus
|
617 | 669 | }
|
618 | 670 | #endif
|
|
0 commit comments