altos: Use standard FIPS testing for chaoskey
[fw/altos] / src / drivers / ao_trng_send.c
1 /*
2  * Copyright © 2015 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_adc_fast.h>
21 #include <ao_crc.h>
22 #include <ao_trng_send.h>
23 #include <ao_exti.h>
24 #include <ao_power.h>
25
26 static struct ao_task   ao_trng_send_task;
27 static uint8_t          trng_running;
28 static AO_TICK_TYPE     trng_power_time;
29
30 #define TRNG_ENABLE_DELAY       AO_MS_TO_TICKS(100)
31
32 static uint8_t          random_mutex;
33
34 #if AO_USB_HAS_IN2
35
36 static struct ao_task   ao_trng_send_raw_task;
37
38 static void
39 ao_trng_get_raw(uint16_t *buf)
40 {
41         uint16_t        i;
42         uint16_t        t;
43         uint16_t        v;
44
45         t = ao_adc_get(AO_USB_IN_SIZE>>1);      /* one 16-bit value per two output bytes */
46         for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
47                 v = ao_adc_ring[t];
48                 *buf++ = v;
49                 t = (t + 1) & (AO_ADC_RING_SIZE - 1);
50         }
51         ao_adc_ack(AO_USB_IN_SIZE>>1);
52 }
53
54 static void
55 ao_trng_send_raw(void)
56 {
57         static uint16_t *buffer[2];
58         int             usb_buf_id;
59
60         if (!buffer[0]) {
61                 buffer[0] = ao_usb_alloc();
62                 buffer[1] = ao_usb_alloc();
63                 if (!buffer[0])
64                         ao_exit();
65         }
66
67         usb_buf_id = 0;
68
69         for (;;) {
70                 ao_mutex_get(&random_mutex);
71                 if (!trng_running) {
72                         AO_TICK_TYPE    delay;
73
74                         delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
75                         if (delay > TRNG_ENABLE_DELAY)
76                                 delay = TRNG_ENABLE_DELAY;
77
78                         /* Delay long enough for the HV power supply
79                          * to stabilize so that the first bits we read
80                          * aren't of poor quality
81                          */
82                         ao_delay(delay);
83                         trng_running = TRUE;
84                 }
85 #ifdef AO_LED_TRNG_RAW
86                 ao_led_on(AO_LED_TRNG_RAW);
87 #endif
88                 ao_trng_get_raw(buffer[usb_buf_id]);
89 #ifdef AO_LED_TRNG_RAW
90                 ao_led_off(AO_LED_TRNG_RAW);
91 #endif
92                 ao_mutex_put(&random_mutex);
93                 ao_usb_write2(buffer[usb_buf_id], AO_USB_IN_SIZE);
94                 usb_buf_id = 1-usb_buf_id;
95         }
96 }
97
98 #endif
99
100 static uint32_t previous[AO_USB_IN_SIZE / sizeof (uint16_t)];
101
102 static int
103 ao_trng_get_cooked(uint16_t *buf)
104 {
105         uint16_t        i;
106         uint16_t        t;
107         uint32_t        *rnd = (uint32_t *) ao_adc_ring;
108         uint8_t         mismatch = 0;
109
110         t = ao_adc_get(AO_USB_IN_SIZE) >> 1;            /* one 16-bit value per output byte */
111         for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
112                 uint32_t        v;
113
114                 /* Fetch two values in one operation */
115                 v = rnd[t];
116                 if (v != previous[i]) {
117                         mismatch = 1;
118                         previous[i] = v;
119                 }
120                 t = (t + 1) & ((AO_ADC_RING_SIZE >> 1) - 1);
121
122                 *buf++ = ao_crc_in_32_out_16(v);
123         }
124         ao_adc_ack(AO_USB_IN_SIZE);
125         return mismatch;
126 }
127
128 #define AO_TRNG_START_WAIT      1024
129 #define AO_TRNG_START_CHECK     32
130
131 static void
132 ao_trng_send(void)
133 {
134         static uint16_t *buffer[2];
135         int     usb_buf_id;
136         int     good_bits;
137         int     failed;
138         int     s;
139
140         if (!buffer[0]) {
141                 buffer[0] = ao_usb_alloc();
142                 buffer[1] = ao_usb_alloc();
143                 if (!buffer[0])
144                         ao_exit();
145         }
146
147         usb_buf_id = 0;
148
149 #ifdef AO_TRNG_ENABLE_PORT
150         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
151 #endif
152         trng_power_time = ao_time();
153
154         ao_crc_reset();
155
156         ao_delay(TRNG_ENABLE_DELAY);
157
158         for (s = 0; s < AO_TRNG_START_WAIT; s++) {
159                 if (ao_trng_get_cooked(buffer[0]))
160                         break;
161                 ao_delay(AO_MS_TO_TICKS(10));
162         }
163
164         /* Validate the hardware before enabling USB */
165         failed = 0;
166         for (s = 0; s < AO_TRNG_START_CHECK; s++) {
167                 if (!ao_trng_get_cooked(buffer[0])) {
168                         failed++;
169                         ao_delay(AO_MS_TO_TICKS(10));
170                 }
171         }
172         if (failed > AO_TRNG_START_CHECK / 4)
173                 ao_panic(AO_PANIC_DMA);
174
175 #if AO_USB_HAS_IN2
176         ao_add_task(&ao_trng_send_raw_task, ao_trng_send_raw, "trng_send_raw");
177 #endif
178
179 #ifdef AO_USB_START_DISABLED
180         ao_usb_enable();
181 #endif
182
183         for (;;) {
184                 ao_mutex_get(&random_mutex);
185                 if (!trng_running) {
186                         AO_TICK_TYPE    delay;
187
188                         delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
189                         if (delay > TRNG_ENABLE_DELAY)
190                                 delay = TRNG_ENABLE_DELAY;
191
192                         /* Delay long enough for the HV power supply
193                          * to stabilize so that the first bits we read
194                          * aren't of poor quality
195                          */
196                         ao_delay(delay);
197                         trng_running = TRUE;
198                 }
199 #ifdef AO_LED_TRNG_COOKED
200                 ao_led_on(AO_LED_TRNG_COOKED);
201 #endif
202                 good_bits = ao_trng_get_cooked(buffer[usb_buf_id]);
203 #ifdef AO_LED_TRNG_COOKED
204                 ao_led_off(AO_LED_TRNG_COOKED);
205 #endif
206                 ao_mutex_put(&random_mutex);
207                 if (good_bits) {
208                         ao_usb_write(buffer[usb_buf_id], AO_USB_IN_SIZE);
209                         usb_buf_id = 1-usb_buf_id;
210                         failed = 0;
211                 } else {
212                         failed++;
213                         ao_delay(AO_MS_TO_TICKS(10));
214                         if (failed > 10) {
215                                 ao_usb_disable();
216                                 ao_panic(AO_PANIC_DMA);
217                         }
218                 }
219         }
220 }
221
222 #if AO_POWER_MANAGEMENT
223
224 static void ao_trng_suspend(void *arg)
225 {
226         (void) arg;
227 #ifdef AO_TRNG_ENABLE_PORT
228         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
229 #endif
230         trng_running = FALSE;
231 }
232
233 static void ao_trng_resume(void *arg)
234 {
235         (void) arg;
236 #ifdef AO_TRNG_ENABLE_PORT
237         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
238 #endif
239         trng_power_time = ao_time();
240 }
241
242 static struct ao_power ao_trng_power = {
243         .suspend = ao_trng_suspend,
244         .resume = ao_trng_resume
245 };
246
247 #endif
248
249 void
250 ao_trng_send_init(void)
251 {
252 #ifdef AO_TRNG_ENABLE_PORT
253         ao_enable_output(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
254         ao_power_register(&ao_trng_power);
255 #endif
256         ao_enable_input(AO_RAW_PORT, AO_RAW_BIT, AO_EXTI_MODE_PULL_UP);
257         ao_add_task(&ao_trng_send_task, ao_trng_send, "trng_send");
258 }