s2n_drbg.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #pragma once
  16. #include <openssl/evp.h>
  17. #include "crypto/s2n_hash.h"
  18. #include "utils/s2n_blob.h"
  19. #include "utils/s2n_result.h"
  20. #define S2N_DRBG_BLOCK_SIZE 16
  21. #define S2N_DRBG_MAX_KEY_SIZE 32
  22. #define S2N_DRBG_MAX_SEED_SIZE (S2N_DRBG_BLOCK_SIZE + S2N_DRBG_MAX_KEY_SIZE)
  23. /* The maximum size of any one request: from NIST SP800-90A 10.2.1 Table 3 */
  24. #define S2N_DRBG_GENERATE_LIMIT 8192
  25. struct s2n_drbg {
  26. /* Track how many bytes have been used */
  27. uint64_t bytes_used;
  28. EVP_CIPHER_CTX *ctx;
  29. /* The current DRBG 'value' */
  30. uint8_t v[S2N_DRBG_BLOCK_SIZE];
  31. /* Used only by the unit tests: how many times has entropy been mixed in */
  32. uint64_t mixes;
  33. };
  34. /*
  35. * S2N_AES_128_CTR_NO_DF_PR is a deterministic random bit generator using AES 128 in counter mode (AES_128_CTR). It does not
  36. * use a derivation function (NO_DF) on the seed but does have prediction resistance (PR).
  37. *
  38. * S2N_AES_256_CTR_NO_DF_PR is a deterministic random bit generator using AES 256 in counter mode (AES_128_CTR). It does not
  39. * use a derivation function on the seed but does have prediction resistance.
  40. */
  41. typedef enum {
  42. S2N_AES_128_CTR_NO_DF_PR,
  43. S2N_AES_256_CTR_NO_DF_PR
  44. } s2n_drbg_mode;
  45. /* Per NIST SP 800-90C 6.3
  46. *
  47. * s2n's DRBG uses prediction resistance and does not support the
  48. * additional_input parameter (which per 800-90C may be zero).
  49. *
  50. * The security strength provided by s2n's DRBG is either 128 or 256 bits
  51. * depending on the s2n_drbg_mode passed in.
  52. */
  53. S2N_RESULT s2n_drbg_instantiate(struct s2n_drbg *drbg, struct s2n_blob *personalization_string, const s2n_drbg_mode mode);
  54. S2N_RESULT s2n_drbg_generate(struct s2n_drbg *drbg, struct s2n_blob *returned_bits);
  55. S2N_RESULT s2n_drbg_wipe(struct s2n_drbg *drbg);
  56. S2N_RESULT s2n_drbg_bytes_used(struct s2n_drbg *drbg, uint64_t *bytes_used);
  57. /* Use for testing only */
  58. S2N_RESULT s2n_ignore_prediction_resistance_for_testing(bool true_or_false);