From 7c3ad106faf1eea267d350621c5e0dc666f6b905 Mon Sep 17 00:00:00 2001 From: Portalis Date: Sun, 27 Dec 2020 15:23:26 +0100 Subject: [PATCH 1/4] Add / strip header to / from public PEM --- SwCrypt/SwCrypt.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SwCrypt/SwCrypt.swift b/SwCrypt/SwCrypt.swift index 40a7504..adb4f10 100644 --- a/SwCrypt/SwCrypt.swift +++ b/SwCrypt/SwCrypt.swift @@ -436,11 +436,11 @@ open class PEM { fileprivate static let pemPrefix = "-----BEGIN PUBLIC KEY-----\n" fileprivate static let pemSuffix = "\n-----END PUBLIC KEY-----" - fileprivate static func addHeader(_ base64: String) -> String { + public static func addHeader(_ base64: String) -> String { return pemPrefix + base64 + pemSuffix } - fileprivate static func stripHeader(_ pemKey: String) -> String? { + public static func stripHeader(_ pemKey: String) -> String? { return PEM.stripHeaderFooter(pemKey, header: pemPrefix, footer: pemSuffix) } } From aa9bde30e1b389ee81ca2073b5fe6931bc282e2a Mon Sep 17 00:00:00 2001 From: Portalis Date: Thu, 15 Apr 2021 08:42:17 +0200 Subject: [PATCH 2/4] Use Swift 3+ to get rid of dangling pointer warning --- SwCrypt/SwCrypt.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SwCrypt/SwCrypt.swift b/SwCrypt/SwCrypt.swift index adb4f10..c34f645 100644 --- a/SwCrypt/SwCrypt.swift +++ b/SwCrypt/SwCrypt.swift @@ -2078,7 +2078,7 @@ open class CC { open class KeyWrap { fileprivate static let rfc3394IVData: [UInt8] = [0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6] - public static let rfc3394IV = Data(bytes: UnsafePointer(rfc3394IVData), count:rfc3394IVData.count) + public static let rfc3394IV = Data(rfc3394IVData) public static func SymmetricKeyWrap(_ iv: Data, kek: Data, From 7120412d9b9492da6a5c80c4449a303c3347db18 Mon Sep 17 00:00:00 2001 From: Portalis GmbH Date: Tue, 20 Apr 2021 09:43:46 +0200 Subject: [PATCH 3/4] Update unsafe pointers to comply with Swift 5 --- SwCrypt/SwCrypt.swift | 76 +++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/SwCrypt/SwCrypt.swift b/SwCrypt/SwCrypt.swift index c34f645..678750f 100644 --- a/SwCrypt/SwCrypt.swift +++ b/SwCrypt/SwCrypt.swift @@ -624,7 +624,7 @@ open class CC { public static func generateRandom(_ size: Int) -> Data { var data = Data(count: size) data.withUnsafeMutableBytes { dataBytes in - _ = CCRandomGenerateBytes!(dataBytes, size) + _ = CCRandomGenerateBytes!(dataBytes.baseAddress!.assumingMemoryBound(to: CCRandomGenerateBytesT.self), size) } return data } @@ -761,7 +761,7 @@ open class CC { status = result.withUnsafeMutableBytes { resultBytes in return CCCryptorFinal!( cryptor!, - resultBytes + updateLen, + resultBytes.baseAddress!.assumingMemoryBound(to: UInt8.self) + updateLen, rescount - updateLen, &finalLen) } @@ -955,7 +955,7 @@ open class CC { return CCCryptorCreateWithMode!( opMode.rawValue, AuthBlockMode.ccm.rawValue, algorithm.rawValue, Padding.noPadding.rawValue, - nil, keyBytes, key.count, nil, 0, + nil, keyBytes.baseAddress!.assumingMemoryBound(to: UInt8.self), key.count, nil, 0, 0, CCModeOptions(), &cryptor) } guard status == noErr else { throw CCError(status) } @@ -969,12 +969,12 @@ open class CC { guard status == noErr else { throw CCError(status) } status = iv.withUnsafeBytes { ivBytes in - return CCCryptorAddParameter!(cryptor!, Parameter.iv.rawValue, ivBytes, iv.count) + return CCCryptorAddParameter!(cryptor!, Parameter.iv.rawValue, ivBytes.baseAddress!.assumingMemoryBound(to: UInt8.self), iv.count) } guard status == noErr else { throw CCError(status) } status = aData.withUnsafeBytes { aDataBytes in - return CCCryptorAddParameter!(cryptor!, Parameter.authData.rawValue, aDataBytes, aData.count) + return CCCryptorAddParameter!(cryptor!, Parameter.authData.rawValue, aDataBytes.baseAddress!.assumingMemoryBound(to: UInt8.self), aData.count) } guard status == noErr else { throw CCError(status) } @@ -991,7 +991,7 @@ open class CC { var finalLen: size_t = 0 status = result.withUnsafeMutableBytes { resultBytes in - return CCCryptorFinal!(cryptor!, resultBytes + updateLen, + return CCCryptorFinal!(cryptor!, resultBytes.baseAddress!.assumingMemoryBound(to: UInt8.self) + updateLen, rescount - updateLen, &finalLen) } @@ -1003,7 +1003,7 @@ open class CC { var tag = Data(count: tagLength) status = tag.withUnsafeMutableBytes { tagBytes in return CCCryptorGetParameter!(cryptor!, Parameter.authTag.rawValue, - tagBytes, &tagLength_) + tagBytes.baseAddress!.assumingMemoryBound(to: UInt8.self), &tagLength_) } guard status == noErr else { throw CCError(status) } @@ -1143,7 +1143,7 @@ open class CC { var key: CCRSACryptorRef? = nil let status = derKey.withUnsafeBytes { derKeyBytes in return CCRSACryptorImport!( - derKeyBytes, + derKeyBytes.baseAddress!.assumingMemoryBound(to: UInt8.self), derKey.count, &key) } @@ -1156,7 +1156,7 @@ open class CC { var derKeyLength = 8192 var derKey = Data(count: derKeyLength) let status = derKey.withUnsafeMutableBytes { derKeyBytes in - return CCRSACryptorExport!(key, derKeyBytes, &derKeyLength) + return CCRSACryptorExport!(key, derKeyBytes.baseAddress!.assumingMemoryBound(to: UInt8.self), &derKeyLength) } guard status == noErr else { throw CCError(status) } @@ -1346,7 +1346,7 @@ open class CC { let zeroBits = 8 * emLength - emBits maskedDB.withUnsafeMutableBytes { maskedDBBytes in - maskedDBBytes[0] &= 0xff >> UInt8(zeroBits) + maskedDBBytes.baseAddress!.assumingMemoryBound(to: UInt8.self)[0] &= 0xff >> UInt8(zeroBits) } var ret = maskedDB @@ -1392,7 +1392,7 @@ open class CC { let dbMask = mgf1(digest, seed: mPrimeHash, maskLength: emLength - hash.count - 1) var db = xorData(maskedDB, dbMask) db.withUnsafeMutableBytes { dbBytes in - dbBytes[0] &= 0xff >> UInt8(zeroBits) + dbBytes.baseAddress!.assumingMemoryBound(to: UInt8.self)[0] &= 0xff >> UInt8(zeroBits) } let zeroLength = emLength - hash.count - saltLength - 2 @@ -1559,7 +1559,7 @@ open class CC { var outputLength = 8192 var output = Data(count: outputLength) let status = output.withUnsafeMutableBytes { outputBytes in - return CCDHGenerateKey!(ref!, outputBytes, &outputLength) + return CCDHGenerateKey!(ref!, outputBytes.baseAddress!.assumingMemoryBound(to: UInt8.self), &outputLength) } output.count = outputLength guard status != -1 else { @@ -1727,7 +1727,7 @@ open class CC { var outSize = 8192 var result = Data(count:outSize) let status = result.withUnsafeMutableBytes { resultBytes in - return CCECCryptorComputeSharedSecret!(privKey, pubKey, resultBytes, &outSize) + return CCECCryptorComputeSharedSecret!(privKey, pubKey, resultBytes.baseAddress!.assumingMemoryBound(to: UInt8.self), &outSize) } guard status == noErr else { throw CCError(status) } @@ -1800,7 +1800,7 @@ open class CC { var impKey: CCECCryptorRef? = nil let status = key.withUnsafeBytes { keyBytes in return CCECCryptorImportKey!(format.rawValue, - keyBytes, key.count, + keyBytes.baseAddress!.assumingMemoryBound(to: UInt8.self), key.count, keyType.rawValue, &impKey) } guard status == noErr else { throw CCError(status) } @@ -1815,7 +1815,7 @@ open class CC { let status = expKey.withUnsafeMutableBytes { expKeyBytes in return CCECCryptorExportKey!( format.rawValue, - expKeyBytes, + expKeyBytes.baseAddress!.assumingMemoryBound(to: UInt8.self), &expKeyLength, type.rawValue, key) @@ -1978,7 +1978,7 @@ open class CC { let status = input.withUnsafeBytes { inputBytes in return CNCRC!( mode.rawValue, - inputBytes, input.count, + inputBytes.baseAddress!.assumingMemoryBound(to: UInt8.self), input.count, &result) } guard status == noErr else { @@ -2184,8 +2184,8 @@ extension Data { public func hexadecimalString() -> String { var hexstr = String() - self.withUnsafeBytes {(data: UnsafePointer) in - for i in UnsafeBufferPointer(start: data, count: count) { + self.withUnsafeBytes { data in + for i in UnsafeBufferPointer(start: data.baseAddress!.assumingMemoryBound(to: UInt8.self), count: count) { hexstr += String(format: "%02X", i) } } @@ -2224,7 +2224,7 @@ extension Data { subscript (position: Int) -> UInt8 { var value: UInt8 = 0 data.withUnsafeBytes { dataBytes in - value = UnsafeBufferPointer(start: dataBytes, count: data.count)[position] + value = UnsafeBufferPointer(start: dataBytes.baseAddress!.assumingMemoryBound(to: UInt8.self), count: data.count)[position] } return value } @@ -2302,7 +2302,10 @@ fileprivate func withUnsafePointers( ) rethrows -> Result { return try arg0.withUnsafeBytes { p0 in return try arg1.withUnsafeBytes { p1 in - return try body(p0, p1) + return try body( + p0.baseAddress!.assumingMemoryBound(to: A0.self), + p1.baseAddress!.assumingMemoryBound(to: A1.self) + ) } } } @@ -2316,7 +2319,10 @@ fileprivate func withUnsafePointers( ) rethrows -> Result { return try arg0.withUnsafeBytes { p0 in return try arg1.withUnsafeMutableBytes { p1 in - return try body(p0, p1) + return try body( + p0.baseAddress!.assumingMemoryBound(to: A0.self), + p1.baseAddress!.assumingMemoryBound(to: A1.self) + ) } } } @@ -2333,7 +2339,11 @@ fileprivate func withUnsafePointers( return try arg0.withUnsafeBytes { p0 in return try arg1.withUnsafeBytes { p1 in return try arg2.withUnsafeMutableBytes { p2 in - return try body(p0, p1, p2) + return try body( + p0.baseAddress!.assumingMemoryBound(to: A0.self), + p1.baseAddress!.assumingMemoryBound(to: A1.self), + p2.baseAddress!.assumingMemoryBound(to: A2.self) + ) } } } @@ -2351,7 +2361,11 @@ fileprivate func withUnsafePointers( return try arg0.withUnsafeMutableBytes { p0 in return try arg1.withUnsafeMutableBytes { p1 in return try arg2.withUnsafeMutableBytes { p2 in - return try body(p0, p1, p2) + return try body( + p0.baseAddress!.assumingMemoryBound(to: A0.self), + p1.baseAddress!.assumingMemoryBound(to: A1.self), + p2.baseAddress!.assumingMemoryBound(to: A2.self) + ) } } } @@ -2372,7 +2386,12 @@ fileprivate func withUnsafePointers( return try arg1.withUnsafeBytes { p1 in return try arg2.withUnsafeBytes { p2 in return try arg3.withUnsafeMutableBytes { p3 in - return try body(p0, p1, p2, p3) + return try body( + p0.baseAddress!.assumingMemoryBound(to: A0.self), + p1.baseAddress!.assumingMemoryBound(to: A1.self), + p2.baseAddress!.assumingMemoryBound(to: A2.self), + p3.baseAddress!.assumingMemoryBound(to: A3.self) + ) } } } @@ -2400,7 +2419,14 @@ fileprivate func withUnsafePointers( return try arg3.withUnsafeBytes { p3 in return try arg4.withUnsafeMutableBytes { p4 in return try arg5.withUnsafeMutableBytes { p5 in - return try body(p0, p1, p2, p3, p4, p5) + return try body( + p0.baseAddress!.assumingMemoryBound(to: A0.self), + p1.baseAddress!.assumingMemoryBound(to: A1.self), + p2.baseAddress!.assumingMemoryBound(to: A2.self), + p3.baseAddress!.assumingMemoryBound(to: A3.self), + p4.baseAddress!.assumingMemoryBound(to: A4.self), + p5.baseAddress!.assumingMemoryBound(to: A5.self) + ) } } } From e9178e23702615e887b3b14fca0c0cdfc3ae683c Mon Sep 17 00:00:00 2001 From: Portalis GmbH Date: Tue, 20 Apr 2021 09:50:57 +0200 Subject: [PATCH 4/4] Update podspec to Swift 5 --- SwCrypt.podspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SwCrypt.podspec b/SwCrypt.podspec index 6d774c3..14c3d69 100644 --- a/SwCrypt.podspec +++ b/SwCrypt.podspec @@ -1,11 +1,11 @@ Pod::Spec.new do |spec| spec.name = "SwCrypt" - spec.version = "5.1.3" + spec.version = "5.1.4" spec.summary = "RSA public/private key generation, RSA, AES encryption/decryption, RSA sign/verify in Swift with CommonCrypto in iOS and OS X" spec.homepage = "https://github.com/soyersoyer/SwCrypt" spec.license = { :type => 'MIT' } spec.authors = { "soyersoyer" => 'soyer@irl.hu' } - spec.swift_version = "4.2" + spec.swift_version = "5.0" spec.ios.deployment_target = "8.0" spec.osx.deployment_target = "10.12" spec.watchos.deployment_target = "3.0"