aboutsummaryrefslogtreecommitdiffstats
path: root/lib/random32.c
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
commit8dea78da5cee153b8af9c07a2745f6c55057fe12 (patch)
treea8f4d49d63b1ecc92f2fddceba0655b2472c5bd9 /lib/random32.c
parent406089d01562f1e2bf9f089fd7637009ebaad589 (diff)
Patched in Tegra support.
Diffstat (limited to 'lib/random32.c')
-rw-r--r--lib/random32.c99
1 files changed, 25 insertions, 74 deletions
diff --git a/lib/random32.c b/lib/random32.c
index 52280d5526b..fc3545a3277 100644
--- a/lib/random32.c
+++ b/lib/random32.c
@@ -35,20 +35,20 @@
35 35
36#include <linux/types.h> 36#include <linux/types.h>
37#include <linux/percpu.h> 37#include <linux/percpu.h>
38#include <linux/export.h> 38#include <linux/module.h>
39#include <linux/jiffies.h> 39#include <linux/jiffies.h>
40#include <linux/random.h> 40#include <linux/random.h>
41 41
42static DEFINE_PER_CPU(struct rnd_state, net_rand_state); 42static DEFINE_PER_CPU(struct rnd_state, net_rand_state);
43 43
44/** 44/**
45 * prandom_u32_state - seeded pseudo-random number generator. 45 * prandom32 - seeded pseudo-random number generator.
46 * @state: pointer to state structure holding seeded state. 46 * @state: pointer to state structure holding seeded state.
47 * 47 *
48 * This is used for pseudo-randomness with no outside seeding. 48 * This is used for pseudo-randomness with no outside seeding.
49 * For more random results, use prandom_u32(). 49 * For more random results, use random32().
50 */ 50 */
51u32 prandom_u32_state(struct rnd_state *state) 51u32 prandom32(struct rnd_state *state)
52{ 52{
53#define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b) 53#define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
54 54
@@ -58,81 +58,32 @@ u32 prandom_u32_state(struct rnd_state *state)
58 58
59 return (state->s1 ^ state->s2 ^ state->s3); 59 return (state->s1 ^ state->s2 ^ state->s3);
60} 60}
61EXPORT_SYMBOL(prandom_u32_state); 61EXPORT_SYMBOL(prandom32);
62 62
63/** 63/**
64 * prandom_u32 - pseudo random number generator 64 * random32 - pseudo random number generator
65 * 65 *
66 * A 32 bit pseudo-random number is generated using a fast 66 * A 32 bit pseudo-random number is generated using a fast
67 * algorithm suitable for simulation. This algorithm is NOT 67 * algorithm suitable for simulation. This algorithm is NOT
68 * considered safe for cryptographic use. 68 * considered safe for cryptographic use.
69 */ 69 */
70u32 prandom_u32(void) 70u32 random32(void)
71{ 71{
72 unsigned long r; 72 unsigned long r;
73 struct rnd_state *state = &get_cpu_var(net_rand_state); 73 struct rnd_state *state = &get_cpu_var(net_rand_state);
74 r = prandom_u32_state(state); 74 r = prandom32(state);
75 put_cpu_var(state); 75 put_cpu_var(state);
76 return r; 76 return r;
77} 77}
78EXPORT_SYMBOL(prandom_u32); 78EXPORT_SYMBOL(random32);
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 */
90void 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}
113EXPORT_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 */
120void 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}
127EXPORT_SYMBOL(prandom_bytes);
128 79
129/** 80/**
130 * prandom_seed - add entropy to pseudo random number generator 81 * srandom32 - add entropy to pseudo random number generator
131 * @seed: seed value 82 * @seed: seed value
132 * 83 *
133 * Add some additional seeding to the prandom pool. 84 * Add some additional seeding to the random32() pool.
134 */ 85 */
135void prandom_seed(u32 entropy) 86void srandom32(u32 entropy)
136{ 87{
137 int i; 88 int i;
138 /* 89 /*
@@ -144,13 +95,13 @@ void prandom_seed(u32 entropy)
144 state->s1 = __seed(state->s1 ^ entropy, 1); 95 state->s1 = __seed(state->s1 ^ entropy, 1);
145 } 96 }
146} 97}
147EXPORT_SYMBOL(prandom_seed); 98EXPORT_SYMBOL(srandom32);
148 99
149/* 100/*
150 * Generate some initially weak seeding values to allow 101 * Generate some initially weak seeding values to allow
151 * to start the prandom_u32() engine. 102 * to start the random32() engine.
152 */ 103 */
153static int __init prandom_init(void) 104static int __init random32_init(void)
154{ 105{
155 int i; 106 int i;
156 107
@@ -163,22 +114,22 @@ static int __init prandom_init(void)
163 state->s3 = __seed(LCG(state->s2), 15); 114 state->s3 = __seed(LCG(state->s2), 15);
164 115
165 /* "warm it up" */ 116 /* "warm it up" */
166 prandom_u32_state(state); 117 prandom32(state);
167 prandom_u32_state(state); 118 prandom32(state);
168 prandom_u32_state(state); 119 prandom32(state);
169 prandom_u32_state(state); 120 prandom32(state);
170 prandom_u32_state(state); 121 prandom32(state);
171 prandom_u32_state(state); 122 prandom32(state);
172 } 123 }
173 return 0; 124 return 0;
174} 125}
175core_initcall(prandom_init); 126core_initcall(random32_init);
176 127
177/* 128/*
178 * Generate better values after random number generator 129 * Generate better values after random number generator
179 * is fully initialized. 130 * is fully initialized.
180 */ 131 */
181static int __init prandom_reseed(void) 132static int __init random32_reseed(void)
182{ 133{
183 int i; 134 int i;
184 135
@@ -192,8 +143,8 @@ static int __init prandom_reseed(void)
192 state->s3 = __seed(seeds[2], 15); 143 state->s3 = __seed(seeds[2], 15);
193 144
194 /* mix it in */ 145 /* mix it in */
195 prandom_u32_state(state); 146 prandom32(state);
196 } 147 }
197 return 0; 148 return 0;
198} 149}
199late_initcall(prandom_reseed); 150late_initcall(random32_reseed);