I am using Elliptic Curve keys generated using SecKeyCreateRandomKey from the Security framework for performing Diffe Helman Key exchange in my app. After generating the key pair I am converting the Private key into data using SecKeyCopyExternalRepresentation(<myPrivateKey>,&error)
and then store the base64 encoded string of this in the Keystore. To convert the Private key back I am using the following code
let priKeyData = Data(base64Encoded: priKeyStr)
if(priKeyData != nil) {
guard let dataPtr = CFDataCreate(kCFAllocatorDefault, UnsafePointer<UInt8>(Array(priKeyData!)), priKeyData!.count)
else {return (nil) }
guard let priKey = SecKeyCreateWithData(dataPtr as CFData,attributes as CFDictionary,&error)
else {return (nil)}
}
This works totally fine on iOS version prior 16.1. But in devices with iOS 16.1 the conversion from data to SecKey is failing with the following error:
Swift.Unmanaged<__C.CFErrorRef>(_value: Error Domain=NSOSStatusErrorDomain Code=-50 "EC private key creation from data failed" UserInfo={numberOfErrorsDeep=0, NSDescription=EC private key creation from data failed}))
What could be the reason for this? is there any other way to store SecKey in the Keystore without doing the conversions?