chacha.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. //
  10. // This is a derivative work, original copyright follows:
  11. //
  12. /*
  13. Copyright (c) 2015 Orson Peters <orsonpeters@gmail.com>
  14. This software is provided 'as-is', without any express or implied warranty. In no event will the
  15. authors be held liable for any damages arising from the use of this software.
  16. Permission is granted to anyone to use this software for any purpose, including commercial
  17. applications, and to alter it and redistribute it freely, subject to the following restrictions:
  18. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the
  19. original software. If you use this software in a product, an acknowledgment in the product
  20. documentation would be appreciated but is not required.
  21. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
  22. being the original software.
  23. 3. This notice may not be removed or altered from any source distribution.
  24. */
  25. #ifndef BOOST_BEAST_CORE_DETAIL_CHACHA_HPP
  26. #define BOOST_BEAST_CORE_DETAIL_CHACHA_HPP
  27. #include <cstdint>
  28. #include <limits>
  29. namespace boost {
  30. namespace beast {
  31. namespace detail {
  32. template<std::size_t R>
  33. class chacha
  34. {
  35. alignas(16) std::uint32_t block_[16];
  36. std::uint32_t keysetup_[8];
  37. std::uint64_t ctr_ = 0;
  38. int idx_ = 16;
  39. void generate_block()
  40. {
  41. std::uint32_t constexpr constants[4] = {
  42. 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 };
  43. std::uint32_t input[16];
  44. for (int i = 0; i < 4; ++i)
  45. input[i] = constants[i];
  46. for (int i = 0; i < 8; ++i)
  47. input[4 + i] = keysetup_[i];
  48. input[12] = (ctr_ / 16) & 0xffffffffu;
  49. input[13] = (ctr_ / 16) >> 32;
  50. input[14] = input[15] = 0xdeadbeef; // Could use 128-bit counter.
  51. for (int i = 0; i < 16; ++i)
  52. block_[i] = input[i];
  53. chacha_core();
  54. for (int i = 0; i < 16; ++i)
  55. block_[i] += input[i];
  56. }
  57. void chacha_core()
  58. {
  59. #define BOOST_BEAST_CHACHA_ROTL32(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
  60. #define BOOST_BEAST_CHACHA_QUARTERROUND(x, a, b, c, d) \
  61. x[a] = x[a] + x[b]; x[d] ^= x[a]; x[d] = BOOST_BEAST_CHACHA_ROTL32(x[d], 16); \
  62. x[c] = x[c] + x[d]; x[b] ^= x[c]; x[b] = BOOST_BEAST_CHACHA_ROTL32(x[b], 12); \
  63. x[a] = x[a] + x[b]; x[d] ^= x[a]; x[d] = BOOST_BEAST_CHACHA_ROTL32(x[d], 8); \
  64. x[c] = x[c] + x[d]; x[b] ^= x[c]; x[b] = BOOST_BEAST_CHACHA_ROTL32(x[b], 7)
  65. for (unsigned i = 0; i < R; i += 2)
  66. {
  67. BOOST_BEAST_CHACHA_QUARTERROUND(block_, 0, 4, 8, 12);
  68. BOOST_BEAST_CHACHA_QUARTERROUND(block_, 1, 5, 9, 13);
  69. BOOST_BEAST_CHACHA_QUARTERROUND(block_, 2, 6, 10, 14);
  70. BOOST_BEAST_CHACHA_QUARTERROUND(block_, 3, 7, 11, 15);
  71. BOOST_BEAST_CHACHA_QUARTERROUND(block_, 0, 5, 10, 15);
  72. BOOST_BEAST_CHACHA_QUARTERROUND(block_, 1, 6, 11, 12);
  73. BOOST_BEAST_CHACHA_QUARTERROUND(block_, 2, 7, 8, 13);
  74. BOOST_BEAST_CHACHA_QUARTERROUND(block_, 3, 4, 9, 14);
  75. }
  76. #undef BOOST_BEAST_CHACHA_QUARTERROUND
  77. #undef BOOST_BEAST_CHACHA_ROTL32
  78. }
  79. public:
  80. static constexpr std::size_t state_size = sizeof(chacha::keysetup_);
  81. using result_type = std::uint32_t;
  82. chacha(std::uint32_t const* v, std::uint64_t stream)
  83. {
  84. for (int i = 0; i < 6; ++i)
  85. keysetup_[i] = v[i];
  86. keysetup_[6] = v[6] + (stream & 0xffffffff);
  87. keysetup_[7] = v[7] + ((stream >> 32) & 0xffffffff);
  88. }
  89. std::uint32_t
  90. operator()()
  91. {
  92. if(idx_ == 16)
  93. {
  94. idx_ = 0;
  95. ++ctr_;
  96. generate_block();
  97. }
  98. return block_[idx_++];
  99. }
  100. };
  101. } // detail
  102. } // beast
  103. } // boost
  104. #endif