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