]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/man3/EVP_RAND.pod
Rename OPENSSL_CTX prefix to OSSL_LIB_CTX
[thirdparty/openssl.git] / doc / man3 / EVP_RAND.pod
CommitLineData
dc4e74ef
P
1=pod
2
3=head1 NAME
4
5EVP_RAND, EVP_RAND_fetch, EVP_RAND_free, EVP_RAND_up_ref, EVP_RAND_CTX,
6EVP_RAND_CTX_new, EVP_RAND_CTX_free, EVP_RAND_instantiate,
7EVP_RAND_uninstantiate, EVP_RAND_generate, EVP_RAND_reseed,
7d615e21 8EVP_RAND_nonce, EVP_RAND_enable_locking,
dc4e74ef
P
9EVP_RAND_verify_zeroization, EVP_RAND_strength, EVP_RAND_state,
10EVP_RAND_provider, EVP_RAND_CTX_rand, EVP_RAND_is_a, EVP_RAND_number,
11EVP_RAND_name, EVP_RAND_names_do_all, EVP_RAND_get_ctx_params,
12EVP_RAND_set_ctx_params, EVP_RAND_do_all_provided, EVP_RAND_get_params,
13EVP_RAND_gettable_ctx_params, EVP_RAND_settable_ctx_params,
14EVP_RAND_gettable_params, EVP_RAND_STATE_UNINITIALISED, EVP_RAND_STATE_READY,
15EVP_RAND_STATE_ERROR - EVP RAND routines
16
17=head1 SYNOPSIS
18
19 #include <openssl/evp.h>
20
21 typedef struct evp_rand_st EVP_RAND;
22 typedef struct evp_rand_ctx_st EVP_RAND_CTX;
23
b4250010 24 EVP_RAND *EVP_RAND_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
dc4e74ef
P
25 const char *properties);
26 int EVP_RAND_up_ref(EVP_RAND *rand);
27 void EVP_RAND_free(EVP_RAND *rand);
28 EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent);
29 void EVP_RAND_CTX_free(EVP_RAND_CTX *ctx);
30 EVP_RAND *EVP_RAND_CTX_rand(EVP_RAND_CTX *ctx);
31 int EVP_RAND_get_params(EVP_RAND *rand, OSSL_PARAM params[]);
32 int EVP_RAND_get_ctx_params(EVP_RAND_CTX *ctx, OSSL_PARAM params[]);
33 int EVP_RAND_set_ctx_params(EVP_RAND_CTX *ctx, const OSSL_PARAM params[]);
34 const OSSL_PARAM *EVP_RAND_gettable_params(const EVP_RAND *rand);
35 const OSSL_PARAM *EVP_RAND_gettable_ctx_params(const EVP_RAND *rand);
36 const OSSL_PARAM *EVP_RAND_settable_ctx_params(const EVP_RAND *rand);
37 int EVP_RAND_number(const EVP_RAND *rand);
38 const char *EVP_RAND_name(const EVP_RAND *rand);
39 int EVP_RAND_is_a(const EVP_RAND *rand, const char *name);
40 const OSSL_PROVIDER *EVP_RAND_provider(const EVP_RAND *rand);
b4250010 41 void EVP_RAND_do_all_provided(OSSL_LIB_CTX *libctx,
dc4e74ef
P
42 void (*fn)(EVP_RAND *rand, void *arg),
43 void *arg);
44 void EVP_RAND_names_do_all(const EVP_RAND *rand,
45 void (*fn)(const char *name, void *data),
46 void *data);
47
48 int EVP_RAND_instantiate(EVP_RAND_CTX *ctx, unsigned int strength,
49 int prediction_resistance,
50 const unsigned char *pstr, size_t pstr_len);
51 int EVP_RAND_uninstantiate(EVP_RAND_CTX *ctx);
52 int EVP_RAND_generate(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen,
53 unsigned int strength, int prediction_resistance,
54 const unsigned char *addin, size_t addin_len);
55 int EVP_RAND_reseed(EVP_RAND_CTX *ctx, int prediction_resistance,
56 const unsigned char *ent, size_t ent_len,
57 const unsigned char *addin, size_t addin_len);
58 int EVP_RAND_nonce(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen);
59 int EVP_RAND_enable_locking(EVP_RAND_CTX *ctx);
dc4e74ef
P
60 int EVP_RAND_verify_zeroization(EVP_RAND_CTX *ctx);
61 unsigned int EVP_RAND_strength(EVP_RAND_CTX *ctx);
62 int EVP_RAND_state(EVP_RAND_CTX *ctx);
63
64 #define EVP_RAND_STATE_UNINITIALISED 0
65 #define EVP_RAND_STATE_READY 1
66 #define EVP_RAND_STATE_ERROR 2
67
68=head1 DESCRIPTION
69
8c1cbc72 70The EVP RAND routines are a high-level interface to random number generators
dc4e74ef
P
71both deterministic and not.
72If you just want to generate random bytes then you don't need to use
73these functions: just call RAND_bytes() or RAND_priv_bytes().
74If you want to do more, these calls should be used instead of the older
75RAND and RAND_DRBG functions.
76
77After creating a B<EVP_RAND_CTX> for the required algorithm using
78EVP_RAND_CTX_new(), inputs to the algorithm are supplied
79using calls to EVP_RAND_set_ctx_params() before
80calling EVP_RAND_instantiate() and then EVP_RAND_generate() to produce
81cryptographically secure random bytes.
82
83=head2 Types
84
85B<EVP_RAND> is a type that holds the implementation of a RAND.
86
87B<EVP_RAND_CTX> is a context type that holds the algorithm inputs.
4640cd00 88B<EVP_RAND_CTX> structures are reference counted.
dc4e74ef
P
89
90=head2 Algorithm implementation fetching
91
92EVP_RAND_fetch() fetches an implementation of a RAND I<algorithm>, given
93a library context I<libctx> and a set of I<properties>.
94See L<provider(7)/Fetching algorithms> for further information.
95
96The returned value must eventually be freed with
97L<EVP_RAND_free(3)>.
98
99EVP_RAND_up_ref() increments the reference count of an already fetched
100RAND.
101
102EVP_RAND_free() frees a fetched algorithm.
103NULL is a valid parameter, for which this function is a no-op.
104
105=head2 Context manipulation functions
106
107EVP_RAND_CTX_new() creates a new context for the RAND implementation I<rand>.
108If not NULL, I<parent> specifies the seed source for this implementation.
109Not all random number generators need to have a seed source specified.
110If a parent is required, a NULL I<parent> will utilise the operating
111system entropy sources.
112It is recommended to minimise the number of random number generators that
113rely on the operating system for their randomness because this is often scarce.
114
115EVP_RAND_CTX_free() frees up the context I<ctx>. If I<ctx> is NULL, nothing
116is done.
117
118EVP_RAND_CTX_rand() returns the B<EVP_RAND> associated with the context
119I<ctx>.
120
121=head2 Random Number Generator Functions
122
123EVP_RAND_instantiate() instantiates the RAND I<ctx> with a minimum security
124strength of <strength> and personalisation string I<pstr> of length <pstr_len>.
125If I<prediction_resistance> is specified, fresh entropy from a live source
126will be sought. This call operates as per NIST SP 800-90A and SP 800-90C.
127
128EVP_RAND_uninstantiate() uninstantiates the RAND I<ctx> as per
129NIST SP 800-90A and SP 800-90C. Subsequent to this call, the RAND cannot
130be used to generate bytes. It can only be freed or instantiated again.
131
132EVP_RAND_generate() produces random bytes from the RAND I<ctx> with the
133additional input I<addin> of length I<addin_len>. The bytes
134produced will meet the security I<strength>.
135If I<prediction_resistance> is specified, fresh entropy from a live source
136will be sought. This call operates as per NIST SP 800-90A and SP 800-90C.
137
138EVP_RAND_reseed() reseeds the RAND with new entropy.
139Entropy I<ent> of length I<ent_len> bytes can be supplied as can additional
140input I<addin> of length I<addin_len> bytes. In the FIPS provider, both are
141treated as additional input as per NIST SP-800-90Ar1, Sections 9.1 and 9.2.
142Additional seed material is also drawn from the RAND's parent or the
143operating system. If I<prediction_resistance> is specified, fresh entropy
144from a live source will be sought. This call operates as per NIST SP 800-90A
145and SP 800-90C.
146
147EVP_RAND_nonce() creates a nonce in I<out> of maximum length I<outlen>
148bytes from the RAND I<ctx>. The function returns the length of the generated
149nonce. If I<out> is NULL, the length is still returned but no generation
150takes place. This allows a caller to dynamically allocate a buffer of the
151appropriate size.
152
153EVP_RAND_enable_locking() enables locking for the RAND I<ctx> and all of
154its parents. After this I<ctx> will operate in a thread safe manner, albeit
155more slowly.
156
dc4e74ef
P
157EVP_RAND_get_params() retrieves details about the implementation
158I<rand>.
159The set of parameters given with I<params> determine exactly what
160parameters should be retrieved.
161Note that a parameter that is unknown in the underlying context is
162simply ignored.
163
164EVP_RAND_get_ctx_params() retrieves chosen parameters, given the
165context I<ctx> and its underlying context.
166The set of parameters given with I<params> determine exactly what
167parameters should be retrieved.
168Note that a parameter that is unknown in the underlying context is
169simply ignored.
170
171EVP_RAND_set_ctx_params() passes chosen parameters to the underlying
172context, given a context I<ctx>.
173The set of parameters given with I<params> determine exactly what
174parameters are passed down.
175Note that a parameter that is unknown in the underlying context is
176simply ignored.
177Also, what happens when a needed parameter isn't passed down is
178defined by the implementation.
179
180EVP_RAND_gettable_params(), EVP_RAND_gettable_ctx_params() and
181EVP_RAND_settable_ctx_params() get a constant B<OSSL_PARAM> array that
182describes the retrievable and settable parameters, i.e. parameters that
183can be used with EVP_RAND_get_params(), EVP_RAND_get_ctx_params()
184and EVP_RAND_set_ctx_params(), respectively.
185See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as parameter descriptor.
186
187=head2 Information functions
188
189EVP_RAND_strength() returns the security strength of the RAND I<ctx>.
190
191EVP_RAND_state() returns the current state of the RAND I<ctx>.
7d615e21 192States defined by the OpenSSL RNGs are:
dc4e74ef
P
193
194=over 4
195
196=item *
197
7d615e21 198EVP_RAND_STATE_UNINITIALISED: this RNG is currently uninitialised.
dc4e74ef
P
199The instantiate call will change this to the ready state.
200
201=item *
202
7d615e21 203EVP_RAND_STATE_READY: this RNG is currently ready to generate output.
dc4e74ef
P
204
205=item *
206
7d615e21 207EVP_RAND_STATE_ERROR: this RNG is in an error state.
dc4e74ef
P
208
209=back
210
211EVP_RAND_is_a() returns 1 if I<rand> is an implementation of an
212algorithm that's identifiable with I<name>, otherwise 0.
213
214EVP_RAND_provider() returns the provider that holds the implementation
215of the given I<rand>.
216
217EVP_RAND_do_all_provided() traverses all RAND implemented by all activated
218providers in the given library context I<libctx>, and for each of the
219implementations, calls the given function I<fn> with the implementation method
220and the given I<arg> as argument.
221
222EVP_RAND_number() returns the internal dynamic number assigned to
223I<rand>.
224
225EVP_RAND_name() returns the canonical name of I<rand>.
226
227EVP_RAND_names_do_all() traverses all names for I<rand>, and calls
228I<fn> with each name and I<data>.
229
230EVP_RAND_verify_zeroization() confirms if the internal DRBG state is
231currently zeroed. This is used by the FIPS provider to support the mandatory
232self tests.
233
234=head1 PARAMETERS
235
236The standard parameter names are:
237
238=over 4
239
240=item "state" (B<OSSL_RAND_PARAM_STATE>) <integer>
241
242Returns the state of the random number generator.
243
244=item "strength" (B<OSSL_RAND_PARAM_STRENGTH>) <unsigned integer>
245
246Returns the bit strength of the random number generator.
247
248=back
249
250For rands that are also deterministic random bit generators (DRBGs), these
251additional parameters are recognised. Not all
252parameters are relevant to, or are understood by all DRBG rands:
253
254=over 4
255
256=item "reseed_requests" (B<OSSL_DRBG_PARAM_RESEED_REQUESTS>) <unsigned integer>
257
258Reads or set the number of generate requests before reseeding the
259associated RAND ctx.
260
261=item "reseed_time_interval" (B<OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL>) <integer>
262
263Reads or set the number of elapsed seconds before reseeding the
264associated RAND ctx.
265
266=item "max_request" (B<OSSL_DRBG_PARAM_RESEED_REQUESTS>) <unsigned integer>
267
268Specifies the maximum number of bytes that can be generated in a single
363b1e5d 269call to OSSL_FUNC_rand_generate.
dc4e74ef
P
270
271=item "min_entropylen" (B<OSSL_DRBG_PARAM_MIN_ENTROPYLEN>) <unsigned integer>
272
273=item "max_entropylen" (B<OSSL_DRBG_PARAM_MAX_ENTROPYLEN>) <unsigned integer>
274
275Specify the minimum and maximum number of bytes of random material that
276can be used to seed the DRBG.
277
278=item "min_noncelen" (B<OSSL_DRBG_PARAM_MIN_NONCELEN>) <unsigned integer>
279
280=item "max_noncelen" (B<OSSL_DRBG_PARAM_MAX_NONCELEN>) <unsigned integer>
281
282Specify the minimum and maximum number of bytes of nonce that can be used to
283seed the DRBG.
284
285=item "max_perslen" (B<OSSL_DRBG_PARAM_MAX_PERSLEN>) <unsigned integer>
286
287=item "max_adinlen" (B<OSSL_DRBG_PARAM_MAX_ADINLEN>) <unsigned integer>
288
289Specify the minimum and maximum number of bytes of personalisation string
290that can be used with the DRBG.
291
b0614f0a 292=item "reseed_counter" (B<OSSL_DRBG_PARAM_RESEED_COUNTER>) <unsigned integer>
dc4e74ef
P
293
294Specifies the number of times the DRBG has been seeded or reseeded.
295
296=item "properties" (B<OSSL_RAND_PARAM_PROPERTIES>) <UTF8 string>
297
298=item "mac" (B<OSSL_RAND_PARAM_MAC>) <UTF8 string>
299
300=item "digest" (B<OSSL_RAND_PARAM_DIGEST>) <UTF8 string>
301
302=item "cipher" (B<OSSL_RAND_PARAM_CIPHER>) <UTF8 string>
303
304For RAND implementations that use an underlying computation MAC, digest or
305cipher, these parameters set what the algorithm should be.
306
307The value is always the name of the intended algorithm,
308or the properties in the case of B<OSSL_RAND_PARAM_PROPERTIES>.
309
310=back
311
312=head1 RETURN VALUES
313
314EVP_RAND_fetch() returns a pointer to a newly fetched B<EVP_RAND>, or
315NULL if allocation failed.
316
317EVP_RAND_provider() returns a pointer to the provider for the RAND, or
318NULL on error.
319
320EVP_RAND_CTX_rand() returns a pointer to the B<EVP_RAND> associated with the
321context.
322
323EVP_RAND_name() returns the name of the random number generation algorithm.
324
325EVP_RAND_number() returns the provider specific identification number
326for the specified algorithm.
327
328EVP_RAND_up_ref() returns 1 on success, 0 on error.
329
330EVP_RAND_CTX_new() returns either the newly allocated
331B<EVP_RAND_CTX> structure or NULL if an error occurred.
332
333EVP_RAND_CTX_free() does not return a value.
334
335EVP_RAND_nonce() returns the length of the nonce.
336
8c1cbc72 337EVP_RAND_strength() returns the strength of the random number generator in bits.
dc4e74ef
P
338
339EVP_RAND_gettable_params(), EVP_RAND_gettable_ctx_params() and
340EVP_RAND_settable_ctx_params() return an array of OSSL_PARAMs.
341
342EVP_RAND_verify_zeroization() returns 1 if the internal DRBG state is
343currently zeroed, and 0 if not.
344
345The remaining functions return 1 for success and 0 or a negative value for
346failure.
347
348=head1 SEE ALSO
349
350L<RAND_bytes(3)>,
351L<EVP_RAND-CTR-DRBG(7)>,
352L<EVP_RAND-HASH-DRBG(7)>,
353L<EVP_RAND-HMAC-DRBG(7)>,
354L<EVP_RAND-TEST-RAND(7)>,
355L<provider-rand(7)>
356
357=head1 HISTORY
358
359This functionality was added to OpenSSL 3.0.
360
361=head1 COPYRIGHT
362
363Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
364
365Licensed under the Apache License 2.0 (the "License"). You may not use
366this file except in compliance with the License. You can obtain a copy
367in the file LICENSE in the source distribution or at
368L<https://www.openssl.org/source/license.html>.
369
370=cut