# phpman > man > provider-rand(7ssl)

[PROVIDER-RAND(7SSL)](https://www.chedong.com/phpMan.php/man/PROVIDER-RAND/7SSL/markdown)                            OpenSSL                           [PROVIDER-RAND(7SSL)](https://www.chedong.com/phpMan.php/man/PROVIDER-RAND/7SSL/markdown)



## NAME
       provider-rand - The random number generation library <-> provider functions

## SYNOPSIS
        #include <openssl/core_dispatch.h>
        #include <openssl/core_names.h>

        /*
         * None of these are actual functions, but are displayed like this for
         * the function signatures for functions that are offered as function
         * pointers in OSSL_DISPATCH arrays.
         */

        /* Context management */
        void *OSSL_FUNC_rand_newctx(void *provctx, void *parent,
                                    const OSSL_DISPATCH *parent_calls);
        void OSSL_FUNC_rand_freectx(void *ctx);

        /* Random number generator functions: NIST */
        int OSSL_FUNC_rand_instantiate(void *ctx, unsigned int strength,
                                       int prediction_resistance,
                                       const unsigned char *pstr, size_t pstr_len,
                                       const OSSL_PARAM params[]);
        int OSSL_FUNC_rand_uninstantiate(void *ctx);
        int OSSL_FUNC_rand_generate(void *ctx, unsigned char *out, size_t outlen,
                                    unsigned int strength, int prediction_resistance,
                                    const unsigned char *addin, size_t addin_len);
        int OSSL_FUNC_rand_reseed(void *ctx, int prediction_resistance,
                                  const unsigned char *ent, size_t ent_len,
                                  const unsigned char *addin, size_t addin_len);

        /* Random number generator functions: additional */
        size_t OSSL_FUNC_rand_nonce(void *ctx, unsigned char *out, size_t outlen,
                                    int strength, size_t min_noncelen,
                                    size_t max_noncelen);
        size_t OSSL_FUNC_rand_get_seed(void *ctx, unsigned char **buffer,
                                       int entropy, size_t min_len, size_t max_len,
                                       int prediction_resistance,
                                       const unsigned char *adin, size_t adin_len);
        void OSSL_FUNC_rand_clear_seed(void *ctx, unsigned char *buffer, size_t b_len);
        int OSSL_FUNC_rand_verify_zeroization(void *ctx);

        /* Context Locking */
        int OSSL_FUNC_rand_enable_locking(void *ctx);
        int OSSL_FUNC_rand_lock(void *ctx);
        void OSSL_FUNC_rand_unlock(void *ctx);

        /* RAND parameter descriptors */
        const OSSL_PARAM *OSSL_FUNC_rand_gettable_params(void *provctx);
        const OSSL_PARAM *OSSL_FUNC_rand_gettable_ctx_params(void *ctx, void *provctx);
        const OSSL_PARAM *OSSL_FUNC_rand_settable_ctx_params(void *ctx, void *provctx);

        /* RAND parameters */
        int OSSL_FUNC_rand_get_params(OSSL_PARAM params[]);
        int OSSL_FUNC_rand_get_ctx_params(void *ctx, OSSL_PARAM params[]);
        int OSSL_FUNC_rand_set_ctx_params(void *ctx, const OSSL_PARAM params[]);

## DESCRIPTION
       This documentation is primarily aimed at provider authors. See [**provider**(7)](https://www.chedong.com/phpMan.php/man/provider/7/markdown) for further
       information.

       The RAND operation enables providers to implement random number generation algorithms and
       random number sources and make them available to applications via the API function
       **EVP**___**[RAND**(3)](https://www.chedong.com/phpMan.php/man/RAND/3/markdown).

### Context Management Functions
       **OSSL**___**FUNC**___**rand**___**newctx()** should create and return a pointer to a provider side structure for
       holding context information during a rand operation.  A pointer to this context will be
       passed back in a number of the other rand operation function calls.  The parameter _provctx_ is
       the provider context generated during provider initialisation (see [**provider**(7)](https://www.chedong.com/phpMan.php/man/provider/7/markdown)).  The
       parameter _parent_ specifies another rand instance to be used for seeding purposes.  If NULL
       and the specific instance supports it, the operating system will be used for seeding.  The
       parameter _parent_calls_ points to the dispatch table for _parent_.  Thus, the parent need not be
       from the same provider as the new instance.

       **OSSL**___**FUNC**___**rand**___**freectx()** is passed a pointer to the provider side rand context in the _mctx_
       parameter.  If it receives NULL as _ctx_ value, it should not do anything other than return.
       This function should free any resources associated with that context.

### Random Number Generator Functions: NIST
       These functions correspond to those defined in NIST SP 800-90A and SP 800-90C.

       **OSSL**___**FUNC**___**rand**___**instantiate()** is used to instantiate the DRBG _ctx_ at a requested security
       _strength_.  In addition, _prediction_resistance_ can be requested.  Additional input _addin_ of
       length _addin_len_ bytes can optionally be provided.  The parameters specified in _params_
       configure the DRBG and these should be processed before instantiation.

       **OSSL**___**FUNC**___**rand**___**uninstantiate()** is used to uninstantiate the DRBG _ctx_.  After being
       uninstantiated, a DRBG is unable to produce output until it is instantiated anew.

       **OSSL**___**FUNC**___**rand**___**generate()** is used to generate random bytes from the DRBG _ctx_.  It will
       generate _outlen_ bytes placing them into the buffer pointed to by _out_.  The generated bytes
       will meet the specified security _strength_ and, if _prediction_resistance_ is true, the bytes
       will be produced after reseeding from a live entropy source.  Additional input _addin_ of
       length _addin_len_ bytes can optionally be provided.

### Random Number Generator Functions: Additional
       **OSSL**___**FUNC**___**rand**___**nonce()** is used to generate a nonce of the given _strength_ with a length from
       _min_noncelen_ to _max_noncelen_. If the output buffer _out_ is NULL, the length of the nonce
       should be returned.

       **OSSL**___**FUNC**___**rand**___**get**___**seed()** is used by deterministic generators to obtain their seeding
       material from their parent.  The seed bytes will meet the specified security level of _entropy_
       bits and there will be between _min_len_ and _max_len_ inclusive bytes in total.  If
       _prediction_resistance_ is true, the bytes will be produced from a live entropy source.
       Additional input _addin_ of length _addin_len_ bytes can optionally be provided.  A pointer to
       the seed material is returned in _*buffer_ and this must be freed by a later call to
       **OSSL**___**FUNC**___**rand**___**clear**___**seed()**.

       **OSSL**___**FUNC**___**rand**___**clear**___**seed()** frees a seed _buffer_ of length _b_len_ bytes which was previously
       allocated by **OSSL**___**FUNC**___**rand**___**get**___**seed()**.

       **OSSL**___**FUNC**___**rand**___**verify**___**zeroization()** is used to determine if the internal state of the DRBG is
       zero.  This capability is mandated by NIST as part of the self tests, it is unlikely to be
       useful in other circumstances.

### Context Locking
       When DRBGs are used by multiple threads, there must be locking employed to ensure their
       proper operation.  Because locking introduces an overhead, it is disabled by default.

       **OSSL**___**FUNC**___**rand**___**enable**___**locking()** allows locking to be turned on for a DRBG and all of its
       parent DRBGs.  From this call onwards, the DRBG can be used in a thread safe manner.

       **OSSL**___**FUNC**___**rand**___**lock()** is used to lock a DRBG.  Once locked, exclusive access is guaranteed.

       **OSSL**___**FUNC**___**rand**___**unlock()** is used to unlock a DRBG.

### Rand Parameters
       See **OSSL**___**[PARAM**(3)](https://www.chedong.com/phpMan.php/man/PARAM/3/markdown) for further details on the parameters structure used by these functions.

       **OSSL**___**FUNC**___**rand**___**get**___**params()** gets details of parameter values associated with the provider
       algorithm and stores them in _params_.

       **OSSL**___**FUNC**___**rand**___**set**___**ctx**___**params()** sets rand parameters associated with the given provider side
       rand context _ctx_ to _params_.  Any parameter settings are additional to any that were
       previously set.  Passing NULL for _params_ should return true.

       **OSSL**___**FUNC**___**rand**___**get**___**ctx**___**params()** gets details of currently set parameter values associated
       with the given provider side rand context _ctx_ and stores them in _params_.  Passing NULL for
       _params_ should return true.

       **OSSL**___**FUNC**___**rand**___**gettable**___**params()**, **OSSL**___**FUNC**___**rand**___**gettable**___**ctx**___**params()**, and
       **OSSL**___**FUNC**___**rand**___**settable**___**ctx**___**params()** all return constant **OSSL**___**PARAM** arrays as descriptors of
       the parameters that **OSSL**___**FUNC**___**rand**___**get**___**params()**, **OSSL**___**FUNC**___**rand**___**get**___**ctx**___**params()**, and
       **OSSL**___**FUNC**___**rand**___**set**___**ctx**___**params()** can handle, respectively.
       **OSSL**___**FUNC**___**rand**___**gettable**___**ctx**___**params()** and **OSSL**___**FUNC**___**rand**___**settable**___**ctx**___**params()** will return the
       parameters associated with the provider side context _ctx_ in its current state if it is not
       NULL.  Otherwise, they return the parameters associated with the provider side algorithm
       _provctx_.

       Parameters currently recognised by built-in rands are as follows. Not all parameters are
       relevant to, or are understood by all rands:

       "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.

       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 set the number of generate requests before reseeding the associated RAND ctx.

       "reseed_time_interval" (**OSSL**___**DRBG**___**PARAM**___**RESEED**___**TIME**___**INTERVAL**) <integer>
           Reads or set the number of elapsed seconds before reseeding the associated RAND ctx.

       "max_request" (**OSSL**___**DRBG**___**PARAM**___**RESEED**___**REQUESTS**) <unsigned integer>
           Specifies the maximum number of bytes that can be generated in a single call to
           OSSL_FUNC_rand_generate.

       "min_entropylen" (**OSSL**___**DRBG**___**PARAM**___**MIN**___**ENTROPYLEN**) <unsigned integer>
       "max_entropylen" (**OSSL**___**DRBG**___**PARAM**___**MAX**___**ENTROPYLEN**) <unsigned integer>
           Specify the minimum and maximum number of bytes of random material that can be used to
           seed the DRBG.

       "min_noncelen" (**OSSL**___**DRBG**___**PARAM**___**MIN**___**NONCELEN**) <unsigned integer>
       "max_noncelen" (**OSSL**___**DRBG**___**PARAM**___**MAX**___**NONCELEN**) <unsigned integer>
           Specify the minimum and maximum number of bytes of nonce that can be used to instantiate
           the DRBG.

       "max_perslen" (**OSSL**___**DRBG**___**PARAM**___**MAX**___**PERSLEN**) <unsigned integer>
       "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.

       "digest" (**OSSL**___**DRBG**___**PARAM**___**DIGEST**) <UTF8 string>
       "cipher" (**OSSL**___**DRBG**___**PARAM**___**CIPHER**) <UTF8 string>
       "mac" (**OSSL**___**DRBG**___**PARAM**___**MAC**) <UTF8 string>
           Sets the name of the underlying cipher, digest or MAC to be used.  It must name a
           suitable algorithm for the DRBG that's being used.

       "properties" (**OSSL**___**DRBG**___**PARAM**___**PROPERTIES**) <UTF8 string>
           Sets the properties to be queried when trying to fetch an underlying algorithm.  This
           must be given together with the algorithm naming parameter to be considered valid.

## RETURN VALUES
       **OSSL**___**FUNC**___**rand**___**newctx()** should return the newly created provider side rand context, or NULL
       on failure.

       **OSSL**___**FUNC**___**rand**___**gettable**___**params()**, **OSSL**___**FUNC**___**rand**___**gettable**___**ctx**___**params()** and
       **OSSL**___**FUNC**___**rand**___**settable**___**ctx**___**params()** should return a constant **OSSL**___**PARAM** array, or NULL if
       none is offered.

       **OSSL**___**FUNC**___**rand**___**nonce()** returns the size of the generated nonce, or 0 on error.

       **OSSL**___**FUNC**___**rand**___**get**___**seed()** returns the size of the generated seed, or 0 on error.

       All of the remaining functions should return 1 for success or 0 on error.

## NOTES
       The RAND life-cycle is described in **life**___**[cycle-rand**(7)](https://www.chedong.com/phpMan.php/man/cycle-rand/7/markdown).  Providers should ensure that the
       various transitions listed there are supported.  At some point the EVP layer will begin
       enforcing the listed transitions.

## SEE ALSO
       [**provider**(7)](https://www.chedong.com/phpMan.php/man/provider/7/markdown), [**RAND**(7)](https://www.chedong.com/phpMan.php/man/RAND/7/markdown), **EVP**___**[RAND**(7)](https://www.chedong.com/phpMan.php/man/RAND/7/markdown), **life**___**[cycle-rand**(7)](https://www.chedong.com/phpMan.php/man/cycle-rand/7/markdown), **EVP**___**[RAND**(3)](https://www.chedong.com/phpMan.php/man/RAND/3/markdown)

## HISTORY
       The provider RAND interface was introduced in OpenSSL 3.0.

## COPYRIGHT
       Copyright 2020-2021 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>>.



3.0.2                                        2026-06-02                          [PROVIDER-RAND(7SSL)](https://www.chedong.com/phpMan.php/man/PROVIDER-RAND/7SSL/markdown)
