| 1 |
// Copyright (c) 2015, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H |
| 5 |
#define OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H |
| 6 |
|
| 7 |
#include <openssl/aes.h> |
| 8 |
#include <openssl/ctrdrbg.h> |
| 9 |
#include <openssl/rand.h> |
| 10 |
|
| 11 |
#include "../../internal.h" |
| 12 |
#include "../modes/internal.h" |
| 13 |
|
| 14 |
#if defined(__cplusplus) |
| 15 |
extern "C" { |
| 16 |
#endif |
| 17 |
|
| 18 |
// kCtrDrbgReseedInterval is the number of generate calls made to CTR-DRBG, |
| 19 |
// for a specific state, before reseeding. |
| 20 |
static const uint64_t kCtrDrbgReseedInterval = 4096; |
| 21 |
|
| 22 |
#define RAND_NO_USER_PRED_RESISTANCE 0 |
| 23 |
#define RAND_USE_USER_PRED_RESISTANCE 1 |
| 24 |
|
| 25 |
OPENSSL_EXPORT uint64_t get_private_thread_generate_calls_since_seed(void); |
| 26 |
OPENSSL_EXPORT uint64_t get_private_thread_reseed_calls_since_initialization(void); |
| 27 |
|
| 28 |
OPENSSL_EXPORT uint64_t get_public_thread_generate_calls_since_seed(void); |
| 29 |
OPENSSL_EXPORT uint64_t get_public_thread_reseed_calls_since_initialization(void); |
| 30 |
|
| 31 |
// rand_thread_local_state_clear_all_FOR_TESTING runs the same shutdown |
| 32 |
// zeroization sequence that is registered via |atexit|. It is exposed for tests |
| 33 |
// that need to verify shutdown behavior (e.g. that a thread exiting after |
| 34 |
// shutdown does not deadlock). After this is called, no further |RAND_bytes| |
| 35 |
// calls in the test process will return output, so the caller is responsible |
| 36 |
// for arranging that no other code paths in the same process require |
| 37 |
// randomness afterwards. |
| 38 |
OPENSSL_EXPORT void rand_thread_local_state_clear_all_FOR_TESTING(void); |
| 39 |
|
| 40 |
// CTR_DRBG_STATE contains the state of a CTR_DRBG based on AES-256. See SP |
| 41 |
// 800-90Ar1. |
| 42 |
struct ctr_drbg_state_st { |
| 43 |
AES_KEY ks; |
| 44 |
block128_f block; |
| 45 |
ctr128_f ctr; |
| 46 |
uint8_t counter[16]; |
| 47 |
uint64_t reseed_counter; |
| 48 |
}; |
| 49 |
OPENSSL_STATIC_ASSERT((sizeof((struct ctr_drbg_state_st*)0)->reseed_counter) * 8 >= 48, value_can_overflow) |
| 50 |
|
| 51 |
// CTR_DRBG_init initialises |*drbg| given |CTR_DRBG_ENTROPY_LEN| bytes of |
| 52 |
// entropy in |entropy| and, optionally, a personalization string up to |
| 53 |
// |CTR_DRBG_ENTROPY_LEN| bytes in length. It returns one on success and zero |
| 54 |
// on error. |entropy| and |personalization| must not alias. |
| 55 |
OPENSSL_EXPORT int CTR_DRBG_init(CTR_DRBG_STATE *drbg, |
| 56 |
const uint8_t entropy[CTR_DRBG_ENTROPY_LEN], |
| 57 |
const uint8_t *personalization, |
| 58 |
size_t personalization_len); |
| 59 |
|
| 60 |
#if defined(__cplusplus) |
| 61 |
} // extern C |
| 62 |
#endif |
| 63 |
|
| 64 |
#endif // OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H |