Changeset View
Changeset View
Standalone View
Standalone View
src/test/crypto_tests.cpp
Show All 20 Lines | |||||
#include <openssl/aes.h> | #include <openssl/aes.h> | ||||
#include <openssl/evp.h> | #include <openssl/evp.h> | ||||
#include <vector> | #include <vector> | ||||
BOOST_FIXTURE_TEST_SUITE(crypto_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(crypto_tests, BasicTestingSetup) | ||||
template <typename Hasher, typename In, typename Out> | template <typename Hasher, typename In, typename Out> | ||||
void TestVector(const Hasher &h, const In &in, const Out &out) { | static void TestVector(const Hasher &h, const In &in, const Out &out) { | ||||
Out hash; | Out hash; | ||||
BOOST_CHECK(out.size() == h.OUTPUT_SIZE); | BOOST_CHECK(out.size() == h.OUTPUT_SIZE); | ||||
hash.resize(out.size()); | hash.resize(out.size()); | ||||
{ | { | ||||
// Test that writing the whole input string at once works. | // Test that writing the whole input string at once works. | ||||
Hasher(h).Write((uint8_t *)&in[0], in.size()).Finalize(&hash[0]); | Hasher(h).Write((uint8_t *)&in[0], in.size()).Finalize(&hash[0]); | ||||
BOOST_CHECK(hash == out); | BOOST_CHECK(hash == out); | ||||
} | } | ||||
Show All 15 Lines | for (int i = 0; i < 32; i++) { | ||||
BOOST_CHECK(hash == out); | BOOST_CHECK(hash == out); | ||||
} | } | ||||
} | } | ||||
hasher.Finalize(&hash[0]); | hasher.Finalize(&hash[0]); | ||||
BOOST_CHECK(hash == out); | BOOST_CHECK(hash == out); | ||||
} | } | ||||
} | } | ||||
void TestSHA1(const std::string &in, const std::string &hexout) { | static void TestSHA1(const std::string &in, const std::string &hexout) { | ||||
TestVector(CSHA1(), in, ParseHex(hexout)); | TestVector(CSHA1(), in, ParseHex(hexout)); | ||||
} | } | ||||
void TestSHA256(const std::string &in, const std::string &hexout) { | static void TestSHA256(const std::string &in, const std::string &hexout) { | ||||
TestVector(CSHA256(), in, ParseHex(hexout)); | TestVector(CSHA256(), in, ParseHex(hexout)); | ||||
} | } | ||||
void TestSHA512(const std::string &in, const std::string &hexout) { | static void TestSHA512(const std::string &in, const std::string &hexout) { | ||||
TestVector(CSHA512(), in, ParseHex(hexout)); | TestVector(CSHA512(), in, ParseHex(hexout)); | ||||
} | } | ||||
void TestRIPEMD160(const std::string &in, const std::string &hexout) { | static void TestRIPEMD160(const std::string &in, const std::string &hexout) { | ||||
TestVector(CRIPEMD160(), in, ParseHex(hexout)); | TestVector(CRIPEMD160(), in, ParseHex(hexout)); | ||||
} | } | ||||
void TestHMACSHA256(const std::string &hexkey, const std::string &hexin, | static void TestHMACSHA256(const std::string &hexkey, const std::string &hexin, | ||||
const std::string &hexout) { | const std::string &hexout) { | ||||
std::vector<uint8_t> key = ParseHex(hexkey); | std::vector<uint8_t> key = ParseHex(hexkey); | ||||
TestVector(CHMAC_SHA256(&key[0], key.size()), ParseHex(hexin), | TestVector(CHMAC_SHA256(&key[0], key.size()), ParseHex(hexin), | ||||
ParseHex(hexout)); | ParseHex(hexout)); | ||||
} | } | ||||
void TestHMACSHA512(const std::string &hexkey, const std::string &hexin, | static void TestHMACSHA512(const std::string &hexkey, const std::string &hexin, | ||||
const std::string &hexout) { | const std::string &hexout) { | ||||
std::vector<uint8_t> key = ParseHex(hexkey); | std::vector<uint8_t> key = ParseHex(hexkey); | ||||
TestVector(CHMAC_SHA512(&key[0], key.size()), ParseHex(hexin), | TestVector(CHMAC_SHA512(&key[0], key.size()), ParseHex(hexin), | ||||
ParseHex(hexout)); | ParseHex(hexout)); | ||||
} | } | ||||
void TestAES128(const std::string &hexkey, const std::string &hexin, | static void TestAES128(const std::string &hexkey, const std::string &hexin, | ||||
const std::string &hexout) { | const std::string &hexout) { | ||||
std::vector<uint8_t> key = ParseHex(hexkey); | std::vector<uint8_t> key = ParseHex(hexkey); | ||||
std::vector<uint8_t> in = ParseHex(hexin); | std::vector<uint8_t> in = ParseHex(hexin); | ||||
std::vector<uint8_t> correctout = ParseHex(hexout); | std::vector<uint8_t> correctout = ParseHex(hexout); | ||||
std::vector<uint8_t> buf, buf2; | std::vector<uint8_t> buf, buf2; | ||||
assert(key.size() == 16); | assert(key.size() == 16); | ||||
assert(in.size() == 16); | assert(in.size() == 16); | ||||
assert(correctout.size() == 16); | assert(correctout.size() == 16); | ||||
AES128Encrypt enc(&key[0]); | AES128Encrypt enc(&key[0]); | ||||
buf.resize(correctout.size()); | buf.resize(correctout.size()); | ||||
buf2.resize(correctout.size()); | buf2.resize(correctout.size()); | ||||
enc.Encrypt(&buf[0], &in[0]); | enc.Encrypt(&buf[0], &in[0]); | ||||
BOOST_CHECK_EQUAL(HexStr(buf), HexStr(correctout)); | BOOST_CHECK_EQUAL(HexStr(buf), HexStr(correctout)); | ||||
AES128Decrypt dec(&key[0]); | AES128Decrypt dec(&key[0]); | ||||
dec.Decrypt(&buf2[0], &buf[0]); | dec.Decrypt(&buf2[0], &buf[0]); | ||||
BOOST_CHECK_EQUAL(HexStr(buf2), HexStr(in)); | BOOST_CHECK_EQUAL(HexStr(buf2), HexStr(in)); | ||||
} | } | ||||
void TestAES256(const std::string &hexkey, const std::string &hexin, | static void TestAES256(const std::string &hexkey, const std::string &hexin, | ||||
const std::string &hexout) { | const std::string &hexout) { | ||||
std::vector<uint8_t> key = ParseHex(hexkey); | std::vector<uint8_t> key = ParseHex(hexkey); | ||||
std::vector<uint8_t> in = ParseHex(hexin); | std::vector<uint8_t> in = ParseHex(hexin); | ||||
std::vector<uint8_t> correctout = ParseHex(hexout); | std::vector<uint8_t> correctout = ParseHex(hexout); | ||||
std::vector<uint8_t> buf; | std::vector<uint8_t> buf; | ||||
assert(key.size() == 32); | assert(key.size() == 32); | ||||
assert(in.size() == 16); | assert(in.size() == 16); | ||||
assert(correctout.size() == 16); | assert(correctout.size() == 16); | ||||
AES256Encrypt enc(&key[0]); | AES256Encrypt enc(&key[0]); | ||||
buf.resize(correctout.size()); | buf.resize(correctout.size()); | ||||
enc.Encrypt(&buf[0], &in[0]); | enc.Encrypt(&buf[0], &in[0]); | ||||
BOOST_CHECK(buf == correctout); | BOOST_CHECK(buf == correctout); | ||||
AES256Decrypt dec(&key[0]); | AES256Decrypt dec(&key[0]); | ||||
dec.Decrypt(&buf[0], &buf[0]); | dec.Decrypt(&buf[0], &buf[0]); | ||||
BOOST_CHECK(buf == in); | BOOST_CHECK(buf == in); | ||||
} | } | ||||
void TestAES128CBC(const std::string &hexkey, const std::string &hexiv, | static void TestAES128CBC(const std::string &hexkey, const std::string &hexiv, | ||||
bool pad, const std::string &hexin, | bool pad, const std::string &hexin, | ||||
const std::string &hexout) { | const std::string &hexout) { | ||||
std::vector<uint8_t> key = ParseHex(hexkey); | std::vector<uint8_t> key = ParseHex(hexkey); | ||||
std::vector<uint8_t> iv = ParseHex(hexiv); | std::vector<uint8_t> iv = ParseHex(hexiv); | ||||
std::vector<uint8_t> in = ParseHex(hexin); | std::vector<uint8_t> in = ParseHex(hexin); | ||||
std::vector<uint8_t> correctout = ParseHex(hexout); | std::vector<uint8_t> correctout = ParseHex(hexout); | ||||
std::vector<uint8_t> realout(in.size() + AES_BLOCKSIZE); | std::vector<uint8_t> realout(in.size() + AES_BLOCKSIZE); | ||||
// Encrypt the plaintext and verify that it equals the cipher | // Encrypt the plaintext and verify that it equals the cipher | ||||
AES128CBCEncrypt enc(&key[0], &iv[0], pad); | AES128CBCEncrypt enc(&key[0], &iv[0], pad); | ||||
Show All 26 Lines | for (std::vector<uint8_t>::iterator i(in.begin()); i != in.end(); ++i) { | ||||
BOOST_CHECK(decrypted.size() == in.size()); | BOOST_CHECK(decrypted.size() == in.size()); | ||||
BOOST_CHECK_MESSAGE(subdecrypted == sub, HexStr(subdecrypted) + | BOOST_CHECK_MESSAGE(subdecrypted == sub, HexStr(subdecrypted) + | ||||
std::string(" != ") + | std::string(" != ") + | ||||
HexStr(sub)); | HexStr(sub)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, | static void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, | ||||
bool pad, const std::string &hexin, | bool pad, const std::string &hexin, | ||||
const std::string &hexout) { | const std::string &hexout) { | ||||
std::vector<uint8_t> key = ParseHex(hexkey); | std::vector<uint8_t> key = ParseHex(hexkey); | ||||
std::vector<uint8_t> iv = ParseHex(hexiv); | std::vector<uint8_t> iv = ParseHex(hexiv); | ||||
std::vector<uint8_t> in = ParseHex(hexin); | std::vector<uint8_t> in = ParseHex(hexin); | ||||
std::vector<uint8_t> correctout = ParseHex(hexout); | std::vector<uint8_t> correctout = ParseHex(hexout); | ||||
std::vector<uint8_t> realout(in.size() + AES_BLOCKSIZE); | std::vector<uint8_t> realout(in.size() + AES_BLOCKSIZE); | ||||
// Encrypt the plaintext and verify that it equals the cipher | // Encrypt the plaintext and verify that it equals the cipher | ||||
AES256CBCEncrypt enc(&key[0], &iv[0], pad); | AES256CBCEncrypt enc(&key[0], &iv[0], pad); | ||||
Show All 26 Lines | for (std::vector<uint8_t>::iterator i(in.begin()); i != in.end(); ++i) { | ||||
BOOST_CHECK(decrypted.size() == in.size()); | BOOST_CHECK(decrypted.size() == in.size()); | ||||
BOOST_CHECK_MESSAGE(subdecrypted == sub, HexStr(subdecrypted) + | BOOST_CHECK_MESSAGE(subdecrypted == sub, HexStr(subdecrypted) + | ||||
std::string(" != ") + | std::string(" != ") + | ||||
HexStr(sub)); | HexStr(sub)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
void TestChaCha20(const std::string &hexkey, uint64_t nonce, uint64_t seek, | static void TestChaCha20(const std::string &hexkey, uint64_t nonce, | ||||
const std::string &hexout) { | uint64_t seek, const std::string &hexout) { | ||||
std::vector<uint8_t> key = ParseHex(hexkey); | std::vector<uint8_t> key = ParseHex(hexkey); | ||||
ChaCha20 rng(key.data(), key.size()); | ChaCha20 rng(key.data(), key.size()); | ||||
rng.SetIV(nonce); | rng.SetIV(nonce); | ||||
rng.Seek(seek); | rng.Seek(seek); | ||||
std::vector<uint8_t> out = ParseHex(hexout); | std::vector<uint8_t> out = ParseHex(hexout); | ||||
std::vector<uint8_t> outres; | std::vector<uint8_t> outres; | ||||
outres.resize(out.size()); | outres.resize(out.size()); | ||||
rng.Output(outres.data(), outres.size()); | rng.Output(outres.data(), outres.size()); | ||||
BOOST_CHECK(out == outres); | BOOST_CHECK(out == outres); | ||||
} | } | ||||
std::string LongTestString(void) { | static std::string LongTestString(void) { | ||||
std::string ret; | std::string ret; | ||||
for (int i = 0; i < 200000; i++) { | for (int i = 0; i < 200000; i++) { | ||||
ret += uint8_t(i); | ret += uint8_t(i); | ||||
ret += uint8_t(i >> 4); | ret += uint8_t(i >> 4); | ||||
ret += uint8_t(i >> 8); | ret += uint8_t(i >> 8); | ||||
ret += uint8_t(i >> 12); | ret += uint8_t(i >> 12); | ||||
ret += uint8_t(i >> 16); | ret += uint8_t(i >> 16); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 460 Lines • Show Last 20 Lines |