Skip to content

Commit 0341ae4

Browse files
sigvartmhnvlsianpu
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]>
1 parent 295e8c2 commit 0341ae4

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
@@ -50,6 +50,10 @@
5050
#include <cc310_glue.h>
5151
#endif /* MCUBOOT_USE_CC310 */
5252

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

5559
#ifdef __cplusplus
@@ -120,6 +124,29 @@ static inline void bootutil_sha256_finish(bootutil_sha256_context *ctx,
120124
}
121125
#endif /* MCUBOOT_USE_CC310 */
122126

127+
#ifdef MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
128+
typedef bl_sha256_ctx_t bootutil_sha256_context;
129+
130+
static inline void bootutil_sha256_init(bootutil_sha256_context *ctx)
131+
{
132+
bl_sha256_init(ctx);
133+
}
134+
135+
static inline void bootutil_sha256_update(bootutil_sha256_context *ctx,
136+
const void * data,
137+
uint32_t data_len)
138+
{
139+
bl_sha256_update(ctx, data, data_len);
140+
141+
}
142+
143+
static inline void bootutil_sha256_finish(bootutil_sha256_context *ctx,
144+
uint8_t * output)
145+
{
146+
bl_sha256_finalize(ctx, output);
147+
}
148+
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */
149+
123150
#ifdef __cplusplus
124151
}
125152
#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
@@ -71,6 +71,8 @@ if(CONFIG_BOOT_USE_NRF_CC310_BL)
7171
set(NRFXLIB_DIR ${MCUBOOT_DIR}/../nrfxlib)
7272
assert_exists(NRFXLIB_DIR)
7373
endif()
74+
set(NRF_EXTERNAL_CRYPTO_DIR "${MCUBOOT_DIR}/../nrf/subsys/bootloader/bl_crypto")
75+
assert_exists(NRF_EXTERNAL_CRYPTO_DIR)
7476

7577
zephyr_library_include_directories(
7678
include
@@ -132,6 +134,8 @@ if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
132134
zephyr_library_sources(${NRF_DIR}/cc310_glue.c)
133135
zephyr_library_include_directories(${NRF_DIR})
134136
zephyr_link_libraries(nrfxlib_crypto)
137+
elseif(CONFIG_BOOT_USE_NRF_EXTERNAL_CRYPTO)
138+
zephyr_include_directories(${BL_CRYPTO_DIR}/../include)
135139
endif()
136140

137141
# 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
@@ -64,19 +75,28 @@ config BOOT_SIGNATURE_TYPE_ECDSA_P256
6475
if BOOT_SIGNATURE_TYPE_ECDSA_P256
6576
choice
6677
prompt "Ecdsa implementation"
78+
default BOOT_NRF_EXTERNAL_CRYPTO if SECURE_BOOT
6779
default BOOT_CC310 if HAS_HW_NRF_CC310
6880
default BOOT_TINYCRYPT
81+
6982
config BOOT_TINYCRYPT
7083
bool "Use tinycrypt"
7184
select BOOT_USE_TINYCRYPT
85+
7286
config BOOT_CC310
7387
bool "Use CC310"
7488
select BOOT_USE_NRF_CC310_BL if HAS_HW_NRF_CC310
75-
select NRF_CC310_BL if HAS_HW_NRF_CC310
7689
select NRFXLIB_CRYPTO if SOC_FAMILY_NRF
7790
select BOOT_USE_CC310
91+
92+
config BOOT_NRF_EXTERNAL_CRYPTO
93+
bool "Use Shared Crypto from bootloader"
94+
select BOOT_USE_NRF_EXTERNAL_CRYPTO
95+
depends on SECURE_BOOT
96+
7897
endchoice
79-
endif
98+
endif #BOOT_SIGNATURE_TYPE_ECDSA_P256
99+
80100
endchoice
81101

82102
config MBEDTLS_CFG_FILE

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@
3030
#define MCUBOOT_USE_TINYCRYPT
3131
#elif defined(CONFIG_BOOT_USE_CC310)
3232
#define MCUBOOT_USE_CC310
33-
#ifdef CONFIG_BOOT_USE_NRF_CC310_BL
34-
#define MCUBOOT_USE_NRF_CC310_BL
35-
#endif
33+
#elif defined(CONFIG_BOOT_USE_NRF_EXTERNAL_CRYPTO)
34+
#define MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
3635
#endif
3736

3837
#ifdef CONFIG_BOOT_VALIDATE_SLOT0

0 commit comments

Comments
 (0)