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