RAND_DRBG.pod 11 KB

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