diff options
author | Patryk Duda <pdk@semihalf.com> | 2023-03-29 19:14:40 +0200 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-04-13 16:44:16 +0000 |
commit | 59de61f2dbbfcea4534a48eec30b96001105920c (patch) | |
tree | 7675bc9b5e336c9e9d91b6d5f04934c75979e06d /zephyr/shim | |
parent | 74e498c2647dcad68d46022abac34c8c5c6b96b3 (diff) | |
download | chrome-ec-59de61f2dbbfcea4534a48eec30b96001105920c.tar.gz |
zephyr: Add support for RNG devices
This patch provides implementation of trng_rand_bytes() using Zephyr's
entropy API. Zephyr driver is responsible for hardware initialization,
enabling and disabling, so trng_init() and trng_exit() are no-op.
Add hostcmd and console command which can be used to test random number
generator. Both commands have unit tests implemented.
BUG=b:277029648
BRANCH=none
TEST=zmake build bloonchipper
TEST=Enable CONFIG_PLATFORM_EC_CONSOLE_CMD_RAND and compile firmware.
Use 'rand' command to check if random values are generated.
TEST=./twister -v -i -T zephyr/test/drivers/ --test drivers.random
Change-Id: I3fa2c6a1aaf84dc23a8fbb06e557d9e07f5d1d32
Signed-off-by: Patryk Duda <pdk@semihalf.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4381919
Tested-by: Patryk Duda <patrykd@google.com>
Commit-Queue: Patryk Duda <patrykd@google.com>
Reviewed-by: Keith Short <keithshort@chromium.org>
Diffstat (limited to 'zephyr/shim')
-rw-r--r-- | zephyr/shim/include/config_chip.h | 5 | ||||
-rw-r--r-- | zephyr/shim/src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | zephyr/shim/src/random.c | 69 |
3 files changed, 75 insertions, 0 deletions
diff --git a/zephyr/shim/include/config_chip.h b/zephyr/shim/include/config_chip.h index 2659ac66ce..3bb72ff8cf 100644 --- a/zephyr/shim/include/config_chip.h +++ b/zephyr/shim/include/config_chip.h @@ -2368,6 +2368,11 @@ BUILD_ASSERT((DT_NUM_INST_STATUS_OKAY(maxim_max695x)) == 1, #define CONFIG_PANIC_CONSOLE_OUTPUT #endif +#undef CONFIG_RNG +#ifdef CONFIG_PLATFORM_EC_RANDOM +#define CONFIG_RNG +#endif + #undef CONFIG_CMD_CRASH #ifdef CONFIG_PLATFORM_EC_CONSOLE_CMD_CRASH #define CONFIG_CMD_CRASH diff --git a/zephyr/shim/src/CMakeLists.txt b/zephyr/shim/src/CMakeLists.txt index 17932540e4..a2d87f5571 100644 --- a/zephyr/shim/src/CMakeLists.txt +++ b/zephyr/shim/src/CMakeLists.txt @@ -55,6 +55,7 @@ zephyr_library_sources_ifdef(CONFIG_CROS_EC_POWER_SIGNAL_LIST power.c) zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_PWM_HC pwm_hc.c) zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_LED_PWM pwm_led.c) +zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_RANDOM random.c) zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_RTC rtc.c) zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_SWITCHCAP_GPIO switchcap_gpio.c) diff --git a/zephyr/shim/src/random.c b/zephyr/shim/src/random.c new file mode 100644 index 0000000000..a18d83066e --- /dev/null +++ b/zephyr/shim/src/random.c @@ -0,0 +1,69 @@ +/* Copyright 2023 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "console.h" +#include "host_command.h" +#include "printf.h" +#include "system.h" + +#include <zephyr/drivers/entropy.h> +#include <zephyr/kernel.h> + +#define rng DEVICE_DT_GET(DT_CHOSEN(zephyr_entropy)) + +void trng_rand_bytes(void *buffer, size_t len) +{ + /* + * In EC, we use size_t to represent buffer size, but Zephyr uses + * uint16_t. Crash the system if user requested more than UINT16_MAX + * bytes. + */ + if (!device_is_ready(rng) || len > UINT16_MAX) + k_oops(); + + if (entropy_get_entropy(rng, (uint8_t *)buffer, (uint16_t)len)) + k_oops(); +} + +#if defined(CONFIG_PLATFORM_EC_CONSOLE_CMD_RAND) +static int command_rand(const struct shell *shell, int argc, const char **argv) +{ + uint8_t data[32]; + char str_buf[hex_str_buf_size(sizeof(data))]; + + trng_rand_bytes(data, sizeof(data)); + + snprintf_hex_buffer(str_buf, sizeof(str_buf), + HEX_BUF(data, sizeof(data))); + shell_fprintf(shell, SHELL_NORMAL, "rand %s\n", str_buf); + + return EC_SUCCESS; +} +SHELL_CMD_REGISTER(rand, NULL, "Output random bytes to console.", command_rand); +#endif + +#if defined(CONFIG_PLATFORM_EC_HOSTCMD_RAND) +static enum ec_status host_command_rand(struct host_cmd_handler_args *args) +{ + const struct ec_params_rand_num *p = args->params; + struct ec_response_rand_num *r = args->response; + uint16_t num_rand_bytes = p->num_rand_bytes; + + if (system_is_locked()) + return EC_RES_ACCESS_DENIED; + + if (num_rand_bytes > args->response_max) + return EC_RES_OVERFLOW; + + trng_rand_bytes(r->rand, num_rand_bytes); + + args->response_size = num_rand_bytes; + + return EC_RES_SUCCESS; +} + +DECLARE_HOST_COMMAND(EC_CMD_RAND_NUM, host_command_rand, + EC_VER_MASK(EC_VER_RAND_NUM)); +#endif |