I'm trying to write an app which reads a value from a mifare DESFire card.
I'm using Swift and Core NFC and have managed to successfully connect to the card. I've also managed to send a mifare command to select the relevant application.
However, I'm hitting problems trying to authenticate with the application. I'm trying to use CommonCrypto to perform the 3DES encrypt/decrypt, but I'm way outside my comfort zone when dealing with pointers!
I have a 16 byte key which is the "secret" for authentication, stored as readKey
.
I've then tried to write a function to decrypt a data object received from the card:
func decrypt(_ data: Data)
{
var numberOfBytesDecrypted: size_t = 0
let bufferSize: size_t = data.count
let buffer = UnsafeMutablePointer<NSData>.allocate(capacity: bufferSize)
let cryptoResult: CCCryptorStatus = CCCrypt(CCOperation(kCCDecrypt),
CCAlgorithm(kCCAlgorithm3DES),
CCOptions(kCCOptionPKCS7Padding),
CFDataGetBytePtr(readKey as CFData),
CFDataGetLength(readKey as CFData),
nil,
CFDataGetBytePtr(data as CFData),
CFDataGetLength(data as CFData),
buffer,
bufferSize,
&numberOfBytesDecrypted)
if cryptoResult == kCCSuccess
{
print("Successful decryption... I think")
}
else
{
print("An error occurred when trying to decrypt.")
}
}
...but I always get the error message when trying to call the function. I'm really not familiar with CommonCrypto and have been flailing wildly, trying different variations of the above in the hope something will work but would really appreciate some input from someone who actually knows what they're doing .