Skip to content

Commit 695141b

Browse files
committed
Merge #15512: Add ChaCha20 encryption option (XOR)
2dfe275 Add ChaCha20 bench (Jonas Schnelli) 2bc2b8b Add ChaCha20 encryption option (XOR) (Jonas Schnelli) Pull request description: The current ChaCha20 implementation does not support message encryption (it can only output the keystream which is sufficient for the RNG). This PR adds the actual XORing of the `plaintext` with the `keystream` in order to return the desired `ciphertext`. Required for v2 message transport protocol. ACKs for commit 2dfe27: jnewbery: Looks good. utACK 2dfe275. jnewbery: utACK 2dfe275 sipa: utACK 2dfe275 ryanofsky: utACK 2dfe275. Changes since last review are just renaming the Crypt method, adding comments, and simplifying the benchmark. Tree-SHA512: 84bb234da2ca9fdc44bc29a786d9dd215520f81245270c1aef801ef66b6091b7793e2eb38ad6dbb084925245065c5dce9e5582f2d0fa220ab3e182d43412d5b5
2 parents 79046d5 + 2dfe275 commit 695141b

File tree

7 files changed

+232
-16
lines changed

7 files changed

+232
-16
lines changed

src/Makefile.bench.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ bench_bench_bitcoin_SOURCES = \
2121
bench/duplicate_inputs.cpp \
2222
bench/examples.cpp \
2323
bench/rollingbloom.cpp \
24+
bench/chacha20.cpp \
2425
bench/crypto_hash.cpp \
2526
bench/ccoins_caching.cpp \
2627
bench/gcs_filter.cpp \

src/bench/chacha20.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2019 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 <iostream>
6+
7+
#include <bench/bench.h>
8+
#include <hash.h>
9+
#include <crypto/chacha20.h>
10+
11+
/* Number of bytes to process per iteration */
12+
static const uint64_t BUFFER_SIZE_TINY = 64;
13+
static const uint64_t BUFFER_SIZE_SMALL = 256;
14+
static const uint64_t BUFFER_SIZE_LARGE = 1024*1024;
15+
16+
static void CHACHA20(benchmark::State& state, size_t buffersize)
17+
{
18+
std::vector<uint8_t> key(32,0);
19+
ChaCha20 ctx(key.data(), key.size());
20+
ctx.SetIV(0);
21+
ctx.Seek(0);
22+
std::vector<uint8_t> in(buffersize,0);
23+
std::vector<uint8_t> out(buffersize,0);
24+
while (state.KeepRunning()) {
25+
ctx.Crypt(in.data(), out.data(), in.size());
26+
}
27+
}
28+
29+
static void CHACHA20_64BYTES(benchmark::State& state)
30+
{
31+
CHACHA20(state, BUFFER_SIZE_TINY);
32+
}
33+
34+
static void CHACHA20_256BYTES(benchmark::State& state)
35+
{
36+
CHACHA20(state, BUFFER_SIZE_SMALL);
37+
}
38+
39+
static void CHACHA20_1MB(benchmark::State& state)
40+
{
41+
CHACHA20(state, BUFFER_SIZE_LARGE);
42+
}
43+
44+
BENCHMARK(CHACHA20_64BYTES, 500000);
45+
BENCHMARK(CHACHA20_256BYTES, 250000);
46+
BENCHMARK(CHACHA20_1MB, 340);

src/crypto/chacha20.cpp

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void ChaCha20::Seek(uint64_t pos)
7171
input[13] = pos >> 32;
7272
}
7373

