Coverage Report

Created: 2025-12-10 06:24

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