Skip to content

Commit

Permalink
fix: Added deallocate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Lees11 committed Feb 25, 2019
1 parent db01492 commit 76d80d9
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Sources/CryptorRSA/CryptorRSA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,21 @@ public class CryptorRSA {
let iv = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(IVLength))

let encrypted = UnsafeMutablePointer<UInt8>.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<UInt8Ptr>.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
Expand Down Expand Up @@ -392,6 +407,14 @@ public class CryptorRSA {

let decrypted = UnsafeMutablePointer<UInt8>.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<UInt8>) -> Int32 in
return encryptedIV.withUnsafeBytes({ (iv: UnsafePointer<UInt8>) -> Int32 in
Expand Down Expand Up @@ -513,6 +536,14 @@ public class CryptorRSA {
EVP_DigestSignFinal(md_ctx, nil, &sig_len)
let sig = UnsafeMutablePointer<UInt8>.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."
Expand Down

0 comments on commit 76d80d9

Please sign in to comment.