xref: /aosp_15_r20/external/abseil-cpp/absl/random/internal/randen.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker #include "absl/random/internal/randen.h"
16*9356374aSAndroid Build Coastguard Worker 
17*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/raw_logging.h"
18*9356374aSAndroid Build Coastguard Worker #include "absl/random/internal/randen_detect.h"
19*9356374aSAndroid Build Coastguard Worker 
20*9356374aSAndroid Build Coastguard Worker // RANDen = RANDom generator or beetroots in Swiss High German.
21*9356374aSAndroid Build Coastguard Worker // 'Strong' (well-distributed, unpredictable, backtracking-resistant) random
22*9356374aSAndroid Build Coastguard Worker // generator, faster in some benchmarks than std::mt19937_64 and pcg64_c32.
23*9356374aSAndroid Build Coastguard Worker //
24*9356374aSAndroid Build Coastguard Worker // High-level summary:
25*9356374aSAndroid Build Coastguard Worker // 1) Reverie (see "A Robust and Sponge-Like PRNG with Improved Efficiency") is
26*9356374aSAndroid Build Coastguard Worker //    a sponge-like random generator that requires a cryptographic permutation.
27*9356374aSAndroid Build Coastguard Worker //    It improves upon "Provably Robust Sponge-Based PRNGs and KDFs" by
28*9356374aSAndroid Build Coastguard Worker //    achieving backtracking resistance with only one Permute() per buffer.
29*9356374aSAndroid Build Coastguard Worker //
30*9356374aSAndroid Build Coastguard Worker // 2) "Simpira v2: A Family of Efficient Permutations Using the AES Round
31*9356374aSAndroid Build Coastguard Worker //    Function" constructs up to 1024-bit permutations using an improved
32*9356374aSAndroid Build Coastguard Worker //    Generalized Feistel network with 2-round AES-128 functions. This Feistel
33*9356374aSAndroid Build Coastguard Worker //    block shuffle achieves diffusion faster and is less vulnerable to
34*9356374aSAndroid Build Coastguard Worker //    sliced-biclique attacks than the Type-2 cyclic shuffle.
35*9356374aSAndroid Build Coastguard Worker //
36*9356374aSAndroid Build Coastguard Worker // 3) "Improving the Generalized Feistel" and "New criterion for diffusion
37*9356374aSAndroid Build Coastguard Worker //    property" extends the same kind of improved Feistel block shuffle to 16
38*9356374aSAndroid Build Coastguard Worker //    branches, which enables a 2048-bit permutation.
39*9356374aSAndroid Build Coastguard Worker //
40*9356374aSAndroid Build Coastguard Worker // We combine these three ideas and also change Simpira's subround keys from
41*9356374aSAndroid Build Coastguard Worker // structured/low-entropy counters to digits of Pi.
42*9356374aSAndroid Build Coastguard Worker 
43*9356374aSAndroid Build Coastguard Worker namespace absl {
44*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
45*9356374aSAndroid Build Coastguard Worker namespace random_internal {
46*9356374aSAndroid Build Coastguard Worker namespace {
47*9356374aSAndroid Build Coastguard Worker 
48*9356374aSAndroid Build Coastguard Worker struct RandenState {
49*9356374aSAndroid Build Coastguard Worker   const void* keys;
50*9356374aSAndroid Build Coastguard Worker   bool has_crypto;
51*9356374aSAndroid Build Coastguard Worker };
52*9356374aSAndroid Build Coastguard Worker 
GetRandenState()53*9356374aSAndroid Build Coastguard Worker RandenState GetRandenState() {
54*9356374aSAndroid Build Coastguard Worker   static const RandenState state = []() {
55*9356374aSAndroid Build Coastguard Worker     RandenState tmp;
56*9356374aSAndroid Build Coastguard Worker #if ABSL_RANDOM_INTERNAL_AES_DISPATCH
57*9356374aSAndroid Build Coastguard Worker     // HW AES Dispatch.
58*9356374aSAndroid Build Coastguard Worker     if (HasRandenHwAesImplementation() && CPUSupportsRandenHwAes()) {
59*9356374aSAndroid Build Coastguard Worker       tmp.has_crypto = true;
60*9356374aSAndroid Build Coastguard Worker       tmp.keys = RandenHwAes::GetKeys();
61*9356374aSAndroid Build Coastguard Worker     } else {
62*9356374aSAndroid Build Coastguard Worker       tmp.has_crypto = false;
63*9356374aSAndroid Build Coastguard Worker       tmp.keys = RandenSlow::GetKeys();
64*9356374aSAndroid Build Coastguard Worker     }
65*9356374aSAndroid Build Coastguard Worker #elif ABSL_HAVE_ACCELERATED_AES
66*9356374aSAndroid Build Coastguard Worker     // HW AES is enabled.
67*9356374aSAndroid Build Coastguard Worker     tmp.has_crypto = true;
68*9356374aSAndroid Build Coastguard Worker     tmp.keys = RandenHwAes::GetKeys();
69*9356374aSAndroid Build Coastguard Worker #else
70*9356374aSAndroid Build Coastguard Worker     // HW AES is disabled.
71*9356374aSAndroid Build Coastguard Worker     tmp.has_crypto = false;
72*9356374aSAndroid Build Coastguard Worker     tmp.keys = RandenSlow::GetKeys();
73*9356374aSAndroid Build Coastguard Worker #endif
74*9356374aSAndroid Build Coastguard Worker     return tmp;
75*9356374aSAndroid Build Coastguard Worker   }();
76*9356374aSAndroid Build Coastguard Worker   return state;
77*9356374aSAndroid Build Coastguard Worker }
78*9356374aSAndroid Build Coastguard Worker 
79*9356374aSAndroid Build Coastguard Worker }  // namespace
80*9356374aSAndroid Build Coastguard Worker 
Randen()81*9356374aSAndroid Build Coastguard Worker Randen::Randen() {
82*9356374aSAndroid Build Coastguard Worker   auto tmp = GetRandenState();
83*9356374aSAndroid Build Coastguard Worker   keys_ = tmp.keys;
84*9356374aSAndroid Build Coastguard Worker #if ABSL_RANDOM_INTERNAL_AES_DISPATCH
85*9356374aSAndroid Build Coastguard Worker   has_crypto_ = tmp.has_crypto;
86*9356374aSAndroid Build Coastguard Worker #endif
87*9356374aSAndroid Build Coastguard Worker }
88*9356374aSAndroid Build Coastguard Worker 
89*9356374aSAndroid Build Coastguard Worker }  // namespace random_internal
90*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
91*9356374aSAndroid Build Coastguard Worker }  // namespace absl
92