blob: bab8075a54f89e6a28b695fa1599e961c40673b8 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// Copyright 2017 The Abseil 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 "absl/random/internal/randen.h"
16
17#include "absl/base/internal/raw_logging.h"
18#include "absl/random/internal/randen_detect.h"
19
20// RANDen = RANDom generator or beetroots in Swiss German.
21// 'Strong' (well-distributed, unpredictable, backtracking-resistant) random
22// generator, faster in some benchmarks than std::mt19937_64 and pcg64_c32.
23//
24// High-level summary:
25// 1) Reverie (see "A Robust and Sponge-Like PRNG with Improved Efficiency") is
26// a sponge-like random generator that requires a cryptographic permutation.
27// It improves upon "Provably Robust Sponge-Based PRNGs and KDFs" by
28// achieving backtracking resistance with only one Permute() per buffer.
29//
30// 2) "Simpira v2: A Family of Efficient Permutations Using the AES Round
31// Function" constructs up to 1024-bit permutations using an improved
32// Generalized Feistel network with 2-round AES-128 functions. This Feistel
33// block shuffle achieves diffusion faster and is less vulnerable to
34// sliced-biclique attacks than the Type-2 cyclic shuffle.
35//
36// 3) "Improving the Generalized Feistel" and "New criterion for diffusion
37// property" extends the same kind of improved Feistel block shuffle to 16
38// branches, which enables a 2048-bit permutation.
39//
40// We combine these three ideas and also change Simpira's subround keys from
41// structured/low-entropy counters to digits of Pi.
42
43namespace absl {
44namespace random_internal {
45namespace {
46
47struct RandenState {
48 const void* keys;
49 bool has_crypto;
50};
51
52RandenState GetRandenState() {
53 static const RandenState state = []() {
54 RandenState tmp;
55#if ABSL_RANDOM_INTERNAL_AES_DISPATCH
56 // HW AES Dispatch.
57 if (HasRandenHwAesImplementation() && CPUSupportsRandenHwAes()) {
58 tmp.has_crypto = true;
59 tmp.keys = RandenHwAes::GetKeys();
60 } else {
61 tmp.has_crypto = false;
62 tmp.keys = RandenSlow::GetKeys();
63 }
64#elif ABSL_HAVE_ACCELERATED_AES
65 // HW AES is enabled.
66 tmp.has_crypto = true;
67 tmp.keys = RandenHwAes::GetKeys();
68#else
69 // HW AES is disabled.
70 tmp.has_crypto = false;
71 tmp.keys = RandenSlow::GetKeys();
72#endif
73 return tmp;
74 }();
75 return state;
76}
77
78} // namespace
79
80Randen::Randen() {
81 auto tmp = GetRandenState();
82 keys_ = tmp.keys;
83#if ABSL_RANDOM_INTERNAL_AES_DISPATCH
84 has_crypto_ = tmp.has_crypto;
85#endif
86}
87
88} // namespace random_internal
89} // namespace absl