seed_material.h 3.6 KB

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