Coverage Report

Created: 2025-12-08 06:22

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