1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1 christos provider-rand - The random number generation library E<lt>-E<gt> provider 6 1.1 christos functions 7 1.1 christos 8 1.1 christos =head1 SYNOPSIS 9 1.1 christos 10 1.1 christos =for openssl multiple includes 11 1.1 christos 12 1.1 christos #include <openssl/core_dispatch.h> 13 1.1 christos #include <openssl/core_names.h> 14 1.1 christos 15 1.1 christos /* 16 1.1 christos * None of these are actual functions, but are displayed like this for 17 1.1 christos * the function signatures for functions that are offered as function 18 1.1 christos * pointers in OSSL_DISPATCH arrays. 19 1.1 christos */ 20 1.1 christos 21 1.1 christos /* Context management */ 22 1.1 christos void *OSSL_FUNC_rand_newctx(void *provctx, void *parent, 23 1.1 christos const OSSL_DISPATCH *parent_calls); 24 1.1 christos void OSSL_FUNC_rand_freectx(void *ctx); 25 1.1 christos 26 1.1 christos /* Random number generator functions: NIST */ 27 1.1 christos int OSSL_FUNC_rand_instantiate(void *ctx, unsigned int strength, 28 1.1 christos int prediction_resistance, 29 1.1 christos const unsigned char *pstr, size_t pstr_len, 30 1.1 christos const OSSL_PARAM params[]); 31 1.1 christos int OSSL_FUNC_rand_uninstantiate(void *ctx); 32 1.1 christos int OSSL_FUNC_rand_generate(void *ctx, unsigned char *out, size_t outlen, 33 1.1 christos unsigned int strength, int prediction_resistance, 34 1.1 christos const unsigned char *addin, size_t addin_len); 35 1.1 christos int OSSL_FUNC_rand_reseed(void *ctx, int prediction_resistance, 36 1.1 christos const unsigned char *ent, size_t ent_len, 37 1.1 christos const unsigned char *addin, size_t addin_len); 38 1.1 christos 39 1.1 christos /* Random number generator functions: additional */ 40 1.1 christos size_t OSSL_FUNC_rand_nonce(void *ctx, unsigned char *out, size_t outlen, 41 1.1 christos int strength, size_t min_noncelen, 42 1.1 christos size_t max_noncelen); 43 1.1 christos size_t OSSL_FUNC_rand_get_seed(void *ctx, unsigned char **buffer, 44 1.1 christos int entropy, size_t min_len, size_t max_len, 45 1.1 christos int prediction_resistance, 46 1.1 christos const unsigned char *adin, size_t adin_len); 47 1.1 christos void OSSL_FUNC_rand_clear_seed(void *ctx, unsigned char *buffer, size_t b_len); 48 1.1 christos int OSSL_FUNC_rand_verify_zeroization(void *ctx); 49 1.1 christos 50 1.1 christos /* Context Locking */ 51 1.1 christos int OSSL_FUNC_rand_enable_locking(void *ctx); 52 1.1 christos int OSSL_FUNC_rand_lock(void *ctx); 53 1.1 christos void OSSL_FUNC_rand_unlock(void *ctx); 54 1.1 christos 55 1.1 christos /* RAND parameter descriptors */ 56 1.1 christos const OSSL_PARAM *OSSL_FUNC_rand_gettable_params(void *provctx); 57 1.1 christos const OSSL_PARAM *OSSL_FUNC_rand_gettable_ctx_params(void *ctx, void *provctx); 58 1.1 christos const OSSL_PARAM *OSSL_FUNC_rand_settable_ctx_params(void *ctx, void *provctx); 59 1.1 christos 60 1.1 christos /* RAND parameters */ 61 1.1 christos int OSSL_FUNC_rand_get_params(OSSL_PARAM params[]); 62 1.1 christos int OSSL_FUNC_rand_get_ctx_params(void *ctx, OSSL_PARAM params[]); 63 1.1 christos int OSSL_FUNC_rand_set_ctx_params(void *ctx, const OSSL_PARAM params[]); 64 1.1 christos 65 1.1 christos =head1 DESCRIPTION 66 1.1 christos 67 1.1 christos This documentation is primarily aimed at provider authors. See L<provider(7)> 68 1.1 christos for further information. 69 1.1 christos 70 1.1 christos The RAND operation enables providers to implement random number generation 71 1.1 christos algorithms and random number sources and make 72 1.1 christos them available to applications via the API function L<EVP_RAND(3)>. 73 1.1 christos 74 1.1 christos =head2 Context Management Functions 75 1.1 christos 76 1.1 christos OSSL_FUNC_rand_newctx() should create and return a pointer to a provider side 77 1.1 christos structure for holding context information during a rand operation. 78 1.1 christos A pointer to this context will be passed back in a number of the other rand 79 1.1 christos operation function calls. 80 1.1 christos The parameter I<provctx> is the provider context generated during provider 81 1.1 christos initialisation (see L<provider(7)>). 82 1.1 christos The parameter I<parent> specifies another rand instance to be used for 83 1.1 christos seeding purposes. If NULL and the specific instance supports it, the 84 1.1 christos operating system will be used for seeding. 85 1.1 christos The parameter I<parent_calls> points to the dispatch table for I<parent>. 86 1.1 christos Thus, the parent need not be from the same provider as the new instance. 87 1.1 christos 88 1.1 christos OSSL_FUNC_rand_freectx() is passed a pointer to the provider side rand context in 89 1.1 christos the I<mctx> parameter. 90 1.1 christos If it receives NULL as I<ctx> value, it should not do anything other than 91 1.1 christos return. 92 1.1 christos This function should free any resources associated with that context. 93 1.1 christos 94 1.1 christos =head2 Random Number Generator Functions: NIST 95 1.1 christos 96 1.1 christos These functions correspond to those defined in NIST SP 800-90A and SP 800-90C. 97 1.1 christos 98 1.1 christos OSSL_FUNC_rand_instantiate() is used to instantiate the DRBG I<ctx> at a requested 99 1.1 christos security I<strength>. In addition, I<prediction_resistance> can be requested. 100 1.1 christos Additional input I<addin> of length I<addin_len> bytes can optionally 101 1.1 christos be provided. The parameters specified in I<params> configure the DRBG and these 102 1.1 christos should be processed before instantiation. 103 1.1 christos 104 1.1 christos OSSL_FUNC_rand_uninstantiate() is used to uninstantiate the DRBG I<ctx>. After being 105 1.1 christos uninstantiated, a DRBG is unable to produce output until it is instantiated 106 1.1 christos anew. 107 1.1 christos 108 1.1 christos OSSL_FUNC_rand_generate() is used to generate random bytes from the DRBG I<ctx>. 109 1.1 christos It will generate I<outlen> bytes placing them into the buffer pointed to by 110 1.1 christos I<out>. The generated bytes will meet the specified security I<strength> and, 111 1.1 christos if I<prediction_resistance> is true, the bytes will be produced after reseeding 112 1.1 christos from a live entropy source. Additional input I<addin> of length I<addin_len> 113 1.1 christos bytes can optionally be provided. 114 1.1 christos 115 1.1 christos =head2 Random Number Generator Functions: Additional 116 1.1 christos 117 1.1 christos OSSL_FUNC_rand_nonce() is used to generate a nonce of the given I<strength> with a 118 1.1 christos length from I<min_noncelen> to I<max_noncelen>. If the output buffer I<out> is 119 1.1 christos NULL, the length of the nonce should be returned. 120 1.1 christos 121 1.1 christos OSSL_FUNC_rand_get_seed() is used by deterministic generators to obtain their 122 1.1 christos seeding material from their parent. The seed bytes will meet the specified 123 1.1 christos security level of I<entropy> bits and there will be between I<min_len> 124 1.1 christos and I<max_len> inclusive bytes in total. If I<prediction_resistance> is 125 1.1 christos true, the bytes will be produced from a live entropy source. Additional 126 1.1 christos input I<addin> of length I<addin_len> bytes can optionally be provided. 127 1.1 christos A pointer to the seed material is returned in I<*buffer> and this must be 128 1.1 christos freed by a later call to OSSL_FUNC_rand_clear_seed(). 129 1.1 christos 130 1.1 christos OSSL_FUNC_rand_clear_seed() frees a seed I<buffer> of length I<b_len> bytes 131 1.1 christos which was previously allocated by OSSL_FUNC_rand_get_seed(). 132 1.1 christos 133 1.1 christos OSSL_FUNC_rand_verify_zeroization() is used to determine if the internal state of the 134 1.1 christos DRBG is zero. This capability is mandated by NIST as part of the self 135 1.1 christos tests, it is unlikely to be useful in other circumstances. 136 1.1 christos 137 1.1 christos =head2 Context Locking 138 1.1 christos 139 1.1 christos When DRBGs are used by multiple threads, there must be locking employed to 140 1.1 christos ensure their proper operation. Because locking introduces an overhead, it 141 1.1 christos is disabled by default. 142 1.1 christos 143 1.1 christos OSSL_FUNC_rand_enable_locking() allows locking to be turned on for a DRBG and all of 144 1.1 christos its parent DRBGs. From this call onwards, the DRBG can be used in a thread 145 1.1 christos safe manner. 146 1.1 christos 147 1.1 christos OSSL_FUNC_rand_lock() is used to lock a DRBG. Once locked, exclusive access 148 1.1 christos is guaranteed. 149 1.1 christos 150 1.1 christos OSSL_FUNC_rand_unlock() is used to unlock a DRBG. 151 1.1 christos 152 1.1 christos =head2 Rand Parameters 153 1.1 christos 154 1.1 christos See L<OSSL_PARAM(3)> for further details on the parameters structure used by 155 1.1 christos these functions. 156 1.1 christos 157 1.1 christos OSSL_FUNC_rand_get_params() gets details of parameter values associated with the 158 1.1 christos provider algorithm and stores them in I<params>. 159 1.1 christos 160 1.1 christos OSSL_FUNC_rand_set_ctx_params() sets rand parameters associated with the given 161 1.1 christos provider side rand context I<ctx> to I<params>. 162 1.1 christos Any parameter settings are additional to any that were previously set. 163 1.1 christos Passing NULL for I<params> should return true. 164 1.1 christos 165 1.1 christos OSSL_FUNC_rand_get_ctx_params() gets details of currently set parameter values 166 1.1 christos associated with the given provider side rand context I<ctx> and stores them 167 1.1 christos in I<params>. 168 1.1 christos Passing NULL for I<params> should return true. 169 1.1 christos 170 1.1 christos OSSL_FUNC_rand_gettable_params(), OSSL_FUNC_rand_gettable_ctx_params(), 171 1.1 christos and OSSL_FUNC_rand_settable_ctx_params() all return constant L<OSSL_PARAM(3)> 172 1.1 christos arrays as descriptors of the parameters that OSSL_FUNC_rand_get_params(), 173 1.1 christos OSSL_FUNC_rand_get_ctx_params(), and OSSL_FUNC_rand_set_ctx_params() 174 1.1 christos can handle, respectively. OSSL_FUNC_rand_gettable_ctx_params() 175 1.1 christos and OSSL_FUNC_rand_settable_ctx_params() will return the parameters 176 1.1 christos associated with the provider side context I<ctx> in its current state 177 1.1 christos if it is not NULL. Otherwise, they return the parameters associated 178 1.1 christos with the provider side algorithm I<provctx>. 179 1.1 christos 180 1.1 christos 181 1.1 christos Parameters currently recognised by built-in rands are as follows. Not all 182 1.1 christos parameters are relevant to, or are understood by all rands: 183 1.1 christos 184 1.1 christos =over 4 185 1.1 christos 186 1.1 christos =item "state" (B<OSSL_RAND_PARAM_STATE>) <integer> 187 1.1 christos 188 1.1 christos Returns the state of the random number generator. 189 1.1 christos 190 1.1 christos =item "strength" (B<OSSL_RAND_PARAM_STRENGTH>) <unsigned integer> 191 1.1 christos 192 1.1 christos Returns the bit strength of the random number generator. 193 1.1 christos 194 1.1 christos =back 195 1.1 christos 196 1.1 christos For rands that are also deterministic random bit generators (DRBGs), these 197 1.1 christos additional parameters are recognised. Not all 198 1.1 christos parameters are relevant to, or are understood by all DRBG rands: 199 1.1 christos 200 1.1 christos =over 4 201 1.1 christos 202 1.1 christos =item "reseed_requests" (B<OSSL_DRBG_PARAM_RESEED_REQUESTS>) <unsigned integer> 203 1.1 christos 204 1.1 christos Reads or set the number of generate requests before reseeding the 205 1.1 christos associated RAND ctx. 206 1.1 christos 207 1.1 christos =item "reseed_time_interval" (B<OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL>) <integer> 208 1.1 christos 209 1.1 christos Reads or set the number of elapsed seconds before reseeding the 210 1.1 christos associated RAND ctx. 211 1.1 christos 212 1.1 christos =item "max_request" (B<OSSL_DRBG_PARAM_RESEED_REQUESTS>) <unsigned integer> 213 1.1 christos 214 1.1 christos Specifies the maximum number of bytes that can be generated in a single 215 1.1 christos call to OSSL_FUNC_rand_generate. 216 1.1 christos 217 1.1 christos =item "min_entropylen" (B<OSSL_DRBG_PARAM_MIN_ENTROPYLEN>) <unsigned integer> 218 1.1 christos 219 1.1 christos =item "max_entropylen" (B<OSSL_DRBG_PARAM_MAX_ENTROPYLEN>) <unsigned integer> 220 1.1 christos 221 1.1 christos Specify the minimum and maximum number of bytes of random material that 222 1.1 christos can be used to seed the DRBG. 223 1.1 christos 224 1.1 christos =item "min_noncelen" (B<OSSL_DRBG_PARAM_MIN_NONCELEN>) <unsigned integer> 225 1.1 christos 226 1.1 christos =item "max_noncelen" (B<OSSL_DRBG_PARAM_MAX_NONCELEN>) <unsigned integer> 227 1.1 christos 228 1.1 christos Specify the minimum and maximum number of bytes of nonce that can be used to 229 1.1 christos instantiate the DRBG. 230 1.1 christos 231 1.1 christos =item "max_perslen" (B<OSSL_DRBG_PARAM_MAX_PERSLEN>) <unsigned integer> 232 1.1 christos 233 1.1 christos =item "max_adinlen" (B<OSSL_DRBG_PARAM_MAX_ADINLEN>) <unsigned integer> 234 1.1 christos 235 1.1 christos Specify the minimum and maximum number of bytes of personalisation string 236 1.1 christos that can be used with the DRBG. 237 1.1 christos 238 1.1 christos =item "reseed_counter" (B<OSSL_DRBG_PARAM_RESEED_COUNTER>) <unsigned integer> 239 1.1 christos 240 1.1 christos Specifies the number of times the DRBG has been seeded or reseeded. 241 1.1 christos 242 1.1 christos =item "digest" (B<OSSL_DRBG_PARAM_DIGEST>) <UTF8 string> 243 1.1 christos 244 1.1 christos =item "cipher" (B<OSSL_DRBG_PARAM_CIPHER>) <UTF8 string> 245 1.1 christos 246 1.1 christos =item "mac" (B<OSSL_DRBG_PARAM_MAC>) <UTF8 string> 247 1.1 christos 248 1.1 christos Sets the name of the underlying cipher, digest or MAC to be used. 249 1.1 christos It must name a suitable algorithm for the DRBG that's being used. 250 1.1 christos 251 1.1 christos =item "properties" (B<OSSL_DRBG_PARAM_PROPERTIES>) <UTF8 string> 252 1.1 christos 253 1.1 christos Sets the properties to be queried when trying to fetch an underlying algorithm. 254 1.1 christos This must be given together with the algorithm naming parameter to be 255 1.1 christos considered valid. 256 1.1 christos 257 1.1 christos =back 258 1.1 christos 259 1.1 christos =head1 RETURN VALUES 260 1.1 christos 261 1.1 christos OSSL_FUNC_rand_newctx() should return the newly created 262 1.1 christos provider side rand context, or NULL on failure. 263 1.1 christos 264 1.1 christos OSSL_FUNC_rand_gettable_params(), OSSL_FUNC_rand_gettable_ctx_params() and 265 1.1 christos OSSL_FUNC_rand_settable_ctx_params() should return a constant L<OSSL_PARAM(3)> 266 1.1 christos array, or NULL if none is offered. 267 1.1 christos 268 1.1 christos OSSL_FUNC_rand_nonce() returns the size of the generated nonce, or 0 on error. 269 1.1 christos 270 1.1 christos OSSL_FUNC_rand_get_seed() returns the size of the generated seed, or 0 on 271 1.1 christos error. 272 1.1 christos 273 1.1 christos All of the remaining functions should return 1 for success or 0 on error. 274 1.1 christos 275 1.1 christos =head1 NOTES 276 1.1 christos 277 1.1 christos The RAND life-cycle is described in L<life_cycle-rand(7)>. Providers should 278 1.1 christos ensure that the various transitions listed there are supported. At some point 279 1.1 christos the EVP layer will begin enforcing the listed transitions. 280 1.1 christos 281 1.1 christos =head1 SEE ALSO 282 1.1 christos 283 1.1 christos L<provider(7)>, 284 1.1 christos L<RAND(7)>, 285 1.1 christos L<EVP_RAND(7)>, 286 1.1 christos L<life_cycle-rand(7)>, 287 1.1 christos L<EVP_RAND(3)> 288 1.1 christos 289 1.1 christos =head1 HISTORY 290 1.1 christos 291 1.1 christos The provider RAND interface was introduced in OpenSSL 3.0. 292 1.1 christos 293 1.1 christos =head1 COPYRIGHT 294 1.1 christos 295 1.1 christos Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. 296 1.1 christos 297 1.1 christos Licensed under the Apache License 2.0 (the "License"). You may not use 298 1.1 christos this file except in compliance with the License. You can obtain a copy 299 1.1 christos in the file LICENSE in the source distribution or at 300 1.1 christos L<https://www.openssl.org/source/license.html>. 301 1.1 christos 302 1.1 christos =cut 303