/src/boringssl/crypto/fipsmodule/rand/internal.h
Line | Count | Source |
1 | | // Copyright 2015 The BoringSSL Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef OPENSSL_HEADER_CRYPTO_FIPSMODULE_RAND_INTERNAL_H |
16 | | #define OPENSSL_HEADER_CRYPTO_FIPSMODULE_RAND_INTERNAL_H |
17 | | |
18 | | #include <openssl/aes.h> |
19 | | #include <openssl/ctrdrbg.h> |
20 | | |
21 | | #include "../../bcm_support.h" |
22 | | #include "../aes/internal.h" |
23 | | |
24 | | |
25 | | BSSL_NAMESPACE_BEGIN |
26 | | |
27 | | // rand_fork_unsafe_buffering_enabled returns whether fork-unsafe buffering has |
28 | | // been enabled via `RAND_enable_fork_unsafe_buffering`. |
29 | | int rand_fork_unsafe_buffering_enabled(); |
30 | | |
31 | | BSSL_NAMESPACE_END |
32 | | |
33 | | // CTR_DRBG_STATE contains the state of a CTR_DRBG based on AES-256. See SP |
34 | | // 800-90Ar1. |
35 | | struct ctr_drbg_state_st { |
36 | | AES_KEY ks; |
37 | | bssl::block128_f block; |
38 | | bssl::ctr128_f ctr; |
39 | | uint8_t counter[16]; |
40 | | uint64_t reseed_counter; |
41 | | int df; |
42 | | }; |
43 | | |
44 | | BSSL_NAMESPACE_BEGIN |
45 | | |
46 | | // CTR_DRBG_init initialises `*drbg` given `entropy_len` bytes of entropy in |
47 | | // `entropy` and, optionally, a personalization string up to |
48 | | // `CTR_DRBG_SEED_LEN` bytes in length. It returns one on success and zero on |
49 | | // error. |
50 | | // |
51 | | // If `df` is false then `entropy_len` must be `CTR_DRBG_ENTROPY_LEN` and |
52 | | // `nonce` must be nullptr. |
53 | | OPENSSL_EXPORT int CTR_DRBG_init(CTR_DRBG_STATE *drbg, int df, |
54 | | const uint8_t *entropy, size_t entropy_len, |
55 | | const uint8_t nonce[CTR_DRBG_NONCE_LEN], |
56 | | const uint8_t *personalization, |
57 | | size_t personalization_len); |
58 | | |
59 | | // BSSL_ENTROPY_DAEMON_RESPONSE_LEN is the number of bytes that the Android |
60 | | // entropy daemon replies with. |
61 | | #define BSSL_ENTROPY_DAEMON_RESPONSE_LEN (48 * 10 + 16) |
62 | | |
63 | | // bssl_get_seed_from_daemon fetches up to `*inout_entropy_len` bytes of |
64 | | // entropy from the Android entropy daemon, writing them to `*out_entropy`. |
65 | | // On successful exit, `*inout_entropy_len` contains the number of bytes |
66 | | // written. Returns one on success and zero on error. |
67 | | int bssl_get_seed_from_daemon(uint8_t *out_entropy, size_t *inout_entropy_len); |
68 | | |
69 | | #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) |
70 | | |
71 | 0 | inline int have_rdrand() { return CRYPTO_is_RDRAND_capable(); } |
72 | | |
73 | | // have_fast_rdrand returns true if RDRAND is supported and it's reasonably |
74 | | // fast. Concretely the latter is defined by whether the chip is Intel (fast) or |
75 | | // not (assumed slow). |
76 | 416k | inline int have_fast_rdrand() { |
77 | 416k | return CRYPTO_is_RDRAND_capable() && CRYPTO_is_intel_cpu(); |
78 | 416k | } |
79 | | |
80 | | // CRYPTO_rdrand writes eight bytes of random data from the hardware RNG to |
81 | | // `out`. It returns one on success or zero on hardware failure. |
82 | | extern "C" int CRYPTO_rdrand(uint8_t out[8]); |
83 | | |
84 | | // CRYPTO_rdrand_multiple8_buf fills `len` bytes at `buf` with random data from |
85 | | // the hardware RNG. The `len` argument must be a multiple of eight. It returns |
86 | | // one on success and zero on hardware failure. |
87 | | extern "C" int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len); |
88 | | |
89 | | #else // OPENSSL_X86_64 && !OPENSSL_NO_ASM |
90 | | |
91 | | inline int have_rdrand() { return 0; } |
92 | | |
93 | | inline int have_fast_rdrand() { return 0; } |
94 | | |
95 | | #endif // OPENSSL_X86_64 && !OPENSSL_NO_ASM |
96 | | |
97 | | BSSL_NAMESPACE_END |
98 | | |
99 | | #endif // OPENSSL_HEADER_CRYPTO_FIPSMODULE_RAND_INTERNAL_H |