Skip to content

Commit

Permalink
drivers: entropy: Add the PSA Crypto Random entropy driver
Browse files Browse the repository at this point in the history
This adds an entropy driver calling the PSA Crypto psa_generate_random()
API to get random bytes.

Currently this only uses the TFM provided psa_generate_random().

Signed-off-by: Neil Armstrong <[email protected]>
Signed-off-by: Valerio Setti <[email protected]>
  • Loading branch information
superna9999 authored and carlescufi committed Oct 27, 2022
1 parent e1429d7 commit 3b407a1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
7 changes: 7 additions & 0 deletions drivers/entropy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ zephyr_library_sources_ifdef(CONFIG_ENTROPY_GECKO_TRNG entropy_gecko_trn
zephyr_library_sources_ifdef(CONFIG_ENTROPY_NEORV32_TRNG entropy_neorv32_trng.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_BT_HCI entropy_bt_hci.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_GECKO_SE entropy_gecko_se.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_PSA_CRYPTO_RNG entropy_psa_crypto.c)

if (CONFIG_BUILD_WITH_TFM)
target_include_directories(${ZEPHYR_CURRENT_LIBRARY} PRIVATE
$<TARGET_PROPERTY:tfm,TFM_BINARY_DIR>/install/interface/include
)
endif()
1 change: 1 addition & 0 deletions drivers/entropy/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ source "drivers/entropy/Kconfig.litex"
source "drivers/entropy/Kconfig.gecko"
source "drivers/entropy/Kconfig.neorv32"
source "drivers/entropy/Kconfig.bt_hci"
source "drivers/entropy/Kconfig.psa_crypto"

config ENTROPY_HAS_DRIVER
bool
Expand Down
11 changes: 11 additions & 0 deletions drivers/entropy/Kconfig.psa_crypto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) 2022 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

# PSA Crypto Random source configuration options

config ENTROPY_PSA_CRYPTO_RNG
bool "PSA Crypto Random source Entropy driver"
depends on BUILD_WITH_TFM
select ENTROPY_HAS_DRIVER
help
Enable the PSA Crypto source Entropy driver.
51 changes: 51 additions & 0 deletions drivers/entropy/entropy_psa_crypto.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#define DT_DRV_COMPAT zephyr_psa_crypto_rng

#include <zephyr/drivers/entropy.h>
#include <psa/crypto.h>

/* API implementation: PSA Crypto initialization */
static int entropy_psa_crypto_rng_init(const struct device *dev)
{
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;

ARG_UNUSED(dev);

status = psa_crypto_init();
if (status != PSA_SUCCESS) {
return -EIO;
}

return 0;
}

/* API implementation: get_entropy */
static int entropy_psa_crypto_rng_get_entropy(const struct device *dev,
uint8_t *buffer, uint16_t length)
{
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;

ARG_UNUSED(dev);

status = psa_generate_random(buffer, length);
if (status != PSA_SUCCESS) {
return -EIO;
}

return 0;
}

/* Entropy driver APIs structure */
static const struct entropy_driver_api entropy_psa_crypto_rng_api = {
.get_entropy = entropy_psa_crypto_rng_get_entropy,
};

/* Entropy driver registration */
DEVICE_DT_INST_DEFINE(0, entropy_psa_crypto_rng_init, NULL, NULL, NULL,
PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
&entropy_psa_crypto_rng_api);

0 comments on commit 3b407a1

Please sign in to comment.