Skip to content

Commit 106ade3

Browse files
sigvartmhrlubos
authored andcommitted
[nrf noup] boot: Add shared crypto for ECDSA and SHA
* Add functions for ecdsa_verify_secp256r1 and sha256 to use the shared crypto API * Add Kconfig and CMake variables for selecting shared crypto when using ecdsa * Add custom section to project for placing the API section in the correct location in flash * Add kconfig fragment for using external crypto Signed-off-by: Sigvart Hovland <[email protected]> Signed-off-by: Martí Bolívar <[email protected]> Signed-off-by: Emil Obalski <[email protected]> Signed-off-by: Andrzej Puzdrowski <[email protected]> Signed-off-by: Håkon Øye Amundsen <[email protected]> Signed-off-by: Ioannis Glaropoulos <[email protected]> Signed-off-by: Trond Einar Snekvik <[email protected]> Signed-off-by: Georgios Vasilakis <[email protected]> Signed-off-by: Johann Fischer <[email protected]> Signed-off-by: Torsten Rasmussen <[email protected]> Signed-off-by: Jamie McCrae <[email protected]> Signed-off-by: Dominik Ermel <[email protected]> (cherry picked from commit 4e0dee6)
1 parent 8809820 commit 106ade3

File tree

5 files changed

+114
-9
lines changed

5 files changed

+114
-9
lines changed

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

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#if (defined(MCUBOOT_USE_TINYCRYPT) + \
3636
defined(MCUBOOT_USE_CC310) + \
37+
defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO) + \
3738
defined(MCUBOOT_USE_PSA_OR_MBED_TLS)) != 1
3839
#error "One crypto backend must be defined: either CC310/TINYCRYPT/MBED_TLS/PSA_CRYPTO"
3940
#endif
@@ -70,12 +71,18 @@
7071
#include "bootutil/sign_key.h"
7172
#include "common.h"
7273

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+
7379
#ifdef __cplusplus
7480
extern "C" {
7581
#endif
7682

7783
#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)
7986
/*
8087
* Declaring these like this adds NULL termination.
8188
*/
@@ -127,8 +134,6 @@ static int bootutil_import_key(uint8_t **cp, uint8_t *end)
127134
}
128135
#endif /* (MCUBOOT_USE_TINYCRYPT || MCUBOOT_USE_MBED_TLS || MCUBOOT_USE_CC310) && !MCUBOOT_USE_PSA_CRYPTO */
129136

130-
#if defined(MCUBOOT_USE_TINYCRYPT)
131-
#ifndef MCUBOOT_ECDSA_NEED_ASN1_SIG
132137
/*
133138
* cp points to ASN1 string containing an integer.
134139
* 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
178183
}
179184
return 0;
180185
}
181-
#endif /* not MCUBOOT_ECDSA_NEED_ASN1_SIG */
182186

187+
#if defined(MCUBOOT_USE_TINYCRYPT)
183188
typedef uintptr_t bootutil_ecdsa_context;
184189
static inline void bootutil_ecdsa_init(bootutil_ecdsa_context *ctx)
185190
{
@@ -248,16 +253,20 @@ static inline int bootutil_ecdsa_verify(bootutil_ecdsa_context *ctx,
248253
{
249254
(void)ctx;
250255
(void)pk_len;
251-
(void)sig_len;
252256
(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+
}
253262

254263
/* Only support uncompressed keys. */
255264
if (pk[0] != 0x04) {
256265
return -1;
257266
}
258267
pk++;
259268

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);
261270
}
262271

263272
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,
613622

614623
#endif /* MCUBOOT_USE_MBED_TLS */
615624

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+
616668
#ifdef __cplusplus
617669
}
618670
#endif

boot/bootutil/include/bootutil/crypto/sha.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#if (defined(MCUBOOT_USE_PSA_OR_MBED_TLS) + \
3232
defined(MCUBOOT_USE_TINYCRYPT) + \
33+
defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO) + \
3334
defined(MCUBOOT_USE_CC310)) != 1
3435
#error "One crypto backend must be defined: either CC310/MBED_TLS/TINYCRYPT/PSA_CRYPTO"
3536
#endif
@@ -211,6 +212,37 @@ static inline int bootutil_sha_finish(bootutil_sha_context *ctx,
211212
}
212213
#endif /* MCUBOOT_USE_CC310 */
213214

215+
#if defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO)
216+
217+
#include <bl_crypto.h>
218+
219+
typedef bl_sha256_ctx_t bootutil_sha_context;
220+
221+
static inline void bootutil_sha_init(bootutil_sha_context *ctx)
222+
{
223+
bl_sha256_init(ctx);
224+
}
225+
226+
static inline void bootutil_sha_drop(bootutil_sha_context *ctx)
227+
{
228+
(void)ctx;
229+
}
230+
231+
static inline int bootutil_sha_update(bootutil_sha_context *ctx,
232+
const void *data,
233+
uint32_t data_len)
234+
{
235+
return bl_sha256_update(ctx, data, data_len);
236+
}
237+
238+
static inline int bootutil_sha_finish(bootutil_sha_context *ctx,
239+
uint8_t *output)
240+
{
241+
bl_sha256_finalize(ctx, output);
242+
return 0;
243+
}
244+
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */
245+
214246
#ifdef __cplusplus
215247
}
216248
#endif

boot/zephyr/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256 OR CONFIG_BOOT_ENCRYPT_EC256)
171171
zephyr_library_sources(${MCUBOOT_NRF_EXT_DIR}/cc310_glue.c)
172172
zephyr_library_include_directories(${MCUBOOT_NRF_EXT_DIR})
173173
zephyr_link_libraries(nrfxlib_crypto)
174+
elseif(CONFIG_BOOT_USE_NRF_EXTERNAL_CRYPTO)
175+
zephyr_include_directories(${BL_CRYPTO_DIR}/../include)
174176
endif()
175177

176178
# Since here we are not using Zephyr's mbedTLS but rather our own, we need

boot/zephyr/external_crypto.conf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright (c) 2021 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# These configurations should be used when using nrf/samples/bootloader
8+
# as the immutable bootloader (B0), and MCUBoot as the second stage updateable
9+
# bootloader.
10+
11+
# Set ECDSA as signing mechanism
12+
CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256=y
13+
14+
# Use crypto backend from B0
15+
CONFIG_BOOT_NRF_EXTERNAL_CRYPTO=y
16+
CONFIG_SECURE_BOOT_CRYPTO=y
17+
CONFIG_SB_CRYPTO_CLIENT_ECDSA_SECP256R1=y
18+
CONFIG_SB_CRYPTO_CLIENT_SHA256=y
19+
CONFIG_BL_SHA256_EXT_API_REQUIRED=y
20+
CONFIG_BL_SECP256R1_EXT_API_REQUIRED=y

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@
4040
#define MCUBOOT_USE_TINYCRYPT
4141
#elif defined(CONFIG_BOOT_USE_CC310)
4242
#define MCUBOOT_USE_CC310
43-
#ifdef CONFIG_BOOT_USE_NRF_CC310_BL
44-
#define MCUBOOT_USE_NRF_CC310_BL
45-
#endif
4643
#elif defined(CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT)
4744
#define MCUBOOT_USE_PSA_CRYPTO
45+
#elif defined(CONFIG_BOOT_USE_NRF_EXTERNAL_CRYPTO)
46+
#define MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
4847
#endif
4948

5049
#ifdef CONFIG_BOOT_IMG_HASH_ALG_SHA512

0 commit comments

Comments
 (0)