Changeset View
Changeset View
Standalone View
Standalone View
src/test/crypto_tests.cpp
// Copyright (c) 2014-2019 The Bitcoin Core developers | // Copyright (c) 2014-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <crypto/aes.h> | #include <crypto/aes.h> | ||||
#include <crypto/chacha20.h> | #include <crypto/chacha20.h> | ||||
#include <crypto/hkdf_sha256_32.h> | #include <crypto/hkdf_sha256_32.h> | ||||
#include <crypto/hmac_sha256.h> | #include <crypto/hmac_sha256.h> | ||||
#include <crypto/hmac_sha512.h> | #include <crypto/hmac_sha512.h> | ||||
#include <crypto/poly1305.h> | |||||
#include <crypto/ripemd160.h> | #include <crypto/ripemd160.h> | ||||
#include <crypto/sha1.h> | #include <crypto/sha1.h> | ||||
#include <crypto/sha256.h> | #include <crypto/sha256.h> | ||||
#include <crypto/sha512.h> | #include <crypto/sha512.h> | ||||
#include <random.h> | #include <random.h> | ||||
#include <util/strencodings.h> | #include <util/strencodings.h> | ||||
▲ Show 20 Lines • Show All 228 Lines • ▼ Show 20 Lines | if (!hex_message.empty()) { | ||||
rng.Keystream(only_keystream.data(), only_keystream.size()); | rng.Keystream(only_keystream.data(), only_keystream.size()); | ||||
for (size_t i = 0; i != m.size(); i++) { | for (size_t i = 0; i != m.size(); i++) { | ||||
outres[i] = m[i] ^ only_keystream[i]; | outres[i] = m[i] ^ only_keystream[i]; | ||||
} | } | ||||
BOOST_CHECK(out == outres); | BOOST_CHECK(out == outres); | ||||
} | } | ||||
} | } | ||||
static void TestPoly1305(const std::string &hexmessage, | |||||
const std::string &hexkey, const std::string &hextag) { | |||||
std::vector<uint8_t> key = ParseHex(hexkey); | |||||
std::vector<uint8_t> m = ParseHex(hexmessage); | |||||
std::vector<uint8_t> tag = ParseHex(hextag); | |||||
std::vector<uint8_t> tagres; | |||||
tagres.resize(POLY1305_TAGLEN); | |||||
poly1305_auth(tagres.data(), m.data(), m.size(), key.data()); | |||||
BOOST_CHECK(tag == tagres); | |||||
} | |||||
static void TestHKDF_SHA256_32(const std::string &ikm_hex, | static void TestHKDF_SHA256_32(const std::string &ikm_hex, | ||||
const std::string &salt_hex, | const std::string &salt_hex, | ||||
const std::string &info_hex, | const std::string &info_hex, | ||||
const std::string &okm_check_hex) { | const std::string &okm_check_hex) { | ||||
std::vector<uint8_t> initial_key_material = ParseHex(ikm_hex); | std::vector<uint8_t> initial_key_material = ParseHex(ikm_hex); | ||||
std::vector<uint8_t> salt = ParseHex(salt_hex); | std::vector<uint8_t> salt = ParseHex(salt_hex); | ||||
std::vector<uint8_t> info = ParseHex(info_hex); | std::vector<uint8_t> info = ParseHex(info_hex); | ||||
▲ Show 20 Lines • Show All 477 Lines • ▼ Show 20 Lines | TestHKDF_SHA256_32( | ||||
"d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5" | "d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5" | ||||
"f6f7f8f9fafbfcfdfeff", | "f6f7f8f9fafbfcfdfeff", | ||||
"b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c"); | "b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c"); | ||||
TestHKDF_SHA256_32( | TestHKDF_SHA256_32( | ||||
"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", "", "", | "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", "", "", | ||||
"8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d"); | "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d"); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(poly1305_testvector) { | |||||
// RFC 7539, section 2.5.2. | |||||
TestPoly1305( | |||||
"43727970746f6772617068696320466f72756d2052657365617263682047726f7570", | |||||
"85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b", | |||||
"a8061dc1305136c6c22b8baf0c0127a9"); | |||||
// RFC 7539, section A.3. | |||||
TestPoly1305( | |||||
"0000000000000000000000000000000000000000000000000000000000000000000000" | |||||
"0000000000000000000000000000000" | |||||
"000000000000000000000000000", | |||||
"0000000000000000000000000000000000000000000000000000000000000000", | |||||
"00000000000000000000000000000000"); | |||||
TestPoly1305( | |||||
"416e79207375626d697373696f6e20746f20746865204945544620696e74656e646564" | |||||
"2062792074686520436f6e747269627" | |||||
"5746f7220666f72207075626c69636174696f6e20617320616c6c206f7220706172742" | |||||
"06f6620616e204945544620496e7465" | |||||
"726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74" | |||||
"206d6164652077697468696e2074686" | |||||
"520636f6e74657874206f6620616e204945544620616374697669747920697320636f6" | |||||
"e7369646572656420616e2022494554" | |||||
"4620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e" | |||||
"636c756465206f72616c20737461746" | |||||
"56d656e747320696e20494554462073657373696f6e732c2061732077656c6c2061732" | |||||
"07772697474656e20616e6420656c65" | |||||
"6374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e7920" | |||||
"74696d65206f7220706c6163652c207" | |||||
"768696368206172652061646472657373656420746f", | |||||
"0000000000000000000000000000000036e5f6b5c5e06070f0efca96227a863e", | |||||
"36e5f6b5c5e06070f0efca96227a863e"); | |||||
TestPoly1305( | |||||
"416e79207375626d697373696f6e20746f20746865204945544620696e74656e646564" | |||||
"2062792074686520436f6e747269627" | |||||
"5746f7220666f72207075626c69636174696f6e20617320616c6c206f7220706172742" | |||||
"06f6620616e204945544620496e7465" | |||||
"726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74" | |||||
"206d6164652077697468696e2074686" | |||||
"520636f6e74657874206f6620616e204945544620616374697669747920697320636f6" | |||||
"e7369646572656420616e2022494554" | |||||
"4620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e" | |||||
"636c756465206f72616c20737461746" | |||||
"56d656e747320696e20494554462073657373696f6e732c2061732077656c6c2061732" | |||||
"07772697474656e20616e6420656c65" | |||||
"6374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e7920" | |||||
"74696d65206f7220706c6163652c207" | |||||
"768696368206172652061646472657373656420746f", | |||||
"36e5f6b5c5e06070f0efca96227a863e00000000000000000000000000000000", | |||||
"f3477e7cd95417af89a6b8794c310cf0"); | |||||
TestPoly1305( | |||||
"2754776173206272696c6c69672c20616e642074686520736c6974687920746f766573" | |||||
"0a446964206779726520616e6420676" | |||||
"96d626c6520696e2074686520776162653a0a416c6c206d696d7379207765726520746" | |||||
"86520626f726f676f7665732c0a416e" | |||||
"6420746865206d6f6d65207261746873206f757467726162652e", | |||||
"1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0", | |||||
"4541669a7eaaee61e708dc7cbcc5eb62"); | |||||
TestPoly1305( | |||||
"ffffffffffffffffffffffffffffffff", | |||||
"0200000000000000000000000000000000000000000000000000000000000000", | |||||
"03000000000000000000000000000000"); | |||||
TestPoly1305( | |||||
"02000000000000000000000000000000", | |||||
"02000000000000000000000000000000ffffffffffffffffffffffffffffffff", | |||||
"03000000000000000000000000000000"); | |||||
TestPoly1305( | |||||
"fffffffffffffffffffffffffffffffff0ffffffffffffffffffffffffffffff110000" | |||||
"00000000000000000000000000", | |||||
"0100000000000000000000000000000000000000000000000000000000000000", | |||||
"05000000000000000000000000000000"); | |||||
TestPoly1305( | |||||
"fffffffffffffffffffffffffffffffffbfefefefefefefefefefefefefefefe010101" | |||||
"01010101010101010101010101", | |||||
"0100000000000000000000000000000000000000000000000000000000000000", | |||||
"00000000000000000000000000000000"); | |||||
TestPoly1305( | |||||
"fdffffffffffffffffffffffffffffff", | |||||
"0200000000000000000000000000000000000000000000000000000000000000", | |||||
"faffffffffffffffffffffffffffffff"); | |||||
TestPoly1305( | |||||
"e33594d7505e43b900000000000000003394d7505e4379cd0100000000000000000000" | |||||
"0000000000000000000000000001000000000000000000000000000000", | |||||
"0100000000000000040000000000000000000000000000000000000000000000", | |||||
"14000000000000005500000000000000"); | |||||
TestPoly1305( | |||||
"e33594d7505e43b900000000000000003394d7505e4379cd0100000000000000000000" | |||||
"00000000000000000000000000", | |||||
"0100000000000000040000000000000000000000000000000000000000000000", | |||||
"13000000000000000000000000000000"); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(countbits_tests) { | BOOST_AUTO_TEST_CASE(countbits_tests) { | ||||
FastRandomContext ctx; | FastRandomContext ctx; | ||||
for (unsigned int i = 0; i <= 64; ++i) { | for (unsigned int i = 0; i <= 64; ++i) { | ||||
if (i == 0) { | if (i == 0) { | ||||
// Check handling of zero. | // Check handling of zero. | ||||
BOOST_CHECK_EQUAL(CountBits(0), 0U); | BOOST_CHECK_EQUAL(CountBits(0), 0U); | ||||
} else if (i < 10) { | } else if (i < 10) { | ||||
for (uint64_t j = 1 << (i - 1); (j >> i) == 0; ++j) { | for (uint64_t j = 1 << (i - 1); (j >> i) == 0; ++j) { | ||||
Show All 29 Lines |