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.

nonsecure_base.h 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <iterator>
  19. #include <type_traits>
  20. #include <utility>
  21. #include <vector>
  22. #include "absl/base/macros.h"
  23. #include "absl/container/inlined_vector.h"
  24. #include "absl/meta/type_traits.h"
  25. #include "absl/random/internal/pool_urbg.h"
  26. #include "absl/random/internal/salted_seed_seq.h"
  27. #include "absl/random/internal/seed_material.h"
  28. #include "absl/types/span.h"
  29. namespace absl
  30. {
  31. ABSL_NAMESPACE_BEGIN
  32. namespace random_internal
  33. {
  34. // RandenPoolSeedSeq is a custom seed sequence type where generate() fills the
  35. // provided buffer via the RandenPool entropy source.
  36. class RandenPoolSeedSeq
  37. {
  38. private:
  39. struct ContiguousTag
  40. {
  41. };
  42. struct BufferTag
  43. {
  44. };
  45. // Generate random unsigned values directly into the buffer.
  46. template<typename Contiguous>
  47. void generate_impl(ContiguousTag, Contiguous begin, Contiguous end)
  48. {
  49. const size_t n = std::distance(begin, end);
  50. auto* a = &(*begin);
  51. RandenPool<uint8_t>::Fill(
  52. absl::MakeSpan(reinterpret_cast<uint8_t*>(a), sizeof(*a) * n)
  53. );
  54. }
  55. // Construct a buffer of size n and fill it with values, then copy
  56. // those values into the seed iterators.
  57. template<typename RandomAccessIterator>
  58. void generate_impl(BufferTag, RandomAccessIterator begin, RandomAccessIterator end)
  59. {
  60. const size_t n = std::distance(begin, end);
  61. absl::InlinedVector<uint32_t, 8> data(n, 0);
  62. RandenPool<uint32_t>::Fill(absl::MakeSpan(data.begin(), data.end()));
  63. std::copy(std::begin(data), std::end(data), begin);
  64. }
  65. public:
  66. using result_type = uint32_t;
  67. size_t size()
  68. {
  69. return 0;
  70. }
  71. template<typename OutIterator>
  72. void param(OutIterator) const
  73. {
  74. }
  75. template<typename RandomAccessIterator>
  76. void generate(RandomAccessIterator begin, RandomAccessIterator end)
  77. {
  78. // RandomAccessIterator must be assignable from uint32_t
  79. if (begin != end)
  80. {
  81. using U = typename std::iterator_traits<RandomAccessIterator>::value_type;
  82. // ContiguousTag indicates the common case of a known contiguous buffer,
  83. // which allows directly filling the buffer. In C++20,
  84. // std::contiguous_iterator_tag provides a mechanism for testing this
  85. // capability, however until Abseil's support requirements allow us to
  86. // assume C++20, limit checks to a few common cases.
  87. using TagType = absl::conditional_t<
  88. (std::is_pointer<RandomAccessIterator>::value ||
  89. std::is_same<RandomAccessIterator, typename std::vector<U>::iterator>::value),
  90. ContiguousTag,
  91. BufferTag>;
  92. generate_impl(TagType{}, begin, end);
  93. }
  94. }
  95. };
  96. // Each instance of NonsecureURBGBase<URBG> will be seeded by variates produced
  97. // by a thread-unique URBG-instance.
  98. template<typename URBG, typename Seeder = RandenPoolSeedSeq>
  99. class NonsecureURBGBase
  100. {
  101. public:
  102. using result_type = typename URBG::result_type;
  103. // Default constructor
  104. NonsecureURBGBase() :
  105. urbg_(ConstructURBG())
  106. {
  107. }
  108. // Copy disallowed, move allowed.
  109. NonsecureURBGBase(const NonsecureURBGBase&) = delete;
  110. NonsecureURBGBase& operator=(const NonsecureURBGBase&) = delete;
  111. NonsecureURBGBase(NonsecureURBGBase&&) = default;
  112. NonsecureURBGBase& operator=(NonsecureURBGBase&&) = default;
  113. // Constructor using a seed
  114. template<class SSeq, typename = typename absl::enable_if_t<!std::is_same<SSeq, NonsecureURBGBase>::value>>
  115. explicit NonsecureURBGBase(SSeq&& seq) :
  116. urbg_(ConstructURBG(std::forward<SSeq>(seq)))
  117. {
  118. }
  119. // Note: on MSVC, min() or max() can be interpreted as MIN() or MAX(), so we
  120. // enclose min() or max() in parens as (min)() and (max)().
  121. // Additionally, clang-format requires no space before this construction.
  122. // NonsecureURBGBase::min()
  123. static constexpr result_type(min)()
  124. {
  125. return (URBG::min)();
  126. }
  127. // NonsecureURBGBase::max()
  128. static constexpr result_type(max)()
  129. {
  130. return (URBG::max)();
  131. }
  132. // NonsecureURBGBase::operator()()
  133. result_type operator()()
  134. {
  135. return urbg_();
  136. }
  137. // NonsecureURBGBase::discard()
  138. void discard(unsigned long long values)
  139. { // NOLINT(runtime/int)
  140. urbg_.discard(values);
  141. }
  142. bool operator==(const NonsecureURBGBase& other) const
  143. {
  144. return urbg_ == other.urbg_;
  145. }
  146. bool operator!=(const NonsecureURBGBase& other) const
  147. {
  148. return !(urbg_ == other.urbg_);
  149. }
  150. private:
  151. static URBG ConstructURBG()
  152. {
  153. Seeder seeder;
  154. return URBG(seeder);
  155. }
  156. template<typename SSeq>
  157. static URBG ConstructURBG(SSeq&& seq)
  158. { // NOLINT(runtime/references)
  159. auto salted_seq =
  160. random_internal::MakeSaltedSeedSeq(std::forward<SSeq>(seq));
  161. return URBG(salted_seq);
  162. }
  163. URBG urbg_;
  164. };
  165. } // namespace random_internal
  166. ABSL_NAMESPACE_END
  167. } // namespace absl
  168. #endif // ABSL_RANDOM_INTERNAL_NONSECURE_BASE_H_