1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * See the paper "On Randomness" by Ben Laurie for an explanation of this PRNG.
18 * http://www.apache-ssl.org/randomness.pdf
19 * XXX: Is there a formal proof of this PRNG? Couldn't we use the more popular
20 * Mersenne Twister PRNG (and BSD licensed)?
24 #include "apr_pools.h"
25 #include "apr_random.h"
26 #include "apr_thread_proc.h"
32 #define min(a,b) ((a) < (b) ? (a) : (b))
34 #define APR_RANDOM_DEFAULT_POOLS 32
35 #define APR_RANDOM_DEFAULT_REHASH_SIZE 1024
36 #define APR_RANDOM_DEFAULT_RESEED_SIZE 32
37 #define APR_RANDOM_DEFAULT_HASH_SECRET_SIZE 32
38 #define APR_RANDOM_DEFAULT_G_FOR_INSECURE 32
39 #define APR_RANDOM_DEFAULT_G_FOR_SECURE 320
41 typedef struct apr_random_pool_t {
44 unsigned int pool_size;
47 #define hash_init(h) (h)->init(h)
48 #define hash_add(h,b,n) (h)->add(h,b,n)
49 #define hash_finish(h,r) (h)->finish(h,r)
51 #define hash(h,r,b,n) hash_init(h),hash_add(h,b,n),hash_finish(h,r)
53 #define crypt_setkey(c,k) (c)->set_key((c)->data,k)
54 #define crypt_crypt(c,out,in) (c)->crypt((c)->date,out,in)
58 apr_crypto_hash_t *pool_hash;
60 apr_random_pool_t *pools;
61 unsigned int next_pool;
62 unsigned int generation;
63 apr_size_t rehash_size;
64 apr_size_t reseed_size;
65 apr_crypto_hash_t *key_hash;
66 #define K_size(g) ((g)->key_hash->size)
67 apr_crypto_hash_t *prng_hash;
68 #define B_size(g) ((g)->prng_hash->size)
71 unsigned char *H_waiting;
72 #define H_size(g) (B_size(g)+K_size(g))
73 #define H_current(g) (((g)->insecure_started && !(g)->secure_started) \
74 ? (g)->H_waiting : (g)->H)
76 unsigned char *randomness;
77 apr_size_t random_bytes;
78 unsigned int g_for_insecure;
79 unsigned int g_for_secure;
80 unsigned int secure_base;
81 unsigned int insecure_started:1;
82 unsigned int secure_started:1;
87 static apr_random_t *all_random;
89 static apr_status_t random_cleanup(void *data)
91 apr_random_t *remove_this = data,
93 **prev_ptr = &all_random;
95 if (cur == remove_this) {
96 *prev_ptr = cur->next;
99 prev_ptr = &cur->next;
106 APR_DECLARE(void) apr_random_init(apr_random_t *g,apr_pool_t *p,
107 apr_crypto_hash_t *pool_hash,
108 apr_crypto_hash_t *key_hash,
109 apr_crypto_hash_t *prng_hash)
115 g->pool_hash = pool_hash;
116 g->key_hash = key_hash;
117 g->prng_hash = prng_hash;
119 g->npools = APR_RANDOM_DEFAULT_POOLS;
120 g->pools = apr_palloc(p,g->npools*sizeof *g->pools);
121 for (n = 0; n < g->npools; ++n) {
122 g->pools[n].bytes = g->pools[n].pool_size = 0;
123 g->pools[n].pool = NULL;
129 g->rehash_size = APR_RANDOM_DEFAULT_REHASH_SIZE;
130 /* Ensure that the rehash size is twice the size of the pool hasher */
131 g->rehash_size = ((g->rehash_size+2*g->pool_hash->size-1)/g->pool_hash->size
132 /2)*g->pool_hash->size*2;
133 g->reseed_size = APR_RANDOM_DEFAULT_RESEED_SIZE;
135 g->H = apr_pcalloc(p,H_size(g));
136 g->H_waiting = apr_pcalloc(p,H_size(g));
138 g->randomness = apr_palloc(p,B_size(g));
141 g->g_for_insecure = APR_RANDOM_DEFAULT_G_FOR_INSECURE;
143 g->g_for_secure = APR_RANDOM_DEFAULT_G_FOR_SECURE;
144 g->secure_started = g->insecure_started = 0;
146 g->next = all_random;
148 apr_pool_cleanup_register(p, g, random_cleanup, apr_pool_cleanup_null);
151 static void mix_pid(apr_random_t *g,unsigned char *H,pid_t pid)
153 hash_init(g->key_hash);
154 hash_add(g->key_hash,H,H_size(g));
155 hash_add(g->key_hash,&pid,sizeof pid);
156 hash_finish(g->key_hash,H);
159 static void mixer(apr_random_t *g,pid_t pid)
161 unsigned char *H = H_current(g);
163 /* mix the PID into the current H */
165 /* if we are in waiting, then also mix into main H */
168 /* change order of pool mixing for good measure - note that going
169 backwards is much better than going forwards */
171 /* blow away any lingering randomness */
175 APR_DECLARE(void) apr_random_after_fork(apr_proc_t *proc)
179 for (r = all_random; r; r = r->next)
181 * XXX Note: the pid does not provide sufficient entropy to
182 * actually call this secure. See Ben's paper referenced at
183 * the top of this file.
188 APR_DECLARE(apr_random_t *) apr_random_standard_new(apr_pool_t *p)
190 apr_random_t *r = apr_palloc(p,sizeof *r);
192 apr_random_init(r,p,apr_crypto_sha256_new(p),apr_crypto_sha256_new(p),
193 apr_crypto_sha256_new(p));
197 static void rekey(apr_random_t *g)
200 unsigned char *H = H_current(g);
202 hash_init(g->key_hash);
203 hash_add(g->key_hash,H,H_size(g));
204 for (n = 0 ; n < g->npools && (n == 0 || g->generation&(1 << (n-1)))
206 hash_add(g->key_hash,g->pools[n].pool,g->pools[n].bytes);
207 g->pools[n].bytes = 0;
209 hash_finish(g->key_hash,H+B_size(g));
212 if (!g->insecure_started && g->generation > g->g_for_insecure) {
213 g->insecure_started = 1;
214 if (!g->secure_started) {
215 memcpy(g->H_waiting,g->H,H_size(g));
216 g->secure_base = g->generation;
220 if (!g->secure_started && g->generation > g->secure_base+g->g_for_secure) {
221 g->secure_started = 1;
222 memcpy(g->H,g->H_waiting,H_size(g));
226 APR_DECLARE(void) apr_random_add_entropy(apr_random_t *g,const void *entropy_,
230 const unsigned char *entropy = entropy_;
232 for (n = 0; n < bytes; ++n) {
233 apr_random_pool_t *p = &g->pools[g->next_pool];
235 if (++g->next_pool == g->npools)
238 if (p->pool_size < p->bytes+1) {
239 unsigned char *np = apr_palloc(g->apr_pool,(p->bytes+1)*2);
241 memcpy(np,p->pool,p->bytes);
243 p->pool_size = (p->bytes+1)*2;
245 p->pool[p->bytes++] = entropy[n];
247 if (p->bytes == g->rehash_size) {
250 for (r = 0; r < p->bytes/2; r+=g->pool_hash->size)
251 hash(g->pool_hash,p->pool+r,p->pool+r*2,g->pool_hash->size*2);
254 assert(p->bytes < g->rehash_size);
257 if (g->pools[0].bytes >= g->reseed_size)
261 /* This will give g->B_size bytes of randomness */
262 static void apr_random_block(apr_random_t *g,unsigned char *random)
264 /* FIXME: in principle, these are different hashes */
265 hash(g->prng_hash,g->H,g->H,H_size(g));
266 hash(g->prng_hash,random,g->H,B_size(g));
269 static void apr_random_bytes(apr_random_t *g,unsigned char *random,
274 for (n = 0; n < bytes; ) {
277 if (g->random_bytes == 0) {
278 apr_random_block(g,g->randomness);
279 g->random_bytes = B_size(g);
281 l = min(bytes-n,g->random_bytes);
282 memcpy(&random[n],g->randomness+B_size(g)-g->random_bytes,l);
288 APR_DECLARE(apr_status_t) apr_random_secure_bytes(apr_random_t *g,
292 if (!g->secure_started)
293 return APR_ENOTENOUGHENTROPY;
294 apr_random_bytes(g,random,bytes);
298 APR_DECLARE(apr_status_t) apr_random_insecure_bytes(apr_random_t *g,
302 if (!g->insecure_started)
303 return APR_ENOTENOUGHENTROPY;
304 apr_random_bytes(g,random,bytes);
308 APR_DECLARE(void) apr_random_barrier(apr_random_t *g)
310 g->secure_started = 0;
311 g->secure_base = g->generation;
314 APR_DECLARE(apr_status_t) apr_random_secure_ready(apr_random_t *r)
316 if (!r->secure_started)
317 return APR_ENOTENOUGHENTROPY;
321 APR_DECLARE(apr_status_t) apr_random_insecure_ready(apr_random_t *r)
323 if (!r->insecure_started)
324 return APR_ENOTENOUGHENTROPY;