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