EVP_RAND(7SSL)

๐Ÿ’– NAME ๐Ÿ’–

EVP_RAND โ€” the random bit generator ๐ŸŽฒโœจ (yes bestie, this is the OpenSSL page EVP_RAND(7SSL), sit down, we're learning)


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <openssl/evp.h>
#include <rand.h>

โœจ DESCRIPTION โœจ

Okay so. The default OpenSSL RAND method is built on the EVP_RAND classes, and its whole job is feeding non-deterministic inputs into all the other crypto algorithms. It's the โœจsauceโœจ behind everything.

Now here's the vibe split, and it's important:

  • The RAND API is the 'frontend' โ€” that's the one you, the application developer, are supposed to use when you just want random bytes.
  • The EVP_RAND API is the 'backend' โ€” it's the one hooking the frontend up to the operating system's entropy sources and giving you access to deterministic random bit generators (DRBG) plus all their config parameters.

And what even is a DRBG? It's a certain type of cryptographically-secure pseudo-random number generator (CSPRNG), described in [NIST SP 800-90A Rev. 1]. Iconic government document behaviour, no notes. ๐Ÿ“„๐Ÿ‘‘

๐Ÿซถ Disclaimer ๐Ÿซถ

Okay listen. Unless you have very specific requirements for your random generator, you genuinely do not need to touch the EVP_RAND API directly. Like, at all. The usual way to get random bytes is RAND_bytes(3) or RAND_priv_bytes(3) โ€” see also RAND(7). That's it, that's the move, go in peace. ๐Ÿ™

๐ŸŒˆ Typical Use Cases ๐ŸŒˆ

So when would you actually need this? Typical examples of these special use cases are the following:

  • You want to use your own private DRBG instances. ๐Ÿ’… Multiple DRBG instances that are only ever accessed by a single thread give you extra security (because their internal states are independent) and better scalability in multithreaded applications (because they don't need to be locked). No lock contention, no drama, we love to see it.
  • You need to integrate a previously unsupported entropy source. ๐ŸŒฑ Go read provider-rand(7) for the implementation details on how to support adding randomness sources to EVP_RAND.
  • You need to change the default settings of the standard OpenSSL RAND implementation to meet specific requirements. โš™๏ธ Customise her, bestie.

๐Ÿ”— EVP_RAND CHAINING ๐Ÿ”—

An EVP_RAND instance can be used as the entropy source of another EVP_RAND instance โ€” as long as it has access to a valid entropy source itself. Chain 'em up, babes. ๐Ÿงš

Naming time, because we're cute AND organised:

  • The EVP_RAND instance acting as the entropy source is the parent.
  • The other instance is the child.

Typically the child will be a DRBG, because honestly it makes no sense for the child to be an entropy source. Like, why would you.

This whole setup is called chaining. A chained EVP_RAND instance is created by passing a pointer to the parent EVP_RAND_CTX as argument to the EVP_RAND_CTX_new() call. And yes, you can create chains of more than two DRBGs in a row. ๐Ÿš‚ Also possible: using any EVP_RAND_CTX class as the parent โ€” however, only a live entropy source may ignore and not use its parent.


๐Ÿ‘‘ THE THREE SHARED DRBG INSTANCES ๐Ÿ‘‘

Currently, there are three shared DRBG instances: the <primary>, <public>, and <private> DRBG. And here's the tea:

  • <primary> is a single global instance.
  • <public> and <private> are created per thread and accessed through thread-local storage. ๐ŸŒฑ

By default, the functions RAND_bytes(3) and RAND_priv_bytes(3) use the thread-local <public> and <private> DRBG instance, respectively.

โญ The <primary> DRBG instance โญ

The <primary> DRBG is not used directly by the application โ€” it exists only for reseeding the other two DRBG instances. It reseeds itself by obtaining randomness either from os entropy sources, or by consuming randomness that was added previously by RAND_add(3). She's the backstage queen, never on stage. ๐Ÿ’œ

๐ŸŒธ The <public> DRBG instance ๐ŸŒธ

This instance is used per default by RAND_bytes(3).

๐ŸŽ€ The <private> DRBG instance ๐ŸŽ€

This instance is used per default by RAND_priv_bytes(3)


๐Ÿ”’ LOCKING ๐Ÿ”’

The <primary> DRBG is intended to be accessed concurrently for reseeding by its child DRBG instances. The necessary locking is done internally. ๐Ÿ‘‘ But check this out:

It is not thread-safe to access the <primary> DRBG directly via the EVP_RAND interface. Don't do it. I'm serious. ๐Ÿšจ

The <public> and <private> DRBG are thread-local, i.e. there is an instance of each per thread. So they can safely be accessed without locking via the EVP_RAND interface. ๐Ÿฅณ

Pointers to these DRBG instances can be obtained using RAND_get0_primary(), RAND_get0_public() and RAND_get0_private(), respectively. Note that it is not allowed to store a pointer to one of the thread-local DRBG instances in a variable or other memory location where it will be accessed and used by multiple threads. That's a full-blown violation bestie, don't.

All other DRBG instances created by an application don't support locking, because they are intended to be used by a single thread. So instead of accessing a single DRBG instance concurrently from different threads, it is recommended to instantiate a separate DRBG instance per thread. ๐Ÿ’…

Using the <primary> DRBG as entropy source for multiple DRBG instances on different threads is thread-safe, because the DRBG instance will lock the <primary> DRBG automatically for obtaining random input. Automatic. Effortless. Iconic. โœจ


๐Ÿ–ผ๏ธ THE OVERALL PICTURE ๐Ÿ–ผ๏ธ

The following picture gives an overview over how the DRBG instances work together and are being used:

               +--------------------+
               | os entropy sources |
               +--------------------+
                        |
                        v           +-----------------------------+
     RAND_add() ==> <primary>     <-| shared DRBG (with locking)  |
                      /   \         +-----------------------------+
                     /     \              +---------------------------+
              <public>     <private>   <- | per-thread DRBG instances |
                 |             |          +---------------------------+
                 v             v
               RAND_bytes()   RAND_priv_bytes()
                    |               ^
                    |               |
    +------------------+      +------------------------------------+
    | general purpose  |      | used for secrets like session keys |
    | random generator |      | and private keys for certificates  |
    +------------------+      +------------------------------------+

The usual way to obtain random bytes is to call RAND_bytes(...) or RAND_priv_bytes(...). These calls are roughly equivalent to calling EVP_RAND_generate(<public>, ...) and EVP_RAND_generate(<private>, ...), respectively. ๐Ÿซถ

๐Ÿ’– RESEEDING ๐Ÿ’–

okay so bestie, here's the tea: a DRBG instance seeds itself automatically, pulling random input straight from its entropy source. and that entropy source can be either a trusted operating system entropy source, or another DRBG that has access to such a source. cute, right? ๐ŸŽ€

Automatic reseeding happens after a predefined number of generate requests. the selection of the trusted entropy sources is configured at build time using the --with-rand-seed option. the following sections explain the reseeding process in more detail. buckle up ๐Ÿ’…

๐ŸŒธ Automatic Reseeding ๐ŸŒธ

before it satisfies a generate request (EVP_RAND_generate(3)), the DRBG reseeds itself automatically if one of these conditions holds:

  • the DRBG was not instantiated (=seeded) yet or has been uninstantiated. ๐Ÿ’€
  • the number of generate requests since the last reseeding exceeds a certain threshold, the so called reseed_interval. this behaviour can be disabled by setting the reseed_interval to 0.
  • the time elapsed since the last reseeding exceeds a certain time interval, the so called reseed_time_interval. this can be disabled by setting the reseed_time_interval to 0.
  • the DRBG is in an error state.
Note: an error state is entered if the entropy source fails while the DRBG is seeding or reseeding. that last case is the glow-up clause โ€” it makes sure the DRBG automatically recovers from the error as soon as the entropy source is available again. self-healing icon behaviour, fr fr โœจ

๐Ÿฆ‹ Manual Reseeding ๐Ÿฆ‹

besides the automatic thing, the caller can request an immediate reseeding of the DRBG with fresh entropy by setting the prediction resistance parameter to 1 when calling EVP_RAND_generate(3).

the document [NIST SP 800-90C] describes prediction resistance requests in detail and imposes strict conditions on the entropy sources that are approved for providing prediction resistance. a request for prediction resistance can only be satisfied by pulling fresh entropy from a live entropy source (section 5.5.2 of [NIST SP 800-90C]). it is up to the user to ensure that a live entropy source is configured and is being used. no cap, that part is on you ๐Ÿ‘‘

for the three shared DRBGs (and only for these) there is another way to reseed them manually: if RAND_add(3) is called with a positive randomness argument (or RAND_seed(3)), then this will immediately reseed the <primary> DRBG. the <public> and <private> DRBG will detect this on their next generate call and reseed, pulling randomness from <primary>. the way they coordinateโ€ฆ obsessed ๐Ÿ’ž

that last feature was added to support the common practice used with previous OpenSSL versions to call RAND_add() before calling RAND_bytes().

๐Ÿ“ Entropy Input and Additional Data ๐Ÿ“

the DRBG distinguishes two different types of random input: entropy, which comes from a trusted source, and additional input, which can optionally be added by the user and is considered untrusted. it is possible to add additional input not only during reseeding, but also for every generate request. so yeah, both vibes are always welcome โœจ

๐ŸŒˆ Configuring the Random Seed Source ๐ŸŒˆ

in most cases OpenSSL will automatically choose a suitable seed source for automatically seeding and reseeding its <primary> DRBG. the default seed source can be configured when OpenSSL is compiled by setting -DOPENSSL_DEFAULT_SEED_SRC=SEED-SRC. if not set then "SEED-SRC" is used. you can specify a third-party provider seed-source, or -DOPENSSL_DEFAULT_SEED_SRC=JITTER if available. options bestie, we love options ๐Ÿ’…

in some cases however, it will be necessary to explicitly specify a seed source used by "SEED-SRC" during configuration, using the --with-rand-seed option. for more information, see the INSTALL instructions. there are also operating systems where no seed source is available and automatic reseeding is disabled by default. ๐Ÿ™ˆ

the following two sections describe the reseeding process of the primary DRBG, depending on whether automatic reseeding is available or not.

๐Ÿ’œ Reseeding the primary DRBG with automatic seeding enabled ๐Ÿ’œ

calling RAND_poll() or RAND_add() is not necessary, because the DRBG pulls the necessary entropy from its source automatically. however, both calls are permitted, and do reseed the RNG. so like, optional but valid ๐Ÿซถ

RAND_add() can be used to add both kinds of random input, depending on the value of the randomness argument:

  • randomness == 0: the random bytes are mixed as additional input into the current state of the DRBG. mixing in additional input is not considered a full reseeding, hence the reseed counter is not reset.
  • randomness > 0: the random bytes are used as entropy input for a full reseeding (resp. reinstantiation) if the DRBG is instantiated (resp. uninstantiated or in an error state). the number of random bits required for reseeding is determined by the security strength of the DRBG. currently it defaults to 256 bits (32 bytes). it is possible to provide less randomness than required. in this case the missing randomness will be obtained by pulling random input from the trusted entropy sources.
NOTE: manual reseeding is not allowed in FIPS mode, because [NIST SP-800-90Ar1] mandates that entropy shall not be provided by the consuming application for instantiation (Section 9.1) or reseeding (Section 9.2). for that reason, the randomness argument is ignored and the random bytes provided by the RAND_add(3) and RAND_seed(3) calls are treated as additional data. yeah that's a whole situation, don't fight it ๐Ÿ’€

โญ Reseeding the primary DRBG with automatic seeding disabled โญ

calling RAND_poll() will always fail.

RAND_add() needs to be called for initial seeding and periodic reseeding. at least 48 bytes (384 bits) of randomness have to be provided, otherwise the (re-)seeding of the DRBG will fail. this corresponds to one and a half times the security strength of the DRBG. the extra half is used for the nonce during instantiation. one and a half times, hello?? math slay ๐Ÿงฎโœจ

more precisely, the number of bytes needed for seeding depend on the security strength of the DRBG, which is set to 256 by default.


๐ŸŽ€ SEE ALSO ๐ŸŽ€

RAND(7), EVP_RAND(3)

๐ŸŒŸ HISTORY ๐ŸŒŸ

this functionality was added in OpenSSL 3.0.

Copyright 2017-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>.


aaaaand that's a wrap bestie ๐Ÿซถ this whole thing is straight from OpenSSL 4.0.3, dated 2026-09-29, manpage EVP_RAND(7SSL). go forth and be random, but like, securely ๐Ÿ’…๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from