40e2464a8258369fea549ad9ecc73f5b603d16c5
[openssl.git] / crypto / rand / rand_lib.c
1 /*
2  * Copyright 1995-2016 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.h"
15 #include <openssl/engine.h>
16 #include "internal/thread_once.h"
17
18 #ifndef OPENSSL_NO_ENGINE
19 /* non-NULL if default_RAND_meth is ENGINE-provided */
20 static ENGINE *funct_ref = NULL;
21 static CRYPTO_RWLOCK *rand_engine_lock = NULL;
22 #endif
23 static const RAND_METHOD *default_RAND_meth = NULL;
24 static CRYPTO_RWLOCK *rand_meth_lock = NULL;
25 static CRYPTO_ONCE rand_lock_init = CRYPTO_ONCE_STATIC_INIT;
26
27 DEFINE_RUN_ONCE_STATIC(do_rand_lock_init)
28 {
29     int ret = 1;
30 #ifndef OPENSSL_NO_ENGINE
31     rand_engine_lock = CRYPTO_THREAD_lock_new();
32     ret &= rand_engine_lock != NULL;
33 #endif
34     rand_meth_lock = CRYPTO_THREAD_lock_new();
35     ret &= rand_meth_lock != NULL;
36     return ret;
37 }
38
39 int RAND_set_rand_method(const RAND_METHOD *meth)
40 {
41     if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
42         return 0;
43
44     CRYPTO_THREAD_write_lock(rand_meth_lock);
45 #ifndef OPENSSL_NO_ENGINE
46     ENGINE_finish(funct_ref);
47     funct_ref = NULL;
48 #endif
49     default_RAND_meth = meth;
50     CRYPTO_THREAD_unlock(rand_meth_lock);
51     return 1;
52 }
53
54 const RAND_METHOD *RAND_get_rand_method(void)
55 {
56     const RAND_METHOD *tmp_meth = NULL;
57
58     if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
59         return NULL;
60
61     CRYPTO_THREAD_write_lock(rand_meth_lock);
62     if (!default_RAND_meth) {
63 #ifndef OPENSSL_NO_ENGINE
64         ENGINE *e = ENGINE_get_default_RAND();
65         if (e) {
66             default_RAND_meth = ENGINE_get_RAND(e);
67             if (default_RAND_meth == NULL) {
68                 ENGINE_finish(e);
69                 e = NULL;
70             }
71         }
72         if (e)
73             funct_ref = e;
74         else
75 #endif
76             default_RAND_meth = RAND_OpenSSL();
77     }
78     tmp_meth = default_RAND_meth;
79     CRYPTO_THREAD_unlock(rand_meth_lock);
80     return tmp_meth;
81 }
82
83 #ifndef OPENSSL_NO_ENGINE
84 int RAND_set_rand_engine(ENGINE *engine)
85 {
86     const RAND_METHOD *tmp_meth = NULL;
87
88     if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init))
89         return 0;
90
91     if (engine) {
92         if (!ENGINE_init(engine))
93             return 0;
94         tmp_meth = ENGINE_get_RAND(engine);
95         if (tmp_meth == NULL) {
96             ENGINE_finish(engine);
97             return 0;
98         }
99     }
100     CRYPTO_THREAD_write_lock(rand_engine_lock);
101     /* This function releases any prior ENGINE so call it first */
102     RAND_set_rand_method(tmp_meth);
103     funct_ref = engine;
104     CRYPTO_THREAD_unlock(rand_engine_lock);
105     return 1;
106 }
107 #endif
108
109 void rand_cleanup_int(void)
110 {
111     const RAND_METHOD *meth = default_RAND_meth;
112     if (meth && meth->cleanup)
113         meth->cleanup();
114     RAND_set_rand_method(NULL);
115     CRYPTO_THREAD_lock_free(rand_meth_lock);
116     CRYPTO_THREAD_lock_free(rand_engine_lock);
117 }
118
119 void RAND_seed(const void *buf, int num)
120 {
121     const RAND_METHOD *meth = RAND_get_rand_method();
122     if (meth && meth->seed)
123         meth->seed(buf, num);
124 }
125
126 void RAND_add(const void *buf, int num, double entropy)
127 {
128     const RAND_METHOD *meth = RAND_get_rand_method();
129     if (meth && meth->add)
130         meth->add(buf, num, entropy);
131 }
132
133 int RAND_bytes(unsigned char *buf, int num)
134 {
135     const RAND_METHOD *meth = RAND_get_rand_method();
136     if (meth && meth->bytes)
137         return meth->bytes(buf, num);
138     return (-1);
139 }
140
141 #if OPENSSL_API_COMPAT < 0x10100000L
142 int RAND_pseudo_bytes(unsigned char *buf, int num)
143 {
144     const RAND_METHOD *meth = RAND_get_rand_method();
145     if (meth && meth->pseudorand)
146         return meth->pseudorand(buf, num);
147     return (-1);
148 }
149 #endif
150
151 int RAND_status(void)
152 {
153     const RAND_METHOD *meth = RAND_get_rand_method();
154     if (meth && meth->status)
155         return meth->status();
156     return 0;
157 }