blob: b6a062d58d16172f9da056109d003e0c68904443 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
/* 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 "registers.h"
void init_trng(void)
{
GWRITE(TRNG, POWER_DOWN_B, 1);
GWRITE(TRNG, GO_EVENT, 1);
while (GREAD(TRNG, EMPTY))
;
GREAD(TRNG, READ_DATA);
}
uint32_t rand(void)
{
while (GREAD(TRNG, EMPTY))
;
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);
}
}
|