EVP_RAND.pod 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. =pod
  2. =head1 NAME
  3. EVP_RAND - the random bit generator
  4. =head1 SYNOPSIS
  5. #include <openssl/evp.h>
  6. #include <rand.h>
  7. =head1 DESCRIPTION
  8. The default OpenSSL RAND method is based on the EVP_RAND classes to provide
  9. non-deterministic inputs to other cryptographic algorithms.
  10. While the RAND API is the 'frontend' which is intended to be used by
  11. application developers for obtaining random bytes, the EVP_RAND API
  12. serves as the 'backend', connecting the former with the operating
  13. systems's entropy sources and providing access to deterministic random
  14. bit generators (DRBG) and their configuration parameters.
  15. A DRBG is a certain type of cryptographically-secure pseudo-random
  16. number generator (CSPRNG), which is described in
  17. [NIST SP 800-90A Rev. 1].
  18. =head2 Disclaimer
  19. Unless you have very specific requirements for your random generator,
  20. it is in general not necessary to utilize the EVP_RAND API directly.
  21. The usual way to obtain random bytes is to use L<RAND_bytes(3)> or
  22. L<RAND_priv_bytes(3)>, see also L<RAND(7)>.
  23. =head2 Typical Use Cases
  24. Typical examples for such special use cases are the following:
  25. =over 2
  26. =item *
  27. You want to use your own private DRBG instances.
  28. Multiple DRBG instances which are accessed only by a single thread provide
  29. additional security (because their internal states are independent) and
  30. better scalability in multithreaded applications (because they don't need
  31. to be locked).
  32. =item *
  33. You need to integrate a previously unsupported entropy source.
  34. Refer to L<provider-rand(7)> for the implementation details to support adding
  35. randomness sources to EVP_RAND.
  36. =item *
  37. You need to change the default settings of the standard OpenSSL RAND
  38. implementation to meet specific requirements.
  39. =back
  40. =head1 EVP_RAND CHAINING
  41. An EVP_RAND instance can be used as the entropy source of another
  42. EVP_RAND instance, provided it has itself access to a valid entropy source.
  43. The EVP_RAND instance which acts as entropy source is called the I<parent>,
  44. the other instance the I<child>. Typically, the child will be a DRBG because
  45. it does not make sense for the child to be an entropy source.
  46. This is called chaining. A chained EVP_RAND instance is created by passing
  47. a pointer to the parent EVP_RAND_CTX as argument to the EVP_RAND_CTX_new() call.
  48. It is possible to create chains of more than two DRBG in a row.
  49. It is also possible to use any EVP_RAND_CTX class as the parent, however, only
  50. a live entropy source may ignore and not use its parent.
  51. =head1 THE THREE SHARED DRBG INSTANCES
  52. Currently, there are three shared DRBG instances,
  53. the <primary>, <public>, and <private> DRBG.
  54. While the <primary> DRBG is a single global instance, the <public> and <private>
  55. DRBG are created per thread and accessed through thread-local storage.
  56. By default, the functions L<RAND_bytes(3)> and L<RAND_priv_bytes(3)> use
  57. the thread-local <public> and <private> DRBG instance, respectively.
  58. =head2 The <primary> DRBG instance
  59. The <primary> DRBG is not used directly by the application, only for reseeding
  60. the two other two DRBG instances. It reseeds itself by obtaining randomness
  61. either from os entropy sources or by consuming randomness which was added
  62. previously by L<RAND_add(3)>.
  63. =head2 The <public> DRBG instance
  64. This instance is used per default by L<RAND_bytes(3)>.
  65. =head2 The <private> DRBG instance
  66. This instance is used per default by L<RAND_priv_bytes(3)>
  67. =head1 LOCKING
  68. The <primary> DRBG is intended to be accessed concurrently for reseeding
  69. by its child DRBG instances. The necessary locking is done internally.
  70. It is I<not> thread-safe to access the <primary> DRBG directly via the
  71. EVP_RAND interface.
  72. The <public> and <private> DRBG are thread-local, i.e. there is an
  73. instance of each per thread. So they can safely be accessed without
  74. locking via the EVP_RAND interface.
  75. Pointers to these DRBG instances can be obtained using
  76. RAND_get0_primary(), RAND_get0_public() and RAND_get0_private(), respectively.
  77. Note that it is not allowed to store a pointer to one of the thread-local
  78. DRBG instances in a variable or other memory location where it will be
  79. accessed and used by multiple threads.
  80. All other DRBG instances created by an application don't support locking,
  81. because they are intended to be used by a single thread.
  82. Instead of accessing a single DRBG instance concurrently from different
  83. threads, it is recommended to instantiate a separate DRBG instance per
  84. thread. Using the <primary> DRBG as entropy source for multiple DRBG
  85. instances on different threads is thread-safe, because the DRBG instance
  86. will lock the <primary> DRBG automatically for obtaining random input.
  87. =head1 THE OVERALL PICTURE
  88. The following picture gives an overview over how the DRBG instances work
  89. together and are being used.
  90. +--------------------+
  91. | os entropy sources |
  92. +--------------------+
  93. |
  94. v +-----------------------------+
  95. RAND_add() ==> <primary> <-| shared DRBG (with locking) |
  96. / \ +-----------------------------+
  97. / \ +---------------------------+
  98. <public> <private> <- | per-thread DRBG instances |
  99. | | +---------------------------+
  100. v v
  101. RAND_bytes() RAND_priv_bytes()
  102. | ^
  103. | |
  104. +------------------+ +------------------------------------+
  105. | general purpose | | used for secrets like session keys |
  106. | random generator | | and private keys for certificates |
  107. +------------------+ +------------------------------------+
  108. The usual way to obtain random bytes is to call RAND_bytes(...) or
  109. RAND_priv_bytes(...). These calls are roughly equivalent to calling
  110. EVP_RAND_generate(<public>, ...) and
  111. EVP_RAND_generate(<private>, ...),
  112. respectively.
  113. =head1 RESEEDING
  114. A DRBG instance seeds itself automatically, pulling random input from
  115. its entropy source. The entropy source can be either a trusted operating
  116. system entropy source, or another DRBG with access to such a source.
  117. Automatic reseeding occurs after a predefined number of generate requests.
  118. The selection of the trusted entropy sources is configured at build
  119. time using the --with-rand-seed option. The following sections explain
  120. the reseeding process in more detail.
  121. =head2 Automatic Reseeding
  122. Before satisfying a generate request (L<EVP_RAND_generate(3)>), the DRBG
  123. reseeds itself automatically, if one of the following conditions holds:
  124. - the DRBG was not instantiated (=seeded) yet or has been uninstantiated.
  125. - the number of generate requests since the last reseeding exceeds a
  126. certain threshold, the so called I<reseed_interval>.
  127. This behaviour can be disabled by setting the I<reseed_interval> to 0.
  128. - the time elapsed since the last reseeding exceeds a certain time
  129. interval, the so called I<reseed_time_interval>.
  130. This can be disabled by setting the I<reseed_time_interval> to 0.
  131. - the DRBG is in an error state.
  132. B<Note>: An error state is entered if the entropy source fails while
  133. the DRBG is seeding or reseeding.
  134. The last case ensures that the DRBG automatically recovers
  135. from the error as soon as the entropy source is available again.
  136. =head2 Manual Reseeding
  137. In addition to automatic reseeding, the caller can request an immediate
  138. reseeding of the DRBG with fresh entropy by setting the
  139. I<prediction resistance> parameter to 1 when calling
  140. L<EVP_RAND_generate(3)>.
  141. The document [NIST SP 800-90C] describes prediction resistance requests
  142. in detail and imposes strict conditions on the entropy sources that are
  143. approved for providing prediction resistance.
  144. A request for prediction resistance can only be satisfied by pulling fresh
  145. entropy from a live entropy source (section 5.5.2 of [NIST SP 800-90C]).
  146. It is up to the user to ensure that a live entropy source is configured
  147. and is being used.
  148. For the three shared DRBGs (and only for these) there is another way to
  149. reseed them manually:
  150. If L<RAND_add(3)> is called with a positive I<randomness> argument
  151. (or L<RAND_seed(3)>), then this will immediately reseed the <primary> DRBG.
  152. The <public> and <private> DRBG will detect this on their next generate
  153. call and reseed, pulling randomness from <primary>.
  154. The last feature has been added to support the common practice used with
  155. previous OpenSSL versions to call RAND_add() before calling RAND_bytes().
  156. =head2 Entropy Input and Additional Data
  157. The DRBG distinguishes two different types of random input: I<entropy>,
  158. which comes from a trusted source, and I<additional input>',
  159. which can optionally be added by the user and is considered untrusted.
  160. It is possible to add I<additional input> not only during reseeding,
  161. but also for every generate request.
  162. =head2 Configuring the Random Seed Source
  163. In most cases OpenSSL will automatically choose a suitable seed source
  164. for automatically seeding and reseeding its <primary> DRBG. In some cases
  165. however, it will be necessary to explicitly specify a seed source during
  166. configuration, using the --with-rand-seed option. For more information,
  167. see the INSTALL instructions. There are also operating systems where no
  168. seed source is available and automatic reseeding is disabled by default.
  169. The following two sections describe the reseeding process of the primary
  170. DRBG, depending on whether automatic reseeding is available or not.
  171. =head2 Reseeding the primary DRBG with automatic seeding enabled
  172. Calling RAND_poll() or RAND_add() is not necessary, because the DRBG
  173. pulls the necessary entropy from its source automatically.
  174. However, both calls are permitted, and do reseed the RNG.
  175. RAND_add() can be used to add both kinds of random input, depending on the
  176. value of the I<randomness> argument:
  177. =over 4
  178. =item randomness == 0:
  179. The random bytes are mixed as additional input into the current state of
  180. the DRBG.
  181. Mixing in additional input is not considered a full reseeding, hence the
  182. reseed counter is not reset.
  183. =item randomness > 0:
  184. The random bytes are used as entropy input for a full reseeding
  185. (resp. reinstantiation) if the DRBG is instantiated
  186. (resp. uninstantiated or in an error state).
  187. The number of random bits required for reseeding is determined by the
  188. security strength of the DRBG. Currently it defaults to 256 bits (32 bytes).
  189. It is possible to provide less randomness than required.
  190. In this case the missing randomness will be obtained by pulling random input
  191. from the trusted entropy sources.
  192. =back
  193. NOTE: Manual reseeding is *not allowed* in FIPS mode, because
  194. [NIST SP-800-90Ar1] mandates that entropy *shall not* be provided by
  195. the consuming application for instantiation (Section 9.1) or
  196. reseeding (Section 9.2). For that reason, the I<randomness>
  197. argument is ignored and the random bytes provided by the L<RAND_add(3)> and
  198. L<RAND_seed(3)> calls are treated as additional data.
  199. =head2 Reseeding the primary DRBG with automatic seeding disabled
  200. Calling RAND_poll() will always fail.
  201. RAND_add() needs to be called for initial seeding and periodic reseeding.
  202. At least 48 bytes (384 bits) of randomness have to be provided, otherwise
  203. the (re-)seeding of the DRBG will fail. This corresponds to one and a half
  204. times the security strength of the DRBG. The extra half is used for the
  205. nonce during instantiation.
  206. More precisely, the number of bytes needed for seeding depend on the
  207. I<security strength> of the DRBG, which is set to 256 by default.
  208. =head1 SEE ALSO
  209. L<RAND(7)>, L<EVP_RAND(3)>
  210. =head1 HISTORY
  211. This functionality was added in OpenSSL 3.0.
  212. =head1 COPYRIGHT
  213. Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
  214. Licensed under the Apache License 2.0 (the "License"). You may not use
  215. this file except in compliance with the License. You can obtain a copy
  216. in the file LICENSE in the source distribution or at
  217. L<https://www.openssl.org/source/license.html>.
  218. =cut