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