forked from marvinroger/ESP8266TrueRandom
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathESPTrueRandom.cpp
More file actions
173 lines (153 loc) · 4.81 KB
/
ESPTrueRandom.cpp
File metadata and controls
173 lines (153 loc) · 4.81 KB
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
/*
* TrueRandom - A true random number generator for Arduino.
* This is variant of original work originally implemented as:
* https://code.google.com/archive/p/tinkerit/ https://github.com/Cathedrow/TrueRandom
* Copyright (c) 2010 Peter Knight, Tinker.it! All rights reserved.
* Now modified for the ESP8266
*
* 2019-08-08 Forked from ESP8266TrueRandom
* Added support for ESP32
* Renamed to ESPTrueRandom
*
*/
#include "ESPTrueRandom.h"
ICACHE_FLASH_ATTR ESPTrueRandomClass::ESPTrueRandomClass() {
useRNG = true;
lastYield = 0;
}
ICACHE_FLASH_ATTR int ESPTrueRandomClass::randomBitRaw(void) {
// Needed to keep wifi stack running smoothly
// And to avoid wdt reset
if (lastYield == 0 || millis() - lastYield >= 50) {
yield();
lastYield = millis();
}
uint8_t bit = useRNG
? (int)RANDOM_REG32 //using the onboard hardware random number generator (esp8266_peri.h)
: analogRead(A0); //using A0 / TOUT
return bit & 1;
}
ICACHE_FLASH_ATTR int ESPTrueRandomClass::randomBitRaw2(void) {
// Software whiten bits using Von Neumann algorithm
//
// von Neumann, John (1951). "Various techniques used in connection
// with random digits". National Bureau of Standards Applied Math Series
// 12:36.
//
for(;;) {
int a = randomBitRaw() | (randomBitRaw()<<1);
if (a==1) return 0; // 1 to 0 transition: log a zero bit
if (a==2) return 1; // 0 to 1 transition: log a one bit
// For other cases, try again.
}
return 0;
}
ICACHE_FLASH_ATTR int ESPTrueRandomClass::randomBit(void) {
// Software whiten bits using Von Neumann algorithm
//
// von Neumann, John (1951). "Various techniques used in connection
// with random digits". National Bureau of Standards Applied Math Series
// 12:36.
//
for(;;) {
int a = randomBitRaw2() | (randomBitRaw2()<<1);
if (a==1) return 0; // 1 to 0 transition: log a zero bit
if (a==2) return 1; // 0 to 1 transition: log a one bit
// For other cases, try again.
}
return 0;
}
ICACHE_FLASH_ATTR char ESPTrueRandomClass::randomByte(void) {
char result = 0;
uint8_t i;
for (i=8; i--;) result += result + randomBit();
return result;
}
ICACHE_FLASH_ATTR int ESPTrueRandomClass::rand() {
int result = 0;
uint8_t i;
for (i=15; i--;) result += result + randomBit();
return result;
}
ICACHE_FLASH_ATTR long ESPTrueRandomClass::random() {
long result = 0;
uint8_t i;
for (i=31; i--;) result += result + randomBit();
return result;
}
ICACHE_FLASH_ATTR long ESPTrueRandomClass::random(long howBig) {
long randomValue;
long topBit;
long bitPosition;
if (!howBig) return 0;
randomValue = 0;
if (howBig & (howBig-1)) {
// Range is not a power of 2 - use slow method
topBit = howBig-1;
topBit |= topBit>>1;
topBit |= topBit>>2;
topBit |= topBit>>4;
topBit |= topBit>>8;
topBit |= topBit>>16;
topBit = (topBit+1) >> 1;
bitPosition = topBit;
do {
// Generate the next bit of the result
if (randomBit()) randomValue |= bitPosition;
// Check if bit
if (randomValue >= howBig) {
// Number is over the top limit - start again.
randomValue = 0;
bitPosition = topBit;
} else {
// Repeat for next bit
bitPosition >>= 1;
}
} while (bitPosition);
} else {
// Special case, howBig is a power of 2
bitPosition = howBig >> 1;
while (bitPosition) {
if (randomBit()) randomValue |= bitPosition;
bitPosition >>= 1;
}
}
return randomValue;
}
ICACHE_FLASH_ATTR long ESPTrueRandomClass::random(long howSmall, long howBig) {
if (howSmall >= howBig) return howSmall;
long diff = howBig - howSmall;
return ESPTrueRandomClass::random(diff) + howSmall;
}
ICACHE_FLASH_ATTR void ESPTrueRandomClass::memfill(char* location, int size) {
for (;size--;) *location++ = randomByte();
}
ICACHE_FLASH_ATTR void ESPTrueRandomClass::mac(uint8_t* macLocation) {
memfill((char*)macLocation,6);
}
ICACHE_FLASH_ATTR void ESPTrueRandomClass::uuid(uint8_t* uuidLocation) {
// Generate a Version 4 UUID according to RFC4122
memfill((char*)uuidLocation,16);
// Although the UUID contains 128 bits, only 122 of those are random.
// The other 6 bits are fixed, to indicate a version number.
uuidLocation[6] = 0x40 | (0x0F & uuidLocation[6]);
uuidLocation[8] = 0x80 | (0x3F & uuidLocation[8]);
}
ICACHE_FLASH_ATTR String ESPTrueRandomClass::uuidToString(uint8_t* uuidLocation) {
String string = "";
int i;
for (i=0; i<16; i++) {
if (i==4) string += "-";
if (i==6) string += "-";
if (i==8) string += "-";
if (i==10) string += "-";
int topDigit = uuidLocation[i] >> 4;
int bottomDigit = uuidLocation[i] & 0x0f;
// High hex digit
string += "0123456789abcdef"[topDigit];
// Low hex digit
string += "0123456789abcdef"[bottomDigit];
}
return string;
}
ESPTrueRandomClass ESPTrueRandom;