summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornagendra modadugu <ngm@google.com>2015-12-21 16:18:26 -0800
committerchrome-bot <chrome-bot@chromium.org>2015-12-22 13:12:53 -0800
commit617fb66fc71bbd21edb80d8093f60fa9a8ed6c1c (patch)
tree2863e8a4af87c8c5ce4ea53d6c9ccd76213528db
parente415307589ed3833fa4e07834139b7ef404a687e (diff)
downloadchrome-ec-617fb66fc71bbd21edb80d8093f60fa9a8ed6c1c.tar.gz
Move rand_bytes implementation from tpm2 to chip/g.
BRANCH=none TEST=none BUG=chrome-os-partner:43025,chrome-os-partner:47524 Signed-off-by: nagendra modadugu <ngm@google.com> Change-Id: Ic7a850fdf2594ac1981237edda8dceb16cc7cbe6 Reviewed-on: https://chromium-review.googlesource.com/319155 Commit-Ready: Nagendra Modadugu <ngm@google.com> Tested-by: Nagendra Modadugu <ngm@google.com> Reviewed-by: Vadim Bendebury <vbendeb@chromium.org>
-rw-r--r--board/cr50/tpm2/platform.c25
-rw-r--r--chip/g/trng.c22
-rw-r--r--include/trng.h9
3 files changed, 35 insertions, 21 deletions
diff --git a/board/cr50/tpm2/platform.c b/board/cr50/tpm2/platform.c
index afe5c86c74..20b5854625 100644
--- a/board/cr50/tpm2/platform.c
+++ b/board/cr50/tpm2/platform.c
@@ -8,26 +8,9 @@
#include "trng.h"
-UINT16 _cpri__GenerateRandom(INT32 randomSize,
- BYTE *buffer)
+uint16_t _cpri__GenerateRandom(size_t random_size,
+ uint8_t *buffer)
{
- int random_togo = 0;
- int buffer_index = 0;
- uint32_t random_value;
-
- /*
- * Retrieve random numbers in 4 byte quantities and pack as many bytes
- * as needed into 'buffer'. If randomSize is not divisible by 4, the
- * remaining random bytes get dropped.
- */
- while (buffer_index < randomSize) {
- if (!random_togo) {
- random_value = rand();
- random_togo = sizeof(random_value);
- }
- buffer[buffer_index++] = random_value >>
- ((random_togo-- - 1) * 8);
- }
-
- return randomSize;
+ rand_bytes(buffer, random_size);
+ return random_size;
}
diff --git a/chip/g/trng.c b/chip/g/trng.c
index e462127da8..b6a062d58d 100644
--- a/chip/g/trng.c
+++ b/chip/g/trng.c
@@ -20,3 +20,25 @@ uint32_t rand(void)
;
return GREAD(TRNG, READ_DATA);
}
+
+void rand_bytes(void *buffer, size_t len)
+{
+ int random_togo = 0;
+ int buffer_index = 0;
+ uint32_t random_value;
+ uint8_t *buf = (uint8_t *) buffer;
+
+ /*
+ * Retrieve random numbers in 4 byte quantities and pack as many bytes
+ * as needed into 'buffer'. If len is not divisible by 4, the
+ * remaining random bytes get dropped.
+ */
+ while (buffer_index < len) {
+ if (!random_togo) {
+ random_value = rand();
+ random_togo = sizeof(random_value);
+ }
+ buf[buffer_index++] = random_value >>
+ ((random_togo-- - 1) * 8);
+ }
+}
diff --git a/include/trng.h b/include/trng.h
index fe1c96825e..a35496c3fd 100644
--- a/include/trng.h
+++ b/include/trng.h
@@ -5,6 +5,8 @@
#ifndef __EC_INCLUDE_TRNG_H
#define __EC_INCLUDE_TRNG_H
+#include <sys/types.h>
+
/**
* Initialize the true random number generator.
*
@@ -19,4 +21,11 @@ void init_trng(void);
**/
uint32_t rand(void);
+/**
+ * Output len random bytes into buffer.
+ *
+ * Not supported on all platforms.
+ **/
+void rand_bytes(void *buffer, size_t len);
+
#endif /* __EC_INCLUDE_TRNG_H */