From 657ebfae0cd3caaea6b14fa54f5a6e3b714216bd Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 27 Sep 2020 21:03:05 +0300 Subject: [PATCH] Improve AES CBC test. GitOrigin-RevId: 03d3cb464c49d9fed9db2104ed1ace3d72579140 --- tdutils/test/crypto.cpp | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/tdutils/test/crypto.cpp b/tdutils/test/crypto.cpp index 8d509f931..61617ac58 100644 --- a/tdutils/test/crypto.cpp +++ b/tdutils/test/crypto.cpp @@ -126,10 +126,10 @@ TEST(Crypto, AesIgeState) { } TEST(Crypto, AesCbcState) { - td::vector answers1{0u, 3617355989u, 3449188102u, 186999968u, 4244808847u}; + td::vector answers1{0u, 3617355989u, 3449188102u, 186999968u, 4244808847u, 2626031206u}; 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) { @@ -149,16 +149,34 @@ TEST(Crypto, AesCbcState) { } td::AesCbcState state(as_slice(key), as_slice(iv)); - //state.init(as_slice(key), as_slice(iv), true); td::string t(length, '\0'); - state.encrypt(s, t); + td::UInt128 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_cbc_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 = td::AesCbcState(as_slice(key), as_slice(iv)); - 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_cbc_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++; } }