25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

internal.h 3.6 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #ifndef OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H
  15. #define OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H
  16. #include <openssl/aes.h>
  17. #include "../../internal.h"
  18. #include "../modes/internal.h"
  19. #if defined(__cplusplus)
  20. extern "C" {
  21. #endif
  22. // RAND_bytes_with_additional_data samples from the RNG after mixing 32 bytes
  23. // from |user_additional_data| in.
  24. void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,
  25. const uint8_t user_additional_data[32]);
  26. // CRYPTO_sysrand fills |len| bytes at |buf| with entropy from the operating
  27. // system.
  28. void CRYPTO_sysrand(uint8_t *buf, size_t len);
  29. // rand_fork_unsafe_buffering_enabled returns whether fork-unsafe buffering has
  30. // been enabled via |RAND_enable_fork_unsafe_buffering|.
  31. int rand_fork_unsafe_buffering_enabled(void);
  32. // CTR_DRBG_STATE contains the state of a CTR_DRBG based on AES-256. See SP
  33. // 800-90Ar1.
  34. typedef struct {
  35. alignas(16) AES_KEY ks;
  36. block128_f block;
  37. ctr128_f ctr;
  38. union {
  39. uint8_t bytes[16];
  40. uint32_t words[4];
  41. } counter;
  42. uint64_t reseed_counter;
  43. } CTR_DRBG_STATE;
  44. // See SP 800-90Ar1, table 3.
  45. #define CTR_DRBG_ENTROPY_LEN 48
  46. #define CTR_DRBG_MAX_GENERATE_LENGTH 65536
  47. // CTR_DRBG_init initialises |*drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of
  48. // entropy in |entropy| and, optionally, a personalization string up to
  49. // |CTR_DRBG_ENTROPY_LEN| bytes in length. It returns one on success and zero
  50. // on error.
  51. OPENSSL_EXPORT int CTR_DRBG_init(CTR_DRBG_STATE *drbg,
  52. const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
  53. const uint8_t *personalization,
  54. size_t personalization_len);
  55. // CTR_DRBG_reseed reseeds |drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of entropy
  56. // in |entropy| and, optionally, up to |CTR_DRBG_ENTROPY_LEN| bytes of
  57. // additional data. It returns one on success or zero on error.
  58. OPENSSL_EXPORT int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg,
  59. const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
  60. const uint8_t *additional_data,
  61. size_t additional_data_len);
  62. // CTR_DRBG_generate processes to up |CTR_DRBG_ENTROPY_LEN| bytes of additional
  63. // data (if any) and then writes |out_len| random bytes to |out|, where
  64. // |out_len| <= |CTR_DRBG_MAX_GENERATE_LENGTH|. It returns one on success or
  65. // zero on error.
  66. OPENSSL_EXPORT int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out,
  67. size_t out_len,
  68. const uint8_t *additional_data,
  69. size_t additional_data_len);
  70. // CTR_DRBG_clear zeroises the state of |drbg|.
  71. OPENSSL_EXPORT void CTR_DRBG_clear(CTR_DRBG_STATE *drbg);
  72. #if defined(__cplusplus)
  73. } // extern C
  74. #endif
  75. #endif // OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H