2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "internal/cryptlib.h"
13 #include <openssl/opensslconf.h>
14 #include "crypto/rand.h"
15 #include <openssl/engine.h>
16 #include "internal/thread_once.h"
17 #include "rand_local.h"
20 #ifndef OPENSSL_NO_ENGINE
21 /* non-NULL if default_RAND_meth is ENGINE-provided */
22 static ENGINE *funct_ref;
23 static CRYPTO_RWLOCK *rand_engine_lock;
25 static CRYPTO_RWLOCK *rand_meth_lock;
26 static const RAND_METHOD *default_RAND_meth;
27 static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT;
29 static CRYPTO_RWLOCK *rand_nonce_lock;
30 static int rand_nonce_count;
32 static int rand_inited = 0;
34 #ifdef OPENSSL_RAND_SEED_RDTSC
36 * IMPORTANT NOTE: It is not currently possible to use this code
37 * because we are not sure about the amount of randomness it provides.
38 * Some SP900 tests have been run, but there is internal skepticism.
39 * So for now this code is not used.
41 # error "RDTSC enabled? Should not be possible!"
44 * Acquire entropy from high-speed clock
46 * Since we get some randomness from the low-order bits of the
47 * high-speed clock, it can help.
49 * Returns the total entropy count, if it exceeds the requested
50 * entropy count. Otherwise, returns an entropy count of 0.
52 size_t rand_acquire_entropy_from_tsc(RAND_POOL *pool)
57 if ((OPENSSL_ia32cap_P[0] & (1 << 4)) != 0) {
58 for (i = 0; i < TSC_READ_COUNT; i++) {
59 c = (unsigned char)(OPENSSL_rdtsc() & 0xFF);
60 rand_pool_add(pool, &c, 1, 4);
63 return rand_pool_entropy_available(pool);
67 #ifdef OPENSSL_RAND_SEED_RDCPU
68 size_t OPENSSL_ia32_rdseed_bytes(unsigned char *buf, size_t len);
69 size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len);
71 extern unsigned int OPENSSL_ia32cap_P[];
74 * Acquire entropy using Intel-specific cpu instructions
76 * Uses the RDSEED instruction if available, otherwise uses
77 * RDRAND if available.
79 * For the differences between RDSEED and RDRAND, and why RDSEED
80 * is the preferred choice, see https://goo.gl/oK3KcN
82 * Returns the total entropy count, if it exceeds the requested
83 * entropy count. Otherwise, returns an entropy count of 0.
85 size_t rand_acquire_entropy_from_cpu(RAND_POOL *pool)
88 unsigned char *buffer;
90 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
91 if (bytes_needed > 0) {
92 buffer = rand_pool_add_begin(pool, bytes_needed);
95 /* Whichever comes first, use RDSEED, RDRAND or nothing */
96 if ((OPENSSL_ia32cap_P[2] & (1 << 18)) != 0) {
97 if (OPENSSL_ia32_rdseed_bytes(buffer, bytes_needed)
99 rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
101 } else if ((OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) != 0) {
102 if (OPENSSL_ia32_rdrand_bytes(buffer, bytes_needed)
104 rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
107 rand_pool_add_end(pool, 0, 0);
112 return rand_pool_entropy_available(pool);
118 * Implements the get_entropy() callback (see RAND_DRBG_set_callbacks())
120 * If the DRBG has a parent, then the required amount of entropy input
121 * is fetched using the parent's RAND_DRBG_generate().
123 * Otherwise, the entropy is polled from the system entropy sources
124 * using rand_pool_acquire_entropy().
126 * If a random pool has been added to the DRBG using RAND_add(), then
127 * its entropy will be used up first.
129 size_t rand_drbg_get_entropy(RAND_DRBG *drbg,
130 unsigned char **pout,
131 int entropy, size_t min_len, size_t max_len,
132 int prediction_resistance)
135 size_t entropy_available = 0;
138 if (drbg->parent != NULL && drbg->strength > drbg->parent->strength) {
140 * We currently don't support the algorithm from NIST SP 800-90C
141 * 10.1.2 to use a weaker DRBG as source
143 RANDerr(RAND_F_RAND_DRBG_GET_ENTROPY, RAND_R_PARENT_STRENGTH_TOO_WEAK);
147 if (drbg->seed_pool != NULL) {
148 pool = drbg->seed_pool;
149 pool->entropy_requested = entropy;
151 pool = rand_pool_new(entropy, drbg->secure, min_len, max_len);
156 if (drbg->parent != NULL) {
157 size_t bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
158 unsigned char *buffer = rand_pool_add_begin(pool, bytes_needed);
160 if (buffer != NULL) {
164 * Get random data from parent. Include our address as additional input,
165 * in order to provide some additional distinction between different
166 * DRBG child instances.
167 * Our lock is already held, but we need to lock our parent before
168 * generating bits from it. (Note: taking the lock will be a no-op
169 * if locking if drbg->parent->lock == NULL.)
171 rand_drbg_lock(drbg->parent);
172 if (RAND_DRBG_generate(drbg->parent,
173 buffer, bytes_needed,
174 prediction_resistance,
175 (unsigned char *)&drbg, sizeof(drbg)) != 0)
176 bytes = bytes_needed;
177 rand_drbg_unlock(drbg->parent);
179 rand_pool_add_end(pool, bytes, 8 * bytes);
180 entropy_available = rand_pool_entropy_available(pool);
184 if (prediction_resistance) {
186 * We don't have any entropy sources that comply with the NIST
187 * standard to provide prediction resistance (see NIST SP 800-90C,
190 RANDerr(RAND_F_RAND_DRBG_GET_ENTROPY,
191 RAND_R_PREDICTION_RESISTANCE_NOT_SUPPORTED);
195 /* Get entropy by polling system entropy sources. */
196 entropy_available = rand_pool_acquire_entropy(pool);
199 if (entropy_available > 0) {
200 ret = rand_pool_length(pool);
201 *pout = rand_pool_detach(pool);
205 if (drbg->seed_pool == NULL)
206 rand_pool_free(pool);
211 * Implements the cleanup_entropy() callback (see RAND_DRBG_set_callbacks())
214 void rand_drbg_cleanup_entropy(RAND_DRBG *drbg,
215 unsigned char *out, size_t outlen)
217 if (drbg->seed_pool == NULL) {
219 OPENSSL_secure_clear_free(out, outlen);
221 OPENSSL_clear_free(out, outlen);
227 * Implements the get_nonce() callback (see RAND_DRBG_set_callbacks())
230 size_t rand_drbg_get_nonce(RAND_DRBG *drbg,
231 unsigned char **pout,
232 int entropy, size_t min_len, size_t max_len)
242 memset(&data, 0, sizeof(data));
243 pool = rand_pool_new(0, 0, min_len, max_len);
247 if (rand_pool_add_nonce_data(pool) == 0)
250 data.instance = drbg;
251 CRYPTO_atomic_add(&rand_nonce_count, 1, &data.count, rand_nonce_lock);
253 if (rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0) == 0)
256 ret = rand_pool_length(pool);
257 *pout = rand_pool_detach(pool);
260 rand_pool_free(pool);
266 * Implements the cleanup_nonce() callback (see RAND_DRBG_set_callbacks())
269 void rand_drbg_cleanup_nonce(RAND_DRBG *drbg,
270 unsigned char *out, size_t outlen)
272 OPENSSL_clear_free(out, outlen);
276 * Generate additional data that can be used for the drbg. The data does
277 * not need to contain entropy, but it's useful if it contains at least
278 * some bits that are unpredictable.
280 * Returns 0 on failure.
282 * On success it allocates a buffer at |*pout| and returns the length of
283 * the data. The buffer should get freed using OPENSSL_secure_clear_free().
285 size_t rand_drbg_get_additional_data(RAND_POOL *pool, unsigned char **pout)
289 if (rand_pool_add_additional_data(pool) == 0)
292 ret = rand_pool_length(pool);
293 *pout = rand_pool_detach(pool);
299 void rand_drbg_cleanup_additional_data(RAND_POOL *pool, unsigned char *out)
301 rand_pool_reattach(pool, out);
304 DEFINE_RUN_ONCE_STATIC(do_rand_init)
306 #ifndef OPENSSL_NO_ENGINE
307 rand_engine_lock = CRYPTO_THREAD_lock_new();
308 if (rand_engine_lock == NULL)
312 rand_meth_lock = CRYPTO_THREAD_lock_new();
313 if (rand_meth_lock == NULL)
316 rand_nonce_lock = CRYPTO_THREAD_lock_new();
317 if (rand_nonce_lock == NULL)
320 if (!rand_pool_init())
327 CRYPTO_THREAD_lock_free(rand_nonce_lock);
328 rand_nonce_lock = NULL;
330 CRYPTO_THREAD_lock_free(rand_meth_lock);
331 rand_meth_lock = NULL;
333 #ifndef OPENSSL_NO_ENGINE
334 CRYPTO_THREAD_lock_free(rand_engine_lock);
335 rand_engine_lock = NULL;
340 void rand_cleanup_int(void)
342 const RAND_METHOD *meth = default_RAND_meth;
347 if (meth != NULL && meth->cleanup != NULL)
349 RAND_set_rand_method(NULL);
351 #ifndef OPENSSL_NO_ENGINE
352 CRYPTO_THREAD_lock_free(rand_engine_lock);
353 rand_engine_lock = NULL;
355 CRYPTO_THREAD_lock_free(rand_meth_lock);
356 rand_meth_lock = NULL;
357 CRYPTO_THREAD_lock_free(rand_nonce_lock);
358 rand_nonce_lock = NULL;
363 * RAND_close_seed_files() ensures that any seed file descriptors are
366 void RAND_keep_random_devices_open(int keep)
368 if (RUN_ONCE(&rand_init, do_rand_init))
369 rand_pool_keep_random_devices_open(keep);
373 * RAND_poll() reseeds the default RNG using random input
375 * The random input is obtained from polling various entropy
376 * sources which depend on the operating system and are
377 * configurable via the --with-rand-seed configure option.
383 RAND_POOL *pool = NULL;
385 const RAND_METHOD *meth = RAND_get_rand_method();
390 if (meth == RAND_OpenSSL()) {
391 /* fill random pool and seed the master DRBG */
392 RAND_DRBG *drbg = RAND_DRBG_get0_master();
397 rand_drbg_lock(drbg);
398 ret = rand_drbg_restart(drbg, NULL, 0, 0);
399 rand_drbg_unlock(drbg);
404 /* fill random pool and seed the current legacy RNG */
405 pool = rand_pool_new(RAND_DRBG_STRENGTH, 1,
406 (RAND_DRBG_STRENGTH + 7) / 8,
407 RAND_POOL_MAX_LENGTH);
411 if (rand_pool_acquire_entropy(pool) == 0)
414 if (meth->add == NULL
415 || meth->add(rand_pool_buffer(pool),
416 rand_pool_length(pool),
417 (rand_pool_entropy(pool) / 8.0)) == 0)
424 rand_pool_free(pool);
429 * Allocate memory and initialize a new random pool
432 RAND_POOL *rand_pool_new(int entropy_requested, int secure,
433 size_t min_len, size_t max_len)
435 RAND_POOL *pool = OPENSSL_zalloc(sizeof(*pool));
436 size_t min_alloc_size = RAND_POOL_MIN_ALLOCATION(secure);
439 RANDerr(RAND_F_RAND_POOL_NEW, ERR_R_MALLOC_FAILURE);
443 pool->min_len = min_len;
444 pool->max_len = (max_len > RAND_POOL_MAX_LENGTH) ?
445 RAND_POOL_MAX_LENGTH : max_len;
446 pool->alloc_len = min_len < min_alloc_size ? min_alloc_size : min_len;
447 if (pool->alloc_len > pool->max_len)
448 pool->alloc_len = pool->max_len;
451 pool->buffer = OPENSSL_secure_zalloc(pool->alloc_len);
453 pool->buffer = OPENSSL_zalloc(pool->alloc_len);
455 if (pool->buffer == NULL) {
456 RANDerr(RAND_F_RAND_POOL_NEW, ERR_R_MALLOC_FAILURE);
460 pool->entropy_requested = entropy_requested;
461 pool->secure = secure;
471 * Attach new random pool to the given buffer
473 * This function is intended to be used only for feeding random data
474 * provided by RAND_add() and RAND_seed() into the <master> DRBG.
476 RAND_POOL *rand_pool_attach(const unsigned char *buffer, size_t len,
479 RAND_POOL *pool = OPENSSL_zalloc(sizeof(*pool));
482 RANDerr(RAND_F_RAND_POOL_ATTACH, ERR_R_MALLOC_FAILURE);
487 * The const needs to be cast away, but attached buffers will not be
488 * modified (in contrary to allocated buffers which are zeroed and
491 pool->buffer = (unsigned char *) buffer;
496 pool->min_len = pool->max_len = pool->alloc_len = pool->len;
497 pool->entropy = entropy;
503 * Free |pool|, securely erasing its buffer.
505 void rand_pool_free(RAND_POOL *pool)
511 * Although it would be advisable from a cryptographical viewpoint,
512 * we are not allowed to clear attached buffers, since they are passed
513 * to rand_pool_attach() as `const unsigned char*`.
514 * (see corresponding comment in rand_pool_attach()).
516 if (!pool->attached) {
518 OPENSSL_secure_clear_free(pool->buffer, pool->alloc_len);
520 OPENSSL_clear_free(pool->buffer, pool->alloc_len);
527 * Return the |pool|'s buffer to the caller (readonly).
529 const unsigned char *rand_pool_buffer(RAND_POOL *pool)
535 * Return the |pool|'s entropy to the caller.
537 size_t rand_pool_entropy(RAND_POOL *pool)
539 return pool->entropy;
543 * Return the |pool|'s buffer length to the caller.
545 size_t rand_pool_length(RAND_POOL *pool)
551 * Detach the |pool| buffer and return it to the caller.
552 * It's the responsibility of the caller to free the buffer
553 * using OPENSSL_secure_clear_free() or to re-attach it
554 * again to the pool using rand_pool_reattach().
556 unsigned char *rand_pool_detach(RAND_POOL *pool)
558 unsigned char *ret = pool->buffer;
565 * Re-attach the |pool| buffer. It is only allowed to pass
566 * the |buffer| which was previously detached from the same pool.
568 void rand_pool_reattach(RAND_POOL *pool, unsigned char *buffer)
570 pool->buffer = buffer;
571 OPENSSL_cleanse(pool->buffer, pool->len);
576 * If |entropy_factor| bits contain 1 bit of entropy, how many bytes does one
577 * need to obtain at least |bits| bits of entropy?
579 #define ENTROPY_TO_BYTES(bits, entropy_factor) \
580 (((bits) * (entropy_factor) + 7) / 8)
584 * Checks whether the |pool|'s entropy is available to the caller.
585 * This is the case when entropy count and buffer length are high enough.
588 * |entropy| if the entropy count and buffer size is large enough
591 size_t rand_pool_entropy_available(RAND_POOL *pool)
593 if (pool->entropy < pool->entropy_requested)
596 if (pool->len < pool->min_len)
599 return pool->entropy;
603 * Returns the (remaining) amount of entropy needed to fill
607 size_t rand_pool_entropy_needed(RAND_POOL *pool)
609 if (pool->entropy < pool->entropy_requested)
610 return pool->entropy_requested - pool->entropy;
615 /* Increase the allocation size -- not usable for an attached pool */
616 static int rand_pool_grow(RAND_POOL *pool, size_t len)
618 if (len > pool->alloc_len - pool->len) {
620 const size_t limit = pool->max_len / 2;
621 size_t newlen = pool->alloc_len;
623 if (pool->attached || len > pool->max_len - pool->len) {
624 RANDerr(RAND_F_RAND_POOL_GROW, ERR_R_INTERNAL_ERROR);
629 newlen = newlen < limit ? newlen * 2 : pool->max_len;
630 while (len > newlen - pool->len);
633 p = OPENSSL_secure_zalloc(newlen);
635 p = OPENSSL_zalloc(newlen);
637 RANDerr(RAND_F_RAND_POOL_GROW, ERR_R_MALLOC_FAILURE);
640 memcpy(p, pool->buffer, pool->len);
642 OPENSSL_secure_clear_free(pool->buffer, pool->alloc_len);
644 OPENSSL_clear_free(pool->buffer, pool->alloc_len);
646 pool->alloc_len = newlen;
652 * Returns the number of bytes needed to fill the pool, assuming
653 * the input has 1 / |entropy_factor| entropy bits per data bit.
654 * In case of an error, 0 is returned.
657 size_t rand_pool_bytes_needed(RAND_POOL *pool, unsigned int entropy_factor)
660 size_t entropy_needed = rand_pool_entropy_needed(pool);
662 if (entropy_factor < 1) {
663 RANDerr(RAND_F_RAND_POOL_BYTES_NEEDED, RAND_R_ARGUMENT_OUT_OF_RANGE);
667 bytes_needed = ENTROPY_TO_BYTES(entropy_needed, entropy_factor);
669 if (bytes_needed > pool->max_len - pool->len) {
670 /* not enough space left */
671 RANDerr(RAND_F_RAND_POOL_BYTES_NEEDED, RAND_R_RANDOM_POOL_OVERFLOW);
675 if (pool->len < pool->min_len &&
676 bytes_needed < pool->min_len - pool->len)
677 /* to meet the min_len requirement */
678 bytes_needed = pool->min_len - pool->len;
681 * Make sure the buffer is large enough for the requested amount
682 * of data. This guarantees that existing code patterns where
683 * rand_pool_add_begin, rand_pool_add_end or rand_pool_add
684 * are used to collect entropy data without any error handling
685 * whatsoever, continue to be valid.
686 * Furthermore if the allocation here fails once, make sure that
687 * we don't fall back to a less secure or even blocking random source,
688 * as that could happen by the existing code patterns.
689 * This is not a concern for additional data, therefore that
690 * is not needed if rand_pool_grow fails in other places.
692 if (!rand_pool_grow(pool, bytes_needed)) {
693 /* persistent error for this pool */
694 pool->max_len = pool->len = 0;
701 /* Returns the remaining number of bytes available */
702 size_t rand_pool_bytes_remaining(RAND_POOL *pool)
704 return pool->max_len - pool->len;
708 * Add random bytes to the random pool.
710 * It is expected that the |buffer| contains |len| bytes of
711 * random input which contains at least |entropy| bits of
714 * Returns 1 if the added amount is adequate, otherwise 0
716 int rand_pool_add(RAND_POOL *pool,
717 const unsigned char *buffer, size_t len, size_t entropy)
719 if (len > pool->max_len - pool->len) {
720 RANDerr(RAND_F_RAND_POOL_ADD, RAND_R_ENTROPY_INPUT_TOO_LONG);
724 if (pool->buffer == NULL) {
725 RANDerr(RAND_F_RAND_POOL_ADD, ERR_R_INTERNAL_ERROR);
731 * This is to protect us from accidentally passing the buffer
732 * returned from rand_pool_add_begin.
733 * The check for alloc_len makes sure we do not compare the
734 * address of the end of the allocated memory to something
735 * different, since that comparison would have an
736 * indeterminate result.
738 if (pool->alloc_len > pool->len && pool->buffer + pool->len == buffer) {
739 RANDerr(RAND_F_RAND_POOL_ADD, ERR_R_INTERNAL_ERROR);
743 * We have that only for cases when a pool is used to collect
745 * For entropy data, as long as the allocation request stays within
746 * the limits given by rand_pool_bytes_needed this rand_pool_grow
747 * below is guaranteed to succeed, thus no allocation happens.
749 if (!rand_pool_grow(pool, len))
751 memcpy(pool->buffer + pool->len, buffer, len);
753 pool->entropy += entropy;
760 * Start to add random bytes to the random pool in-place.
762 * Reserves the next |len| bytes for adding random bytes in-place
763 * and returns a pointer to the buffer.
764 * The caller is allowed to copy up to |len| bytes into the buffer.
765 * If |len| == 0 this is considered a no-op and a NULL pointer
766 * is returned without producing an error message.
768 * After updating the buffer, rand_pool_add_end() needs to be called
769 * to finish the update operation (see next comment).
771 unsigned char *rand_pool_add_begin(RAND_POOL *pool, size_t len)
776 if (len > pool->max_len - pool->len) {
777 RANDerr(RAND_F_RAND_POOL_ADD_BEGIN, RAND_R_RANDOM_POOL_OVERFLOW);
781 if (pool->buffer == NULL) {
782 RANDerr(RAND_F_RAND_POOL_ADD_BEGIN, ERR_R_INTERNAL_ERROR);
787 * As long as the allocation request stays within the limits given
788 * by rand_pool_bytes_needed this rand_pool_grow below is guaranteed
789 * to succeed, thus no allocation happens.
790 * We have that only for cases when a pool is used to collect
791 * additional data. Then the buffer might need to grow here,
792 * and of course the caller is responsible to check the return
793 * value of this function.
795 if (!rand_pool_grow(pool, len))
798 return pool->buffer + pool->len;
802 * Finish to add random bytes to the random pool in-place.
804 * Finishes an in-place update of the random pool started by
805 * rand_pool_add_begin() (see previous comment).
806 * It is expected that |len| bytes of random input have been added
807 * to the buffer which contain at least |entropy| bits of randomness.
808 * It is allowed to add less bytes than originally reserved.
810 int rand_pool_add_end(RAND_POOL *pool, size_t len, size_t entropy)
812 if (len > pool->alloc_len - pool->len) {
813 RANDerr(RAND_F_RAND_POOL_ADD_END, RAND_R_RANDOM_POOL_OVERFLOW);
819 pool->entropy += entropy;
825 int RAND_set_rand_method(const RAND_METHOD *meth)
827 if (!RUN_ONCE(&rand_init, do_rand_init))
830 CRYPTO_THREAD_write_lock(rand_meth_lock);
831 #ifndef OPENSSL_NO_ENGINE
832 ENGINE_finish(funct_ref);
835 default_RAND_meth = meth;
836 CRYPTO_THREAD_unlock(rand_meth_lock);
840 const RAND_METHOD *RAND_get_rand_method(void)
842 const RAND_METHOD *tmp_meth = NULL;
844 if (!RUN_ONCE(&rand_init, do_rand_init))
847 CRYPTO_THREAD_write_lock(rand_meth_lock);
848 if (default_RAND_meth == NULL) {
849 #ifndef OPENSSL_NO_ENGINE
852 /* If we have an engine that can do RAND, use it. */
853 if ((e = ENGINE_get_default_RAND()) != NULL
854 && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
856 default_RAND_meth = tmp_meth;
859 default_RAND_meth = &rand_meth;
862 default_RAND_meth = &rand_meth;
865 tmp_meth = default_RAND_meth;
866 CRYPTO_THREAD_unlock(rand_meth_lock);
870 #ifndef OPENSSL_NO_ENGINE
871 int RAND_set_rand_engine(ENGINE *engine)
873 const RAND_METHOD *tmp_meth = NULL;
875 if (!RUN_ONCE(&rand_init, do_rand_init))
878 if (engine != NULL) {
879 if (!ENGINE_init(engine))
881 tmp_meth = ENGINE_get_RAND(engine);
882 if (tmp_meth == NULL) {
883 ENGINE_finish(engine);
887 CRYPTO_THREAD_write_lock(rand_engine_lock);
888 /* This function releases any prior ENGINE so call it first */
889 RAND_set_rand_method(tmp_meth);
891 CRYPTO_THREAD_unlock(rand_engine_lock);
896 void RAND_seed(const void *buf, int num)
898 const RAND_METHOD *meth = RAND_get_rand_method();
900 if (meth != NULL && meth->seed != NULL)
901 meth->seed(buf, num);
904 void RAND_add(const void *buf, int num, double randomness)
906 const RAND_METHOD *meth = RAND_get_rand_method();
908 if (meth != NULL && meth->add != NULL)
909 meth->add(buf, num, randomness);
913 * This function is not part of RAND_METHOD, so if we're not using
914 * the default method, then just call RAND_bytes(). Otherwise make
915 * sure we're instantiated and use the private DRBG.
917 int RAND_priv_bytes(unsigned char *buf, int num)
919 const RAND_METHOD *meth = RAND_get_rand_method();
922 if (meth != NULL && meth != RAND_OpenSSL())
923 return RAND_bytes(buf, num);
925 drbg = RAND_DRBG_get0_private();
927 return RAND_DRBG_bytes(drbg, buf, num);
932 int RAND_bytes(unsigned char *buf, int num)
934 const RAND_METHOD *meth = RAND_get_rand_method();
936 if (meth != NULL && meth->bytes != NULL)
937 return meth->bytes(buf, num);
938 RANDerr(RAND_F_RAND_BYTES, RAND_R_FUNC_NOT_IMPLEMENTED);
942 #if OPENSSL_API_COMPAT < 0x10100000L
943 int RAND_pseudo_bytes(unsigned char *buf, int num)
945 const RAND_METHOD *meth = RAND_get_rand_method();
947 if (meth != NULL && meth->pseudorand != NULL)
948 return meth->pseudorand(buf, num);
949 RANDerr(RAND_F_RAND_PSEUDO_BYTES, RAND_R_FUNC_NOT_IMPLEMENTED);
954 int RAND_status(void)
956 const RAND_METHOD *meth = RAND_get_rand_method();
958 if (meth != NULL && meth->status != NULL)
959 return meth->status();