forked from kiibohd/controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entropy.c
428 lines (320 loc) · 10.6 KB
/
entropy.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* Entropy - A entropy (random number) generator for the Arduino
* Forked from https://github.com/pmjdebruijn/Arduino-Entropy-Library (2017)
*
* The latest version of this library will always be stored in the following
* google code repository:
* http://code.google.com/p/avr-hardware-random-number-generation/source/browse/#git%2FEntropy
* with more information available on the libraries wiki page
* http://code.google.com/p/avr-hardware-random-number-generation/wiki/WikiAVRentropy
*
* Copyright 2014 by Walter Anderson
* Modifications 2017-2018 by Jacob Alexander
*
* Entropy is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Entropy is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Entropy. If not, see <http://www.gnu.org/licenses/>.
*/
// ----- Target Includes -----
#include "mcu_compat.h"
#if defined(_host_)
// ----- Includes -----
#include <time.h>
#include <stdlib.h>
#include "entropy.h"
// ----- Functions -----
void rand_initialize()
{
}
void rand_disable()
{
}
uint8_t rand_available()
{
return 1;
}
// Pseudo-random value using clock
// XXX (HaaTa) Host shouldn't need random numbers.
// If they are needed, this should be changed to something better.
uint32_t rand_value32()
{
srand( time( NULL ) );
return rand();
}
#elif defined(_kinetis_)
// ----- Includes -----
#include "atomic.h"
#include "entropy.h"
#include "kinetis.h"
// ----- Defines -----
#define WDT_MAX_8INT 0xFF
#define WDT_MAX_16INT 0xFFFF
#define WDT_MAX_32INT 0xFFFFFFFF
#define WDT_POOL_SIZE 8
#define gWDT_buffer_SIZE 32
// ----- Variables -----
uint8_t gWDT_buffer[gWDT_buffer_SIZE];
uint8_t gWDT_buffer_position;
uint8_t gWDT_loop_counter;
volatile uint8_t gWDT_pool_start;
volatile uint8_t gWDT_pool_end;
volatile uint8_t gWDT_pool_count;
volatile uint32_t gWDT_entropy_pool[WDT_POOL_SIZE];
// ----- Functions -----
// This function initializes the global variables needed to implement the circular entropy pool and
// the buffer that holds the raw Timer 1 values that are used to create the entropy pool. It then
// Initializes the Low Power Timer (LPTMR) to perform an interrupt every 2048 clock cycles, (about
// 16 ms) which is as fast as it can be set.
void rand_initialize()
{
gWDT_buffer_position = 0;
gWDT_pool_start = 0;
gWDT_pool_end = 0;
gWDT_pool_count = 0;
SIM_SCGC5 |= SIM_SCGC5_LPTIMER;
LPTMR0_CSR = 0b10000100;
LPTMR0_PSR = 0b00000101; // PCS=01 : 1 kHz clock
LPTMR0_CMR = 0x0006; // smaller number = faster random numbers...
LPTMR0_CSR = 0b01000101;
NVIC_ENABLE_IRQ( IRQ_LPTMR );
}
// Disables interrupt, thus stopping CPU usage generating entropy
void rand_disable()
{
NVIC_DISABLE_IRQ( IRQ_LPTMR );
}
// This function returns a uniformly distributed random integer in the range
// of [0,0xFFFFFFFF] as long as some entropy exists in the pool and a 0
// otherwise. To ensure a proper random return the available() function
// should be called first to ensure that entropy exists.
//
// The pool is implemented as an 8 value circular buffer
uint32_t rand_value32()
{
uint32_t retVal = 0;
uint8_t waiting;
while ( gWDT_pool_count < 1 )
{
waiting += 1;
}
ATOMIC_BLOCK( ATOMIC_RESTORESTATE )
{
retVal = gWDT_entropy_pool[gWDT_pool_start];
gWDT_pool_start = (gWDT_pool_start + 1) % WDT_POOL_SIZE;
--gWDT_pool_count;
}
return retVal;
}
// This function returns a unsigned char (8-bit) with the number of unsigned long values
// in the entropy pool
uint8_t rand_available()
{
return gWDT_pool_count;
}
// This interrupt service routine is called every time the LPTMR interrupt is triggered.
// With the default configuration that is approximately once every 16ms, producing
// approximately two 32-bit integer values every second.
//
// The pool is implemented as an 8 value circular buffer
static void isr_hardware_neutral( uint8_t val )
{
gWDT_buffer[gWDT_buffer_position] = val;
gWDT_buffer_position++; // every time the WDT interrupt is triggered
if ( gWDT_buffer_position >= gWDT_buffer_SIZE )
{
gWDT_pool_end = (gWDT_pool_start + gWDT_pool_count) % WDT_POOL_SIZE;
// The following code is an implementation of Jenkin's one at a time hash
// This hash function has had preliminary testing to verify that it
// produces reasonably uniform random results when using WDT jitter
// on a variety of Arduino platforms
for ( gWDT_loop_counter = 0; gWDT_loop_counter < gWDT_buffer_SIZE; ++gWDT_loop_counter )
{
gWDT_entropy_pool[gWDT_pool_end] += gWDT_buffer[gWDT_loop_counter];
gWDT_entropy_pool[gWDT_pool_end] += (gWDT_entropy_pool[gWDT_pool_end] << 10);
gWDT_entropy_pool[gWDT_pool_end] ^= (gWDT_entropy_pool[gWDT_pool_end] >> 6);
}
gWDT_entropy_pool[gWDT_pool_end] += (gWDT_entropy_pool[gWDT_pool_end] << 3);
gWDT_entropy_pool[gWDT_pool_end] ^= (gWDT_entropy_pool[gWDT_pool_end] >> 11);
gWDT_entropy_pool[gWDT_pool_end] += (gWDT_entropy_pool[gWDT_pool_end] << 15);
gWDT_entropy_pool[gWDT_pool_end] = gWDT_entropy_pool[gWDT_pool_end];
// Start collecting the next 32 bytes of Timer 1 counts
gWDT_buffer_position = 0;
// The entropy pool is full
if (gWDT_pool_count == WDT_POOL_SIZE)
{
gWDT_pool_start = (gWDT_pool_start + 1) % WDT_POOL_SIZE;
}
// Add another unsigned long (32 bits) to the entropy pool
else
{
++gWDT_pool_count;
}
}
}
// ----- Interrupts -----
void lptmr_isr()
{
LPTMR0_CSR = 0b10000100;
LPTMR0_CSR = 0b01000101;
isr_hardware_neutral(SYST_CVR);
}
#elif defined(_sam_)
// ----- Includes -----
#include <stdlib.h>
#include "atomic.h"
#include "entropy.h"
#include "sam.h"
// ----- Defines -----
#define WDT_MAX_8INT 0xFF
#define WDT_MAX_16INT 0xFFFF
#define WDT_MAX_32INT 0xFFFFFFFF
#define WDT_POOL_SIZE 8
#define gWDT_buffer_SIZE 32
// ----- Variables -----
uint8_t gWDT_buffer[gWDT_buffer_SIZE];
uint8_t gWDT_buffer_position;
uint8_t gWDT_loop_counter;
volatile uint8_t gWDT_pool_start;
volatile uint8_t gWDT_pool_end;
volatile uint8_t gWDT_pool_count;
volatile uint32_t gWDT_entropy_pool[WDT_POOL_SIZE];
// ----- Functions -----
// This function initializes the global variables needed to implement the circular entropy pool and
// the buffer that holds the raw Timer 1 values that are used to create the entropy pool. It then
// Initializes tc0 (channel 1), to perform an interrupt every 2048 clock cycles.
// NOTE: Atmel is dumb and uses the range TC{0..5} to refer to TC{0,1}->TC_CHANNEL{0..2}
void rand_initialize()
{
gWDT_buffer_position = 0;
gWDT_pool_start = 0;
gWDT_pool_end = 0;
gWDT_pool_count = 0;
// Enable clock for timer TC0 (Channel 1)
PMC->PMC_PCER0 |= (1 << ID_TC1);
// Setup Timer Counter to MCK/128, compare resets counter
TC0->TC_CHANNEL[1].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK4 | TC_CMR_CPCTRG;
// Timer Count-down value
// Number of cycles to count from CPU clock before calling interrupt
TC0->TC_CHANNEL[1].TC_RC = TC_RC_RC(937); // Approx. ~1 kHz @ 120 MHz MCK
// Enable Timer, Enable interrupt
TC0->TC_CHANNEL[1].TC_IER = TC_IER_CPCS;
TC0->TC_CHANNEL[1].TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
// Enable TC1 interrupt
NVIC_EnableIRQ( TC1_IRQn );
// Set TC1 interrupt to a low priority
NVIC_SetPriority( TC1_IRQn, 200 );
}
// Disables interrupt, thus stopping CPU usage generating entropy
void rand_disable()
{
TC0->TC_CHANNEL[1].TC_CCR = TC_CCR_CLKDIS;
TC0->TC_CHANNEL[1].TC_IDR = 0xFF;
NVIC_DisableIRQ( TC1_IRQn );
}
// This function returns a unsigned char (8-bit) with the number of unsigned long values
// in the entropy pool
uint8_t rand_available()
{
return gWDT_pool_count;
}
// Pseudo-random value using clock
uint32_t rand_value32()
{
uint32_t retVal = 0;
uint8_t waiting;
while ( gWDT_pool_count < 1 )
{
waiting += 1;
}
ATOMIC_BLOCK( ATOMIC_RESTORESTATE )
{
retVal = gWDT_entropy_pool[gWDT_pool_start];
gWDT_pool_start = (gWDT_pool_start + 1) % WDT_POOL_SIZE;
--gWDT_pool_count;
}
return retVal;
}
// This interrupt service routine is called every time the TC1 interrupt is triggered.
// With the default configuration that is approximately once every 16ms, producing
// approximately two 32-bit integer values every second.
//
// The pool is implemented as an 8 value circular buffer
static void isr_hardware_neutral( uint8_t val )
{
gWDT_buffer[gWDT_buffer_position] = val;
gWDT_buffer_position++; // every time the WDT interrupt is triggered
if ( gWDT_buffer_position >= gWDT_buffer_SIZE )
{
gWDT_pool_end = (gWDT_pool_start + gWDT_pool_count) % WDT_POOL_SIZE;
// The following code is an implementation of Jenkin's one at a time hash
// This hash function has had preliminary testing to verify that it
// produces reasonably uniform random results when using WDT jitter
// on a variety of Arduino platforms
for ( gWDT_loop_counter = 0; gWDT_loop_counter < gWDT_buffer_SIZE; ++gWDT_loop_counter )
{
gWDT_entropy_pool[gWDT_pool_end] += gWDT_buffer[gWDT_loop_counter];
gWDT_entropy_pool[gWDT_pool_end] += (gWDT_entropy_pool[gWDT_pool_end] << 10);
gWDT_entropy_pool[gWDT_pool_end] ^= (gWDT_entropy_pool[gWDT_pool_end] >> 6);
}
gWDT_entropy_pool[gWDT_pool_end] += (gWDT_entropy_pool[gWDT_pool_end] << 3);
gWDT_entropy_pool[gWDT_pool_end] ^= (gWDT_entropy_pool[gWDT_pool_end] >> 11);
gWDT_entropy_pool[gWDT_pool_end] += (gWDT_entropy_pool[gWDT_pool_end] << 15);
gWDT_entropy_pool[gWDT_pool_end] = gWDT_entropy_pool[gWDT_pool_end];
// Start collecting the next 32 bytes of Timer 1 counts
gWDT_buffer_position = 0;
// The entropy pool is full
if (gWDT_pool_count == WDT_POOL_SIZE)
{
gWDT_pool_start = (gWDT_pool_start + 1) % WDT_POOL_SIZE;
}
// Add another unsigned long (32 bits) to the entropy pool
else
{
++gWDT_pool_count;
}
}
}
void TC1_Handler()
{
uint32_t status = TC0->TC_CHANNEL[1].TC_SR;
if ( status & TC_SR_CPCS )
{
// Use the current state of systick for seeding
isr_hardware_neutral(SysTick->VAL & SysTick_VAL_CURRENT_Msk);
}
}
#elif defined(_nrf_)
// ----- Includes -----
#include <stdlib.h>
#include "entropy.h"
// ----- Functions -----
void rand_initialize()
{
// TODO (HaaTa)
}
void rand_disable()
{
// TODO (HaaTa)
}
uint8_t rand_available()
{
// TODO (HaaTa)
return 1;
}
// Pseudo-random value using clock
uint32_t rand_value32()
{
// TODO (HaaTa)
return 0;
}
#else
#error "Unknown build target for Lib/entropy"
#endif