Branch data Line data Source code
1 : : // Copyright (c) 2020-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 : : #include <crypto/chacha20.h>
6 : : #include <test/fuzz/FuzzedDataProvider.h>
7 : : #include <test/fuzz/fuzz.h>
8 : : #include <test/fuzz/util.h>
9 : : #include <util/check.h>
10 : :
11 : : #include <cstdint>
12 : : #include <vector>
13 : :
14 : : /*
15 : : From https://cr.yp.to/chacha.html
16 : : chacha-merged.c version 20080118
17 : : D. J. Bernstein
18 : : Public domain.
19 : : */
20 : :
21 : : typedef unsigned int u32;
22 : : typedef unsigned char u8;
23 : :
24 : : #define U8C(v) (v##U)
25 : : #define U32C(v) (v##U)
26 : :
27 : : #define U8V(v) ((u8)(v)&U8C(0xFF))
28 : : #define U32V(v) ((u32)(v)&U32C(0xFFFFFFFF))
29 : :
30 : : #define ROTL32(v, n) (U32V((v) << (n)) | ((v) >> (32 - (n))))
31 : :
32 : : #define U8TO32_LITTLE(p) \
33 : : (((u32)((p)[0])) | ((u32)((p)[1]) << 8) | ((u32)((p)[2]) << 16) | \
34 : : ((u32)((p)[3]) << 24))
35 : :
36 : : #define U32TO8_LITTLE(p, v) \
37 : : do { \
38 : : (p)[0] = U8V((v)); \
39 : : (p)[1] = U8V((v) >> 8); \
40 : : (p)[2] = U8V((v) >> 16); \
41 : : (p)[3] = U8V((v) >> 24); \
42 : : } while (0)
43 : :
44 : : /* ------------------------------------------------------------------------- */
45 : : /* Data structures */
46 : :
47 : : typedef struct
48 : : {
49 : : u32 input[16];
50 : : } ECRYPT_ctx;
51 : :
52 : : /* ------------------------------------------------------------------------- */
53 : : /* Mandatory functions */
54 : :
55 : : void ECRYPT_keysetup(
56 : : ECRYPT_ctx* ctx,
57 : : const u8* key,
58 : : u32 keysize, /* Key size in bits. */
59 : : u32 ivsize); /* IV size in bits. */
60 : :
61 : : void ECRYPT_ivsetup(
62 : : ECRYPT_ctx* ctx,
63 : : const u8* iv);
64 : :
65 : : void ECRYPT_encrypt_bytes(
66 : : ECRYPT_ctx* ctx,
67 : : const u8* plaintext,
68 : : u8* ciphertext,
69 : : u32 msglen); /* Message length in bytes. */
70 : :
71 : : /* ------------------------------------------------------------------------- */
72 : :
73 : : /* Optional features */
74 : :
75 : : void ECRYPT_keystream_bytes(
76 : : ECRYPT_ctx* ctx,
77 : : u8* keystream,
78 : : u32 length); /* Length of keystream in bytes. */
79 : :
80 : : /* ------------------------------------------------------------------------- */
81 : :
82 : : #define ROTATE(v, c) (ROTL32(v, c))
83 : : #define XOR(v, w) ((v) ^ (w))
84 : : #define PLUS(v, w) (U32V((v) + (w)))
85 : : #define PLUSONE(v) (PLUS((v), 1))
86 : :
87 : : #define QUARTERROUND(a, b, c, d) \
88 : : a = PLUS(a, b); d = ROTATE(XOR(d, a), 16); \
89 : : c = PLUS(c, d); b = ROTATE(XOR(b, c), 12); \
90 : : a = PLUS(a, b); d = ROTATE(XOR(d, a), 8); \
91 : : c = PLUS(c, d); b = ROTATE(XOR(b, c), 7);
92 : :
93 : : static const char sigma[] = "expand 32-byte k";
94 : : static const char tau[] = "expand 16-byte k";
95 : :
96 : 6932 : void ECRYPT_keysetup(ECRYPT_ctx* x, const u8* k, u32 kbits, u32 ivbits)
97 : : {
98 : 6932 : const char* constants;
99 : :
100 : 6932 : x->input[4] = U8TO32_LITTLE(k + 0);
101 : 6932 : x->input[5] = U8TO32_LITTLE(k + 4);
102 : 6932 : x->input[6] = U8TO32_LITTLE(k + 8);
103 : 6932 : x->input[7] = U8TO32_LITTLE(k + 12);
104 [ + - ]: 6932 : if (kbits == 256) { /* recommended */
105 : 6932 : k += 16;
106 : 6932 : constants = sigma;
107 : : } else { /* kbits == 128 */
108 : : constants = tau;
109 : : }
110 : 6932 : x->input[8] = U8TO32_LITTLE(k + 0);
111 : 6932 : x->input[9] = U8TO32_LITTLE(k + 4);
112 : 6932 : x->input[10] = U8TO32_LITTLE(k + 8);
113 : 6932 : x->input[11] = U8TO32_LITTLE(k + 12);
114 : 6932 : x->input[0] = U8TO32_LITTLE(constants + 0);
115 : 6932 : x->input[1] = U8TO32_LITTLE(constants + 4);
116 : 6932 : x->input[2] = U8TO32_LITTLE(constants + 8);
117 : 6932 : x->input[3] = U8TO32_LITTLE(constants + 12);
118 : 6932 : }
119 : :
120 : 6932 : void ECRYPT_ivsetup(ECRYPT_ctx* x, const u8* iv)
121 : : {
122 : 6932 : x->input[12] = 0;
123 : 6932 : x->input[13] = 0;
124 : 6932 : x->input[14] = U8TO32_LITTLE(iv + 0);
125 : 6932 : x->input[15] = U8TO32_LITTLE(iv + 4);
126 : 6932 : }
127 : :
128 : 194431 : void ECRYPT_encrypt_bytes(ECRYPT_ctx* x, const u8* m, u8* c, u32 bytes)
129 : : {
130 : 194431 : u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
131 : 194431 : u32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
132 : 194431 : u8* ctarget = nullptr;
133 : 194431 : u8 tmp[64];
134 : 194431 : uint32_t i;
135 : :
136 [ + + ]: 194431 : if (!bytes) return;
137 : :
138 : 163296 : j0 = x->input[0];
139 : 163296 : j1 = x->input[1];
140 : 163296 : j2 = x->input[2];
141 : 163296 : j3 = x->input[3];
142 : 163296 : j4 = x->input[4];
143 : 163296 : j5 = x->input[5];
144 : 163296 : j6 = x->input[6];
145 : 163296 : j7 = x->input[7];
146 : 163296 : j8 = x->input[8];
147 : 163296 : j9 = x->input[9];
148 : 163296 : j10 = x->input[10];
149 : 163296 : j11 = x->input[11];
150 : 163296 : j12 = x->input[12];
151 : 163296 : j13 = x->input[13];
152 : 163296 : j14 = x->input[14];
153 : 163296 : j15 = x->input[15];
154 : :
155 : 9201698 : for (;;) {
156 [ + + ]: 4682497 : if (bytes < 64) {
157 [ + + ]: 3358201 : for (i = 0; i < bytes; ++i)
158 : 3212051 : tmp[i] = m[i];
159 : : m = tmp;
160 : : ctarget = c;
161 : : c = tmp;
162 : : }
163 : 4682497 : x0 = j0;
164 : 4682497 : x1 = j1;
165 : 4682497 : x2 = j2;
166 : 4682497 : x3 = j3;
167 : 4682497 : x4 = j4;
168 : 4682497 : x5 = j5;
169 : 4682497 : x6 = j6;
170 : 4682497 : x7 = j7;
171 : 4682497 : x8 = j8;
172 : 4682497 : x9 = j9;
173 : 4682497 : x10 = j10;
174 : 4682497 : x11 = j11;
175 : 4682497 : x12 = j12;
176 : 4682497 : x13 = j13;
177 : 4682497 : x14 = j14;
178 : 4682497 : x15 = j15;
179 [ + + ]: 51507467 : for (i = 20; i > 0; i -= 2) {
180 : 46824970 : QUARTERROUND(x0, x4, x8, x12)
181 : 46824970 : QUARTERROUND(x1, x5, x9, x13)
182 : 46824970 : QUARTERROUND(x2, x6, x10, x14)
183 : 46824970 : QUARTERROUND(x3, x7, x11, x15)
184 : 46824970 : QUARTERROUND(x0, x5, x10, x15)
185 : 46824970 : QUARTERROUND(x1, x6, x11, x12)
186 : 46824970 : QUARTERROUND(x2, x7, x8, x13)
187 : 46824970 : QUARTERROUND(x3, x4, x9, x14)
188 : : }
189 : 4682497 : x0 = PLUS(x0, j0);
190 : 4682497 : x1 = PLUS(x1, j1);
191 : 4682497 : x2 = PLUS(x2, j2);
192 : 4682497 : x3 = PLUS(x3, j3);
193 : 4682497 : x4 = PLUS(x4, j4);
194 : 4682497 : x5 = PLUS(x5, j5);
195 : 4682497 : x6 = PLUS(x6, j6);
196 : 4682497 : x7 = PLUS(x7, j7);
197 : 4682497 : x8 = PLUS(x8, j8);
198 : 4682497 : x9 = PLUS(x9, j9);
199 : 4682497 : x10 = PLUS(x10, j10);
200 : 4682497 : x11 = PLUS(x11, j11);
201 : 4682497 : x12 = PLUS(x12, j12);
202 : 4682497 : x13 = PLUS(x13, j13);
203 : 4682497 : x14 = PLUS(x14, j14);
204 : 4682497 : x15 = PLUS(x15, j15);
205 : :
206 : 4682497 : x0 = XOR(x0, U8TO32_LITTLE(m + 0));
207 : 4682497 : x1 = XOR(x1, U8TO32_LITTLE(m + 4));
208 : 4682497 : x2 = XOR(x2, U8TO32_LITTLE(m + 8));
209 : 4682497 : x3 = XOR(x3, U8TO32_LITTLE(m + 12));
210 : 4682497 : x4 = XOR(x4, U8TO32_LITTLE(m + 16));
211 : 4682497 : x5 = XOR(x5, U8TO32_LITTLE(m + 20));
212 : 4682497 : x6 = XOR(x6, U8TO32_LITTLE(m + 24));
213 : 4682497 : x7 = XOR(x7, U8TO32_LITTLE(m + 28));
214 : 4682497 : x8 = XOR(x8, U8TO32_LITTLE(m + 32));
215 : 4682497 : x9 = XOR(x9, U8TO32_LITTLE(m + 36));
216 : 4682497 : x10 = XOR(x10, U8TO32_LITTLE(m + 40));
217 : 4682497 : x11 = XOR(x11, U8TO32_LITTLE(m + 44));
218 : 4682497 : x12 = XOR(x12, U8TO32_LITTLE(m + 48));
219 : 4682497 : x13 = XOR(x13, U8TO32_LITTLE(m + 52));
220 : 4682497 : x14 = XOR(x14, U8TO32_LITTLE(m + 56));
221 : 4682497 : x15 = XOR(x15, U8TO32_LITTLE(m + 60));
222 : :
223 : 4682497 : j12 = PLUSONE(j12);
224 [ + + ]: 4682497 : if (!j12) {
225 : 3020 : j13 = PLUSONE(j13);
226 : : /* stopping at 2^70 bytes per nonce is user's responsibility */
227 : : }
228 : :
229 : 4682497 : U32TO8_LITTLE(c + 0, x0);
230 : 4682497 : U32TO8_LITTLE(c + 4, x1);
231 : 4682497 : U32TO8_LITTLE(c + 8, x2);
232 : 4682497 : U32TO8_LITTLE(c + 12, x3);
233 : 4682497 : U32TO8_LITTLE(c + 16, x4);
234 : 4682497 : U32TO8_LITTLE(c + 20, x5);
235 : 4682497 : U32TO8_LITTLE(c + 24, x6);
236 : 4682497 : U32TO8_LITTLE(c + 28, x7);
237 : 4682497 : U32TO8_LITTLE(c + 32, x8);
238 : 4682497 : U32TO8_LITTLE(c + 36, x9);
239 : 4682497 : U32TO8_LITTLE(c + 40, x10);
240 : 4682497 : U32TO8_LITTLE(c + 44, x11);
241 : 4682497 : U32TO8_LITTLE(c + 48, x12);
242 : 4682497 : U32TO8_LITTLE(c + 52, x13);
243 : 4682497 : U32TO8_LITTLE(c + 56, x14);
244 : 4682497 : U32TO8_LITTLE(c + 60, x15);
245 : :
246 [ + + ]: 4682497 : if (bytes <= 64) {
247 [ + + ]: 163296 : if (bytes < 64) {
248 [ + + ]: 3358201 : for (i = 0; i < bytes; ++i)
249 : 3212051 : ctarget[i] = c[i];
250 : : }
251 : 163296 : x->input[12] = j12;
252 : 163296 : x->input[13] = j13;
253 : 163296 : return;
254 : : }
255 : 4519201 : bytes -= 64;
256 : 4519201 : c += 64;
257 : 4519201 : m += 64;
258 : : }
259 : : }
260 : :
261 : 144336 : void ECRYPT_keystream_bytes(ECRYPT_ctx* x, u8* stream, u32 bytes)
262 : : {
263 : 144336 : u32 i;
264 [ + + ]: 150440858 : for (i = 0; i < bytes; ++i)
265 : 150296522 : stream[i] = 0;
266 : 144336 : ECRYPT_encrypt_bytes(x, stream, stream, bytes);
267 : 144336 : }
268 : :
269 [ + - ]: 935 : FUZZ_TARGET(crypto_diff_fuzz_chacha20)
270 : : {
271 : 459 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
272 : :
273 : 459 : ECRYPT_ctx ctx;
274 : :
275 : 459 : const std::vector<unsigned char> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, 32);
276 : 459 : ChaCha20 chacha20{MakeByteSpan(key)};
277 [ - + ]: 459 : ECRYPT_keysetup(&ctx, key.data(), key.size() * 8, 0);
278 : :
279 : : // ECRYPT_keysetup() doesn't set the counter and nonce to 0 while SetKey() does
280 : 459 : static const uint8_t iv[8] = {0, 0, 0, 0, 0, 0, 0, 0};
281 : 459 : ChaCha20::Nonce96 nonce{0, 0};
282 : 459 : uint32_t counter{0};
283 : 459 : ECRYPT_ivsetup(&ctx, iv);
284 : :
285 [ + + + + ]: 233500 : LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 3000) {
286 [ + - ]: 233041 : CallOneOf(
287 : : fuzzed_data_provider,
288 : 6473 : [&] {
289 : 6473 : const std::vector<unsigned char> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, 32);
290 : 6473 : chacha20.SetKey(MakeByteSpan(key));
291 [ - + ]: 6473 : nonce = {0, 0};
292 : 6473 : counter = 0;
293 [ - + ]: 6473 : ECRYPT_keysetup(&ctx, key.data(), key.size() * 8, 0);
294 : : // ECRYPT_keysetup() doesn't set the counter and nonce to 0 while SetKey() does
295 : 6473 : uint8_t iv[8] = {0, 0, 0, 0, 0, 0, 0, 0};
296 : 6473 : ECRYPT_ivsetup(&ctx, iv);
297 : 6473 : },
298 : 32137 : [&] {
299 : 32137 : uint32_t iv_prefix = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
300 : 32137 : uint64_t iv = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
301 : 32137 : nonce = {iv_prefix, iv};
302 : 32137 : counter = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
303 : 32137 : chacha20.Seek(nonce, counter);
304 : 32137 : ctx.input[12] = counter;
305 : 32137 : ctx.input[13] = iv_prefix;
306 : 32137 : ctx.input[14] = iv;
307 : 32137 : ctx.input[15] = iv >> 32;
308 : 32137 : },
309 : 144336 : [&] {
310 : 144336 : uint32_t integralInRange = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096);
311 : 144336 : std::vector<uint8_t> output(integralInRange);
312 : 144336 : chacha20.Keystream(MakeWritableByteSpan(output));
313 [ + - ]: 144336 : std::vector<uint8_t> djb_output(integralInRange);
314 [ - + ]: 144336 : ECRYPT_keystream_bytes(&ctx, djb_output.data(), djb_output.size());
315 [ - + ]: 144336 : assert(output == djb_output);
316 : : // DJB's version seeks forward to a multiple of 64 bytes after every operation. Correct for that.
317 : 144336 : uint32_t old_counter = counter;
318 : 144336 : counter += (integralInRange + 63) >> 6;
319 [ + + ]: 144336 : if (counter < old_counter) ++nonce.first;
320 [ + + ]: 144336 : if (integralInRange & 63) {
321 : 105248 : chacha20.Seek(nonce, counter);
322 : : }
323 [ - + ]: 144336 : assert(counter == ctx.input[12]);
324 : 144336 : },
325 : 50095 : [&] {
326 : 50095 : uint32_t integralInRange = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096);
327 : 50095 : std::vector<uint8_t> output(integralInRange);
328 [ - + ]: 50095 : const std::vector<uint8_t> input = ConsumeFixedLengthByteVector(fuzzed_data_provider, output.size());
329 : 50095 : chacha20.Crypt(MakeByteSpan(input), MakeWritableByteSpan(output));
330 [ + - ]: 50095 : std::vector<uint8_t> djb_output(integralInRange);
331 [ - + ]: 50095 : ECRYPT_encrypt_bytes(&ctx, input.data(), djb_output.data(), input.size());
332 [ - + ]: 50095 : assert(output == djb_output);
333 : : // DJB's version seeks forward to a multiple of 64 bytes after every operation. Correct for that.
334 : 50095 : uint32_t old_counter = counter;
335 : 50095 : counter += (integralInRange + 63) >> 6;
336 [ + + ]: 50095 : if (counter < old_counter) ++nonce.first;
337 [ + + ]: 50095 : if (integralInRange & 63) {
338 : 40902 : chacha20.Seek(nonce, counter);
339 : : }
340 [ - + ]: 50095 : assert(counter == ctx.input[12]);
341 : 50095 : });
342 : : }
343 : 459 : }
|