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