/src/openssl/crypto/rand/rand_pool.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (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 "crypto/rand.h"  | 
15  |  | #include <openssl/engine.h>  | 
16  |  | #include "internal/thread_once.h"  | 
17  |  | #include "crypto/rand_pool.h"  | 
18  |  |  | 
19  |  | /*  | 
20  |  |  * Allocate memory and initialize a new random pool  | 
21  |  |  */  | 
22  |  | RAND_POOL *ossl_rand_pool_new(int entropy_requested, int secure,  | 
23  |  |                               size_t min_len, size_t max_len)  | 
24  | 0  | { | 
25  | 0  |     RAND_POOL *pool = OPENSSL_zalloc(sizeof(*pool));  | 
26  | 0  |     size_t min_alloc_size = RAND_POOL_MIN_ALLOCATION(secure);  | 
27  |  | 
  | 
28  | 0  |     if (pool == NULL)  | 
29  | 0  |         return NULL;  | 
30  |  |  | 
31  | 0  |     pool->min_len = min_len;  | 
32  | 0  |     pool->max_len = (max_len > RAND_POOL_MAX_LENGTH) ?  | 
33  | 0  |         RAND_POOL_MAX_LENGTH : max_len;  | 
34  | 0  |     pool->alloc_len = min_len < min_alloc_size ? min_alloc_size : min_len;  | 
35  | 0  |     if (pool->alloc_len > pool->max_len)  | 
36  | 0  |         pool->alloc_len = pool->max_len;  | 
37  |  | 
  | 
38  | 0  |     if (secure)  | 
39  | 0  |         pool->buffer = OPENSSL_secure_zalloc(pool->alloc_len);  | 
40  | 0  |     else  | 
41  | 0  |         pool->buffer = OPENSSL_zalloc(pool->alloc_len);  | 
42  |  | 
  | 
43  | 0  |     if (pool->buffer == NULL)  | 
44  | 0  |         goto err;  | 
45  |  |  | 
46  | 0  |     pool->entropy_requested = entropy_requested;  | 
47  | 0  |     pool->secure = secure;  | 
48  | 0  |     return pool;  | 
49  |  |  | 
50  | 0  | err:  | 
51  | 0  |     OPENSSL_free(pool);  | 
52  | 0  |     return NULL;  | 
53  | 0  | }  | 
54  |  |  | 
55  |  | /*  | 
56  |  |  * Attach new random pool to the given buffer  | 
57  |  |  *  | 
58  |  |  * This function is intended to be used only for feeding random data  | 
59  |  |  * provided by RAND_add() and RAND_seed() into the <master> DRBG.  | 
60  |  |  */  | 
61  |  | RAND_POOL *ossl_rand_pool_attach(const unsigned char *buffer, size_t len,  | 
62  |  |                                  size_t entropy)  | 
63  | 0  | { | 
64  | 0  |     RAND_POOL *pool = OPENSSL_zalloc(sizeof(*pool));  | 
65  |  | 
  | 
66  | 0  |     if (pool == NULL)  | 
67  | 0  |         return NULL;  | 
68  |  |  | 
69  |  |     /*  | 
70  |  |      * The const needs to be cast away, but attached buffers will not be  | 
71  |  |      * modified (in contrary to allocated buffers which are zeroed and  | 
72  |  |      * freed in the end).  | 
73  |  |      */  | 
74  | 0  |     pool->buffer = (unsigned char *) buffer;  | 
75  | 0  |     pool->len = len;  | 
76  |  | 
  | 
77  | 0  |     pool->attached = 1;  | 
78  |  | 
  | 
79  | 0  |     pool->min_len = pool->max_len = pool->alloc_len = pool->len;  | 
80  | 0  |     pool->entropy = entropy;  | 
81  |  | 
  | 
82  | 0  |     return pool;  | 
83  | 0  | }  | 
84  |  |  | 
85  |  | /*  | 
86  |  |  * Free |pool|, securely erasing its buffer.  | 
87  |  |  */  | 
88  |  | void ossl_rand_pool_free(RAND_POOL *pool)  | 
89  | 0  | { | 
90  | 0  |     if (pool == NULL)  | 
91  | 0  |         return;  | 
92  |  |  | 
93  |  |     /*  | 
94  |  |      * Although it would be advisable from a cryptographical viewpoint,  | 
95  |  |      * we are not allowed to clear attached buffers, since they are passed  | 
96  |  |      * to ossl_rand_pool_attach() as `const unsigned char*`.  | 
97  |  |      * (see corresponding comment in ossl_rand_pool_attach()).  | 
98  |  |      */  | 
99  | 0  |     if (!pool->attached) { | 
100  | 0  |         if (pool->secure)  | 
101  | 0  |             OPENSSL_secure_clear_free(pool->buffer, pool->alloc_len);  | 
102  | 0  |         else  | 
103  | 0  |             OPENSSL_clear_free(pool->buffer, pool->alloc_len);  | 
104  | 0  |     }  | 
105  |  | 
  | 
106  | 0  |     OPENSSL_free(pool);  | 
107  | 0  | }  | 
108  |  |  | 
109  |  | /*  | 
110  |  |  * Return the |pool|'s buffer to the caller (readonly).  | 
111  |  |  */  | 
112  |  | const unsigned char *ossl_rand_pool_buffer(RAND_POOL *pool)  | 
113  | 0  | { | 
114  | 0  |     return pool->buffer;  | 
115  | 0  | }  | 
116  |  |  | 
117  |  | /*  | 
118  |  |  * Return the |pool|'s entropy to the caller.  | 
119  |  |  */  | 
120  |  | size_t ossl_rand_pool_entropy(RAND_POOL *pool)  | 
121  | 0  | { | 
122  | 0  |     return pool->entropy;  | 
123  | 0  | }  | 
124  |  |  | 
125  |  | /*  | 
126  |  |  * Return the |pool|'s buffer length to the caller.  | 
127  |  |  */  | 
128  |  | size_t ossl_rand_pool_length(RAND_POOL *pool)  | 
129  | 0  | { | 
130  | 0  |     return pool->len;  | 
131  | 0  | }  | 
132  |  |  | 
133  |  | /*  | 
134  |  |  * Detach the |pool| buffer and return it to the caller.  | 
135  |  |  * It's the responsibility of the caller to free the buffer  | 
136  |  |  * using OPENSSL_secure_clear_free() or to re-attach it  | 
137  |  |  * again to the pool using ossl_rand_pool_reattach().  | 
138  |  |  */  | 
139  |  | unsigned char *ossl_rand_pool_detach(RAND_POOL *pool)  | 
140  | 0  | { | 
141  | 0  |     unsigned char *ret = pool->buffer;  | 
142  | 0  |     pool->buffer = NULL;  | 
143  | 0  |     pool->entropy = 0;  | 
144  | 0  |     return ret;  | 
145  | 0  | }  | 
146  |  |  | 
147  |  | /*  | 
148  |  |  * Re-attach the |pool| buffer. It is only allowed to pass  | 
149  |  |  * the |buffer| which was previously detached from the same pool.  | 
150  |  |  */  | 
151  |  | void ossl_rand_pool_reattach(RAND_POOL *pool, unsigned char *buffer)  | 
152  | 0  | { | 
153  | 0  |     pool->buffer = buffer;  | 
154  | 0  |     OPENSSL_cleanse(pool->buffer, pool->len);  | 
155  | 0  |     pool->len = 0;  | 
156  | 0  | }  | 
157  |  |  | 
158  |  | /*  | 
159  |  |  * If |entropy_factor| bits contain 1 bit of entropy, how many bytes does one  | 
160  |  |  * need to obtain at least |bits| bits of entropy?  | 
161  |  |  */  | 
162  |  | #define ENTROPY_TO_BYTES(bits, entropy_factor) \  | 
163  | 0  |     (((bits) * (entropy_factor) + 7) / 8)  | 
164  |  |  | 
165  |  |  | 
166  |  | /*  | 
167  |  |  * Checks whether the |pool|'s entropy is available to the caller.  | 
168  |  |  * This is the case when entropy count and buffer length are high enough.  | 
169  |  |  * Returns  | 
170  |  |  *  | 
171  |  |  *  |entropy|  if the entropy count and buffer size is large enough  | 
172  |  |  *      0      otherwise  | 
173  |  |  */  | 
174  |  | size_t ossl_rand_pool_entropy_available(RAND_POOL *pool)  | 
175  | 0  | { | 
176  | 0  |     if (pool->entropy < pool->entropy_requested)  | 
177  | 0  |         return 0;  | 
178  |  |  | 
179  | 0  |     if (pool->len < pool->min_len)  | 
180  | 0  |         return 0;  | 
181  |  |  | 
182  | 0  |     return pool->entropy;  | 
183  | 0  | }  | 
184  |  |  | 
185  |  | /*  | 
186  |  |  * Returns the (remaining) amount of entropy needed to fill  | 
187  |  |  * the random pool.  | 
188  |  |  */  | 
189  |  |  | 
190  |  | size_t ossl_rand_pool_entropy_needed(RAND_POOL *pool)  | 
191  | 0  | { | 
192  | 0  |     if (pool->entropy < pool->entropy_requested)  | 
193  | 0  |         return pool->entropy_requested - pool->entropy;  | 
194  |  |  | 
195  | 0  |     return 0;  | 
196  | 0  | }  | 
197  |  |  | 
198  |  | /* Increase the allocation size -- not usable for an attached pool */  | 
199  |  | static int rand_pool_grow(RAND_POOL *pool, size_t len)  | 
200  | 0  | { | 
201  | 0  |     if (len > pool->alloc_len - pool->len) { | 
202  | 0  |         unsigned char *p;  | 
203  | 0  |         const size_t limit = pool->max_len / 2;  | 
204  | 0  |         size_t newlen = pool->alloc_len;  | 
205  |  | 
  | 
206  | 0  |         if (pool->attached || len > pool->max_len - pool->len) { | 
207  | 0  |             ERR_raise(ERR_LIB_RAND, ERR_R_INTERNAL_ERROR);  | 
208  | 0  |             return 0;  | 
209  | 0  |         }  | 
210  |  |  | 
211  | 0  |         do  | 
212  | 0  |             newlen = newlen < limit ? newlen * 2 : pool->max_len;  | 
213  | 0  |         while (len > newlen - pool->len);  | 
214  |  | 
  | 
215  | 0  |         if (pool->secure)  | 
216  | 0  |             p = OPENSSL_secure_zalloc(newlen);  | 
217  | 0  |         else  | 
218  | 0  |             p = OPENSSL_zalloc(newlen);  | 
219  | 0  |         if (p == NULL)  | 
220  | 0  |             return 0;  | 
221  | 0  |         memcpy(p, pool->buffer, pool->len);  | 
222  | 0  |         if (pool->secure)  | 
223  | 0  |             OPENSSL_secure_clear_free(pool->buffer, pool->alloc_len);  | 
224  | 0  |         else  | 
225  | 0  |             OPENSSL_clear_free(pool->buffer, pool->alloc_len);  | 
226  | 0  |         pool->buffer = p;  | 
227  | 0  |         pool->alloc_len = newlen;  | 
228  | 0  |     }  | 
229  | 0  |     return 1;  | 
230  | 0  | }  | 
231  |  |  | 
232  |  | /*  | 
233  |  |  * Returns the number of bytes needed to fill the pool, assuming  | 
234  |  |  * the input has 1 / |entropy_factor| entropy bits per data bit.  | 
235  |  |  * In case of an error, 0 is returned.  | 
236  |  |  */  | 
237  |  |  | 
238  |  | size_t ossl_rand_pool_bytes_needed(RAND_POOL *pool, unsigned int entropy_factor)  | 
239  | 0  | { | 
240  | 0  |     size_t bytes_needed;  | 
241  | 0  |     size_t entropy_needed = ossl_rand_pool_entropy_needed(pool);  | 
242  |  | 
  | 
243  | 0  |     if (entropy_factor < 1) { | 
244  | 0  |         ERR_raise(ERR_LIB_RAND, RAND_R_ARGUMENT_OUT_OF_RANGE);  | 
245  | 0  |         return 0;  | 
246  | 0  |     }  | 
247  |  |  | 
248  | 0  |     bytes_needed = ENTROPY_TO_BYTES(entropy_needed, entropy_factor);  | 
249  |  | 
  | 
250  | 0  |     if (bytes_needed > pool->max_len - pool->len) { | 
251  |  |         /* not enough space left */  | 
252  | 0  |         ERR_raise_data(ERR_LIB_RAND, RAND_R_RANDOM_POOL_OVERFLOW,  | 
253  | 0  |                        "entropy_factor=%u, entropy_needed=%zu, bytes_needed=%zu,"  | 
254  | 0  |                        "pool->max_len=%zu, pool->len=%zu",  | 
255  | 0  |                        entropy_factor, entropy_needed, bytes_needed,  | 
256  | 0  |                        pool->max_len, pool->len);  | 
257  | 0  |         return 0;  | 
258  | 0  |     }  | 
259  |  |  | 
260  | 0  |     if (pool->len < pool->min_len &&  | 
261  | 0  |         bytes_needed < pool->min_len - pool->len)  | 
262  |  |         /* to meet the min_len requirement */  | 
263  | 0  |         bytes_needed = pool->min_len - pool->len;  | 
264  |  |  | 
265  |  |     /*  | 
266  |  |      * Make sure the buffer is large enough for the requested amount  | 
267  |  |      * of data. This guarantees that existing code patterns where  | 
268  |  |      * ossl_rand_pool_add_begin, ossl_rand_pool_add_end or ossl_rand_pool_add  | 
269  |  |      * are used to collect entropy data without any error handling  | 
270  |  |      * whatsoever, continue to be valid.  | 
271  |  |      * Furthermore if the allocation here fails once, make sure that  | 
272  |  |      * we don't fall back to a less secure or even blocking random source,  | 
273  |  |      * as that could happen by the existing code patterns.  | 
274  |  |      * This is not a concern for additional data, therefore that  | 
275  |  |      * is not needed if rand_pool_grow fails in other places.  | 
276  |  |      */  | 
277  | 0  |     if (!rand_pool_grow(pool, bytes_needed)) { | 
278  |  |         /* persistent error for this pool */  | 
279  | 0  |         pool->max_len = pool->len = 0;  | 
280  | 0  |         return 0;  | 
281  | 0  |     }  | 
282  |  |  | 
283  | 0  |     return bytes_needed;  | 
284  | 0  | }  | 
285  |  |  | 
286  |  | /* Returns the remaining number of bytes available */  | 
287  |  | size_t ossl_rand_pool_bytes_remaining(RAND_POOL *pool)  | 
288  | 0  | { | 
289  | 0  |     return pool->max_len - pool->len;  | 
290  | 0  | }  | 
291  |  |  | 
292  |  | /*  | 
293  |  |  * Add random bytes to the random pool.  | 
294  |  |  *  | 
295  |  |  * It is expected that the |buffer| contains |len| bytes of  | 
296  |  |  * random input which contains at least |entropy| bits of  | 
297  |  |  * randomness.  | 
298  |  |  *  | 
299  |  |  * Returns 1 if the added amount is adequate, otherwise 0  | 
300  |  |  */  | 
301  |  | int ossl_rand_pool_add(RAND_POOL *pool,  | 
302  |  |                   const unsigned char *buffer, size_t len, size_t entropy)  | 
303  | 0  | { | 
304  | 0  |     if (len > pool->max_len - pool->len) { | 
305  | 0  |         ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_INPUT_TOO_LONG);  | 
306  | 0  |         return 0;  | 
307  | 0  |     }  | 
308  |  |  | 
309  | 0  |     if (pool->buffer == NULL) { | 
310  | 0  |         ERR_raise(ERR_LIB_RAND, ERR_R_INTERNAL_ERROR);  | 
311  | 0  |         return 0;  | 
312  | 0  |     }  | 
313  |  |  | 
314  | 0  |     if (len > 0) { | 
315  |  |         /*  | 
316  |  |          * This is to protect us from accidentally passing the buffer  | 
317  |  |          * returned from ossl_rand_pool_add_begin.  | 
318  |  |          * The check for alloc_len makes sure we do not compare the  | 
319  |  |          * address of the end of the allocated memory to something  | 
320  |  |          * different, since that comparison would have an  | 
321  |  |          * indeterminate result.  | 
322  |  |          */  | 
323  | 0  |         if (pool->alloc_len > pool->len && pool->buffer + pool->len == buffer) { | 
324  | 0  |             ERR_raise(ERR_LIB_RAND, ERR_R_INTERNAL_ERROR);  | 
325  | 0  |             return 0;  | 
326  | 0  |         }  | 
327  |  |         /*  | 
328  |  |          * We have that only for cases when a pool is used to collect  | 
329  |  |          * additional data.  | 
330  |  |          * For entropy data, as long as the allocation request stays within  | 
331  |  |          * the limits given by ossl_rand_pool_bytes_needed this rand_pool_grow  | 
332  |  |          * below is guaranteed to succeed, thus no allocation happens.  | 
333  |  |          */  | 
334  | 0  |         if (!rand_pool_grow(pool, len))  | 
335  | 0  |             return 0;  | 
336  | 0  |         memcpy(pool->buffer + pool->len, buffer, len);  | 
337  | 0  |         pool->len += len;  | 
338  | 0  |         pool->entropy += entropy;  | 
339  | 0  |     }  | 
340  |  |  | 
341  | 0  |     return 1;  | 
342  | 0  | }  | 
343  |  |  | 
344  |  | /*  | 
345  |  |  * Start to add random bytes to the random pool in-place.  | 
346  |  |  *  | 
347  |  |  * Reserves the next |len| bytes for adding random bytes in-place  | 
348  |  |  * and returns a pointer to the buffer.  | 
349  |  |  * The caller is allowed to copy up to |len| bytes into the buffer.  | 
350  |  |  * If |len| == 0 this is considered a no-op and a NULL pointer  | 
351  |  |  * is returned without producing an error message.  | 
352  |  |  *  | 
353  |  |  * After updating the buffer, ossl_rand_pool_add_end() needs to be called  | 
354  |  |  * to finish the update operation (see next comment).  | 
355  |  |  */  | 
356  |  | unsigned char *ossl_rand_pool_add_begin(RAND_POOL *pool, size_t len)  | 
357  | 0  | { | 
358  | 0  |     if (len == 0)  | 
359  | 0  |         return NULL;  | 
360  |  |  | 
361  | 0  |     if (len > pool->max_len - pool->len) { | 
362  | 0  |         ERR_raise(ERR_LIB_RAND, RAND_R_RANDOM_POOL_OVERFLOW);  | 
363  | 0  |         return NULL;  | 
364  | 0  |     }  | 
365  |  |  | 
366  | 0  |     if (pool->buffer == NULL) { | 
367  | 0  |         ERR_raise(ERR_LIB_RAND, ERR_R_INTERNAL_ERROR);  | 
368  | 0  |         return NULL;  | 
369  | 0  |     }  | 
370  |  |  | 
371  |  |     /*  | 
372  |  |      * As long as the allocation request stays within the limits given  | 
373  |  |      * by ossl_rand_pool_bytes_needed this rand_pool_grow below is guaranteed  | 
374  |  |      * to succeed, thus no allocation happens.  | 
375  |  |      * We have that only for cases when a pool is used to collect  | 
376  |  |      * additional data. Then the buffer might need to grow here,  | 
377  |  |      * and of course the caller is responsible to check the return  | 
378  |  |      * value of this function.  | 
379  |  |      */  | 
380  | 0  |     if (!rand_pool_grow(pool, len))  | 
381  | 0  |         return NULL;  | 
382  |  |  | 
383  | 0  |     return pool->buffer + pool->len;  | 
384  | 0  | }  | 
385  |  |  | 
386  |  | /*  | 
387  |  |  * Finish to add random bytes to the random pool in-place.  | 
388  |  |  *  | 
389  |  |  * Finishes an in-place update of the random pool started by  | 
390  |  |  * ossl_rand_pool_add_begin() (see previous comment).  | 
391  |  |  * It is expected that |len| bytes of random input have been added  | 
392  |  |  * to the buffer which contain at least |entropy| bits of randomness.  | 
393  |  |  * It is allowed to add less bytes than originally reserved.  | 
394  |  |  */  | 
395  |  | int ossl_rand_pool_add_end(RAND_POOL *pool, size_t len, size_t entropy)  | 
396  | 0  | { | 
397  | 0  |     if (len > pool->alloc_len - pool->len) { | 
398  | 0  |         ERR_raise(ERR_LIB_RAND, RAND_R_RANDOM_POOL_OVERFLOW);  | 
399  | 0  |         return 0;  | 
400  | 0  |     }  | 
401  |  |  | 
402  | 0  |     if (len > 0) { | 
403  | 0  |         pool->len += len;  | 
404  | 0  |         pool->entropy += entropy;  | 
405  | 0  |     }  | 
406  |  | 
  | 
407  | 0  |     return 1;  | 
408  | 0  | }  | 
409  |  |  | 
410  |  | /**  | 
411  |  |  * @brief Mix in the additional input into an existing entropy in the pool  | 
412  |  |  *  | 
413  |  |  * @param pool     A RAND_POOL to mix the additional input in  | 
414  |  |  * @param adin     A buffer with the additional input  | 
415  |  |  * @param adin_len A length of the additional input  | 
416  |  |  *  | 
417  |  |  * @return 1 if there is any existing entropy in the pool so the additional input  | 
418  |  |  *         can be mixed in, 0 otherwise.  | 
419  |  |  */  | 
420  |  |  | 
421  |  | int ossl_rand_pool_adin_mix_in(RAND_POOL *pool, const unsigned char *adin,  | 
422  |  |                                size_t adin_len)  | 
423  | 0  | { | 
424  | 0  |     if (adin == NULL || adin_len == 0)  | 
425  |  |         /* Nothing to mix in -> success */  | 
426  | 0  |         return 1;  | 
427  |  |  | 
428  | 0  |     if (pool->buffer == NULL) { | 
429  | 0  |         ERR_raise(ERR_LIB_RAND, ERR_R_INTERNAL_ERROR);  | 
430  | 0  |         return 0;  | 
431  | 0  |     }  | 
432  |  |  | 
433  | 0  |     if (pool->len == 0) { | 
434  | 0  |         ERR_raise(ERR_LIB_RAND, RAND_R_RANDOM_POOL_IS_EMPTY);  | 
435  | 0  |         return 0;  | 
436  | 0  |     }  | 
437  |  |  | 
438  | 0  |     if (adin != NULL && adin_len > 0) { | 
439  | 0  |         size_t i;  | 
440  |  |  | 
441  |  |         /* xor the additional data into the pool */  | 
442  | 0  |         for (i = 0; i < adin_len; ++i)  | 
443  | 0  |             pool->buffer[i % pool->len] ^= adin[i];  | 
444  | 0  |     }  | 
445  |  | 
  | 
446  | 0  |     return 1;  | 
447  | 0  | }  |