Skip to content

Commit e912bb2

Browse files
sigvartmhde-nordic
authored andcommitted
[nrf noup] boot: bootutil: 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 e741540) (cherry picked from commit 6804ab2)
1 parent 1630628 commit e912bb2

File tree

6 files changed

+99
-4
lines changed

6 files changed

+99
-4
lines changed

boot/bootutil/include/bootutil/crypto/ecdsa_p256.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#if (defined(MCUBOOT_USE_TINYCRYPT) + \
1616
defined(MCUBOOT_USE_CC310) + \
17+
defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO) + \
1718
defined(MCUBOOT_USE_MBED_TLS)) != 1
1819
#error "One crypto backend must be defined: either CC310, TINYCRYPT, or MBED_TLS"
1920
#endif
@@ -47,6 +48,11 @@
4748
#include "bootutil/sign_key.h"
4849
#include "common.h"
4950

51+
#if defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO)
52+
#include <bl_crypto.h>
53+
#define BOOTUTIL_CRYPTO_ECDSA_P256_HASH_SIZE (4 * 8)
54+
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */
55+
5056
#ifdef __cplusplus
5157
extern "C" {
5258
#endif
@@ -372,6 +378,43 @@ static inline int bootutil_ecdsa_p256_parse_public_key(bootutil_ecdsa_p256_conte
372378

373379
#endif /* MCUBOOT_USE_MBED_TLS */
374380

381+
#if defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO)
382+
typedef uintptr_t bootutil_ecdsa_p256_context;
383+
384+
static inline void bootutil_ecdsa_p256_init(bootutil_ecdsa_p256_context *ctx)
385+
{
386+
(void)ctx;
387+
}
388+
389+
static inline void bootutil_ecdsa_p256_drop(bootutil_ecdsa_p256_context *ctx)
390+
{
391+
(void)ctx;
392+
}
393+
394+
static inline int bootutil_ecdsa_p256_verify(bootutil_ecdsa_p256_context *ctx,
395+
uint8_t *pk, size_t pk_len,
396+
uint8_t *hash,
397+
uint8_t *sig, size_t sig_len)
398+
{
399+
(void)ctx;
400+
(void)pk_len;
401+
(void)sig_len;
402+
403+
/* As described on the compact representation in IETF protocols,
404+
* the first byte of the key defines if the ECC points are
405+
* compressed (0x2 or 0x3) or uncompressed (0x4).
406+
* We only support uncompressed keys.
407+
*/
408+
if (pk[0] != 0x04)
409+
return -1;
410+
411+
pk++;
412+
413+
return bl_secp256r1_validate(hash, BOOTUTIL_CRYPTO_ECDSA_P256_HASH_SIZE,
414+
pk, sig);
415+
}
416+
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */
417+
375418
#ifdef __cplusplus
376419
}
377420
#endif

boot/bootutil/include/bootutil/crypto/sha256.h

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

2323
#if (defined(MCUBOOT_USE_MBED_TLS) + \
2424
defined(MCUBOOT_USE_TINYCRYPT) + \
25+
defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO) + \
2526
defined(MCUBOOT_USE_CC310)) != 1
2627
#error "One crypto backend must be defined: either CC310, MBED_TLS or TINYCRYPT"
2728
#endif
@@ -139,6 +140,37 @@ static inline int bootutil_sha256_finish(bootutil_sha256_context *ctx,
139140
}
140141
#endif /* MCUBOOT_USE_CC310 */
141142

143+
#if defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO)
144+
145+
#include <bl_crypto.h>
146+
147+
typedef bl_sha256_ctx_t bootutil_sha256_context;
148+
149+
static inline void bootutil_sha256_init(bootutil_sha256_context *ctx)
150+
{
151+
bl_sha256_init(ctx);
152+
}
153+
154+
static inline void bootutil_sha256_drop(bootutil_sha256_context *ctx)
155+
{
156+
(void)ctx;
157+
}
158+
159+
static inline int bootutil_sha256_update(bootutil_sha256_context *ctx,
160+
const void *data,
161+
uint32_t data_len)
162+
{
163+
return bl_sha256_update(ctx, data, data_len);
164+
}
165+
166+
static inline int bootutil_sha256_finish(bootutil_sha256_context *ctx,
167+
uint8_t *output)
168+
{
169+
bl_sha256_finalize(ctx, output);
170+
return 0;
171+
}
172+
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */
173+
142174
#ifdef __cplusplus
143175
}
144176
#endif

boot/bootutil/src/image_ec256.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "mcuboot_config/mcuboot_config.h"
3131

3232
#ifdef MCUBOOT_SIGN_EC256
33-
3433
#include "bootutil_priv.h"
3534
#include "bootutil/fault_injection_hardening.h"
3635
#include "bootutil/crypto/ecdsa_p256.h"

boot/zephyr/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256 OR CONFIG_BOOT_ENCRYPT_EC256 OR CONFIG_
155155
zephyr_library_sources(${NRF_DIR}/cc310_glue.c)
156156
zephyr_library_include_directories(${NRF_DIR})
157157
zephyr_link_libraries(nrfxlib_crypto)
158+
elseif(CONFIG_BOOT_USE_NRF_EXTERNAL_CRYPTO)
159+
zephyr_include_directories(${BL_CRYPTO_DIR}/../include)
158160
endif()
159161

160162
# 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
@@ -39,9 +39,8 @@
3939
#define MCUBOOT_USE_TINYCRYPT
4040
#elif defined(CONFIG_BOOT_USE_CC310)
4141
#define MCUBOOT_USE_CC310
42-
#ifdef CONFIG_BOOT_USE_NRF_CC310_BL
43-
#define MCUBOOT_USE_NRF_CC310_BL
44-
#endif
42+
#elif defined(CONFIG_BOOT_USE_NRF_EXTERNAL_CRYPTO)
43+
#define MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
4544
#endif
4645

4746
/* Zephyr, regardless of C library used, provides snprintf */

0 commit comments

Comments
 (0)