drbg_local.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright 1995-2024 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. /* ASCII: "OpenSSL NIST SP 800-90A DRBG", in hex for EBCDIC compatibility */
  46. #define DRBG_DEFAULT_PERS_STRING "\x4f\x70\x65\x6e\x53\x53\x4c\x20\x4e\x49\x53\x54\x20\x53\x50\x20\x38\x30\x30\x2d\x39\x30\x41\x20\x44\x52\x42\x47"
  47. typedef struct prov_drbg_st PROV_DRBG;
  48. /* DRBG status values */
  49. typedef enum drbg_status_e {
  50. DRBG_UNINITIALISED,
  51. DRBG_READY,
  52. DRBG_ERROR
  53. } DRBG_STATUS;
  54. /*
  55. * The state of all types of DRBGs.
  56. */
  57. struct prov_drbg_st {
  58. CRYPTO_RWLOCK *lock;
  59. PROV_CTX *provctx;
  60. /* Virtual functions are cache here */
  61. int (*instantiate)(PROV_DRBG *drbg,
  62. const unsigned char *entropy, size_t entropylen,
  63. const unsigned char *nonce, size_t noncelen,
  64. const unsigned char *pers, size_t perslen);
  65. int (*uninstantiate)(PROV_DRBG *ctx);
  66. int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,
  67. const unsigned char *adin, size_t adin_len);
  68. int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,
  69. const unsigned char *adin, size_t adin_len);
  70. /* Parent PROV_RAND and its dispatch table functions */
  71. void *parent;
  72. OSSL_FUNC_rand_enable_locking_fn *parent_enable_locking;
  73. OSSL_FUNC_rand_lock_fn *parent_lock;
  74. OSSL_FUNC_rand_unlock_fn *parent_unlock;
  75. OSSL_FUNC_rand_get_ctx_params_fn *parent_get_ctx_params;
  76. OSSL_FUNC_rand_nonce_fn *parent_nonce;
  77. OSSL_FUNC_rand_get_seed_fn *parent_get_seed;
  78. OSSL_FUNC_rand_clear_seed_fn *parent_clear_seed;
  79. const OSSL_DISPATCH *parent_dispatch;
  80. /*
  81. * Stores the return value of openssl_get_fork_id() as of when we last
  82. * reseeded. The DRBG reseeds automatically whenever drbg->fork_id !=
  83. * openssl_get_fork_id(). Used to provide fork-safety and reseed this
  84. * DRBG in the child process.
  85. */
  86. int fork_id;
  87. unsigned short flags; /* various external flags */
  88. /*
  89. * The following parameters are setup by the per-type "init" function.
  90. *
  91. * The supported types and their init functions are:
  92. * (1) CTR_DRBG: drbg_ctr_init().
  93. * (2) HMAC_DRBG: drbg_hmac_init().
  94. * (3) HASH_DRBG: drbg_hash_init().
  95. *
  96. * The parameters are closely related to the ones described in
  97. * section '10.2.1 CTR_DRBG' of [NIST SP 800-90Ar1], with one
  98. * crucial difference: In the NIST standard, all counts are given
  99. * in bits, whereas in OpenSSL entropy counts are given in bits
  100. * and buffer lengths are given in bytes.
  101. *
  102. * Since this difference has lead to some confusion in the past,
  103. * (see [GitHub Issue #2443], formerly [rt.openssl.org #4055])
  104. * the 'len' suffix has been added to all buffer sizes for
  105. * clarification.
  106. */
  107. unsigned int strength;
  108. size_t max_request;
  109. size_t min_entropylen, max_entropylen;
  110. size_t min_noncelen, max_noncelen;
  111. size_t max_perslen, max_adinlen;
  112. /*
  113. * Counts the number of generate requests since the last reseed
  114. * (Starts at 1). This value is the reseed_counter as defined in
  115. * NIST SP 800-90Ar1
  116. */
  117. unsigned int generate_counter;
  118. /*
  119. * Maximum number of generate requests until a reseed is required.
  120. * This value is ignored if it is zero.
  121. */
  122. unsigned int reseed_interval;
  123. /* Stores the time when the last reseeding occurred */
  124. time_t reseed_time;
  125. /*
  126. * Specifies the maximum time interval (in seconds) between reseeds.
  127. * This value is ignored if it is zero.
  128. */
  129. time_t reseed_time_interval;
  130. /*
  131. * Counts the number of reseeds since instantiation.
  132. * This value is ignored if it is zero.
  133. *
  134. * This counter is used only for seed propagation from the <master> DRBG
  135. * to its two children, the <public> and <private> DRBG. This feature is
  136. * very special and its sole purpose is to ensure that any randomness which
  137. * is added by PROV_add() or PROV_seed() will have an immediate effect on
  138. * the output of PROV_bytes() resp. PROV_priv_bytes().
  139. */
  140. TSAN_QUALIFIER unsigned int reseed_counter;
  141. unsigned int reseed_next_counter;
  142. unsigned int parent_reseed_counter;
  143. size_t seedlen;
  144. DRBG_STATUS state;
  145. /* DRBG specific data */
  146. void *data;
  147. /* Entropy and nonce gathering callbacks */
  148. void *callback_arg;
  149. OSSL_INOUT_CALLBACK *get_entropy_fn;
  150. OSSL_CALLBACK *cleanup_entropy_fn;
  151. OSSL_INOUT_CALLBACK *get_nonce_fn;
  152. OSSL_CALLBACK *cleanup_nonce_fn;
  153. };
  154. PROV_DRBG *ossl_rand_drbg_new
  155. (void *provctx, void *parent, const OSSL_DISPATCH *parent_dispatch,
  156. int (*dnew)(PROV_DRBG *ctx),
  157. void (*dfree)(void *vctx),
  158. int (*instantiate)(PROV_DRBG *drbg,
  159. const unsigned char *entropy, size_t entropylen,
  160. const unsigned char *nonce, size_t noncelen,
  161. const unsigned char *pers, size_t perslen),
  162. int (*uninstantiate)(PROV_DRBG *ctx),
  163. int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,
  164. const unsigned char *adin, size_t adin_len),
  165. int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,
  166. const unsigned char *adin, size_t adin_len));
  167. void ossl_rand_drbg_free(PROV_DRBG *drbg);
  168. int ossl_prov_drbg_instantiate(PROV_DRBG *drbg, unsigned int strength,
  169. int prediction_resistance,
  170. const unsigned char *pers, size_t perslen);
  171. int ossl_prov_drbg_uninstantiate(PROV_DRBG *drbg);
  172. int ossl_prov_drbg_reseed(PROV_DRBG *drbg, int prediction_resistance,
  173. const unsigned char *ent, size_t ent_len,
  174. const unsigned char *adin, size_t adinlen);
  175. int ossl_prov_drbg_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen,
  176. unsigned int strength, int prediction_resistance,
  177. const unsigned char *adin, size_t adinlen);
  178. /* Seeding api */
  179. OSSL_FUNC_rand_get_seed_fn ossl_drbg_get_seed;
  180. OSSL_FUNC_rand_clear_seed_fn ossl_drbg_clear_seed;
  181. /* Verify that an array of numeric values is all zero */
  182. #define PROV_DRBG_VERIFY_ZEROIZATION(v) \
  183. { \
  184. size_t i; \
  185. \
  186. for (i = 0; i < OSSL_NELEM(v); i++) \
  187. if ((v)[i] != 0) \
  188. goto err; \
  189. }
  190. /* locking api */
  191. OSSL_FUNC_rand_enable_locking_fn ossl_drbg_enable_locking;
  192. OSSL_FUNC_rand_lock_fn ossl_drbg_lock;
  193. OSSL_FUNC_rand_unlock_fn ossl_drbg_unlock;
  194. /* Common parameters for all of our DRBGs */
  195. int ossl_drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[]);
  196. int ossl_drbg_get_ctx_params_no_lock(PROV_DRBG *drbg, OSSL_PARAM params[],
  197. int *complete);
  198. int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[]);
  199. #define OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON \
  200. OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), \
  201. OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL)
  202. #define OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON \
  203. OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL), \
  204. OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL), \
  205. OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), \
  206. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_ENTROPYLEN, NULL), \
  207. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ENTROPYLEN, NULL), \
  208. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_NONCELEN, NULL), \
  209. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_NONCELEN, NULL), \
  210. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_PERSLEN, NULL), \
  211. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ADINLEN, NULL), \
  212. OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_COUNTER, NULL), \
  213. OSSL_PARAM_time_t(OSSL_DRBG_PARAM_RESEED_TIME, NULL), \
  214. OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, NULL), \
  215. OSSL_PARAM_uint64(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, NULL)
  216. /* Continuous test "entropy" calls */
  217. size_t ossl_crngt_get_entropy(PROV_DRBG *drbg,
  218. unsigned char **pout,
  219. int entropy, size_t min_len, size_t max_len,
  220. int prediction_resistance);
  221. void ossl_crngt_cleanup_entropy(PROV_DRBG *drbg,
  222. unsigned char *out, size_t outlen);
  223. /* Confirm digest is allowed to be used with a DRBG */
  224. int ossl_drbg_verify_digest(ossl_unused OSSL_LIB_CTX *libctx, const EVP_MD *md);
  225. #endif