| 147 | } |
| 148 | |
| 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 | |
| 190 | static void TestChaCha20(const std::string &hexkey, uint64_t nonce, uint64_t seek, const std::string& hexout) |
| 191 | { |