2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "crypto/rand.h"
11 #include "crypto/rand_pool.h"
12 #include <openssl/core_dispatch.h>
13 #include <openssl/err.h>
15 size_t ossl_rand_get_entropy(ossl_unused OSSL_CORE_HANDLE
*handle
,
16 unsigned char **pout
, int entropy
,
17 size_t min_len
, size_t max_len
)
20 size_t entropy_available
;
23 pool
= ossl_rand_pool_new(entropy
, 1, min_len
, max_len
);
25 ERR_raise(ERR_LIB_RAND
, ERR_R_MALLOC_FAILURE
);
29 /* Get entropy by polling system entropy sources. */
30 entropy_available
= ossl_pool_acquire_entropy(pool
);
32 if (entropy_available
> 0) {
33 ret
= ossl_rand_pool_length(pool
);
34 *pout
= ossl_rand_pool_detach(pool
);
37 ossl_rand_pool_free(pool
);
41 void ossl_rand_cleanup_entropy(ossl_unused OSSL_CORE_HANDLE
*handle
,
42 unsigned char *buf
, size_t len
)
44 OPENSSL_secure_clear_free(buf
, len
);
47 size_t ossl_rand_get_nonce(ossl_unused OSSL_CORE_HANDLE
*handle
,
48 unsigned char **pout
, size_t min_len
, size_t max_len
,
49 const void *salt
, size_t salt_len
)
54 pool
= ossl_rand_pool_new(0, 0, min_len
, max_len
);
56 ERR_raise(ERR_LIB_RAND
, ERR_R_MALLOC_FAILURE
);
60 if (!ossl_pool_add_nonce_data(pool
))
63 if (salt
!= NULL
&& !ossl_rand_pool_add(pool
, salt
, salt_len
, 0))
65 ret
= ossl_rand_pool_length(pool
);
66 *pout
= ossl_rand_pool_detach(pool
);
68 ossl_rand_pool_free(pool
);
72 void ossl_rand_cleanup_nonce(ossl_unused OSSL_CORE_HANDLE
*handle
,
73 unsigned char *buf
, size_t len
)
75 OPENSSL_clear_free(buf
, len
);