21 #if defined(BORINGSSL_FIPS)
32 #include "../../internal.h"
33 #include "../delocate.h"
57 #define CRNGT_BLOCK_SIZE 16
70 #if defined(BORINGSSL_FIPS)
79 #if defined(BORINGSSL_FIPS)
88 static void rand_thread_state_clear_all(
void)
__attribute__((destructor));
89 static void rand_thread_state_clear_all(
void) {
106 if (state_in == NULL) {
110 #if defined(BORINGSSL_FIPS)
113 if (
state->prev != NULL) {
116 *thread_states_list_bss_get() =
state->
next;
131 #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
132 !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
136 const size_t len_multiple8 =
len & ~7;
140 const size_t remainder =
len - len_multiple8;
142 if (remainder != 0) {
143 assert(remainder < 8);
163 #if defined(BORINGSSL_FIPS)
165 void CRYPTO_get_seed_entropy(
uint8_t *out_entropy,
size_t out_entropy_len,
174 #if defined(BORINGSSL_FIPS_BREAK_CRNG)
185 struct entropy_buffer {
199 void RAND_load_entropy(
const uint8_t *entropy,
size_t entropy_len,
201 struct entropy_buffer *
const buffer = entropy_buffer_bss_get();
204 const size_t space =
sizeof(
buffer->bytes) -
buffer->bytes_valid;
205 if (entropy_len > space) {
210 buffer->bytes_valid += entropy_len;
211 buffer->from_cpu |= from_cpu && (entropy_len != 0);
217 static void get_seed_entropy(
uint8_t *out_entropy,
size_t out_entropy_len,
219 struct entropy_buffer *
const buffer = entropy_buffer_bss_get();
220 if (out_entropy_len >
sizeof(
buffer->bytes)) {
225 while (
buffer->bytes_valid < out_entropy_len) {
227 RAND_need_entropy(out_entropy_len -
buffer->bytes_valid);
231 *out_used_cpu =
buffer->from_cpu;
234 buffer->bytes_valid - out_entropy_len);
235 buffer->bytes_valid -= out_entropy_len;
236 if (
buffer->bytes_valid == 0) {
248 if (!
state->last_block_valid) {
250 get_seed_entropy(
state->last_block,
sizeof(
state->last_block), &unused);
251 state->last_block_valid = 1;
255 get_seed_entropy(entropy,
sizeof(entropy), out_used_cpu);
261 fprintf(
stderr,
"CRNGT failed.\n");
262 BORINGSSL_FIPS_abort();
270 fprintf(
stderr,
"CRNGT failed.\n");
271 BORINGSSL_FIPS_abort();
280 for (
size_t i = 1;
i < BORINGSSL_FIPS_OVERREAD;
i++) {
303 const uint8_t user_additional_data[32]) {
318 !
rdrand(additional_data,
sizeof(additional_data))) {
329 sizeof(additional_data)) &&
330 !
rdrand(additional_data,
sizeof(additional_data))) {
336 for (
size_t i = 0;
i <
sizeof(additional_data);
i++) {
337 additional_data[
i] ^= user_additional_data[
i];
351 state = &stack_state;
354 state->last_block_valid = 0;
360 size_t personalization_len = 0;
361 #if defined(OPENSSL_URANDOM)
367 personalization_len =
sizeof(personalization);
372 personalization_len)) {
378 #if defined(BORINGSSL_FIPS)
379 if (
state != &stack_state) {
387 *states_list =
state;
398 #if defined(BORINGSSL_FIPS)
415 #if defined(BORINGSSL_FIPS)
421 while (out_len > 0) {
422 size_t todo = out_len;
428 first_call ?
sizeof(additional_data) : 0)) {
440 if (
state == &stack_state) {
444 #if defined(BORINGSSL_FIPS)
450 static const uint8_t kZeroAdditionalData[32] = {0};