| 90 | } |
| 91 | |
| 92 | static void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, bool pad, const std::string &hexin, const std::string &hexout) |
| 93 | { |
| 94 | std::vector<unsigned char> key = ParseHex(hexkey); |
| 95 | std::vector<unsigned char> iv = ParseHex(hexiv); |
| 96 | std::vector<unsigned char> in = ParseHex(hexin); |
| 97 | std::vector<unsigned char> correctout = ParseHex(hexout); |
| 98 | std::vector<unsigned char> realout(in.size() + AES_BLOCKSIZE); |
| 99 | |
| 100 | // Encrypt the plaintext and verify that it equals the cipher |
| 101 | AES256CBCEncrypt enc(key.data(), iv.data(), pad); |
| 102 | int size = enc.Encrypt(in.data(), in.size(), realout.data()); |
| 103 | realout.resize(size); |
| 104 | BOOST_CHECK(realout.size() == correctout.size()); |
| 105 | BOOST_CHECK_MESSAGE(realout == correctout, HexStr(realout) + std::string(" != ") + hexout); |
| 106 | |
| 107 | // Decrypt the cipher and verify that it equals the plaintext |
| 108 | std::vector<unsigned char> decrypted(correctout.size()); |
| 109 | AES256CBCDecrypt dec(key.data(), iv.data(), pad); |
| 110 | size = dec.Decrypt(correctout.data(), correctout.size(), decrypted.data()); |
| 111 | decrypted.resize(size); |
| 112 | BOOST_CHECK(decrypted.size() == in.size()); |
| 113 | BOOST_CHECK_MESSAGE(decrypted == in, HexStr(decrypted) + std::string(" != ") + hexin); |
| 114 | |
| 115 | // Encrypt and re-decrypt substrings of the plaintext and verify that they equal each-other |
| 116 | for(std::vector<unsigned char>::iterator i(in.begin()); i != in.end(); ++i) |
| 117 | { |
| 118 | std::vector<unsigned char> sub(i, in.end()); |
| 119 | std::vector<unsigned char> subout(sub.size() + AES_BLOCKSIZE); |
| 120 | int _size = enc.Encrypt(sub.data(), sub.size(), subout.data()); |
| 121 | if (_size != 0) |
| 122 | { |
| 123 | subout.resize(_size); |
| 124 | std::vector<unsigned char> subdecrypted(subout.size()); |
| 125 | _size = dec.Decrypt(subout.data(), subout.size(), subdecrypted.data()); |
| 126 | subdecrypted.resize(_size); |
| 127 | BOOST_CHECK(decrypted.size() == in.size()); |
| 128 | BOOST_CHECK_MESSAGE(subdecrypted == sub, HexStr(subdecrypted) + std::string(" != ") + HexStr(sub)); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static void TestChaCha20(const std::string &hex_message, const std::string &hexkey, uint64_t nonce, uint64_t seek, const std::string& hexout) |
| 134 | { |