xref: /aosp_15_r20/external/abseil-cpp/absl/random/internal/seed_material.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_SEED_MATERIAL_H_
16*9356374aSAndroid Build Coastguard Worker #define ABSL_RANDOM_INTERNAL_SEED_MATERIAL_H_
17*9356374aSAndroid Build Coastguard Worker 
18*9356374aSAndroid Build Coastguard Worker #include <cassert>
19*9356374aSAndroid Build Coastguard Worker #include <cstdint>
20*9356374aSAndroid Build Coastguard Worker #include <cstdlib>
21*9356374aSAndroid Build Coastguard Worker #include <string>
22*9356374aSAndroid Build Coastguard Worker #include <vector>
23*9356374aSAndroid Build Coastguard Worker 
24*9356374aSAndroid Build Coastguard Worker #include "absl/base/attributes.h"
25*9356374aSAndroid Build Coastguard Worker #include "absl/random/internal/fast_uniform_bits.h"
26*9356374aSAndroid Build Coastguard Worker #include "absl/types/optional.h"
27*9356374aSAndroid Build Coastguard Worker #include "absl/types/span.h"
28*9356374aSAndroid Build Coastguard Worker 
29*9356374aSAndroid Build Coastguard Worker namespace absl {
30*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
31*9356374aSAndroid Build Coastguard Worker namespace random_internal {
32*9356374aSAndroid Build Coastguard Worker 
33*9356374aSAndroid Build Coastguard Worker // Returns the number of 32-bit blocks needed to contain the given number of
34*9356374aSAndroid Build Coastguard Worker // bits.
SeedBitsToBlocks(size_t seed_size)35*9356374aSAndroid Build Coastguard Worker constexpr size_t SeedBitsToBlocks(size_t seed_size) {
36*9356374aSAndroid Build Coastguard Worker   return (seed_size + 31) / 32;
37*9356374aSAndroid Build Coastguard Worker }
38*9356374aSAndroid Build Coastguard Worker 
39*9356374aSAndroid Build Coastguard Worker // Amount of entropy (measured in bits) used to instantiate a Seed Sequence,
40*9356374aSAndroid Build Coastguard Worker // with which to create a URBG.
41*9356374aSAndroid Build Coastguard Worker constexpr size_t kEntropyBitsNeeded = 256;
42*9356374aSAndroid Build Coastguard Worker 
43*9356374aSAndroid Build Coastguard Worker // Amount of entropy (measured in 32-bit blocks) used to instantiate a Seed
44*9356374aSAndroid Build Coastguard Worker // Sequence, with which to create a URBG.
45*9356374aSAndroid Build Coastguard Worker constexpr size_t kEntropyBlocksNeeded =
46*9356374aSAndroid Build Coastguard Worker     random_internal::SeedBitsToBlocks(kEntropyBitsNeeded);
47*9356374aSAndroid Build Coastguard Worker 
48*9356374aSAndroid Build Coastguard Worker static_assert(kEntropyBlocksNeeded > 0,
49*9356374aSAndroid Build Coastguard Worker               "Entropy used to seed URBGs must be nonzero.");
50*9356374aSAndroid Build Coastguard Worker 
51*9356374aSAndroid Build Coastguard Worker // Attempts to fill a span of uint32_t-values using an OS-provided source of
52*9356374aSAndroid Build Coastguard Worker // true entropy (eg. /dev/urandom) into an array of uint32_t blocks of data. The
53*9356374aSAndroid Build Coastguard Worker // resulting array may be used to initialize an instance of a class conforming
54*9356374aSAndroid Build Coastguard Worker // to the C++ Standard "Seed Sequence" concept [rand.req.seedseq].
55*9356374aSAndroid Build Coastguard Worker //
56*9356374aSAndroid Build Coastguard Worker // If values.data() == nullptr, the behavior is undefined.
57*9356374aSAndroid Build Coastguard Worker ABSL_MUST_USE_RESULT
58*9356374aSAndroid Build Coastguard Worker bool ReadSeedMaterialFromOSEntropy(absl::Span<uint32_t> values);
59*9356374aSAndroid Build Coastguard Worker 
60*9356374aSAndroid Build Coastguard Worker // Attempts to fill a span of uint32_t-values using variates generated by an
61*9356374aSAndroid Build Coastguard Worker // existing instance of a class conforming to the C++ Standard "Uniform Random
62*9356374aSAndroid Build Coastguard Worker // Bit Generator" concept [rand.req.urng]. The resulting data may be used to
63*9356374aSAndroid Build Coastguard Worker // initialize an instance of a class conforming to the C++ Standard
64*9356374aSAndroid Build Coastguard Worker // "Seed Sequence" concept [rand.req.seedseq].
65*9356374aSAndroid Build Coastguard Worker //
66*9356374aSAndroid Build Coastguard Worker // If urbg == nullptr or values.data() == nullptr, the behavior is undefined.
67*9356374aSAndroid Build Coastguard Worker template <typename URBG>
ReadSeedMaterialFromURBG(URBG * urbg,absl::Span<uint32_t> values)68*9356374aSAndroid Build Coastguard Worker ABSL_MUST_USE_RESULT bool ReadSeedMaterialFromURBG(
69*9356374aSAndroid Build Coastguard Worker     URBG* urbg, absl::Span<uint32_t> values) {
70*9356374aSAndroid Build Coastguard Worker   random_internal::FastUniformBits<uint32_t> distr;
71*9356374aSAndroid Build Coastguard Worker 
72*9356374aSAndroid Build Coastguard Worker   assert(urbg != nullptr && values.data() != nullptr);
73*9356374aSAndroid Build Coastguard Worker   if (urbg == nullptr || values.data() == nullptr) {
74*9356374aSAndroid Build Coastguard Worker     return false;
75*9356374aSAndroid Build Coastguard Worker   }
76*9356374aSAndroid Build Coastguard Worker 
77*9356374aSAndroid Build Coastguard Worker   for (uint32_t& seed_value : values) {
78*9356374aSAndroid Build Coastguard Worker     seed_value = distr(*urbg);
79*9356374aSAndroid Build Coastguard Worker   }
80*9356374aSAndroid Build Coastguard Worker   return true;
81*9356374aSAndroid Build Coastguard Worker }
82*9356374aSAndroid Build Coastguard Worker 
83*9356374aSAndroid Build Coastguard Worker // Mixes given sequence of values with into given sequence of seed material.
84*9356374aSAndroid Build Coastguard Worker // Time complexity of this function is O(sequence.size() *
85*9356374aSAndroid Build Coastguard Worker // seed_material.size()).
86*9356374aSAndroid Build Coastguard Worker //
87*9356374aSAndroid Build Coastguard Worker // Algorithm is based on code available at
88*9356374aSAndroid Build Coastguard Worker // https://gist.github.com/imneme/540829265469e673d045
89*9356374aSAndroid Build Coastguard Worker // by Melissa O'Neill.
90*9356374aSAndroid Build Coastguard Worker void MixIntoSeedMaterial(absl::Span<const uint32_t> sequence,
91*9356374aSAndroid Build Coastguard Worker                          absl::Span<uint32_t> seed_material);
92*9356374aSAndroid Build Coastguard Worker 
93*9356374aSAndroid Build Coastguard Worker // Returns salt value.
94*9356374aSAndroid Build Coastguard Worker //
95*9356374aSAndroid Build Coastguard Worker // Salt is obtained only once and stored in static variable.
96*9356374aSAndroid Build Coastguard Worker //
97*9356374aSAndroid Build Coastguard Worker // May return empty value if optaining the salt was not possible.
98*9356374aSAndroid Build Coastguard Worker absl::optional<uint32_t> GetSaltMaterial();
99*9356374aSAndroid Build Coastguard Worker 
100*9356374aSAndroid Build Coastguard Worker }  // namespace random_internal
101*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
102*9356374aSAndroid Build Coastguard Worker }  // namespace absl
103*9356374aSAndroid Build Coastguard Worker 
104*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_RANDOM_INTERNAL_SEED_MATERIAL_H_
105