aboutsummaryrefslogtreecommitdiffstats
path: root/lib/random32.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/random32.c')
-rw-r--r--lib/random32.c39
1 files changed, 18 insertions, 21 deletions
diff --git a/lib/random32.c b/lib/random32.c
index c9b6bf3afe0c..0bee183fa18f 100644
--- a/lib/random32.c
+++ b/lib/random32.c
@@ -37,6 +37,7 @@
37#include <linux/jiffies.h> 37#include <linux/jiffies.h>
38#include <linux/random.h> 38#include <linux/random.h>
39#include <linux/sched.h> 39#include <linux/sched.h>
40#include <asm/unaligned.h>
40 41
41#ifdef CONFIG_RANDOM32_SELFTEST 42#ifdef CONFIG_RANDOM32_SELFTEST
42static void __init prandom_state_selftest(void); 43static void __init prandom_state_selftest(void);
@@ -96,27 +97,23 @@ EXPORT_SYMBOL(prandom_u32);
96 * This is used for pseudo-randomness with no outside seeding. 97 * This is used for pseudo-randomness with no outside seeding.
97 * For more random results, use prandom_bytes(). 98 * For more random results, use prandom_bytes().
98 */ 99 */
99void prandom_bytes_state(struct rnd_state *state, void *buf, int bytes) 100void prandom_bytes_state(struct rnd_state *state, void *buf, size_t bytes)
100{ 101{
101 unsigned char *p = buf; 102 u8 *ptr = buf;
102 int i;
103
104 for (i = 0; i < round_down(bytes, sizeof(u32)); i += sizeof(u32)) {
105 u32 random = prandom_u32_state(state);
106 int j;
107 103
108 for (j = 0; j < sizeof(u32); j++) { 104 while (bytes >= sizeof(u32)) {
109 p[i + j] = random; 105 put_unaligned(prandom_u32_state(state), (u32 *) ptr);
110 random >>= BITS_PER_BYTE; 106 ptr += sizeof(u32);
111 } 107 bytes -= sizeof(u32);
112 } 108 }
113 if (i < bytes) {
114 u32 random = prandom_u32_state(state);
115 109
116 for (; i < bytes; i++) { 110 if (bytes > 0) {
117 p[i] = random; 111 u32 rem = prandom_u32_state(state);
118 random >>= BITS_PER_BYTE; 112 do {
119 } 113 *ptr++ = (u8) rem;
114 bytes--;
115 rem >>= BITS_PER_BYTE;
116 } while (bytes > 0);
120 } 117 }
121} 118}
122EXPORT_SYMBOL(prandom_bytes_state); 119EXPORT_SYMBOL(prandom_bytes_state);
@@ -126,7 +123,7 @@ EXPORT_SYMBOL(prandom_bytes_state);
126 * @buf: where to copy the pseudo-random bytes to 123 * @buf: where to copy the pseudo-random bytes to
127 * @bytes: the requested number of bytes 124 * @bytes: the requested number of bytes
128 */ 125 */
129void prandom_bytes(void *buf, int bytes) 126void prandom_bytes(void *buf, size_t bytes)
130{ 127{
131 struct rnd_state *state = &get_cpu_var(net_rand_state); 128 struct rnd_state *state = &get_cpu_var(net_rand_state);
132 129
@@ -137,7 +134,7 @@ EXPORT_SYMBOL(prandom_bytes);
137 134
138static void prandom_warmup(struct rnd_state *state) 135static void prandom_warmup(struct rnd_state *state)
139{ 136{
140 /* Calling RNG ten times to satify recurrence condition */ 137 /* Calling RNG ten times to satisfy recurrence condition */
141 prandom_u32_state(state); 138 prandom_u32_state(state);
142 prandom_u32_state(state); 139 prandom_u32_state(state);
143 prandom_u32_state(state); 140 prandom_u32_state(state);
@@ -152,7 +149,7 @@ static void prandom_warmup(struct rnd_state *state)
152 149
153static u32 __extract_hwseed(void) 150static u32 __extract_hwseed(void)
154{ 151{
155 u32 val = 0; 152 unsigned int val = 0;
156 153
157 (void)(arch_get_random_seed_int(&val) || 154 (void)(arch_get_random_seed_int(&val) ||
158 arch_get_random_int(&val)); 155 arch_get_random_int(&val));
@@ -228,7 +225,7 @@ static void __prandom_timer(unsigned long dontcare)
228 prandom_seed(entropy); 225 prandom_seed(entropy);
229 226
230 /* reseed every ~60 seconds, in [40 .. 80) interval with slack */ 227 /* reseed every ~60 seconds, in [40 .. 80) interval with slack */
231 expires = 40 + (prandom_u32() % 40); 228 expires = 40 + prandom_u32_max(40);
232 seed_timer.expires = jiffies + msecs_to_jiffies(expires * MSEC_PER_SEC); 229 seed_timer.expires = jiffies + msecs_to_jiffies(expires * MSEC_PER_SEC);
233 230
234 add_timer(&seed_timer); 231 add_timer(&seed_timer);