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