rand_drbg.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2017-2018 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 HEADER_DRBG_RAND_H
  10. # define HEADER_DRBG_RAND_H
  11. # include <time.h>
  12. # include <openssl/ossl_typ.h>
  13. # include <openssl/obj_mac.h>
  14. /*
  15. * RAND_DRBG flags
  16. *
  17. * Note: if new flags are added, the constant `rand_drbg_used_flags`
  18. * in drbg_lib.c needs to be updated accordingly.
  19. */
  20. /* In CTR mode, disable derivation function ctr_df */
  21. # define RAND_DRBG_FLAG_CTR_NO_DF 0x1
  22. /*
  23. * This flag is only used when a digest NID is specified (i.e: not a CTR cipher)
  24. * Selects DRBG_HMAC if this is set otherwise use DRBG_HASH.
  25. */
  26. # define RAND_DRBG_FLAG_HMAC 0x2
  27. /* Used by RAND_DRBG_set_defaults() to set the master DRBG type and flags. */
  28. # define RAND_DRBG_FLAG_MASTER 0x4
  29. /* Used by RAND_DRBG_set_defaults() to set the public DRBG type and flags. */
  30. # define RAND_DRBG_FLAG_PUBLIC 0x8
  31. /* Used by RAND_DRBG_set_defaults() to set the private DRBG type and flags. */
  32. # define RAND_DRBG_FLAG_PRIVATE 0x10
  33. # if !OPENSSL_API_3
  34. /* This #define was replaced by an internal constant and should not be used. */
  35. # define RAND_DRBG_USED_FLAGS (RAND_DRBG_FLAG_CTR_NO_DF)
  36. # endif
  37. /*
  38. * Default security strength (in the sense of [NIST SP 800-90Ar1])
  39. *
  40. * NIST SP 800-90Ar1 supports the strength of the DRBG being smaller than that
  41. * of the cipher by collecting less entropy. The current DRBG implementation
  42. * does not take RAND_DRBG_STRENGTH into account and sets the strength of the
  43. * DRBG to that of the cipher.
  44. *
  45. * RAND_DRBG_STRENGTH is currently only used for the legacy RAND
  46. * implementation.
  47. *
  48. * Currently supported ciphers are: NID_aes_128_ctr, NID_aes_192_ctr and
  49. * NID_aes_256_ctr.
  50. * The digest types for DRBG_hash or DRBG_hmac are: NID_sha1, NID_sha224,
  51. * NID_sha256, NID_sha384, NID_sha512, NID_sha512_224, NID_sha512_256,
  52. * NID_sha3_224, NID_sha3_256, NID_sha3_384 and NID_sha3_512.
  53. */
  54. # define RAND_DRBG_STRENGTH 256
  55. /* Default drbg type */
  56. # define RAND_DRBG_TYPE NID_aes_256_ctr
  57. /* Default drbg flags */
  58. # define RAND_DRBG_FLAGS 0
  59. # ifdef __cplusplus
  60. extern "C" {
  61. # endif
  62. /*
  63. * Object lifetime functions.
  64. */
  65. RAND_DRBG *RAND_DRBG_new_ex(OPENSSL_CTX *ctx, int type, unsigned int flags,
  66. RAND_DRBG *parent);
  67. RAND_DRBG *RAND_DRBG_secure_new_ex(OPENSSL_CTX *ctx, int type,
  68. unsigned int flags, RAND_DRBG *parent);
  69. RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent);
  70. RAND_DRBG *RAND_DRBG_secure_new(int type, unsigned int flags, RAND_DRBG *parent);
  71. int RAND_DRBG_set(RAND_DRBG *drbg, int type, unsigned int flags);
  72. int RAND_DRBG_set_defaults(int type, unsigned int flags);
  73. int RAND_DRBG_instantiate(RAND_DRBG *drbg,
  74. const unsigned char *pers, size_t perslen);
  75. int RAND_DRBG_uninstantiate(RAND_DRBG *drbg);
  76. void RAND_DRBG_free(RAND_DRBG *drbg);
  77. /*
  78. * Object "use" functions.
  79. */
  80. int RAND_DRBG_reseed(RAND_DRBG *drbg,
  81. const unsigned char *adin, size_t adinlen,
  82. int prediction_resistance);
  83. int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
  84. int prediction_resistance,
  85. const unsigned char *adin, size_t adinlen);
  86. int RAND_DRBG_bytes(RAND_DRBG *drbg, unsigned char *out, size_t outlen);
  87. int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval);
  88. int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval);
  89. int RAND_DRBG_set_reseed_defaults(
  90. unsigned int master_reseed_interval,
  91. unsigned int slave_reseed_interval,
  92. time_t master_reseed_time_interval,
  93. time_t slave_reseed_time_interval
  94. );
  95. RAND_DRBG *OPENSSL_CTX_get0_master_drbg(OPENSSL_CTX *ctx);
  96. RAND_DRBG *OPENSSL_CTX_get0_public_drbg(OPENSSL_CTX *ctx);
  97. RAND_DRBG *OPENSSL_CTX_get0_private_drbg(OPENSSL_CTX *ctx);
  98. RAND_DRBG *RAND_DRBG_get0_master(void);
  99. RAND_DRBG *RAND_DRBG_get0_public(void);
  100. RAND_DRBG *RAND_DRBG_get0_private(void);
  101. /*
  102. * EXDATA
  103. */
  104. # define RAND_DRBG_get_ex_new_index(l, p, newf, dupf, freef) \
  105. CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DRBG, l, p, newf, dupf, freef)
  106. int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg);
  107. void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx);
  108. /*
  109. * Callback function typedefs
  110. */
  111. typedef size_t (*RAND_DRBG_get_entropy_fn)(RAND_DRBG *drbg,
  112. unsigned char **pout,
  113. int entropy, size_t min_len,
  114. size_t max_len,
  115. int prediction_resistance);
  116. typedef void (*RAND_DRBG_cleanup_entropy_fn)(RAND_DRBG *ctx,
  117. unsigned char *out, size_t outlen);
  118. typedef size_t (*RAND_DRBG_get_nonce_fn)(RAND_DRBG *drbg, unsigned char **pout,
  119. int entropy, size_t min_len,
  120. size_t max_len);
  121. typedef void (*RAND_DRBG_cleanup_nonce_fn)(RAND_DRBG *drbg,
  122. unsigned char *out, size_t outlen);
  123. int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
  124. RAND_DRBG_get_entropy_fn get_entropy,
  125. RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
  126. RAND_DRBG_get_nonce_fn get_nonce,
  127. RAND_DRBG_cleanup_nonce_fn cleanup_nonce);
  128. # ifdef __cplusplus
  129. }
  130. # endif
  131. #endif