1 |
2 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # << NEORV32: neorv32_trng.c - True Random Number Generator (TRNG) HW Driver >> #
|
3 |
|
|
// # ********************************************************************************************* #
|
4 |
|
|
// # BSD 3-Clause License #
|
5 |
|
|
// # #
|
6 |
|
|
// # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
7 |
|
|
// # #
|
8 |
|
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
9 |
|
|
// # permitted provided that the following conditions are met: #
|
10 |
|
|
// # #
|
11 |
|
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
12 |
|
|
// # conditions and the following disclaimer. #
|
13 |
|
|
// # #
|
14 |
|
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
15 |
|
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
16 |
|
|
// # provided with the distribution. #
|
17 |
|
|
// # #
|
18 |
|
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
19 |
|
|
// # endorse or promote products derived from this software without specific prior written #
|
20 |
|
|
// # permission. #
|
21 |
|
|
// # #
|
22 |
|
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
23 |
|
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
24 |
|
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
25 |
|
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
26 |
|
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
27 |
|
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
28 |
|
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
29 |
|
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
30 |
|
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
31 |
|
|
// # ********************************************************************************************* #
|
32 |
|
|
// # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
33 |
|
|
// #################################################################################################
|
34 |
|
|
|
35 |
|
|
|
36 |
|
|
/**********************************************************************//**
|
37 |
|
|
* @file neorv32_trng.c
|
38 |
|
|
* @author Stephan Nolting
|
39 |
|
|
* @brief True Random Number Generator (TRNG) HW driver source file.
|
40 |
|
|
*
|
41 |
|
|
* @note These functions should only be used if the TRNG unit was synthesized (IO_TRNG_USE = true).
|
42 |
|
|
**************************************************************************/
|
43 |
|
|
|
44 |
|
|
#include "neorv32.h"
|
45 |
|
|
#include "neorv32_trng.h"
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
/**********************************************************************//**
|
49 |
|
|
* Check if TRNG unit was synthesized.
|
50 |
|
|
*
|
51 |
|
|
* @return 0 if TRNG was not synthesized, 1 if TRNG is available.
|
52 |
|
|
**************************************************************************/
|
53 |
|
|
int neorv32_trng_available(void) {
|
54 |
|
|
|
55 |
|
|
if (neorv32_cpu_csr_read(CSR_MFEATURES) & (1 << CPU_MFEATURES_IO_TRNG)) {
|
56 |
|
|
return 1;
|
57 |
|
|
}
|
58 |
|
|
else {
|
59 |
|
|
return 0;
|
60 |
|
|
}
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
/**********************************************************************//**
|
65 |
|
|
* Enable and configure true random number generator. The TRNG control register bits are listed in #NEORV32_TRNG_CT_enum.
|
66 |
|
|
*
|
67 |
|
|
* @param[in] tap_mask 16-bit tap mask for GARO array.
|
68 |
|
|
* @return Returns 0 if the provided tap_mask works correctly (GARO-array is oscillating), returns 1 otherwise.
|
69 |
|
|
**************************************************************************/
|
70 |
|
|
int neorv32_trng_setup(uint16_t tap_mask) {
|
71 |
|
|
|
72 |
|
|
TRNG_CT = 0; // reset
|
73 |
|
|
|
74 |
|
|
// configure
|
75 |
|
|
uint32_t tap_config = (uint32_t)(tap_mask);
|
76 |
|
|
tap_config = tap_config << TRNG_CT_TAP_LSB;
|
77 |
|
|
TRNG_CT = (1 << TRNG_CT_EN) | tap_config;
|
78 |
|
|
|
79 |
|
|
neorv32_cpu_delay_ms(1);
|
80 |
|
|
|
81 |
|
|
// check if TRNG is oscillating
|
82 |
|
|
uint16_t trng_data;
|
83 |
|
|
if (neorv32_trng_get(&trng_data) == 0) {
|
84 |
|
|
return 0;
|
85 |
|
|
}
|
86 |
|
|
else {
|
87 |
|
|
return 1;
|
88 |
|
|
}
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
/**********************************************************************//**
|
93 |
|
|
* Disable true random number generator.
|
94 |
|
|
**************************************************************************/
|
95 |
|
|
void neorv32_trng_disable(void) {
|
96 |
|
|
|
97 |
|
|
TRNG_CT &= ~((uint32_t)(1 << TRNG_CT_EN));
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
|
101 |
|
|
/**********************************************************************//**
|
102 |
|
|
* Get random data from TRNG.
|
103 |
|
|
*
|
104 |
|
|
* @param[in,out] data uint16_t pointer for storing random data word
|
105 |
|
|
* @return Valid data when 0, invalid data when 1
|
106 |
|
|
**************************************************************************/
|
107 |
|
|
int neorv32_trng_get(uint16_t *data) {
|
108 |
|
|
|
109 |
|
|
int i;
|
110 |
|
|
const int max_try = 16;
|
111 |
|
|
uint32_t trng_data;
|
112 |
|
|
uint32_t rnd_data;
|
113 |
|
|
|
114 |
|
|
for(i=0; i<max_try; i++) {
|
115 |
|
|
|
116 |
|
|
trng_data = TRNG_DATA;
|
117 |
|
|
rnd_data = trng_data >> TRNG_DATA_LSB;
|
118 |
|
|
*data = (uint16_t)rnd_data;
|
119 |
|
|
|
120 |
|
|
if (trng_data & (1<<TRNG_DATA_VALID)) { // output data valid?
|
121 |
|
|
return 0; // valid
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
return 1; // invalid
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
/**********************************************************************//**
|
131 |
|
|
* Try to find a valid TRNG tap configuration mask.
|
132 |
|
|
*
|
133 |
|
|
* @return Tap mask for configuring the TRNG. If return is zero, no valid tap mask was found.
|
134 |
|
|
**************************************************************************/
|
135 |
|
|
uint16_t neorv32_trng_find_tap_mask(void) {
|
136 |
|
|
|
137 |
|
|
int j;
|
138 |
|
|
uint16_t tap_config16 = 0xfff0; // keep the lowest inverters without feedback
|
139 |
|
|
uint16_t trng_data;
|
140 |
|
|
int success = 0;
|
141 |
|
|
|
142 |
|
|
// tap mask is zero, try to find a nice one
|
143 |
|
|
while (1) {
|
144 |
|
|
|
145 |
|
|
// good mask found?
|
146 |
|
|
if ((success) || (tap_config16 <= 0x0008)) {
|
147 |
|
|
break;
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
// generate a new tap
|
151 |
|
|
tap_config16 -= 0x0008;
|
152 |
|
|
|
153 |
|
|
// install it
|
154 |
|
|
if (neorv32_trng_setup(tap_config16)) {
|
155 |
|
|
continue;
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
// does it work?
|
159 |
|
|
success = 1;
|
160 |
|
|
for (j=0; j < 100; j++) {
|
161 |
|
|
neorv32_cpu_delay_ms(1);
|
162 |
|
|
if (neorv32_trng_get(&trng_data)) { // stop testing on fail
|
163 |
|
|
success = 0;
|
164 |
|
|
break;
|
165 |
|
|
}
|
166 |
|
|
}
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
return (uint16_t)tap_config16;
|
170 |
|
|
}
|
171 |
|
|
|