Commit a4818a55 authored by Marti Bolivar's avatar Marti Bolivar Committed by Fabio Utzig
Browse files

zephyr: migrate signature type to Kconfig

Handle the CONFIG_BOOT_SIGNATURE_TYPE_xxx values in Zephyr's
mcuboot_config.h by converting them into the platform-agnostic MCUboot
definitions.

This requires some changes to the way the release test Makefile is
structured, since Kconfig symbols cannot be set from the command line.

Instead, use the OVERLAY_CONFIG feature of the Zephyr build system,
which allows specifying extra fragments to merge into the final
.config. (This is an orthogonal mechanism to setting CONF_FILE; it is
used by Zephyr's CI script sanitycheck to add additional fragments, so
it's appropriate for use by MCUboot's testing scripts as well.)

We additionally need to move to a single prj.conf file due to a
dependency issue. We can no longer determine CONF_FILE from the
signature type, since that is now determined from the final .config or
autoconf.h, which is a build output that depends on CONF_FILE.

To move to a single prj.conf:

- delete prj-p256.conf and adjust prj.conf to serve both signature types
- add a top-level mbedTLS configuration file which dispatches to
  the right sub-header depending on the key type
- as a side effect, have the simulator pick the right config file
  depending on the case

This fixes and cleans up quite a bit of the signature type handling,
which had become something of a mess over time. For example, it fixes
a bug in ECDSA mode's configuration that wasn't actually selecting
config-asn1.h, and forces the simulator to use the same mbedTLS
configuration file as builds for real hardware.

Finally, we also have to move the mbedTLS vs. TinyCrypt choice into
mcuboot_config.h at the same time as well, since CMakeLists.txt was
making that decision based on the signature type.

Signed-off-by: default avatarMarti Bolivar <marti@opensourcefoundries.com>
parent f91bca51
Loading
Loading
Loading
Loading
+11 −40
Changes for boot/zephyr/CMakeLists.txt: 11 added lines, 40 removed lines.
Original line number Diff line number Diff line
@@ -12,20 +12,6 @@ set(KCONFIG_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/Kconfig)
# Configuration choices.
########################

# Set CONF_SIGNATURE_TYPE to determine the signature type used.
# Currently, it should be set to either RSA or ECDSA_P256.
#
# To choose RSA (this is the default):
#
#     cmake -DCONF_SIGNATURE_TYPE=RSA [...]
#
# To use ECDSA_P256:
#
#     cmake -DCONF_SIGNATURE_TYPE=ECDSA_P256 [...]
if (NOT DEFINED CONF_SIGNATURE_TYPE)
  set(CONF_SIGNATURE_TYPE RSA)
endif()

# If CONF_VALIDATE_SLOT0 is set, the bootloader attempts to validate
# the signature of slot0 every boot.  This adds the signature check
# time to every boot, but can mitigate against some changes that are
@@ -81,28 +67,15 @@ endif()

set(MCUBOOT_EXTRA_CFLAGS)

# Determine CFLAGS / MCUBOOT_CONF_FILE / NEED_TINYCRYPT from the signature type.
if(CONF_SIGNATURE_TYPE STREQUAL RSA)
  set(MCUBOOT_CONF_FILE prj.conf) # RSA
  list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_SIGN_RSA" "-DMCUBOOT_USE_MBED_TLS")
  set(NEED_TINYCRYPT NO)
elseif(CONF_SIGNATURE_TYPE STREQUAL ECDSA_P256)
  set(MCUBOOT_CONF_FILE prj-p256.conf) # ECDSA P-256
  list(APPEND MCUBOOT_EXTRA_CFLAGS "-DMCUBOOT_SIGN_EC256" "-DMCUBOOT_USE_TINYCRYPT")
  set(NEED_TINYCRYPT YES)
else()
  message(FATAL_ERROR "Invalid CONF_SIGNATURE_TYPE specified: '${CONF_SIGNATURE_TYPE}'")
endif()

# Board-specific CONF_FILES should get merged into the build as well.
#
# Do this by defining the set_conf_file macro:
# http://docs.zephyrproject.org/application/application.html#application-configuration
macro(set_conf_file)
  if (EXISTS ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf)
    set(CONF_FILE "${MCUBOOT_CONF_FILE} ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf")
    set(CONF_FILE "prj.conf ${APPLICATION_SOURCE_DIR}/boards/${BOARD}.conf")
  else()
    set(CONF_FILE "${MCUBOOT_CONF_FILE}")
    set(CONF_FILE prj.conf)
  endif()
endmacro()

@@ -177,15 +150,6 @@ set(TINYCRYPT_DIR "${MCUBOOT_DIR}/ext/tinycrypt/lib")
# Path to mbed-tls' asn1 parser library.
set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls")

# Zephyr application include directories.
if (NOT NEED_TINYCRYPT)
  # Zephyr's mbedTLS needs this.
  zephyr_include_directories(include)

  # Use full mbedtls provided by OS for RSA
  target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/ext/lib/crypto/mbedtls/include)
endif()

target_include_directories(app PRIVATE include)
target_include_directories(app PRIVATE targets)
if(EXISTS "${APPLICATION_SOURCE_DIR}/targets/${BOARD}.h")
@@ -211,8 +175,8 @@ target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_rsa.c")
target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/image_ec256.c")
target_sources(app PRIVATE "${BOOT_DIR}/bootutil/src/caps.c")

