Commit 6582c665d6b882dad8329e05749fbcf119f1ab88
Committed by
Linus Torvalds
1 parent
496f2f93b1
Exists in
smarc-l5.0.0_1.0.0-ga
and in
5 other branches
prandom: introduce prandom_bytes() and prandom_bytes_state()
Add functions to get the requested number of pseudo-random bytes. The difference from get_random_bytes() is that it generates pseudo-random numbers by prandom_u32(). It doesn't consume the entropy pool, and the sequence is reproducible if the same rnd_state is used. So it is suitable for generating random bytes for testing. Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com> Cc: "Theodore Ts'o" <tytso@mit.edu> Cc: Artem Bityutskiy <dedekind1@gmail.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: David Woodhouse <dwmw2@infradead.org> Cc: Eilon Greenstein <eilong@broadcom.com> Cc: David Laight <david.laight@aculab.com> Cc: Michel Lespinasse <walken@google.com> Cc: Robert Love <robert.w.love@intel.com> Cc: Valdis Kletnieks <valdis.kletnieks@vt.edu> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Showing 2 changed files with 51 additions and 0 deletions Side-by-side Diff
include/linux/random.h
... | ... | @@ -26,6 +26,7 @@ |
26 | 26 | unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len); |
27 | 27 | |
28 | 28 | u32 prandom_u32(void); |
29 | +void prandom_bytes(void *buf, int nbytes); | |
29 | 30 | void prandom_seed(u32 seed); |
30 | 31 | |
31 | 32 | /* |
... | ... | @@ -36,6 +37,7 @@ |
36 | 37 | #define srandom32(seed) prandom_seed(seed) |
37 | 38 | |
38 | 39 | u32 prandom_u32_state(struct rnd_state *); |
40 | +void prandom_bytes_state(struct rnd_state *state, void *buf, int nbytes); | |
39 | 41 | |
40 | 42 | /* |
41 | 43 | * Handle minimum values for seeds |
lib/random32.c
... | ... | @@ -77,6 +77,55 @@ |
77 | 77 | } |
78 | 78 | EXPORT_SYMBOL(prandom_u32); |
79 | 79 | |
80 | +/* | |
81 | + * prandom_bytes_state - get the requested number of pseudo-random bytes | |
82 | + * | |
83 | + * @state: pointer to state structure holding seeded state. | |
84 | + * @buf: where to copy the pseudo-random bytes to | |
85 | + * @bytes: the requested number of bytes | |
86 | + * | |
87 | + * This is used for pseudo-randomness with no outside seeding. | |
88 | + * For more random results, use prandom_bytes(). | |
89 | + */ | |
90 | +void prandom_bytes_state(struct rnd_state *state, void *buf, int bytes) | |
91 | +{ | |
92 | + unsigned char *p = buf; | |
93 | + int i; | |
94 | + | |
95 | + for (i = 0; i < round_down(bytes, sizeof(u32)); i += sizeof(u32)) { | |
96 | + u32 random = prandom_u32_state(state); | |
97 | + int j; | |
98 | + | |
99 | + for (j = 0; j < sizeof(u32); j++) { | |
100 | + p[i + j] = random; | |
101 | + random >>= BITS_PER_BYTE; | |
102 | + } | |
103 | + } | |
104 | + if (i < bytes) { | |
105 | + u32 random = prandom_u32_state(state); | |
106 | + | |
107 | + for (; i < bytes; i++) { | |
108 | + p[i] = random; | |
109 | + random >>= BITS_PER_BYTE; | |
110 | + } | |
111 | + } | |
112 | +} | |
113 | +EXPORT_SYMBOL(prandom_bytes_state); | |
114 | + | |
115 | +/** | |
116 | + * prandom_bytes - get the requested number of pseudo-random bytes | |
117 | + * @buf: where to copy the pseudo-random bytes to | |
118 | + * @bytes: the requested number of bytes | |
119 | + */ | |
120 | +void prandom_bytes(void *buf, int bytes) | |
121 | +{ | |
122 | + struct rnd_state *state = &get_cpu_var(net_rand_state); | |
123 | + | |
124 | + prandom_bytes_state(state, buf, bytes); | |
125 | + put_cpu_var(state); | |
126 | +} | |
127 | +EXPORT_SYMBOL(prandom_bytes); | |
128 | + | |
80 | 129 | /** |
81 | 130 | * prandom_seed - add entropy to pseudo random number generator |
82 | 131 | * @seed: seed value |