Skip to content

Commit 3dfaa01

Browse files
adsz-nordicnvlsianpu
authored andcommitted
[nrf noup] bootutil: Add support for nrfxlib_crypto
Add support for subset of functions available in nrfxlib_crypto using ocrypto SW cryptography: - ecdsa_p256 - sha256 This allows to bypass layered system of Oberon PSA Crypto drivers and use SW crypto library calls directly, which can help reducing binary size. Changes are intented for Ultralight (nrf54ls05b), which has limited resources and can benefit from smaller mcuboot binary size. Ref: ncsdk-37699 Signed-off-by: Adam Szczygieł <adam.szczygiel@nordicsemi.no>
1 parent 0f2a6c3 commit 3dfaa01

5 files changed

Lines changed: 131 additions & 6 deletions

File tree

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

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
#if (defined(MCUBOOT_USE_TINYCRYPT) + \
3636
defined(MCUBOOT_USE_CC310) + \
3737
defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO) + \
38-
defined(MCUBOOT_USE_PSA_OR_MBED_TLS)) != 1
39-
#error "One crypto backend must be defined: either CC310/TINYCRYPT/MBED_TLS/PSA_CRYPTO"
38+
defined(MCUBOOT_USE_PSA_OR_MBED_TLS) + \
39+
defined(MCUBOOT_USE_NRF_OBERON)) != 1
40+
#error "One crypto backend must be defined: either CC310/TINYCRYPT/MBED_TLS/PSA_CRYPTO/NRF_OBERON"
4041
#endif
4142

4243
#if defined(MCUBOOT_USE_TINYCRYPT)
@@ -58,8 +59,13 @@
5859
#define MCUBOOT_ECDSA_NEED_ASN1_SIG
5960
#endif /* MCUBOOT_USE_MBED_TLS */
6061

62+
#if defined(MCUBOOT_USE_NRF_OBERON)
63+
#include <ocrypto_ecdsa_p256.h>
64+
#endif /* MCUBOOT_USE_NRF_OBERON */
65+
6166
/*TODO: remove this after cypress port mbedtls to abstract crypto api */
62-
#if defined(MCUBOOT_USE_CC310) || defined(MCUBOOT_USE_MBED_TLS)
67+
#if defined(MCUBOOT_USE_CC310) || defined(MCUBOOT_USE_MBED_TLS) || \
68+
defined(MCUBOOT_USE_NRF_OBERON)
6369
#define NUM_ECC_BYTES (256 / 8)
6470
#endif
6571

@@ -83,7 +89,8 @@ extern "C" {
8389
#endif
8490

8591
#if (defined(MCUBOOT_USE_TINYCRYPT) || defined(MCUBOOT_USE_MBED_TLS) || \
86-
defined(MCUBOOT_USE_CC310) || defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO)) \
92+
defined(MCUBOOT_USE_CC310) || defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO) || \
93+
defined(MCUBOOT_USE_NRF_OBERON)) \
8794
&& !defined(MCUBOOT_USE_PSA_CRYPTO)
8895
/*
8996
* Declaring these like this adds NULL termination.
@@ -719,6 +726,66 @@ static inline int bootutil_ecdsa_parse_public_key(bootutil_ecdsa_context *ctx,
719726
}
720727
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */
721728

729+
#if defined(MCUBOOT_USE_NRF_OBERON)
730+
#define UNCOMPRESSED_KEY_PREFIX 0x04
731+
732+
typedef uintptr_t bootutil_ecdsa_context;
733+
734+
static inline void bootutil_ecdsa_init(bootutil_ecdsa_context *ctx)
735+
{
736+
(void)ctx;
737+
}
738+
739+
static inline void bootutil_ecdsa_drop(bootutil_ecdsa_context *ctx)
740+
{
741+
(void)ctx;
742+
}
743+
744+
static inline int bootutil_ecdsa_verify(bootutil_ecdsa_context *ctx,
745+
uint8_t *pk, size_t pk_len,
746+
uint8_t *hash, size_t hash_len,
747+
uint8_t *sig, size_t sig_len)
748+
{
749+
if (pk == NULL || hash == NULL || sig == NULL) {
750+
return -1;
751+
}
752+
753+
/* API expects particular values:
754+
* pk_len -> 64, plus one byte for key type/format (consumed by this function)
755+
* hash_len -> 32
756+
* sig_len -> 64 but it is encoded in asn1 format which can have variable length
757+
* */
758+
if (pk_len != 65 || hash_len != 32 || sig_len == 0) {
759+
return -1;
760+
}
761+
762+
uint8_t signature[2 * NUM_ECC_BYTES];
763+
int rc = bootutil_decode_sig(signature, sig, sig + sig_len);
764+
if (rc) {
765+
return rc;
766+
}
767+
768+
/* Support only uncompressed keys */
769+
if (pk[0] != UNCOMPRESSED_KEY_PREFIX) {
770+
return -1;
771+
}
772+
773+
/* Skip the first byte holding key format */
774+
pk++;
775+
776+
return ocrypto_ecdsa_p256_verify_hash(signature, hash, pk);
777+
}
778+
779+
static inline int bootutil_ecdsa_parse_public_key(bootutil_ecdsa_context *ctx,
780+
uint8_t **cp, uint8_t *end)
781+
{
782+
(void)ctx;
783+
784+
return bootutil_import_key(cp, end);
785+
}
786+
787+
#endif /* MCUBOOT_USE_NRF_OBERON */
788+
722789
#ifdef __cplusplus
723790
}
724791
#endif

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
#if (defined(MCUBOOT_USE_PSA_OR_MBED_TLS) + \
3232
defined(MCUBOOT_USE_TINYCRYPT) + \
3333
defined(MCUBOOT_USE_NRF_EXTERNAL_CRYPTO) + \
34-
defined(MCUBOOT_USE_CC310)) != 1
35-
#error "One crypto backend must be defined: either CC310/MBED_TLS/TINYCRYPT/PSA_CRYPTO"
34+
defined(MCUBOOT_USE_CC310) + \
35+
defined(MCUBOOT_USE_NRF_OBERON)) != 1
36+
#error "One crypto backend must be defined: either CC310/MBED_TLS/TINYCRYPT/PSA_CRYPTO/NRF_OBERON"
3637
#endif
3738

