5 provider-rand - The random number generation library E<lt>-E<gt> provider
10 =for openssl multiple includes
12 #include <openssl/core_dispatch.h>
13 #include <openssl/core_names.h>
16 * None of these are actual functions, but are displayed like this for
17 * the function signatures for functions that are offered as function
18 * pointers in OSSL_DISPATCH arrays.
21 /* Context management */
22 void *OSSL_FUNC_rand_newctx(void *provctx, void *parent,
23 const OSSL_DISPATCH *parent_calls);
24 void OSSL_FUNC_rand_freectx(void *ctx);
26 /* Random number generator functions: NIST */
27 int OSSL_FUNC_rand_instantiate(void *ctx, unsigned int strength,
28 int prediction_resistance,
29 const unsigned char *pstr, size_t pstr_len);
30 int OSSL_FUNC_rand_uninstantiate(void *ctx);
31 int OSSL_FUNC_rand_generate(void *ctx, unsigned char *out, size_t outlen,
32 unsigned int strength, int prediction_resistance,
33 const unsigned char *addin, size_t addin_len);
34 int OSSL_FUNC_rand_reseed(void *ctx, int prediction_resistance,
35 const unsigned char *ent, size_t ent_len,
36 const unsigned char *addin, size_t addin_len);
38 /* Random number generator functions: additional */
39 size_t OSSL_FUNC_rand_nonce(void *ctx, unsigned char *out, size_t outlen,
40 int strength, size_t min_noncelen, size_t max_noncelen);
41 int OSSL_FUNC_rand_verify_zeroization(void *ctx);
44 int OSSL_FUNC_rand_enable_locking(void *ctx);
45 int OSSL_FUNC_rand_lock(void *ctx);
46 void OSSL_FUNC_rand_unlock(void *ctx);
48 /* RAND parameter descriptors */
49 const OSSL_PARAM *OSSL_FUNC_rand_gettable_params(void *provctx);
50 const OSSL_PARAM *OSSL_FUNC_rand_gettable_ctx_params(void *provctx);
51 const OSSL_PARAM *OSSL_FUNC_rand_settable_ctx_params(void *provctx);
54 int OSSL_FUNC_rand_get_params(OSSL_PARAM params[]);
55 int OSSL_FUNC_rand_get_ctx_params(void *ctx, OSSL_PARAM params[]);
56 int OSSL_FUNC_rand_set_ctx_params(void *ctx, const OSSL_PARAM params[]);
60 This documentation is primarily aimed at provider authors. See L<provider(7)>
61 for further information.
63 The RAND operation enables providers to implement random number generation
64 algorithms and random number sources and make
65 them available to applications via the API function L<EVP_RAND(3)>.
67 =head2 Context Management Functions
69 OSSL_FUNC_rand_newctx() should create and return a pointer to a provider side
70 structure for holding context information during a rand operation.
71 A pointer to this context will be passed back in a number of the other rand
72 operation function calls.
73 The parameter I<provctx> is the provider context generated during provider
74 initialisation (see L<provider(7)>).
75 The parameter I<parent> specifies another rand instance to be used for
76 seeding purposes. If NULL and the specific instance supports it, the
77 operating system will be used for seeding.
78 The parameter I<parent_calls> points to the dispatch table for I<parent>.
79 Thus, the parent need not be from the same provider as the new instance.
81 OSSL_FUNC_rand_freectx() is passed a pointer to the provider side rand context in
82 the I<mctx> parameter.
83 If it receives NULL as I<ctx> value, it should not do anything other than
85 This function should free any resources associated with that context.
87 =head2 Random Number Generator Functions: NIST
89 These functions correspond to those defined in NIST SP 800-90A and SP 800-90C.
91 OSSL_FUNC_rand_instantiate() is used to instantiate the DRBG I<ctx> at a requested
92 security I<strength>. In addition, I<prediction_resistance> can be requested.
93 Additional input I<addin> of length I<addin_len> bytes can optionally
96 OSSL_FUNC_rand_uninstantiate() is used to uninstantiate the DRBG I<ctx>. After being
97 uninstantiated, a DRBG is unable to produce output until it is instantiated
100 OSSL_FUNC_rand_generate() is used to generate random bytes from the DRBG I<ctx>.
101 It will generate I<outlen> bytes placing them into the buffer pointed to by
102 I<out>. The generated bytes will meet the specified security I<strength> and,
103 if I<prediction_resistance> is true, the bytes will be produced after reseeding
104 from a live entropy source. Additional input I<addin> of length I<addin_len>
105 bytes can optionally be provided.
107 =head2 Random Number Generator Functions: Additional
109 OSSL_FUNC_rand_nonce() is used to generate a nonce of the given I<strength> with a
110 length from I<min_noncelen> to I<max_noncelen>. If the output buffer I<out> is
111 NULL, the length of the nonce should be returned.
113 OSSL_FUNC_rand_verify_zeroization() is used to determine if the internal state of the
114 DRBG is zero. This capability is mandated by NIST as part of the self
115 tests, it is unlikely to be useful in other circumstances.
117 =head2 Context Locking
119 When DRBGs are used by multiple threads, there must be locking employed to
120 ensure their proper operation. Because locking introduces an overhead, it
121 is disabled by default.
123 OSSL_FUNC_rand_enable_locking() allows locking to be turned on for a DRBG and all of
124 its parent DRBGs. From this call onwards, the DRBG can be used in a thread
127 OSSL_FUNC_rand_lock() is used to lock a DRBG. Once locked, exclusive access
130 OSSL_FUNC_rand_unlock() is used to unlock a DRBG.
132 =head2 Rand Parameters
134 See L<OSSL_PARAM(3)> for further details on the parameters structure used by
137 OSSL_FUNC_rand_get_params() gets details of parameter values associated with the
138 provider algorithm and stores them in I<params>.
140 OSSL_FUNC_rand_set_ctx_params() sets rand parameters associated with the given
141 provider side rand context I<ctx> to I<params>.
142 Any parameter settings are additional to any that were previously set.
144 OSSL_FUNC_rand_get_ctx_params() gets details of currently set parameter values
145 associated with the given provider side rand context I<ctx> and stores them
148 OSSL_FUNC_rand_gettable_params(), OSSL_FUNC_rand_gettable_ctx_params(), and
149 OSSL_FUNC_rand_settable_ctx_params() all return constant B<OSSL_PARAM> arrays
150 as descriptors of the parameters that OSSL_FUNC_rand_get_params(),
151 OSSL_FUNC_rand_get_ctx_params(), and OSSL_FUNC_rand_set_ctx_params() can handle,
154 Parameters currently recognised by built-in rands are as follows. Not all
155 parameters are relevant to, or are understood by all rands:
159 =item "state" (B<OSSL_RAND_PARAM_STATE>) <integer>
161 Returns the state of the random number generator.
163 =item "strength" (B<OSSL_RAND_PARAM_STRENGTH>) <unsigned integer>
165 Returns the bit strength of the random number generator.
169 For rands that are also deterministic random bit generators (DRBGs), these
170 additional parameters are recognised. Not all
171 parameters are relevant to, or are understood by all DRBG rands:
175 =item "reseed_requests" (B<OSSL_DRBG_PARAM_RESEED_REQUESTS>) <unsigned integer>
177 Reads or set the number of generate requests before reseeding the
180 =item "reseed_time_interval" (B<OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL>) <integer>
182 Reads or set the number of elapsed seconds before reseeding the
185 =item "max_request" (B<OSSL_DRBG_PARAM_RESEED_REQUESTS>) <unsigned integer>
187 Specifies the maximum number of bytes that can be generated in a single
188 call to OSSL_FUNC_rand_generate.
190 =item "min_entropylen" (B<OSSL_DRBG_PARAM_MIN_ENTROPYLEN>) <unsigned integer>
192 =item "max_entropylen" (B<OSSL_DRBG_PARAM_MAX_ENTROPYLEN>) <unsigned integer>
194 Specify the minimum and maximum number of bytes of random material that
195 can be used to seed the DRBG.
197 =item "min_noncelen" (B<OSSL_DRBG_PARAM_MIN_NONCELEN>) <unsigned integer>
199 =item "max_noncelen" (B<OSSL_DRBG_PARAM_MAX_NONCELEN>) <unsigned integer>
201 Specify the minimum and maximum number of bytes of nonce that can be used to
202 instantiate the DRBG.
204 =item "max_perslen" (B<OSSL_DRBG_PARAM_MAX_PERSLEN>) <unsigned integer>
206 =item "max_adinlen" (B<OSSL_DRBG_PARAM_MAX_ADINLEN>) <unsigned integer>
208 Specify the minimum and maximum number of bytes of personalisation string
209 that can be used with the DRBG.
211 =item "reseed_counter" (B<OSSL_DRBG_PARAM_RESEED_COUNTER>) <unsigned integer>
213 Specifies the number of times the DRBG has been seeded or reseeded.
215 =item "digest" (B<OSSL_DRBG_PARAM_DIGEST>) <UTF8 string>
217 =item "cipher" (B<OSSL_DRBG_PARAM_CIPHER>) <UTF8 string>
219 =item "mac" (B<OSSL_DRBG_PARAM_MAC>) <UTF8 string>
221 Sets the name of the underlying cipher, digest or MAC to be used.
222 It must name a suitable algorithm for the DRBG that's being used.
224 =item "properties" (B<OSSL_DRBG_PARAM_PROPERTIES>) <UTF8 string>
226 Sets the properties to be queried when trying to fetch an underlying algorithm.
227 This must be given together with the algorithm naming parameter to be
234 OSSL_FUNC_rand_newctx() should return the newly created
235 provider side rand context, or NULL on failure.
237 OSSL_FUNC_rand_gettable_params(), OSSL_FUNC_rand_gettable_ctx_params() and
238 OSSL_FUNC_rand_settable_ctx_params() should return a constant B<OSSL_PARAM>
239 array, or NULL if none is offered.
241 OSSL_FUNC_rand_nonce() returns the size of the generated nonce, or 0 on error.
243 All of the remaining functions should return 1 for success or 0 on error.
253 The provider RAND interface was introduced in OpenSSL 3.0.
257 Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
259 Licensed under the Apache License 2.0 (the "License"). You may not use
260 this file except in compliance with the License. You can obtain a copy
261 in the file LICENSE in the source distribution or at
262 L<https://www.openssl.org/source/license.html>.