randen.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include "absl/random/internal/randen.h"
  15. #include "absl/base/internal/raw_logging.h"
  16. #include "absl/random/internal/randen_detect.h"
  17. // RANDen = RANDom generator or beetroots in Swiss High German.
  18. // 'Strong' (well-distributed, unpredictable, backtracking-resistant) random
  19. // generator, faster in some benchmarks than std::mt19937_64 and pcg64_c32.
  20. //
  21. // High-level summary:
  22. // 1) Reverie (see "A Robust and Sponge-Like PRNG with Improved Efficiency") is
  23. // a sponge-like random generator that requires a cryptographic permutation.
  24. // It improves upon "Provably Robust Sponge-Based PRNGs and KDFs" by
  25. // achieving backtracking resistance with only one Permute() per buffer.
  26. //
  27. // 2) "Simpira v2: A Family of Efficient Permutations Using the AES Round
  28. // Function" constructs up to 1024-bit permutations using an improved
  29. // Generalized Feistel network with 2-round AES-128 functions. This Feistel
  30. // block shuffle achieves diffusion faster and is less vulnerable to
  31. // sliced-biclique attacks than the Type-2 cyclic shuffle.
  32. //
  33. // 3) "Improving the Generalized Feistel" and "New criterion for diffusion
  34. // property" extends the same kind of improved Feistel block shuffle to 16
  35. // branches, which enables a 2048-bit permutation.
  36. //
  37. // We combine these three ideas and also change Simpira's subround keys from
  38. // structured/low-entropy counters to digits of Pi.
  39. namespace absl {
  40. ABSL_NAMESPACE_BEGIN
  41. namespace random_internal {
  42. namespace {
  43. struct RandenState {
  44. const void* keys;
  45. bool has_crypto;
  46. };
  47. RandenState GetRandenState() {
  48. static const RandenState state = []() {
  49. RandenState tmp;
  50. #if ABSL_RANDOM_INTERNAL_AES_DISPATCH
  51. // HW AES Dispatch.
  52. if (HasRandenHwAesImplementation() && CPUSupportsRandenHwAes()) {
  53. tmp.has_crypto = true;
  54. tmp.keys = RandenHwAes::GetKeys();
  55. } else {
  56. tmp.has_crypto = false;
  57. tmp.keys = RandenSlow::GetKeys();
  58. }
  59. #elif ABSL_HAVE_ACCELERATED_AES
  60. // HW AES is enabled.
  61. tmp.has_crypto = true;
  62. tmp.keys = RandenHwAes::GetKeys();
  63. #else
  64. // HW AES is disabled.
  65. tmp.has_crypto = false;
  66. tmp.keys = RandenSlow::GetKeys();
  67. #endif
  68. return tmp;
  69. }();
  70. return state;
  71. }
  72. } // namespace
  73. Randen::Randen() {
  74. auto tmp = GetRandenState();
  75. keys_ = tmp.keys;
  76. #if ABSL_RANDOM_INTERNAL_AES_DISPATCH
  77. has_crypto_ = tmp.has_crypto;
  78. #endif
  79. }
  80. } // namespace random_internal
  81. ABSL_NAMESPACE_END
  82. } // namespace absl