74-
void ChaCha20::Output(unsigned char* c, size_t bytes)
74+
void ChaCha20::Keystream(unsigned char* c, size_t bytes)
7575
{
7676
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
7777
uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
@@ -178,3 +178,133 @@ void ChaCha20::Output(unsigned char* c, size_t bytes)
178178
c += 64;
179179
}
180180
}
181+
182+
void ChaCha20::Crypt(const unsigned char* m, unsigned char* c, size_t bytes)
183+
{
184+
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
185+
uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
186+
unsigned char *ctarget = nullptr;
187+
unsigned char tmp[64];
188+
unsigned int i;
189+
190+
if (!bytes) return;
191+
192+
j0 = input[0];
193+
j1 = input[1];
194+
j2 = input[2];
195+
j3 = input[3];
196+
j4 = input[4];
197+
j5 = input[5];
198+
j6 = input[6];
199+
j7 = input[7];
200+
j8 = input[8];
201+
j9 = input[9];
202+
j10 = input[10];
203+
j11 = input[11];
204+
j12 = input[12];
205+
j13 = input[13];
206+
j14 = input[14];
207+
j15 = input[15];
208+
209+
for (;;) {
210+
if (bytes < 64) {
211+
// if m has fewer than 64 bytes available, copy m to tmp and
212+
// read from tmp instead
213+
for (i = 0;i < bytes;++i) tmp[i] = m[i];
214+
m = tmp;
215+
ctarget = c;
216+
c = tmp;
217+
}
218+
x0 = j0;
219+
x1 = j1;
220+
x2 = j2;
221+
x3 = j3;
222+
x4 = j4;
223+
x5 = j5;
224+
x6 = j6;
225+
x7 = j7;
226+
x8 = j8;
227+
x9 = j9;
228+
x10 = j10;
229+
x11 = j11;
230+
x12 = j12;
231+
x13 = j13;
232+
x14 = j14;
233+
x15 = j15;
234+
for (i = 20;i > 0;i -= 2) {
235+
QUARTERROUND( x0, x4, x8,x12)
236+
QUARTERROUND( x1, x5, x9,x13)
237+
QUARTERROUND( x2, x6,x10,x14)
238+
QUARTERROUND( x3, x7,x11,x15)
239+
QUARTERROUND( x0, x5,x10,x15)
240+
QUARTERROUND( x1, x6,x11,x12)
241+
QUARTERROUND( x2, x7, x8,x13)
242+
QUARTERROUND( x3, x4, x9,x14)
243+
}
244+
x0 += j0;
245+
x1 += j1;
246+
x2 += j2;
247+
x3 += j3;
248+
x4 += j4;
249+
x5 += j5;
250+
x6 += j6;
251+
x7 += j7;
252+
x8 += j8;
253+
x9 += j9;
254+
x10 += j10;
255+
x11 += j11;
256+
x12 += j12;
257+
x13 += j13;
258+
x14 += j14;
259+
x15 += j15;
260+
261+
x0 ^= ReadLE32(m + 0);
262+
x1 ^= ReadLE32(m + 4);
263+
x2 ^= ReadLE32(m + 8);
264+
x3 ^= ReadLE32(m + 12);
265+
x4 ^= ReadLE32(m + 16);
266+
x5 ^= ReadLE32(m + 20);
267+
x6 ^= ReadLE32(m + 24);
268+
x7 ^= ReadLE32(m + 28);
269+
x8 ^= ReadLE32(m + 32);
270+
x9 ^= ReadLE32(m + 36);
271+
x10 ^= ReadLE32(m + 40);
272+
x11 ^= ReadLE32(m + 44);
273+
x12 ^= ReadLE32(m + 48);
274+
x13 ^= ReadLE32(m + 52);
275+
x14 ^= ReadLE32(m + 56);
276+
x15 ^= ReadLE32(m + 60);
277+
278+
++j12;
279+
if (!j12) ++j13;
280+
281+
WriteLE32(c + 0, x0);
282+
WriteLE32(c + 4, x1);
283+
WriteLE32(c + 8, x2);
284+
WriteLE32(c + 12, x3);
285+
WriteLE32(c + 16, x4);
286+
WriteLE32(c + 20, x5);
287+
WriteLE32(c + 24, x6);
288+
WriteLE32(c + 28, x7);
289+
WriteLE32(c + 32, x8);
290+
WriteLE32(c + 36, x9);
291+
WriteLE32(c + 40, x10);
292+
WriteLE32(c + 44, x11);
293+
WriteLE32(c + 48, x12);
294+
WriteLE32(c + 52, x13);
295+
WriteLE32(c + 56, x14);
296+
WriteLE32(c + 60, x15);
297+
298+
if (bytes <= 64) {
299+
if (bytes < 64) {
300+
for (i = 0;i < bytes;++i) ctarget[i] = c[i];
301+
}
302+
input[12] = j12;
303+
input[13] = j13;
304+
return;
305+
}
306+
bytes -= 64;
307+
c += 64;
308+
m += 64;
309+
}
310+
}

