From 466de70edda08faf8d946d0c282ef153340581f3 Mon Sep 17 00:00:00 2001 From: Isaac <> Date: Fri, 13 Feb 2026 18:10:16 +0400 Subject: [PATCH] constTimeIsEqual --- .../SecretChats/SecretChatEncryption.swift | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/submodules/TelegramCore/Sources/SecretChats/SecretChatEncryption.swift b/submodules/TelegramCore/Sources/SecretChats/SecretChatEncryption.swift index 90b410ebff..b46bcf7a00 100644 --- a/submodules/TelegramCore/Sources/SecretChats/SecretChatEncryption.swift +++ b/submodules/TelegramCore/Sources/SecretChats/SecretChatEncryption.swift @@ -119,6 +119,19 @@ private func messageKey(key: SecretChatKey, msgKey: UnsafeRawPointer, mode: Secr } } +private func constTimeIsEqual(data1: Data, data2: Data) -> Bool { + if data1.count != data2.count { + return false + } + var isEqual = true + for i in 0 ..< data1.count { + if data1[i] != data2[i] { + isEqual = false + } + } + return isEqual +} + func withDecryptedMessageContents(parameters: SecretChatEncryptionParameters, data: MemoryBuffer) -> MemoryBuffer? { assert(parameters.key.key.length == 256) @@ -211,7 +224,7 @@ func withDecryptedMessageContents(parameters: SecretChatEncryptionParameters, da if Int(payloadLength) <= 0 || Int(payloadLength) > decryptedData.count - 4 || paddingLength < 12 || paddingLength > 1024 { - if localMessageKey != msgKeyData { + if !constTimeIsEqual(data1: localMessageKey, data2: msgKeyData) { Logger.shared.log("SecretChatEncryption", "message key doesn't match (length check)") }