Skip to content

Commit e04326f

Browse files
committed
Add ChaCha20
1 parent 663fbae commit e04326f

File tree

4 files changed

+253
-0
lines changed

4 files changed

+253
-0
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ crypto_libbitcoin_crypto_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
242242
crypto_libbitcoin_crypto_a_SOURCES = \
243243
crypto/aes.cpp \
244244
crypto/aes.h \
245+
crypto/chacha20.h \
246+
crypto/chacha20.cpp \
245247
crypto/common.h \
246248
crypto/hmac_sha256.cpp \
247249
crypto/hmac_sha256.h \

src/crypto/chacha20.cpp

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
// Copyright (c) 2017 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+
11+
#include <string.h>
12+
13+
constexpr static inline uint32_t rotl32(uint32_t v, int c) { return (v << c) | (v >> (32 - c)); }
14+
15+
#define QUARTERROUND(a,b,c,d) \
16+
a += b; d = rotl32(d ^ a, 16); \
17+
c += d; b = rotl32(b ^ c, 12); \
18+
a += b; d = rotl32(d ^ a, 8); \
19+
c += d; b = rotl32(b ^ c, 7);
20+
21+
static const unsigned char sigma[] = "expand 32-byte k";
22+
static const unsigned char tau[] = "expand 16-byte k";
23+
24+
void ChaCha20::SetKey(const unsigned char* k, size_t keylen)
25+
{
26+
const unsigned char *constants;
27+
28+
input[4] = ReadLE32(k + 0);
29+
input[5] = ReadLE32(k + 4);
30+
input[6] = ReadLE32(k + 8);
31+
input[7] = ReadLE32(k + 12);
32+
if (keylen == 32) { /* recommended */
33+
k += 16;
34+
constants = sigma;
35+
} else { /* keylen == 16 */
36+
constants = tau;
37+
}
38+
input[8] = ReadLE32(k + 0);
39+
input[9] = ReadLE32(k + 4);
40+
input[10] = ReadLE32(k + 8);
41+
input[11] = ReadLE32(k + 12);
42+
input[0] = ReadLE32(constants + 0);
43+
input[1] = ReadLE32(constants + 4);
44+
input[2] = ReadLE32(constants + 8);
45+
input[3] = ReadLE32(constants + 12);
46+
input[12] = 0;
47+
input[13] = 0;
48+
input[14] = 0;
49+
input[15] = 0;
50+
}
51+
52+
ChaCha20::ChaCha20()
53+
{
54+
memset(input, 0, sizeof(input));
55+
}
56+
57+
ChaCha20::ChaCha20(const unsigned char* k, size_t keylen)
58+
{
59+
SetKey(k, keylen);
60+
}
61+
62+
void ChaCha20::SetIV(uint64_t iv)
63+
{
64+
input[14] = iv;
65+
input[15] = iv >> 32;
66+
}
67+
68+
void ChaCha20::Seek(uint64_t pos)
69+
{
70+
input[12] = pos;
71+
input[13] = pos >> 32;
72+
}
73+
74+
void ChaCha20::Output(unsigned char* c, size_t bytes)
75+
{
76+
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
77+
uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
78+
unsigned char *ctarget = NULL;
79+
unsigned char tmp[64];
80+
unsigned int i;
81+
82+
if (!bytes) return;
83+
84+
j0 = input[0];
85+
j1 = input[1];
86+
j2 = input[2];
87+
j3 = input[3];
88+
j4 = input[4];
89+
j5 = input[5];
90+
j6 = input[6];
91+
j7 = input[7];
92+
j8 = input[8];
93+
j9 = input[9];
94+
j10 = input[10];
95+
j11 = input[11];
96+
j12 = input[12];
97+
j13 = input[13];
98+
j14 = input[14];
99+
j15 = input[15];
100+
101+
for (;;) {
102+
if (bytes < 64) {
103+
ctarget = c;
104+
c = tmp;
105+
}
106+
x0 = j0;
107+
x1 = j1;
108+
x2 = j2;
109+
x3 = j3;
110+
x4 = j4;
111+
x5 = j5;
112+
x6 = j6;
113+
x7 = j7;
114+
x8 = j8;
115+
x9 = j9;
116+
x10 = j10;
117+
x11 = j11;
118+
x12 = j12;
119+
x13 = j13;
120+
x14 = j14;
121+
x15 = j15;
122+
for (i = 20;i > 0;i -= 2) {
123+
QUARTERROUND( x0, x4, x8,x12)
124+
QUARTERROUND( x1, x5, x9,x13)
125+
QUARTERROUND( x2, x6,x10,x14)
126+
QUARTERROUND( x3, x7,x11,x15)
127+
QUARTERROUND( x0, x5,x10,x15)
128+
QUARTERROUND( x1, x6,x11,x12)
129+
QUARTERROUND( x2, x7, x8,x13)
130+
QUARTERROUND( x3, x4, x9,x14)
131+
}
132+
x0 += j0;
133+
x1 += j1;
134+
x2 += j2;
135+
x3 += j3;
136+
x4 += j4;
137+
x5 += j5;
138+
x6 += j6;
139+
x7 += j7;
140+
x8 += j8;
141+
x9 += j9;
142+
x10 += j10;
143+
x11 += j11;
144+
x12 += j12;
145+
x13 += j13;
146+
x14 += j14;
147+
x15 += j15;
148+
149+
++j12;
150+
if (!j12) ++j13;
151+
152+
WriteLE32(c + 0, x0);
153+
WriteLE32(c + 4, x1);
154+
WriteLE32(c + 8, x2);
155+
WriteLE32(c + 12, x3);
156+
WriteLE32(c + 16, x4);
157+
WriteLE32(c + 20, x5);
158+
WriteLE32(c + 24, x6);
159+
WriteLE32(c + 28, x7);
160+
WriteLE32(c + 32, x8);
161+
WriteLE32(c + 36, x9);
162+
WriteLE32(c + 40, x10);
163+
WriteLE32(c + 44, x11);
164+
WriteLE32(c + 48, x12);
165+
WriteLE32(c + 52, x13);
166+
WriteLE32(c + 56, x14);
167+
WriteLE32(c + 60, x15);
168+
169+
if (bytes <= 64) {
170+
if (bytes < 64) {
171+
for (i = 0;i < bytes;++i) ctarget[i] = c[i];
172+
}
173+
input[12] = j12;
174+
input[13] = j13;
175+
return;
176+
}
177+
bytes -= 64;
178+
c += 64;
179+
}
180+
}

