/src/boringssl/crypto/rand/deterministic.cc
Line | Count | Source |
1 | | // Copyright 2016 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 | | #include <openssl/rand.h> |
16 | | |
17 | | // IWYU pragma: begin_keep |
18 | | #include "../bcm_support.h" |
19 | | #include "internal.h" |
20 | | // IWYU pragma: end_keep |
21 | | |
22 | | #if defined(OPENSSL_RAND_DETERMINISTIC) |
23 | | |
24 | | #include <string.h> |
25 | | |
26 | | #include <openssl/chacha.h> |
27 | | |
28 | | #include "../internal.h" |
29 | | |
30 | | |
31 | | using namespace bssl; |
32 | | |
33 | | namespace { |
34 | | |
35 | | // g_num_calls is the number of calls to `CRYPTO_sysrand` that have occurred. |
36 | | // |
37 | | // This is intentionally not thread-safe. If the fuzzer mode is ever used in a |
38 | | // multi-threaded program, replace this with a thread-local. (A mutex would not |
39 | | // be deterministic.) |
40 | | uint64_t g_num_calls = 0; |
41 | | StaticMutex g_num_calls_lock; |
42 | | |
43 | | } // namespace |
44 | | |
45 | 54.8k | void RAND_reset_for_fuzzing() { g_num_calls = 0; } |
46 | | |
47 | 0 | void bssl::CRYPTO_init_sysrand() {} |
48 | | |
49 | 115 | void bssl::CRYPTO_sysrand(uint8_t *out, size_t requested) { |
50 | 115 | static const uint8_t kZeroKey[32] = {0}; |
51 | | |
52 | 115 | g_num_calls_lock.LockWrite(); |
53 | 115 | uint64_t num_calls = g_num_calls++; |
54 | 115 | g_num_calls_lock.UnlockWrite(); |
55 | | |
56 | 115 | uint8_t nonce[12]; |
57 | 115 | OPENSSL_memset(nonce, 0, sizeof(nonce)); |
58 | 115 | OPENSSL_memcpy(nonce, &num_calls, sizeof(num_calls)); |
59 | | |
60 | 115 | OPENSSL_memset(out, 0, requested); |
61 | 115 | CRYPTO_chacha_20(out, out, requested, kZeroKey, nonce, 0); |
62 | 115 | } |
63 | | |
64 | | #endif // OPENSSL_RAND_DETERMINISTIC |