src/crypto/chacha20.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#include <stdint.h>
99
#include <stdlib.h>
1010

11-
/** A PRNG class for ChaCha20. */
11+
/** A class for ChaCha20 256-bit stream cipher developed by Daniel J. Bernstein
12+
https://cr.yp.to/chacha/chacha-20080128.pdf */
1213
class ChaCha20
1314
{
1415
private:
@@ -17,10 +18,17 @@ class ChaCha20
1718
public:
1819
ChaCha20();
1920
ChaCha20(const unsigned char* key, size_t keylen);
20-
void SetKey(const unsigned char* key, size_t keylen);
21-
void SetIV(uint64_t iv);
22-
void Seek(uint64_t pos);
23-
void Output(unsigned char* output, size_t bytes);
21+
void SetKey(const unsigned char* key, size_t keylen); //!< set key with flexible keylength; 256bit recommended */
22+
void SetIV(uint64_t iv); // set the 64bit nonce
23+
void Seek(uint64_t pos); // set the 64bit block counter
24+
25+
/** outputs the keystream of size <bytes> into <c> */
26+
void Keystream(unsigned char* c, size_t bytes);
27+
28+
/** enciphers the message <input> of length <bytes> and write the enciphered representation into <output>
29+
* Used for encryption and decryption (XOR)
30+
*/
31+
void Crypt(const unsigned char* input, unsigned char* output, size_t bytes);
2432
};
2533

2634
#endif // BITCOIN_CRYPTO_CHACHA20_H

src/random.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ std::vector<unsigned char> FastRandomContext::randbytes(size_t len)
652652
if (requires_seed) RandomSeed();
653653
std::vector<unsigned char> ret(len);
654654
if (len > 0) {
655-
rng.Output(&ret[0], len);
655+
rng.Keystream(&ret[0], len);
656656
}
657657
return ret;
658658
}

src/random.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class FastRandomContext {
111111
if (requires_seed) {
112112
RandomSeed();
113113
}
114-
rng.Output(bytebuf, sizeof(bytebuf));
114+
rng.Keystream(bytebuf, sizeof(bytebuf));
115115
bytebuf_size = sizeof(bytebuf);
116116
}
117117

src/test/crypto_tests.cpp

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,36 @@ static void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, b
125125
}
126126
}
127127

128-
static void TestChaCha20(const std::string &hexkey, uint64_t nonce, uint64_t seek, const std::string& hexout)
128+
static void TestChaCha20(const std::string &hex_message, const std::string &hexkey, uint64_t nonce, uint64_t seek, const std::string& hexout)
129129
{
130130
std::vector<unsigned char> key = ParseHex(hexkey);
131+
std::vector<unsigned char> m = ParseHex(hex_message);
131132
ChaCha20 rng(key.data(), key.size());
132133
rng.SetIV(nonce);
133134
rng.Seek(seek);
134135
std::vector<unsigned char> out = ParseHex(hexout);
135136
std::vector<unsigned char> outres;
136137
outres.resize(out.size());
137-
rng.Output(outres.data(), outres.size());
138+
assert(hex_message.empty() || m.size() == out.size());
139+
140+
// perform the ChaCha20 round(s), if message is provided it will output the encrypted ciphertext otherwise the keystream
141+
if (!hex_message.empty()) {
142+
rng.Crypt(m.data(), outres.data(), outres.size());
143+
} else {
144+
rng.Keystream(outres.data(), outres.size());
145+
}
138146
BOOST_CHECK(out == outres);
147+
if (!hex_message.empty()) {
148+
// Manually XOR with the keystream and compare the output
149+
rng.SetIV(nonce);
150+
rng.Seek(seek);
151+
std::vector<unsigned char> only_keystream(outres.size());
152+
rng.Keystream(only_keystream.data(), only_keystream.size());
153+
for (size_t i = 0; i != m.size(); i++) {
154+
outres[i] = m[i] ^ only_keystream[i];
155+
}
156+
BOOST_CHECK(out == outres);
157+
}
139158
}
140159

