RAND_GET0_PRIMARY(3ossl)
β¨ RAND_GET0_PRIMARY β¨
okay so this is an OpenSSL manual page, bestie β it lives in section 3ossl of the OpenSSL docs, and it's literally all about the global EVP_RAND_CTX instances. grab a lil snack, we're getting into the random number generation lore π
πΈ NAME πΈ
RAND_get0_primary, RAND_get0_public, RAND_get0_private, RAND_set0_public, RAND_set0_private β these are the girlypops that give you access to the global EVP_RAND_CTX instances. that's the whole job. slay π
π¦ SYNOPSIS π¦
#include <openssl/rand.h>
EVP_RAND_CTX *RAND_get0_primary(OSSL_LIB_CTX *ctx);
EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx);
EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx);
int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand);
int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand);
π DESCRIPTION π
so the default RAND API implementation β that's RAND_OpenSSL() β utilizes three shared DRBG instances, and you reach all three of them through the RAND API. three! a whole trio! it's giving main-character ensemble cast π
- the public and private DRBG are thread-local instances, and they're used by
RAND_bytes()andRAND_priv_bytes()respectively. each thread gets its own, iconic behaviour honestly. - the primary DRBG is a global instance. it is not intended to be used directly β no cap β it's used internally to reseed the other two instances. it's the backstage crew keeping everything running π«Ά
the three get functions provide access to the shared DRBG instances.
the two set functions allow the public and private DRBG instances to be replaced by another random number generator. swap her out, bestie β¨
β RETURN VALUES β
RAND_get0_primary()returns a pointer to the primary DRBG instance for the givenOSSL_LIB_CTX ctx.RAND_get0_public()returns a pointer to the public DRBG instance for the givenOSSL_LIB_CTX ctx.RAND_get0_private()returns a pointer to the private DRBG instance for the givenOSSL_LIB_CTX ctx.RAND_set0_public()andRAND_set0_private()return1on success and0on error.
one for the girls, one for the pointer-retrievers, one for the boolean-checkers π
π§ NOTES π§
okay this is the part where you listen, bestie β
it is not thread-safe to access the primary DRBG instance. π¨
the public and private DRBG instances can be accessed safely, because they're thread-local. but heads up: changes to these two instances apply only to the current thread. it's giving "this stays in this group chat" energy π
for that reason, it's recommended not to change the settings of these three instances directly. instead, an application should change the default settings for new DRBG instances at initialization time, before creating additional threads. timing is everything, lowkey π
during initialization, it's possible to change the reseed interval and the reseed time interval. it's also possible to exchange the reseeding callbacks entirely. full glow-up of the callback situation π
to set the type of DRBG that will be instantiated, use the RAND_set_DRBG_type(3) call before accessing the random number generation infrastructure. order matters, fr fr.
the two set functions operate on the current thread. so if you want to use the same random number generator across all threads, each thread must individually call the set functions. every single one. no shortcuts, no "but I already did it once" π
π₯ SEE ALSO π₯
π HISTORY π
RAND_set0_public()andRAND_set0_private()were added in OpenSSL3.1. the new girls on the block β¨- the remaining functions were added in OpenSSL
3.0. the OGs π
π COPYRIGHT π
Copyright 2020-2024 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>.
and that's the whole page, bestie β mwah π this lil doc is from version 3.5.0, dated 2026-09-25, and it's RAND_GET0_PRIMARY(3ossl) all the way down. go forth and reseed responsibly π¦π
π¦ where this page comes from
- opensuse-leap 16.0: openssl-3-doc 3.5.0-160000.11.1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms