Add missing RAND_DRBG locking
[openssl.git] / crypto / rand / drbg_lib.c
1 /*
2  * Copyright 2011-2017 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 <string.h>
11 #include <openssl/crypto.h>
12 #include <openssl/err.h>
13 #include <openssl/rand.h>
14 #include "rand_lcl.h"
15 #include "internal/thread_once.h"
16 #include "internal/rand_int.h"
17
18 static RAND_DRBG rand_drbg; /* The default global DRBG. */
19 static RAND_DRBG priv_drbg; /* The global private-key DRBG. */
20
21 /* NIST SP 800-90A DRBG recommends the use of a personalization string. */
22 static const char ossl_pers_string[] = "OpenSSL NIST SP 800-90A DRBG";
23
24 /*
25  * Support framework for NIST SP 800-90A DRBG, AES-CTR mode.
26  * The RAND_DRBG is OpenSSL's pointer to an instance of the DRBG.
27  *
28  * The OpenSSL model is to have new and free functions, and that new
29  * does all initialization.  That is not the NIST model, which has
30  * instantiation and un-instantiate, and re-use within a new/free
31  * lifecycle.  (No doubt this comes from the desire to support hardware
32  * DRBG, where allocation of resources on something like an HSM is
33  * a much bigger deal than just re-setting an allocated resource.)
34  */
35
36 static CRYPTO_ONCE rand_drbg_init = CRYPTO_ONCE_STATIC_INIT;
37
38 static int drbg_setup(RAND_DRBG *drbg, const char *name);
39
40 /*
41  * Set/initialize |drbg| to be of type |nid|, with optional |flags|.
42  * Return -2 if the type is not supported, 1 on success and -1 on
43  * failure.
44  */
45 int RAND_DRBG_set(RAND_DRBG *drbg, int nid, unsigned int flags)
46 {
47     int ret = 1;
48
49     drbg->state = DRBG_UNINITIALISED;
50     drbg->flags = flags;
51     drbg->nid = nid;
52
53     switch (nid) {
54     default:
55         RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_UNSUPPORTED_DRBG_TYPE);
56         return -2;
57     case 0:
58         /* Uninitialized; that's okay. */
59         return 1;
60     case NID_aes_128_ctr:
61     case NID_aes_192_ctr:
62     case NID_aes_256_ctr:
63         ret = ctr_init(drbg);
64         break;
65     }
66
67     if (ret < 0)
68         RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_ERROR_INITIALISING_DRBG);
69     return ret;
70 }
71
72 /*
73  * Allocate memory and initialize a new DRBG.  The |parent|, if not
74  * NULL, will be used to auto-seed this RAND_DRBG as needed.
75  */
76 RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent)
77 {
78     RAND_DRBG *drbg = OPENSSL_zalloc(sizeof(*drbg));
79
80     if (drbg == NULL) {
81         RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE);
82         goto err;
83     }
84     drbg->fork_count = rand_fork_count;
85     drbg->parent = parent;
86     if (RAND_DRBG_set(drbg, type, flags) < 0)
87         goto err;
88
89     if (parent != NULL) {
90         if (!RAND_DRBG_set_callbacks(drbg, rand_drbg_get_entropy,
91                                      rand_drbg_cleanup_entropy,
92                                      NULL, NULL))
93             goto err;
94     }
95
96     return drbg;
97
98 err:
99     OPENSSL_free(drbg);
100     return NULL;
101 }
102
103 /*
104  * Uninstantiate |drbg| and free all memory.
105  */
106 void RAND_DRBG_free(RAND_DRBG *drbg)
107 {
108     if (drbg == NULL)
109         return;
110
111     ctr_uninstantiate(drbg);
112     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
113     OPENSSL_clear_free(drbg, sizeof(*drbg));
114 }
115
116 /*
117  * Instantiate |drbg|, after it has been initialized.  Use |pers| and
118  * |perslen| as prediction-resistance input.
119  *
120  * Requires that drbg->lock is already locked for write, if non-null.
121  */
122 int RAND_DRBG_instantiate(RAND_DRBG *drbg,
123                           const unsigned char *pers, size_t perslen)
124 {
125     unsigned char *nonce = NULL, *entropy = NULL;
126     size_t noncelen = 0, entropylen = 0;
127
128     if (perslen > drbg->max_perslen) {
129         RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
130                 RAND_R_PERSONALISATION_STRING_TOO_LONG);
131         goto end;
132     }
133     if (drbg->state != DRBG_UNINITIALISED) {
134         RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
135                 drbg->state == DRBG_ERROR ? RAND_R_IN_ERROR_STATE
136                                           : RAND_R_ALREADY_INSTANTIATED);
137         goto end;
138     }
139
140     drbg->state = DRBG_ERROR;
141     if (drbg->get_entropy != NULL)
142         entropylen = drbg->get_entropy(drbg, &entropy, drbg->strength,
143                                    drbg->min_entropylen, drbg->max_entropylen);
144     if (entropylen < drbg->min_entropylen
145         || entropylen > drbg->max_entropylen) {
146         RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_ENTROPY);
147         goto end;
148     }
149
150     if (drbg->max_noncelen > 0 && drbg->get_nonce != NULL) {
151         noncelen = drbg->get_nonce(drbg, &nonce, drbg->strength / 2,
152                                    drbg->min_noncelen, drbg->max_noncelen);
153         if (noncelen < drbg->min_noncelen || noncelen > drbg->max_noncelen) {
154             RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
155                     RAND_R_ERROR_RETRIEVING_NONCE);
156             goto end;
157         }
158     }
159
160     if (!ctr_instantiate(drbg, entropy, entropylen,
161                          nonce, noncelen, pers, perslen)) {
162         RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_INSTANTIATING_DRBG);
163         goto end;
164     }
165
166     drbg->state = DRBG_READY;
167     drbg->reseed_counter = 1;
168
169 end:
170     if (entropy != NULL && drbg->cleanup_entropy != NULL)
171         drbg->cleanup_entropy(drbg, entropy, entropylen);
172     if (nonce != NULL && drbg->cleanup_nonce!= NULL )
173         drbg->cleanup_nonce(drbg, nonce, noncelen);
174     if (drbg->pool != NULL) {
175         if (drbg->state == DRBG_READY) {
176             RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
177                     RAND_R_ERROR_ENTROPY_POOL_WAS_IGNORED);
178             drbg->state = DRBG_ERROR;
179         }
180         RAND_POOL_free(drbg->pool);
181         drbg->pool = NULL;
182     }
183     if (drbg->state == DRBG_READY)
184         return 1;
185     return 0;
186 }
187
188 /*
189  * Uninstantiate |drbg|. Must be instantiated before it can be used.
190  *
191  * Requires that drbg->lock is already locked for write, if non-null.
192  */
193 int RAND_DRBG_uninstantiate(RAND_DRBG *drbg)
194 {
195     int ret = ctr_uninstantiate(drbg);
196
197     OPENSSL_cleanse(&drbg->ctr, sizeof(drbg->ctr));
198     drbg->state = DRBG_UNINITIALISED;
199     return ret;
200 }
201
202 /*
203  * Reseed |drbg|, mixing in the specified data
204  *
205  * Requires that drbg->lock is already locked for write, if non-null.
206  */
207 int RAND_DRBG_reseed(RAND_DRBG *drbg,
208                      const unsigned char *adin, size_t adinlen)
209 {
210     unsigned char *entropy = NULL;
211     size_t entropylen = 0;
212
213     if (drbg->state == DRBG_ERROR) {
214         RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_IN_ERROR_STATE);
215         return 0;
216     }
217     if (drbg->state == DRBG_UNINITIALISED) {
218         RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_NOT_INSTANTIATED);
219         return 0;
220     }
221
222     if (adin == NULL)
223         adinlen = 0;
224     else if (adinlen > drbg->max_adinlen) {
225         RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
226         return 0;
227     }
228
229     drbg->state = DRBG_ERROR;
230     if (drbg->get_entropy != NULL)
231         entropylen = drbg->get_entropy(drbg, &entropy, drbg->strength,
232                                    drbg->min_entropylen, drbg->max_entropylen);
233     if (entropylen < drbg->min_entropylen
234         || entropylen > drbg->max_entropylen) {
235         RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ERROR_RETRIEVING_ENTROPY);
236         goto end;
237     }
238
239     if (!ctr_reseed(drbg, entropy, entropylen, adin, adinlen))
240         goto end;
241     drbg->state = DRBG_READY;
242     drbg->reseed_counter = 1;
243
244 end:
245     if (entropy != NULL && drbg->cleanup_entropy != NULL)
246         drbg->cleanup_entropy(drbg, entropy, entropylen);
247     if (drbg->state == DRBG_READY)
248         return 1;
249     return 0;
250 }
251
252 /*
253  * Restart |drbg|, using the specified entropy or additional input
254  *
255  * Tries its best to get the drbg instantiated by all means,
256  * regardless of its current state.
257  *
258  * Optionally, a |buffer| of |len| random bytes can be passed,
259  * which is assumed to contain at least |entropy| bits of entropy.
260  *
261  * If |entropy| > 0, the buffer content is used as entropy input.
262  *
263  * If |entropy| == 0, the buffer content is used as additional input
264  *
265  * Returns 1 on success, 0 on failure.
266  *
267  * This function is used internally only.
268  */
269 int rand_drbg_restart(RAND_DRBG *drbg,
270                       const unsigned char *buffer, size_t len, size_t entropy)
271 {
272     int reseeded = 0;
273     const unsigned char *adin = NULL;
274     size_t adinlen = 0;
275
276     if (drbg->pool != NULL) {
277         RANDerr(RAND_F_RAND_DRBG_RESTART, ERR_R_INTERNAL_ERROR);
278         RAND_POOL_free(drbg->pool);
279         drbg->pool = NULL;
280     }
281
282     if (buffer != NULL) {
283         if (entropy > 0) {
284             if (drbg->max_entropylen < len) {
285                 RANDerr(RAND_F_RAND_DRBG_RESTART,
286                     RAND_R_ENTROPY_INPUT_TOO_LONG);
287                 return 0;
288             }
289
290             if (entropy > 8 * len) {
291                 RANDerr(RAND_F_RAND_DRBG_RESTART, RAND_R_ENTROPY_OUT_OF_RANGE);
292                 return 0;
293             }
294
295             /* will be picked up by the rand_drbg_get_entropy() callback */
296             drbg->pool = RAND_POOL_new(entropy, len, len);
297             if (drbg->pool == NULL)
298                 return 0;
299
300             RAND_POOL_add(drbg->pool, buffer, len, entropy);
301         } else {
302             if (drbg->max_adinlen < len) {
303                 RANDerr(RAND_F_RAND_DRBG_RESTART,
304                         RAND_R_ADDITIONAL_INPUT_TOO_LONG);
305                 return 0;
306             }
307             adin = buffer;
308             adinlen = len;
309         }
310     }
311
312     /* repair error state */
313     if (drbg->state == DRBG_ERROR)
314         RAND_DRBG_uninstantiate(drbg);
315
316     /* repair uninitialized state */
317     if (drbg->state == DRBG_UNINITIALISED) {
318         drbg_setup(drbg, NULL);
319         /* already reseeded. prevent second reseeding below */
320         reseeded = (drbg->state == DRBG_READY);
321     }
322
323     /* refresh current state if entropy or additional input has been provided */
324     if (drbg->state == DRBG_READY) {
325         if (adin != NULL) {
326             /*
327              * mix in additional input without reseeding
328              *
329              * Similar to RAND_DRBG_reseed(), but the provided additional
330              * data |adin| is mixed into the current state without pulling
331              * entropy from the trusted entropy source using get_entropy().
332              * This is not a reseeding in the strict sense of NIST SP 800-90A.
333              */
334             ctr_reseed(drbg, adin, adinlen, NULL, 0);
335         } else if (reseeded == 0) {
336             /* do a full reseeding if it has not been done yet above */
337             RAND_DRBG_reseed(drbg, NULL, 0);
338         }
339     }
340
341     /* check whether a given entropy pool was cleared properly during reseed */
342     if (drbg->pool != NULL) {
343         drbg->state = DRBG_ERROR;
344         RANDerr(RAND_F_RAND_DRBG_RESTART, ERR_R_INTERNAL_ERROR);
345         RAND_POOL_free(drbg->pool);
346         drbg->pool = NULL;
347         return 0;
348     }
349
350     return drbg->state == DRBG_READY;
351 }
352
353 /*
354  * Generate |outlen| bytes into the buffer at |out|.  Reseed if we need
355  * to or if |prediction_resistance| is set.  Additional input can be
356  * sent in |adin| and |adinlen|.
357  *
358  * Requires that drbg->lock is already locked for write, if non-null.
359  *
360  * Returns 1 on success, 0 on failure.
361  *
362  */
363 int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
364                        int prediction_resistance,
365                        const unsigned char *adin, size_t adinlen)
366 {
367     int reseed_required = 0;
368
369     if (drbg->state != DRBG_READY) {
370         /* try to recover from previous errors */
371         rand_drbg_restart(drbg, NULL, 0, 0);
372
373         if (drbg->state == DRBG_ERROR) {
374             RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_IN_ERROR_STATE);
375             return 0;
376         }
377         if (drbg->state == DRBG_UNINITIALISED) {
378             RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_NOT_INSTANTIATED);
379             return 0;
380         }
381     }
382
383     if (outlen > drbg->max_request) {
384         RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG);
385         return 0;
386     }
387     if (adinlen > drbg->max_adinlen) {
388         RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
389         return 0;
390     }
391
392     if (drbg->fork_count != rand_fork_count) {
393         drbg->fork_count = rand_fork_count;
394         reseed_required = 1;
395     }
396
397     if (drbg->reseed_counter >= drbg->reseed_interval)
398         reseed_required = 1;
399
400     if (reseed_required || prediction_resistance) {
401         if (!RAND_DRBG_reseed(drbg, adin, adinlen)) {
402             RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_RESEED_ERROR);
403             return 0;
404         }
405         adin = NULL;
406         adinlen = 0;
407     }
408
409     if (!ctr_generate(drbg, out, outlen, adin, adinlen)) {
410         drbg->state = DRBG_ERROR;
411         RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_GENERATE_ERROR);
412         return 0;
413     }
414
415     drbg->reseed_counter++;
416
417     return 1;
418 }
419
420 /*
421  * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
422  *
423  * In the following, the signature and the semantics of the
424  * get_entropy() and cleanup_entropy() callbacks are explained.
425  *
426  * GET_ENTROPY
427  *
428  *     size_t get_entropy(RAND_DRBG *ctx,
429  *                        unsigned char **pout,
430  *                        int entropy,
431  *                        size_t min_len, size_t max_len);
432  *
433  * This is a request to allocate and fill a buffer of size
434  * |min_len| <= size <= |max_len| (in bytes) which contains
435  * at least |entropy| bits of randomness. The buffer's address is
436  * to be returned in |*pout| and the number of collected
437  * randomness bytes (which may be less than the allocated size
438  * of the buffer) as return value.
439  *
440  * If the callback fails to acquire at least |entropy| bits of
441  * randomness, it shall return a buffer length of 0.
442  *
443  * CLEANUP_ENTROPY
444  *
445  *     void cleanup_entropy(RAND_DRBG *ctx,
446  *                          unsigned char *out, size_t outlen);
447  *
448  * A request to clear and free the buffer allocated by get_entropy().
449  * The values |out| and |outlen| are expected to be the random buffer's
450  * address and length, as returned by the get_entropy() callback.
451  *
452  * GET_NONCE, CLEANUP_NONCE
453  *
454  * Signature and semantics of the get_nonce() and cleanup_nonce()
455  * callbacks are analogous to get_entropy() and cleanup_entropy().
456  * Currently, the nonce is used only for the known answer tests.
457  */
458 int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
459                             RAND_DRBG_get_entropy_fn get_entropy,
460                             RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
461                             RAND_DRBG_get_nonce_fn get_nonce,
462                             RAND_DRBG_cleanup_nonce_fn cleanup_nonce)
463 {
464     if (drbg->state != DRBG_UNINITIALISED)
465         return 0;
466     drbg->get_entropy = get_entropy;
467     drbg->cleanup_entropy = cleanup_entropy;
468     drbg->get_nonce = get_nonce;
469     drbg->cleanup_nonce = cleanup_nonce;
470     return 1;
471 }
472
473 /*
474  * Set the reseed interval.
475  */
476 int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, int interval)
477 {
478     if (interval < 0 || interval > MAX_RESEED)
479         return 0;
480     drbg->reseed_interval = interval;
481     return 1;
482 }
483
484 /*
485  * Get and set the EXDATA
486  */
487 int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg)
488 {
489     return CRYPTO_set_ex_data(&drbg->ex_data, idx, arg);
490 }
491
492 void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx)
493 {
494     return CRYPTO_get_ex_data(&drbg->ex_data, idx);
495 }
496
497
498 /*
499  * The following functions provide a RAND_METHOD that works on the
500  * global DRBG.  They lock.
501  */
502
503 /*
504  * Initializes the DRBG with default settings.
505  * For global DRBGs a global lock is created with the given name
506  * Returns 1 on success, 0 on failure
507  */
508 static int drbg_setup(RAND_DRBG *drbg, const char *name)
509 {
510     int ret = 1;
511
512     if (name != NULL) {
513         if (drbg->lock != NULL) {
514             RANDerr(RAND_F_DRBG_SETUP, ERR_R_INTERNAL_ERROR);
515             return 0;
516         }
517
518         drbg->lock = CRYPTO_THREAD_glock_new(name);
519         if (drbg->lock == NULL) {
520             RANDerr(RAND_F_DRBG_SETUP, RAND_R_FAILED_TO_CREATE_LOCK);
521             return 0;
522         }
523     }
524
525     ret &= RAND_DRBG_set(drbg,
526                          RAND_DRBG_NID, RAND_DRBG_FLAG_CTR_USE_DF) == 1;
527     ret &= RAND_DRBG_set_callbacks(drbg, rand_drbg_get_entropy,
528                                    rand_drbg_cleanup_entropy, NULL, NULL) == 1;
529     /*
530      * Ignore instantiation error so support just-in-time instantiation.
531      *
532      * The state of the drbg will be checked in RAND_DRBG_generate() and
533      * an automatic recovery is attempted.
534      */
535     RAND_DRBG_instantiate(drbg,
536                           (const unsigned char *) ossl_pers_string,
537                           sizeof(ossl_pers_string) - 1);
538     return ret;
539 }
540
541 /*
542  * Initialize the global DRBGs on first use.
543  * Returns 1 on success, 0 on failure.
544  */
545 DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init)
546 {
547     int ret = 1;
548
549     ret &= drbg_setup(&rand_drbg, "rand_drbg");
550     ret &= drbg_setup(&priv_drbg, "priv_drbg");
551
552     return ret;
553 }
554
555 /* Cleans up the given global DRBG  */
556 static void drbg_cleanup(RAND_DRBG *drbg)
557 {
558     CRYPTO_THREAD_lock_free(drbg->lock);
559     RAND_DRBG_uninstantiate(drbg);
560 }
561
562 /* Clean up the global DRBGs before exit */
563 void rand_drbg_cleanup_int(void)
564 {
565     drbg_cleanup(&rand_drbg);
566     drbg_cleanup(&priv_drbg);
567 }
568
569 /* Implements the default OpenSSL RAND_bytes() method */
570 static int drbg_bytes(unsigned char *out, int count)
571 {
572     int ret = 0;
573     size_t chunk;
574     RAND_DRBG *drbg = RAND_DRBG_get0_global();
575
576     if (drbg == NULL)
577         return 0;
578
579     CRYPTO_THREAD_write_lock(drbg->lock);
580     if (drbg->state == DRBG_UNINITIALISED)
581         goto err;
582
583     for ( ; count > 0; count -= chunk, out += chunk) {
584         chunk = count;
585         if (chunk > drbg->max_request)
586             chunk = drbg->max_request;
587         ret = RAND_DRBG_generate(drbg, out, chunk, 0, NULL, 0);
588         if (!ret)
589             goto err;
590     }
591     ret = 1;
592
593 err:
594     CRYPTO_THREAD_unlock(drbg->lock);
595     return ret;
596 }
597
598 /* Implements the default OpenSSL RAND_add() method */
599 static int drbg_add(const void *buf, int num, double randomness)
600 {
601     int ret = 0;
602     RAND_DRBG *drbg = RAND_DRBG_get0_global();
603
604     if (drbg == NULL)
605         return 0;
606
607     if (num < 0 || randomness < 0.0)
608         return 0;
609
610     if (randomness > (double)drbg->max_entropylen) {
611         /*
612          * The purpose of this check is to bound |randomness| by a
613          * relatively small value in order to prevent an integer
614          * overflow when multiplying by 8 in the rand_drbg_restart()
615          * call below.
616          */
617         return 0;
618     }
619
620     CRYPTO_THREAD_write_lock(drbg->lock);
621     ret = rand_drbg_restart(drbg, buf,
622                             (size_t)(unsigned int)num,
623                             (size_t)(8*randomness));
624     CRYPTO_THREAD_unlock(drbg->lock);
625
626     return ret;
627 }
628
629 /* Implements the default OpenSSL RAND_seed() method */
630 static int drbg_seed(const void *buf, int num)
631 {
632     return drbg_add(buf, num, num);
633 }
634
635 /* Implements the default OpenSSL RAND_status() method */
636 static int drbg_status(void)
637 {
638     int ret;
639     RAND_DRBG *drbg = RAND_DRBG_get0_global();
640
641     if (drbg == NULL)
642         return 0;
643
644     CRYPTO_THREAD_write_lock(drbg->lock);
645     ret = drbg->state == DRBG_READY ? 1 : 0;
646     CRYPTO_THREAD_unlock(drbg->lock);
647     return ret;
648 }
649
650 /*
651  * Get the global public DRBG.
652  * Returns pointer to the DRBG on success, NULL on failure.
653  */
654 RAND_DRBG *RAND_DRBG_get0_global(void)
655 {
656     if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
657         return NULL;
658
659     return &rand_drbg;
660 }
661
662 /*
663  * Get the global private DRBG.
664  * Returns pointer to the DRBG on success, NULL on failure.
665  */
666 RAND_DRBG *RAND_DRBG_get0_priv_global(void)
667 {
668     if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
669         return NULL;
670
671     return &priv_drbg;
672 }
673
674 RAND_METHOD rand_meth = {
675     drbg_seed,
676     drbg_bytes,
677     NULL,
678     drbg_add,
679     drbg_bytes,
680     drbg_status
681 };
682
683 RAND_METHOD *RAND_OpenSSL(void)
684 {
685     return &rand_meth;
686 }