rand_drbg.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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(int type, unsigned int flags, RAND_DRBG *parent);
  66. RAND_DRBG *RAND_DRBG_secure_new(int type, unsigned int flags, RAND_DRBG *parent);
  67. int RAND_DRBG_set(RAND_DRBG *drbg, int type, unsigned int flags);
  68. int RAND_DRBG_set_defaults(int type, unsigned int flags);
  69. int RAND_DRBG_instantiate(RAND_DRBG *drbg,
  70. const unsigned char *pers, size_t perslen);
  71. int RAND_DRBG_uninstantiate(RAND_DRBG *drbg);
  72. void RAND_DRBG_free(RAND_DRBG *drbg);
  73. /*
  74. * Object "use" functions.
  75. */
  76. int RAND_DRBG_reseed(RAND_DRBG *drbg,
  77. const unsigned char *adin, size_t adinlen,
  78. int prediction_resistance);
  79. int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
  80. int prediction_resistance,
  81. const unsigned char *adin, size_t adinlen);
  82. int RAND_DRBG_bytes(RAND_DRBG *drbg, unsigned char *out, size_t outlen);
  83. int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval);
  84. int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval);
  85. int RAND_DRBG_set_reseed_defaults(
  86. unsigned int master_reseed_interval,
  87. unsigned int slave_reseed_interval,
  88. time_t master_reseed_time_interval,
  89. time_t slave_reseed_time_interval
  90. );
  91. RAND_DRBG *RAND_DRBG_get0_master(void);
  92. RAND_DRBG *RAND_DRBG_get0_public(void);
  93. RAND_DRBG *RAND_DRBG_get0_private(void);
  94. /*
  95. * EXDATA
  96. */
  97. # define RAND_DRBG_get_ex_new_index(l, p, newf, dupf, freef) \
  98. CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DRBG, l, p, newf, dupf, freef)
  99. int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg);
  100. void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx);
  101. /*
  102. * Callback function typedefs
  103. */
  104. typedef size_t (*RAND_DRBG_get_entropy_fn)(RAND_DRBG *drbg,
  105. unsigned char **pout,
  106. int entropy, size_t min_len,
  107. size_t max_len,
  108. int prediction_resistance);
  109. typedef void (*RAND_DRBG_cleanup_entropy_fn)(RAND_DRBG *ctx,
  110. unsigned char *out, size_t outlen);
  111. typedef size_t (*RAND_DRBG_get_nonce_fn)(RAND_DRBG *drbg, unsigned char **pout,
  112. int entropy, size_t min_len,
  113. size_t max_len);
  114. typedef void (*RAND_DRBG_cleanup_nonce_fn)(RAND_DRBG *drbg,
  115. unsigned char *out, size_t outlen);
  116. int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
  117. RAND_DRBG_get_entropy_fn get_entropy,
  118. RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
  119. RAND_DRBG_get_nonce_fn get_nonce,
  120. RAND_DRBG_cleanup_nonce_fn cleanup_nonce);
  121. # ifdef __cplusplus
  122. }
  123. # endif
  124. #endif