141160
static void TestPoly1305(const std::string &hexmessage, const std::string &hexkey, const std::string& hextag)
@@ -420,25 +439,37 @@ BOOST_AUTO_TEST_CASE(aes_cbc_testvectors) {
420439
BOOST_AUTO_TEST_CASE(chacha20_testvector)
421440
{
422441
// Test vector from RFC 7539
423-
TestChaCha20("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 0x4a000000UL, 1,
442+
443+
// test encryption
444+
TestChaCha20("4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756"
445+
"c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e"
446+
"20776f756c642062652069742e",
447+
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 0x4a000000UL, 1,
448+
"6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f593dabcd62b3571639d"
449+
"624e65152ab8f530c359f0861d807ca0dbf500d6a6156a38e088a22b65e52bc514d16ccf806818ce91ab77937365af90bbf74"
450+
"a35be6b40b8eedf2785e42874d"
451+
);
452+
453+
// test keystream output
454+
TestChaCha20("", "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 0x4a000000UL, 1,
424455
"224f51f3401bd9e12fde276fb8631ded8c131f823d2c06e27e4fcaec9ef3cf788a3b0aa372600a92b57974cded2b9334794cb"
425456
"a40c63e34cdea212c4cf07d41b769a6749f3f630f4122cafe28ec4dc47e26d4346d70b98c73f3e9c53ac40c5945398b6eda1a"
426457
"832c89c167eacd901d7e2bf363");
427458

428459
// Test vectors from https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7
429-
TestChaCha20("0000000000000000000000000000000000000000000000000000000000000000", 0, 0,
460+
TestChaCha20("", "0000000000000000000000000000000000000000000000000000000000000000", 0, 0,
430461
"76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b"
431462
"8f41518a11cc387b669b2ee6586");
432-
TestChaCha20("0000000000000000000000000000000000000000000000000000000000000001", 0, 0,
463+
TestChaCha20("", "0000000000000000000000000000000000000000000000000000000000000001", 0, 0,
433464
"4540f05a9f1fb296d7736e7b208e3c96eb4fe1834688d2604f450952ed432d41bbe2a0b6ea7566d2a5d1e7e20d42af2c53d79"
434465
"2b1c43fea817e9ad275ae546963");
435-
TestChaCha20("0000000000000000000000000000000000000000000000000000000000000000", 0x0100000000000000ULL, 0,
466+
TestChaCha20("", "0000000000000000000000000000000000000000000000000000000000000000", 0x0100000000000000ULL, 0,
436467
"de9cba7bf3d69ef5e786dc63973f653a0b49e015adbff7134fcb7df137821031e85a050278a7084527214f73efc7fa5b52770"
437468
"62eb7a0433e445f41e3");
438-
TestChaCha20("0000000000000000000000000000000000000000000000000000000000000000", 1, 0,
469+
TestChaCha20("", "0000000000000000000000000000000000000000000000000000000000000000", 1, 0,
439470
"ef3fdfd6c61578fbf5cf35bd3dd33b8009631634d21e42ac33960bd138e50d32111e4caf237ee53ca8ad6426194a88545ddc4"
440471
"97a0b466e7d6bbdb0041b2f586b");
441-
TestChaCha20("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 0x0706050403020100ULL, 0,
472+
TestChaCha20("", "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 0x0706050403020100ULL, 0,
442473
"f798a189f195e66982105ffb640bb7757f579da31602fc93ec01ac56f85ac3c134a4547b733b46413042c9440049176905d3b"
443474
"e59ea1c53f15916155c2be8241a38008b9a26bc35941e2444177c8ade6689de95264986d95889fb60e84629c9bd9a5acb1cc1"
444475
"18be563eb9b3a4a472f82e09a7e778492b562ef7130e88dfe031c79db9d4f7c7a899151b9a475032b63fc385245fe054e3dd5"

0 commit comments

Comments
 (0)