2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
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"
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
;
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
;
30 static int rand_inited
= 0;
31 #endif /* FIPS_MODE */
33 #ifdef OPENSSL_RAND_SEED_RDTSC
35 * IMPORTANT NOTE: It is not currently possible to use this code
36 * because we are not sure about the amount of randomness it provides.
37 * Some SP900 tests have been run, but there is internal skepticism.
38 * So for now this code is not used.
40 # error "RDTSC enabled? Should not be possible!"
43 * Acquire entropy from high-speed clock
45 * Since we get some randomness from the low-order bits of the
46 * high-speed clock, it can help.
48 * Returns the total entropy count, if it exceeds the requested
49 * entropy count. Otherwise, returns an entropy count of 0.
51 size_t rand_acquire_entropy_from_tsc(RAND_POOL
*pool
)
56 if ((OPENSSL_ia32cap_P
[0] & (1 << 4)) != 0) {
57 for (i
= 0; i
< TSC_READ_COUNT
; i
++) {
58 c
= (unsigned char)(OPENSSL_rdtsc() & 0xFF);
59 rand_pool_add(pool
, &c
, 1, 4);
62 return rand_pool_entropy_available(pool
);
66 #ifdef OPENSSL_RAND_SEED_RDCPU
67 size_t OPENSSL_ia32_rdseed_bytes(unsigned char *buf
, size_t len
);
68 size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf
, size_t len
);
71 * Acquire entropy using Intel-specific cpu instructions
73 * Uses the RDSEED instruction if available, otherwise uses
74 * RDRAND if available.
76 * For the differences between RDSEED and RDRAND, and why RDSEED
77 * is the preferred choice, see https://goo.gl/oK3KcN
79 * Returns the total entropy count, if it exceeds the requested
80 * entropy count. Otherwise, returns an entropy count of 0.
82 size_t rand_acquire_entropy_from_cpu(RAND_POOL
*pool
)
85 unsigned char *buffer
;
87 bytes_needed
= rand_pool_bytes_needed(pool
, 1 /*entropy_factor*/);
88 if (bytes_needed
> 0) {
89 buffer
= rand_pool_add_begin(pool
, bytes_needed
);
92 /* Whichever comes first, use RDSEED, RDRAND or nothing */
93 if ((OPENSSL_ia32cap_P
[2] & (1 << 18)) != 0) {
94 if (OPENSSL_ia32_rdseed_bytes(buffer
, bytes_needed
)
96 rand_pool_add_end(pool
, bytes_needed
, 8 * bytes_needed
);
98 } else if ((OPENSSL_ia32cap_P
[1] & (1 << (62 - 32))) != 0) {
99 if (OPENSSL_ia32_rdrand_bytes(buffer
, bytes_needed
)
101 rand_pool_add_end(pool
, bytes_needed
, 8 * bytes_needed
);
104 rand_pool_add_end(pool
, 0, 0);
109 return rand_pool_entropy_available(pool
);
115 * Implements the get_entropy() callback (see RAND_DRBG_set_callbacks())
117 * If the DRBG has a parent, then the required amount of entropy input
118 * is fetched using the parent's RAND_DRBG_generate().
120 * Otherwise, the entropy is polled from the system entropy sources
121 * using rand_pool_acquire_entropy().
123 * If a random pool has been added to the DRBG using RAND_add(), then
124 * its entropy will be used up first.
126 size_t rand_drbg_get_entropy(RAND_DRBG
*drbg
,
127 unsigned char **pout
,
128 int entropy
, size_t min_len
, size_t max_len
,
129 int prediction_resistance
)
132 size_t entropy_available
= 0;
135 if (drbg
->parent
!= NULL
&& drbg
->strength
> drbg
->parent
->strength
) {
137 * We currently don't support the algorithm from NIST SP 800-90C
138 * 10.1.2 to use a weaker DRBG as source
140 RANDerr(RAND_F_RAND_DRBG_GET_ENTROPY
, RAND_R_PARENT_STRENGTH_TOO_WEAK
);
144 if (drbg
->seed_pool
!= NULL
) {
145 pool
= drbg
->seed_pool
;
146 pool
->entropy_requested
= entropy
;
148 pool
= rand_pool_new(entropy
, drbg
->secure
, min_len
, max_len
);
153 if (drbg
->parent
!= NULL
) {
154 size_t bytes_needed
= rand_pool_bytes_needed(pool
, 1 /*entropy_factor*/);
155 unsigned char *buffer
= rand_pool_add_begin(pool
, bytes_needed
);
157 if (buffer
!= NULL
) {
161 * Get random data from parent. Include our address as additional input,
162 * in order to provide some additional distinction between different
163 * DRBG child instances.
164 * Our lock is already held, but we need to lock our parent before
165 * generating bits from it. (Note: taking the lock will be a no-op
166 * if locking if drbg->parent->lock == NULL.)
168 rand_drbg_lock(drbg
->parent
);
169 if (RAND_DRBG_generate(drbg
->parent
,
170 buffer
, bytes_needed
,
171 prediction_resistance
,
172 (unsigned char *)&drbg
, sizeof(drbg
)) != 0)
173 bytes
= bytes_needed
;
174 drbg
->reseed_next_counter
175 = tsan_load(&drbg
->parent
->reseed_prop_counter
);
176 rand_drbg_unlock(drbg
->parent
);
178 rand_pool_add_end(pool
, bytes
, 8 * bytes
);
179 entropy_available
= rand_pool_entropy_available(pool
);
183 /* Get entropy by polling system entropy sources. */
184 entropy_available
= rand_pool_acquire_entropy(pool
);
187 if (entropy_available
> 0) {
188 ret
= rand_pool_length(pool
);
189 *pout
= rand_pool_detach(pool
);
192 if (drbg
->seed_pool
== NULL
)
193 rand_pool_free(pool
);
198 * Implements the cleanup_entropy() callback (see RAND_DRBG_set_callbacks())
201 void rand_drbg_cleanup_entropy(RAND_DRBG
*drbg
,
202 unsigned char *out
, size_t outlen
)
204 if (drbg
->seed_pool
== NULL
) {
206 OPENSSL_secure_clear_free(out
, outlen
);
208 OPENSSL_clear_free(out
, outlen
);
213 * Generate additional data that can be used for the drbg. The data does
214 * not need to contain entropy, but it's useful if it contains at least
215 * some bits that are unpredictable.
217 * Returns 0 on failure.
219 * On success it allocates a buffer at |*pout| and returns the length of
220 * the data. The buffer should get freed using OPENSSL_secure_clear_free().
222 size_t rand_drbg_get_additional_data(RAND_POOL
*pool
, unsigned char **pout
)
226 if (rand_pool_add_additional_data(pool
) == 0)
229 ret
= rand_pool_length(pool
);
230 *pout
= rand_pool_detach(pool
);
236 void rand_drbg_cleanup_additional_data(RAND_POOL
*pool
, unsigned char *out
)
238 rand_pool_reattach(pool
, out
);
242 DEFINE_RUN_ONCE_STATIC(do_rand_init
)
244 # ifndef OPENSSL_NO_ENGINE
245 rand_engine_lock
= CRYPTO_THREAD_lock_new();
246 if (rand_engine_lock
== NULL
)
250 rand_meth_lock
= CRYPTO_THREAD_lock_new();
251 if (rand_meth_lock
== NULL
)
254 if (!rand_pool_init())
261 CRYPTO_THREAD_lock_free(rand_meth_lock
);
262 rand_meth_lock
= NULL
;
263 # ifndef OPENSSL_NO_ENGINE
264 CRYPTO_THREAD_lock_free(rand_engine_lock
);
265 rand_engine_lock
= NULL
;
270 void rand_cleanup_int(void)
272 const RAND_METHOD
*meth
= default_RAND_meth
;
277 if (meth
!= NULL
&& meth
->cleanup
!= NULL
)
279 RAND_set_rand_method(NULL
);
281 # ifndef OPENSSL_NO_ENGINE
282 CRYPTO_THREAD_lock_free(rand_engine_lock
);
283 rand_engine_lock
= NULL
;
285 CRYPTO_THREAD_lock_free(rand_meth_lock
);
286 rand_meth_lock
= NULL
;
290 /* TODO(3.0): Do we need to handle this somehow in the FIPS module? */
292 * RAND_close_seed_files() ensures that any seed file descriptors are
295 void RAND_keep_random_devices_open(int keep
)
297 if (RUN_ONCE(&rand_init
, do_rand_init
))
298 rand_pool_keep_random_devices_open(keep
);
302 * RAND_poll() reseeds the default RNG using random input
304 * The random input is obtained from polling various entropy
305 * sources which depend on the operating system and are
306 * configurable via the --with-rand-seed configure option.
312 const RAND_METHOD
*meth
= RAND_get_rand_method();
314 if (meth
== RAND_OpenSSL()) {
315 /* fill random pool and seed the master DRBG */
316 RAND_DRBG
*drbg
= RAND_DRBG_get0_master();
321 rand_drbg_lock(drbg
);
322 ret
= rand_drbg_restart(drbg
, NULL
, 0, 0);
323 rand_drbg_unlock(drbg
);
328 RAND_POOL
*pool
= NULL
;
330 /* fill random pool and seed the current legacy RNG */
331 pool
= rand_pool_new(RAND_DRBG_STRENGTH
, 1,
332 (RAND_DRBG_STRENGTH
+ 7) / 8,
333 RAND_POOL_MAX_LENGTH
);
337 if (rand_pool_acquire_entropy(pool
) == 0)
340 if (meth
->add
== NULL
341 || meth
->add(rand_pool_buffer(pool
),
342 rand_pool_length(pool
),
343 (rand_pool_entropy(pool
) / 8.0)) == 0)
349 rand_pool_free(pool
);
354 #endif /* FIPS_MODE */
357 * Allocate memory and initialize a new random pool
360 RAND_POOL
*rand_pool_new(int entropy_requested
, int secure
,
361 size_t min_len
, size_t max_len
)
363 RAND_POOL
*pool
= OPENSSL_zalloc(sizeof(*pool
));
364 size_t min_alloc_size
= RAND_POOL_MIN_ALLOCATION(secure
);
367 RANDerr(RAND_F_RAND_POOL_NEW
, ERR_R_MALLOC_FAILURE
);
371 pool
->min_len
= min_len
;
372 pool
->max_len
= (max_len
> RAND_POOL_MAX_LENGTH
) ?
373 RAND_POOL_MAX_LENGTH
: max_len
;
374 pool
->alloc_len
= min_len
< min_alloc_size
? min_alloc_size
: min_len
;
375 if (pool
->alloc_len
> pool
->max_len
)
376 pool
->alloc_len
= pool
->max_len
;
379 pool
->buffer
= OPENSSL_secure_zalloc(pool
->alloc_len
);
381 pool
->buffer
= OPENSSL_zalloc(pool
->alloc_len
);
383 if (pool
->buffer
== NULL
) {
384 RANDerr(RAND_F_RAND_POOL_NEW
, ERR_R_MALLOC_FAILURE
);
388 pool
->entropy_requested
= entropy_requested
;
389 pool
->secure
= secure
;
399 * Attach new random pool to the given buffer
401 * This function is intended to be used only for feeding random data
402 * provided by RAND_add() and RAND_seed() into the <master> DRBG.
404 RAND_POOL
*rand_pool_attach(const unsigned char *buffer
, size_t len
,
407 RAND_POOL
*pool
= OPENSSL_zalloc(sizeof(*pool
));
410 RANDerr(RAND_F_RAND_POOL_ATTACH
, ERR_R_MALLOC_FAILURE
);
415 * The const needs to be cast away, but attached buffers will not be
416 * modified (in contrary to allocated buffers which are zeroed and
419 pool
->buffer
= (unsigned char *) buffer
;
424 pool
->min_len
= pool
->max_len
= pool
->alloc_len
= pool
->len
;
425 pool
->entropy
= entropy
;
431 * Free |pool|, securely erasing its buffer.
433 void rand_pool_free(RAND_POOL
*pool
)
439 * Although it would be advisable from a cryptographical viewpoint,
440 * we are not allowed to clear attached buffers, since they are passed
441 * to rand_pool_attach() as `const unsigned char*`.
442 * (see corresponding comment in rand_pool_attach()).
444 if (!pool
->attached
) {
446 OPENSSL_secure_clear_free(pool
->buffer
, pool
->alloc_len
);
448 OPENSSL_clear_free(pool
->buffer
, pool
->alloc_len
);
455 * Return the |pool|'s buffer to the caller (readonly).
457 const unsigned char *rand_pool_buffer(RAND_POOL
*pool
)
463 * Return the |pool|'s entropy to the caller.
465 size_t rand_pool_entropy(RAND_POOL
*pool
)
467 return pool
->entropy
;
471 * Return the |pool|'s buffer length to the caller.
473 size_t rand_pool_length(RAND_POOL
*pool
)
479 * Detach the |pool| buffer and return it to the caller.
480 * It's the responsibility of the caller to free the buffer
481 * using OPENSSL_secure_clear_free() or to re-attach it
482 * again to the pool using rand_pool_reattach().
484 unsigned char *rand_pool_detach(RAND_POOL
*pool
)
486 unsigned char *ret
= pool
->buffer
;
493 * Re-attach the |pool| buffer. It is only allowed to pass
494 * the |buffer| which was previously detached from the same pool.
496 void rand_pool_reattach(RAND_POOL
*pool
, unsigned char *buffer
)
498 pool
->buffer
= buffer
;
499 OPENSSL_cleanse(pool
->buffer
, pool
->len
);
504 * If |entropy_factor| bits contain 1 bit of entropy, how many bytes does one
505 * need to obtain at least |bits| bits of entropy?
507 #define ENTROPY_TO_BYTES(bits, entropy_factor) \
508 (((bits) * (entropy_factor) + 7) / 8)
512 * Checks whether the |pool|'s entropy is available to the caller.
513 * This is the case when entropy count and buffer length are high enough.
516 * |entropy| if the entropy count and buffer size is large enough
519 size_t rand_pool_entropy_available(RAND_POOL
*pool
)
521 if (pool
->entropy
< pool
->entropy_requested
)
524 if (pool
->len
< pool
->min_len
)
527 return pool
->entropy
;
531 * Returns the (remaining) amount of entropy needed to fill
535 size_t rand_pool_entropy_needed(RAND_POOL
*pool
)
537 if (pool
->entropy
< pool
->entropy_requested
)
538 return pool
->entropy_requested
- pool
->entropy
;
543 /* Increase the allocation size -- not usable for an attached pool */
544 static int rand_pool_grow(RAND_POOL
*pool
, size_t len
)
546 if (len
> pool
->alloc_len
- pool
->len
) {
548 const size_t limit
= pool
->max_len
/ 2;
549 size_t newlen
= pool
->alloc_len
;
551 if (pool
->attached
|| len
> pool
->max_len
- pool
->len
) {
552 RANDerr(RAND_F_RAND_POOL_GROW
, ERR_R_INTERNAL_ERROR
);
557 newlen
= newlen
< limit
? newlen
* 2 : pool
->max_len
;
558 while (len
> newlen
- pool
->len
);
561 p
= OPENSSL_secure_zalloc(newlen
);
563 p
= OPENSSL_zalloc(newlen
);
565 RANDerr(RAND_F_RAND_POOL_GROW
, ERR_R_MALLOC_FAILURE
);
568 memcpy(p
, pool
->buffer
, pool
->len
);
570 OPENSSL_secure_clear_free(pool
->buffer
, pool
->alloc_len
);
572 OPENSSL_clear_free(pool
->buffer
, pool
->alloc_len
);
574 pool
->alloc_len
= newlen
;
580 * Returns the number of bytes needed to fill the pool, assuming
581 * the input has 1 / |entropy_factor| entropy bits per data bit.
582 * In case of an error, 0 is returned.
585 size_t rand_pool_bytes_needed(RAND_POOL
*pool
, unsigned int entropy_factor
)
588 size_t entropy_needed
= rand_pool_entropy_needed(pool
);
590 if (entropy_factor
< 1) {
591 RANDerr(RAND_F_RAND_POOL_BYTES_NEEDED
, RAND_R_ARGUMENT_OUT_OF_RANGE
);
595 bytes_needed
= ENTROPY_TO_BYTES(entropy_needed
, entropy_factor
);
597 if (bytes_needed
> pool
->max_len
- pool
->len
) {
598 /* not enough space left */
599 RANDerr(RAND_F_RAND_POOL_BYTES_NEEDED
, RAND_R_RANDOM_POOL_OVERFLOW
);
603 if (pool
->len
< pool
->min_len
&&
604 bytes_needed
< pool
->min_len
- pool
->len
)
605 /* to meet the min_len requirement */
606 bytes_needed
= pool
->min_len
- pool
->len
;
609 * Make sure the buffer is large enough for the requested amount
610 * of data. This guarantees that existing code patterns where
611 * rand_pool_add_begin, rand_pool_add_end or rand_pool_add
612 * are used to collect entropy data without any error handling
613 * whatsoever, continue to be valid.
614 * Furthermore if the allocation here fails once, make sure that
615 * we don't fall back to a less secure or even blocking random source,
616 * as that could happen by the existing code patterns.
617 * This is not a concern for additional data, therefore that
618 * is not needed if rand_pool_grow fails in other places.
620 if (!rand_pool_grow(pool
, bytes_needed
)) {
621 /* persistent error for this pool */
622 pool
->max_len
= pool
->len
= 0;
629 /* Returns the remaining number of bytes available */
630 size_t rand_pool_bytes_remaining(RAND_POOL
*pool
)
632 return pool
->max_len
- pool
->len
;
636 * Add random bytes to the random pool.
638 * It is expected that the |buffer| contains |len| bytes of
639 * random input which contains at least |entropy| bits of
642 * Returns 1 if the added amount is adequate, otherwise 0
644 int rand_pool_add(RAND_POOL
*pool
,
645 const unsigned char *buffer
, size_t len
, size_t entropy
)
647 if (len
> pool
->max_len
- pool
->len
) {
648 RANDerr(RAND_F_RAND_POOL_ADD
, RAND_R_ENTROPY_INPUT_TOO_LONG
);
652 if (pool
->buffer
== NULL
) {
653 RANDerr(RAND_F_RAND_POOL_ADD
, ERR_R_INTERNAL_ERROR
);
659 * This is to protect us from accidentally passing the buffer
660 * returned from rand_pool_add_begin.
661 * The check for alloc_len makes sure we do not compare the
662 * address of the end of the allocated memory to something
663 * different, since that comparison would have an
664 * indeterminate result.
666 if (pool
->alloc_len
> pool
->len
&& pool
->buffer
+ pool
->len
== buffer
) {
667 RANDerr(RAND_F_RAND_POOL_ADD
, ERR_R_INTERNAL_ERROR
);
671 * We have that only for cases when a pool is used to collect
673 * For entropy data, as long as the allocation request stays within
674 * the limits given by rand_pool_bytes_needed this rand_pool_grow
675 * below is guaranteed to succeed, thus no allocation happens.
677 if (!rand_pool_grow(pool
, len
))
679 memcpy(pool
->buffer
+ pool
->len
, buffer
, len
);
681 pool
->entropy
+= entropy
;
688 * Start to add random bytes to the random pool in-place.
690 * Reserves the next |len| bytes for adding random bytes in-place
691 * and returns a pointer to the buffer.
692 * The caller is allowed to copy up to |len| bytes into the buffer.
693 * If |len| == 0 this is considered a no-op and a NULL pointer
694 * is returned without producing an error message.
696 * After updating the buffer, rand_pool_add_end() needs to be called
697 * to finish the udpate operation (see next comment).
699 unsigned char *rand_pool_add_begin(RAND_POOL
*pool
, size_t len
)
704 if (len
> pool
->max_len
- pool
->len
) {
705 RANDerr(RAND_F_RAND_POOL_ADD_BEGIN
, RAND_R_RANDOM_POOL_OVERFLOW
);
709 if (pool
->buffer
== NULL
) {
710 RANDerr(RAND_F_RAND_POOL_ADD_BEGIN
, ERR_R_INTERNAL_ERROR
);
715 * As long as the allocation request stays within the limits given
716 * by rand_pool_bytes_needed this rand_pool_grow below is guaranteed
717 * to succeed, thus no allocation happens.
718 * We have that only for cases when a pool is used to collect
719 * additional data. Then the buffer might need to grow here,
720 * and of course the caller is responsible to check the return
721 * value of this function.
723 if (!rand_pool_grow(pool
, len
))
726 return pool
->buffer
+ pool
->len
;
730 * Finish to add random bytes to the random pool in-place.
732 * Finishes an in-place update of the random pool started by
733 * rand_pool_add_begin() (see previous comment).
734 * It is expected that |len| bytes of random input have been added
735 * to the buffer which contain at least |entropy| bits of randomness.
736 * It is allowed to add less bytes than originally reserved.
738 int rand_pool_add_end(RAND_POOL
*pool
, size_t len
, size_t entropy
)
740 if (len
> pool
->alloc_len
- pool
->len
) {
741 RANDerr(RAND_F_RAND_POOL_ADD_END
, RAND_R_RANDOM_POOL_OVERFLOW
);
747 pool
->entropy
+= entropy
;
754 int RAND_set_rand_method(const RAND_METHOD
*meth
)
756 if (!RUN_ONCE(&rand_init
, do_rand_init
))
759 CRYPTO_THREAD_write_lock(rand_meth_lock
);
760 # ifndef OPENSSL_NO_ENGINE
761 ENGINE_finish(funct_ref
);
764 default_RAND_meth
= meth
;
765 CRYPTO_THREAD_unlock(rand_meth_lock
);
770 const RAND_METHOD
*RAND_get_rand_method(void)
775 const RAND_METHOD
*tmp_meth
= NULL
;
777 if (!RUN_ONCE(&rand_init
, do_rand_init
))
780 CRYPTO_THREAD_write_lock(rand_meth_lock
);
781 if (default_RAND_meth
== NULL
) {
782 # ifndef OPENSSL_NO_ENGINE
785 /* If we have an engine that can do RAND, use it. */
786 if ((e
= ENGINE_get_default_RAND()) != NULL
787 && (tmp_meth
= ENGINE_get_RAND(e
)) != NULL
) {
789 default_RAND_meth
= tmp_meth
;
792 default_RAND_meth
= &rand_meth
;
795 default_RAND_meth
= &rand_meth
;
798 tmp_meth
= default_RAND_meth
;
799 CRYPTO_THREAD_unlock(rand_meth_lock
);
804 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
805 int RAND_set_rand_engine(ENGINE
*engine
)
807 const RAND_METHOD
*tmp_meth
= NULL
;
809 if (!RUN_ONCE(&rand_init
, do_rand_init
))
812 if (engine
!= NULL
) {
813 if (!ENGINE_init(engine
))
815 tmp_meth
= ENGINE_get_RAND(engine
);
816 if (tmp_meth
== NULL
) {
817 ENGINE_finish(engine
);
821 CRYPTO_THREAD_write_lock(rand_engine_lock
);
822 /* This function releases any prior ENGINE so call it first */
823 RAND_set_rand_method(tmp_meth
);
825 CRYPTO_THREAD_unlock(rand_engine_lock
);
830 void RAND_seed(const void *buf
, int num
)
832 const RAND_METHOD
*meth
= RAND_get_rand_method();
834 if (meth
->seed
!= NULL
)
835 meth
->seed(buf
, num
);
838 void RAND_add(const void *buf
, int num
, double randomness
)
840 const RAND_METHOD
*meth
= RAND_get_rand_method();
842 if (meth
->add
!= NULL
)
843 meth
->add(buf
, num
, randomness
);
847 * This function is not part of RAND_METHOD, so if we're not using
848 * the default method, then just call RAND_bytes(). Otherwise make
849 * sure we're instantiated and use the private DRBG.
851 int rand_priv_bytes_ex(OPENSSL_CTX
*ctx
, unsigned char *buf
, int num
)
855 const RAND_METHOD
*meth
= RAND_get_rand_method();
857 if (meth
!= RAND_OpenSSL())
858 return meth
->bytes(buf
, num
);
860 drbg
= OPENSSL_CTX_get0_private_drbg(ctx
);
864 ret
= RAND_DRBG_bytes(drbg
, buf
, num
);
868 int RAND_priv_bytes(unsigned char *buf
, int num
)
870 return rand_priv_bytes_ex(NULL
, buf
, num
);
873 int rand_bytes_ex(OPENSSL_CTX
*ctx
, unsigned char *buf
, int num
)
877 const RAND_METHOD
*meth
= RAND_get_rand_method();
879 if (meth
!= RAND_OpenSSL()) {
880 if (meth
->bytes
!= NULL
)
881 return meth
->bytes(buf
, num
);
882 RANDerr(RAND_F_RAND_BYTES_EX
, RAND_R_FUNC_NOT_IMPLEMENTED
);
886 drbg
= OPENSSL_CTX_get0_public_drbg(ctx
);
890 ret
= RAND_DRBG_bytes(drbg
, buf
, num
);
894 int RAND_bytes(unsigned char *buf
, int num
)
896 return rand_bytes_ex(NULL
, buf
, num
);
899 #if !OPENSSL_API_1_1_0 && !defined(FIPS_MODE)
900 int RAND_pseudo_bytes(unsigned char *buf
, int num
)
902 const RAND_METHOD
*meth
= RAND_get_rand_method();
904 if (meth
->pseudorand
!= NULL
)
905 return meth
->pseudorand(buf
, num
);
910 int RAND_status(void)
912 const RAND_METHOD
*meth
= RAND_get_rand_method();
914 if (meth
->status
!= NULL
)
915 return meth
->status();