xref: /aosp_15_r20/external/abseil-cpp/absl/random/internal/randen.h (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 #ifndef ABSL_RANDOM_INTERNAL_RANDEN_H_
16*9356374aSAndroid Build Coastguard Worker #define ABSL_RANDOM_INTERNAL_RANDEN_H_
17*9356374aSAndroid Build Coastguard Worker 
18*9356374aSAndroid Build Coastguard Worker #include <cstddef>
19*9356374aSAndroid Build Coastguard Worker 
20*9356374aSAndroid Build Coastguard Worker #include "absl/random/internal/platform.h"
21*9356374aSAndroid Build Coastguard Worker #include "absl/random/internal/randen_hwaes.h"
22*9356374aSAndroid Build Coastguard Worker #include "absl/random/internal/randen_slow.h"
23*9356374aSAndroid Build Coastguard Worker #include "absl/random/internal/randen_traits.h"
24*9356374aSAndroid Build Coastguard Worker 
25*9356374aSAndroid Build Coastguard Worker namespace absl {
26*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
27*9356374aSAndroid Build Coastguard Worker namespace random_internal {
28*9356374aSAndroid Build Coastguard Worker 
29*9356374aSAndroid Build Coastguard Worker // RANDen = RANDom generator or beetroots in Swiss High German.
30*9356374aSAndroid Build Coastguard Worker // 'Strong' (well-distributed, unpredictable, backtracking-resistant) random
31*9356374aSAndroid Build Coastguard Worker // generator, faster in some benchmarks than std::mt19937_64 and pcg64_c32.
32*9356374aSAndroid Build Coastguard Worker //
33*9356374aSAndroid Build Coastguard Worker // Randen implements the basic state manipulation methods.
34*9356374aSAndroid Build Coastguard Worker class Randen {
35*9356374aSAndroid Build Coastguard Worker  public:
36*9356374aSAndroid Build Coastguard Worker   static constexpr size_t kStateBytes = RandenTraits::kStateBytes;
37*9356374aSAndroid Build Coastguard Worker   static constexpr size_t kCapacityBytes = RandenTraits::kCapacityBytes;
38*9356374aSAndroid Build Coastguard Worker   static constexpr size_t kSeedBytes = RandenTraits::kSeedBytes;
39*9356374aSAndroid Build Coastguard Worker 
40*9356374aSAndroid Build Coastguard Worker   ~Randen() = default;
41*9356374aSAndroid Build Coastguard Worker 
42*9356374aSAndroid Build Coastguard Worker   Randen();
43*9356374aSAndroid Build Coastguard Worker 
44*9356374aSAndroid Build Coastguard Worker   // Generate updates the randen sponge. The outer portion of the sponge
45*9356374aSAndroid Build Coastguard Worker   // (kCapacityBytes .. kStateBytes) may be consumed as PRNG state.
46*9356374aSAndroid Build Coastguard Worker   // REQUIRES: state points to kStateBytes of state.
Generate(void * state)47*9356374aSAndroid Build Coastguard Worker   inline void Generate(void* state) const {
48*9356374aSAndroid Build Coastguard Worker #if ABSL_RANDOM_INTERNAL_AES_DISPATCH
49*9356374aSAndroid Build Coastguard Worker     // HW AES Dispatch.
50*9356374aSAndroid Build Coastguard Worker     if (has_crypto_) {
51*9356374aSAndroid Build Coastguard Worker       RandenHwAes::Generate(keys_, state);
52*9356374aSAndroid Build Coastguard Worker     } else {
53*9356374aSAndroid Build Coastguard Worker       RandenSlow::Generate(keys_, state);
54*9356374aSAndroid Build Coastguard Worker     }
55*9356374aSAndroid Build Coastguard Worker #elif ABSL_HAVE_ACCELERATED_AES
56*9356374aSAndroid Build Coastguard Worker     // HW AES is enabled.
57*9356374aSAndroid Build Coastguard Worker     RandenHwAes::Generate(keys_, state);
58*9356374aSAndroid Build Coastguard Worker #else
59*9356374aSAndroid Build Coastguard Worker     // HW AES is disabled.
60*9356374aSAndroid Build Coastguard Worker     RandenSlow::Generate(keys_, state);
61*9356374aSAndroid Build Coastguard Worker #endif
62*9356374aSAndroid Build Coastguard Worker   }
63*9356374aSAndroid Build Coastguard Worker 
64*9356374aSAndroid Build Coastguard Worker   // Absorb incorporates additional seed material into the randen sponge.  After
65*9356374aSAndroid Build Coastguard Worker   // absorb returns, Generate must be called before the state may be consumed.
66*9356374aSAndroid Build Coastguard Worker   // REQUIRES: seed points to kSeedBytes of seed.
67*9356374aSAndroid Build Coastguard Worker   // REQUIRES: state points to kStateBytes of state.
Absorb(const void * seed,void * state)68*9356374aSAndroid Build Coastguard Worker   inline void Absorb(const void* seed, void* state) const {
69*9356374aSAndroid Build Coastguard Worker #if ABSL_RANDOM_INTERNAL_AES_DISPATCH
70*9356374aSAndroid Build Coastguard Worker     // HW AES Dispatch.
71*9356374aSAndroid Build Coastguard Worker     if (has_crypto_) {
72*9356374aSAndroid Build Coastguard Worker       RandenHwAes::Absorb(seed, state);
73*9356374aSAndroid Build Coastguard Worker     } else {
74*9356374aSAndroid Build Coastguard Worker       RandenSlow::Absorb(seed, state);
75*9356374aSAndroid Build Coastguard Worker     }
76*9356374aSAndroid Build Coastguard Worker #elif ABSL_HAVE_ACCELERATED_AES
77*9356374aSAndroid Build Coastguard Worker     // HW AES is enabled.
78*9356374aSAndroid Build Coastguard Worker     RandenHwAes::Absorb(seed, state);
79*9356374aSAndroid Build Coastguard Worker #else
80*9356374aSAndroid Build Coastguard Worker     // HW AES is disabled.
81*9356374aSAndroid Build Coastguard Worker     RandenSlow::Absorb(seed, state);
82*9356374aSAndroid Build Coastguard Worker #endif
83*9356374aSAndroid Build Coastguard Worker   }
84*9356374aSAndroid Build Coastguard Worker 
85*9356374aSAndroid Build Coastguard Worker  private:
86*9356374aSAndroid Build Coastguard Worker   const void* keys_;
87*9356374aSAndroid Build Coastguard Worker #if ABSL_RANDOM_INTERNAL_AES_DISPATCH
88*9356374aSAndroid Build Coastguard Worker   bool has_crypto_;
89*9356374aSAndroid Build Coastguard Worker #endif
90*9356374aSAndroid Build Coastguard Worker };
91*9356374aSAndroid Build Coastguard Worker 
92*9356374aSAndroid Build Coastguard Worker }  // namespace random_internal
93*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
94*9356374aSAndroid Build Coastguard Worker }  // namespace absl
95*9356374aSAndroid Build Coastguard Worker 
96*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_RANDOM_INTERNAL_RANDEN_H_
97