2020-07-22 10:55:31 +08:00
|
|
|
/*
|
2021-04-08 20:04:41 +08:00
|
|
|
* Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2020-07-22 10:55:31 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include "rand_local.h"
|
|
|
|
|
|
|
|
/* Implements the default OpenSSL RAND_add() method */
|
|
|
|
static int drbg_add(const void *buf, int num, double randomness)
|
|
|
|
{
|
|
|
|
EVP_RAND_CTX *drbg = RAND_get0_primary(NULL);
|
|
|
|
|
|
|
|
if (drbg == NULL || num <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Implements the default OpenSSL RAND_seed() method */
|
|
|
|
static int drbg_seed(const void *buf, int num)
|
|
|
|
{
|
|
|
|
return drbg_add(buf, num, num);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Implements the default OpenSSL RAND_status() method */
|
|
|
|
static int drbg_status(void)
|
|
|
|
{
|
|
|
|
EVP_RAND_CTX *drbg = RAND_get0_primary(NULL);
|
|
|
|
|
|
|
|
if (drbg == NULL)
|
|
|
|
return 0;
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
return EVP_RAND_get_state(drbg) == EVP_RAND_STATE_READY ? 1 : 0;
|
2020-07-22 10:55:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Implements the default OpenSSL RAND_bytes() method */
|
|
|
|
static int drbg_bytes(unsigned char *out, int count)
|
|
|
|
{
|
|
|
|
EVP_RAND_CTX *drbg = RAND_get0_public(NULL);
|
|
|
|
|
|
|
|
if (drbg == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return EVP_RAND_generate(drbg, out, count, 0, 0, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2021-03-08 17:17:53 +08:00
|
|
|
RAND_METHOD ossl_rand_meth = {
|
2020-07-22 10:55:31 +08:00
|
|
|
drbg_seed,
|
|
|
|
drbg_bytes,
|
|
|
|
NULL,
|
|
|
|
drbg_add,
|
|
|
|
drbg_bytes,
|
|
|
|
drbg_status
|
|
|
|
};
|
|
|
|
|
|
|
|
RAND_METHOD *RAND_OpenSSL(void)
|
|
|
|
{
|
2021-03-08 17:17:53 +08:00
|
|
|
return &ossl_rand_meth;
|
2020-07-22 10:55:31 +08:00
|
|
|
}
|