mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
18 lines
827 B
Swift
18 lines
827 B
Swift
import CryptoSwift
|
|
|
|
enum DecryptionUtilities {
|
|
|
|
/// - Note: Sync. Don't call from the main thread.
|
|
internal static func decrypt(_ ivAndCiphertext: Data, usingAESGCMWithSymmetricKey symmetricKey: Data) throws -> Data {
|
|
if Thread.isMainThread {
|
|
#if DEBUG
|
|
preconditionFailure("It's illegal to call decrypt(_:usingAESGCMWithSymmetricKey:) from the main thread.")
|
|
#endif
|
|
}
|
|
let iv = ivAndCiphertext[0..<Int(EncryptionUtilities.ivSize)]
|
|
let ciphertext = ivAndCiphertext[Int(EncryptionUtilities.ivSize)...]
|
|
let gcm = GCM(iv: iv.bytes, tagLength: Int(EncryptionUtilities.gcmTagSize), mode: .combined)
|
|
let aes = try AES(key: symmetricKey.bytes, blockMode: gcm, padding: .noPadding)
|
|
return Data(try aes.decrypt(ciphertext.bytes))
|
|
}
|
|
}
|