Allocate DRBG additional data pool from non-secure memory
[openssl.git] / crypto / rand / rand_lib.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 #include "e_os.h"
19
20 #ifndef FIPS_MODE
21 # ifndef OPENSSL_NO_ENGINE
22 /* non-NULL if default_RAND_meth is ENGINE-provided */
23 static ENGINE *funct_ref;
24 static CRYPTO_RWLOCK *rand_engine_lock;
25 # endif
26 static CRYPTO_RWLOCK *rand_meth_lock;
27 static const RAND_METHOD *default_RAND_meth;
28 static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT;
29
30 static int rand_inited = 0;
31 #endif /* FIPS_MODE */
32
33 int rand_fork_count;
34
35 #ifdef OPENSSL_RAND_SEED_RDTSC
36 /*
37  * IMPORTANT NOTE:  It is not currently possible to use this code
38  * because we are not sure about the amount of randomness it provides.
39  * Some SP900 tests have been run, but there is internal skepticism.
40  * So for now this code is not used.
41  */
42 # error "RDTSC enabled?  Should not be possible!"
43
44 /*
45  * Acquire entropy from high-speed clock
46  *
47  * Since we get some randomness from the low-order bits of the
48  * high-speed clock, it can help.
49  *
50  * Returns the total entropy count, if it exceeds the requested
51  * entropy count. Otherwise, returns an entropy count of 0.
52  */
53 size_t rand_acquire_entropy_from_tsc(RAND_POOL *pool)
54 {
55     unsigned char c;
56     int i;
57
58     if ((OPENSSL_ia32cap_P[0] & (1 << 4)) != 0) {
59         for (i = 0; i < TSC_READ_COUNT; i++) {
60             c = (unsigned char)(OPENSSL_rdtsc() & 0xFF);
61             rand_pool_add(pool, &c, 1, 4);
62         }
63     }
64     return rand_pool_entropy_available(pool);
65 }
66 #endif
67
68 #ifdef OPENSSL_RAND_SEED_RDCPU
69 size_t OPENSSL_ia32_rdseed_bytes(unsigned char *buf, size_t len);
70 size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len);
71
72 extern unsigned int OPENSSL_ia32cap_P[];
73
74 /*
75  * Acquire entropy using Intel-specific cpu instructions
76  *
77  * Uses the RDSEED instruction if available, otherwise uses
78  * RDRAND if available.
79  *
80  * For the differences between RDSEED and RDRAND, and why RDSEED
81  * is the preferred choice, see https://goo.gl/oK3KcN
82  *
83  * Returns the total entropy count, if it exceeds the requested
84  * entropy count. Otherwise, returns an entropy count of 0.
85  */
86 size_t rand_acquire_entropy_from_cpu(RAND_POOL *pool)
87 {
88     size_t bytes_needed;
89     unsigned char *buffer;
90
91     bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
92     if (bytes_needed > 0) {
93         buffer = rand_pool_add_begin(pool, bytes_needed);
94
95         if (buffer != NULL) {
96             /* Whichever comes first, use RDSEED, RDRAND or nothing */
97             if ((OPENSSL_ia32cap_P[2] & (1 << 18)) != 0) {
98                 if (OPENSSL_ia32_rdseed_bytes(buffer, bytes_needed)
99                     == bytes_needed) {
100                     rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
101                 }
102             } else if ((OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) != 0) {
103                 if (OPENSSL_ia32_rdrand_bytes(buffer, bytes_needed)
104                     == bytes_needed) {
105                     rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
106                 }
107             } else {
108                 rand_pool_add_end(pool, 0, 0);
109             }
110         }
111     }
112
113     return rand_pool_entropy_available(pool);
114 }
115 #endif
116
117
118 /*
119  * Implements the get_entropy() callback (see RAND_DRBG_set_callbacks())
120  *
121  * If the DRBG has a parent, then the required amount of entropy input
122  * is fetched using the parent's RAND_DRBG_generate().
123  *
124  * Otherwise, the entropy is polled from the system entropy sources
125  * using rand_pool_acquire_entropy().
126  *
127  * If a random pool has been added to the DRBG using RAND_add(), then
128  * its entropy will be used up first.
129  */
130 size_t rand_drbg_get_entropy(RAND_DRBG *drbg,
131                              unsigned char **pout,
132                              int entropy, size_t min_len, size_t max_len,
133                              int prediction_resistance)
134 {
135     size_t ret = 0;
136     size_t entropy_available = 0;
137     RAND_POOL *pool;
138
139     if (drbg->parent != NULL && drbg->strength > drbg->parent->strength) {
140         /*
141          * We currently don't support the algorithm from NIST SP 800-90C
142          * 10.1.2 to use a weaker DRBG as source
143          */
144         RANDerr(RAND_F_RAND_DRBG_GET_ENTROPY, RAND_R_PARENT_STRENGTH_TOO_WEAK);
145         return 0;
146     }
147
148     if (drbg->seed_pool != NULL) {
149         pool = drbg->seed_pool;
150         pool->entropy_requested = entropy;
151     } else {
152         pool = rand_pool_new(entropy, drbg->secure, min_len, max_len);
153         if (pool == NULL)
154             return 0;
155     }
156
157     if (drbg->parent != NULL) {
158         size_t bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
159         unsigned char *buffer = rand_pool_add_begin(pool, bytes_needed);
160
161         if (buffer != NULL) {
162             size_t bytes = 0;
163
164             /*
165              * Get random from parent, include our state as additional input.
166              * Our lock is already held, but we need to lock our parent before
167              * generating bits from it. (Note: taking the lock will be a no-op
168              * if locking if drbg->parent->lock == NULL.)
169              */
170             rand_drbg_lock(drbg->parent);
171             if (RAND_DRBG_generate(drbg->parent,
172                                    buffer, bytes_needed,
173                                    prediction_resistance,
174                                    NULL, 0) != 0)
175                 bytes = bytes_needed;
176             drbg->reseed_next_counter
177                 = tsan_load(&drbg->parent->reseed_prop_counter);
178             rand_drbg_unlock(drbg->parent);
179
180             rand_pool_add_end(pool, bytes, 8 * bytes);
181             entropy_available = rand_pool_entropy_available(pool);
182         }
183
184     } else {
185         /* Get entropy by polling system entropy sources. */
186         entropy_available = rand_pool_acquire_entropy(pool);
187     }
188
189     if (entropy_available > 0) {
190         ret   = rand_pool_length(pool);
191         *pout = rand_pool_detach(pool);
192     }
193
194     if (drbg->seed_pool == NULL)
195         rand_pool_free(pool);
196     return ret;
197 }
198
199 /*
200  * Implements the cleanup_entropy() callback (see RAND_DRBG_set_callbacks())
201  *
202  */
203 void rand_drbg_cleanup_entropy(RAND_DRBG *drbg,
204                                unsigned char *out, size_t outlen)
205 {
206     if (drbg->seed_pool == NULL) {
207         if (drbg->secure)
208             OPENSSL_secure_clear_free(out, outlen);
209         else
210             OPENSSL_clear_free(out, outlen);
211     }
212 }
213
214 /*
215  * Generate additional data that can be used for the drbg. The data does
216  * not need to contain entropy, but it's useful if it contains at least
217  * some bits that are unpredictable.
218  *
219  * Returns 0 on failure.
220  *
221  * On success it allocates a buffer at |*pout| and returns the length of
222  * the data. The buffer should get freed using OPENSSL_secure_clear_free().
223  */
224 size_t rand_drbg_get_additional_data(RAND_POOL *pool, unsigned char **pout)
225 {
226     size_t ret = 0;
227
228     if (rand_pool_add_additional_data(pool) == 0)
229         goto err;
230
231     ret = rand_pool_length(pool);
232     *pout = rand_pool_detach(pool);
233
234  err:
235     return ret;
236 }
237
238 void rand_drbg_cleanup_additional_data(RAND_POOL *pool, unsigned char *out)
239 {
240     rand_pool_reattach(pool, out);
241 }
242
243 void rand_fork(void)
244 {
245     rand_fork_count++;
246 }
247
248 #ifndef FIPS_MODE
249 DEFINE_RUN_ONCE_STATIC(do_rand_init)
250 {
251 # ifndef OPENSSL_NO_ENGINE
252     rand_engine_lock = CRYPTO_THREAD_lock_new();
253     if (rand_engine_lock == NULL)
254         return 0;
255 # endif
256
257     rand_meth_lock = CRYPTO_THREAD_lock_new();
258     if (rand_meth_lock == NULL)
259         goto err;
260
261     if (!rand_pool_init())
262         goto err;
263
264     rand_inited = 1;
265     return 1;
266
267  err:
268     CRYPTO_THREAD_lock_free(rand_meth_lock);
269     rand_meth_lock = NULL;
270 # ifndef OPENSSL_NO_ENGINE
271     CRYPTO_THREAD_lock_free(rand_engine_lock);
272     rand_engine_lock = NULL;
273 # endif
274     return 0;
275 }
276
277 void rand_cleanup_int(void)
278 {
279     const RAND_METHOD *meth = default_RAND_meth;
280
281     if (!rand_inited)
282         return;
283
284     if (meth != NULL && meth->cleanup != NULL)
285         meth->cleanup();
286     RAND_set_rand_method(NULL);
287     rand_pool_cleanup();
288 # ifndef OPENSSL_NO_ENGINE
289     CRYPTO_THREAD_lock_free(rand_engine_lock);
290     rand_engine_lock = NULL;
291 # endif
292     CRYPTO_THREAD_lock_free(rand_meth_lock);
293     rand_meth_lock = NULL;
294     rand_inited = 0;
295 }
296
297 /* TODO(3.0): Do we need to handle this somehow in the FIPS module? */
298 /*
299  * RAND_close_seed_files() ensures that any seed file descriptors are
300  * closed after use.
301  */
302 void RAND_keep_random_devices_open(int keep)
303 {
304     if (RUN_ONCE(&rand_init, do_rand_init))
305         rand_pool_keep_random_devices_open(keep);
306 }
307
308 /*
309  * RAND_poll() reseeds the default RNG using random input
310  *
311  * The random input is obtained from polling various entropy
312  * sources which depend on the operating system and are
313  * configurable via the --with-rand-seed configure option.
314  */
315 int RAND_poll(void)
316 {
317     int ret = 0;
318
319     const RAND_METHOD *meth = RAND_get_rand_method();
320
321     if (meth == RAND_OpenSSL()) {
322         /* fill random pool and seed the master DRBG */
323         RAND_DRBG *drbg = RAND_DRBG_get0_master();
324
325         if (drbg == NULL)
326             return 0;
327
328         rand_drbg_lock(drbg);
329         ret = rand_drbg_restart(drbg, NULL, 0, 0);
330         rand_drbg_unlock(drbg);
331
332         return ret;
333
334     } else {
335         RAND_POOL *pool = NULL;
336
337         /* fill random pool and seed the current legacy RNG */
338         pool = rand_pool_new(RAND_DRBG_STRENGTH, 1,
339                              (RAND_DRBG_STRENGTH + 7) / 8,
340                              RAND_POOL_MAX_LENGTH);
341         if (pool == NULL)
342             return 0;
343
344         if (rand_pool_acquire_entropy(pool) == 0)
345             goto err;
346
347         if (meth->add == NULL
348             || meth->add(rand_pool_buffer(pool),
349                          rand_pool_length(pool),
350                          (rand_pool_entropy(pool) / 8.0)) == 0)
351             goto err;
352
353         ret = 1;
354
355      err:
356         rand_pool_free(pool);
357     }
358
359     return ret;
360 }
361 #endif /* FIPS_MODE */
362
363 /*
364  * Allocate memory and initialize a new random pool
365  */
366
367 RAND_POOL *rand_pool_new(int entropy_requested, int secure,
368                          size_t min_len, size_t max_len)
369 {
370     RAND_POOL *pool = OPENSSL_zalloc(sizeof(*pool));
371
372     if (pool == NULL) {
373         RANDerr(RAND_F_RAND_POOL_NEW, ERR_R_MALLOC_FAILURE);
374         return NULL;
375     }
376
377     pool->min_len = min_len;
378     pool->max_len = (max_len > RAND_POOL_MAX_LENGTH) ?
379         RAND_POOL_MAX_LENGTH : max_len;
380
381     if (secure)
382         pool->buffer = OPENSSL_secure_zalloc(pool->max_len);
383     else
384         pool->buffer = OPENSSL_zalloc(pool->max_len);
385
386     if (pool->buffer == NULL) {
387         RANDerr(RAND_F_RAND_POOL_NEW, ERR_R_MALLOC_FAILURE);
388         goto err;
389     }
390
391     pool->entropy_requested = entropy_requested;
392     pool->secure = secure;
393
394     return pool;
395
396 err:
397     OPENSSL_free(pool);
398     return NULL;
399 }
400
401 /*
402  * Attach new random pool to the given buffer
403  *
404  * This function is intended to be used only for feeding random data
405  * provided by RAND_add() and RAND_seed() into the <master> DRBG.
406  */
407 RAND_POOL *rand_pool_attach(const unsigned char *buffer, size_t len,
408                             size_t entropy)
409 {
410     RAND_POOL *pool = OPENSSL_zalloc(sizeof(*pool));
411
412     if (pool == NULL) {
413         RANDerr(RAND_F_RAND_POOL_ATTACH, ERR_R_MALLOC_FAILURE);
414         return NULL;
415     }
416
417     /*
418      * The const needs to be cast away, but attached buffers will not be
419      * modified (in contrary to allocated buffers which are zeroed and
420      * freed in the end).
421      */
422     pool->buffer = (unsigned char *) buffer;
423     pool->len = len;
424
425     pool->attached = 1;
426
427     pool->min_len = pool->max_len = pool->len;
428     pool->entropy = entropy;
429
430     return pool;
431 }
432
433 /*
434  * Free |pool|, securely erasing its buffer.
435  */
436 void rand_pool_free(RAND_POOL *pool)
437 {
438     if (pool == NULL)
439         return;
440
441     /*
442      * Although it would be advisable from a cryptographical viewpoint,
443      * we are not allowed to clear attached buffers, since they are passed
444      * to rand_pool_attach() as `const unsigned char*`.
445      * (see corresponding comment in rand_pool_attach()).
446      */
447     if (!pool->attached) {
448         if (pool->secure)
449             OPENSSL_secure_clear_free(pool->buffer, pool->max_len);
450         else
451             OPENSSL_clear_free(pool->buffer, pool->max_len);
452     }
453
454     OPENSSL_free(pool);
455 }
456
457 /*
458  * Return the |pool|'s buffer to the caller (readonly).
459  */
460 const unsigned char *rand_pool_buffer(RAND_POOL *pool)
461 {
462     return pool->buffer;
463 }
464
465 /*
466  * Return the |pool|'s entropy to the caller.
467  */
468 size_t rand_pool_entropy(RAND_POOL *pool)
469 {
470     return pool->entropy;
471 }
472
473 /*
474  * Return the |pool|'s buffer length to the caller.
475  */
476 size_t rand_pool_length(RAND_POOL *pool)
477 {
478     return pool->len;
479 }
480
481 /*
482  * Detach the |pool| buffer and return it to the caller.
483  * It's the responsibility of the caller to free the buffer
484  * using OPENSSL_secure_clear_free() or to re-attach it
485  * again to the pool using rand_pool_reattach().
486  */
487 unsigned char *rand_pool_detach(RAND_POOL *pool)
488 {
489     unsigned char *ret = pool->buffer;
490     pool->buffer = NULL;
491     pool->entropy = 0;
492     return ret;
493 }
494
495 /*
496  * Re-attach the |pool| buffer. It is only allowed to pass
497  * the |buffer| which was previously detached from the same pool.
498  */
499 void rand_pool_reattach(RAND_POOL *pool, unsigned char *buffer)
500 {
501     pool->buffer = buffer;
502     OPENSSL_cleanse(pool->buffer, pool->len);
503     pool->len = 0;
504 }
505
506 /*
507  * If |entropy_factor| bits contain 1 bit of entropy, how many bytes does one
508  * need to obtain at least |bits| bits of entropy?
509  */
510 #define ENTROPY_TO_BYTES(bits, entropy_factor) \
511     (((bits) * (entropy_factor) + 7) / 8)
512
513
514 /*
515  * Checks whether the |pool|'s entropy is available to the caller.
516  * This is the case when entropy count and buffer length are high enough.
517  * Returns
518  *
519  *  |entropy|  if the entropy count and buffer size is large enough
520  *      0      otherwise
521  */
522 size_t rand_pool_entropy_available(RAND_POOL *pool)
523 {
524     if (pool->entropy < pool->entropy_requested)
525         return 0;
526
527     if (pool->len < pool->min_len)
528         return 0;
529
530     return pool->entropy;
531 }
532
533 /*
534  * Returns the (remaining) amount of entropy needed to fill
535  * the random pool.
536  */
537
538 size_t rand_pool_entropy_needed(RAND_POOL *pool)
539 {
540     if (pool->entropy < pool->entropy_requested)
541         return pool->entropy_requested - pool->entropy;
542
543     return 0;
544 }
545
546 /*
547  * Returns the number of bytes needed to fill the pool, assuming
548  * the input has 1 / |entropy_factor| entropy bits per data bit.
549  * In case of an error, 0 is returned.
550  */
551
552 size_t rand_pool_bytes_needed(RAND_POOL *pool, unsigned int entropy_factor)
553 {
554     size_t bytes_needed;
555     size_t entropy_needed = rand_pool_entropy_needed(pool);
556
557     if (entropy_factor < 1) {
558         RANDerr(RAND_F_RAND_POOL_BYTES_NEEDED, RAND_R_ARGUMENT_OUT_OF_RANGE);
559         return 0;
560     }
561
562     bytes_needed = ENTROPY_TO_BYTES(entropy_needed, entropy_factor);
563
564     if (bytes_needed > pool->max_len - pool->len) {
565         /* not enough space left */
566         RANDerr(RAND_F_RAND_POOL_BYTES_NEEDED, RAND_R_RANDOM_POOL_OVERFLOW);
567         return 0;
568     }
569
570     if (pool->len < pool->min_len &&
571         bytes_needed < pool->min_len - pool->len)
572         /* to meet the min_len requirement */
573         bytes_needed = pool->min_len - pool->len;
574
575     return bytes_needed;
576 }
577
578 /* Returns the remaining number of bytes available */
579 size_t rand_pool_bytes_remaining(RAND_POOL *pool)
580 {
581     return pool->max_len - pool->len;
582 }
583
584 /*
585  * Add random bytes to the random pool.
586  *
587  * It is expected that the |buffer| contains |len| bytes of
588  * random input which contains at least |entropy| bits of
589  * randomness.
590  *
591  * Returns 1 if the added amount is adequate, otherwise 0
592  */
593 int rand_pool_add(RAND_POOL *pool,
594                   const unsigned char *buffer, size_t len, size_t entropy)
595 {
596     if (len > pool->max_len - pool->len) {
597         RANDerr(RAND_F_RAND_POOL_ADD, RAND_R_ENTROPY_INPUT_TOO_LONG);
598         return 0;
599     }
600
601     if (pool->buffer == NULL) {
602         RANDerr(RAND_F_RAND_POOL_ADD, ERR_R_INTERNAL_ERROR);
603         return 0;
604     }
605
606     if (len > 0) {
607         memcpy(pool->buffer + pool->len, buffer, len);
608         pool->len += len;
609         pool->entropy += entropy;
610     }
611
612     return 1;
613 }
614
615 /*
616  * Start to add random bytes to the random pool in-place.
617  *
618  * Reserves the next |len| bytes for adding random bytes in-place
619  * and returns a pointer to the buffer.
620  * The caller is allowed to copy up to |len| bytes into the buffer.
621  * If |len| == 0 this is considered a no-op and a NULL pointer
622  * is returned without producing an error message.
623  *
624  * After updating the buffer, rand_pool_add_end() needs to be called
625  * to finish the udpate operation (see next comment).
626  */
627 unsigned char *rand_pool_add_begin(RAND_POOL *pool, size_t len)
628 {
629     if (len == 0)
630         return NULL;
631
632     if (len > pool->max_len - pool->len) {
633         RANDerr(RAND_F_RAND_POOL_ADD_BEGIN, RAND_R_RANDOM_POOL_OVERFLOW);
634         return NULL;
635     }
636
637     if (pool->buffer == NULL) {
638         RANDerr(RAND_F_RAND_POOL_ADD_BEGIN, ERR_R_INTERNAL_ERROR);
639         return NULL;
640     }
641
642     return pool->buffer + pool->len;
643 }
644
645 /*
646  * Finish to add random bytes to the random pool in-place.
647  *
648  * Finishes an in-place update of the random pool started by
649  * rand_pool_add_begin() (see previous comment).
650  * It is expected that |len| bytes of random input have been added
651  * to the buffer which contain at least |entropy| bits of randomness.
652  * It is allowed to add less bytes than originally reserved.
653  */
654 int rand_pool_add_end(RAND_POOL *pool, size_t len, size_t entropy)
655 {
656     if (len > pool->max_len - pool->len) {
657         RANDerr(RAND_F_RAND_POOL_ADD_END, RAND_R_RANDOM_POOL_OVERFLOW);
658         return 0;
659     }
660
661     if (len > 0) {
662         pool->len += len;
663         pool->entropy += entropy;
664     }
665
666     return 1;
667 }
668
669 #ifndef FIPS_MODE
670 int RAND_set_rand_method(const RAND_METHOD *meth)
671 {
672     if (!RUN_ONCE(&rand_init, do_rand_init))
673         return 0;
674
675     CRYPTO_THREAD_write_lock(rand_meth_lock);
676 # ifndef OPENSSL_NO_ENGINE
677     ENGINE_finish(funct_ref);
678     funct_ref = NULL;
679 # endif
680     default_RAND_meth = meth;
681     CRYPTO_THREAD_unlock(rand_meth_lock);
682     return 1;
683 }
684 #endif
685
686 const RAND_METHOD *RAND_get_rand_method(void)
687 {
688 #ifdef FIPS_MODE
689     return NULL;
690 #else
691     const RAND_METHOD *tmp_meth = NULL;
692
693     if (!RUN_ONCE(&rand_init, do_rand_init))
694         return NULL;
695
696     CRYPTO_THREAD_write_lock(rand_meth_lock);
697     if (default_RAND_meth == NULL) {
698 # ifndef OPENSSL_NO_ENGINE
699         ENGINE *e;
700
701         /* If we have an engine that can do RAND, use it. */
702         if ((e = ENGINE_get_default_RAND()) != NULL
703                 && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
704             funct_ref = e;
705             default_RAND_meth = tmp_meth;
706         } else {
707             ENGINE_finish(e);
708             default_RAND_meth = &rand_meth;
709         }
710 # else
711         default_RAND_meth = &rand_meth;
712 # endif
713     }
714     tmp_meth = default_RAND_meth;
715     CRYPTO_THREAD_unlock(rand_meth_lock);
716     return tmp_meth;
717 #endif
718 }
719
720 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
721 int RAND_set_rand_engine(ENGINE *engine)
722 {
723     const RAND_METHOD *tmp_meth = NULL;
724
725     if (!RUN_ONCE(&rand_init, do_rand_init))
726         return 0;
727
728     if (engine != NULL) {
729         if (!ENGINE_init(engine))
730             return 0;
731         tmp_meth = ENGINE_get_RAND(engine);
732         if (tmp_meth == NULL) {
733             ENGINE_finish(engine);
734             return 0;
735         }
736     }
737     CRYPTO_THREAD_write_lock(rand_engine_lock);
738     /* This function releases any prior ENGINE so call it first */
739     RAND_set_rand_method(tmp_meth);
740     funct_ref = engine;
741     CRYPTO_THREAD_unlock(rand_engine_lock);
742     return 1;
743 }
744 #endif
745
746 void RAND_seed(const void *buf, int num)
747 {
748     const RAND_METHOD *meth = RAND_get_rand_method();
749
750     if (meth->seed != NULL)
751         meth->seed(buf, num);
752 }
753
754 void RAND_add(const void *buf, int num, double randomness)
755 {
756     const RAND_METHOD *meth = RAND_get_rand_method();
757
758     if (meth->add != NULL)
759         meth->add(buf, num, randomness);
760 }
761
762 /*
763  * This function is not part of RAND_METHOD, so if we're not using
764  * the default method, then just call RAND_bytes().  Otherwise make
765  * sure we're instantiated and use the private DRBG.
766  */
767 int rand_priv_bytes_ex(OPENSSL_CTX *ctx, unsigned char *buf, int num)
768 {
769     RAND_DRBG *drbg;
770     int ret;
771     const RAND_METHOD *meth = RAND_get_rand_method();
772
773     if (meth != RAND_OpenSSL())
774         return meth->bytes(buf, num);
775
776     drbg = OPENSSL_CTX_get0_private_drbg(ctx);
777     if (drbg == NULL)
778         return 0;
779
780     ret = RAND_DRBG_bytes(drbg, buf, num);
781     return ret;
782 }
783
784 int RAND_priv_bytes(unsigned char *buf, int num)
785 {
786     return rand_priv_bytes_ex(NULL, buf, num);
787 }
788
789 int rand_bytes_ex(OPENSSL_CTX *ctx, unsigned char *buf, int num)
790 {
791     RAND_DRBG *drbg;
792     int ret;
793     const RAND_METHOD *meth = RAND_get_rand_method();
794
795     if (meth != RAND_OpenSSL()) {
796         if (meth->bytes != NULL)
797             return meth->bytes(buf, num);
798         RANDerr(RAND_F_RAND_BYTES_EX, RAND_R_FUNC_NOT_IMPLEMENTED);
799         return -1;
800     }
801
802     drbg = OPENSSL_CTX_get0_public_drbg(ctx);
803     if (drbg == NULL)
804         return 0;
805
806     ret = RAND_DRBG_bytes(drbg, buf, num);
807     return ret;
808 }
809
810 int RAND_bytes(unsigned char *buf, int num)
811 {
812     return rand_bytes_ex(NULL, buf, num);
813 }
814
815 #if !OPENSSL_API_1_1_0 && !defined(FIPS_MODE)
816 int RAND_pseudo_bytes(unsigned char *buf, int num)
817 {
818     const RAND_METHOD *meth = RAND_get_rand_method();
819
820     if (meth->pseudorand != NULL)
821         return meth->pseudorand(buf, num);
822     return -1;
823 }
824 #endif
825
826 int RAND_status(void)
827 {
828     const RAND_METHOD *meth = RAND_get_rand_method();
829
830     if (meth->status != NULL)
831         return meth->status();
832     return 0;
833 }