diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index 8c2cdcccaefb..a63594934b76 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -15,3 +15,5 @@ add_subdirectory_ifdef(CONFIG_BT_LL_NRFXLIB bt_ll_nrfxlib) add_subdirectory_ifdef(CONFIG_NRF9160_GPS nrf9160_gps) add_subdirectory_ifdef(CONFIG_FPROTECT fprotect) add_subdirectory(flash_patch) +add_subdirectory_ifdef(CONFIG_HW_CC310 hw_cc310) +add_subdirectory(entropy) diff --git a/drivers/Kconfig b/drivers/Kconfig index 07f46b282349..e5cb020ac910 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -10,6 +10,8 @@ rsource "adp536x/Kconfig" rsource "at_cmd/Kconfig" rsource "gps_sim/Kconfig" +rsource "entropy/Kconfig" +rsource "hw_cc310/Kconfig" rsource "nrf9160_gps/Kconfig" rsource "lte_link_control/Kconfig" rsource "net/Kconfig" diff --git a/drivers/entropy/CMakeLists.txt b/drivers/entropy/CMakeLists.txt new file mode 100644 index 000000000000..85e8599ad828 --- /dev/null +++ b/drivers/entropy/CMakeLists.txt @@ -0,0 +1,16 @@ +# +# Copyright (c) 2019 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic +# +zephyr_library_amend() +zephyr_library_sources_if_kconfig(entropy_cc310.c) + +# Link with the nrf_cc310 platform library if the following is met: +# -nRF52840 device +# -nRF9160 device that is not using SPM +# -nRF9150 device that is using SPM and in a secure image +# (CONFIG_SPM is not defined in a secure image) +if (CONFIG_SOC_NRF52840 OR (CONFIG_SOC_NRF9160 AND (NOT CONFIG_SPM))) + zephyr_link_libraries_ifdef(CONFIG_ENTROPY_CC310 ${IMAGE}platform_cc310) +endif () diff --git a/drivers/entropy/Kconfig b/drivers/entropy/Kconfig new file mode 100644 index 000000000000..c80ec7fe71fe --- /dev/null +++ b/drivers/entropy/Kconfig @@ -0,0 +1,19 @@ +# Kconfig - Arm CC310 entropy driver for nRF52840 and nRF9160 +# +# Copyright (c) 2019 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic +# + +config ENTROPY_CC310 + bool "Arm CC310 RNG driver for Nordic devices" + depends on HW_CC310 || (SOC_NRF9160 && SPM) + depends on ENTROPY_GENERATOR + depends on !BT_LL_SW_LEGACY + depends on !MPU_STACK_GUARD + select ENTROPY_HAS_DRIVER + select ENTROPY_NRF_FORCE_ALT + default y + help + This option enables the Arm CC310 RNG devices in nRF52840 and nRF9160 + devices. This is dependent on CC310 being enabled in nrf_security. diff --git a/drivers/entropy/entropy_cc310.c b/drivers/entropy/entropy_cc310.c new file mode 100644 index 000000000000..6888b218b51e --- /dev/null +++ b/drivers/entropy/entropy_cc310.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic + */ + +#include +#include +#include +#include +#include + +#include +#include + +#if CONFIG_ENTROPY_CC310 + +#if defined(CONFIG_SPM) +#include "secure_services.h" +#else +#include "nrf_cc310_platform_entropy.h" +#endif + +static int entropy_cc310_rng_get_entropy(struct device *dev, u8_t *buffer, + u16_t length) +{ + int res = -EINVAL; + size_t olen; + + __ASSERT_NO_MSG(dev != NULL); + __ASSERT_NO_MSG(buffer != NULL); + +#if defined(CONFIG_SPM) + /** This is a call from a non-secure app that enables secure services, + * in which case entropy is gathered by calling through SPM + */ + res = spm_request_random_number(buffer, length, &olen); + if (olen != length) { + return -EINVAL; + } + +#else + /** This is a call from a secure app, in which case entropy is gathered + * using CC310 HW using the nrf_cc310_platform library. + */ + res = nrf_cc310_platform_entropy_get(buffer, length, &olen); + if (olen != length) { + return -EINVAL; + } +#endif + + return res; +} + +static int entropy_cc310_rng_init(struct device *dev) +{ + /* No initialization is required */ + (void)dev; + + return 0; +} + +static const struct entropy_driver_api entropy_cc310_rng_api = { + .get_entropy = entropy_cc310_rng_get_entropy +}; + +DEVICE_AND_API_INIT(entropy_cc310_rng, CONFIG_ENTROPY_NAME, + &entropy_cc310_rng_init, + NULL, + NULL, + POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, + &entropy_cc310_rng_api); + +#endif /* CONFIG_ENTROPY_CC310 */ diff --git a/drivers/hw_cc310/CMakeLists.txt b/drivers/hw_cc310/CMakeLists.txt new file mode 100644 index 000000000000..a6cd66eead94 --- /dev/null +++ b/drivers/hw_cc310/CMakeLists.txt @@ -0,0 +1,10 @@ +# +# Copyright (c) 2019 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic +# +zephyr_library() +zephyr_library_sources(hw_cc310.c) + +# Link with the nrf_cc310 platform library +zephyr_library_link_libraries(${IMAGE}platform_cc310) diff --git a/drivers/hw_cc310/Kconfig b/drivers/hw_cc310/Kconfig new file mode 100644 index 000000000000..d2e283809898 --- /dev/null +++ b/drivers/hw_cc310/Kconfig @@ -0,0 +1,42 @@ +# Kconfig - Arm CC310 hw driver for nRF52840 and nRF9160 +# +# Copyright (c) 2019 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic +# + +# Defining MCUBOOT symbol here to avoid an issue discovered when building +# multi image zephyr/mcuboot. +config MCUBOOT + bool + +config HW_CC310_FORCE_ALT + bool + depends on SOC_COMPATIBLE_NRF + help + This option can be enabled to force an alternative implementation of + the Arm CC310 hardware driver. + +if !HW_CC310_FORCE_ALT + +menuconfig HW_CC310 + bool "Arm CC310 hw driver for Nordic devices" + depends on SOC_NRF52840 || (SOC_NRF9160 && !SPM) || (SOC_NRF9160 && TRUSTED_EXECUTION_SECURE) + select NRF_CC310_PLATFORM + default n if MCUBOOT + default y if SOC_NRF52840 || (SOC_NRF9160 && !SPM) || (SOC_NRF9160 && TRUSTED_EXECUTION_SECURE) + help + This option enables the Arm CC310 hw devices in nRF52840 and nRF9160 devices. + +if HW_CC310 + +config HW_CC310_NAME + string "Entropy Device Name" + default "HW_CC310_0" + help + Specify the device name to be used for the HW_CC310 driver. + + +endif # HW_CC310 + +endif # !HW_CC310_FORCE_ALT diff --git a/drivers/hw_cc310/hw_cc310.c b/drivers/hw_cc310/hw_cc310.c new file mode 100644 index 000000000000..834ac4bded0b --- /dev/null +++ b/drivers/hw_cc310/hw_cc310.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic + */ + +#include +#include +#include +#include + +#include + +#if CONFIG_HW_CC310 + +#include "nrf_cc310_platform.h" + +static int hw_cc310_init(struct device *dev) +{ + int res; + + __ASSERT_NO_MSG(dev != NULL); + + /* Set the RTOS abort APIs */ + nrf_cc310_platform_abort_init(); + + /* Set the RTOS mutex APIs */ + nrf_cc310_platform_mutex_init(); + + /* Initialize the cc310 HW with or without RNG support */ +#if CONFIG_ENTROPY_CC310 + res = nrf_cc310_platform_init(); +#else + res = nrf_cc310_platform_init_no_rng(); +#endif + return res; +} + +DEVICE_INIT(hw_cc310, CONFIG_HW_CC310_NAME, hw_cc310_init, + NULL, NULL, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); + +#endif /* CONFIG_HW_CC310 */ diff --git a/lib/lwm2m_carrier/Kconfig b/lib/lwm2m_carrier/Kconfig index 71722c6d411e..2bed21589056 100644 --- a/lib/lwm2m_carrier/Kconfig +++ b/lib/lwm2m_carrier/Kconfig @@ -9,7 +9,7 @@ menuconfig LWM2M_CARRIER depends on NEWLIB_LIBC depends on BSD_LIBRARY && !BSD_LIBRARY_SYS_INIT # Need a source of entropy - depends on TEST_RANDOM_GENERATOR + depends on ENTROPY_HAS_DRIVER # Flash settings depends on FLASH && FLASH_PAGE_LAYOUT depends on MPU_ALLOW_FLASH_WRITE diff --git a/samples/nrf9160/coap_client/overlay-carrier.conf b/samples/nrf9160/coap_client/overlay-carrier.conf index adf6096d01e5..f21161c993d7 100644 --- a/samples/nrf9160/coap_client/overlay-carrier.conf +++ b/samples/nrf9160/coap_client/overlay-carrier.conf @@ -9,9 +9,6 @@ CONFIG_LWM2M_CARRIER=y # The library requires newlibc CONFIG_NEWLIB_LIBC=y -# The library needs a source of entropy -CONFIG_TEST_RANDOM_GENERATOR=y - CONFIG_BSD_LIBRARY=y CONFIG_BSD_LIBRARY_SYS_INIT=n diff --git a/samples/nrf9160/lwm2m_carrier/prj.conf b/samples/nrf9160/lwm2m_carrier/prj.conf index 6891ea9900c7..d5b0992dcffc 100644 --- a/samples/nrf9160/lwm2m_carrier/prj.conf +++ b/samples/nrf9160/lwm2m_carrier/prj.conf @@ -9,9 +9,6 @@ CONFIG_LWM2M_CARRIER=y # The library requires newlibc CONFIG_NEWLIB_LIBC=y -# The library needs a source of entropy -CONFIG_TEST_RANDOM_GENERATOR=y - CONFIG_BSD_LIBRARY=y CONFIG_BSD_LIBRARY_SYS_INIT=n diff --git a/samples/nrf9160/mqtt_simple/overlay-carrier.conf b/samples/nrf9160/mqtt_simple/overlay-carrier.conf index adf6096d01e5..f21161c993d7 100644 --- a/samples/nrf9160/mqtt_simple/overlay-carrier.conf +++ b/samples/nrf9160/mqtt_simple/overlay-carrier.conf @@ -9,9 +9,6 @@ CONFIG_LWM2M_CARRIER=y # The library requires newlibc CONFIG_NEWLIB_LIBC=y -# The library needs a source of entropy -CONFIG_TEST_RANDOM_GENERATOR=y - CONFIG_BSD_LIBRARY=y CONFIG_BSD_LIBRARY_SYS_INIT=n