2
0

EVP_RAND.pod 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. =pod
  2. =head1 NAME
  3. EVP_RAND, EVP_RAND_fetch, EVP_RAND_free, EVP_RAND_up_ref, EVP_RAND_CTX,
  4. EVP_RAND_CTX_new, EVP_RAND_CTX_free, EVP_RAND_CTX_up_ref, EVP_RAND_instantiate,
  5. EVP_RAND_uninstantiate, EVP_RAND_generate, EVP_RAND_reseed, EVP_RAND_nonce,
  6. EVP_RAND_enable_locking, EVP_RAND_verify_zeroization, EVP_RAND_get_strength,
  7. EVP_RAND_get_state,
  8. EVP_RAND_get0_provider, EVP_RAND_CTX_get0_rand, EVP_RAND_is_a,
  9. EVP_RAND_get0_name, EVP_RAND_names_do_all,
  10. EVP_RAND_get0_description,
  11. EVP_RAND_CTX_get_params,
  12. EVP_RAND_CTX_set_params, EVP_RAND_do_all_provided, EVP_RAND_get_params,
  13. EVP_RAND_gettable_ctx_params, EVP_RAND_settable_ctx_params,
  14. EVP_RAND_CTX_gettable_params, EVP_RAND_CTX_settable_params,
  15. EVP_RAND_gettable_params, EVP_RAND_STATE_UNINITIALISED, EVP_RAND_STATE_READY,
  16. EVP_RAND_STATE_ERROR - EVP RAND routines
  17. =head1 SYNOPSIS
  18. #include <openssl/evp.h>
  19. typedef struct evp_rand_st EVP_RAND;
  20. typedef struct evp_rand_ctx_st EVP_RAND_CTX;
  21. EVP_RAND *EVP_RAND_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
  22. const char *properties);
  23. int EVP_RAND_up_ref(EVP_RAND *rand);
  24. void EVP_RAND_free(EVP_RAND *rand);
  25. EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent);
  26. void EVP_RAND_CTX_free(EVP_RAND_CTX *ctx);
  27. int EVP_RAND_CTX_up_ref(EVP_RAND_CTX *ctx);
  28. EVP_RAND *EVP_RAND_CTX_get0_rand(EVP_RAND_CTX *ctx);
  29. int EVP_RAND_get_params(EVP_RAND *rand, OSSL_PARAM params[]);
  30. int EVP_RAND_CTX_get_params(EVP_RAND_CTX *ctx, OSSL_PARAM params[]);
  31. int EVP_RAND_CTX_set_params(EVP_RAND_CTX *ctx, const OSSL_PARAM params[]);
  32. const OSSL_PARAM *EVP_RAND_gettable_params(const EVP_RAND *rand);
  33. const OSSL_PARAM *EVP_RAND_gettable_ctx_params(const EVP_RAND *rand);
  34. const OSSL_PARAM *EVP_RAND_settable_ctx_params(const EVP_RAND *rand);
  35. const OSSL_PARAM *EVP_RAND_CTX_gettable_params(EVP_RAND_CTX *ctx);
  36. const OSSL_PARAM *EVP_RAND_CTX_settable_params(EVP_RAND_CTX *ctx);
  37. const char *EVP_RAND_get0_name(const EVP_RAND *rand);
  38. const char *EVP_RAND_get0_description(const EVP_RAND *rand);
  39. int EVP_RAND_is_a(const EVP_RAND *rand, const char *name);
  40. const OSSL_PROVIDER *EVP_RAND_get0_provider(const EVP_RAND *rand);
  41. void EVP_RAND_do_all_provided(OSSL_LIB_CTX *libctx,
  42. void (*fn)(EVP_RAND *rand, void *arg),
  43. void *arg);
  44. int EVP_RAND_names_do_all(const EVP_RAND *rand,
  45. void (*fn)(const char *name, void *data),
  46. void *data);
  47. int EVP_RAND_instantiate(EVP_RAND_CTX *ctx, unsigned int strength,
  48. int prediction_resistance,
  49. const unsigned char *pstr, size_t pstr_len,
  50. const OSSL_PARAM params[]);
  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);
  60. int EVP_RAND_verify_zeroization(EVP_RAND_CTX *ctx);
  61. unsigned int EVP_RAND_get_strength(EVP_RAND_CTX *ctx);
  62. int EVP_RAND_get_state(EVP_RAND_CTX *ctx);
  63. #define EVP_RAND_STATE_UNINITIALISED 0
  64. #define EVP_RAND_STATE_READY 1
  65. #define EVP_RAND_STATE_ERROR 2
  66. =head1 DESCRIPTION
  67. The EVP RAND routines are a high-level interface to random number generators
  68. both deterministic and not.
  69. If you just want to generate random bytes then you don't need to use
  70. these functions: just call RAND_bytes() or RAND_priv_bytes().
  71. If you want to do more, these calls should be used instead of the older
  72. RAND and RAND_DRBG functions.
  73. After creating a B<EVP_RAND_CTX> for the required algorithm using
  74. EVP_RAND_CTX_new(), inputs to the algorithm are supplied either by
  75. passing them as part of the EVP_RAND_instantiate() call or using calls to
  76. EVP_RAND_CTX_set_params() before calling EVP_RAND_instantiate(). Finally,
  77. call EVP_RAND_generate() to produce cryptographically secure random bytes.
  78. =head2 Types
  79. B<EVP_RAND> is a type that holds the implementation of a RAND.
  80. B<EVP_RAND_CTX> is a context type that holds the algorithm inputs.
  81. B<EVP_RAND_CTX> structures are reference counted.
  82. =head2 Algorithm implementation fetching
  83. EVP_RAND_fetch() fetches an implementation of a RAND I<algorithm>, given
  84. a library context I<libctx> and a set of I<properties>.
  85. See L<crypto(7)/ALGORITHM FETCHING> for further information.
  86. The returned value must eventually be freed with
  87. L<EVP_RAND_free(3)>.
  88. EVP_RAND_up_ref() increments the reference count of an already fetched
  89. RAND.
  90. EVP_RAND_free() frees a fetched algorithm.
  91. NULL is a valid parameter, for which this function is a no-op.
  92. =head2 Context manipulation functions
  93. EVP_RAND_CTX_new() creates a new context for the RAND implementation I<rand>.
  94. If not NULL, I<parent> specifies the seed source for this implementation.
  95. Not all random number generators need to have a seed source specified.
  96. If a parent is required, a NULL I<parent> will utilise the operating
  97. system entropy sources.
  98. It is recommended to minimise the number of random number generators that
  99. rely on the operating system for their randomness because this is often scarce.
  100. EVP_RAND_CTX_free() frees up the context I<ctx>. If I<ctx> is NULL, nothing
  101. is done.
  102. EVP_RAND_CTX_get0_rand() returns the B<EVP_RAND> associated with the context
  103. I<ctx>.
  104. =head2 Random Number Generator Functions
  105. EVP_RAND_instantiate() processes any parameters in I<params> and
  106. then instantiates the RAND I<ctx> with a minimum security strength
  107. of <strength> and personalisation string I<pstr> of length <pstr_len>.
  108. If I<prediction_resistance> is specified, fresh entropy from a live source
  109. will be sought. This call operates as per NIST SP 800-90A and SP 800-90C.
  110. EVP_RAND_uninstantiate() uninstantiates the RAND I<ctx> as per
  111. NIST SP 800-90A and SP 800-90C. Subsequent to this call, the RAND cannot
  112. be used to generate bytes. It can only be freed or instantiated again.
  113. EVP_RAND_generate() produces random bytes from the RAND I<ctx> with the
  114. additional input I<addin> of length I<addin_len>. The bytes
  115. produced will meet the security I<strength>.
  116. If I<prediction_resistance> is specified, fresh entropy from a live source
  117. will be sought. This call operates as per NIST SP 800-90A and SP 800-90C.
  118. EVP_RAND_reseed() reseeds the RAND with new entropy.
  119. Entropy I<ent> of length I<ent_len> bytes can be supplied as can additional
  120. input I<addin> of length I<addin_len> bytes. In the FIPS provider, both are
  121. treated as additional input as per NIST SP-800-90Ar1, Sections 9.1 and 9.2.
  122. Additional seed material is also drawn from the RAND's parent or the
  123. operating system. If I<prediction_resistance> is specified, fresh entropy
  124. from a live source will be sought. This call operates as per NIST SP 800-90A
  125. and SP 800-90C.
  126. EVP_RAND_nonce() creates a nonce in I<out> of maximum length I<outlen>
  127. bytes from the RAND I<ctx>. The function returns the length of the generated
  128. nonce. If I<out> is NULL, the length is still returned but no generation
  129. takes place. This allows a caller to dynamically allocate a buffer of the
  130. appropriate size.
  131. EVP_RAND_enable_locking() enables locking for the RAND I<ctx> and all of
  132. its parents. After this I<ctx> will operate in a thread safe manner, albeit
  133. more slowly. This function is not itself thread safe if called with the same
  134. I<ctx> from multiple threads. Typically locking should be enabled before a
  135. I<ctx> is shared across multiple threads.
  136. EVP_RAND_get_params() retrieves details about the implementation
  137. I<rand>.
  138. The set of parameters given with I<params> determine exactly what
  139. parameters should be retrieved.
  140. Note that a parameter that is unknown in the underlying context is
  141. simply ignored.
  142. EVP_RAND_CTX_get_params() retrieves chosen parameters, given the
  143. context I<ctx> and its underlying context.
  144. The set of parameters given with I<params> determine exactly what
  145. parameters should be retrieved.
  146. Note that a parameter that is unknown in the underlying context is
  147. simply ignored.
  148. EVP_RAND_CTX_set_params() passes chosen parameters to the underlying
  149. context, given a context I<ctx>.
  150. The set of parameters given with I<params> determine exactly what
  151. parameters are passed down.
  152. Note that a parameter that is unknown in the underlying context is
  153. simply ignored.
  154. Also, what happens when a needed parameter isn't passed down is
  155. defined by the implementation.
  156. EVP_RAND_gettable_params() returns an L<OSSL_PARAM(3)> array that describes
  157. the retrievable and settable parameters. EVP_RAND_gettable_params() returns
  158. parameters that can be used with EVP_RAND_get_params().
  159. EVP_RAND_gettable_ctx_params() and EVP_RAND_CTX_gettable_params() return
  160. constant L<OSSL_PARAM(3)> arrays that describe the retrievable parameters that
  161. can be used with EVP_RAND_CTX_get_params(). EVP_RAND_gettable_ctx_params()
  162. returns the parameters that can be retrieved from the algorithm, whereas
  163. EVP_RAND_CTX_gettable_params() returns the parameters that can be retrieved
  164. in the context's current state.
  165. EVP_RAND_settable_ctx_params() and EVP_RAND_CTX_settable_params() return
  166. constant L<OSSL_PARAM(3)> arrays that describe the settable parameters that
  167. can be used with EVP_RAND_CTX_set_params(). EVP_RAND_settable_ctx_params()
  168. returns the parameters that can be retrieved from the algorithm, whereas
  169. EVP_RAND_CTX_settable_params() returns the parameters that can be retrieved
  170. in the context's current state.
  171. =head2 Information functions
  172. EVP_RAND_get_strength() returns the security strength of the RAND I<ctx>.
  173. EVP_RAND_get_state() returns the current state of the RAND I<ctx>.
  174. States defined by the OpenSSL RNGs are:
  175. =over 4
  176. =item *
  177. EVP_RAND_STATE_UNINITIALISED: this RNG is currently uninitialised.
  178. The instantiate call will change this to the ready state.
  179. =item *
  180. EVP_RAND_STATE_READY: this RNG is currently ready to generate output.
  181. =item *
  182. EVP_RAND_STATE_ERROR: this RNG is in an error state.
  183. =back
  184. EVP_RAND_is_a() returns 1 if I<rand> is an implementation of an
  185. algorithm that's identifiable with I<name>, otherwise 0.
  186. EVP_RAND_get0_provider() returns the provider that holds the implementation
  187. of the given I<rand>.
  188. EVP_RAND_do_all_provided() traverses all RAND implemented by all activated
  189. providers in the given library context I<libctx>, and for each of the
  190. implementations, calls the given function I<fn> with the implementation method
  191. and the given I<arg> as argument.
  192. EVP_RAND_get0_name() returns the canonical name of I<rand>.
  193. EVP_RAND_names_do_all() traverses all names for I<rand>, and calls
  194. I<fn> with each name and I<data>.
  195. EVP_RAND_get0_description() returns a description of the rand, meant for
  196. display and human consumption. The description is at the discretion of
  197. the rand implementation.
  198. EVP_RAND_verify_zeroization() confirms if the internal DRBG state is
  199. currently zeroed. This is used by the FIPS provider to support the mandatory
  200. self tests.
  201. =head1 PARAMETERS
  202. The standard parameter names are:
  203. =over 4
  204. =item "state" (B<OSSL_RAND_PARAM_STATE>) <integer>
  205. Returns the state of the random number generator.
  206. =item "strength" (B<OSSL_RAND_PARAM_STRENGTH>) <unsigned integer>
  207. Returns the bit strength of the random number generator.
  208. =back
  209. For rands that are also deterministic random bit generators (DRBGs), these
  210. additional parameters are recognised. Not all
  211. parameters are relevant to, or are understood by all DRBG rands:
  212. =over 4
  213. =item "reseed_requests" (B<OSSL_DRBG_PARAM_RESEED_REQUESTS>) <unsigned integer>
  214. Reads or set the number of generate requests before reseeding the
  215. associated RAND ctx.
  216. =item "reseed_time_interval" (B<OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL>) <integer>
  217. Reads or set the number of elapsed seconds before reseeding the
  218. associated RAND ctx.
  219. =item "max_request" (B<OSSL_DRBG_PARAM_RESEED_REQUESTS>) <unsigned integer>
  220. Specifies the maximum number of bytes that can be generated in a single
  221. call to OSSL_FUNC_rand_generate.
  222. =item "min_entropylen" (B<OSSL_DRBG_PARAM_MIN_ENTROPYLEN>) <unsigned integer>
  223. =item "max_entropylen" (B<OSSL_DRBG_PARAM_MAX_ENTROPYLEN>) <unsigned integer>
  224. Specify the minimum and maximum number of bytes of random material that
  225. can be used to seed the DRBG.
  226. =item "min_noncelen" (B<OSSL_DRBG_PARAM_MIN_NONCELEN>) <unsigned integer>
  227. =item "max_noncelen" (B<OSSL_DRBG_PARAM_MAX_NONCELEN>) <unsigned integer>
  228. Specify the minimum and maximum number of bytes of nonce that can be used to
  229. seed the DRBG.
  230. =item "max_perslen" (B<OSSL_DRBG_PARAM_MAX_PERSLEN>) <unsigned integer>
  231. =item "max_adinlen" (B<OSSL_DRBG_PARAM_MAX_ADINLEN>) <unsigned integer>
  232. Specify the minimum and maximum number of bytes of personalisation string
  233. that can be used with the DRBG.
  234. =item "reseed_counter" (B<OSSL_DRBG_PARAM_RESEED_COUNTER>) <unsigned integer>
  235. Specifies the number of times the DRBG has been seeded or reseeded.
  236. =item "properties" (B<OSSL_RAND_PARAM_PROPERTIES>) <UTF8 string>
  237. =item "mac" (B<OSSL_RAND_PARAM_MAC>) <UTF8 string>
  238. =item "digest" (B<OSSL_RAND_PARAM_DIGEST>) <UTF8 string>
  239. =item "cipher" (B<OSSL_RAND_PARAM_CIPHER>) <UTF8 string>
  240. For RAND implementations that use an underlying computation MAC, digest or
  241. cipher, these parameters set what the algorithm should be.
  242. The value is always the name of the intended algorithm,
  243. or the properties in the case of B<OSSL_RAND_PARAM_PROPERTIES>.
  244. =back
  245. =head1 NOTES
  246. The use of a nonzero value for the I<prediction_resistance> argument to
  247. EVP_RAND_instantiate(), EVP_RAND_generate() or EVP_RAND_reseed() should
  248. be used sparingly. In the default setup, this will cause all public and
  249. private DRBGs to be reseeded on next use. Since, by default, public and
  250. private DRBGs are allocated on a per thread basis, this can result in
  251. significant overhead for highly multi-threaded applications. For normal
  252. use-cases, the default "reseed_requests" and "reseed_time_interval"
  253. thresholds ensure sufficient prediction resistance over time and you
  254. can reduce those values if you think they are too high. Explicitly
  255. requesting prediction resistance is intended for more special use-cases
  256. like generating long-term secrets.
  257. An B<EVP_RAND_CTX> needs to have locking enabled if it acts as the parent of
  258. more than one child and the children can be accessed concurrently. This must
  259. be done by explicitly calling EVP_RAND_enable_locking().
  260. The RAND life-cycle is described in L<life_cycle-rand(7)>. In the future,
  261. the transitions described there will be enforced. When this is done, it will
  262. not be considered a breaking change to the API.
  263. =head1 RETURN VALUES
  264. EVP_RAND_fetch() returns a pointer to a newly fetched B<EVP_RAND>, or
  265. NULL if allocation failed.
  266. EVP_RAND_get0_provider() returns a pointer to the provider for the RAND, or
  267. NULL on error.
  268. EVP_RAND_CTX_get0_rand() returns a pointer to the B<EVP_RAND> associated
  269. with the context.
  270. EVP_RAND_get0_name() returns the name of the random number generation
  271. algorithm.
  272. EVP_RAND_up_ref() returns 1 on success, 0 on error.
  273. EVP_RAND_names_do_all() returns 1 if the callback was called for all names. A
  274. return value of 0 means that the callback was not called for any names.
  275. EVP_RAND_CTX_new() returns either the newly allocated
  276. B<EVP_RAND_CTX> structure or NULL if an error occurred.
  277. EVP_RAND_CTX_free() does not return a value.
  278. EVP_RAND_CTX_up_ref() returns 1 on success, 0 on error.
  279. EVP_RAND_nonce() returns the length of the nonce.
  280. EVP_RAND_get_strength() returns the strength of the random number generator
  281. in bits.
  282. EVP_RAND_gettable_params(), EVP_RAND_gettable_ctx_params() and
  283. EVP_RAND_settable_ctx_params() return an array of OSSL_PARAMs.
  284. EVP_RAND_verify_zeroization() returns 1 if the internal DRBG state is
  285. currently zeroed, and 0 if not.
  286. The remaining functions return 1 for success and 0 or a negative value for
  287. failure.
  288. =head1 SEE ALSO
  289. L<RAND_bytes(3)>,
  290. L<EVP_RAND-CTR-DRBG(7)>,
  291. L<EVP_RAND-HASH-DRBG(7)>,
  292. L<EVP_RAND-HMAC-DRBG(7)>,
  293. L<EVP_RAND-TEST-RAND(7)>,
  294. L<provider-rand(7)>,
  295. L<life_cycle-rand(7)>
  296. =head1 HISTORY
  297. EVP_RAND_CTX_up_ref() was added in OpenSSL 3.1.
  298. The remaining functions were added in OpenSSL 3.0.
  299. =head1 COPYRIGHT
  300. Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
  301. Licensed under the Apache License 2.0 (the "License"). You may not use
  302. this file except in compliance with the License. You can obtain a copy
  303. in the file LICENSE in the source distribution or at
  304. L<https://www.openssl.org/source/license.html>.
  305. =cut