rand_drbg.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. # if OPENSSL_API_COMPAT < 0x10200000L
  23. /* This #define was replaced by an internal constant and should not be used. */
  24. # define RAND_DRBG_USED_FLAGS (RAND_DRBG_FLAG_CTR_NO_DF)
  25. # endif
  26. /*
  27. * Default security strength (in the sense of [NIST SP 800-90Ar1])
  28. *
  29. * NIST SP 800-90Ar1 supports the strength of the DRBG being smaller than that
  30. * of the cipher by collecting less entropy. The current DRBG implementation
  31. * does not take RAND_DRBG_STRENGTH into account and sets the strength of the
  32. * DRBG to that of the cipher.
  33. *
  34. * RAND_DRBG_STRENGTH is currently only used for the legacy RAND
  35. * implementation.
  36. *
  37. * Currently supported ciphers are: NID_aes_128_ctr, NID_aes_192_ctr and
  38. * NID_aes_256_ctr
  39. */
  40. # define RAND_DRBG_STRENGTH 256
  41. /* Default drbg type */
  42. # define RAND_DRBG_TYPE NID_aes_256_ctr
  43. /* Default drbg flags */
  44. # define RAND_DRBG_FLAGS 0
  45. # ifdef __cplusplus
  46. extern "C" {
  47. # endif
  48. /*
  49. * Object lifetime functions.
  50. */
  51. RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent);
  52. RAND_DRBG *RAND_DRBG_secure_new(int type, unsigned int flags, RAND_DRBG *parent);
  53. int RAND_DRBG_set(RAND_DRBG *drbg, int type, unsigned int flags);
  54. int RAND_DRBG_set_defaults(int type, unsigned int flags);
  55. int RAND_DRBG_instantiate(RAND_DRBG *drbg,
  56. const unsigned char *pers, size_t perslen);
  57. int RAND_DRBG_uninstantiate(RAND_DRBG *drbg);
  58. void RAND_DRBG_free(RAND_DRBG *drbg);
  59. /*
  60. * Object "use" functions.
  61. */
  62. int RAND_DRBG_reseed(RAND_DRBG *drbg,
  63. const unsigned char *adin, size_t adinlen,
  64. int prediction_resistance);
  65. int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
  66. int prediction_resistance,
  67. const unsigned char *adin, size_t adinlen);
  68. int RAND_DRBG_bytes(RAND_DRBG *drbg, unsigned char *out, size_t outlen);
  69. int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval);
  70. int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval);
  71. int RAND_DRBG_set_reseed_defaults(
  72. unsigned int master_reseed_interval,
  73. unsigned int slave_reseed_interval,
  74. time_t master_reseed_time_interval,
  75. time_t slave_reseed_time_interval
  76. );
  77. RAND_DRBG *RAND_DRBG_get0_master(void);
  78. RAND_DRBG *RAND_DRBG_get0_public(void);
  79. RAND_DRBG *RAND_DRBG_get0_private(void);
  80. /*
  81. * EXDATA
  82. */
  83. # define RAND_DRBG_get_ex_new_index(l, p, newf, dupf, freef) \
  84. CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DRBG, l, p, newf, dupf, freef)
  85. int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg);
  86. void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx);
  87. /*
  88. * Callback function typedefs
  89. */
  90. typedef size_t (*RAND_DRBG_get_entropy_fn)(RAND_DRBG *drbg,
  91. unsigned char **pout,
  92. int entropy, size_t min_len,
  93. size_t max_len,
  94. int prediction_resistance);
  95. typedef void (*RAND_DRBG_cleanup_entropy_fn)(RAND_DRBG *ctx,
  96. unsigned char *out, size_t outlen);
  97. typedef size_t (*RAND_DRBG_get_nonce_fn)(RAND_DRBG *drbg, unsigned char **pout,
  98. int entropy, size_t min_len,
  99. size_t max_len);
  100. typedef void (*RAND_DRBG_cleanup_nonce_fn)(RAND_DRBG *drbg,
  101. unsigned char *out, size_t outlen);
  102. int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
  103. RAND_DRBG_get_entropy_fn get_entropy,
  104. RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
  105. RAND_DRBG_get_nonce_fn get_nonce,
  106. RAND_DRBG_cleanup_nonce_fn cleanup_nonce);
  107. # ifdef __cplusplus
  108. }
  109. # endif
  110. #endif