summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Cross <davidmcross@google.com>2023-05-04 13:14:23 -0700
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-05-12 22:06:18 +0000
commit2800116fe719572f9655819b2ece260fac5e0eaa (patch)
treea0f70de7385f62f62a0143d54d3116347841e098
parent58008e49b6ef5b75965fe4abc507597066860e64 (diff)
downloadchrome-ec-2800116fe719572f9655819b2ece260fac5e0eaa.tar.gz
common: move chip agnostic trng code from stm32 to common
Making this change causes common trng functions to move memory locations. This change only affects fingerprint (FPMCU) boards BUG=b:280889889 TEST=make BOARD=bloonchipper, CONFIG_CMD_RAND enabled in board.h ALLOW_CONFIG=1 ./util/compare_build.sh -b all => All boards match, except the following Fingerprint boards: bloonchipper dartmonkey hatch_fp nami_fp nocturne_fp nucleo-dartmonkey Change-Id: I4dbb5e255be0f61eb7b446af4565bea487c08e52 Signed-off-by: David Cross <davidmcross@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4507892 Reviewed-by: Bobby Casey <bobbycasey@google.com>
-rw-r--r--chip/host/trng.c5
-rw-r--r--chip/stm32/trng.c71
-rw-r--r--common/build.mk3
-rw-r--r--common/trng.c82
-rw-r--r--include/trng.h7
5 files changed, 97 insertions, 71 deletions
diff --git a/chip/host/trng.c b/chip/host/trng.c
index d0def66277..f67dd6c4db 100644
--- a/chip/host/trng.c
+++ b/chip/host/trng.c
@@ -31,6 +31,11 @@ test_mockable void trng_exit(void)
{
}
+test_mockable uint32_t trng_rand(void)
+{
+ return (uint32_t)rand_r(&seed);
+}
+
test_mockable void trng_rand_bytes(void *buffer, size_t len)
{
uint8_t *b, *end;
diff --git a/chip/stm32/trng.c b/chip/stm32/trng.c
index 67d3700cf1..63641741d3 100644
--- a/chip/stm32/trng.c
+++ b/chip/stm32/trng.c
@@ -16,7 +16,7 @@
#include "trng.h"
#include "util.h"
-static uint32_t trng_rand(void)
+uint32_t trng_rand(void)
{
int tries = 300;
/* Wait for a valid random number */
@@ -29,25 +29,6 @@ static uint32_t trng_rand(void)
return STM32_RNG_DR;
}
-test_mockable void trng_rand_bytes(void *buffer, size_t len)
-{
- while (len) {
- uint32_t number = trng_rand();
- size_t cnt = 4;
- /* deal with the lack of alignment guarantee in the API */
- uintptr_t align = (uintptr_t)buffer & 3;
-
- if (len < 4 || align) {
- cnt = MIN(4 - align, len);
- memcpy(buffer, &number, cnt);
- } else {
- *(uint32_t *)buffer = number;
- }
- len -= cnt;
- buffer += cnt;
- }
-}
-
test_mockable void trng_init(void)
{
#ifdef CHIP_FAMILY_STM32L4
@@ -97,53 +78,3 @@ test_mockable void trng_exit(void)
/* Nothing to do */
#endif
}
-
-#if defined(CONFIG_CMD_RAND)
-/*
- * We want to avoid accidentally exposing debug commands in RO since we can't
- * update RO once in production.
- */
-#if defined(SECTION_IS_RW)
-static int command_rand(int argc, const char **argv)
-{
- uint8_t data[32];
- char str_buf[hex_str_buf_size(sizeof(data))];
-
- trng_init();
- trng_rand_bytes(data, sizeof(data));
- trng_exit();
-
- snprintf_hex_buffer(str_buf, sizeof(str_buf),
- HEX_BUF(data, sizeof(data)));
- ccprintf("rand %s\n", str_buf);
-
- return EC_SUCCESS;
-}
-DECLARE_CONSOLE_COMMAND(rand, command_rand, NULL,
- "Output random bytes to console.");
-
-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_init();
- trng_rand_bytes(r->rand, num_rand_bytes);
- trng_exit();
-
- args->response_size = num_rand_bytes;
-
- return EC_SUCCESS;
-}
-
-DECLARE_HOST_COMMAND(EC_CMD_RAND_NUM, host_command_rand,
- EC_VER_MASK(EC_VER_RAND_NUM));
-#endif /* SECTION_IS_RW */
-#endif /* CONFIG_CMD_RAND */
diff --git a/common/build.mk b/common/build.mk
index bbc0789f9b..03d1e7bbaf 100644
--- a/common/build.mk
+++ b/common/build.mk
@@ -141,8 +141,9 @@ common-$(CONFIG_PWM)+=pwm.o
common-$(CONFIG_PWM_KBLIGHT)+=pwm_kblight.o
common-$(CONFIG_KEYBOARD_BACKLIGHT)+=keyboard_backlight.o
common-$(CONFIG_RGB_KEYBOARD)+=rgb_keyboard.o
-common-$(CONFIG_RSA)+=rsa.o
+common-$(CONFIG_RNG)+=trng.o
common-$(CONFIG_ROLLBACK)+=rollback.o
+common-$(CONFIG_RSA)+=rsa.o
common-$(CONFIG_RWSIG)+=rwsig.o vboot/common.o
common-$(CONFIG_RWSIG_TYPE_RWSIG)+=vboot/vb21_lib.o
common-$(CONFIG_MATH_UTIL)+=math_util.o
diff --git a/common/trng.c b/common/trng.c
new file mode 100644
index 0000000000..273a078d7a
--- /dev/null
+++ b/common/trng.c
@@ -0,0 +1,82 @@
+/* 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.
+ */
+
+/* Common Random Number Generation (RNG) routines */
+
+#include "common.h"
+#include "console.h"
+#include "cpu.h"
+#include "host_command.h"
+#include "panic.h"
+#include "printf.h"
+#include "registers.h"
+#include "system.h"
+#include "task.h"
+#include "trng.h"
+#include "util.h"
+
+test_mockable void trng_rand_bytes(void *buffer, size_t len)
+{
+ while (len) {
+ uint32_t number = trng_rand();
+ size_t cnt = 4;
+ /* deal with the lack of alignment guarantee in the API */
+ uintptr_t align = (uintptr_t)buffer & 3;
+
+ if (len < 4 || align) {
+ cnt = MIN(4 - align, len);
+ memcpy(buffer, &number, cnt);
+ } else {
+ *(uint32_t *)buffer = number;
+ }
+ len -= cnt;
+ buffer += cnt;
+ }
+}
+
+#if defined(CONFIG_CMD_RAND)
+/*
+ * We want to avoid accidentally exposing debug commands in RO since we can't
+ * update RO once in production.
+ */
+#if defined(SECTION_IS_RW)
+static int command_rand(int argc, const char **argv)
+{
+ uint8_t data[32];
+ char str_buf[hex_str_buf_size(sizeof(data))];
+
+ trng_init();
+ trng_rand_bytes(data, sizeof(data));
+ trng_exit();
+ snprintf_hex_buffer(str_buf, sizeof(str_buf),
+ HEX_BUF(data, sizeof(data)));
+ ccprintf("rand %s\n", str_buf);
+
+ return EC_RES_SUCCESS;
+}
+DECLARE_CONSOLE_COMMAND(rand, command_rand, NULL,
+ "Output random bytes to console.");
+
+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_init();
+ trng_rand_bytes(r->rand, num_rand_bytes);
+ trng_exit();
+ 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 /* SECTION_IS_RW */
+#endif /* CONFIG_CMD_RAND */
diff --git a/include/trng.h b/include/trng.h
index ca8144eeec..06fbcc8544 100644
--- a/include/trng.h
+++ b/include/trng.h
@@ -26,6 +26,13 @@
void trng_init(void);
/**
+ * Generate true random number.
+ *
+ * Not supported by all platforms.
+ **/
+uint32_t trng_rand(void);
+
+/**
* Shutdown the true random number generator.
*
* The opposite operation of trng_init(), disable the hardware resources