From 979440a583ad4e3de8b4c29e7a0861206eb271aa Mon Sep 17 00:00:00 2001 From: Vadim Bendebury Date: Fri, 16 Oct 2015 14:53:50 -0700 Subject: 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 Reviewed-on: https://chromium-review.googlesource.com/306780 Reviewed-by: Bill Richardson --- board/cr50/board.c | 24 ++++++++++++++++-------- board/cr50/build.mk | 1 + board/cr50/tpm2/platform.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 board/cr50/tpm2/platform.c 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; +} -- cgit v1.2.1