drbg_local.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_CRYPTO_PROV_LOCAL_H
  10. # define OSSL_CRYPTO_PROV_LOCAL_H
  11. # include <openssl/evp.h>
  12. # include <openssl/core_dispatch.h>
  13. # include <openssl/core_names.h>
  14. # include <openssl/params.h>
  15. # include "internal/tsan_assist.h"
  16. # include "internal/nelem.h"
  17. # include "internal/numbers.h"
  18. # include "prov/provider_ctx.h"
  19. /* How many times to read the TSC as a randomness source. */
  20. # define TSC_READ_COUNT 4
  21. /* Maximum reseed intervals */
  22. # define MAX_RESEED_INTERVAL (1 << 24)
  23. # define MAX_RESEED_TIME_INTERVAL (1 << 20) /* approx. 12 days */
  24. /* Default reseed intervals */
  25. # define RESEED_INTERVAL (1 << 8)
  26. # define TIME_INTERVAL (60*60) /* 1 hour */
  27. /*
  28. * The number of bytes that constitutes an atomic lump of entropy with respect
  29. * to the FIPS 140-2 section 4.9.2 Conditional Tests. The size is somewhat
  30. * arbitrary, the smaller the value, the less entropy is consumed on first
  31. * read but the higher the probability of the test failing by accident.
  32. *
  33. * The value is in bytes.
  34. */
  35. #define CRNGT_BUFSIZ 16
  36. /*
  37. * Maximum input size for the DRBG (entropy, nonce, personalization string)
  38. *
  39. * NIST SP800 90Ar1 allows a maximum of (1 << 35) bits i.e., (1 << 32) bytes.
  40. *
  41. * We lower it to 'only' INT32_MAX bytes, which is equivalent to 2 gigabytes.
  42. */
  43. # define DRBG_MAX_LENGTH INT32_MAX
  44. /* The default nonce */
  45. #ifdef CHARSET_EBCDIC
  46. # define DRBG_DEFAULT_PERS_STRING { 0x4f, 0x70, 0x65, 0x6e, 0x53, 0x53, \
  47. 0x4c, 0x20, 0x4e, 0x49, 0x53, 0x54, 0x20, 0x53, 0x50, 0x20, 0x38, 0x30, \
  48. 0x30, 0x2d, 0x39, 0x30, 0x41, 0x20, 0x44, 0x52, 0x42, 0x47, 0x00};
  49. #else
  50. # define DRBG_DEFAULT_PERS_STRING "OpenSSL NIST SP 800-90A DRBG"
  51. #endif
  52. typedef struct prov_drbg_st PROV_DRBG;
  53. /* DRBG status values */
  54. typedef enum drbg_status_e {
  55. DRBG_UNINITIALISED,
  56. DRBG_READY,
  57. DRBG_ERROR
  58. } DRBG_STATUS;
  59. /*
  60. * The state of all types of DRBGs.
  61. */
  62. struct prov_drbg_st {
  63. CRYPTO_RWLOCK *lock;
  64. PROV_CTX *provctx;
  65. /* Virtual functions are cache here */
  66. int (*instantiate)(PROV_DRBG *drbg,
  67. const unsigned char *entropy, size_t entropylen,
  68. const unsigned char *nonce, size_t noncelen,
  69. const unsigned char *pers, size_t perslen);
  70. int (*uninstantiate)(PROV_DRBG *ctx);
  71. int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,
  72. const unsigned char *adin, size_t adin_len);
  73. int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,
  74. const unsigned char *adin, size_t adin_len);
  75. /* Parent PROV_RAND and its dispatch table functions */
  76. void *parent;
  77. OSSL_FUNC_rand_enable_locking_fn *parent_enable_locking;
  78. OSSL_FUNC_rand_lock_fn *parent_lock;
  79. OSSL_FUNC_rand_unlock_fn *parent_unlock;
  80. OSSL_FUNC_rand_get_ctx_params_fn *parent_get_ctx_params;
  81. OSSL_FUNC_rand_nonce_fn *parent_nonce;
  82. OSSL_FUNC_rand_get_seed_fn *parent_get_seed;
  83. OSSL_FUNC_rand_clear_seed_fn *parent_clear_seed;
  84. const OSSL_DISPATCH *parent_dispatch;
  85. /*
  86. * Stores the return value of openssl_get_fork_id() as of when we last
  87. * reseeded. The DRBG reseeds automatically whenever drbg->fork_id !=
  88. * openssl_get_fork_id(). Used to provide fork-safety and reseed this
  89. * DRBG in the child process.
  90. */
  91. int fork_id;
  92. unsigned short flags; /* various external flags */
  93. /*
  94. * The following parameters are setup by the per-type "init" function.
  95. *
  96. * The supported types and their init functions are:
  97. * (1) CTR_DRBG: drbg_ctr_init().
  98. * (2) HMAC_DRBG: drbg_hmac_init().
  99. * (3) HASH_DRBG: drbg_hash_init().
  100. *
  101. * The parameters are closely related to the ones described in
  102. * section '10.2.1 CTR_DRBG' of [NIST SP 800-90Ar1], with one
  103. * crucial difference: In the NIST standard, all counts are given
  104. * in bits, whereas in OpenSSL entropy counts are given in bits
  105. * and buffer lengths are given in bytes.
  106. *
  107. * Since this difference has lead to some confusion in the past,
  108. * (see [GitHub Issue #2443], formerly [rt.openssl.org #4055])
  109. * the 'len' suffix has been added to all buffer sizes for
  110. * clarification.
  111. */
  112. unsigned int strength;
  113. size_t max_request;
  114. size_t min_entropylen, max_entropylen;
  115. size_t min_noncelen, max_noncelen;
  116. size_t max_perslen, max_adinlen;
  117. /*
  118. * Counts the number of generate requests since the last reseed
  119. * (Starts at 1). This value is the reseed_counter as defined in
  120. * NIST SP 800-90Ar1
  121. */
  122. unsigned int generate_counter;
  123. /*
  124. * Maximum number of generate requests until a reseed is required.
  125. * This value is ignored if it is zero.
  126. */
  127. unsigned int reseed_interval;
  128. /* Stores the time when the last reseeding occurred */
  129. time_t reseed_time;
  130. /*
  131. * Specifies the maximum time interval (in seconds) between reseeds.
  132. * This value is ignored if it is zero.
  133. */
  134. time_t reseed_time_interval;
  135. /*
  136. * Counts the number of reseeds since instantiation.
  137. * This value is ignored if it is zero.
  138. *
  139. * This counter is used only for seed propagation from the <master> DRBG
  140. * to its two children, the <public> and <private> DRBG. This feature is
  141. * very special and its sole purpose is to ensure that any randomness which
  142. * is added by PROV_add() or PROV_seed() will have an immediate effect on
  143. * the output of PROV_bytes() resp. PROV_priv_bytes().
  144. */
  145. TSAN_QUALIFIER unsigned int reseed_counter;
  146. unsigned int reseed_next_counter;
  147. unsigned int parent_reseed_counter;
  148. size_t seedlen;
  149. DRBG_STATUS state;
  150. /* DRBG specific data */
  151. void *data;
  152. /* Entropy and nonce gathering callbacks */
  153. void *callback_arg;
  154. OSSL_INOUT_CALLBACK *get_entropy_fn;
  155. OSSL_CALLBACK *cleanup_entropy_fn;
  156. OSSL_INOUT_CALLBACK *get_nonce_fn;
  157. OSSL_CALLBACK *cleanup_nonce_fn;
  158. };
  159. PROV_DRBG *ossl_rand_drbg_new
  160. (void *provctx, void *parent, const OSSL_DISPATCH *parent_dispatch,
  161. int (*dnew)(PROV_DRBG *ctx),
  162. int (*instantiate)(PROV_DRBG *drbg,
  163. const unsigned char *entropy, size_t entropylen,
  164. const unsigned char *nonce, size_t noncelen,
  165. const unsigned char *pers, size_t perslen),
  166. int (*uninstantiate)(PROV_DRBG *ctx),
  167. int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,
  168. const unsigned char *adin, size_t adin_len),
  169. int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,
  170. const unsigned char *adin, size_t adin_len));
  171. void ossl_rand_drbg_free(PROV_DRBG *drbg);
  172. int ossl_prov_drbg_instantiate(PROV_DRBG *drbg, unsigned int strength,
  173. int prediction_resistance,
  174. const unsigned char *pers, size_t perslen);
  175. int ossl_prov_drbg_uninstantiate(PROV_DRBG *drbg);
  176. int ossl_prov_drbg_reseed(PROV_DRBG *drbg, int prediction_resistance,
  177. const unsigned char *ent, size_t ent_len,
  178. const unsigned char *adin, size_t adinlen);
  179. int ossl_prov_drbg_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen,
  180. unsigned int strength, int prediction_resistance,
  181. const unsigned char *adin, size_t adinlen);
  182. /* Seeding api */
  183. OSSL_FUNC_rand_get_seed_fn ossl_drbg_get_seed;
  184. OSSL_FUNC_rand_clear_seed_fn ossl_drbg_clear_seed;
  185. /* Verify that an array of numeric values is all zero */
  186. #define PROV_DRBG_VERYIFY_ZEROIZATION(v) \
  187. { \
  188. size_t i; \
  189. \
  190. for (i = 0; i < OSSL_NELEM(v); i++) \
  191. if ((v)[i] != 0) \
  192. return 0; \
  193. }
  194. /* locking api */
  195. OSSL_FUNC_rand_enable_locking_fn ossl_drbg_enable_locking;
  196. OSSL_FUNC_rand_lock_fn ossl_drbg_lock;
  197. OSSL_FUNC_rand_unlock_fn ossl_drbg_unlock;
  198. /* Common parameters for all of our DRBGs */
  199. int ossl_drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[]);
  200. int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[]);
  201. #define OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON \
  202. OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), \
  203. OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL)
  204. #define OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON \
  205. OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL), \
  206. OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL), \
  207. OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), \
  208. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_ENTROPYLEN, NULL), \
  209. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ENTROPYLEN, NULL), \
  210. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_NONCELEN, NULL), \
  211. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_NONCELEN, NULL), \
  212. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_PERSLEN, NULL), \
  213. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ADINLEN, NULL), \
  214. OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_COUNTER, NULL), \
  215. OSSL_PARAM_time_t(OSSL_DRBG_PARAM_RESEED_TIME, NULL), \
  216. OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), \
  217. OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL)
  218. /* Continuous test "entropy" calls */
  219. size_t ossl_crngt_get_entropy(PROV_DRBG *drbg,
  220. unsigned char **pout,
  221. int entropy, size_t min_len, size_t max_len,
  222. int prediction_resistance);
  223. void ossl_crngt_cleanup_entropy(PROV_DRBG *drbg,
  224. unsigned char *out, size_t outlen);
  225. #endif