drbg_local.h 9.6 KB

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