Skip to content

Commit a14ac2c

Browse files
authored
Bring back old semantics to random and randomSeed, add secureRandom (#1710) (#2142)
1 parent b9dfe01 commit a14ac2c

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

Diff for: cores/esp8266/Arduino.h

+2
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ void noTone(uint8_t _pin);
271271
long random(long);
272272
long random(long, long);
273273
void randomSeed(unsigned long);
274+
long secureRandom(long);
275+
long secureRandom(long, long);
274276
long map(long, long, long, long, long);
275277

276278
extern "C" void configTime(long timezone, int daylightOffset_sec,

Diff for: cores/esp8266/WMath.cpp

+22-2
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,22 @@ extern "C" {
2828
}
2929
#include "esp8266_peri.h"
3030

31+
static bool s_randomSeedCalled = false;
32+
3133
void randomSeed(unsigned long seed) {
3234
if(seed != 0) {
33-
srand((seed ^ RANDOM_REG32));
35+
srand(seed);
36+
s_randomSeedCalled = true;
3437
}
3538
}
3639

3740
long random(long howbig) {
3841
if(howbig == 0) {
3942
return 0;
4043
}
41-
return (rand() ^ RANDOM_REG32) % howbig;
44+
// if randomSeed was called, fall back to software PRNG
45+
uint32_t val = (s_randomSeedCalled) ? rand() : RANDOM_REG32;
46+
return val % howbig;
4247
}
4348

4449
long random(long howsmall, long howbig) {
@@ -49,6 +54,21 @@ long random(long howsmall, long howbig) {
4954
return random(diff) + howsmall;
5055
}
5156

57+
long secureRandom(long howbig) {
58+
if(howbig == 0) {
59+
return 0;
60+
}
61+
return RANDOM_REG32 % howbig;
62+
}
63+
64+
long secureRandom(long howsmall, long howbig) {
65+
if(howsmall >= howbig) {
66+
return howsmall;
67+
}
68+
long diff = howbig - howsmall;
69+
return secureRandom(diff) + howsmall;
70+
}
71+
5272
long map(long x, long in_min, long in_max, long out_min, long out_max) {
5373
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
5474
}

Diff for: tests/device/test_random/test_random.ino

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <BSTest.h>
2+
3+
BS_ENV_DECLARE();
4+
5+
void setup()
6+
{
7+
Serial.begin(115200);
8+
BS_RUN(Serial);
9+
}
10+
11+
12+
TEST_CASE("If randomSeed is not called, random() uses hardware PRNG", "[random]")
13+
{
14+
int32_t data[32];
15+
srand(10);
16+
for (int i = 0; i < sizeof(data)/sizeof(data[0]); ++i) {
17+
data[i] = random(0x7fffffff);
18+
}
19+
srand(10);
20+
for (int i = 0; i < sizeof(data)/sizeof(data[0]); ++i) {
21+
CHECK(random(0x7fffffff) != data[i]);
22+
}
23+
}
24+
25+
26+
TEST_CASE("If randomSeed is called, we get same random sequence every time", "[random]")
27+
{
28+
const int32_t reference_sequence[] = {
29+
2104627054, 2013331137, 258660947, 107061148,
30+
317460219, 663931879, 307161078, 1718702872,
31+
1306951058, 1066376876, 624381721, 850811527,
32+
329784053, 726742932, 182903521, 787925035,
33+
1364123723, 198878220, 1117075042, 1108236242,
34+
1775000610, 500470195, 896676389, 6341838,
35+
785214762, 1084946248, 1601419914, 2058135092,
36+
1671754873, 1952290050, 1572975837, 1596343802,
37+
240941423, 1843946550, 793779187, 441773333,
38+
884819086, 590861527, 1676358848, 2132930493,
39+
969627641, 930717537, 195748182, 2064531490,
40+
1195920216, 347502525, 584628940, 1938341337,
41+
642503024, 915004020, 2034338438, 1690522669,
42+
1805037441, 1904039418, 1491310300, 227517325,
43+
17943876, 570537582, 1409581066, 1819703730,
44+
730240988, 786466794, 1411137128, 1680096093,
45+
};
46+
randomSeed(42);
47+
for (int i = 0; i < sizeof(reference_sequence)/sizeof(reference_sequence[0]); ++i) {
48+
CHECK(random(0x7fffffff) == reference_sequence[i]);
49+
}
50+
}
51+
52+
53+
void loop() {}

0 commit comments

Comments
 (0)