-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpprng.c
84 lines (66 loc) · 1.73 KB
/
pprng.c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-FileCopyrightText: 2020 Foundation Devices, Inc. <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// SPDX-FileCopyrightText: 2018 Coinkite, Inc. <coldcardwallet.com>
// SPDX-License-Identifier: GPL-3.0-only
//
/*
* (c) Copyright 2018 by Coinkite Inc. This file is part of Coldcard <coldcardwallet.com>
* and is covered by GPLv3 license found in COPYING.
*/
#include <string.h>
#include "stm32h7xx_hal_conf.h"
#include "delay.h"
#include "utils.h"
#include "pprng.h"
void rng_setup(void)
{
if (RNG->CR & RNG_CR_RNGEN) {
// already setup
return;
}
// Enable the RNG clocl
__HAL_RCC_RNG_CLK_ENABLE();
// Enable the RNG
RNG->CR |= RNG_CR_RNGEN;
// Sample twice to be sure that we have a
// valid RNG result.
uint32_t chk = rng_sample();
uint32_t chk2 = rng_sample();
// die if we are clearly not getting random values
if (chk == 0 ||
chk == ~0 ||
chk2 == 0 ||
chk2 == ~0 ||
chk == chk2) {
while(1) ;
}
}
uint32_t rng_sample(void)
{
static uint32_t last_rng_result;
while(1) {
// Check if data register contains valid random data
while(!(RNG->SR & RNG_SR_DRDY)) {
// busy wait; okay to get stuck here... better than failing.
}
// Get the new number
uint32_t rv = RNG->DR;
if(rv != last_rng_result && rv) {
last_rng_result = rv;
return rv;
}
// keep trying if not a new number
}
// NOT-REACHED
}
void rng_buffer(uint8_t *result, int len)
{
while(len > 0) {
uint32_t t = rng_sample();
memcpy(result, &t, MIN(4, len));
len -= 4;
result += 4;
}
}
// EOF