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