Set randomness buffer pointer in get_entropy calls.
[openssl.git] / crypto / rand / rand_lib.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <time.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/opensslconf.h>
14 #include "internal/rand_int.h"
15 #include <openssl/engine.h>
16 #include "internal/thread_once.h"
17 #include "rand_lcl.h"
18
19 #ifndef OPENSSL_NO_ENGINE
20 /* non-NULL if default_RAND_meth is ENGINE-provided */
21 static ENGINE *funct_ref;
22 static CRYPTO_RWLOCK *rand_engine_lock;
23 #endif
24 static CRYPTO_RWLOCK *rand_meth_lock;
25 static const RAND_METHOD *default_RAND_meth;
26 static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT;
27 RAND_BYTES_BUFFER rand_bytes;
28
29 #ifdef OPENSSL_RAND_SEED_RDTSC
30 /*
31  * IMPORTANT NOTE:  It is not currently possible to use this code
32  * because we are not sure about the amount of randomness.  Some
33  * SP900 tests have been run, but there is internal skepticism.
34  * So for now this code is not used.
35  */
36 # error "RDTSC enabled?  Should not be possible!"
37
38 /*
39  * Since we get some randomness from the low-order bits of the
40  * high-speec clock, it can help.  But don't return a status since
41  * it's not sufficient to indicate whether or not the seeding was
42  * done.
43  */
44 void rand_read_tsc(RAND_poll_fn cb, void *arg)
45 {
46     unsigned char c;
47     int i;
48
49     for (i = 0; i < 10; i++) {
50         c = (unsigned char)(OPENSSL_rdtsc() & 0xFF);
51         cb(arg, &c, 1, 0.5);
52     }
53 }
54 #endif
55
56 #ifdef OPENSSL_RAND_SEED_RDCPU
57 size_t OPENSSL_ia32_rdseed(void);
58 size_t OPENSSL_ia32_rdrand(void);
59
60 extern unsigned int OPENSSL_ia32cap_P[];
61
62 int rand_read_cpu(RAND_poll_fn cb, void *arg)
63 {
64     size_t i, s;
65
66     /* If RDSEED is available, use that. */
67     if ((OPENSSL_ia32cap_P[1] & (1 << 18)) != 0) {
68         for (i = 0; i < RANDOMNESS_NEEDED; i += sizeof(s)) {
69             s = OPENSSL_ia32_rdseed();
70             if (s == 0)
71                 break;
72             cb(arg, &s, (int)sizeof(s), sizeof(s));
73         }
74         if (i >= RANDOMNESS_NEEDED)
75             return 1;
76     }
77
78     /* Second choice is RDRAND. */
79     if ((OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) != 0) {
80         for (i = 0; i < RANDOMNESS_NEEDED; i += sizeof(s)) {
81             s = OPENSSL_ia32_rdrand();
82             if (s == 0)
83                 break;
84             cb(arg, &s, (int)sizeof(s), sizeof(s));
85         }
86         if (i >= RANDOMNESS_NEEDED)
87             return 1;
88     }
89
90     return 0;
91 }
92 #endif
93
94
95 /*
96  * DRBG has two sets of callbacks; we only discuss the "entropy" one
97  * here.  When the DRBG needs additional randomness bits (called entropy
98  * in the NIST document), it calls the get_entropy callback which fills in
99  * a pointer and returns the number of bytes. When the DRBG is finished with
100  * the buffer, it calls the cleanup_entropy callback, with the value of
101  * the buffer that the get_entropy callback filled in.
102  *
103  * Get entropy from the system, via RAND_poll if needed.  The |entropy|
104  * is the bits of randomness required, and is expected to fit into a buffer
105  * of |min_len|..|max__len| size.  We assume we're getting high-quality
106  * randomness from the system, and that |min_len| bytes will do.
107  */
108 size_t drbg_entropy_from_system(RAND_DRBG *drbg,
109                                 unsigned char **pout,
110                                 int entropy, size_t min_len, size_t max_len)
111 {
112     int i;
113
114
115     if (min_len > (size_t)drbg->size) {
116         /* Should not happen.  See comment near RANDOMNESS_NEEDED. */
117         min_len = drbg->size;
118     }
119
120     if (rand_drbg.filled) {
121         /* Re-use what we have. */
122         *pout = drbg->randomness;
123         return drbg->size;
124     }
125
126     /* If we don't have enough, try to get more. */
127     CRYPTO_THREAD_write_lock(rand_bytes.lock);
128     for (i = RAND_POLL_RETRIES; rand_bytes.curr < min_len && --i >= 0; ) {
129         CRYPTO_THREAD_unlock(rand_bytes.lock);
130         RAND_poll();
131         CRYPTO_THREAD_write_lock(rand_bytes.lock);
132     }
133
134     /* Get desired amount, but no more than we have. */
135     if (min_len > rand_bytes.curr)
136         min_len = rand_bytes.curr;
137     if (min_len != 0) {
138         memcpy(drbg->randomness, rand_bytes.buff, min_len);
139         rand_drbg.filled = 1;
140         /* Update amount left and shift it down. */
141         rand_bytes.curr -= min_len;
142         if (rand_bytes.curr != 0)
143             memmove(rand_bytes.buff, &rand_bytes.buff[min_len], rand_bytes.curr);
144     }
145     CRYPTO_THREAD_unlock(rand_bytes.lock);
146     *pout = drbg->randomness;
147     return min_len;
148 }
149
150 size_t drbg_entropy_from_parent(RAND_DRBG *drbg,
151                                 unsigned char **pout,
152                                 int entropy, size_t min_len, size_t max_len)
153 {
154     int st;
155
156     if (min_len > (size_t)drbg->size) {
157         /* Should not happen.  See comment near RANDOMNESS_NEEDED. */
158         min_len = drbg->size;
159     }
160
161     /* Get random from parent, include our state as additional input. */
162     st = RAND_DRBG_generate(drbg->parent, drbg->randomness, min_len, 0,
163                             (unsigned char *)drbg, sizeof(*drbg));
164     if (st == 0)
165         return 0;
166     drbg->filled = 1;
167     *pout = drbg->randomness;
168     return min_len;
169 }
170
171 void drbg_release_entropy(RAND_DRBG *drbg, unsigned char *out)
172 {
173     drbg->filled = 0;
174     OPENSSL_cleanse(drbg->randomness, drbg->size);
175 }
176
177
178 /*
179  * Set up a global DRBG.
180  */
181 static int setup_drbg(RAND_DRBG *drbg)
182 {
183     int ret = 1;
184
185     drbg->lock = CRYPTO_THREAD_lock_new();
186     ret &= drbg->lock != NULL;
187     drbg->size = RANDOMNESS_NEEDED;
188     drbg->randomness = OPENSSL_malloc(drbg->size);
189     ret &= drbg->randomness != NULL;
190     /* If you change these parameters, see RANDOMNESS_NEEDED */
191     ret &= RAND_DRBG_set(drbg,
192                          NID_aes_128_ctr, RAND_DRBG_FLAG_CTR_USE_DF) == 1;
193     ret &= RAND_DRBG_set_callbacks(drbg, drbg_entropy_from_system,
194                                    drbg_release_entropy, NULL, NULL) == 1;
195     return ret;
196 }
197
198 static void free_drbg(RAND_DRBG *drbg)
199 {
200     CRYPTO_THREAD_lock_free(drbg->lock);
201     OPENSSL_clear_free(drbg->randomness, drbg->size);
202     RAND_DRBG_uninstantiate(drbg);
203 }
204
205 DEFINE_RUN_ONCE_STATIC(do_rand_init)
206 {
207     int ret = 1;
208
209 #ifndef OPENSSL_NO_ENGINE
210     rand_engine_lock = CRYPTO_THREAD_lock_new();
211     ret &= rand_engine_lock != NULL;
212 #endif
213     rand_meth_lock = CRYPTO_THREAD_lock_new();
214     ret &= rand_meth_lock != NULL;
215
216     rand_bytes.lock = CRYPTO_THREAD_lock_new();
217     ret &= rand_bytes.lock != NULL;
218     rand_bytes.curr = 0;
219     rand_bytes.size = MAX_RANDOMNESS_HELD;
220     /* TODO: Should this be secure malloc? */
221     rand_bytes.buff = malloc(rand_bytes.size);
222
223     ret &= rand_bytes.buff != NULL;
224     ret &= setup_drbg(&rand_drbg);
225     ret &= setup_drbg(&priv_drbg);
226     return ret;
227 }
228
229
230 void rand_cleanup_int(void)
231 {
232     const RAND_METHOD *meth = default_RAND_meth;
233
234     if (meth != NULL && meth->cleanup != NULL)
235         meth->cleanup();
236     RAND_set_rand_method(NULL);
237 #ifndef OPENSSL_NO_ENGINE
238     CRYPTO_THREAD_lock_free(rand_engine_lock);
239 #endif
240     CRYPTO_THREAD_lock_free(rand_meth_lock);
241     CRYPTO_THREAD_lock_free(rand_bytes.lock);
242     OPENSSL_clear_free(rand_bytes.buff, rand_bytes.size);
243     free_drbg(&rand_drbg);
244     free_drbg(&priv_drbg);
245 }
246
247 /*
248  * RAND_poll_ex() gets a function pointer to call when it has random bytes.
249  * RAND_poll() sets the function pointer to be a wrapper that calls RAND_add().
250  */
251 static void call_rand_add(void* arg, const void *buf, int num, double r)
252 {
253     RAND_add(buf, num, r);
254 }
255
256 int RAND_poll(void)
257 {
258     return RAND_poll_ex(call_rand_add, NULL);
259 }
260
261 int RAND_set_rand_method(const RAND_METHOD *meth)
262 {
263     if (!RUN_ONCE(&rand_init, do_rand_init))
264         return 0;
265
266     CRYPTO_THREAD_write_lock(rand_meth_lock);
267 #ifndef OPENSSL_NO_ENGINE
268     ENGINE_finish(funct_ref);
269     funct_ref = NULL;
270 #endif
271     default_RAND_meth = meth;
272     CRYPTO_THREAD_unlock(rand_meth_lock);
273     return 1;
274 }
275
276 const RAND_METHOD *RAND_get_rand_method(void)
277 {
278     const RAND_METHOD *tmp_meth = NULL;
279
280     if (!RUN_ONCE(&rand_init, do_rand_init))
281         return NULL;
282
283     CRYPTO_THREAD_write_lock(rand_meth_lock);
284     if (default_RAND_meth == NULL) {
285 #ifndef OPENSSL_NO_ENGINE
286         ENGINE *e;
287
288         /* If we have an engine that can do RAND, use it. */
289         if ((e = ENGINE_get_default_RAND()) != NULL
290                 && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
291             funct_ref = e;
292             default_RAND_meth = tmp_meth;
293         } else {
294             ENGINE_finish(e);
295             default_RAND_meth = &rand_meth;
296         }
297 #else
298         default_RAND_meth = &rand_meth;
299 #endif
300     }
301     tmp_meth = default_RAND_meth;
302     CRYPTO_THREAD_unlock(rand_meth_lock);
303     return tmp_meth;
304 }
305
306 #ifndef OPENSSL_NO_ENGINE
307 int RAND_set_rand_engine(ENGINE *engine)
308 {
309     const RAND_METHOD *tmp_meth = NULL;
310
311     if (!RUN_ONCE(&rand_init, do_rand_init))
312         return 0;
313
314     if (engine != NULL) {
315         if (!ENGINE_init(engine))
316             return 0;
317         tmp_meth = ENGINE_get_RAND(engine);
318         if (tmp_meth == NULL) {
319             ENGINE_finish(engine);
320             return 0;
321         }
322     }
323     CRYPTO_THREAD_write_lock(rand_engine_lock);
324     /* This function releases any prior ENGINE so call it first */
325     RAND_set_rand_method(tmp_meth);
326     funct_ref = engine;
327     CRYPTO_THREAD_unlock(rand_engine_lock);
328     return 1;
329 }
330 #endif
331
332 void RAND_seed(const void *buf, int num)
333 {
334     const RAND_METHOD *meth = RAND_get_rand_method();
335
336     if (meth->seed != NULL)
337         meth->seed(buf, num);
338 }
339
340 void RAND_add(const void *buf, int num, double randomness)
341 {
342     const RAND_METHOD *meth = RAND_get_rand_method();
343
344     if (meth->add != NULL)
345         meth->add(buf, num, randomness);
346 }
347
348 /*
349  * This function is not part of RAND_METHOD, so if we're not using
350  * the default method, then just call RAND_bytes().  Otherwise make
351  * sure we're instantiated and use the private DRBG.
352  */
353 int RAND_priv_bytes(unsigned char *buf, int num)
354 {
355     const RAND_METHOD *meth = RAND_get_rand_method();
356
357     if (meth != RAND_OpenSSL())
358         return RAND_bytes(buf, num);
359
360     if (priv_drbg.state == DRBG_UNINITIALISED
361             && RAND_DRBG_instantiate(&priv_drbg, NULL, 0) == 0)
362         return 0;
363     return RAND_DRBG_generate(&priv_drbg, buf, num, 0, NULL, 0);
364
365 }
366
367 int RAND_bytes(unsigned char *buf, int num)
368 {
369     const RAND_METHOD *meth = RAND_get_rand_method();
370
371     if (meth->bytes != NULL)
372         return meth->bytes(buf, num);
373     RANDerr(RAND_F_RAND_BYTES, RAND_R_FUNC_NOT_IMPLEMENTED);
374     return -1;
375 }
376
377 #if OPENSSL_API_COMPAT < 0x10100000L
378 int RAND_pseudo_bytes(unsigned char *buf, int num)
379 {
380     const RAND_METHOD *meth = RAND_get_rand_method();
381
382     if (meth->pseudorand != NULL)
383         return meth->pseudorand(buf, num);
384     return -1;
385 }
386 #endif
387
388 int RAND_status(void)
389 {
390     const RAND_METHOD *meth = RAND_get_rand_method();
391
392     if (meth->status != NULL)
393         return meth->status();
394     return 0;
395 }