From 6278121549d916bfce3cc018075ccc6bac5bfe51 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 27 Sep 2020 22:03:00 +0300 Subject: [PATCH] Improve AES IGE test. GitOrigin-RevId: b0b0ac715b062b339f7d64c4695b0fb99b78b409 --- tdutils/test/crypto.cpp | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/tdutils/test/crypto.cpp b/tdutils/test/crypto.cpp index 61617ac58..1a634b2e1 100644 --- a/tdutils/test/crypto.cpp +++ b/tdutils/test/crypto.cpp @@ -89,10 +89,10 @@ TEST(Crypto, AesCtrState) { } TEST(Crypto, AesIgeState) { - td::vector answers1{0u, 2045698207u, 2423540300u, 525522475u, 1545267325u}; + td::vector 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++; } }