nonsecure_base.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_NONSECURE_BASE_H_
  15. #define ABSL_RANDOM_INTERNAL_NONSECURE_BASE_H_
  16. #include <algorithm>
  17. #include <cstdint>
  18. #include <iostream>
  19. #include <iterator>
  20. #include <random>
  21. #include <string>
  22. #include <type_traits>
  23. #include <vector>
  24. #include "absl/base/macros.h"
  25. #include "absl/meta/type_traits.h"
  26. #include "absl/random/internal/pool_urbg.h"
  27. #include "absl/random/internal/salted_seed_seq.h"
  28. #include "absl/random/internal/seed_material.h"
  29. #include "absl/types/optional.h"
  30. #include "absl/types/span.h"
  31. namespace absl {
  32. ABSL_NAMESPACE_BEGIN
  33. namespace random_internal {
  34. // Each instance of NonsecureURBGBase<URBG> will be seeded by variates produced
  35. // by a thread-unique URBG-instance.
  36. template <typename URBG>
  37. class NonsecureURBGBase {
  38. public:
  39. using result_type = typename URBG::result_type;
  40. // Default constructor
  41. NonsecureURBGBase() : urbg_(ConstructURBG()) {}
  42. // Copy disallowed, move allowed.
  43. NonsecureURBGBase(const NonsecureURBGBase&) = delete;
  44. NonsecureURBGBase& operator=(const NonsecureURBGBase&) = delete;
  45. NonsecureURBGBase(NonsecureURBGBase&&) = default;
  46. NonsecureURBGBase& operator=(NonsecureURBGBase&&) = default;
  47. // Constructor using a seed
  48. template <class SSeq, typename = typename absl::enable_if_t<
  49. !std::is_same<SSeq, NonsecureURBGBase>::value>>
  50. explicit NonsecureURBGBase(SSeq&& seq)
  51. : urbg_(ConstructURBG(std::forward<SSeq>(seq))) {}
  52. // Note: on MSVC, min() or max() can be interpreted as MIN() or MAX(), so we
  53. // enclose min() or max() in parens as (min)() and (max)().
  54. // Additionally, clang-format requires no space before this construction.
  55. // NonsecureURBGBase::min()
  56. static constexpr result_type(min)() { return (URBG::min)(); }
  57. // NonsecureURBGBase::max()
  58. static constexpr result_type(max)() { return (URBG::max)(); }
  59. // NonsecureURBGBase::operator()()
  60. result_type operator()() { return urbg_(); }
  61. // NonsecureURBGBase::discard()
  62. void discard(unsigned long long values) { // NOLINT(runtime/int)
  63. urbg_.discard(values);
  64. }
  65. bool operator==(const NonsecureURBGBase& other) const {
  66. return urbg_ == other.urbg_;
  67. }
  68. bool operator!=(const NonsecureURBGBase& other) const {
  69. return !(urbg_ == other.urbg_);
  70. }
  71. private:
  72. // Seeder is a custom seed sequence type where generate() fills the provided
  73. // buffer via the RandenPool entropy source.
  74. struct Seeder {
  75. using result_type = uint32_t;
  76. size_t size() { return 0; }
  77. template <typename OutIterator>
  78. void param(OutIterator) const {}
  79. template <typename RandomAccessIterator>
  80. void generate(RandomAccessIterator begin, RandomAccessIterator end) {
  81. if (begin != end) {
  82. // begin, end must be random access iterators assignable from uint32_t.
  83. generate_impl(
  84. std::integral_constant<bool, sizeof(*begin) == sizeof(uint32_t)>{},
  85. begin, end);
  86. }
  87. }
  88. // Commonly, generate is invoked with a pointer to a buffer which
  89. // can be cast to a uint32_t.
  90. template <typename RandomAccessIterator>
  91. void generate_impl(std::integral_constant<bool, true>,
  92. RandomAccessIterator begin, RandomAccessIterator end) {
  93. auto buffer = absl::MakeSpan(begin, end);
  94. auto target = absl::MakeSpan(reinterpret_cast<uint32_t*>(buffer.data()),
  95. buffer.size());
  96. RandenPool<uint32_t>::Fill(target);
  97. }
  98. // The non-uint32_t case should be uncommon, and involves an extra copy,
  99. // filling the uint32_t buffer and then mixing into the output.
  100. template <typename RandomAccessIterator>
  101. void generate_impl(std::integral_constant<bool, false>,
  102. RandomAccessIterator begin, RandomAccessIterator end) {
  103. const size_t n = std::distance(begin, end);
  104. absl::InlinedVector<uint32_t, 8> data(n, 0);
  105. RandenPool<uint32_t>::Fill(absl::MakeSpan(data.begin(), data.end()));
  106. std::copy(std::begin(data), std::end(data), begin);
  107. }
  108. };
  109. static URBG ConstructURBG() {
  110. Seeder seeder;
  111. return URBG(seeder);
  112. }
  113. template <typename SSeq>
  114. static URBG ConstructURBG(SSeq&& seq) { // NOLINT(runtime/references)
  115. auto salted_seq =
  116. random_internal::MakeSaltedSeedSeq(std::forward<SSeq>(seq));
  117. return URBG(salted_seq);
  118. }
  119. URBG urbg_;
  120. };
  121. } // namespace random_internal
  122. ABSL_NAMESPACE_END
  123. } // namespace absl
  124. #endif // ABSL_RANDOM_INTERNAL_NONSECURE_BASE_H_