From a6dc50b07e6db1e87f644d274647f2f7fc2ca150 Mon Sep 17 00:00:00 2001 From: fmodf Date: Tue, 27 Aug 2024 15:28:46 +0200 Subject: [PATCH] wip --- .../AppData/Services/AESGSMEngine.swift | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/ConversationsClassic/AppData/Services/AESGSMEngine.swift b/ConversationsClassic/AppData/Services/AESGSMEngine.swift index 0c9b26a..114514f 100644 --- a/ConversationsClassic/AppData/Services/AESGSMEngine.swift +++ b/ConversationsClassic/AppData/Services/AESGSMEngine.swift @@ -6,13 +6,42 @@ final class AESGSMEngine: AES_GCM_Engine { static let shared = AESGSMEngine() private init() {} + func encrypt(iv: Data, key: Data, message: Data, output: UnsafeMutablePointer?, tag: UnsafeMutablePointer?) -> Bool { - print(iv, key, message, output, tag) - return false + do { + let symmetricKey = SymmetricKey(data: key) + let sealedBox = try AES.GCM.seal(message, using: symmetricKey, nonce: AES.GCM.Nonce(data: iv)) + + if let output = output { + output.pointee = sealedBox.ciphertext + } + if let tag = tag { + tag.pointee = sealedBox.tag + } + return true + } catch { + print("Encryption error: \(error)") + return false + } } func decrypt(iv: Data, key: Data, encoded: Data, auth tag: Data?, output: UnsafeMutablePointer?) -> Bool { - print(iv, key, encoded, tag, output) - return false + do { + let symmetricKey = SymmetricKey(data: key) + guard let tag = tag else { + print("Tag is missing") + return false + } + let sealedBox = try AES.GCM.SealedBox(nonce: AES.GCM.Nonce(data: iv), ciphertext: encoded, tag: tag) + let decryptedData = try AES.GCM.open(sealedBox, using: symmetricKey) + + if let output = output { + output.pointee = decryptedData + } + return true + } catch { + print("Decryption error: \(error)") + return false + } } }