2
0

provider-rand.pod 12 KB

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