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>
This commit is contained in:
nagendra modadugu
2015-12-21 16:18:26 -08:00
committed by chrome-bot
parent e415307589
commit 617fb66fc7
3 changed files with 35 additions and 21 deletions

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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 */