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 15 #ifndef OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H 16 #define OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H 17 18 #include <openssl/aes.h> 19 #include <openssl/cpu.h> 20 21 #include "../../internal.h" 22 #include "../modes/internal.h" 23 24 #if defined(__cplusplus) 25 extern "C" { 26 #endif 27 28 29 // RAND_bytes_with_additional_data samples from the RNG after mixing 32 bytes 30 // from |user_additional_data| in. 31 void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len, 32 const uint8_t user_additional_data[32]); 33 34 // CRYPTO_sysrand fills |len| bytes at |buf| with entropy from the operating 35 // system. 36 void CRYPTO_sysrand(uint8_t *buf, size_t len); 37 38 // rand_fork_unsafe_buffering_enabled returns whether fork-unsafe buffering has 39 // been enabled via |RAND_enable_fork_unsafe_buffering|. 40 int rand_fork_unsafe_buffering_enabled(void); 41 42 // CTR_DRBG_STATE contains the state of a CTR_DRBG based on AES-256. See SP 43 // 800-90Ar1. 44 typedef struct { 45 AES_KEY ks; 46 block128_f block; 47 ctr128_f ctr; 48 union { 49 uint8_t bytes[16]; 50 uint32_t words[4]; 51 } counter; 52 uint64_t reseed_counter; 53 } CTR_DRBG_STATE; 54 55 // See SP 800-90Ar1, table 3. 56 #define CTR_DRBG_ENTROPY_LEN 48 57 #define CTR_DRBG_MAX_GENERATE_LENGTH 65536 58 59 // CTR_DRBG_init initialises |*drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of 60 // entropy in |entropy| and, optionally, a personalization string up to 61 // |CTR_DRBG_ENTROPY_LEN| bytes in length. It returns one on success and zero 62 // on error. 63 OPENSSL_EXPORT int CTR_DRBG_init(CTR_DRBG_STATE *drbg, 64 const uint8_t entropy[CTR_DRBG_ENTROPY_LEN], 65 const uint8_t *personalization, 66 size_t personalization_len); 67 68 // CTR_DRBG_reseed reseeds |drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of entropy 69 // in |entropy| and, optionally, up to |CTR_DRBG_ENTROPY_LEN| bytes of 70 // additional data. It returns one on success or zero on error. 71 OPENSSL_EXPORT int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg, 72 const uint8_t entropy[CTR_DRBG_ENTROPY_LEN], 73 const uint8_t *additional_data, 74 size_t additional_data_len); 75 76 // CTR_DRBG_generate processes to up |CTR_DRBG_ENTROPY_LEN| bytes of additional 77 // data (if any) and then writes |out_len| random bytes to |out|, where 78 // |out_len| <= |CTR_DRBG_MAX_GENERATE_LENGTH|. It returns one on success or 79 // zero on error. 80 OPENSSL_EXPORT int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out, 81 size_t out_len, 82 const uint8_t *additional_data, 83 size_t additional_data_len); 84 85 // CTR_DRBG_clear zeroises the state of |drbg|. 86 OPENSSL_EXPORT void CTR_DRBG_clear(CTR_DRBG_STATE *drbg); 87 88 89 #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) 90 OPENSSL_INLINE int have_rdrand(void) { 91 return (OPENSSL_ia32cap_get()[1] & (1u << 30)) != 0; 92 } 93 94 // CRYPTO_rdrand writes eight bytes of random data from the hardware RNG to 95 // |out|. It returns one on success or zero on hardware failure. 96 int CRYPTO_rdrand(uint8_t out[8]); 97 98 // CRYPTO_rdrand_multiple8_buf fills |len| bytes at |buf| with random data from 99 // the hardware RNG. The |len| argument must be a multiple of eight. It returns 100 // one on success and zero on hardware failure. 101 int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len); 102 #endif // OPENSSL_X86_64 && !OPENSSL_NO_ASM 103 104 105 #if defined(__cplusplus) 106 } // extern C 107 #endif 108 109 #endif // OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H 110