Skip to content

Commit fce09a9

Browse files
committed
Add local pseudo-RNG to randLIB
Rather than using system rand(), provide our own pseudo-RNG. Generator used is "xoroshiro128+", which has 16 bytes of state and 2^128-1 period. Main advantage is that we can now seed with up to 128 bits of entropy, rather than the 32 bits srand() limited us to. We also can be assured of the quality of the algorithm. As the core generator is 64-bit, we now provide a get 64-bit function, and others are based on this. Incorporate Linux's /dev/urandom use into the main source file.
1 parent 80f5c49 commit fce09a9

File tree

8 files changed

+319
-134
lines changed

8 files changed

+319
-134
lines changed

mbed-client-randlib/platform/arm_hal_random.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ extern "C" {
2525
extern void arm_random_module_init(void);
2626
/**
2727
* \brief Get random library seed value.
28+
*
29+
* This function should return as random a value as possible, using
30+
* hardware sources. Repeated calls should return different values if
31+
* at all possible.
2832
*/
2933
extern uint32_t arm_random_seed_get(void);
3034
#ifdef __cplusplus

mbed-client-randlib/randLIB.h

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,23 @@ extern "C" {
4848
/**
4949
* \brief Init seed for Pseudo Random.
5050
*
51+
* Makes call(s) to the platform's arm_random_seed_get() to seed the
52+
* pseudo-random generator.
53+
*
5154
* \return None
5255
*
5356
*/
5457
extern void randLIB_seed_random(void);
5558

59+
/**
60+
* \brief Update seed for pseudo-random generator
61+
*
62+
* Adds seed information to existing generator, to perturb the
63+
* sequence.
64+
* \param seed 64 bits of data to add to the seed.
65+
*/
66+
extern void randLIB_add_seed(uint64_t seed);
67+
5668
/**
5769
* \brief Generate 8-bit random number.
5870
*
@@ -75,21 +87,29 @@ extern uint16_t randLIB_get_16bit(void);
7587
* \brief Generate 32-bit random number.
7688
*
7789
* \param None
78-
* \return 16-bit random number
90+
* \return 32-bit random number
7991
*
8092
*/
8193
extern uint32_t randLIB_get_32bit(void);
8294

95+
/**
96+
* \brief Generate 64-bit random number.
97+
*
98+
* \param None
99+
* \return 64-bit random number
100+
*
101+
*/
102+
extern uint64_t randLIB_get_64bit(void);
103+
83104
/**
84105
* \brief Generate n-bytes random numbers.
85106
*
86107
* \param data_ptr pointer where random will be stored
87-
* \param eight_bit_boundary how many bytes need random
88-
* \return 0 process valid
89-
* \return -1 Unsupported Parameters
108+
* \param count how many bytes need random
90109
*
110+
* \return data_ptr
91111
*/
92-
extern int8_t randLIB_get_n_bytes_random(uint8_t *data_ptr, uint8_t eight_bit_boundary);
112+
extern void *randLIB_get_n_bytes_random(void *data_ptr, uint8_t count);
93113

94114
/**
95115
* \brief Generate a random number within a range.
@@ -117,6 +137,12 @@ uint16_t randLIB_get_random_in_range(uint16_t min, uint16_t max);
117137
*/
118138
uint32_t randLIB_randomise_base(uint32_t base, uint16_t min_factor, uint16_t max_factor);
119139

140+
#ifdef RANDLIB_PRNG
141+
/* \internal Reset the PRNG state to zero (invalid) */
142+
void randLIB_reset(void);
143+
#endif
144+
145+
120146
#ifdef __cplusplus
121147
}
122148
#endif

0 commit comments

Comments
 (0)