2 * Copyright 1995-2016 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 "internal/rand.h"
16 #include <openssl/engine.h>
19 # include <openssl/fips.h>
20 # include <openssl/fips_rand.h>
23 #ifndef OPENSSL_NO_ENGINE
24 /* non-NULL if default_RAND_meth is ENGINE-provided */
25 static ENGINE *funct_ref = NULL;
27 static const RAND_METHOD *default_RAND_meth = NULL;
29 int RAND_set_rand_method(const RAND_METHOD *meth)
31 #ifndef OPENSSL_NO_ENGINE
32 ENGINE_finish(funct_ref);
35 default_RAND_meth = meth;
39 const RAND_METHOD *RAND_get_rand_method(void)
41 if (!default_RAND_meth) {
42 #ifndef OPENSSL_NO_ENGINE
43 ENGINE *e = ENGINE_get_default_RAND();
45 default_RAND_meth = ENGINE_get_RAND(e);
46 if (default_RAND_meth == NULL) {
55 default_RAND_meth = RAND_OpenSSL();
57 return default_RAND_meth;
60 #ifndef OPENSSL_NO_ENGINE
61 int RAND_set_rand_engine(ENGINE *engine)
63 const RAND_METHOD *tmp_meth = NULL;
65 if (!ENGINE_init(engine))
67 tmp_meth = ENGINE_get_RAND(engine);
68 if (tmp_meth == NULL) {
69 ENGINE_finish(engine);
73 /* This function releases any prior ENGINE so call it first */
74 RAND_set_rand_method(tmp_meth);
80 void rand_cleanup_int(void)
82 const RAND_METHOD *meth = RAND_get_rand_method();
83 if (meth && meth->cleanup)
85 RAND_set_rand_method(NULL);
88 void RAND_seed(const void *buf, int num)
90 const RAND_METHOD *meth = RAND_get_rand_method();
91 if (meth && meth->seed)
95 void RAND_add(const void *buf, int num, double entropy)
97 const RAND_METHOD *meth = RAND_get_rand_method();
98 if (meth && meth->add)
99 meth->add(buf, num, entropy);
102 int RAND_bytes(unsigned char *buf, int num)
104 const RAND_METHOD *meth = RAND_get_rand_method();
105 if (meth && meth->bytes)
106 return meth->bytes(buf, num);
110 #if OPENSSL_API_COMPAT < 0x10100000L
111 int RAND_pseudo_bytes(unsigned char *buf, int num)
113 const RAND_METHOD *meth = RAND_get_rand_method();
114 if (meth && meth->pseudorand)
115 return meth->pseudorand(buf, num);
120 int RAND_status(void)
122 const RAND_METHOD *meth = RAND_get_rand_method();
123 if (meth && meth->status)
124 return meth->status();