summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Hughes <tomhughes@chromium.org>2022-06-17 17:15:14 -0700
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-06-28 00:28:45 +0000
commita6b0b3554f59cc9b0c4aae9bff7dff075f2089a9 (patch)
treee6cfc5ad8589d33ed5e3bbe9bbeac2719181a67c
parentd4e422b6bbfcb434092eaed39c3849ff75e797ce (diff)
downloadchrome-ec-a6b0b3554f59cc9b0c4aae9bff7dff075f2089a9.tar.gz
trng: Rename rand to trng_rand
The declaration for rand conflicts with the standard library declaration so rename it from "rand" to "trng_rand". This has the benefit of making it obvious when we're using the true random number generator. For consistency, this also renames init_trng/exit_trng to trng_init/trng_exit. BRANCH=none BUG=b:234181908 TEST=./util/compare_build.sh -b all -j 120 => MATCH Signed-off-by: Tom Hughes <tomhughes@chromium.org> Change-Id: Ic3305a91263c45786c051eaa5b3689e7464aa0ab Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3712032 Reviewed-by: Bobby Casey <bobbycasey@google.com> Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
-rw-r--r--chip/host/trng.c6
-rw-r--r--chip/stm32/trng.c22
-rw-r--r--common/fpsensor/fpsensor.c26
-rw-r--r--common/rollback.c6
-rw-r--r--include/trng.h12
-rw-r--r--third_party/boringssl/common/curve25519.c2
6 files changed, 36 insertions, 38 deletions
diff --git a/chip/host/trng.c b/chip/host/trng.c
index 8407aa6ea1..cb69268e34 100644
--- a/chip/host/trng.c
+++ b/chip/host/trng.c
@@ -21,17 +21,17 @@
static unsigned int seed;
-test_mockable void init_trng(void)
+test_mockable void trng_init(void)
{
seed = 0;
srand(seed);
}
-test_mockable void exit_trng(void)
+test_mockable void trng_exit(void)
{
}
-test_mockable void rand_bytes(void *buffer, size_t len)
+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 48d5335c53..447ccbccad 100644
--- a/chip/stm32/trng.c
+++ b/chip/stm32/trng.c
@@ -15,7 +15,7 @@
#include "trng.h"
#include "util.h"
-uint32_t rand(void)
+uint32_t trng_rand(void)
{
int tries = 300;
/* Wait for a valid random number */
@@ -28,10 +28,10 @@ uint32_t rand(void)
return STM32_RNG_DR;
}
-test_mockable void rand_bytes(void *buffer, size_t len)
+test_mockable void trng_rand_bytes(void *buffer, size_t len)
{
while (len) {
- uint32_t number = rand();
+ 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;
@@ -47,7 +47,7 @@ test_mockable void rand_bytes(void *buffer, size_t len)
}
}
-test_mockable void init_trng(void)
+test_mockable void trng_init(void)
{
#ifdef CHIP_FAMILY_STM32L4
/* Enable the 48Mhz internal RC oscillator */
@@ -84,7 +84,7 @@ test_mockable void init_trng(void)
STM32_RNG_CR |= STM32_RNG_CR_RNGEN;
}
-test_mockable void exit_trng(void)
+test_mockable void trng_exit(void)
{
STM32_RNG_CR &= ~STM32_RNG_CR_RNGEN;
STM32_RCC_AHB2ENR &= ~STM32_RCC_AHB2ENR_RNGEN;
@@ -107,9 +107,9 @@ static int command_rand(int argc, char **argv)
{
uint8_t data[32];
- init_trng();
- rand_bytes(data, sizeof(data));
- exit_trng();
+ trng_init();
+ trng_rand_bytes(data, sizeof(data));
+ trng_exit();
ccprintf("rand %ph\n", HEX_BUF(data, sizeof(data)));
@@ -130,9 +130,9 @@ static enum ec_status host_command_rand(struct host_cmd_handler_args *args)
if (num_rand_bytes > args->response_max)
return EC_RES_OVERFLOW;
- init_trng();
- rand_bytes(r->rand, num_rand_bytes);
- exit_trng();
+ trng_init();
+ trng_rand_bytes(r->rand, num_rand_bytes);
+ trng_exit();
args->response_size = num_rand_bytes;
diff --git a/common/fpsensor/fpsensor.c b/common/fpsensor/fpsensor.c
index 12904c0b39..a491e1af84 100644
--- a/common/fpsensor/fpsensor.c
+++ b/common/fpsensor/fpsensor.c
@@ -472,11 +472,11 @@ static enum ec_status fp_command_frame(struct host_cmd_handler_args *args)
*/
enc_info = (void *)fp_enc_buffer;
enc_info->struct_version = FP_TEMPLATE_FORMAT_VERSION;
- init_trng();
- rand_bytes(enc_info->nonce, FP_CONTEXT_NONCE_BYTES);
- rand_bytes(enc_info->encryption_salt,
- FP_CONTEXT_ENCRYPTION_SALT_BYTES);
- exit_trng();
+ trng_init();
+ trng_rand_bytes(enc_info->nonce, FP_CONTEXT_NONCE_BYTES);
+ trng_rand_bytes(enc_info->encryption_salt,
+ FP_CONTEXT_ENCRYPTION_SALT_BYTES);
+ trng_exit();
if (fgr == template_newly_enrolled) {
/*
@@ -485,10 +485,10 @@ static enum ec_status fp_command_frame(struct host_cmd_handler_args *args)
* value.
*/
template_newly_enrolled = FP_NO_SUCH_TEMPLATE;
- init_trng();
- rand_bytes(fp_positive_match_salt[fgr],
- FP_POSITIVE_MATCH_SALT_BYTES);
- exit_trng();
+ trng_init();
+ trng_rand_bytes(fp_positive_match_salt[fgr],
+ FP_POSITIVE_MATCH_SALT_BYTES);
+ trng_exit();
}
ret = derive_encryption_key(key, enc_info->encryption_salt);
@@ -647,10 +647,10 @@ static enum ec_status fp_command_template(struct host_cmd_handler_args *args)
sizeof(fp_template[0]));
if (template_needs_validation_value(enc_info)) {
CPRINTS("fgr%d: Generating positive match salt.", idx);
- init_trng();
- rand_bytes(positive_match_salt,
- FP_POSITIVE_MATCH_SALT_BYTES);
- exit_trng();
+ trng_init();
+ trng_rand_bytes(positive_match_salt,
+ FP_POSITIVE_MATCH_SALT_BYTES);
+ trng_exit();
}
if (bytes_are_trivial(positive_match_salt,
sizeof(fp_positive_match_salt[0]))) {
diff --git a/common/rollback.c b/common/rollback.c
index 984058c49a..3f9e176782 100644
--- a/common/rollback.c
+++ b/common/rollback.c
@@ -400,9 +400,9 @@ static void add_entropy_deferred(void)
if (add_entropy_action == ADD_ENTROPY_RESET_ASYNC)
repeat = ROLLBACK_REGIONS;
- init_trng();
+ trng_init();
do {
- rand_bytes(rand, sizeof(rand));
+ trng_rand_bytes(rand, sizeof(rand));
if (rollback_add_entropy(rand, sizeof(rand)) != EC_SUCCESS) {
add_entropy_rv = EC_RES_ERROR;
goto out;
@@ -411,7 +411,7 @@ static void add_entropy_deferred(void)
add_entropy_rv = EC_RES_SUCCESS;
out:
- exit_trng();
+ trng_exit();
}
DECLARE_DEFERRED(add_entropy_deferred);
diff --git a/include/trng.h b/include/trng.h
index cea4555b41..969366ae8e 100644
--- a/include/trng.h
+++ b/include/trng.h
@@ -14,32 +14,30 @@
*
* Not supported by all platforms.
**/
-void init_trng(void);
+void trng_init(void);
/**
* Shutdown the true random number generator.
*
- * The opposite operation of init_trng(), disable the hardware resources
+ * The opposite operation of trng_init(), disable the hardware resources
* used by the TRNG to save power.
*
* Not supported by all platforms.
**/
-void exit_trng(void);
+void trng_exit(void);
/**
* Retrieve a 32 bit random value.
*
* Not supported on all platforms.
**/
-#ifndef HIDE_EC_STDLIB
-uint32_t rand(void);
-#endif
+uint32_t trng_rand(void);
/**
* Output len random bytes into buffer.
*
* Not supported on all platforms.
**/
-void rand_bytes(void *buffer, size_t len);
+void trng_rand_bytes(void *buffer, size_t len);
#endif /* __EC_INCLUDE_TRNG_H */
diff --git a/third_party/boringssl/common/curve25519.c b/third_party/boringssl/common/curve25519.c
index 2a7fad6509..b74d3c7d97 100644
--- a/third_party/boringssl/common/curve25519.c
+++ b/third_party/boringssl/common/curve25519.c
@@ -27,7 +27,7 @@
#ifdef CONFIG_RNG
void X25519_keypair(uint8_t out_public_value[32], uint8_t out_private_key[32]) {
- rand_bytes(out_private_key, 32);
+ trng_rand_bytes(out_private_key, 32);
/* All X25519 implementations should decode scalars correctly (see
* https://tools.ietf.org/html/rfc7748#section-5). However, if an