| 1 |
// Copyright (c) 2016, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/rand.h> |
| 5 |
|
| 6 |
#include "internal.h" |
| 7 |
|
| 8 |
#if defined(OPENSSL_RAND_DETERMINISTIC) |
| 9 |
|
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include <openssl/chacha.h> |
| 13 |
|
| 14 |
#include "../internal.h" |
| 15 |
|
| 16 |
|
| 17 |
// g_num_calls is the number of calls to |CRYPTO_sysrand| that have occurred. |
| 18 |
// |
| 19 |
// This is intentionally not thread-safe. If the fuzzer mode is ever used in a |
| 20 |
// multi-threaded program, replace this with a thread-local. (A mutex would not |
| 21 |
// be deterministic.) |
| 22 |
static uint64_t g_num_calls = 0; |
| 23 |
static struct CRYPTO_STATIC_MUTEX g_num_calls_lock = CRYPTO_STATIC_MUTEX_INIT; |
| 24 |
|
| 25 |
void RAND_reset_for_fuzzing(void) { g_num_calls = 0; } |
| 26 |
|
| 27 |
void CRYPTO_sysrand(uint8_t *out, size_t requested) { |
| 28 |
static const uint8_t kZeroKey[32]; |
| 29 |
|
| 30 |
CRYPTO_STATIC_MUTEX_lock_write(&g_num_calls_lock); |
| 31 |
uint64_t num_calls = g_num_calls++; |
| 32 |
CRYPTO_STATIC_MUTEX_unlock_write(&g_num_calls_lock); |
| 33 |
|
| 34 |
uint8_t nonce[12]; |
| 35 |
OPENSSL_memset(nonce, 0, sizeof(nonce)); |
| 36 |
OPENSSL_memcpy(nonce, &num_calls, sizeof(num_calls)); |
| 37 |
|
| 38 |
OPENSSL_memset(out, 0, requested); |
| 39 |
CRYPTO_chacha_20(out, out, requested, kZeroKey, nonce, 0); |
| 40 |
} |
| 41 |
|
| 42 |
#endif // OPENSSL_RAND_DETERMINISTIC |