Skip to content

Commit cd41748

Browse files
sigvartmhmbolivar-nordic
authored andcommitted
[nrf noup] boot: bootutil: Add shared crypto functions for ECDSA
* 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 Signed-off-by: Sigvart Hovland <[email protected]> (cherry picked from commit 0341ae4)
1 parent 2dbfddd commit cd41748

File tree

5 files changed

+107
-5
lines changed

5 files changed

+107
-5
lines changed

boot/bootutil/include/bootutil/sha256.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
#include <cc310_glue.h>
5050
#endif /* MCUBOOT_USE_CC310 */
5151

52+
#ifdef MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
53+
#include <bl_crypto.h>
54+
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */
55+
5256
#include <stdint.h>
5357

5458
#ifdef __cplusplus
@@ -119,6 +123,29 @@ static inline void bootutil_sha256_finish(bootutil_sha256_context *ctx,
119123
}
120124
#endif /* MCUBOOT_USE_CC310 */
121125

126+
#ifdef MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
127+
typedef bl_sha256_ctx_t bootutil_sha256_context;
128+
129+
static inline void bootutil_sha256_init(bootutil_sha256_context *ctx)
130+
{
131+
bl_sha256_init(ctx);
132+
}
133+
134+
static inline void bootutil_sha256_update(bootutil_sha256_context *ctx,
135+
const void * data,
136+
uint32_t data_len)
137+
{
138+
bl_sha256_update(ctx, data, data_len);
139+
140+
}
141+
142+
static inline void bootutil_sha256_finish(bootutil_sha256_context *ctx,
143+
uint8_t * output)
144+
{
145+
bl_sha256_finalize(ctx, output);
146+
}
147+
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */
148+
122149
#ifdef __cplusplus
123150
}
124151
#endif

boot/bootutil/src/image_ec256.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
#include "cc310_glue.h"
3535
#define NUM_ECC_BYTES (4*8)
3636
#endif
37+
#ifdef MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
38+
#include "bl_crypto.h"
39+
#define NUM_ECC_BYTES (4*8)
40+
#endif
41+
3742
#include "bootutil_priv.h"
3843

