summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVadim Bendebury <vbendeb@chromium.org>2015-10-16 14:53:50 -0700
committerchrome-bot <chrome-bot@chromium.org>2015-10-17 09:53:07 -0700
commit979440a583ad4e3de8b4c29e7a0861206eb271aa (patch)
tree16e285288a6b1c8d3be3a246e2880c0ca04230d2
parent92e350197aae867bebd3c017ca678d11c9a64162 (diff)
downloadchrome-ec-979440a583ad4e3de8b4c29e7a0861206eb271aa.tar.gz
Provide interface to random number generator
It needs to be possible to retrieve an arbitrary number of random bytes, which is accomplished by this _cpri__GenerateRandom() implementation. Also, this patch rearranges the board initialization code to initialize both interrupts and the TRNG. CL-DEPEND=CL:306781 BRANCH=none BUG=chrome-os-partner:43025 TEST=ran a couple of tests retrieving random numbers from TPM, observed randomly looking values generated (this is not a validation of the TRNG implementation). Change-Id: I6314cdf5e6e96443b3fadb7f1502fc8477c41d0f Signed-off-by: Vadim Bendebury <vbendeb@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/306780 Reviewed-by: Bill Richardson <wfrichar@chromium.org>
-rw-r--r--board/cr50/board.c24
-rw-r--r--board/cr50/build.mk1
-rw-r--r--board/cr50/tpm2/platform.c33
3 files changed, 50 insertions, 8 deletions
diff --git a/board/cr50/board.c b/board/cr50/board.c
index 36b4fea164..8dee558cca 100644
--- a/board/cr50/board.c
+++ b/board/cr50/board.c
@@ -10,6 +10,7 @@
#include "hooks.h"
#include "registers.h"
#include "task.h"
+#include "trng.h"
#include "usb.h"
#include "usb_hid.h"
#include "util.h"
@@ -64,18 +65,25 @@ void button_event(enum gpio_signal signal)
gpio_set_level(signal - GPIO_SW_N + GPIO_LED_4, v);
}
+static void init_interrutps(void)
+{
+ int i;
+ static const enum gpio_signal gpio_signals[] = {
+ GPIO_SW_N, GPIO_SW_S, GPIO_SW_W, GPIO_SW_E,
+ GPIO_SW_N_, GPIO_SW_S_, GPIO_SW_W_, GPIO_SW_E_
+ };
+
+ for (i = 0; i < ARRAY_SIZE(gpio_signals); i++)
+ gpio_enable_interrupt(gpio_signals[i]);
+}
+
/* Initialize board. */
static void board_init(void)
{
- gpio_enable_interrupt(GPIO_SW_N);
- gpio_enable_interrupt(GPIO_SW_S);
- gpio_enable_interrupt(GPIO_SW_W);
- gpio_enable_interrupt(GPIO_SW_E);
- gpio_enable_interrupt(GPIO_SW_N_);
- gpio_enable_interrupt(GPIO_SW_S_);
- gpio_enable_interrupt(GPIO_SW_W_);
- gpio_enable_interrupt(GPIO_SW_E_);
+ init_interrutps();
+ init_trng();
}
+
DECLARE_HOOK(HOOK_INIT, board_init, HOOK_PRIO_DEFAULT);
#if defined(CONFIG_USB)
diff --git a/board/cr50/build.mk b/board/cr50/build.mk
index 0b3f80c96d..61fc79e903 100644
--- a/board/cr50/build.mk
+++ b/board/cr50/build.mk
@@ -27,6 +27,7 @@ dirs-y += $(BDIR)/tpm2
# Objects that we need to build
board-y = board.o
+board-y += tpm2/platform.o
board-y += tpm2/NVMem.o
# Build and link with an external library
diff --git a/board/cr50/tpm2/platform.c b/board/cr50/tpm2/platform.c
new file mode 100644
index 0000000000..afe5c86c74
--- /dev/null
+++ b/board/cr50/tpm2/platform.c
@@ -0,0 +1,33 @@
+/* Copyright 2015 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "Platform.h"
+#include "TPM_Types.h"
+
+#include "trng.h"
+
+UINT16 _cpri__GenerateRandom(INT32 randomSize,
+ BYTE *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;
+}