From 76d80d94df6af9ce08ac576360adfaa145169066 Mon Sep 17 00:00:00 2001 From: andy Date: Mon, 25 Feb 2019 11:54:48 +0000 Subject: [PATCH] fix: Added deallocate functions --- Sources/CryptorRSA/CryptorRSA.swift | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Sources/CryptorRSA/CryptorRSA.swift b/Sources/CryptorRSA/CryptorRSA.swift index 31974ec..5398391 100644 --- a/Sources/CryptorRSA/CryptorRSA.swift +++ b/Sources/CryptorRSA/CryptorRSA.swift @@ -260,6 +260,21 @@ public class CryptorRSA { let iv = UnsafeMutablePointer.allocate(capacity: Int(IVLength)) let encrypted = UnsafeMutablePointer.allocate(capacity: self.data.count + Int(IVLength)) + + defer { + #if swift(>=4.1) + ek.deallocate() + ekPtr.deallocate() + iv.deallocate() + encrypted.deallocate() + #else + ek.deallocate(capacity: Int(EVP_PKEY_size(evp_key))) + ekPtr.deallocate(capacity: MemoryLayout.size) + iv.deallocate(capacity: Int(IVLength)) + encrypted.deallocate(capacity: self.data.count + Int(IVLength)) + #endif + } + var encKeyLength: Int32 = 0 var processedLength: Int32 = 0 var encLength: Int32 = 0 @@ -392,6 +407,14 @@ public class CryptorRSA { let decrypted = UnsafeMutablePointer.allocate(capacity: Int(encryptedData.count + encryptedIV.count)) + defer { + #if swift(>=4.1) + decrypted.deallocate() + #else + decrypted.deallocate(capacity: capacity: Int(encryptedData.count + encryptedIV.count)) + #endif + } + // EVP_OpenInit returns 0 on error or the recovered secret key size if successful status = encryptedKey.withUnsafeBytes({ (ek: UnsafePointer) -> Int32 in return encryptedIV.withUnsafeBytes({ (iv: UnsafePointer) -> Int32 in @@ -513,6 +536,14 @@ public class CryptorRSA { EVP_DigestSignFinal(md_ctx, nil, &sig_len) let sig = UnsafeMutablePointer.allocate(capacity: sig_len) + defer { + #if swift(>=4.1) + sig.deallocate() + #else + sig.deallocate(capacity: sig_len) + #endif + } + rc = EVP_DigestSignFinal(md_ctx, sig, &sig_len) guard rc == 1, sig_len > 0 else { let source = "Signing failed."