summaryrefslogtreecommitdiff
path: root/boost/beast/core/detail/chacha.hpp
blob: 976ade5607a01d41a9e0bc63cf536c133c707a8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//

//
// This is a derivative work, original copyright follows:
//

/*
    Copyright (c) 2015 Orson Peters <orsonpeters@gmail.com>
    
    This software is provided 'as-is', without any express or implied warranty. In no event will the
    authors be held liable for any damages arising from the use of this software.
    
    Permission is granted to anyone to use this software for any purpose, including commercial
    applications, and to alter it and redistribute it freely, subject to the following restrictions:
    
    1. The origin of this software must not be misrepresented; you must not claim that you wrote the
       original software. If you use this software in a product, an acknowledgment in the product
       documentation would be appreciated but is not required.
    
    2. Altered source versions must be plainly marked as such, and must not be misrepresented as
       being the original software.
    
    3. This notice may not be removed or altered from any source distribution.
*/

#ifndef BOOST_BEAST_CORE_DETAIL_CHACHA_HPP
#define BOOST_BEAST_CORE_DETAIL_CHACHA_HPP

#include <cstdint>
#include <limits>
#include <iosfwd>

namespace boost {
namespace beast {
namespace detail {

template<std::size_t R>
class chacha
{
    void generate_block();
    void chacha_core();

    alignas(16) std::uint32_t block_[16];
    std::uint32_t keysetup_[8];
    std::uint64_t ctr_ = 0;
    int idx_ = 16;

public:
    static constexpr std::size_t state_size = sizeof(chacha::keysetup_);

    using result_type = std::uint32_t;

    chacha(std::uint32_t const* v, std::uint64_t stream);

    std::uint32_t
    operator()();
   
#if 0
    template<std::size_t R_>
    friend
    bool
    operator==(chacha<R_> const& lhs, chacha<R_> const& rhs);

    template<std::size_t R_>
    friend
    bool
    operator!=(chacha<R_> const& lhs, chacha<R_> const& rhs);

    static
    constexpr
    std::uint32_t
    min()
    {
        return (std::numeric_limits<std::uint32_t>::min)();
    }

    static
    constexpr
    std::uint32_t
    max()
    {
        return (std::numeric_limits<std::uint32_t>::max)();
    }
#endif
};

template<std::size_t R>
chacha<R>::
chacha(std::uint32_t const* v, std::uint64_t stream)
{
    for (int i = 0; i < 6; ++i)
        keysetup_[i] = v[i];
    keysetup_[6] = v[6] + (stream & 0xffffffff);
    keysetup_[7] = v[7] + ((stream >> 32) & 0xffffffff);
}

template<std::size_t R>
std::uint32_t
chacha<R>::
operator()()
{
    if(idx_ == 16)
    {
        idx_ = 0;
        ++ctr_;
        generate_block();
    }
    return block_[idx_++];
}

template<std::size_t R>
void
chacha<R>::
generate_block()
{
    std::uint32_t constexpr constants[4] = {
        0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 };
    std::uint32_t input[16];
    for (int i = 0; i < 4; ++i)
        input[i] = constants[i];
    for (int i = 0; i < 8; ++i)
        input[4 + i] = keysetup_[i];
    input[12] = (ctr_ / 16) & 0xffffffffu;
    input[13] = (ctr_ / 16) >> 32;
    input[14] = input[15] = 0xdeadbeef; // Could use 128-bit counter.
    for (int i = 0; i < 16; ++i)
        block_[i] = input[i];
    chacha_core();
    for (int i = 0; i < 16; ++i)
        block_[i] += input[i];
}

template<std::size_t R>
void
chacha<R>::
chacha_core()
{
    #define BOOST_BEAST_CHACHA_ROTL32(x, n) (((x) << (n)) | ((x) >> (32 - (n))))

    #define BOOST_BEAST_CHACHA_QUARTERROUND(x, a, b, c, d) \
        x[a] = x[a] + x[b]; x[d] ^= x[a]; x[d] = BOOST_BEAST_CHACHA_ROTL32(x[d], 16); \
        x[c] = x[c] + x[d]; x[b] ^= x[c]; x[b] = BOOST_BEAST_CHACHA_ROTL32(x[b], 12); \
        x[a] = x[a] + x[b]; x[d] ^= x[a]; x[d] = BOOST_BEAST_CHACHA_ROTL32(x[d],  8); \
        x[c] = x[c] + x[d]; x[b] ^= x[c]; x[b] = BOOST_BEAST_CHACHA_ROTL32(x[b],  7)

    for (unsigned i = 0; i < R; i += 2)
    {
        BOOST_BEAST_CHACHA_QUARTERROUND(block_, 0, 4, 8, 12);
        BOOST_BEAST_CHACHA_QUARTERROUND(block_, 1, 5, 9, 13);
        BOOST_BEAST_CHACHA_QUARTERROUND(block_, 2, 6, 10, 14);
        BOOST_BEAST_CHACHA_QUARTERROUND(block_, 3, 7, 11, 15);
        BOOST_BEAST_CHACHA_QUARTERROUND(block_, 0, 5, 10, 15);
        BOOST_BEAST_CHACHA_QUARTERROUND(block_, 1, 6, 11, 12);
        BOOST_BEAST_CHACHA_QUARTERROUND(block_, 2, 7, 8, 13);
        BOOST_BEAST_CHACHA_QUARTERROUND(block_, 3, 4, 9, 14);
    }

    #undef BOOST_BEAST_CHACHA_QUARTERROUND
    #undef BOOST_BEAST_CHACHA_ROTL32
}

//#endif

#if 0
// Implement <random> interface.

template<std::size_t R>
bool
operator==(chacha<R> const& lhs, chacha<R> const& rhs)
{
    for (int i = 0; i < 8; ++i)
        if (lhs.keysetup_[i] != rhs.keysetup_[i])
            return false;
    return lhs.ctr_ == rhs.ctr_;
}

template<std::size_t R>
bool
operator!=(chacha<R> const& lhs, chacha<R> const& rhs)
{
    return !(lhs == rhs);
}
#endif

} // detail
} // beast
} // boost

#endif