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