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 #if !defined(OPENSSL_WINDOWS) && !defined(OPENSSL_FUCHSIA) && \
30 !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) && !defined(OPENSSL_TRUSTY)
31 #define OPENSSL_URANDOM
32 #endif
33
34 // RAND_bytes_with_additional_data samples from the RNG after mixing 32 bytes
35 // from |user_additional_data| in.
36 void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,
37 const uint8_t user_additional_data[32]);
38
39 // CRYPTO_sysrand fills |len| bytes at |buf| with entropy from the operating
40 // system.
41 void CRYPTO_sysrand(uint8_t *buf, size_t len);
42
43 #if defined(OPENSSL_URANDOM) || defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
44 // CRYPTO_sysrand_for_seed fills |len| bytes at |buf| with entropy from the
45 // operating system. It may draw from the |GRND_RANDOM| pool on Android,
46 // depending on the vendor's configuration.
47 void CRYPTO_sysrand_for_seed(uint8_t *buf, size_t len);
48
49 // CRYPTO_sysrand_if_available fills |len| bytes at |buf| with entropy from the
50 // operating system, if the entropy pool is initialized. If it is uninitialized,
51 // it will not block and will instead fill |buf| with all zeros or early
52 // /dev/urandom output.
53 void CRYPTO_sysrand_if_available(uint8_t *buf, size_t len);
54 #endif
55
56 // rand_fork_unsafe_buffering_enabled returns whether fork-unsafe buffering has
57 // been enabled via |RAND_enable_fork_unsafe_buffering|.
58 int rand_fork_unsafe_buffering_enabled(void);
59
60 // CTR_DRBG_STATE contains the state of a CTR_DRBG based on AES-256. See SP
61 // 800-90Ar1.
62 typedef struct {
63 AES_KEY ks;
64 block128_f block;
65 ctr128_f ctr;
66 union {
67 uint8_t bytes[16];
68 uint32_t words[4];
69 } counter;
70 uint64_t reseed_counter;
71 } CTR_DRBG_STATE;
72
73 // See SP 800-90Ar1, table 3.
74 #define CTR_DRBG_ENTROPY_LEN 48
75 #define CTR_DRBG_MAX_GENERATE_LENGTH 65536
76
77 // CTR_DRBG_init initialises |*drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of
78 // entropy in |entropy| and, optionally, a personalization string up to
79 // |CTR_DRBG_ENTROPY_LEN| bytes in length. It returns one on success and zero
80 // on error.
81 OPENSSL_EXPORT int CTR_DRBG_init(CTR_DRBG_STATE *drbg,
82 const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
83 const uint8_t *personalization,
84 size_t personalization_len);
85
86 // CTR_DRBG_reseed reseeds |drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of entropy
87 // in |entropy| and, optionally, up to |CTR_DRBG_ENTROPY_LEN| bytes of
88 // additional data. It returns one on success or zero on error.
89 OPENSSL_EXPORT int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg,
90 const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
91 const uint8_t *additional_data,
92 size_t additional_data_len);
93
94 // CTR_DRBG_generate processes to up |CTR_DRBG_ENTROPY_LEN| bytes of additional
95 // data (if any) and then writes |out_len| random bytes to |out|, where
96 // |out_len| <= |CTR_DRBG_MAX_GENERATE_LENGTH|. It returns one on success or
97 // zero on error.
98 OPENSSL_EXPORT int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out,
99 size_t out_len,
100 const uint8_t *additional_data,
101 size_t additional_data_len);
102
103 // CTR_DRBG_clear zeroises the state of |drbg|.
104 OPENSSL_EXPORT void CTR_DRBG_clear(CTR_DRBG_STATE *drbg);
105
106
107 #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM)
have_rdrand(void)108 OPENSSL_INLINE int have_rdrand(void) {
109 return (OPENSSL_ia32cap_get()[1] & (1u << 30)) != 0;
110 }
111
112 // CRYPTO_rdrand writes eight bytes of random data from the hardware RNG to
113 // |out|. It returns one on success or zero on hardware failure.
114 int CRYPTO_rdrand(uint8_t out[8]);
115
116 // CRYPTO_rdrand_multiple8_buf fills |len| bytes at |buf| with random data from
117 // the hardware RNG. The |len| argument must be a multiple of eight. It returns
118 // one on success and zero on hardware failure.
119 int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
120 #endif // OPENSSL_X86_64 && !OPENSSL_NO_ASM
121
122
123 #if defined(__cplusplus)
124 } // extern C
125 #endif
126
127 #endif // OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H
128