2020-10-30 13:53:22 +08:00
|
|
|
/*
|
2023-09-28 21:23:29 +08:00
|
|
|
* Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
|
2020-10-30 13:53:22 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
2023-09-05 10:51:05 +08:00
|
|
|
#include "rand_local.h"
|
2020-10-30 13:53:22 +08:00
|
|
|
#include "crypto/rand.h"
|
|
|
|
#include "crypto/rand_pool.h"
|
2023-09-05 10:51:05 +08:00
|
|
|
#include "internal/core.h"
|
2020-10-30 13:53:22 +08:00
|
|
|
#include <openssl/core_dispatch.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
2023-09-05 10:51:05 +08:00
|
|
|
size_t ossl_rand_get_entropy(ossl_unused OSSL_LIB_CTX *ctx,
|
2020-10-30 13:53:22 +08:00
|
|
|
unsigned char **pout, int entropy,
|
|
|
|
size_t min_len, size_t max_len)
|
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
size_t entropy_available;
|
|
|
|
RAND_POOL *pool;
|
|
|
|
|
2021-03-08 17:17:53 +08:00
|
|
|
pool = ossl_rand_pool_new(entropy, 1, min_len, max_len);
|
2020-10-30 13:53:22 +08:00
|
|
|
if (pool == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_RAND, ERR_R_RAND_LIB);
|
2020-10-30 13:53:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get entropy by polling system entropy sources. */
|
|
|
|
entropy_available = ossl_pool_acquire_entropy(pool);
|
|
|
|
|
|
|
|
if (entropy_available > 0) {
|
2021-03-08 17:17:53 +08:00
|
|
|
ret = ossl_rand_pool_length(pool);
|
|
|
|
*pout = ossl_rand_pool_detach(pool);
|
2020-10-30 13:53:22 +08:00
|
|
|
}
|
|
|
|
|
2021-03-08 17:17:53 +08:00
|
|
|
ossl_rand_pool_free(pool);
|
2020-10-30 13:53:22 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-09-05 10:51:05 +08:00
|
|
|
size_t ossl_rand_get_user_entropy(OSSL_LIB_CTX *ctx,
|
|
|
|
unsigned char **pout, int entropy,
|
|
|
|
size_t min_len, size_t max_len)
|
|
|
|
{
|
|
|
|
unsigned char *buf;
|
|
|
|
EVP_RAND_CTX *rng = ossl_rand_get0_seed_noncreating(ctx);
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
if (rng == NULL)
|
|
|
|
return ossl_rand_get_entropy(ctx, pout, entropy, min_len, max_len);
|
|
|
|
|
|
|
|
/* Determine how many bytes to generate */
|
|
|
|
ret = entropy > 0 ? (size_t)(7 + entropy) / 8 : min_len;
|
|
|
|
if (ret < min_len)
|
|
|
|
ret = min_len;
|
|
|
|
else if (ret > max_len)
|
|
|
|
ret = max_len;
|
|
|
|
|
|
|
|
/* Allocate the return buffer */
|
|
|
|
if ((buf = OPENSSL_secure_malloc(ret)) == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Fill the buffer */
|
|
|
|
if (!EVP_RAND_generate(rng, buf, ret, entropy, 0, NULL, 0)) {
|
|
|
|
OPENSSL_free(buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*pout = buf;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ossl_rand_cleanup_entropy(ossl_unused OSSL_LIB_CTX *ctx,
|
2020-10-30 13:53:22 +08:00
|
|
|
unsigned char *buf, size_t len)
|
|
|
|
{
|
|
|
|
OPENSSL_secure_clear_free(buf, len);
|
|
|
|
}
|
|
|
|
|
2023-10-17 05:48:03 +08:00
|
|
|
void ossl_rand_cleanup_user_entropy(OSSL_LIB_CTX *ctx,
|
|
|
|
unsigned char *buf, size_t len)
|
|
|
|
{
|
|
|
|
OPENSSL_secure_clear_free(buf, len);
|
|
|
|
}
|
|
|
|
|
2023-09-05 10:51:05 +08:00
|
|
|
size_t ossl_rand_get_nonce(ossl_unused OSSL_LIB_CTX *ctx,
|
|
|
|
unsigned char **pout,
|
|
|
|
size_t min_len, ossl_unused size_t max_len,
|
2020-10-30 13:53:22 +08:00
|
|
|
const void *salt, size_t salt_len)
|
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
RAND_POOL *pool;
|
|
|
|
|
2021-03-08 17:17:53 +08:00
|
|
|
pool = ossl_rand_pool_new(0, 0, min_len, max_len);
|
2020-10-30 13:53:22 +08:00
|
|
|
if (pool == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_RAND, ERR_R_RAND_LIB);
|
2020-10-30 13:53:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ossl_pool_add_nonce_data(pool))
|
|
|
|
goto err;
|
|
|
|
|
2021-03-08 17:17:53 +08:00
|
|
|
if (salt != NULL && !ossl_rand_pool_add(pool, salt, salt_len, 0))
|
2020-10-30 13:53:22 +08:00
|
|
|
goto err;
|
2021-03-08 17:17:53 +08:00
|
|
|
ret = ossl_rand_pool_length(pool);
|
|
|
|
*pout = ossl_rand_pool_detach(pool);
|
2020-10-30 13:53:22 +08:00
|
|
|
err:
|
2021-03-08 17:17:53 +08:00
|
|
|
ossl_rand_pool_free(pool);
|
2020-10-30 13:53:22 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-09-05 10:51:05 +08:00
|
|
|
size_t ossl_rand_get_user_nonce(OSSL_LIB_CTX *ctx,
|
|
|
|
unsigned char **pout,
|
|
|
|
size_t min_len, size_t max_len,
|
|
|
|
const void *salt, size_t salt_len)
|
|
|
|
{
|
|
|
|
unsigned char *buf;
|
|
|
|
EVP_RAND_CTX *rng = ossl_rand_get0_seed_noncreating(ctx);
|
|
|
|
|
|
|
|
if (rng == NULL)
|
|
|
|
return ossl_rand_get_nonce(ctx, pout, min_len, max_len, salt, salt_len);
|
|
|
|
|
|
|
|
if ((buf = OPENSSL_malloc(min_len)) == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!EVP_RAND_generate(rng, buf, min_len, 0, 0, salt, salt_len)) {
|
|
|
|
OPENSSL_free(buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*pout = buf;
|
|
|
|
return min_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ossl_rand_cleanup_nonce(ossl_unused OSSL_LIB_CTX *ctx,
|
2020-10-30 13:53:22 +08:00
|
|
|
unsigned char *buf, size_t len)
|
|
|
|
{
|
|
|
|
OPENSSL_clear_free(buf, len);
|
|
|
|
}
|
2023-10-17 05:48:03 +08:00
|
|
|
|
|
|
|
void ossl_rand_cleanup_user_nonce(ossl_unused OSSL_LIB_CTX *ctx,
|
|
|
|
unsigned char *buf, size_t len)
|
|
|
|
{
|
|
|
|
OPENSSL_clear_free(buf, len);
|
|
|
|
}
|