1 // 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 43 namespace absl { 44 ABSL_NAMESPACE_BEGIN 45 namespace random_internal { 46 namespace { 47 48 struct RandenState { 49 const void* keys; 50 bool has_crypto; 51 }; 52 GetRandenState()53RandenState GetRandenState() { 54 static const RandenState state = []() { 55 RandenState tmp; 56 #if ABSL_RANDOM_INTERNAL_AES_DISPATCH 57 // HW AES Dispatch. 58 if (HasRandenHwAesImplementation() && CPUSupportsRandenHwAes()) { 59 tmp.has_crypto = true; 60 tmp.keys = RandenHwAes::GetKeys(); 61 } else { 62 tmp.has_crypto = false; 63 tmp.keys = RandenSlow::GetKeys(); 64 } 65 #elif ABSL_HAVE_ACCELERATED_AES 66 // HW AES is enabled. 67 tmp.has_crypto = true; 68 tmp.keys = RandenHwAes::GetKeys(); 69 #else 70 // HW AES is disabled. 71 tmp.has_crypto = false; 72 tmp.keys = RandenSlow::GetKeys(); 73 #endif 74 return tmp; 75 }(); 76 return state; 77 } 78 79 } // namespace 80 Randen()81Randen::Randen() { 82 auto tmp = GetRandenState(); 83 keys_ = tmp.keys; 84 #if ABSL_RANDOM_INTERNAL_AES_DISPATCH 85 has_crypto_ = tmp.has_crypto; 86 #endif 87 } 88 89 } // namespace random_internal 90 ABSL_NAMESPACE_END 91 } // namespace absl 92