EVP_RAND(3ossl)
β¨ NAME β¨
Okay bestie, welcome to the OpenSSL EVP_RAND(3ossl) page β yes, that's the manual name β and it's all about the EVP RAND routines. The cast list is long, so hydrate first π«Ά:
EVP_RAND,EVP_RAND_fetch,EVP_RAND_free,EVP_RAND_up_ref,EVP_RAND_CTX,EVP_RAND_CTX_new,EVP_RAND_CTX_free,EVP_RAND_CTX_up_refEVP_RAND_instantiate,EVP_RAND_uninstantiate,EVP_RAND_generate,EVP_RAND_reseed,EVP_RAND_nonce,EVP_RAND_enable_locking,EVP_RAND_verify_zeroization,EVP_RAND_get_strength,EVP_RAND_get_stateEVP_RAND_get0_provider,EVP_RAND_CTX_get0_rand,EVP_RAND_is_a,EVP_RAND_get0_name,EVP_RAND_names_do_all,EVP_RAND_get0_descriptionEVP_RAND_CTX_get_params,EVP_RAND_CTX_set_params,EVP_RAND_do_all_provided,EVP_RAND_get_params,EVP_RAND_gettable_ctx_params,EVP_RAND_settable_ctx_params,EVP_RAND_CTX_gettable_params,EVP_RAND_CTX_settable_params,EVP_RAND_gettable_paramsEVP_RAND_STATE_UNINITIALISED,EVP_RAND_STATE_READY,EVP_RAND_STATE_ERROR
That's the whole crew. No cap, every single one π .
π SYNOPSIS π
Here's the synopsis code block, literally untouched, because the signatures are the moment and we do not mess with them:
#include <openssl/evp.h>
typedef struct evp_rand_st EVP_RAND;
typedef struct evp_rand_ctx_st EVP_RAND_CTX;
EVP_RAND *EVP_RAND_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
const char *properties);
int EVP_RAND_up_ref(EVP_RAND *rand);
void EVP_RAND_free(EVP_RAND *rand);
EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent);
void EVP_RAND_CTX_free(EVP_RAND_CTX *ctx);
int EVP_RAND_CTX_up_ref(EVP_RAND_CTX *ctx);
EVP_RAND *EVP_RAND_CTX_get0_rand(EVP_RAND_CTX *ctx);
int EVP_RAND_get_params(EVP_RAND *rand, OSSL_PARAM params[]);
int EVP_RAND_CTX_get_params(EVP_RAND_CTX *ctx, OSSL_PARAM params[]);
int EVP_RAND_CTX_set_params(EVP_RAND_CTX *ctx, const OSSL_PARAM params[]);
const OSSL_PARAM *EVP_RAND_gettable_params(const EVP_RAND *rand);
const OSSL_PARAM *EVP_RAND_gettable_ctx_params(const EVP_RAND *rand);
const OSSL_PARAM *EVP_RAND_settable_ctx_params(const EVP_RAND *rand);
const OSSL_PARAM *EVP_RAND_CTX_gettable_params(EVP_RAND_CTX *ctx);
const OSSL_PARAM *EVP_RAND_CTX_settable_params(EVP_RAND_CTX *ctx);
const char *EVP_RAND_get0_name(const EVP_RAND *rand);
const char *EVP_RAND_get0_description(const EVP_RAND *rand);
int EVP_RAND_is_a(const EVP_RAND *rand, const char *name);
const OSSL_PROVIDER *EVP_RAND_get0_provider(const EVP_RAND *rand);
void EVP_RAND_do_all_provided(OSSL_LIB_CTX *libctx,
void (*fn)(EVP_RAND *rand, void *arg),
void *arg);
int EVP_RAND_names_do_all(const EVP_RAND *rand,
void (*fn)(const char *name, void *data),
void *data);
int EVP_RAND_instantiate(EVP_RAND_CTX *ctx, unsigned int strength,
int prediction_resistance,
const unsigned char *pstr, size_t pstr_len,
const OSSL_PARAM params[]);
int EVP_RAND_uninstantiate(EVP_RAND_CTX *ctx);
int EVP_RAND_generate(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen,
unsigned int strength, int prediction_resistance,
const unsigned char *addin, size_t addin_len);
int EVP_RAND_reseed(EVP_RAND_CTX *ctx, int prediction_resistance,
const unsigned char *ent, size_t ent_len,
const unsigned char *addin, size_t addin_len);
int EVP_RAND_nonce(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen);
int EVP_RAND_enable_locking(EVP_RAND_CTX *ctx);
int EVP_RAND_verify_zeroization(EVP_RAND_CTX *ctx);
unsigned int EVP_RAND_get_strength(EVP_RAND_CTX *ctx);
int EVP_RAND_get_state(EVP_RAND_CTX *ctx);
#define EVP_RAND_STATE_UNINITIALISED 0
#define EVP_RAND_STATE_READY 1
#define EVP_RAND_STATE_ERROR 2
π DESCRIPTION π
okay so the EVP RAND routines are a high-level interface to random number generators β both deterministic and not. If you literally just want to generate random bytes, bestie, you do not need to use these functions: just call RAND_bytes() or RAND_priv_bytes(). If you want to do more, these calls should be used instead of the older RAND and RAND_DRBG functions. no cap β¨
After creating a EVP_RAND_CTX for the required algorithm using EVP_RAND_CTX_new(), inputs to the algorithm are supplied either by passing them as part of the EVP_RAND_instantiate() call or by using calls to EVP_RAND_CTX_set_params() before calling EVP_RAND_instantiate(). Finally, call EVP_RAND_generate() to produce cryptographically secure random bytes. slay π
π Types π
EVP_RANDβ a type that holds the implementation of aRAND.EVP_RAND_CTXβ a context type that holds the algorithm inputs.EVP_RAND_CTXstructures are reference counted. π«Ά
π Algorithm implementation fetching π
EVP_RAND_fetch()β fetches an implementation of aRANDalgorithm, given a library contextlibctxand a set of properties. See"ALGORITHM FETCHING"incrypto(7)for further information.- The returned value must eventually be freed with
EVP_RAND_free(3). EVP_RAND_up_ref()β increments the reference count of an already fetchedRAND.EVP_RAND_free()β frees a fetched algorithm.NULLis a valid parameter, for which this function is a no-op.
π§Έ Context manipulation functions π§Έ
EVP_RAND_CTX_new()β creates a new context for theRANDimplementationrand. If notNULL,parentspecifies the seed source for this implementation. Not all random number generators need to have a seed source specified. If a parent is required, aNULLparent will utilise the operating system entropy sources. It is recommended to minimise the number of random number generators that rely on the operating system for their randomness because this is often scarce. iykyk πΈEVP_RAND_CTX_free()β frees up the contextctx. IfctxisNULL, nothing is done.EVP_RAND_CTX_get0_rand()β returns theEVP_RANDassociated with the contextctx.
π² Random Number Generator Functions π²
EVP_RAND_instantiate()β processes any parameters inparamsand then instantiates theRANDctxwith a minimum security strength of strength and personalisation string pstr of length pstr_len. Ifprediction_resistanceis specified, fresh entropy from a live source will be sought. This call operates as per NIST SP 800-90A and SP 800-90C. main character energy π₯EVP_RAND_uninstantiate()β uninstantiates theRANDctxas per NIST SP 800-90A and SP 800-90C. Subsequent to this call, theRANDcannot be used to generate bytes. It can only be freed or instantiated again. periodtEVP_RAND_generate()β produces random bytes from theRANDctxwith the additional inputaddinof length addin_len. The bytes produced will meet the security strength. Ifprediction_resistanceis specified, fresh entropy from a live source will be sought. This call operates as per NIST SP 800-90A and SP 800-90C.EVP_RAND_reseed()β reseeds theRANDwith new entropy. Entropyentof length ent_len bytes can be supplied as can additional inputaddinof length addin_len bytes. In the FIPS provider, both are treated as additional input as per NIST SP-800-90Ar1, Sections 9.1 and 9.2. Additional seed material is also drawn from theRANDβs parent or the operating system. Ifprediction_resistanceis specified, fresh entropy from a live source will be sought. This call operates as per NIST SP 800-90A and SP 800-90C.EVP_RAND_nonce()β creates a nonce inoutof length outlen bytes from theRANDctx.EVP_RAND_enable_locking()β enables locking for theRANDctxand all of its parents. After thisctxwill operate in a thread safe manner, albeit more slowly. This function is not itself thread safe if called with the samectxfrom multiple threads. Typically locking should be enabled before actxis shared across multiple threads. > tiny warning, bestie: do the locking setup before you pass thatctxaround, mmmkay? π¦EVP_RAND_get_params()β retrieves details about the implementationrand. The set of parameters given withparamsdetermine exactly what parameters should be retrieved. Note that a parameter that is unknown in the underlying context is simply ignored.EVP_RAND_CTX_get_params()β retrieves chosen parameters, given the contextctxand its underlying context. The set of parameters given withparamsdetermine exactly what parameters should be retrieved. Note that a parameter that is unknown in the underlying context is simply ignored.EVP_RAND_CTX_set_params()β passes chosen parameters to the underlying context, given a contextctx. The set of parameters given withparamsdetermine exactly what parameters are passed down. Note that a parameter that is unknown in the underlying context is simply ignored. Also, what happens when a needed parameter isnβt passed down is defined by the implementation. lowkey important πEVP_RAND_gettable_params()β returns anOSSL_PARAM(3)array that describes the retrievable and settable parameters.EVP_RAND_gettable_params()returns parameters that can be used withEVP_RAND_get_params().EVP_RAND_gettable_ctx_params()andEVP_RAND_CTX_gettable_params()β return constantOSSL_PARAM(3)arrays that describe the retrievable parameters that can be used withEVP_RAND_CTX_get_params().EVP_RAND_gettable_ctx_params()returns the parameters that can be retrieved from the algorithm, whereasEVP_RAND_CTX_gettable_params()returns the parameters that can be retrieved in the contextβs current state.EVP_RAND_settable_ctx_params()andEVP_RAND_CTX_settable_params()β return constantOSSL_PARAM(3)arrays that describe the settable parameters that can be used withEVP_RAND_CTX_set_params().EVP_RAND_settable_ctx_params()returns the parameters that can be retrieved from the algorithm, whereasEVP_RAND_CTX_settable_params()returns the parameters that can be retrieved in the contextβs current state. the way that wording is giving βalgorithm vs current ctx stateβ π
π΅οΈ Information functions π΅οΈ
EVP_RAND_get_strength()β returns the security strength of theRANDctx.EVP_RAND_get_state()β returns the current state of theRANDctx. States defined by the OpenSSL RNGs are:EVP_RAND_STATE_UNINITIALISED: this RNG is currently uninitialised. The instantiate call will change this to the ready state.EVP_RAND_STATE_READY: this RNG is currently ready to generate output.EVP_RAND_STATE_ERROR: this RNG is in an error state.
EVP_RAND_is_a()β returns1ifrandis an implementation of an algorithm thatβs identifiable with name, otherwise0.EVP_RAND_get0_provider()β returns the provider that holds the implementation of the givenrand.EVP_RAND_do_all_provided()β traverses allRANDimplemented by all activated providers in the given library contextlibctx, and for each of the implementations, calls the given functionfnwith the implementation method and the givenargas argument.EVP_RAND_get0_name()β returns the canonical name ofrand.EVP_RAND_names_do_all()β traverses all names forrand, and callsfnwith each name anddata.EVP_RAND_get0_description()β returns a description of therand, meant for display and human consumption. The description is at the discretion of therandimplementation.EVP_RAND_verify_zeroization()β confirms if the internal DRBG state is currently zeroed. This is used by the FIPS provider to support the mandatory self tests. mandatory self tests ate π
π PARAMETERS π
The standard parameter names are:
"state"(OSSL_RAND_PARAM_STATE)<integer>β returns the state of the random number generator."strength"(OSSL_RAND_PARAM_STRENGTH)<unsigned integer>β returns the bit strength of the random number generator."fips-indicator"(OSSL_RAND_PARAM_FIPS_APPROVED_INDICATOR)<integer>β a getter that returns1if the operation is FIPS approved, or0otherwise. This option is used by the OpenSSL FIPS provider and is not supported by allEVP_RANDsources. not all sources, bestie π
For rands that are also deterministic random bit generators (DRBGs), these additional parameters are recognised. Not all parameters are relevant to, or are understood by all DRBG rands:
"reseed_requests"(OSSL_DRBG_PARAM_RESEED_REQUESTS)<unsigned integer>β reads or sets the number of generate requests before reseeding the associatedRANDctx."reseed_time_interval"(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL)<integer>β reads or sets the number of elapsed seconds before reseeding the associatedRANDctx."max_request"(OSSL_RAND_PARAM_MAX_REQUEST)<unsigned integer>β specifies the maximum number of bytes that can be generated in a single call toOSSL_FUNC_rand_generate."min_entropylen"(OSSL_DRBG_PARAM_MIN_ENTROPYLEN)<unsigned integer>β specifies the minimum number of bytes of random material that can be used to seed the DRBG."max_entropylen"(OSSL_DRBG_PARAM_MAX_ENTROPYLEN)<unsigned integer>β specifies the maximum number of bytes of random material that can be used to seed the DRBG."min_noncelen"(OSSL_DRBG_PARAM_MIN_NONCELEN)<unsigned integer>β specifies the minimum number of bytes of nonce that can be used to seed the DRBG."max_noncelen"(OSSL_DRBG_PARAM_MAX_NONCELEN)<unsigned integer>β specifies the maximum number of bytes of nonce that can be used to seed the DRBG."max_perslen"(OSSL_DRBG_PARAM_MAX_PERSLEN)<unsigned integer>and"max_adinlen"(OSSL_DRBG_PARAM_MAX_ADINLEN)<unsigned integer>β specify the minimum and maximum number of bytes of personalisation string that can be used with the DRBG."reseed_counter"(OSSL_DRBG_PARAM_RESEED_COUNTER)<unsigned integer>β specifies the number of times the DRBG has been seeded or reseeded."properties"(OSSL_RAND_PARAM_PROPERTIES)<UTF8 string>,"mac"(OSSL_RAND_PARAM_MAC)<UTF8 string>,"digest"(OSSL_RAND_PARAM_DIGEST)<UTF8 string>, and"cipher"(OSSL_RAND_PARAM_CIPHER)<UTF8 string>β forRANDimplementations that use an underlying computation MAC, digest or cipher, these parameters set what the algorithm should be.
The value is always the name of the intended algorithm, or the properties in the case of OSSL_RAND_PARAM_PROPERTIES. algorithm name or properties, no in-between π«
πΈ NOTES πΈ
Okay so real talk, bestie: passing a nonzero prediction_resistance into EVP_RAND_instantiate(), EVP_RAND_generate() or EVP_RAND_reseed() is a sometimes food β use it sparingly π
. In the default setup, that move makes every public and private DRBG reseed on next use. And since public and private DRBGs are allocated on a per thread basis by default, this can lowkey create significant overhead for highly multi-threaded applications π. For normal use-cases, the default "reseed_requests" and "reseed_time_interval" thresholds already keep enough prediction resistance over time, and you can reduce those values if you think theyβre too high. Explicitly asking for prediction resistance is intended for more special use-cases, like generating long-term secrets β¨.
An EVP_RAND_CTX needs locking enabled if it acts as the parent of more than one child and the children can be accessed concurrently. This must be done by explicitly calling EVP_RAND_enable_locking() β no cap, not automatic π«Ά.
The RAND life-cycle is described in life_cycle-rand(7). In the future, the transitions described there will be enforced. When this is done, it will not be considered a breaking change to the API.
π RETURN VALUES π
Hereβs the vibe check on what everything hands back π
EVP_RAND_fetch()β returns a pointer to a newly fetchedEVP_RAND, orNULLif allocation failed.EVP_RAND_get0_provider()β returns a pointer to the provider for the RAND, orNULLon error.EVP_RAND_CTX_get0_rand()β returns a pointer to theEVP_RANDassociated with the context.EVP_RAND_get0_name()β returns the name of the random number generation algorithm.EVP_RAND_up_ref()β returns1on success,0on error.EVP_RAND_names_do_all()β returns1if the callback was called for all names. A return value of0means that the callback was not called for any names.EVP_RAND_CTX_new()β returns either the newly allocatedEVP_RAND_CTXstructure orNULLif an error occurred.EVP_RAND_CTX_free()β does not return a value.EVP_RAND_CTX_up_ref()β returns1on success,0on error.EVP_RAND_nonce()β returns1on success,0on error.EVP_RAND_get_strength()β returns the strength of the random number generator in bits.EVP_RAND_gettable_params(),EVP_RAND_gettable_ctx_params()andEVP_RAND_settable_ctx_params()β return an array ofOSSL_PARAMs.EVP_RAND_verify_zeroization()β returns1if the internal DRBG state is currently zeroed, and0if not.- The remaining functions β return
1for success and0or a negative value for failure.
π SEE ALSO π
Go peek at these icons, iykyk:
RAND_bytes(3)EVP_RAND-CTR-DRBG(7)EVP_RAND-HASH-DRBG(7)EVP_RAND-HMAC-DRBG(7)EVP_RAND-TEST-RAND(7)provider-rand(7)life_cycle-rand(7)
π¦ HISTORY π¦
EVP_RAND_CTX_up_ref() was added in OpenSSL 3.1.
The remaining functions were added in OpenSSL 3.0.
π COPYRIGHT π
Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved.
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>.
later bestie π
β source/version is OpenSSL 3.5.6, dated 2026-04-07, and the manpage tag is EVP_RAND(3ossl) β¨.
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms