I tested in 12.4 (Swift 5.2), and got error in
let status = wrappedKey.withUnsafeMutableBytes { (wrappedKeyBytes: UnsafeMutablePointer) in
Generic parameter 'Pointee' could not be inferred
Does this really compile with 5.2 for you ?
Note: code would be more readable with better formatting:
private static func ccAESKeyWrap(rawKey: Data, keyEncryptionKey: Data) - (data: Data?, status: Int32) {
let alg = CCWrappingAlgorithm(kCCWRAPAES)
var wrappedKeyLength: size_t = CCSymmetricWrappedSize(alg, rawKey.count)
var wrappedKey = Data(count: wrappedKeyLength)
let status = wrappedKey.withUnsafeMutableBytes { (wrappedKeyBytes: UnsafeMutablePointer) in
rawKey.withUnsafeBytes { (rawKeyBytes: UnsafePointer) in
keyEncryptionKey.withUnsafeBytes { (keyEncryptionKeyBytes: UnsafePointer) - Int32 in
guard
let wrappedKeyBytes = wrappedKeyBytes.bindMemory(to: UInt8.self).baseAddress,
let rawKeyBytes = rawKeyBytes.bindMemory(to: UInt8.self).baseAddress,
let keyEncryptionKeyBytes = keyEncryptionKeyBytes.bindMemory(to: UInt8.self).baseAddress
else {
return Int32(kCCMemoryFailure)
}
return CCSymmetricKeyWrap(
alg,
CCrfc3394_iv,
CCrfc3394_ivLen,
keyEncryptionKeyBytes,
keyEncryptionKey.count,
rawKeyBytes, rawKey.count,
wrappedKeyBytes, &wrappedKeyLength)
}
}
}
guard status == kCCSuccess else {
return (nil, status)
}
wrappedKey.removeSubrange(wrappedKeyLength..wrappedKey.count)
return (wrappedKey, status)
}
In Swift 5 (CryptoKit), there is an withUnsafeBytes, whose signature is:
func withUnsafeBytesR( body: (UnsafeRawBufferPointer) throws - R) rethrows- R
In Swift 4 and Swift 5 (Foundation), there is a func whose signature is:
func withUnsafeBytesResultType, ContentType( body: (UnsafePointerContentType) throws - ResultType) rethrows - ResultType
But not the CryptoKit one apparently.