Coverage Report

Created: 2026-07-29 23:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/crypto/chacha20.cpp
Line
Count
Source
1
// Copyright (c) 2017-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
// Based on the public domain implementation 'merged' by D. J. Bernstein
6
// See https://cr.yp.to/chacha.html.
7
8
#include <crypto/common.h>
9
#include <crypto/chacha20.h>
10
#include <support/cleanse.h>
11
12
#include <algorithm>
13
#include <bit>
14
#include <cassert>
15
16
#define QUARTERROUND(a,b,c,d) \
17
  a += b; d = std::rotl(d ^ a, 16); \
18
  c += d; b = std::rotl(b ^ c, 12); \
19
  a += b; d = std::rotl(d ^ a, 8); \
20
  c += d; b = std::rotl(b ^ c, 7);
21
22
17.4M
#define REPEAT10(a) do { {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; } while(0)
23
24
void ChaCha20Aligned::SetKey(std::span<const std::byte> key) noexcept
25
5.31M
{
26
5.31M
    assert(key.size() == KEYLEN);
27
5.31M
    input[0] = ReadLE32(key.data() + 0);
28
5.31M
    input[1] = ReadLE32(key.data() + 4);
29
5.31M
    input[2] = ReadLE32(key.data() + 8);
30
5.31M
    input[3] = ReadLE32(key.data() + 12);
31
5.31M
    input[4] = ReadLE32(key.data() + 16);
32
5.31M
    input[5] = ReadLE32(key.data() + 20);
33
5.31M
    input[6] = ReadLE32(key.data() + 24);
34
5.31M
    input[7] = ReadLE32(key.data() + 28);
35
5.31M
    input[8] = 0;
36
5.31M
    input[9] = 0;
37
5.31M
    input[10] = 0;
38
5.31M
    input[11] = 0;
39
5.31M
}
40
41
ChaCha20Aligned::~ChaCha20Aligned()
42
2.96M
{
43
2.96M
    memory_cleanse(input, sizeof(input));
44
2.96M
}
45
46
ChaCha20Aligned::ChaCha20Aligned(std::span<const std::byte> key) noexcept
47
2.96M
{
48
2.96M
    SetKey(key);
49
2.96M
}
50
51
void ChaCha20Aligned::Seek(Nonce96 nonce, uint32_t block_counter) noexcept
52
2.06M
{
53
2.06M
    input[8] = block_counter;
54
2.06M
    input[9] = nonce.first;
55
2.06M
    input[10] = nonce.second;
56
2.06M
    input[11] = nonce.second >> 32;
57
2.06M
}
58
59
inline void ChaCha20Aligned::Keystream(std::span<std::byte> output) noexcept
60
8.85M
{
61
8.85M
    std::byte* c = output.data();
62
8.85M
    size_t blocks = output.size() / BLOCKLEN;
63
8.85M
    assert(blocks * BLOCKLEN == output.size());
64
65
8.85M
    uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
66
8.85M
    uint32_t j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
67
68
8.85M
    if (!blocks) return;
69
70
8.85M
    j4 = input[0];
71
8.85M
    j5 = input[1];
72
8.85M
    j6 = input[2];
73
8.85M
    j7 = input[3];
74
8.85M
    j8 = input[4];
75
8.85M
    j9 = input[5];
76
8.85M
    j10 = input[6];
77
8.85M
    j11 = input[7];
78
8.85M
    j12 = input[8];
79
8.85M
    j13 = input[9];
80
8.85M
    j14 = input[10];
81
8.85M
    j15 = input[11];
82
83
11.6M
    for (;;) {
84
11.6M
        x0 = 0x61707865;
85
11.6M
        x1 = 0x3320646e;
86
11.6M
        x2 = 0x79622d32;
87
11.6M
        x3 = 0x6b206574;
88
11.6M
        x4 = j4;
89
11.6M
        x5 = j5;
90
11.6M
        x6 = j6;
91
11.6M
        x7 = j7;
92
11.6M
        x8 = j8;
93
11.6M
        x9 = j9;
94
11.6M
        x10 = j10;
95
11.6M
        x11 = j11;
96
11.6M
        x12 = j12;
97
11.6M
        x13 = j13;
98
11.6M
        x14 = j14;
99
11.6M
        x15 = j15;
100
101
        // The 20 inner ChaCha20 rounds are unrolled here for performance.
102
11.6M
        REPEAT10(
103
11.6M
            QUARTERROUND( x0, x4, x8,x12);
104
11.6M
            QUARTERROUND( x1, x5, x9,x13);
105
11.6M
            QUARTERROUND( x2, x6,x10,x14);
106
11.6M
            QUARTERROUND( x3, x7,x11,x15);
107
11.6M
            QUARTERROUND( x0, x5,x10,x15);
108
11.6M
            QUARTERROUND( x1, x6,x11,x12);
109
11.6M
            QUARTERROUND( x2, x7, x8,x13);
110
11.6M
            QUARTERROUND( x3, x4, x9,x14);
111
11.6M
        );
112
113
11.6M
        x0 += 0x61707865;
114
11.6M
        x1 += 0x3320646e;
115
11.6M
        x2 += 0x79622d32;
116
11.6M
        x3 += 0x6b206574;
117
11.6M
        x4 += j4;
118
11.6M
        x5 += j5;
119
11.6M
        x6 += j6;
120
11.6M
        x7 += j7;
121
11.6M
        x8 += j8;
122
11.6M
        x9 += j9;
123
11.6M
        x10 += j10;
124
11.6M
        x11 += j11;
125
11.6M
        x12 += j12;
126
11.6M
        x13 += j13;
127
11.6M
        x14 += j14;
128
11.6M
        x15 += j15;
129
130
11.6M
        ++j12;
131
11.6M
        if (!j12) ++j13;
132
133
11.6M
        WriteLE32(c + 0, x0);
134
11.6M
        WriteLE32(c + 4, x1);
135
11.6M
        WriteLE32(c + 8, x2);
136
11.6M
        WriteLE32(c + 12, x3);
137
11.6M
        WriteLE32(c + 16, x4);
138
11.6M
        WriteLE32(c + 20, x5);
139
11.6M
        WriteLE32(c + 24, x6);
140
11.6M
        WriteLE32(c + 28, x7);
141
11.6M
        WriteLE32(c + 32, x8);
142
11.6M
        WriteLE32(c + 36, x9);
143
11.6M
        WriteLE32(c + 40, x10);
144
11.6M
        WriteLE32(c + 44, x11);
145
11.6M
        WriteLE32(c + 48, x12);
146
11.6M
        WriteLE32(c + 52, x13);
147
11.6M
        WriteLE32(c + 56, x14);
148
11.6M
        WriteLE32(c + 60, x15);
149
150
11.6M
        if (blocks == 1) {
151
8.85M
            input[8] = j12;
152
8.85M
            input[9] = j13;
153
8.85M
            return;
154
8.85M
        }
155
2.76M
        blocks -= 1;
156
2.76M
        c += BLOCKLEN;
157
2.76M
    }
158
8.85M
}
159
160
inline void ChaCha20Aligned::Crypt(std::span<const std::byte> in_bytes, std::span<std::byte> out_bytes) noexcept
161
89.8k
{
162
89.8k
    assert(in_bytes.size() == out_bytes.size());
163
89.8k
    const std::byte* m = in_bytes.data();
164
89.8k
    std::byte* c = out_bytes.data();
165
89.8k
    size_t blocks = out_bytes.size() / BLOCKLEN;
166
89.8k
    assert(blocks * BLOCKLEN == out_bytes.size());
167
168
89.8k
    uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
169
89.8k
    uint32_t j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
170
171
89.8k
    if (!blocks) return;
172
173
89.8k
    j4 = input[0];
174
89.8k
    j5 = input[1];
175
89.8k
    j6 = input[2];
176
89.8k
    j7 = input[3];
177
89.8k
    j8 = input[4];
178
89.8k
    j9 = input[5];
179
89.8k
    j10 = input[6];
180
89.8k
    j11 = input[7];
181
89.8k
    j12 = input[8];
182
89.8k
    j13 = input[9];
183
89.8k
    j14 = input[10];
184
89.8k
    j15 = input[11];
185
186
5.80M
    for (;;) {
187
5.80M
        x0 = 0x61707865;
188
5.80M
        x1 = 0x3320646e;
189
5.80M
        x2 = 0x79622d32;
190
5.80M
        x3 = 0x6b206574;
191
5.80M
        x4 = j4;
192
5.80M
        x5 = j5;
193
5.80M
        x6 = j6;
194
5.80M
        x7 = j7;
195
5.80M
        x8 = j8;
196
5.80M
        x9 = j9;
197
5.80M
        x10 = j10;
198
5.80M
        x11 = j11;
199
5.80M
        x12 = j12;
200
5.80M
        x13 = j13;
201
5.80M
        x14 = j14;
202
5.80M
        x15 = j15;
203
204
        // The 20 inner ChaCha20 rounds are unrolled here for performance.
205
5.80M
        REPEAT10(
206
5.80M
            QUARTERROUND( x0, x4, x8,x12);
207
5.80M
            QUARTERROUND( x1, x5, x9,x13);
208
5.80M
            QUARTERROUND( x2, x6,x10,x14);
209
5.80M
            QUARTERROUND( x3, x7,x11,x15);
210
5.80M
            QUARTERROUND( x0, x5,x10,x15);
211
5.80M
            QUARTERROUND( x1, x6,x11,x12);
212
5.80M
            QUARTERROUND( x2, x7, x8,x13);
213
5.80M
            QUARTERROUND( x3, x4, x9,x14);
214
5.80M
        );
215
216
5.80M
        x0 += 0x61707865;
217
5.80M
        x1 += 0x3320646e;
218
5.80M
        x2 += 0x79622d32;
219
5.80M
        x3 += 0x6b206574;
220
5.80M
        x4 += j4;
221
5.80M
        x5 += j5;
222
5.80M
        x6 += j6;
223
5.80M
        x7 += j7;
224
5.80M
        x8 += j8;
225
5.80M
        x9 += j9;
226
5.80M
        x10 += j10;
227
5.80M
        x11 += j11;
228
5.80M
        x12 += j12;
229
5.80M
        x13 += j13;
230
5.80M
        x14 += j14;
231
5.80M
        x15 += j15;
232
233
5.80M
        x0 ^= ReadLE32(m + 0);
234
5.80M
        x1 ^= ReadLE32(m + 4);
235
5.80M
        x2 ^= ReadLE32(m + 8);
236
5.80M
        x3 ^= ReadLE32(m + 12);
237
5.80M
        x4 ^= ReadLE32(m + 16);
238
5.80M
        x5 ^= ReadLE32(m + 20);
239
5.80M
        x6 ^= ReadLE32(m + 24);
240
5.80M
        x7 ^= ReadLE32(m + 28);
241
5.80M
        x8 ^= ReadLE32(m + 32);
242
5.80M
        x9 ^= ReadLE32(m + 36);
243
5.80M
        x10 ^= ReadLE32(m + 40);
244
5.80M
        x11 ^= ReadLE32(m + 44);
245
5.80M
        x12 ^= ReadLE32(m + 48);
246
5.80M
        x13 ^= ReadLE32(m + 52);
247
5.80M
        x14 ^= ReadLE32(m + 56);
248
5.80M
        x15 ^= ReadLE32(m + 60);
249
250
5.80M
        ++j12;
251
5.80M
        if (!j12) ++j13;
252
253
5.80M
        WriteLE32(c + 0, x0);
254
5.80M
        WriteLE32(c + 4, x1);
255
5.80M
        WriteLE32(c + 8, x2);
256
5.80M
        WriteLE32(c + 12, x3);
257
5.80M
        WriteLE32(c + 16, x4);
258
5.80M
        WriteLE32(c + 20, x5);
259
5.80M
        WriteLE32(c + 24, x6);
260
5.80M
        WriteLE32(c + 28, x7);
261
5.80M
        WriteLE32(c + 32, x8);
262
5.80M
        WriteLE32(c + 36, x9);
263
5.80M
        WriteLE32(c + 40, x10);
264
5.80M
        WriteLE32(c + 44, x11);
265
5.80M
        WriteLE32(c + 48, x12);
266
5.80M
        WriteLE32(c + 52, x13);
267
5.80M
        WriteLE32(c + 56, x14);
268
5.80M
        WriteLE32(c + 60, x15);
269
270
5.80M
        if (blocks == 1) {
271
89.8k
            input[8] = j12;
272
89.8k
            input[9] = j13;
273
89.8k
            return;
274
89.8k
        }
275
5.71M
        blocks -= 1;
276
5.71M
        c += BLOCKLEN;
277
5.71M
        m += BLOCKLEN;
278
5.71M
    }
279
89.8k
}
280
281
void ChaCha20::Keystream(std::span<std::byte> out) noexcept
282
37.3M
{
283
37.3M
    if (out.empty()) return;
284
37.3M
    if (m_bufleft) {
285
29.0M
        unsigned reuse = std::min<size_t>(m_bufleft, out.size());
286
29.0M
        std::copy(m_buffer.end() - m_bufleft, m_buffer.end() - m_bufleft + reuse, out.begin());
287
29.0M
        m_bufleft -= reuse;
288
29.0M
        out = out.subspan(reuse);
289
29.0M
    }
290
37.3M
    if (out.size() >= m_aligned.BLOCKLEN) {
291
1.44M
        size_t blocks = out.size() / m_aligned.BLOCKLEN;
292
1.44M
        m_aligned.Keystream(out.first(blocks * m_aligned.BLOCKLEN));
293
1.44M
        out = out.subspan(blocks * m_aligned.BLOCKLEN);
294
1.44M
    }
295
37.3M
    if (!out.empty()) {
296
7.13M
        m_aligned.Keystream(m_buffer);
297
7.13M
        std::copy(m_buffer.begin(), m_buffer.begin() + out.size(), out.begin());
298
7.13M
        m_bufleft = m_aligned.BLOCKLEN - out.size();
299
7.13M
    }
300
37.3M
}
301
302
void ChaCha20::Crypt(std::span<const std::byte> input, std::span<std::byte> output) noexcept
303
1.71M
{
304
1.71M
    assert(input.size() == output.size());
305
306
1.71M
    if (!input.size()) return;
307
462k
    if (m_bufleft) {
308
299k
        unsigned reuse = std::min<size_t>(m_bufleft, input.size());
309
7.51M
        for (unsigned i = 0; i < reuse; i++) {
310
7.21M
            output[i] = input[i] ^ m_buffer[m_aligned.BLOCKLEN - m_bufleft + i];
311
7.21M
        }
312
299k
        m_bufleft -= reuse;
313
299k
        output = output.subspan(reuse);
314
299k
        input = input.subspan(reuse);
315
299k
    }
316
462k
    if (input.size() >= m_aligned.BLOCKLEN) {
317
89.8k
        size_t blocks = input.size() / m_aligned.BLOCKLEN;
318
89.8k
        m_aligned.Crypt(input.first(blocks * m_aligned.BLOCKLEN), output.first(blocks * m_aligned.BLOCKLEN));
319
89.8k
        output = output.subspan(blocks * m_aligned.BLOCKLEN);
320
89.8k
        input = input.subspan(blocks * m_aligned.BLOCKLEN);
321
89.8k
    }
322
462k
    if (!input.empty()) {
323
264k
        m_aligned.Keystream(m_buffer);
324
3.54M
        for (unsigned i = 0; i < input.size(); i++) {
325
3.27M
            output[i] = input[i] ^ m_buffer[i];
326
3.27M
        }
327
264k
        m_bufleft = m_aligned.BLOCKLEN - input.size();
328
264k
    }
329
462k
}
330
331
ChaCha20::~ChaCha20()
332
2.96M
{
333
2.96M
    memory_cleanse(m_buffer.data(), m_buffer.size());
334
2.96M
}
335
336
void ChaCha20::SetKey(std::span<const std::byte> key) noexcept
337
2.34M
{
338
2.34M
    m_aligned.SetKey(key);
339
2.34M
    m_bufleft = 0;
340
2.34M
    memory_cleanse(m_buffer.data(), m_buffer.size());
341
2.34M
}
342
343
FSChaCha20::FSChaCha20(std::span<const std::byte> key, uint32_t rekey_interval) noexcept :
344
871
    m_chacha20(key), m_rekey_interval(rekey_interval)
345
871
{
346
871
    assert(key.size() == KEYLEN);
347
871
}
348
349
void FSChaCha20::Crypt(std::span<const std::byte> input, std::span<std::byte> output) noexcept
350
191k
{
351
191k
    assert(input.size() == output.size());
352
353
    // Invoke internal stream cipher for actual encryption/decryption.
354
191k
    m_chacha20.Crypt(input, output);
355
356
    // Rekey after m_rekey_interval encryptions/decryptions.
357
191k
    if (++m_chunk_counter == m_rekey_interval) {
358
        // Get new key from the stream cipher.
359
727
        std::byte new_key[KEYLEN];
360
727
        m_chacha20.Keystream(new_key);
361
        // Update its key.
362
727
        m_chacha20.SetKey(new_key);
363
        // Wipe the key (a copy remains inside m_chacha20, where it'll be wiped on the next rekey
364
        // or on destruction).
365
727
        memory_cleanse(new_key, sizeof(new_key));
366
        // Set the nonce for the new section of output.
367
727
        m_chacha20.Seek({0, ++m_rekey_counter}, 0);
368
        // Reset the chunk counter.
369
727
        m_chunk_counter = 0;
370
727
    }
371
191k
}