Skip to content

Commit c78a2b5

Browse files
committed
Mix input to randLIB_add_seed calls
Don't just xor in the 64-bit number given, splitmix64 it across the entire state. Also, make sure we don't accidentally set the state to all-zero. RANDLIB_ASSUME_GOOD_SEED dropped - we now assume splitmix64 will be present for the seed additions. (Note that add_seed will be somewhat slow on platforms like MSP430 with slow multipliers - don't call it too much.)
1 parent 9a00890 commit c78a2b5

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

source/randLIB.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ static inline uint64_t rol(uint64_t n, int bits)
6969
return (n << bits) | (n >> (64 - bits));
7070
}
7171

72-
#ifndef RANDLIB_ASSUME_GOOD_SEED
7372
/* Lower-quality generator used only for initial seeding, if platform
7473
* isn't returning multiple seeds itself. Multiplies are rather heavy
7574
* for lower-end platforms, but this is initialisation only.
@@ -81,7 +80,6 @@ static uint64_t splitmix64(uint64_t *seed)
8180
z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
8281
return z ^ (z >> 31);
8382
}
84-
#endif // RANDLIB_ASSUME_GOOD_SEED
8583
#endif // RANDOM_DEVICE
8684

8785
void randLIB_seed_random(void)
@@ -94,7 +92,8 @@ void randLIB_seed_random(void)
9492
arm_random_module_init();
9593

9694
/* We exclusive-OR with the current state, in case they make this call
97-
* multiple times. We don't want to potentially lose entropy.
95+
* multiple times,or in case someone has called randLIB_add_seed before
96+
* this. We don't want to potentially lose entropy.
9897
*/
9998

10099
/* Spell out expressions so we get known ordering of 4 seed calls */
@@ -104,30 +103,24 @@ void randLIB_seed_random(void)
104103
s = (uint64_t) arm_random_seed_get() << 32;
105104
state[1] ^= s | arm_random_seed_get();
106105

107-
#ifdef RANDLIB_ASSUME_GOOD_SEED
108-
/* Can avoid significant code overhead of splitmix64(), but we do still
109-
* have to check for the theoretically possible illegal case of all-zero.
110-
*/
111-
if (state[0] == 0 && state[1] == 0) {
112-
state[1] = 1;
113-
}
114-
#else
115106
/* This check serves to both to stir the state if the platform is returning
116107
* constant seeding values, and to avoid the illegal all-zero state.
117108
*/
118109
if (state[0] == state[1]) {
119-
uint64_t seed = state[0];
120-
state[0] = splitmix64(&seed);
121-
state[1] = splitmix64(&seed);
110+
randLIB_add_seed(state[0]);
122111
}
123-
#endif // RANDLIB_ASSUME_GOOD_SEED
124112
#endif // RANDOM_DEVICE
125113
}
126114

127115
void randLIB_add_seed(uint64_t seed)
128116
{
129117
#ifndef RANDOM_DEVICE
130-
state[1] += seed;
118+
state[0] ^= splitmix64(&seed);
119+
state[1] ^= splitmix64(&seed);
120+
/* This is absolutely necessary, but I challenge you to add it to line coverage */
121+
if (state[1] == 0 && state[0] == 0) {
122+
state[0] = 1;
123+
}
131124
#endif
132125
}
133126

test/mbed-client-randlib/unittest/randlib/test_randlib.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,18 @@ bool test_randLIB_get_64bit()
111111

112112
/* If passed all "4" seeds, seeding should detect this
113113
* and use splitmix64 to create the actual seed
114-
* (0x03910b0aab9b37e5, 0x0b309ab13d42b2a6),
114+
* (0x03910b0eab9b37e1, 0x0b309ab53d42b2a2),
115115
* and produce this output:
116116
*/
117117
static const uint64_t expected4444[] = {
118-
UINT64_C(0x0ec1a5bbe8ddea8b),
119-
UINT64_C(0x0be710b8fcf5a491),
120-
UINT64_C(0xb21127f7159348b4),
121-
UINT64_C(0xdf31900d21f92182),
122-
UINT64_C(0xd5a797507d94daa9),
123-
UINT64_C(0x66a1c5a4fb04be3d),
124-
UINT64_C(0x259e5385f48353be),
125-
UINT64_C(0x5d3e3286cd4eae19)
118+
UINT64_C(0x0ec1a5c3e8ddea83),
119+
UINT64_C(0x09e710b8faf5a491),
120+
UINT64_C(0xd4102776f79448b4),
121+
UINT64_C(0x5d61988b60091900),
122+
UINT64_C(0xf6c8a72a9c72cb4b),
123+
UINT64_C(0xb06923e0cf0f2fb1),
124+
UINT64_C(0x24bbed475153f573),
125+
UINT64_C(0xfff0b4bd08c5581f),
126126
};
127127

128128
if (!test_output(4, false, expected4444)) {

0 commit comments

Comments
 (0)