1 // Copyright (c) 2014-2018 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/aes.h>
6 #include <crypto/chacha20.h>
7 #include <crypto/ripemd160.h>
8 #include <crypto/sha1.h>
9 #include <crypto/sha256.h>
10 #include <crypto/sha512.h>
11 #include <crypto/hmac_sha256.h>
12 #include <crypto/hmac_sha512.h>
13 #include <random.h>
14 #include <util/strencodings.h>
15 #include <test/test_bitcoin.h>
16 
17 #include <vector>
18 
19 #include <boost/test/unit_test.hpp>
20 #include <openssl/aes.h>
21 #include <openssl/evp.h>
22 
BOOST_FIXTURE_TEST_SUITE(crypto_tests,BasicTestingSetup)23 BOOST_FIXTURE_TEST_SUITE(crypto_tests, BasicTestingSetup)
24 
25 template<typename Hasher, typename In, typename Out>
26 static void TestVector(const Hasher &h, const In &in, const Out &out) {
27     Out hash;
28     BOOST_CHECK(out.size() == h.OUTPUT_SIZE);
29     hash.resize(out.size());
30     {
31         // Test that writing the whole input string at once works.
32         Hasher(h).Write((unsigned char*)&in[0], in.size()).Finalize(&hash[0]);
33         BOOST_CHECK(hash == out);
34     }
35     for (int i=0; i<32; i++) {
36         // Test that writing the string broken up in random pieces works.
37         Hasher hasher(h);
38         size_t pos = 0;
39         while (pos < in.size()) {
40             size_t len = InsecureRandRange((in.size() - pos + 1) / 2 + 1);
41             hasher.Write((unsigned char*)&in[pos], len);
42             pos += len;
43             if (pos > 0 && pos + 2 * out.size() > in.size() && pos < in.size()) {
44                 // Test that writing the rest at once to a copy of a hasher works.
45                 Hasher(hasher).Write((unsigned char*)&in[pos], in.size() - pos).Finalize(&hash[0]);
46                 BOOST_CHECK(hash == out);
47             }
48         }
49         hasher.Finalize(&hash[0]);
50         BOOST_CHECK(hash == out);
51     }
52 }
53 
TestSHA1(const std::string & in,const std::string & hexout)54 static void TestSHA1(const std::string &in, const std::string &hexout) { TestVector(CSHA1(), in, ParseHex(hexout));}
TestSHA256(const std::string & in,const std::string & hexout)55 static void TestSHA256(const std::string &in, const std::string &hexout) { TestVector(CSHA256(), in, ParseHex(hexout));}
TestSHA512(const std::string & in,const std::string & hexout)56 static void TestSHA512(const std::string &in, const std::string &hexout) { TestVector(CSHA512(), in, ParseHex(hexout));}
TestRIPEMD160(const std::string & in,const std::string & hexout)57 static void TestRIPEMD160(const std::string &in, const std::string &hexout) { TestVector(CRIPEMD160(), in, ParseHex(hexout));}
58 
TestHMACSHA256(const std::string & hexkey,const std::string & hexin,const std::string & hexout)59 static void TestHMACSHA256(const std::string &hexkey, const std::string &hexin, const std::string &hexout) {
60     std::vector<unsigned char> key = ParseHex(hexkey);
61     TestVector(CHMAC_SHA256(key.data(), key.size()), ParseHex(hexin), ParseHex(hexout));
62 }
63 
TestHMACSHA512(const std::string & hexkey,const std::string & hexin,const std::string & hexout)64 static void TestHMACSHA512(const std::string &hexkey, const std::string &hexin, const std::string &hexout) {
65     std::vector<unsigned char> key = ParseHex(hexkey);
66     TestVector(CHMAC_SHA512(key.data(), key.size()), ParseHex(hexin), ParseHex(hexout));
67 }
68 
TestAES128(const std::string & hexkey,const std::string & hexin,const std::string & hexout)69 static void TestAES128(const std::string &hexkey, const std::string &hexin, const std::string &hexout)
70 {
71     std::vector<unsigned char> key = ParseHex(hexkey);
72     std::vector<unsigned char> in = ParseHex(hexin);
73     std::vector<unsigned char> correctout = ParseHex(hexout);
74     std::vector<unsigned char> buf, buf2;
75 
76     assert(key.size() == 16);
77     assert(in.size() == 16);
78     assert(correctout.size() == 16);
79     AES128Encrypt enc(key.data());
80     buf.resize(correctout.size());
81     buf2.resize(correctout.size());
82     enc.Encrypt(buf.data(), in.data());
83     BOOST_CHECK_EQUAL(HexStr(buf), HexStr(correctout));
84     AES128Decrypt dec(key.data());
85     dec.Decrypt(buf2.data(), buf.data());
86     BOOST_CHECK_EQUAL(HexStr(buf2), HexStr(in));
87 }
88 
TestAES256(const std::string & hexkey,const std::string & hexin,const std::string & hexout)89 static void TestAES256(const std::string &hexkey, const std::string &hexin, const std::string &hexout)
90 {
91     std::vector<unsigned char> key = ParseHex(hexkey);
92     std::vector<unsigned char> in = ParseHex(hexin);
93     std::vector<unsigned char> correctout = ParseHex(hexout);
94     std::vector<unsigned char> buf;
95 
96     assert(key.size() == 32);
97     assert(in.size() == 16);
98     assert(correctout.size() == 16);
99     AES256Encrypt enc(key.data());
100     buf.resize(correctout.size());
101     enc.Encrypt(buf.data(), in.data());
102     BOOST_CHECK(buf == correctout);
103     AES256Decrypt dec(key.data());
104     dec.Decrypt(buf.data(), buf.data());
105     BOOST_CHECK(buf == in);
106 }
107 
TestAES128CBC(const std::string & hexkey,const std::string & hexiv,bool pad,const std::string & hexin,const std::string & hexout)108 static void TestAES128CBC(const std::string &hexkey, const std::string &hexiv, bool pad, const std::string &hexin, const std::string &hexout)
109 {
110     std::vector<unsigned char> key = ParseHex(hexkey);
111     std::vector<unsigned char> iv = ParseHex(hexiv);
112     std::vector<unsigned char> in = ParseHex(hexin);
113     std::vector<unsigned char> correctout = ParseHex(hexout);
114     std::vector<unsigned char> realout(in.size() + AES_BLOCKSIZE);
115 
116     // Encrypt the plaintext and verify that it equals the cipher
117     AES128CBCEncrypt enc(key.data(), iv.data(), pad);
118     int size = enc.Encrypt(in.data(), in.size(), realout.data());
119     realout.resize(size);
120     BOOST_CHECK(realout.size() == correctout.size());
121     BOOST_CHECK_MESSAGE(realout == correctout, HexStr(realout) + std::string(" != ") + hexout);
122 
123     // Decrypt the cipher and verify that it equals the plaintext
124     std::vector<unsigned char> decrypted(correctout.size());
125     AES128CBCDecrypt dec(key.data(), iv.data(), pad);
126     size = dec.Decrypt(correctout.data(), correctout.size(), decrypted.data());
127     decrypted.resize(size);
128     BOOST_CHECK(decrypted.size() == in.size());
129     BOOST_CHECK_MESSAGE(decrypted == in, HexStr(decrypted) + std::string(" != ") + hexin);
130 
131     // Encrypt and re-decrypt substrings of the plaintext and verify that they equal each-other
132     for(std::vector<unsigned char>::iterator i(in.begin()); i != in.end(); ++i)
133     {
134         std::vector<unsigned char> sub(i, in.end());
135         std::vector<unsigned char> subout(sub.size() + AES_BLOCKSIZE);
136         int _size = enc.Encrypt(sub.data(), sub.size(), subout.data());
137         if (_size != 0)
138         {
139             subout.resize(_size);
140             std::vector<unsigned char> subdecrypted(subout.size());
141             _size = dec.Decrypt(subout.data(), subout.size(), subdecrypted.data());
142             subdecrypted.resize(_size);
143             BOOST_CHECK(decrypted.size() == in.size());
144             BOOST_CHECK_MESSAGE(subdecrypted == sub, HexStr(subdecrypted) + std::string(" != ") + HexStr(sub));
145         }
146     }
147 }
148 
TestAES256CBC(const std::string & hexkey,const std::string & hexiv,bool pad,const std::string & hexin,const std::string & hexout)149 static void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, bool pad, const std::string &hexin, const std::string &hexout)
150 {
151     std::vector<unsigned char> key = ParseHex(hexkey);
152     std::vector<unsigned char> iv = ParseHex(hexiv);
153     std::vector<unsigned char> in = ParseHex(hexin);
154     std::vector<unsigned char> correctout = ParseHex(hexout);
155     std::vector<unsigned char> realout(in.size() + AES_BLOCKSIZE);
156 
157     // Encrypt the plaintext and verify that it equals the cipher
158     AES256CBCEncrypt enc(key.data(), iv.data(), pad);
159     int size = enc.Encrypt(in.data(), in.size(), realout.data());
160     realout.resize(size);
161     BOOST_CHECK(realout.size() == correctout.size());
162     BOOST_CHECK_MESSAGE(realout == correctout, HexStr(realout) + std::string(" != ") + hexout);
163 
164     // Decrypt the cipher and verify that it equals the plaintext
165     std::vector<unsigned char> decrypted(correctout.size());
166     AES256CBCDecrypt dec(key.data(), iv.data(), pad);
167     size = dec.Decrypt(correctout.data(), correctout.size(), decrypted.data());
168     decrypted.resize(size);
169     BOOST_CHECK(decrypted.size() == in.size());
170     BOOST_CHECK_MESSAGE(decrypted == in, HexStr(decrypted) + std::string(" != ") + hexin);
171 
172     // Encrypt and re-decrypt substrings of the plaintext and verify that they equal each-other
173     for(std::vector<unsigned char>::iterator i(in.begin()); i != in.end(); ++i)
174     {
175         std::vector<unsigned char> sub(i, in.end());
176         std::vector<unsigned char> subout(sub.size() + AES_BLOCKSIZE);
177         int _size = enc.Encrypt(sub.data(), sub.size(), subout.data());
178         if (_size != 0)
179         {
180             subout.resize(_size);
181             std::vector<unsigned char> subdecrypted(subout.size());
182             _size = dec.Decrypt(subout.data(), subout.size(), subdecrypted.data());
183             subdecrypted.resize(_size);
184             BOOST_CHECK(decrypted.size() == in.size());
185             BOOST_CHECK_MESSAGE(subdecrypted == sub, HexStr(subdecrypted) + std::string(" != ") + HexStr(sub));
186         }
187     }
188 }
189 
TestChaCha20(const std::string & hexkey,uint64_t nonce,uint64_t seek,const std::string & hexout)190 static void TestChaCha20(const std::string &hexkey, uint64_t nonce, uint64_t seek, const std::string& hexout)
191 {
192     std::vector<unsigned char> key = ParseHex(hexkey);
193     ChaCha20 rng(key.data(), key.size());
194     rng.SetIV(nonce);
195     rng.Seek(seek);
196     std::vector<unsigned char> out = ParseHex(hexout);
197     std::vector<unsigned char> outres;
198     outres.resize(out.size());
199     rng.Output(outres.data(), outres.size());
200     BOOST_CHECK(out == outres);
201 }
202 
LongTestString()203 static std::string LongTestString() {
204     std::string ret;
205     for (int i=0; i<200000; i++) {
206         ret += (unsigned char)(i);
207         ret += (unsigned char)(i >> 4);
208         ret += (unsigned char)(i >> 8);
209         ret += (unsigned char)(i >> 12);
210         ret += (unsigned char)(i >> 16);
211     }
212     return ret;
213 }
214 
215 const std::string test1 = LongTestString();
216 
BOOST_AUTO_TEST_CASE(ripemd160_testvectors)217 BOOST_AUTO_TEST_CASE(ripemd160_testvectors) {
218     TestRIPEMD160("", "9c1185a5c5e9fc54612808977ee8f548b2258d31");
219     TestRIPEMD160("abc", "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc");
220     TestRIPEMD160("message digest", "5d0689ef49d2fae572b881b123a85ffa21595f36");
221     TestRIPEMD160("secure hash algorithm", "20397528223b6a5f4cbc2808aba0464e645544f9");
222     TestRIPEMD160("RIPEMD160 is considered to be safe", "a7d78608c7af8a8e728778e81576870734122b66");
223     TestRIPEMD160("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
224                   "12a053384a9c0c88e405a06c27dcf49ada62eb2b");
225     TestRIPEMD160("For this sample, this 63-byte string will be used as input data",
226                   "de90dbfee14b63fb5abf27c2ad4a82aaa5f27a11");
227     TestRIPEMD160("This is exactly 64 bytes long, not counting the terminating byte",
228                   "eda31d51d3a623b81e19eb02e24ff65d27d67b37");
229     TestRIPEMD160(std::string(1000000, 'a'), "52783243c1697bdbe16d37f97f68f08325dc1528");
230     TestRIPEMD160(test1, "464243587bd146ea835cdf57bdae582f25ec45f1");
231 }
232 
BOOST_AUTO_TEST_CASE(sha1_testvectors)233 BOOST_AUTO_TEST_CASE(sha1_testvectors) {
234     TestSHA1("", "da39a3ee5e6b4b0d3255bfef95601890afd80709");
235     TestSHA1("abc", "a9993e364706816aba3e25717850c26c9cd0d89d");
236     TestSHA1("message digest", "c12252ceda8be8994d5fa0290a47231c1d16aae3");
237     TestSHA1("secure hash algorithm", "d4d6d2f0ebe317513bbd8d967d89bac5819c2f60");
238     TestSHA1("SHA1 is considered to be safe", "f2b6650569ad3a8720348dd6ea6c497dee3a842a");
239     TestSHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
240              "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
241     TestSHA1("For this sample, this 63-byte string will be used as input data",
242              "4f0ea5cd0585a23d028abdc1a6684e5a8094dc49");
243     TestSHA1("This is exactly 64 bytes long, not counting the terminating byte",
244              "fb679f23e7d1ce053313e66e127ab1b444397057");
245     TestSHA1(std::string(1000000, 'a'), "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
246     TestSHA1(test1, "b7755760681cbfd971451668f32af5774f4656b5");
247 }
248 
BOOST_AUTO_TEST_CASE(sha256_testvectors)249 BOOST_AUTO_TEST_CASE(sha256_testvectors) {
250     TestSHA256("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
251     TestSHA256("abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
252     TestSHA256("message digest",
253                "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650");
254     TestSHA256("secure hash algorithm",
255                "f30ceb2bb2829e79e4ca9753d35a8ecc00262d164cc077080295381cbd643f0d");
256     TestSHA256("SHA256 is considered to be safe",
257                "6819d915c73f4d1e77e4e1b52d1fa0f9cf9beaead3939f15874bd988e2a23630");
258     TestSHA256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
259                "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
260     TestSHA256("For this sample, this 63-byte string will be used as input data",
261                "f08a78cbbaee082b052ae0708f32fa1e50c5c421aa772ba5dbb406a2ea6be342");
262     TestSHA256("This is exactly 64 bytes long, not counting the terminating byte",
263                "ab64eff7e88e2e46165e29f2bce41826bd4c7b3552f6b382a9e7d3af47c245f8");
264     TestSHA256("As Bitcoin relies on 80 byte header hashes, we want to have an example for that.",
265                "7406e8de7d6e4fffc573daef05aefb8806e7790f55eab5576f31349743cca743");
266     TestSHA256(std::string(1000000, 'a'),
267                "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
268     TestSHA256(test1, "a316d55510b49662420f49d145d42fb83f31ef8dc016aa4e32df049991a91e26");
269 }
270 
BOOST_AUTO_TEST_CASE(sha512_testvectors)271 BOOST_AUTO_TEST_CASE(sha512_testvectors) {
272     TestSHA512("",
273                "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"
274                "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
275     TestSHA512("abc",
276                "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
277                "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
278     TestSHA512("message digest",
279                "107dbf389d9e9f71a3a95f6c055b9251bc5268c2be16d6c13492ea45b0199f33"
280                "09e16455ab1e96118e8a905d5597b72038ddb372a89826046de66687bb420e7c");
281     TestSHA512("secure hash algorithm",
282                "7746d91f3de30c68cec0dd693120a7e8b04d8073cb699bdce1a3f64127bca7a3"
283                "d5db502e814bb63c063a7a5043b2df87c61133395f4ad1edca7fcf4b30c3236e");
284     TestSHA512("SHA512 is considered to be safe",
285                "099e6468d889e1c79092a89ae925a9499b5408e01b66cb5b0a3bd0dfa51a9964"
286                "6b4a3901caab1318189f74cd8cf2e941829012f2449df52067d3dd5b978456c2");
287     TestSHA512("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
288                "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c335"
289                "96fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445");
290     TestSHA512("For this sample, this 63-byte string will be used as input data",
291                "b3de4afbc516d2478fe9b518d063bda6c8dd65fc38402dd81d1eb7364e72fb6e"
292                "6663cf6d2771c8f5a6da09601712fb3d2a36c6ffea3e28b0818b05b0a8660766");
293     TestSHA512("This is exactly 64 bytes long, not counting the terminating byte",
294                "70aefeaa0e7ac4f8fe17532d7185a289bee3b428d950c14fa8b713ca09814a38"
295                "7d245870e007a80ad97c369d193e41701aa07f3221d15f0e65a1ff970cedf030");
296     TestSHA512("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
297                "ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
298                "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"
299                "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909");
300     TestSHA512(std::string(1000000, 'a'),
301                "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"
302                "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b");
303     TestSHA512(test1,
304                "40cac46c147e6131c5193dd5f34e9d8bb4951395f27b08c558c65ff4ba2de594"
305                "37de8c3ef5459d76a52cedc02dc499a3c9ed9dedbfb3281afd9653b8a112fafc");
306 }
307 
BOOST_AUTO_TEST_CASE(hmac_sha256_testvectors)308 BOOST_AUTO_TEST_CASE(hmac_sha256_testvectors) {
309     // test cases 1, 2, 3, 4, 6 and 7 of RFC 4231
310     TestHMACSHA256("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
311                    "4869205468657265",
312                    "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7");
313     TestHMACSHA256("4a656665",
314                    "7768617420646f2079612077616e7420666f72206e6f7468696e673f",
315                    "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843");
316     TestHMACSHA256("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
317                    "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
318                    "dddddddddddddddddddddddddddddddddddd",
319                    "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe");
320     TestHMACSHA256("0102030405060708090a0b0c0d0e0f10111213141516171819",
321                    "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
322                    "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
323                    "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b");
324     TestHMACSHA256("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
325                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
326                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
327                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
328                    "aaaaaa",
329                    "54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a"
330                    "65204b6579202d2048617368204b6579204669727374",
331                    "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54");
332     TestHMACSHA256("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
333                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
334                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
335                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
336                    "aaaaaa",
337                    "5468697320697320612074657374207573696e672061206c6172676572207468"
338                    "616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074"
339                    "68616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565"
340                    "647320746f20626520686173686564206265666f7265206265696e6720757365"
341                    "642062792074686520484d414320616c676f726974686d2e",
342                    "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2");
343     // Test case with key length 63 bytes.
344     TestHMACSHA256("4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
345                    "4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a6566",
346                    "7768617420646f2079612077616e7420666f72206e6f7468696e673f",
347                    "9de4b546756c83516720a4ad7fe7bdbeac4298c6fdd82b15f895a6d10b0769a6");
348     // Test case with key length 64 bytes.
349     TestHMACSHA256("4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
350                    "4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665",
351                    "7768617420646f2079612077616e7420666f72206e6f7468696e673f",
352                    "528c609a4c9254c274585334946b7c2661bad8f1fc406b20f6892478d19163dd");
353     // Test case with key length 65 bytes.
354     TestHMACSHA256("4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
355                    "4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
356                    "4a",
357                    "7768617420646f2079612077616e7420666f72206e6f7468696e673f",
358                    "d06af337f359a2330deffb8e3cbe4b5b7aa8ca1f208528cdbd245d5dc63c4483");
359 }
360 
BOOST_AUTO_TEST_CASE(hmac_sha512_testvectors)361 BOOST_AUTO_TEST_CASE(hmac_sha512_testvectors) {
362     // test cases 1, 2, 3, 4, 6 and 7 of RFC 4231
363     TestHMACSHA512("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
364                    "4869205468657265",
365                    "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde"
366                    "daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854");
367     TestHMACSHA512("4a656665",
368                    "7768617420646f2079612077616e7420666f72206e6f7468696e673f",
369                    "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554"
370                    "9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737");
371     TestHMACSHA512("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
372                    "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
373                    "dddddddddddddddddddddddddddddddddddd",
374                    "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39"
375                    "bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb");
376     TestHMACSHA512("0102030405060708090a0b0c0d0e0f10111213141516171819",
377                    "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
378                    "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
379                    "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3db"
380                    "a91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd");
381     TestHMACSHA512("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
382                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
383                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
384                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
385                    "aaaaaa",
386                    "54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a"
387                    "65204b6579202d2048617368204b6579204669727374",
388                    "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f352"
389                    "6b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598");
390     TestHMACSHA512("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
391                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
392                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
393                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
394                    "aaaaaa",
395                    "5468697320697320612074657374207573696e672061206c6172676572207468"
396                    "616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074"
397                    "68616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565"
398                    "647320746f20626520686173686564206265666f7265206265696e6720757365"
399                    "642062792074686520484d414320616c676f726974686d2e",
400                    "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944"
401                    "b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58");
402     // Test case with key length 127 bytes.
403     TestHMACSHA512("4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
404                    "4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
405                    "4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
406                    "4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a6566",
407                    "7768617420646f2079612077616e7420666f72206e6f7468696e673f",
408                    "267424dfb8eeb999f3e5ec39a4fe9fd14c923e6187e0897063e5c9e02b2e624a"
409                    "c04413e762977df71a9fb5d562b37f89dfdfb930fce2ed1fa783bbc2a203d80e");
410     // Test case with key length 128 bytes.
411     TestHMACSHA512("4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
412                    "4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
413                    "4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
414                    "4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665",
415                    "7768617420646f2079612077616e7420666f72206e6f7468696e673f",
416                    "43aaac07bb1dd97c82c04df921f83b16a68d76815cd1a30d3455ad43a3d80484"
417                    "2bb35462be42cc2e4b5902de4d204c1c66d93b47d1383e3e13a3788687d61258");
418     // Test case with key length 129 bytes.
419     TestHMACSHA512("4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
420                    "4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
421                    "4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
422                    "4a6566654a6566654a6566654a6566654a6566654a6566654a6566654a656665"
423                    "4a",
424                    "7768617420646f2079612077616e7420666f72206e6f7468696e673f",
425                    "0b273325191cfc1b4b71d5075c8fcad67696309d292b1dad2cd23983a35feb8e"
426                    "fb29795e79f2ef27f68cb1e16d76178c307a67beaad9456fac5fdffeadb16e2c");
427 }
428 
BOOST_AUTO_TEST_CASE(aes_testvectors)429 BOOST_AUTO_TEST_CASE(aes_testvectors) {
430     // AES test vectors from FIPS 197.
431     TestAES128("000102030405060708090a0b0c0d0e0f", "00112233445566778899aabbccddeeff", "69c4e0d86a7b0430d8cdb78070b4c55a");
432     TestAES256("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", "00112233445566778899aabbccddeeff", "8ea2b7ca516745bfeafc49904b496089");
433 
434     // AES-ECB test vectors from NIST sp800-38a.
435     TestAES128("2b7e151628aed2a6abf7158809cf4f3c", "6bc1bee22e409f96e93d7e117393172a", "3ad77bb40d7a3660a89ecaf32466ef97");
436     TestAES128("2b7e151628aed2a6abf7158809cf4f3c", "ae2d8a571e03ac9c9eb76fac45af8e51", "f5d3d58503b9699de785895a96fdbaaf");
437     TestAES128("2b7e151628aed2a6abf7158809cf4f3c", "30c81c46a35ce411e5fbc1191a0a52ef", "43b1cd7f598ece23881b00e3ed030688");
438     TestAES128("2b7e151628aed2a6abf7158809cf4f3c", "f69f2445df4f9b17ad2b417be66c3710", "7b0c785e27e8ad3f8223207104725dd4");
439     TestAES256("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "6bc1bee22e409f96e93d7e117393172a", "f3eed1bdb5d2a03c064b5a7e3db181f8");
440     TestAES256("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "ae2d8a571e03ac9c9eb76fac45af8e51", "591ccb10d410ed26dc5ba74a31362870");
441     TestAES256("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "30c81c46a35ce411e5fbc1191a0a52ef", "b6ed21b99ca6f4f9f153e7b1beafed1d");
442     TestAES256("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "f69f2445df4f9b17ad2b417be66c3710", "23304b7a39f9f3ff067d8d8f9e24ecc7");
443 }
444 
BOOST_AUTO_TEST_CASE(aes_cbc_testvectors)445 BOOST_AUTO_TEST_CASE(aes_cbc_testvectors) {
446 
447     // NIST AES CBC 128-bit encryption test-vectors
448     TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "000102030405060708090A0B0C0D0E0F", false, \
449                   "6bc1bee22e409f96e93d7e117393172a", "7649abac8119b246cee98e9b12e9197d");
450     TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "7649ABAC8119B246CEE98E9B12E9197D", false, \
451                   "ae2d8a571e03ac9c9eb76fac45af8e51", "5086cb9b507219ee95db113a917678b2");
452     TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "5086cb9b507219ee95db113a917678b2", false, \
453                   "30c81c46a35ce411e5fbc1191a0a52ef", "73bed6b8e3c1743b7116e69e22229516");
454     TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "73bed6b8e3c1743b7116e69e22229516", false, \
455                   "f69f2445df4f9b17ad2b417be66c3710", "3ff1caa1681fac09120eca307586e1a7");
456 
457     // The same vectors with padding enabled
458     TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "000102030405060708090A0B0C0D0E0F", true, \
459                   "6bc1bee22e409f96e93d7e117393172a", "7649abac8119b246cee98e9b12e9197d8964e0b149c10b7b682e6e39aaeb731c");
460     TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "7649ABAC8119B246CEE98E9B12E9197D", true, \
461                   "ae2d8a571e03ac9c9eb76fac45af8e51", "5086cb9b507219ee95db113a917678b255e21d7100b988ffec32feeafaf23538");
462     TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "5086cb9b507219ee95db113a917678b2", true, \
463                   "30c81c46a35ce411e5fbc1191a0a52ef", "73bed6b8e3c1743b7116e69e22229516f6eccda327bf8e5ec43718b0039adceb");
464     TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "73bed6b8e3c1743b7116e69e22229516", true, \
465                   "f69f2445df4f9b17ad2b417be66c3710", "3ff1caa1681fac09120eca307586e1a78cb82807230e1321d3fae00d18cc2012");
466 
467     // NIST AES CBC 256-bit encryption test-vectors
468     TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
469                   "000102030405060708090A0B0C0D0E0F", false, "6bc1bee22e409f96e93d7e117393172a", \
470                   "f58c4c04d6e5f1ba779eabfb5f7bfbd6");
471     TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
472                   "F58C4C04D6E5F1BA779EABFB5F7BFBD6", false, "ae2d8a571e03ac9c9eb76fac45af8e51", \
473                   "9cfc4e967edb808d679f777bc6702c7d");
474     TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
475                   "9CFC4E967EDB808D679F777BC6702C7D", false, "30c81c46a35ce411e5fbc1191a0a52ef",
476                   "39f23369a9d9bacfa530e26304231461");
477     TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
478                   "39F23369A9D9BACFA530E26304231461", false, "f69f2445df4f9b17ad2b417be66c3710", \
479                   "b2eb05e2c39be9fcda6c19078c6a9d1b");
480 
481     // The same vectors with padding enabled
482     TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
483                   "000102030405060708090A0B0C0D0E0F", true, "6bc1bee22e409f96e93d7e117393172a", \
484                   "f58c4c04d6e5f1ba779eabfb5f7bfbd6485a5c81519cf378fa36d42b8547edc0");
485     TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
486                   "F58C4C04D6E5F1BA779EABFB5F7BFBD6", true, "ae2d8a571e03ac9c9eb76fac45af8e51", \
487                   "9cfc4e967edb808d679f777bc6702c7d3a3aa5e0213db1a9901f9036cf5102d2");
488     TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
489                   "9CFC4E967EDB808D679F777BC6702C7D", true, "30c81c46a35ce411e5fbc1191a0a52ef",
490                   "39f23369a9d9bacfa530e263042314612f8da707643c90a6f732b3de1d3f5cee");
491     TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
492                   "39F23369A9D9BACFA530E26304231461", true, "f69f2445df4f9b17ad2b417be66c3710", \
493                   "b2eb05e2c39be9fcda6c19078c6a9d1b3f461796d6b0d6b2e0c2a72b4d80e644");
494 }
495 
496 
BOOST_AUTO_TEST_CASE(chacha20_testvector)497 BOOST_AUTO_TEST_CASE(chacha20_testvector)
498 {
499     // Test vector from RFC 7539
500     TestChaCha20("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 0x4a000000UL, 1,
501                  "224f51f3401bd9e12fde276fb8631ded8c131f823d2c06e27e4fcaec9ef3cf788a3b0aa372600a92b57974cded2b9334794cb"
502                  "a40c63e34cdea212c4cf07d41b769a6749f3f630f4122cafe28ec4dc47e26d4346d70b98c73f3e9c53ac40c5945398b6eda1a"
503                  "832c89c167eacd901d7e2bf363");
504 
505     // Test vectors from https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7
506     TestChaCha20("0000000000000000000000000000000000000000000000000000000000000000", 0, 0,
507                  "76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b"
508                  "8f41518a11cc387b669b2ee6586");
509     TestChaCha20("0000000000000000000000000000000000000000000000000000000000000001", 0, 0,
510                  "4540f05a9f1fb296d7736e7b208e3c96eb4fe1834688d2604f450952ed432d41bbe2a0b6ea7566d2a5d1e7e20d42af2c53d79"
511                  "2b1c43fea817e9ad275ae546963");
512     TestChaCha20("0000000000000000000000000000000000000000000000000000000000000000", 0x0100000000000000ULL, 0,
513                  "de9cba7bf3d69ef5e786dc63973f653a0b49e015adbff7134fcb7df137821031e85a050278a7084527214f73efc7fa5b52770"
514                  "62eb7a0433e445f41e3");
515     TestChaCha20("0000000000000000000000000000000000000000000000000000000000000000", 1, 0,
516                  "ef3fdfd6c61578fbf5cf35bd3dd33b8009631634d21e42ac33960bd138e50d32111e4caf237ee53ca8ad6426194a88545ddc4"
517                  "97a0b466e7d6bbdb0041b2f586b");
518     TestChaCha20("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 0x0706050403020100ULL, 0,
519                  "f798a189f195e66982105ffb640bb7757f579da31602fc93ec01ac56f85ac3c134a4547b733b46413042c9440049176905d3b"
520                  "e59ea1c53f15916155c2be8241a38008b9a26bc35941e2444177c8ade6689de95264986d95889fb60e84629c9bd9a5acb1cc1"
521                  "18be563eb9b3a4a472f82e09a7e778492b562ef7130e88dfe031c79db9d4f7c7a899151b9a475032b63fc385245fe054e3dd5"
522                  "a97a5f576fe064025d3ce042c566ab2c507b138db853e3d6959660996546cc9c4a6eafdc777c040d70eaf46f76dad3979e5c5"
523                  "360c3317166a1c894c94a371876a94df7628fe4eaaf2ccb27d5aaae0ad7ad0f9d4b6ad3b54098746d4524d38407a6deb3ab78"
524                  "fab78c9");
525 }
526 
BOOST_AUTO_TEST_CASE(countbits_tests)527 BOOST_AUTO_TEST_CASE(countbits_tests)
528 {
529     FastRandomContext ctx;
530     for (unsigned int i = 0; i <= 64; ++i) {
531         if (i == 0) {
532             // Check handling of zero.
533             BOOST_CHECK_EQUAL(CountBits(0), 0U);
534         } else if (i < 10) {
535             for (uint64_t j = (uint64_t)1 << (i - 1); (j >> i) == 0; ++j) {
536                 // Exhaustively test up to 10 bits
537                 BOOST_CHECK_EQUAL(CountBits(j), i);
538             }
539         } else {
540             for (int k = 0; k < 1000; k++) {
541                 // Randomly test 1000 samples of each length above 10 bits.
542                 uint64_t j = ((uint64_t)1) << (i - 1) | ctx.randbits(i - 1);
543                 BOOST_CHECK_EQUAL(CountBits(j), i);
544             }
545         }
546     }
547 }
548 
BOOST_AUTO_TEST_CASE(sha256d64)549 BOOST_AUTO_TEST_CASE(sha256d64)
550 {
551     for (int i = 0; i <= 32; ++i) {
552         unsigned char in[64 * 32];
553         unsigned char out1[32 * 32], out2[32 * 32];
554         for (int j = 0; j < 64 * i; ++j) {
555             in[j] = InsecureRandBits(8);
556         }
557         for (int j = 0; j < i; ++j) {
558             CHash256().Write(in + 64 * j, 64).Finalize(out1 + 32 * j);
559         }
560         SHA256D64(out2, in, i);
561         BOOST_CHECK(memcmp(out1, out2, 32 * i) == 0);
562     }
563 }
564 
565 BOOST_AUTO_TEST_SUITE_END()
566