src/crypto/chacha20.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2017 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+
#ifndef BITCOIN_CRYPTO_CHACHA20_H
6+
#define BITCOIN_CRYPTO_CHACHA20_H
7+
8+
#include <stdint.h>
9+
#include <stdlib.h>
10+
11+
/** A PRNG class for ChaCha20. */
12+
class ChaCha20
13+
{
14+
private:
15+
uint32_t input[16];
16+
17+
public:
18+
ChaCha20();
19+
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);
24+
};
25+
26+
#endif // BITCOIN_CRYPTO_CHACHA20_H

src/test/crypto_tests.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include "crypto/aes.h"
6+
#include "crypto/chacha20.h"
67
#include "crypto/ripemd160.h"
78
#include "crypto/sha1.h"
89
#include "crypto/sha256.h"
@@ -187,6 +188,19 @@ void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, bool pad
187188
}
188189
}
189190

191+
void TestChaCha20(const std::string &hexkey, uint64_t nonce, uint64_t seek, const std::string& hexout)
192+
{
193+
std::vector<unsigned char> key = ParseHex(hexkey);
194+
ChaCha20 rng(key.data(), key.size());
195+
rng.SetIV(nonce);
196+
rng.Seek(seek);
197+
std::vector<unsigned char> out = ParseHex(hexout);
198+
std::vector<unsigned char> outres;
199+
outres.resize(out.size());
200+
rng.Output(outres.data(), outres.size());
201+
BOOST_CHECK(out == outres);
202+
}
203+
190204
std::string LongTestString(void) {
191205
std::string ret;
192206
for (int i=0; i<200000; i++) {
@@ -439,4 +453,35 @@ BOOST_AUTO_TEST_CASE(aes_cbc_testvectors) {
439453
"b2eb05e2c39be9fcda6c19078c6a9d1b3f461796d6b0d6b2e0c2a72b4d80e644");
440454
}
441455

456+
457+
BOOST_AUTO_TEST_CASE(chacha20_testvector)
458+
{
459+
// Test vector from RFC 7539
460+
TestChaCha20("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 0x4a000000UL, 1,
461+
"224f51f3401bd9e12fde276fb8631ded8c131f823d2c06e27e4fcaec9ef3cf788a3b0aa372600a92b57974cded2b9334794cb"
462+
"a40c63e34cdea212c4cf07d41b769a6749f3f630f4122cafe28ec4dc47e26d4346d70b98c73f3e9c53ac40c5945398b6eda1a"
463+
"832c89c167eacd901d7e2bf363");
464+
465+
// Test vectors from https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7
466+
TestChaCha20("0000000000000000000000000000000000000000000000000000000000000000", 0, 0,
467+
"76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b"
468+
"8f41518a11cc387b669b2ee6586");
469+
TestChaCha20("0000000000000000000000000000000000000000000000000000000000000001", 0, 0,
470+
"4540f05a9f1fb296d7736e7b208e3c96eb4fe1834688d2604f450952ed432d41bbe2a0b6ea7566d2a5d1e7e20d42af2c53d79"
471+
"2b1c43fea817e9ad275ae546963");
472+
TestChaCha20("0000000000000000000000000000000000000000000000000000000000000000", 0x0100000000000000ULL, 0,
473+
"de9cba7bf3d69ef5e786dc63973f653a0b49e015adbff7134fcb7df137821031e85a050278a7084527214f73efc7fa5b52770"
474+
"62eb7a0433e445f41e3");
475+
TestChaCha20("0000000000000000000000000000000000000000000000000000000000000000", 1, 0,
476+
"ef3fdfd6c61578fbf5cf35bd3dd33b8009631634d21e42ac33960bd138e50d32111e4caf237ee53ca8ad6426194a88545ddc4"
477+
"97a0b466e7d6bbdb0041b2f586b");
478+
TestChaCha20("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 0x0706050403020100ULL, 0,
479+
"f798a189f195e66982105ffb640bb7757f579da31602fc93ec01ac56f85ac3c134a4547b733b46413042c9440049176905d3b"
480+
"e59ea1c53f15916155c2be8241a38008b9a26bc35941e2444177c8ade6689de95264986d95889fb60e84629c9bd9a5acb1cc1"
481+
"18be563eb9b3a4a472f82e09a7e778492b562ef7130e88dfe031c79db9d4f7c7a899151b9a475032b63fc385245fe054e3dd5"
482+
"a97a5f576fe064025d3ce042c566ab2c507b138db853e3d6959660996546cc9c4a6eafdc777c040d70eaf46f76dad3979e5c5"
483+
"360c3317166a1c894c94a371876a94df7628fe4eaaf2ccb27d5aaae0ad7ad0f9d4b6ad3b54098746d4524d38407a6deb3ab78"
484+
"fab78c9");
485+
}
486+
442487
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)