Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions SwCrypt.podspec
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
82 changes: 54 additions & 28 deletions SwCrypt/SwCrypt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) }
Expand All @@ -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) }

Expand All @@ -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)
}
Expand All @@ -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) }

Expand Down Expand Up @@ -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)
}
Expand All @@ -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) }

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) }

Expand Down Expand Up @@ -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) }
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<UInt8>(rfc3394IVData), count:rfc3394IVData.count)
public static let rfc3394IV = Data(rfc3394IVData)

public static func SymmetricKeyWrap(_ iv: Data,
kek: Data,
Expand Down Expand Up @@ -2184,8 +2184,8 @@ extension Data {

public func hexadecimalString() -> String {
var hexstr = String()
self.withUnsafeBytes {(data: UnsafePointer<UInt8>) in
for i in UnsafeBufferPointer<UInt8>(start: data, count: count) {
self.withUnsafeBytes { data in
for i in UnsafeBufferPointer<UInt8>(start: data.baseAddress!.assumingMemoryBound(to: UInt8.self), count: count) {
hexstr += String(format: "%02X", i)
}
}
Expand Down Expand Up @@ -2224,7 +2224,7 @@ extension Data {
subscript (position: Int) -> UInt8 {
var value: UInt8 = 0
data.withUnsafeBytes { dataBytes in
value = UnsafeBufferPointer<UInt8>(start: dataBytes, count: data.count)[position]
value = UnsafeBufferPointer<UInt8>(start: dataBytes.baseAddress!.assumingMemoryBound(to: UInt8.self), count: data.count)[position]
}
return value
}
Expand Down Expand Up @@ -2302,7 +2302,10 @@ fileprivate func withUnsafePointers<A0, A1, Result>(
) 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)
)
}
}
}
Expand All @@ -2316,7 +2319,10 @@ fileprivate func withUnsafePointers<A0, A1, Result>(
) 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)
)
}
}
}
Expand All @@ -2333,7 +2339,11 @@ fileprivate func withUnsafePointers<A0, A1, A2, Result>(
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)
)
}
}
}
Expand All @@ -2351,7 +2361,11 @@ fileprivate func withUnsafePointers<A0, A1, A2, Result>(
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)
)
}
}
}
Expand All @@ -2372,7 +2386,12 @@ fileprivate func withUnsafePointers<A0, A1, A2, A3, Result>(
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)
)
}
}
}
Expand Down Expand Up @@ -2400,7 +2419,14 @@ fileprivate func withUnsafePointers<A0, A1, A2, A3, A4, A5, Result>(
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)
)
}
}
}
Expand Down