Fixups for STORE commit
[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_int.h"
15 #include <openssl/engine.h>
16 #include "internal/thread_once.h"
17 #include "rand_lcl.h"
18
19 #ifndef OPENSSL_NO_ENGINE
20 /* non-NULL if default_RAND_meth is ENGINE-provided */
21 static ENGINE *funct_ref;
22 static CRYPTO_RWLOCK *rand_engine_lock;
23 #endif
24 static CRYPTO_RWLOCK *rand_meth_lock;
25 static const RAND_METHOD *default_RAND_meth;
26 static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT;
27
28 #ifdef OPENSSL_RAND_SEED_RDTSC
29 /*
30  * IMPORTANT NOTE:  It is not currently possible to use this code
31  * because we are not sure about the amount of randomness.  Some
32  * SP900 tests have been run, but there is internal skepticism.
33  * So for now this code is not used.
34  */
35 # error "RDTSC enabled?  Should not be possible!"
36
37 /*
38  * Since we get some randomness from the low-order bits of the
39  * high-speec clock, it can help.  But don't return a status since
40  * it's not sufficient to indicate whether or not the seeding was
41  * done.
42  */
43 void rand_rdtsc(void)
44 {
45     unsigned char c;
46     int i;
47
48     for (i = 0; i < 10; i++) {
49         c = (unsigned char)(OPENSSL_rdtsc() & 0xFF);
50         RAND_add(&c, 1, 0.5);
51     }
52 }
53 #endif
54
55 #ifdef OPENSSL_RAND_SEED_RDCPU
56 size_t OPENSSL_ia32_rdseed(void);
57 size_t OPENSSL_ia32_rdrand(void);
58
59 extern unsigned int OPENSSL_ia32cap_P[];
60
61 int rand_rdcpu(void)
62 {
63     size_t i, s;
64
65     /* If RDSEED is available, use that. */
66     if ((OPENSSL_ia32cap_P[1] & (1 << 18)) != 0) {
67         for (i = 0; i < RANDOMNESS_NEEDED; i += sizeof(s)) {
68             s = OPENSSL_ia32_rdseed();
69             if (s == 0)
70                 break;
71             RAND_add(&s, (int)sizeof(s), sizeof(s));
72         }
73         if (i >= RANDOMNESS_NEEDED)
74             return 1;
75     }
76
77     /* Second choice is RDRAND. */
78     if ((OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) != 0) {
79         for (i = 0; i < RANDOMNESS_NEEDED; i += sizeof(s)) {
80             s = OPENSSL_ia32_rdrand();
81             if (s == 0)
82                 break;
83             RAND_add(&s, (int)sizeof(s), sizeof(s));
84         }
85         if (i >= RANDOMNESS_NEEDED)
86             return 1;
87     }
88
89     return 0;
90 }
91 #endif
92
93 DEFINE_RUN_ONCE_STATIC(do_rand_init)
94 {
95     int ret = 1;
96 #ifndef OPENSSL_NO_ENGINE
97     rand_engine_lock = CRYPTO_THREAD_lock_new();
98     ret &= rand_engine_lock != NULL;
99 #endif
100     rand_meth_lock = CRYPTO_THREAD_lock_new();
101     ret &= rand_meth_lock != NULL;
102     return ret;
103 }
104
105 void rand_cleanup_int(void)
106 {
107     const RAND_METHOD *meth = default_RAND_meth;
108
109     if (meth != NULL && meth->cleanup != NULL)
110         meth->cleanup();
111     RAND_set_rand_method(NULL);
112 #ifndef OPENSSL_NO_ENGINE
113     CRYPTO_THREAD_lock_free(rand_engine_lock);
114 #endif
115     CRYPTO_THREAD_lock_free(rand_meth_lock);
116     rand_drbg_cleanup();
117 }
118
119 int RAND_set_rand_method(const RAND_METHOD *meth)
120 {
121     if (!RUN_ONCE(&rand_init, do_rand_init))
122         return 0;
123
124     CRYPTO_THREAD_write_lock(rand_meth_lock);
125 #ifndef OPENSSL_NO_ENGINE
126     ENGINE_finish(funct_ref);
127     funct_ref = NULL;
128 #endif
129     default_RAND_meth = meth;
130     CRYPTO_THREAD_unlock(rand_meth_lock);
131     return 1;
132 }
133
134 const RAND_METHOD *RAND_get_rand_method(void)
135 {
136     const RAND_METHOD *tmp_meth = NULL;
137
138     if (!RUN_ONCE(&rand_init, do_rand_init))
139         return NULL;
140
141     CRYPTO_THREAD_write_lock(rand_meth_lock);
142     if (default_RAND_meth == NULL) {
143 #ifndef OPENSSL_NO_ENGINE
144         ENGINE *e;
145
146         /* If we have an engine that can do RAND, use it. */
147         if ((e = ENGINE_get_default_RAND()) != NULL
148                 && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
149             funct_ref = e;
150             default_RAND_meth = tmp_meth;
151         } else {
152             ENGINE_finish(e);
153             default_RAND_meth = &openssl_rand_meth;
154         }
155 #else
156         default_RAND_meth = &openssl_rand_meth;
157 #endif
158     }
159     tmp_meth = default_RAND_meth;
160     CRYPTO_THREAD_unlock(rand_meth_lock);
161     return tmp_meth;
162 }
163
164 #ifndef OPENSSL_NO_ENGINE
165 int RAND_set_rand_engine(ENGINE *engine)
166 {
167     const RAND_METHOD *tmp_meth = NULL;
168
169     if (!RUN_ONCE(&rand_init, do_rand_init))
170         return 0;
171
172     if (engine != NULL) {
173         if (!ENGINE_init(engine))
174             return 0;
175         tmp_meth = ENGINE_get_RAND(engine);
176         if (tmp_meth == NULL) {
177             ENGINE_finish(engine);
178             return 0;
179         }
180     }
181     CRYPTO_THREAD_write_lock(rand_engine_lock);
182     /* This function releases any prior ENGINE so call it first */
183     RAND_set_rand_method(tmp_meth);
184     funct_ref = engine;
185     CRYPTO_THREAD_unlock(rand_engine_lock);
186     return 1;
187 }
188 #endif
189
190 void RAND_seed(const void *buf, int num)
191 {
192     const RAND_METHOD *meth = RAND_get_rand_method();
193
194     if (meth->seed != NULL)
195         meth->seed(buf, num);
196 }
197
198 void RAND_add(const void *buf, int num, double randomness)
199 {
200     const RAND_METHOD *meth = RAND_get_rand_method();
201
202     if (meth->add != NULL)
203         meth->add(buf, num, randomness);
204 }
205
206 int RAND_bytes(unsigned char *buf, int num)
207 {
208     const RAND_METHOD *meth = RAND_get_rand_method();
209
210     if (meth->bytes != NULL)
211         return meth->bytes(buf, num);
212     RANDerr(RAND_F_RAND_BYTES, RAND_R_FUNC_NOT_IMPLEMENTED);
213     return -1;
214 }
215
216 #if OPENSSL_API_COMPAT < 0x10100000L
217 int RAND_pseudo_bytes(unsigned char *buf, int num)
218 {
219     const RAND_METHOD *meth = RAND_get_rand_method();
220
221     if (meth->pseudorand != NULL)
222         return meth->pseudorand(buf, num);
223     return -1;
224 }
225 #endif
226
227 int RAND_status(void)
228 {
229     const RAND_METHOD *meth = RAND_get_rand_method();
230
231     if (meth->status != NULL)
232         return meth->status();
233     return 0;
234 }