From 1945952864ef87a6afeb57b0beb80756d0647381 Mon Sep 17 00:00:00 2001 From: Nicolas Werner <nicolas.werner@hotmail.de> Date: Thu, 12 Dec 2019 22:58:08 +0100 Subject: [PATCH] Fix encryption bug, which made media unencryptable in Riot --- lib/crypto/utils.cpp | 4 ++-- tests/e2ee.cpp | 4 ++++ tests/events.cpp | 6 +++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/crypto/utils.cpp b/lib/crypto/utils.cpp index cd5b79e26..0b2f03176 100644 --- a/lib/crypto/utils.cpp +++ b/lib/crypto/utils.cpp @@ -203,9 +203,9 @@ encrypt_file(const std::string &plaintext) { mtx::crypto::EncryptedFile encryption_info; - // not sure if 16 bytes would be enough, 32 seems to be safe though + // iv has to be 16 bytes, key 32! BinaryBuf key = create_buffer(32); - BinaryBuf iv = create_buffer(32); + BinaryBuf iv = create_buffer(16); BinaryBuf cyphertext = AES_CTR_256_Encrypt(plaintext, key, iv); diff --git a/tests/e2ee.cpp b/tests/e2ee.cpp index baf58583d..303908ff0 100644 --- a/tests/e2ee.cpp +++ b/tests/e2ee.cpp @@ -1139,6 +1139,10 @@ TEST(Encryption, EncryptedFile) ASSERT_EQ(plaintext, mtx::crypto::to_string(mtx::crypto::decrypt_file( mtx::crypto::to_string(encryption_data.first), encryption_data.second))); + // IV needs to be 16 bytes/128 bits + ASSERT_EQ(16, encryption_data.second.iv.size()); + // key needs to be 32 bytes/256 bits + ASSERT_EQ(32, encryption_data.second.key.k.size()); json j = R"({ "type": "m.room.message", diff --git a/tests/events.cpp b/tests/events.cpp index 62de2d710..44eed1675 100644 --- a/tests/events.cpp +++ b/tests/events.cpp @@ -347,7 +347,7 @@ TEST(StateEvents, JoinRules) "type": "m.room.join_rules" })"_json; - EXPECT_EQ(data, json(ns::StateEvent<ns::state::JoinRules>(data))); + EXPECT_EQ(data, json(ns::StateEvent<ns::state::JoinRules>(data))); data = R"({ "origin_server_ts": 1506761924018, @@ -363,7 +363,7 @@ TEST(StateEvents, JoinRules) "type": "m.room.join_rules" })"_json; - EXPECT_EQ(data, json(ns::StateEvent<ns::state::JoinRules>(data))); + EXPECT_EQ(data, json(ns::StateEvent<ns::state::JoinRules>(data))); data = R"({ "origin_server_ts": 1506761924018, @@ -379,7 +379,7 @@ TEST(StateEvents, JoinRules) "type": "m.room.join_rules" })"_json; - EXPECT_EQ(data, json(ns::StateEvent<ns::state::JoinRules>(data))); + EXPECT_EQ(data, json(ns::StateEvent<ns::state::JoinRules>(data))); } TEST(StateEvents, Member) -- GitLab