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