Skip to content

Commit

Permalink
[nrf noup] boot: bootutil: Add shared crypto functions for ECDSA
Browse files Browse the repository at this point in the history
* 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)
(cherry picked from commit cd41748)
Signed-off-by: Martí Bolívar <[email protected]>
  • Loading branch information
sigvartmh authored and mbolivar-nordic committed Oct 24, 2019
1 parent 65b76e3 commit 4bcdaad
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 5 deletions.
27 changes: 27 additions & 0 deletions boot/bootutil/include/bootutil/sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
#include <cc310_glue.h>
#endif /* MCUBOOT_USE_CC310 */

#ifdef MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
#include <bl_crypto.h>
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */

#include <stdint.h>

#ifdef __cplusplus
Expand Down Expand Up @@ -119,6 +123,29 @@ static inline void bootutil_sha256_finish(bootutil_sha256_context *ctx,
}
#endif /* MCUBOOT_USE_CC310 */

#ifdef MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
typedef bl_sha256_ctx_t bootutil_sha256_context;

static inline void bootutil_sha256_init(bootutil_sha256_context *ctx)
{
bl_sha256_init(ctx);
}

static inline void bootutil_sha256_update(bootutil_sha256_context *ctx,
const void * data,
uint32_t data_len)
{
bl_sha256_update(ctx, data, data_len);

}

static inline void bootutil_sha256_finish(bootutil_sha256_context *ctx,
uint8_t * output)
{
bl_sha256_finalize(ctx, output);
}
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */

#ifdef __cplusplus
}
#endif
Expand Down
52 changes: 52 additions & 0 deletions boot/bootutil/src/image_ec256.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
#include "cc310_glue.h"
#define NUM_ECC_BYTES (4*8)
#endif
#ifdef MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
#include "bl_crypto.h"
#define NUM_ECC_BYTES (4*8)
#endif

#include "bootutil_priv.h"

/*
Expand Down Expand Up @@ -183,6 +188,7 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
}
}
#endif /* MCUBOOT_USE_TINYCRYPT */

#ifdef MCUBOOT_USE_CC310
int
bootutil_verify_sig(uint8_t *hash,
Expand Down Expand Up @@ -227,4 +233,50 @@ bootutil_verify_sig(uint8_t *hash,
return rc;
}
#endif /* MCUBOOT_USE_CC310 */

#ifdef MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
int
bootutil_verify_sig(uint8_t *hash,
uint32_t hlen,
uint8_t *sig,
size_t slen,
uint8_t key_id)
{
int rc;
uint8_t *pubkey;
uint8_t *end;
uint8_t signature[2 * NUM_ECC_BYTES];

pubkey = (uint8_t *)bootutil_keys[key_id].key;
end = pubkey + *bootutil_keys[key_id].len;

rc = bootutil_import_key(&pubkey, end);
if (rc) {
return -1;
}

/* Decode signature */
rc = bootutil_decode_sig(signature, sig, sig + slen);
if (rc) {
return -1;
}

/*
* This is simplified, as the hash length is also 32 bytes.
*/
if (hlen != NUM_ECC_BYTES) {
return -1;
}

/* Initialize and verify in one go */
rc = bl_secp256r1_validate(hash, hlen, pubkey, signature);

if(rc != 0 /*CRYS_OK*/){
return -2;
}

return rc;
}
#endif /* MCUBOOT_USE_NRF_EXTERNAL_CRYPTO */

#endif /* MCUBOOT_SIGN_EC256 */
4 changes: 4 additions & 0 deletions boot/zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ if(CONFIG_BOOT_USE_NRF_CC310_BL)
set(NRFXLIB_DIR ${MCUBOOT_DIR}/../nrfxlib)
assert_exists(NRFXLIB_DIR)
endif()
set(NRF_EXTERNAL_CRYPTO_DIR "${MCUBOOT_DIR}/../nrf/subsys/bootloader/bl_crypto")
assert_exists(NRF_EXTERNAL_CRYPTO_DIR)

zephyr_library_include_directories(
include
Expand Down Expand Up @@ -137,6 +139,8 @@ if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
zephyr_library_sources(${NRF_DIR}/cc310_glue.c)
zephyr_library_include_directories(${NRF_DIR})
zephyr_link_libraries(nrfxlib_crypto)
elseif(CONFIG_BOOT_USE_NRF_EXTERNAL_CRYPTO)
zephyr_include_directories(${BL_CRYPTO_DIR}/../include)
endif()

# Since here we are not using Zephyr's mbedTLS but rather our own, we need
Expand Down
24 changes: 22 additions & 2 deletions boot/zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ config BOOT_USE_NRF_CC310_BL
bool
default n

config BOOT_USE_NRF_EXTERNAL_CRYPTO
bool
# Hidden option
default n
# When building for ECDSA, we use our own copy of mbedTLS, so the
# Zephyr one must not be enabled or the MBEDTLS_CONFIG_FILE macros
# will collide.
depends on ! MBEDTLS
help
Use Shared crypto for crypto primitives.

menu "MCUBoot settings"

choice
Expand Down Expand Up @@ -76,19 +87,28 @@ config BOOT_SIGNATURE_TYPE_ED25519
if BOOT_SIGNATURE_TYPE_ECDSA_P256
choice
prompt "Ecdsa implementation"
default BOOT_NRF_EXTERNAL_CRYPTO if SECURE_BOOT
default BOOT_CC310 if HAS_HW_NRF_CC310
default BOOT_TINYCRYPT

config BOOT_TINYCRYPT
bool "Use tinycrypt"
select BOOT_USE_TINYCRYPT

config BOOT_CC310
bool "Use CC310"
select BOOT_USE_NRF_CC310_BL if HAS_HW_NRF_CC310
select NRF_CC310_BL if HAS_HW_NRF_CC310
select NRFXLIB_CRYPTO if SOC_FAMILY_NRF
select BOOT_USE_CC310

config BOOT_NRF_EXTERNAL_CRYPTO
bool "Use Shared Crypto from bootloader"
select BOOT_USE_NRF_EXTERNAL_CRYPTO
depends on SECURE_BOOT

endchoice
endif
endif #BOOT_SIGNATURE_TYPE_ECDSA_P256

endchoice

config BOOT_SIGNATURE_KEY_FILE
Expand Down
5 changes: 2 additions & 3 deletions boot/zephyr/include/mcuboot_config/mcuboot_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@
#define MCUBOOT_USE_TINYCRYPT
#elif defined(CONFIG_BOOT_USE_CC310)
#define MCUBOOT_USE_CC310
#ifdef CONFIG_BOOT_USE_NRF_CC310_BL
#define MCUBOOT_USE_NRF_CC310_BL
#endif
#elif defined(CONFIG_BOOT_USE_NRF_EXTERNAL_CRYPTO)
#define MCUBOOT_USE_NRF_EXTERNAL_CRYPTO
#endif

#ifdef CONFIG_BOOT_VALIDATE_SLOT0
Expand Down

0 comments on commit 4bcdaad

Please sign in to comment.