]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rand/drbg_lib.c
Make RAND_DRBG fork-safe
[thirdparty/openssl.git] / crypto / rand / drbg_lib.c
1 /*
2 * Copyright 2011-2017 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 <string.h>
11 #include <openssl/crypto.h>
12 #include <openssl/err.h>
13 #include <openssl/rand.h>
14 #include "rand_lcl.h"
15
16 /*
17 * Support framework for NIST SP 800-90A DRBG, AES-CTR mode.
18 * The RAND_DRBG is OpenSSL's pointer to an instance of the DRBG.
19 *
20 * The OpenSSL model is to have new and free functions, and that new
21 * does all initialization. That is not the NIST model, which has
22 * instantiation and un-instantiate, and re-use within a new/free
23 * lifecycle. (No doubt this comes from the desire to support hardware
24 * DRBG, where allocation of resources on something like an HSM is
25 * a much bigger deal than just re-setting an allocated resource.)
26 */
27
28 /*
29 * Set/initialize |drbg| to be of type |nid|, with optional |flags|.
30 * Return -2 if the type is not supported, 1 on success and -1 on
31 * failure.
32 */
33 int RAND_DRBG_set(RAND_DRBG *drbg, int nid, unsigned int flags)
34 {
35 int ret = 1;
36
37 drbg->state = DRBG_UNINITIALISED;
38 drbg->flags = flags;
39 drbg->nid = nid;
40
41 switch (nid) {
42 default:
43 RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_UNSUPPORTED_DRBG_TYPE);
44 return -2;
45 case 0:
46 /* Uninitialized; that's okay. */
47 return 1;
48 case NID_aes_128_ctr:
49 case NID_aes_192_ctr:
50 case NID_aes_256_ctr:
51 ret = ctr_init(drbg);
52 break;
53 }
54
55 if (ret < 0)
56 RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_ERROR_INITIALISING_DRBG);
57 return ret;
58 }
59
60 /*
61 * Allocate memory and initialize a new DRBG. The |parent|, if not
62 * NULL, will be used to auto-seed this RAND_DRBG as needed.
63 */
64 RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent)
65 {
66 RAND_DRBG *drbg = OPENSSL_zalloc(sizeof(*drbg));
67 unsigned char *ucp = OPENSSL_zalloc(RANDOMNESS_NEEDED);
68
69 if (drbg == NULL || ucp == NULL) {
70 RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE);
71 goto err;
72 }
73 drbg->size = RANDOMNESS_NEEDED;
74 drbg->randomness = ucp;
75 drbg->fork_count = rand_fork_count;
76 drbg->parent = parent;
77 if (RAND_DRBG_set(drbg, type, flags) < 0)
78 goto err;
79
80 if (parent != NULL) {
81 if (parent->state == DRBG_UNINITIALISED
82 && RAND_DRBG_instantiate(parent, NULL, 0) == 0)
83 goto err;
84 if (!RAND_DRBG_set_callbacks(drbg, drbg_entropy_from_parent,
85 drbg_release_entropy,
86 NULL, NULL)
87 /*
88 * Add in our address. Note we are adding the pointer
89 * itself, not its contents!
90 */
91 || !RAND_DRBG_instantiate(drbg,
92 (unsigned char*)&drbg, sizeof(drbg)))
93 goto err;
94 }
95
96 return drbg;
97
98 err:
99 OPENSSL_free(ucp);
100 OPENSSL_free(drbg);
101 return NULL;
102 }
103
104 RAND_DRBG *RAND_DRBG_get0_global(void)
105 {
106 return &rand_drbg;
107 }
108
109 /*
110 * Uninstantiate |drbg| and free all memory.
111 */
112 void RAND_DRBG_free(RAND_DRBG *drbg)
113 {
114 /* The global DRBG is free'd by rand_cleanup_int() */
115 if (drbg == NULL || drbg == &rand_drbg)
116 return;
117
118 ctr_uninstantiate(drbg);
119 OPENSSL_cleanse(drbg->randomness, drbg->size);
120 OPENSSL_free(drbg->randomness);
121 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
122 OPENSSL_clear_free(drbg, sizeof(*drbg));
123 }
124
125 /*
126 * Instantiate |drbg|, after it has been initialized. Use |pers| and
127 * |perslen| as prediction-resistance input.
128 */
129 int RAND_DRBG_instantiate(RAND_DRBG *drbg,
130 const unsigned char *pers, size_t perslen)
131 {
132 unsigned char *nonce = NULL, *entropy = NULL;
133 size_t noncelen = 0, entlen = 0;
134
135 if (perslen > drbg->max_pers) {
136 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
137 RAND_R_PERSONALISATION_STRING_TOO_LONG);
138 goto end;
139 }
140 if (drbg->state != DRBG_UNINITIALISED) {
141 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
142 drbg->state == DRBG_ERROR ? RAND_R_IN_ERROR_STATE
143 : RAND_R_ALREADY_INSTANTIATED);
144 goto end;
145 }
146
147 drbg->state = DRBG_ERROR;
148 if (drbg->get_entropy != NULL)
149 entlen = drbg->get_entropy(drbg, &entropy, drbg->strength,
150 drbg->min_entropy, drbg->max_entropy);
151 if (entlen < drbg->min_entropy || entlen > drbg->max_entropy) {
152 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_ENTROPY);
153 goto end;
154 }
155
156 if (drbg->max_nonce > 0 && drbg->get_nonce != NULL) {
157 noncelen = drbg->get_nonce(drbg, &nonce, drbg->strength / 2,
158 drbg->min_nonce, drbg->max_nonce);
159 if (noncelen < drbg->min_nonce || noncelen > drbg->max_nonce) {
160 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_NONCE);
161 goto end;
162 }
163 }
164
165 if (!ctr_instantiate(drbg, entropy, entlen,
166 nonce, noncelen, pers, perslen)) {
167 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_INSTANTIATING_DRBG);
168 goto end;
169 }
170
171 drbg->state = DRBG_READY;
172 drbg->reseed_counter = 1;
173
174 end:
175 if (entropy != NULL && drbg->cleanup_entropy != NULL)
176 drbg->cleanup_entropy(drbg, entropy);
177 if (nonce != NULL && drbg->cleanup_nonce!= NULL )
178 drbg->cleanup_nonce(drbg, nonce);
179 if (drbg->state == DRBG_READY)
180 return 1;
181 return 0;
182 }
183
184 /*
185 * Uninstantiate |drbg|. Must be instantiated before it can be used.
186 */
187 int RAND_DRBG_uninstantiate(RAND_DRBG *drbg)
188 {
189 int ret = ctr_uninstantiate(drbg);
190
191 OPENSSL_cleanse(&drbg->ctr, sizeof(drbg->ctr));
192 drbg->state = DRBG_UNINITIALISED;
193 return ret;
194 }
195
196 /*
197 * Mix in the specified data to reseed |drbg|.
198 */
199 int RAND_DRBG_reseed(RAND_DRBG *drbg,
200 const unsigned char *adin, size_t adinlen)
201 {
202 unsigned char *entropy = NULL;
203 size_t entlen = 0;
204
205 if (drbg->state == DRBG_ERROR) {
206 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_IN_ERROR_STATE);
207 return 0;
208 }
209 if (drbg->state == DRBG_UNINITIALISED) {
210 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_NOT_INSTANTIATED);
211 return 0;
212 }
213
214 if (adin == NULL)
215 adinlen = 0;
216 else if (adinlen > drbg->max_adin) {
217 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
218 return 0;
219 }
220
221 drbg->state = DRBG_ERROR;
222 if (drbg->get_entropy != NULL)
223 entlen = drbg->get_entropy(drbg, &entropy, drbg->strength,
224 drbg->min_entropy, drbg->max_entropy);
225 if (entlen < drbg->min_entropy || entlen > drbg->max_entropy) {
226 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ERROR_RETRIEVING_ENTROPY);
227 goto end;
228 }
229
230 if (!ctr_reseed(drbg, entropy, entlen, adin, adinlen))
231 goto end;
232 drbg->state = DRBG_READY;
233 drbg->reseed_counter = 1;
234
235 end:
236 if (entropy != NULL && drbg->cleanup_entropy != NULL)
237 drbg->cleanup_entropy(drbg, entropy);
238 if (drbg->state == DRBG_READY)
239 return 1;
240 return 0;
241 }
242
243 /*
244 * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
245 * to or if |prediction_resistance| is set. Additional input can be
246 * sent in |adin| and |adinlen|.
247 */
248 int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
249 int prediction_resistance,
250 const unsigned char *adin, size_t adinlen)
251 {
252 if (drbg->state == DRBG_ERROR) {
253 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_IN_ERROR_STATE);
254 return 0;
255 }
256 if (drbg->state == DRBG_UNINITIALISED) {
257 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_NOT_INSTANTIATED);
258 return 0;
259 }
260 if (outlen > drbg->max_request) {
261 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG);
262 return 0;
263 }
264 if (adinlen > drbg->max_adin) {
265 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
266 return 0;
267 }
268
269 if (drbg->fork_count != rand_fork_count) {
270 drbg->fork_count = rand_fork_count;
271 drbg->state = DRBG_RESEED;
272 }
273
274 if (drbg->reseed_counter >= drbg->reseed_interval)
275 drbg->state = DRBG_RESEED;
276
277 if (drbg->state == DRBG_RESEED || prediction_resistance) {
278 if (!RAND_DRBG_reseed(drbg, adin, adinlen)) {
279 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_RESEED_ERROR);
280 return 0;
281 }
282 adin = NULL;
283 adinlen = 0;
284 }
285
286 if (!ctr_generate(drbg, out, outlen, adin, adinlen)) {
287 drbg->state = DRBG_ERROR;
288 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_GENERATE_ERROR);
289 return 0;
290 }
291
292 if (drbg->reseed_counter >= drbg->reseed_interval)
293 drbg->state = DRBG_RESEED;
294 else
295 drbg->reseed_counter++;
296 return 1;
297 }
298
299 /*
300 * Set the callbacks for entropy and nonce. We currently don't use
301 * the nonce; that's mainly for the KATs
302 */
303 int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
304 RAND_DRBG_get_entropy_fn cb_get_entropy,
305 RAND_DRBG_cleanup_entropy_fn cb_cleanup_entropy,
306 RAND_DRBG_get_nonce_fn cb_get_nonce,
307 RAND_DRBG_cleanup_nonce_fn cb_cleanup_nonce)
308 {
309 if (drbg->state != DRBG_UNINITIALISED)
310 return 0;
311 drbg->get_entropy = cb_get_entropy;
312 drbg->cleanup_entropy = cb_cleanup_entropy;
313 drbg->get_nonce = cb_get_nonce;
314 drbg->cleanup_nonce = cb_cleanup_nonce;
315 return 1;
316 }
317
318 /*
319 * Set the reseed interval.
320 */
321 int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, int interval)
322 {
323 if (interval < 0 || interval > MAX_RESEED)
324 return 0;
325 drbg->reseed_interval = interval;
326 return 1;
327 }
328
329 /*
330 * Get and set the EXDATA
331 */
332 int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg)
333 {
334 return CRYPTO_set_ex_data(&drbg->ex_data, idx, arg);
335 }
336
337 void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx)
338 {
339 return CRYPTO_get_ex_data(&drbg->ex_data, idx);
340 }
341
342
343 /*
344 * The following functions provide a RAND_METHOD that works on the
345 * global DRBG. They lock.
346 */
347
348 static int drbg_bytes(unsigned char *out, int count)
349 {
350 int ret = 0;
351 size_t chunk;
352
353 CRYPTO_THREAD_write_lock(rand_drbg.lock);
354 if (rand_drbg.state == DRBG_UNINITIALISED
355 && RAND_DRBG_instantiate(&rand_drbg, NULL, 0) == 0)
356 goto err;
357
358 for ( ; count > 0; count -= chunk, out += chunk) {
359 chunk = count;
360 if (chunk > rand_drbg.max_request)
361 chunk = rand_drbg.max_request;
362 ret = RAND_DRBG_generate(&rand_drbg, out, chunk, 0, NULL, 0);
363 if (!ret)
364 goto err;
365 }
366 ret = 1;
367
368 err:
369 CRYPTO_THREAD_unlock(rand_drbg.lock);
370 return ret;
371 }
372
373 static int drbg_add(const void *buf, int num, double randomness)
374 {
375 unsigned char *in = (unsigned char *)buf;
376 unsigned char *out, *end;
377
378 CRYPTO_THREAD_write_lock(rand_bytes.lock);
379 out = &rand_bytes.buff[rand_bytes.curr];
380 end = &rand_bytes.buff[rand_bytes.size];
381
382 /* Copy whatever fits into the end of the buffer. */
383 for ( ; --num >= 0 && out < end; rand_bytes.curr++)
384 *out++ = *in++;
385
386 /* XOR any the leftover. */
387 while (num > 0) {
388 for (out = rand_bytes.buff; --num >= 0 && out < end; )
389 *out++ ^= *in++;
390 }
391
392 CRYPTO_THREAD_unlock(rand_bytes.lock);
393 return 1;
394 }
395
396 static int drbg_seed(const void *buf, int num)
397 {
398 return drbg_add(buf, num, num);
399 }
400
401 static int drbg_status(void)
402 {
403 int ret;
404
405 CRYPTO_THREAD_write_lock(rand_drbg.lock);
406 ret = rand_drbg.state == DRBG_READY ? 1 : 0;
407 CRYPTO_THREAD_unlock(rand_drbg.lock);
408 return ret;
409 }
410
411 RAND_DRBG rand_drbg; /* The default global DRBG. */
412 RAND_DRBG priv_drbg; /* The global private-key DRBG. */
413
414 RAND_METHOD rand_meth = {
415 drbg_seed,
416 drbg_bytes,
417 NULL,
418 drbg_add,
419 drbg_bytes,
420 drbg_status
421 };
422
423 RAND_METHOD *RAND_OpenSSL(void)
424 {
425 return &rand_meth;
426 }