1 |
198 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # < TRNG test program > #
|
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 NEO430 Processor - https://github.com/stnolting/neo430 #
|
33 |
|
|
// #################################################################################################
|
34 |
|
|
|
35 |
|
|
|
36 |
|
|
// Libraries
|
37 |
|
|
#include <stdint.h>
|
38 |
|
|
#include <neo430.h>
|
39 |
|
|
|
40 |
|
|
// Configuration
|
41 |
|
|
#define BAUD_RATE 19200
|
42 |
|
|
#define NUM_SAMPLES 2000000000
|
43 |
|
|
#define TRNG_TAP_MASK 0b01010001000000 // highly experimental!
|
44 |
|
|
|
45 |
|
|
// Global variables
|
46 |
|
|
uint32_t rnd_hist[256];
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
/* ------------------------------------------------------------
|
50 |
|
|
* INFO Main function
|
51 |
|
|
* ------------------------------------------------------------ */
|
52 |
|
|
int main(void) {
|
53 |
|
|
|
54 |
|
|
// setup UART
|
55 |
|
|
neo430_uart_setup(BAUD_RATE);
|
56 |
|
|
|
57 |
|
|
// intro text
|
58 |
|
|
neo430_printf("\n<<< TRNG Test >>>\n");
|
59 |
|
|
|
60 |
|
|
// check if TRNG was synthesized, exit if not available
|
61 |
|
|
if (!(SYS_FEATURES & (1<<SYS_TRNG_EN))) {
|
62 |
|
|
neo430_printf("Error! No TRNG synthesized!");
|
63 |
|
|
return 1;
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
// reset & start TRNG
|
67 |
|
|
uint8_t rnd_data = 0;
|
68 |
|
|
uint16_t rnd_status = 0;
|
69 |
|
|
neo430_trng_enable(TRNG_TAP_MASK);
|
70 |
|
|
|
71 |
|
|
// make sure TRNG is running
|
72 |
|
|
int k;
|
73 |
|
|
for(k=0; k<1024; k++){
|
74 |
|
|
rnd_status = neo430_trng_get(&rnd_data);
|
75 |
|
|
if (rnd_status) {
|
76 |
|
|
neo430_trng_disable();
|
77 |
|
|
neo430_cpu_delay(100);
|
78 |
|
|
neo430_trng_enable(TRNG_TAP_MASK); // reset TRNG
|
79 |
|
|
}
|
80 |
|
|
else {
|
81 |
|
|
break;
|
82 |
|
|
}
|
83 |
|
|
if (k == 1000) {
|
84 |
|
|
neo430_printf("\nTRNG calibration error!\n");
|
85 |
|
|
return 0;
|
86 |
|
|
}
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
while(1) {
|
90 |
|
|
|
91 |
|
|
// main menu
|
92 |
|
|
neo430_printf("\nCommands:\n"
|
93 |
|
|
" a: Print random numbers (abort by pressing any key)\n"
|
94 |
|
|
" b: Generate random data histogram (%n samples)\n"
|
95 |
|
|
" x: Return to bootloader\n", (uint32_t)NUM_SAMPLES);
|
96 |
|
|
|
97 |
|
|
neo430_printf("CMD:> ");
|
98 |
|
|
char cmd = neo430_uart_getc();
|
99 |
|
|
neo430_uart_putc(cmd); // echo
|
100 |
|
|
neo430_printf("\n");
|
101 |
|
|
|
102 |
|
|
// output RND data
|
103 |
|
|
if (cmd == 'a') {
|
104 |
|
|
uint32_t num_samples = 0;
|
105 |
|
|
while(1) {
|
106 |
|
|
rnd_status = neo430_trng_get(&rnd_data);
|
107 |
|
|
if (rnd_status) {
|
108 |
|
|
neo430_printf("\nTRNG error!\n");
|
109 |
|
|
break;
|
110 |
|
|
}
|
111 |
|
|
neo430_printf("%u ", (uint16_t)rnd_data);
|
112 |
|
|
num_samples++;
|
113 |
|
|
if (neo430_uart_char_received()) { // abort when key pressed
|
114 |
|
|
neo430_printf("\nNumber of samples: %n\n", num_samples);
|
115 |
|
|
break;
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
// compute histogram
|
121 |
|
|
else if (cmd == 'b') {
|
122 |
|
|
// clear histogram memory
|
123 |
|
|
uint16_t i;
|
124 |
|
|
for (i=0; i<256; i++) {
|
125 |
|
|
rnd_hist[i] = 0;
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
// generate histogram
|
129 |
|
|
neo430_printf("Sampling data (%n samples). This may take some time...\n", (uint32_t)NUM_SAMPLES);
|
130 |
|
|
uint32_t j;
|
131 |
|
|
for (j=0; j<NUM_SAMPLES; j++) {
|
132 |
|
|
rnd_status = neo430_trng_get(&rnd_data);
|
133 |
|
|
if (rnd_status
|
134 |
|
|
) {
|
135 |
|
|
neo430_printf("\nTRNG error!\n");
|
136 |
|
|
break;
|
137 |
|
|
}
|
138 |
|
|
rnd_hist[rnd_data]++;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
// output results
|
142 |
|
|
neo430_printf("rnd_hist:\n");
|
143 |
|
|
for(i=0; i<256; i++) {
|
144 |
|
|
neo430_printf("%u =%n\n", i, rnd_hist[i]);
|
145 |
|
|
}
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
// exit
|
149 |
|
|
else if (cmd == 'x') {
|
150 |
|
|
if (!(SYS_FEATURES & (1<<SYS_BTLD_EN)))
|
151 |
|
|
neo430_printf("No bootloader installed!\n");
|
152 |
|
|
else
|
153 |
|
|
asm volatile ("mov #0xF000, r0");
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
else {
|
157 |
|
|
neo430_printf("Invalid option.\n");
|
158 |
|
|
}
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
return 0;
|
162 |
|
|
}
|
163 |
|
|
|