mv-experiment #1
|
@ -6,13 +6,42 @@ final class AESGSMEngine: AES_GCM_Engine {
|
||||||
static let shared = AESGSMEngine()
|
static let shared = AESGSMEngine()
|
||||||
|
|
||||||
private init() {}
|
private init() {}
|
||||||
|
|
||||||
func encrypt(iv: Data, key: Data, message: Data, output: UnsafeMutablePointer<Data>?, tag: UnsafeMutablePointer<Data>?) -> Bool {
|
func encrypt(iv: Data, key: Data, message: Data, output: UnsafeMutablePointer<Data>?, tag: UnsafeMutablePointer<Data>?) -> Bool {
|
||||||
print(iv, key, message, output, tag)
|
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
|
return false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func decrypt(iv: Data, key: Data, encoded: Data, auth tag: Data?, output: UnsafeMutablePointer<Data>?) -> Bool {
|
func decrypt(iv: Data, key: Data, encoded: Data, auth tag: Data?, output: UnsafeMutablePointer<Data>?) -> Bool {
|
||||||
print(iv, key, encoded, tag, output)
|
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
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue