1 /* Copyright 2015 The BoringSSL Authors
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/ctrdrbg.h>
20
21 #include "../../bcm_support.h"
22 #include "../modes/internal.h"
23
24 #if defined(__cplusplus)
25 extern "C" {
26 #endif
27
28 // rand_fork_unsafe_buffering_enabled returns whether fork-unsafe buffering has
29 // been enabled via |RAND_enable_fork_unsafe_buffering|.
30 int rand_fork_unsafe_buffering_enabled(void);
31
32 // CTR_DRBG_STATE contains the state of a CTR_DRBG based on AES-256. See SP
33 // 800-90Ar1.
34 struct ctr_drbg_state_st {
35 AES_KEY ks;
36 block128_f block;
37 ctr128_f ctr;
38 uint8_t counter[16];
39 uint64_t reseed_counter;
40 };
41
42 // CTR_DRBG_init initialises |*drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of
43 // entropy in |entropy| and, optionally, a personalization string up to
44 // |CTR_DRBG_ENTROPY_LEN| bytes in length. It returns one on success and zero
45 // on error.
46 OPENSSL_EXPORT int CTR_DRBG_init(CTR_DRBG_STATE *drbg,
47 const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
48 const uint8_t *personalization,
49 size_t personalization_len);
50
51 #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM)
52
have_rdrand(void)53 inline int have_rdrand(void) { return CRYPTO_is_RDRAND_capable(); }
54
55 // have_fast_rdrand returns true if RDRAND is supported and it's reasonably
56 // fast. Concretely the latter is defined by whether the chip is Intel (fast) or
57 // not (assumed slow).
have_fast_rdrand(void)58 inline int have_fast_rdrand(void) {
59 return CRYPTO_is_RDRAND_capable() && CRYPTO_is_intel_cpu();
60 }
61
62 // CRYPTO_rdrand writes eight bytes of random data from the hardware RNG to
63 // |out|. It returns one on success or zero on hardware failure.
64 int CRYPTO_rdrand(uint8_t out[8]);
65
66 // CRYPTO_rdrand_multiple8_buf fills |len| bytes at |buf| with random data from
67 // the hardware RNG. The |len| argument must be a multiple of eight. It returns
68 // one on success and zero on hardware failure.
69 int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
70
71 #else // OPENSSL_X86_64 && !OPENSSL_NO_ASM
72
have_rdrand(void)73 inline int have_rdrand(void) { return 0; }
74
have_fast_rdrand(void)75 inline int have_fast_rdrand(void) { return 0; }
76
77 #endif // OPENSSL_X86_64 && !OPENSSL_NO_ASM
78
79
80 #if defined(__cplusplus)
81 } // extern C
82 #endif
83
84 #endif // OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H
85