# Tinycrypt sources and includes, if needed.
if (NEED_TINYCRYPT)
if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
  # When using ECDSA signatures, pull in our copy of the tinycrypt library.
  target_include_directories(app PRIVATE "${BOOT_DIR}/zephyr/include")
  target_include_directories(app PRIVATE "${TINYCRYPT_DIR}/include")
  target_include_directories(app PRIVATE "${MBEDTLS_ASN1_DIR}/include")
@@ -222,7 +186,14 @@ if (NEED_TINYCRYPT)
  target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/sha256.c")
  target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/utils.c")

  # Additionally pull in just the ASN.1 parser from mbedTLS.
  target_compile_definitions(app PRIVATE MBEDTLS_CFG_FILE=config-asn1.h)
  target_sources(app PRIVATE "${MBEDTLS_ASN1_DIR}/src/asn1parse.c")
elseif(CONFIG_BOOT_SIGNATURE_TYPE_RSA)
  # Use mbedTLS provided by Zephyr for RSA signatures. (Its config file
  # is set using Kconfig.)
  zephyr_include_directories(include)
  target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/ext/lib/crypto/mbedtls/include)
endif()

if (CONFIG_MCUBOOT_SERIAL)
+4
Changes for boot/zephyr/Kconfig: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ choice
config BOOT_SIGNATURE_TYPE_RSA
	bool "RSA signatures"
	select BOOT_USE_MBEDTLS
	select MBEDTLS

config BOOT_SIGNATURE_TYPE_ECDSA_P256
	bool "Elliptic curve digital signatures with curve P-256"
@@ -35,6 +36,9 @@ config BOOT_SIGNATURE_TYPE_ECDSA_P256

endchoice

config MBEDTLS_CFG_FILE
	default "mcuboot-mbedtls-cfg.h"

config BOOT_VALIDATE_SLOT0
	bool "Validate image slot 0 on every boot"
	default y
+3 −20
Changes for boot/zephyr/include/config-rsa.h: 3 added lines, 20 removed lines.
Original line number Diff line number Diff line
@@ -26,8 +26,8 @@
 * - RSA or ECDSA signature verification
 */

#ifndef MBEDTLS_CONFIG_BOOT_H
#define MBEDTLS_CONFIG_BOOT_H
#ifndef MCUBOOT_MBEDTLS_CONFIG_RSA
#define MCUBOOT_MBEDTLS_CONFIG_RSA

#ifdef CONFIG_MCUBOOT_SERIAL
/* Mcuboot uses mbedts-base64 for serial protocol encoding. */
@@ -56,20 +56,8 @@
#define MBEDTLS_TEST_NULL_ENTROPY
#endif

/* mbed TLS feature support */
#ifdef MCUBOOT_SIGN_EC
#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
#define MBEDTLS_ECP_DP_SECP224R1_ENABLED
#define MBEDTLS_ECP_NIST_OPTIM
#define MBEDTLS_ECDSA_C
#define MBEDTLS_ECDH_C
#define MBEDTLS_ECP_C
#endif

#ifdef MCUBOOT_SIGN_RSA
#define MBEDTLS_RSA_C
#define MBEDTLS_PKCS1_V21
#endif

/* mbed TLS modules */
#define MBEDTLS_ASN1_PARSE_C
@@ -80,13 +68,8 @@
#define MBEDTLS_SHA256_C

/* Save RAM by adjusting to our exact needs */
#ifdef MCUBOOT_SIGN_RSA
#define MBEDTLS_ECP_MAX_BITS             2048
#define MBEDTLS_MPI_MAX_SIZE              256
#else
#define MBEDTLS_ECP_MAX_BITS             256
#define MBEDTLS_MPI_MAX_SIZE              32 // 256 bits is 32 bytes
#endif

#define MBEDTLS_SSL_MAX_CONTENT_LEN 1024

@@ -95,4 +78,4 @@

#include "mbedtls/check_config.h"

#endif /* MBEDTLS_CONFIG_BOOT_H */
#endif /* MCUBOOT_MBEDTLS_CONFIG_RSA */
+32
Changes for boot/zephyr/include/mcuboot-mbedtls-cfg.h: 32 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 *  Copyright (C) 2018 Open Source Foundries Limited
 *  SPDX-License-Identifier: Apache-2.0
 */

#ifndef _MCUBOOT_MBEDTLS_CONFIG_
#define _MCUBOOT_MBEDTLS_CONFIG_

/**
 * @file
 *
 * This is the top-level mbedTLS configuration file for MCUboot. The
 * configuration depends on the signature type, so this file just
 * pulls in the right header depending on that setting.
 */

/*
 * IMPORTANT:
 *
 * If you put any "generic" definitions in here, make sure to update
 * the simulator build.rs accordingly.
 */

#ifdef CONFIG_BOOT_SIGNATURE_TYPE_RSA
#include "config-rsa.h"
#elif defined(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
#include "config-asn1.h"
#else
#error "Cannot configure mbedTLS; signature type is unknown."
#endif

#endif
+11 −3
Changes for boot/zephyr/include/mcuboot_config/mcuboot_config.h: 11 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -18,9 +18,17 @@
 */
#ifndef __BOOTSIM__

/*
 * Initially blank.
 */
#ifdef CONFIG_BOOT_SIGNATURE_TYPE_RSA
#define MCUBOOT_SIGN_RSA
#elif defined(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
#define MCUBOOT_SIGN_EC256
#endif

#ifdef CONFIG_BOOT_USE_MBEDTLS
#define MCUBOOT_USE_MBED_TLS
#elif defined(CONFIG_BOOT_USE_TINYCRYPT)
#define MCUBOOT_USE_TINYCRYPT
#endif

#endif /* !__BOOTSIM__ */

Loading