Changeset View
Changeset View
Standalone View
Standalone View
src/test/crypto_tests.cpp
Show First 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
static 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)); | ||||
} | } | ||||
static 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.data(), key.size()), ParseHex(hexin), | ||||
ParseHex(hexout)); | ParseHex(hexout)); | ||||
} | } | ||||
static 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.data(), key.size()), ParseHex(hexin), | ||||
ParseHex(hexout)); | ParseHex(hexout)); | ||||
} | } | ||||
static 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.data()); | ||||
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.data(), in.data()); | ||||
BOOST_CHECK_EQUAL(HexStr(buf), HexStr(correctout)); | BOOST_CHECK_EQUAL(HexStr(buf), HexStr(correctout)); | ||||
AES128Decrypt dec(&key[0]); | AES128Decrypt dec(key.data()); | ||||
dec.Decrypt(&buf2[0], &buf[0]); | dec.Decrypt(buf2.data(), buf.data()); | ||||
BOOST_CHECK_EQUAL(HexStr(buf2), HexStr(in)); | BOOST_CHECK_EQUAL(HexStr(buf2), HexStr(in)); | ||||
} | } | ||||
static 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.data()); | ||||
buf.resize(correctout.size()); | buf.resize(correctout.size()); | ||||
enc.Encrypt(&buf[0], &in[0]); | enc.Encrypt(buf.data(), in.data()); | ||||
BOOST_CHECK(buf == correctout); | BOOST_CHECK(buf == correctout); | ||||
AES256Decrypt dec(&key[0]); | AES256Decrypt dec(key.data()); | ||||
dec.Decrypt(&buf[0], &buf[0]); | dec.Decrypt(buf.data(), buf.data()); | ||||
BOOST_CHECK(buf == in); | BOOST_CHECK(buf == in); | ||||
} | } | ||||
static 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.data(), iv.data(), pad); | ||||
int size = enc.Encrypt(&in[0], in.size(), &realout[0]); | int size = enc.Encrypt(in.data(), in.size(), realout.data()); | ||||
realout.resize(size); | realout.resize(size); | ||||
BOOST_CHECK(realout.size() == correctout.size()); | BOOST_CHECK(realout.size() == correctout.size()); | ||||
BOOST_CHECK_MESSAGE(realout == correctout, | BOOST_CHECK_MESSAGE(realout == correctout, | ||||
HexStr(realout) + std::string(" != ") + hexout); | HexStr(realout) + std::string(" != ") + hexout); | ||||
// Decrypt the cipher and verify that it equals the plaintext | // Decrypt the cipher and verify that it equals the plaintext | ||||
std::vector<uint8_t> decrypted(correctout.size()); | std::vector<uint8_t> decrypted(correctout.size()); | ||||
AES128CBCDecrypt dec(&key[0], &iv[0], pad); | AES128CBCDecrypt dec(key.data(), iv.data(), pad); | ||||
size = dec.Decrypt(&correctout[0], correctout.size(), &decrypted[0]); | size = dec.Decrypt(correctout.data(), correctout.size(), decrypted.data()); | ||||
decrypted.resize(size); | decrypted.resize(size); | ||||
BOOST_CHECK(decrypted.size() == in.size()); | BOOST_CHECK(decrypted.size() == in.size()); | ||||
BOOST_CHECK_MESSAGE(decrypted == in, | BOOST_CHECK_MESSAGE(decrypted == in, | ||||
HexStr(decrypted) + std::string(" != ") + hexin); | HexStr(decrypted) + std::string(" != ") + hexin); | ||||
// Encrypt and re-decrypt substrings of the plaintext and verify that they | // Encrypt and re-decrypt substrings of the plaintext and verify that they | ||||
// equal each-other | // equal each-other | ||||
for (std::vector<uint8_t>::iterator i(in.begin()); i != in.end(); ++i) { | for (std::vector<uint8_t>::iterator i(in.begin()); i != in.end(); ++i) { | ||||
std::vector<uint8_t> sub(i, in.end()); | std::vector<uint8_t> sub(i, in.end()); | ||||
std::vector<uint8_t> subout(sub.size() + AES_BLOCKSIZE); | std::vector<uint8_t> subout(sub.size() + AES_BLOCKSIZE); | ||||
int _size = enc.Encrypt(&sub[0], sub.size(), &subout[0]); | int _size = enc.Encrypt(sub.data(), sub.size(), subout.data()); | ||||
if (_size != 0) { | if (_size != 0) { | ||||
subout.resize(_size); | subout.resize(_size); | ||||
std::vector<uint8_t> subdecrypted(subout.size()); | std::vector<uint8_t> subdecrypted(subout.size()); | ||||
_size = dec.Decrypt(&subout[0], subout.size(), &subdecrypted[0]); | _size = | ||||
dec.Decrypt(subout.data(), subout.size(), subdecrypted.data()); | |||||
subdecrypted.resize(_size); | subdecrypted.resize(_size); | ||||
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)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
static 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.data(), iv.data(), pad); | ||||
int size = enc.Encrypt(&in[0], in.size(), &realout[0]); | int size = enc.Encrypt(in.data(), in.size(), realout.data()); | ||||
realout.resize(size); | realout.resize(size); | ||||
BOOST_CHECK(realout.size() == correctout.size()); | BOOST_CHECK(realout.size() == correctout.size()); | ||||
BOOST_CHECK_MESSAGE(realout == correctout, | BOOST_CHECK_MESSAGE(realout == correctout, | ||||
HexStr(realout) + std::string(" != ") + hexout); | HexStr(realout) + std::string(" != ") + hexout); | ||||
// Decrypt the cipher and verify that it equals the plaintext | // Decrypt the cipher and verify that it equals the plaintext | ||||
std::vector<uint8_t> decrypted(correctout.size()); | std::vector<uint8_t> decrypted(correctout.size()); | ||||
AES256CBCDecrypt dec(&key[0], &iv[0], pad); | AES256CBCDecrypt dec(key.data(), iv.data(), pad); | ||||
size = dec.Decrypt(&correctout[0], correctout.size(), &decrypted[0]); | size = dec.Decrypt(correctout.data(), correctout.size(), decrypted.data()); | ||||
decrypted.resize(size); | decrypted.resize(size); | ||||
BOOST_CHECK(decrypted.size() == in.size()); | BOOST_CHECK(decrypted.size() == in.size()); | ||||
BOOST_CHECK_MESSAGE(decrypted == in, | BOOST_CHECK_MESSAGE(decrypted == in, | ||||
HexStr(decrypted) + std::string(" != ") + hexin); | HexStr(decrypted) + std::string(" != ") + hexin); | ||||
// Encrypt and re-decrypt substrings of the plaintext and verify that they | // Encrypt and re-decrypt substrings of the plaintext and verify that they | ||||
// equal each-other | // equal each-other | ||||
for (std::vector<uint8_t>::iterator i(in.begin()); i != in.end(); ++i) { | for (std::vector<uint8_t>::iterator i(in.begin()); i != in.end(); ++i) { | ||||
std::vector<uint8_t> sub(i, in.end()); | std::vector<uint8_t> sub(i, in.end()); | ||||
std::vector<uint8_t> subout(sub.size() + AES_BLOCKSIZE); | std::vector<uint8_t> subout(sub.size() + AES_BLOCKSIZE); | ||||
int _size = enc.Encrypt(&sub[0], sub.size(), &subout[0]); | int _size = enc.Encrypt(sub.data(), sub.size(), subout.data()); | ||||
if (_size != 0) { | if (_size != 0) { | ||||
subout.resize(_size); | subout.resize(_size); | ||||
std::vector<uint8_t> subdecrypted(subout.size()); | std::vector<uint8_t> subdecrypted(subout.size()); | ||||
_size = dec.Decrypt(&subout[0], subout.size(), &subdecrypted[0]); | _size = | ||||
dec.Decrypt(subout.data(), subout.size(), subdecrypted.data()); | |||||
subdecrypted.resize(_size); | subdecrypted.resize(_size); | ||||
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)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 483 Lines • Show Last 20 Lines |