3944
/*
@@ -183,6 +188,7 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
183188
}
184189
}
185190
#endif /* MCUBOOT_USE_TINYCRYPT */
191+
186192
#ifdef MCUBOOT_USE_CC310
187193
int
188194
bootutil_verify_sig(uint8_t *hash,
@@ -227,4 +233,50 @@ bootutil_verify_sig(uint8_t *hash,
227233
return rc;
228234
}
229235
#endif /* MCUBOOT_USE_CC310 */
236+
237+
#ifdef MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
238+
int
239+
bootutil_verify_sig(uint8_t *hash,
240+
uint32_t hlen,
241+
uint8_t *sig,
242+
size_t slen,
243+
uint8_t key_id)
244+
{
245+
int rc;
246+
uint8_t *pubkey;
247+
uint8_t *end;
248+
uint8_t signature[2 * NUM_ECC_BYTES];
249+
250+
pubkey = (uint8_t *)bootutil_keys[key_id].key;
251+
end = pubkey + *bootutil_keys[key_id].len;
252+
253+
rc = bootutil_import_key(&pubkey, end);
254+
if (rc) {
255+
return -1;
256+
}
257+
258+
/* Decode signature */
259+
rc = bootutil_decode_sig(signature, sig, sig + slen);
260+
if (rc) {
261+
return -1;
262+
}
263+
264+
/*
265+
* This is simplified, as the hash length is also 32 bytes.
266+
*/
267+
if (hlen != NUM_ECC_BYTES) {
268+
return -1;
269+
}
270+
271+
/* Initialize and verify in one go */
272+
rc = bl_secp256r1_validate(hash, hlen, pubkey, signature);
273+
274+
if(rc != 0 /*CRYS_OK*/){
275+
return -2;
276+
}
277+
278+
return rc;
279+
}
280+
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */
281+
230282
#endif /* MCUBOOT_SIGN_EC256 */

boot/zephyr/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ if(CONFIG_BOOT_USE_NRF_CC310_BL)
7474
set(NRFXLIB_DIR ${MCUBOOT_DIR}/../nrfxlib)
7575
assert_exists(NRFXLIB_DIR)
7676
endif()
77+
set(NRF_EXTERNAL_CRYPTO_DIR "${MCUBOOT_DIR}/../nrf/subsys/bootloader/bl_crypto")
78+
assert_exists(NRF_EXTERNAL_CRYPTO_DIR)
7779

7880
zephyr_library_include_directories(
7981
include
@@ -136,6 +138,8 @@ if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
136138
zephyr_library_sources(${NRF_DIR}/cc310_glue.c)
137139
zephyr_library_include_directories(${NRF_DIR})
138140
zephyr_link_libraries(nrfxlib_crypto)
141+
elseif(CONFIG_BOOT_USE_NRF_EXTERNAL_CRYPTO)
142+
zephyr_include_directories(${BL_CRYPTO_DIR}/../include)
139143
endif()
140144

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

boot/zephyr/Kconfig

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ config BOOT_USE_NRF_CC310_BL
4747
bool
4848
default n
4949

50+
config BOOT_USE_NRF_EXTERNAL_CRYPTO
51+
bool
52+
# Hidden option
53+
default n
54+
# When building for ECDSA, we use our own copy of mbedTLS, so the
55+
# Zephyr one must not be enabled or the MBEDTLS_CONFIG_FILE macros
56+
# will collide.
57+
depends on ! MBEDTLS
58+
help
59+
Use Shared crypto for crypto primitives.
60+
5061
menu "MCUBoot settings"
5162

5263
choice
@@ -76,19 +87,28 @@ config BOOT_SIGNATURE_TYPE_ED25519
7687
if BOOT_SIGNATURE_TYPE_ECDSA_P256
7788
choice
7889
prompt "Ecdsa implementation"
90+
default BOOT_NRF_EXTERNAL_CRYPTO if SECURE_BOOT
7991
default BOOT_CC310 if HAS_HW_NRF_CC310
8092
default BOOT_TINYCRYPT
93+
8194
config BOOT_TINYCRYPT
8295
bool "Use tinycrypt"
8396
select BOOT_USE_TINYCRYPT
97+
8498
config BOOT_CC310
8599
bool "Use CC310"
86100
select BOOT_USE_NRF_CC310_BL if HAS_HW_NRF_CC310
87-
select NRF_CC310_BL if HAS_HW_NRF_CC310
88101
select NRFXLIB_CRYPTO if SOC_FAMILY_NRF
89102
select BOOT_USE_CC310
103+
104+
config BOOT_NRF_EXTERNAL_CRYPTO
105+
bool "Use Shared Crypto from bootloader"
106+
select BOOT_USE_NRF_EXTERNAL_CRYPTO
107+
depends on SECURE_BOOT
108+
90109
endchoice
91-
endif
110+
endif #BOOT_SIGNATURE_TYPE_ECDSA_P256
111+
92112
endchoice
93113

94114
config BOOT_SIGNATURE_KEY_FILE

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@
3838
#define MCUBOOT_USE_TINYCRYPT
3939
#elif defined(CONFIG_BOOT_USE_CC310)
4040
#define MCUBOOT_USE_CC310
41-
#ifdef CONFIG_BOOT_USE_NRF_CC310_BL
42-
#define MCUBOOT_USE_NRF_CC310_BL
43-
#endif
41+
#elif defined(CONFIG_BOOT_USE_NRF_EXTERNAL_CRYPTO)
42+
#define MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
4443
#endif
4544

4645
#ifdef CONFIG_BOOT_VALIDATE_SLOT0

0 commit comments

Comments
 (0)