You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

seed_material.h 4.2 kB

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