3839
#if defined(MCUBOOT_SHA512)
@@ -84,6 +85,10 @@
8485

8586
#include <stdint.h>
8687

88+
#if defined(MCUBOOT_USE_NRF_OBERON)
89+
#include <ocrypto_sha256.h>
90+
#endif /* MCUBOOT_USE_NRF_OBERON */
91+
8792
#ifdef __cplusplus
8893
extern "C" {
8994
#endif
@@ -302,6 +307,43 @@ static inline int bootutil_sha_finish(bootutil_sha_context *ctx,
302307
}
303308
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */
304309

310+
#if defined(MCUBOOT_USE_NRF_OBERON)
311+
typedef ocrypto_sha256_ctx bootutil_sha_context;
312+
313+
static inline int bootutil_sha_init(bootutil_sha_context *ctx)
314+
{
315+
if (ctx == NULL) {
316+
return -1;
317+
}
318+
319+
ocrypto_sha256_init(ctx);
320+
return 0;
321+
}
322+
323+
static inline int bootutil_sha_drop(bootutil_sha_context *ctx)
324+
{
325+
/* NOTE: No corresponding function for ocrypto_sha256 */
326+
(void)ctx;
327+
return 0;
328+
}
329+
330+
static inline int bootutil_sha_update(bootutil_sha_context *ctx,
331+
const void *data,
332+
uint32_t data_len)
333+
{
334+
ocrypto_sha256_update(ctx, (const uint8_t *)data, (size_t)data_len);
335+
return 0;
336+
}
337+
338+
static inline int bootutil_sha_finish(bootutil_sha_context *ctx,
339+
uint8_t *output)
340+
{
341+
ocrypto_sha256_final(ctx, output);
342+
return 0;
343+
}
344+
345+
#endif /* MCUBOOT_USE_NRF_OBERON */
346+
305347
#ifdef __cplusplus
306348
}
307349
#endif

boot/bootutil/zephyr/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ if(CONFIG_BOOT_USE_TINYCRYPT)
4848
)
4949
endif()
5050

51+
if(CONFIG_BOOT_USE_NRF_OBERON)
52+
target_include_directories(MCUBOOT_BOOTUTIL INTERFACE ${OBERON_BASE}/include)
53+
zephyr_link_libraries(nrfxlib_crypto)
54+
endif()
55+
5156
if(CONFIG_BOOT_USE_PSA_CRYPTO)
5257
target_include_directories(MCUBOOT_BOOTUTIL INTERFACE
5358
${ZEPHYR_MBEDTLS_MODULE_DIR}/include

boot/zephyr/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ config BOOT_USE_CC310
5454
config BOOT_USE_NRF_CC310_BL
5555
bool
5656

57+
config BOOT_USE_NRF_OBERON
58+
bool
59+
help
60+
Use nrf oberon SW crypto.
61+
5762
config NRFXLIB_CRYPTO
5863
bool
5964

@@ -286,6 +291,10 @@ choice BOOT_ECDSA_IMPLEMENTATION
286291
default BOOT_ECDSA_PSA if NRF_SECURITY
287292
default BOOT_ECDSA_TINYCRYPT
288293

294+
config BOOT_ECDSA_NRF_OBERON
295+
bool "use nrf oberon SW crypto."
296+
select BOOT_USE_NRF_OBERON
297+
289298
config BOOT_ECDSA_TINYCRYPT
290299
bool "Use tinycrypt"
291300
select BOOT_USE_TINYCRYPT

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
#define MCUBOOT_USE_PSA_CRYPTO
5252
#elif defined(CONFIG_BOOT_USE_NRF_EXTERNAL_CRYPTO)
5353
#define MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
54+
#elif defined(CONFIG_BOOT_USE_NRF_OBERON)
55+
#define MCUBOOT_USE_NRF_OBERON
5456
#endif
5557

5658
#ifdef CONFIG_BOOT_IMG_HASH_ALG_SHA512

0 commit comments

Comments
 (0)