Improve AES IGE test.

GitOrigin-RevId: b0b0ac715b062b339f7d64c4695b0fb99b78b409
This commit is contained in:
levlam 2020-09-27 22:03:00 +03:00
parent 705102cba0
commit 6278121549

View File

@ -89,10 +89,10 @@ TEST(Crypto, AesCtrState) {
}
TEST(Crypto, AesIgeState) {
td::vector<td::uint32> answers1{0u, 2045698207u, 2423540300u, 525522475u, 1545267325u};
td::vector<td::uint32> answers1{0u, 2045698207u, 2423540300u, 525522475u, 1545267325u, 724143417u};
std::size_t i = 0;
for (auto length : {0, 16, 32, 256, 1024}) {
for (auto length : {0, 16, 32, 256, 1024, 65536}) {
td::uint32 seed = length;
td::string s(length, '\0');
for (auto &c : s) {
@ -114,13 +114,33 @@ TEST(Crypto, AesIgeState) {
td::AesIgeState state;
state.init(as_slice(key), as_slice(iv), true);
td::string t(length, '\0');
state.encrypt(s, t);
td::UInt256 iv_copy = iv;
td::string u(length, '\0');
std::size_t pos = 0;
for (auto str : td::rand_split(td::string(length / 16, '\0'))) {
auto len = 16 * str.size();
state.encrypt(td::Slice(s).substr(pos, len), td::MutableSlice(t).substr(pos, len));
td::aes_ige_encrypt(as_slice(key), as_slice(iv_copy), td::Slice(s).substr(pos, len),
td::MutableSlice(u).substr(pos, len));
pos += len;
}
ASSERT_EQ(answers1[i], td::crc32(t));
ASSERT_EQ(answers1[i], td::crc32(u));
state.init(as_slice(key), as_slice(iv), false);
state.decrypt(t, t);
iv_copy = iv;
pos = 0;
for (auto str : td::rand_split(td::string(length / 16, '\0'))) {
auto len = 16 * str.size();
state.decrypt(td::Slice(t).substr(pos, len), td::MutableSlice(t).substr(pos, len));
td::aes_ige_decrypt(as_slice(key), as_slice(iv_copy), td::Slice(u).substr(pos, len),
td::MutableSlice(u).substr(pos, len));
pos += len;
}
ASSERT_STREQ(td::base64_encode(s), td::base64_encode(t));
ASSERT_STREQ(td::base64_encode(s), td::base64_encode(u));
i++;
}
}