prandom: add crypto warnings and switch to new PRNG
Subsystems (4)
cover prandom: add crypto warnings and switch to new PRNG
This series builds on top of the series:
"prandom: remove next_pseudo_random32."
With the current cryptographically safe PRNG in drivers/char/random.c fast enough for most purposes, make annoyingly clear, that no one should use the prandom PRNG for cryptographic purposes without known about this.
While looking at the prandom/random32 code, I informed myself about PRNGs and saw that currently fast PRNGs with better statistical properties than LFSR113, which is currently used, are available.
Recent alternatives to consider are in my opinion:
* PCG: https://www.pcg-random.org
* Xoshiro: https://prng.di.unimi.it
While both seem to have good statistical properties, I recommend to chose Xoshiro256++ here, because it seems to be even faster than RNGs from the PCG series.
Furthermore, the authors of Xoshiro provide a small RNG named SplitMix64 for generating high quality seeds for the Xoshiro RNG. By using this in the default way, no further seed checks or state warmup are necessary. This simplifies the PRNG code IMHO.
Markus Theil (2):
prandom: add usage comments for cryptography
prandom/random32: switch to Xoshiro256++
include/linux/prandom.h | 30 +--- lib/random32.c | 351 ++++++++++++++++++++-------------------- 2 files changed, 180 insertions(+), 201 deletions(-)
Signature · 1 line
2.47.2
PATCH 1/2 prandom: add usage comments for cryptography
Make it more explicit, that the PRNG must not be used for cryptographic purposes.
Future code may also check such things in checkpatch.pl, but it is probably hard to differentiate valid testing code only by looking at paths. Therefore this is left out here by intention.
--- include/linux/prandom.h | 2 ++ lib/random32.c | 6 ++++++ 2 files changed, 8 insertions(+)
include/linux/prandom.h +2 -0
@@ -17,7 +17,9 @@ struct rnd_state {__u32 s1, s2, s3, s4; }; +/* WARNING: this API MUST NOT be used for cryptographic purposes! */ u32 prandom_u32_state (struct rnd_state *state); +/* WARNING: this API MUST NOT be used for cryptographic purposes! */ void prandom_bytes_state (struct rnd_state *state,void *buf,size_t nbytes);void prandom_seed_full_state (struct rnd_state __percpu *pcpu_state);
lib/random32.c +6 -0
@@ -1,5 +1,7 @@// SPDX-License-Identifier: GPL-2.0 /* +* WARNING: this API MUST NOT be used for cryptographic purposes! +* * This is a maximally equidistributed combined Tausworthe generator * based on code from GNU Scientific Library 1.5 (30 Jun 2004) *
@@ -48,6 +50,8 @@* * This is used for pseudo-randomness with no outside seeding. * For more random results, use get_random_u32(). +* +* WARNING: this API MUST NOT be used for cryptographic purposes! */ u32 prandom_u32_state (struct rnd_state *state) {
@@ -70,6 +74,8 @@ EXPORT_SYMBOL(prandom_u32_state);* * This is used for pseudo-randomness with no outside seeding. * For more random results, use get_random_bytes(). +* +* WARNING: this API MUST NOT be used for cryptographic purposes! */ void prandom_bytes_state (struct rnd_state *state,void *buf,size_t bytes) {
Signature · 1 line
2.47.2
PATCH 2/2 prandom/random32: switch to Xoshiro256++
In reply to Markus TheilThe current Linux PRNG is based on LFSR113, which means:
- needs some warmup rounds to yield better statistical properties
- seeds/initial states must be of certain structure
- does not pass L’Ecuyer's BigCrush in TestU01
While of course, there is no clear "best" PRNG, replace with Xoshiro256++, which seams to be a sensible replacement, from todays point of view:
- only needs one bit set to 1 in the seed, needs no warmup, when seeded with splitmix64.
- Also has statistical evaluation, like LFSR113.
- Passes BigCrush in TestU01.
The code got smaller, because some edge cases are ruled out now. I kept the test vectors and adapted them to this RNG.
--- include/linux/prandom.h | 28 +--- lib/random32.c | 347 +++++++++++++++++++--------------------- 2 files changed, 173 insertions(+), 202 deletions(-)
include/linux/prandom.h +4 -24
@@ -14,39 +14,19 @@#include <linux/random.h> struct rnd_state { -__u32 s1, s2, s3, s4; +__u64 s[4 ]; };/* WARNING: this API MUST NOT be used for cryptographic purposes! */ u32 prandom_u32_state (struct rnd_state *state);/* WARNING: this API MUST NOT be used for cryptographic purposes! */ +u64 prandom_u64_state (struct rnd_state *state);void prandom_bytes_state (struct rnd_state *state,void *buf,size_t nbytes); + +void prandom_seed_state (struct rnd_state *state,u64 seed);void prandom_seed_full_state (struct rnd_state __percpu *pcpu_state);#define prandom_init_once (pcpu_state) \ DO_ONCE(prandom_seed_full_state, (pcpu_state)) -/* -* Handle minimum values for seeds -*/ -static inline u32 __seed (u32 x,u32 m) -{ -return (x < m) ? x + m : x; -} - -/** -* prandom_seed_state - set seed for prandom_u32_state(). -* @state: pointer to state structure to receive the seed. -* @seed: arbitrary 64-bit value to use as a seed. -*/ -static inline void prandom_seed_state (struct rnd_state *state,u64 seed) -{ -u32 i = ((seed >>32 ) ^ (seed <<10 ) ^ seed) &0xffffffffUL ; - - state->s1 =__seed (i,2U ); - state->s2 =__seed (i,8U ); - state->s3 =__seed (i,16U ); - state->s4 =__seed (i,128U ); -} -#endif
lib/random32.c +169 -178
417 lines not on this page · raw message
Signature · 1 line
2.47.2
Simon Horman
reviewerRe: [PATCH 2/2] prandom/random32: switch to Xoshiro256++
Quoted 17 lines · Markus Theil
The current Linux PRNG is based on LFSR113, which means:
- needs some warmup rounds to yield better statistical properties
- seeds/initial states must be of certain structure
- does not pass L’Ecuyer's BigCrush in TestU01While of course, there is no clear "best" PRNG, replace with Xoshiro256++, which seams to be a sensible replacement, from todays point of view:
- only needs one bit set to 1 in the seed, needs no warmup, when seeded with splitmix64.
- Also has statistical evaluation, like LFSR113.
- Passes BigCrush in TestU01.The code got smaller, because some edge cases are ruled out now. I kept the test vectors and adapted them to this RNG.
Signed-off-by Markus Theil <[email protected]>
...
Quoted 1 line
lib/random32.c +0
-0
...
Quoted 19 lines
lib/random32.c +9
-9+/** +* prandom_seed_state - set seed for prandom_u32_state(). +* @state: pointer to state structure to receive the seed. +* @seed: arbitrary 64-bit value to use as a seed. +* +* splitmix64 init as suggested for xoshiro256++ +* See: https://prng.di.unimi.it/splitmix64.c +*/ +void prandom_seed_state (struct rnd_state *state,u64 seed) { -/* Calling RNG ten times to satisfy recurrence condition */ -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state);
lib/random32.c void prandom_bytes_state(struct rnd_state *state, void *buf, size_t bytes) · line 121
Quoted 10 lines
lib/random32.c +8
-2-prandom_u32_state (state); -prandom_u32_state (state); +int i; + +for (i =0 ; i <ARRAY_SIZE (state->s); ++i) { + seed +=0x9e3779b97f4a7c15 ; +u64 z = seed; + z = (z ^ (z >>30 )) *0xbf58476d1ce4e5b9 ; + z = (z ^ (z >>27 )) *0x94d049bb133111eb ; + state->s[i] = z ^ (z >>31 );
nit: The indentation seems off here.
Quoted 3 lines
lib/random32.c +2
-0+ } } +EXPORT_SYMBOL (prandom_seed_state);
...
Re: [PATCH 2/2] prandom/random32: switch to Xoshiro256++
Quoted 45 lines · Simon Horman
Quoted 17 lines · Markus Theil
The current Linux PRNG is based on LFSR113, which means:
- needs some warmup rounds to yield better statistical properties
- seeds/initial states must be of certain structure
- does not pass L’Ecuyer's BigCrush in TestU01While of course, there is no clear "best" PRNG, replace with Xoshiro256++, which seams to be a sensible replacement, from todays point of view:
- only needs one bit set to 1 in the seed, needs no warmup, when seeded with splitmix64.
- Also has statistical evaluation, like LFSR113.
- Passes BigCrush in TestU01.The code got smaller, because some edge cases are ruled out now. I kept the test vectors and adapted them to this RNG.
Signed-off-by Markus Theil <[email protected]>...
Quoted 1 line
lib/random32.c +0
-0...
Quoted 22 lines
lib/random32.c +10
-11+/** +* prandom_seed_state - set seed for prandom_u32_state(). +* @state: pointer to state structure to receive the seed. +* @seed: arbitrary 64-bit value to use as a seed. +* +* splitmix64 init as suggested for xoshiro256++ +* See: https://prng.di.unimi.it/splitmix64.c +*/ +void prandom_seed_state (struct rnd_state *state,u64 seed) { -/* Calling RNG ten times to satisfy recurrence condition */ -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); +int i;
Quoted 8 lines
lib/random32.c void prandom_bytes_state(struct rnd_state *state, void *buf, size_t bytes) · line 121
Quoted 7 lines
lib/random32.c +7
-0+ +for (i =0 ; i <ARRAY_SIZE (state->s); ++i) { + seed +=0x9e3779b97f4a7c15 ; +u64 z = seed; + z = (z ^ (z >>30 )) *0xbf58476d1ce4e5b9 ; + z = (z ^ (z >>27 )) *0x94d049bb133111eb ; + state->s[i] = z ^ (z >>31 );nit: The indentation seems off here.
Shall I resend for this line?
Quoted 4 lines
Quoted 3 lines
lib/random32.c +2
-0+ } } +EXPORT_SYMBOL (prandom_seed_state);...
Simon Horman
reviewerRe: [PATCH 2/2] prandom/random32: switch to Xoshiro256++
Quoted 54 lines · Markus Theil
Quoted 45 lines · Simon Horman
Quoted 17 lines · Markus Theil
The current Linux PRNG is based on LFSR113, which means:
- needs some warmup rounds to yield better statistical properties
- seeds/initial states must be of certain structure
- does not pass L’Ecuyer's BigCrush in TestU01While of course, there is no clear "best" PRNG, replace with Xoshiro256++, which seams to be a sensible replacement, from todays point of view:
- only needs one bit set to 1 in the seed, needs no warmup, when seeded with splitmix64.
- Also has statistical evaluation, like LFSR113.
- Passes BigCrush in TestU01.The code got smaller, because some edge cases are ruled out now. I kept the test vectors and adapted them to this RNG.
Signed-off-by Markus Theil <[email protected]>...
Quoted 1 line
lib/random32.c +0
-0...
Quoted 22 lines
lib/random32.c +10
-11+/** +* prandom_seed_state - set seed for prandom_u32_state(). +* @state: pointer to state structure to receive the seed. +* @seed: arbitrary 64-bit value to use as a seed. +* +* splitmix64 init as suggested for xoshiro256++ +* See: https://prng.di.unimi.it/splitmix64.c +*/ +void prandom_seed_state (struct rnd_state *state,u64 seed) { -/* Calling RNG ten times to satisfy recurrence condition */ -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); -prandom_u32_state (state); +int i;Quoted 8 lines
lib/random32.c void prandom_bytes_state(struct rnd_state *state, void *buf, size_t bytes) · line 121
Quoted 7 lines
lib/random32.c +7
-0+ +for (i =0 ; i <ARRAY_SIZE (state->s); ++i) { + seed +=0x9e3779b97f4a7c15 ; +u64 z = seed; + z = (z ^ (z >>30 )) *0xbf58476d1ce4e5b9 ; + z = (z ^ (z >>27 )) *0x94d049bb133111eb ; + state->s[i] = z ^ (z >>31 );nit: The indentation seems off here.
Quoted 1 line
Shall I resend for this line?
The maintainer may think otherwise.
But in my opinion that would be a good idea.
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
lib/random32.c +2
-0+ } } +EXPORT_SYMBOL (prandom_seed_state);...