xref: /aosp_15_r20/external/abseil-cpp/absl/random/internal/randen_traits.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_TRAITS_H_
16*9356374aSAndroid Build Coastguard Worker #define ABSL_RANDOM_INTERNAL_RANDEN_TRAITS_H_
17*9356374aSAndroid Build Coastguard Worker 
18*9356374aSAndroid Build Coastguard Worker // HERMETIC NOTE: The randen_hwaes target must not introduce duplicate
19*9356374aSAndroid Build Coastguard Worker // symbols from arbitrary system and other headers, since it may be built
20*9356374aSAndroid Build Coastguard Worker // with different flags from other targets, using different levels of
21*9356374aSAndroid Build Coastguard Worker // optimization, potentially introducing ODR violations.
22*9356374aSAndroid Build Coastguard Worker 
23*9356374aSAndroid Build Coastguard Worker #include <cstddef>
24*9356374aSAndroid Build Coastguard Worker 
25*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
26*9356374aSAndroid Build Coastguard Worker 
27*9356374aSAndroid Build Coastguard Worker namespace absl {
28*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
29*9356374aSAndroid Build Coastguard Worker namespace random_internal {
30*9356374aSAndroid Build Coastguard Worker 
31*9356374aSAndroid Build Coastguard Worker // RANDen = RANDom generator or beetroots in Swiss High German.
32*9356374aSAndroid Build Coastguard Worker // 'Strong' (well-distributed, unpredictable, backtracking-resistant) random
33*9356374aSAndroid Build Coastguard Worker // generator, faster in some benchmarks than std::mt19937_64 and pcg64_c32.
34*9356374aSAndroid Build Coastguard Worker //
35*9356374aSAndroid Build Coastguard Worker // High-level summary:
36*9356374aSAndroid Build Coastguard Worker // 1) Reverie (see "A Robust and Sponge-Like PRNG with Improved Efficiency") is
37*9356374aSAndroid Build Coastguard Worker //    a sponge-like random generator that requires a cryptographic permutation.
38*9356374aSAndroid Build Coastguard Worker //    It improves upon "Provably Robust Sponge-Based PRNGs and KDFs" by
39*9356374aSAndroid Build Coastguard Worker //    achieving backtracking resistance with only one Permute() per buffer.
40*9356374aSAndroid Build Coastguard Worker //
41*9356374aSAndroid Build Coastguard Worker // 2) "Simpira v2: A Family of Efficient Permutations Using the AES Round
42*9356374aSAndroid Build Coastguard Worker //    Function" constructs up to 1024-bit permutations using an improved
43*9356374aSAndroid Build Coastguard Worker //    Generalized Feistel network with 2-round AES-128 functions. This Feistel
44*9356374aSAndroid Build Coastguard Worker //    block shuffle achieves diffusion faster and is less vulnerable to
45*9356374aSAndroid Build Coastguard Worker //    sliced-biclique attacks than the Type-2 cyclic shuffle.
46*9356374aSAndroid Build Coastguard Worker //
47*9356374aSAndroid Build Coastguard Worker // 3) "Improving the Generalized Feistel" and "New criterion for diffusion
48*9356374aSAndroid Build Coastguard Worker //    property" extends the same kind of improved Feistel block shuffle to 16
49*9356374aSAndroid Build Coastguard Worker //    branches, which enables a 2048-bit permutation.
50*9356374aSAndroid Build Coastguard Worker //
51*9356374aSAndroid Build Coastguard Worker // Combine these three ideas and also change Simpira's subround keys from
52*9356374aSAndroid Build Coastguard Worker // structured/low-entropy counters to digits of Pi (or other random source).
53*9356374aSAndroid Build Coastguard Worker 
54*9356374aSAndroid Build Coastguard Worker // RandenTraits contains the basic algorithm traits, such as the size of the
55*9356374aSAndroid Build Coastguard Worker // state, seed, sponge, etc.
56*9356374aSAndroid Build Coastguard Worker struct RandenTraits {
57*9356374aSAndroid Build Coastguard Worker   // Size of the entire sponge / state for the randen PRNG.
58*9356374aSAndroid Build Coastguard Worker   static constexpr size_t kStateBytes = 256;  // 2048-bit
59*9356374aSAndroid Build Coastguard Worker 
60*9356374aSAndroid Build Coastguard Worker   // Size of the 'inner' (inaccessible) part of the sponge. Larger values would
61*9356374aSAndroid Build Coastguard Worker   // require more frequent calls to RandenGenerate.
62*9356374aSAndroid Build Coastguard Worker   static constexpr size_t kCapacityBytes = 16;  // 128-bit
63*9356374aSAndroid Build Coastguard Worker 
64*9356374aSAndroid Build Coastguard Worker   // Size of the default seed consumed by the sponge.
65*9356374aSAndroid Build Coastguard Worker   static constexpr size_t kSeedBytes = kStateBytes - kCapacityBytes;
66*9356374aSAndroid Build Coastguard Worker 
67*9356374aSAndroid Build Coastguard Worker   // Assuming 128-bit blocks, the number of blocks in the state.
68*9356374aSAndroid Build Coastguard Worker   // Largest size for which security proofs are known.
69*9356374aSAndroid Build Coastguard Worker   static constexpr size_t kFeistelBlocks = 16;
70*9356374aSAndroid Build Coastguard Worker 
71*9356374aSAndroid Build Coastguard Worker   // Ensures SPRP security and two full subblock diffusions.
72*9356374aSAndroid Build Coastguard Worker   // Must be > 4 * log2(kFeistelBlocks).
73*9356374aSAndroid Build Coastguard Worker   static constexpr size_t kFeistelRounds = 16 + 1;
74*9356374aSAndroid Build Coastguard Worker 
75*9356374aSAndroid Build Coastguard Worker   // Size of the key. A 128-bit key block is used for every-other
76*9356374aSAndroid Build Coastguard Worker   // feistel block (Type-2 generalized Feistel network) in each round.
77*9356374aSAndroid Build Coastguard Worker   static constexpr size_t kKeyBytes = 16 * kFeistelRounds * kFeistelBlocks / 2;
78*9356374aSAndroid Build Coastguard Worker };
79*9356374aSAndroid Build Coastguard Worker 
80*9356374aSAndroid Build Coastguard Worker // Randen key arrays. In randen_round_keys.cc
81*9356374aSAndroid Build Coastguard Worker extern const unsigned char kRandenRoundKeys[RandenTraits::kKeyBytes];
82*9356374aSAndroid Build Coastguard Worker extern const unsigned char kRandenRoundKeysBE[RandenTraits::kKeyBytes];
83*9356374aSAndroid Build Coastguard Worker 
84*9356374aSAndroid Build Coastguard Worker }  // namespace random_internal
85*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
86*9356374aSAndroid Build Coastguard Worker }  // namespace absl
87*9356374aSAndroid Build Coastguard Worker 
88*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_RANDOM_INTERNAL_RANDEN_TRAITS_H_
89