Lines Matching refs:pool
20 * Allocate memory and initialize a new random pool
25 RAND_POOL *pool = OPENSSL_zalloc(sizeof(*pool));
28 if (pool == NULL) {
33 pool->min_len = min_len;
34 pool->max_len = (max_len > RAND_POOL_MAX_LENGTH) ?
36 pool->alloc_len = min_len < min_alloc_size ? min_alloc_size : min_len;
37 if (pool->alloc_len > pool->max_len)
38 pool->alloc_len = pool->max_len;
41 pool->buffer = OPENSSL_secure_zalloc(pool->alloc_len);
43 pool->buffer = OPENSSL_zalloc(pool->alloc_len);
45 if (pool->buffer == NULL) {
50 pool->entropy_requested = entropy_requested;
51 pool->secure = secure;
52 return pool;
55 OPENSSL_free(pool);
60 * Attach new random pool to the given buffer
68 RAND_POOL *pool = OPENSSL_zalloc(sizeof(*pool));
70 if (pool == NULL) {
80 pool->buffer = (unsigned char *) buffer;
81 pool->len = len;
83 pool->attached = 1;
85 pool->min_len = pool->max_len = pool->alloc_len = pool->len;
86 pool->entropy = entropy;
88 return pool;
92 * Free |pool|, securely erasing its buffer.
94 void ossl_rand_pool_free(RAND_POOL *pool)
96 if (pool == NULL)
105 if (!pool->attached) {
106 if (pool->secure)
107 OPENSSL_secure_clear_free(pool->buffer, pool->alloc_len);
109 OPENSSL_clear_free(pool->buffer, pool->alloc_len);
112 OPENSSL_free(pool);
116 * Return the |pool|'s buffer to the caller (readonly).
118 const unsigned char *ossl_rand_pool_buffer(RAND_POOL *pool)
120 return pool->buffer;
124 * Return the |pool|'s entropy to the caller.
126 size_t ossl_rand_pool_entropy(RAND_POOL *pool)
128 return pool->entropy;
132 * Return the |pool|'s buffer length to the caller.
134 size_t ossl_rand_pool_length(RAND_POOL *pool)
136 return pool->len;
140 * Detach the |pool| buffer and return it to the caller.
143 * again to the pool using ossl_rand_pool_reattach().
145 unsigned char *ossl_rand_pool_detach(RAND_POOL *pool)
147 unsigned char *ret = pool->buffer;
148 pool->buffer = NULL;
149 pool->entropy = 0;
154 * Re-attach the |pool| buffer. It is only allowed to pass
155 * the |buffer| which was previously detached from the same pool.
157 void ossl_rand_pool_reattach(RAND_POOL *pool, unsigned char *buffer)
159 pool->buffer = buffer;
160 OPENSSL_cleanse(pool->buffer, pool->len);
161 pool->len = 0;
173 * Checks whether the |pool|'s entropy is available to the caller.
180 size_t ossl_rand_pool_entropy_available(RAND_POOL *pool)
182 if (pool->entropy < pool->entropy_requested)
185 if (pool->len < pool->min_len)
188 return pool->entropy;
193 * the random pool.
196 size_t ossl_rand_pool_entropy_needed(RAND_POOL *pool)
198 if (pool->entropy < pool->entropy_requested)
199 return pool->entropy_requested - pool->entropy;
204 /* Increase the allocation size -- not usable for an attached pool */
205 static int rand_pool_grow(RAND_POOL *pool, size_t len)
207 if (len > pool->alloc_len - pool->len) {
209 const size_t limit = pool->max_len / 2;
210 size_t newlen = pool->alloc_len;
212 if (pool->attached || len > pool->max_len - pool->len) {
218 newlen = newlen < limit ? newlen * 2 : pool->max_len;
219 while (len > newlen - pool->len);
221 if (pool->secure)
229 memcpy(p, pool->buffer, pool->len);
230 if (pool->secure)
231 OPENSSL_secure_clear_free(pool->buffer, pool->alloc_len);
233 OPENSSL_clear_free(pool->buffer, pool->alloc_len);
234 pool->buffer = p;
235 pool->alloc_len = newlen;
241 * Returns the number of bytes needed to fill the pool, assuming
246 size_t ossl_rand_pool_bytes_needed(RAND_POOL *pool, unsigned int entropy_factor)
249 size_t entropy_needed = ossl_rand_pool_entropy_needed(pool);
258 if (bytes_needed > pool->max_len - pool->len) {
264 if (pool->len < pool->min_len &&
265 bytes_needed < pool->min_len - pool->len)
267 bytes_needed = pool->min_len - pool->len;
281 if (!rand_pool_grow(pool, bytes_needed)) {
282 /* persistent error for this pool */
283 pool->max_len = pool->len = 0;
291 size_t ossl_rand_pool_bytes_remaining(RAND_POOL *pool)
293 return pool->max_len - pool->len;
297 * Add random bytes to the random pool.
305 int ossl_rand_pool_add(RAND_POOL *pool,
308 if (len > pool->max_len - pool->len) {
313 if (pool->buffer == NULL) {
327 if (pool->alloc_len > pool->len && pool->buffer + pool->len == buffer) {
332 * We have that only for cases when a pool is used to collect
338 if (!rand_pool_grow(pool, len))
340 memcpy(pool->buffer + pool->len, buffer, len);
341 pool->len += len;
342 pool->entropy += entropy;
349 * Start to add random bytes to the random pool in-place.
360 unsigned char *ossl_rand_pool_add_begin(RAND_POOL *pool, size_t len)
365 if (len > pool->max_len - pool->len) {
370 if (pool->buffer == NULL) {
379 * We have that only for cases when a pool is used to collect
384 if (!rand_pool_grow(pool, len))
387 return pool->buffer + pool->len;
391 * Finish to add random bytes to the random pool in-place.
393 * Finishes an in-place update of the random pool started by
399 int ossl_rand_pool_add_end(RAND_POOL *pool, size_t len, size_t entropy)
401 if (len > pool->alloc_len - pool->len) {
407 pool->len += len;
408 pool->entropy += entropy;