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.

randen_traits.h 4.2 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. #ifndef ABSL_RANDOM_INTERNAL_RANDEN_TRAITS_H_
  15. #define ABSL_RANDOM_INTERNAL_RANDEN_TRAITS_H_
  16. // HERMETIC NOTE: The randen_hwaes target must not introduce duplicate
  17. // symbols from arbitrary system and other headers, since it may be built
  18. // with different flags from other targets, using different levels of
  19. // optimization, potentially introducing ODR violations.
  20. #include <cstddef>
  21. #include "absl/base/config.h"
  22. namespace absl
  23. {
  24. ABSL_NAMESPACE_BEGIN
  25. namespace random_internal
  26. {
  27. // RANDen = RANDom generator or beetroots in Swiss High German.
  28. // 'Strong' (well-distributed, unpredictable, backtracking-resistant) random
  29. // generator, faster in some benchmarks than std::mt19937_64 and pcg64_c32.
  30. //
  31. // High-level summary:
  32. // 1) Reverie (see "A Robust and Sponge-Like PRNG with Improved Efficiency") is
  33. // a sponge-like random generator that requires a cryptographic permutation.
  34. // It improves upon "Provably Robust Sponge-Based PRNGs and KDFs" by
  35. // achieving backtracking resistance with only one Permute() per buffer.
  36. //
  37. // 2) "Simpira v2: A Family of Efficient Permutations Using the AES Round
  38. // Function" constructs up to 1024-bit permutations using an improved
  39. // Generalized Feistel network with 2-round AES-128 functions. This Feistel
  40. // block shuffle achieves diffusion faster and is less vulnerable to
  41. // sliced-biclique attacks than the Type-2 cyclic shuffle.
  42. //
  43. // 3) "Improving the Generalized Feistel" and "New criterion for diffusion
  44. // property" extends the same kind of improved Feistel block shuffle to 16
  45. // branches, which enables a 2048-bit permutation.
  46. //
  47. // Combine these three ideas and also change Simpira's subround keys from
  48. // structured/low-entropy counters to digits of Pi (or other random source).
  49. // RandenTraits contains the basic algorithm traits, such as the size of the
  50. // state, seed, sponge, etc.
  51. struct RandenTraits
  52. {
  53. // Size of the entire sponge / state for the randen PRNG.
  54. static constexpr size_t kStateBytes = 256; // 2048-bit
  55. // Size of the 'inner' (inaccessible) part of the sponge. Larger values would
  56. // require more frequent calls to RandenGenerate.
  57. static constexpr size_t kCapacityBytes = 16; // 128-bit
  58. // Size of the default seed consumed by the sponge.
  59. static constexpr size_t kSeedBytes = kStateBytes - kCapacityBytes;
  60. // Assuming 128-bit blocks, the number of blocks in the state.
  61. // Largest size for which security proofs are known.
  62. static constexpr size_t kFeistelBlocks = 16;
  63. // Ensures SPRP security and two full subblock diffusions.
  64. // Must be > 4 * log2(kFeistelBlocks).
  65. static constexpr size_t kFeistelRounds = 16 + 1;
  66. // Size of the key. A 128-bit key block is used for every-other
  67. // feistel block (Type-2 generalized Feistel network) in each round.
  68. static constexpr size_t kKeyBytes = 16 * kFeistelRounds * kFeistelBlocks / 2;
  69. };
  70. // Randen key arrays. In randen_round_keys.cc
  71. extern const unsigned char kRandenRoundKeys[RandenTraits::kKeyBytes];
  72. extern const unsigned char kRandenRoundKeysBE[RandenTraits::kKeyBytes];
  73. } // namespace random_internal
  74. ABSL_NAMESPACE_END
  75. } // namespace absl
  76. #endif // ABSL_RANDOM_INTERNAL_RANDEN_TRAITS_H_