As far as I can tell your key data is wonky. Consider this:
$ cat tmp.b64
L2I9fahDGX373HLMxN1xy5rHaq9gIiMJGOodpLwAiijQP0j0rzZLOMHks6YnLixRWoltfEYggHLuF5zDj2v2onfQ04pdR4NtvtX1va/v+cbiyfoM4Nf5babVeYrFhYEqoE79jPrarPqgRmJWD2xnc1gB5JShhVyFn/IkyYndNqSy2w0pRiNh9NOdP3JYbsuNFJAtQujhVC2oI7KdNvAYqfDfwH2hGRgoMO2rlQvly3nVPN+SejPbGjQrehWc6Tq1f+/hGa6TEU5XD2DRNRBqaLSZTiNke4SViiU/76dhUjgg4GEKPAFlJkh8otxhengevWSgEtC8E+/vvifHIJ3vnw==
$ base64 -D < tmp.b64 > tmp.dat
$ ls -l tmp.dat
-rw-r--r--@ 1 quinn staff 256 10 May 11:29 tmp.dat
Your code takes the file length of 256 and uses it as the key size in bits. However, a 256 bit EC key is not 256 bytes long. Consider the following code:
@import Foundation;
int main(int argc, char **argv) {
#pragma unused(argc)
#pragma unused(argv)
int retVal;
@autoreleasepool {
CFErrorRef error;
SecKeyRef privateKey = SecKeyCreateRandomKey((__bridge CFDictionaryRef) @{
(__bridge id) kSecAttrKeyType: (__bridge id) kSecAttrKeyTypeECSECPrimeRandom,
(__bridge id) kSecAttrKeySizeInBits: @256
}, &error);
assert(privateKey != NULL);
SecKeyRef publicKey = SecKeyCopyPublicKey(privateKey);
assert(publicKey != NULL);
NSData * publicKeyData = CFBridgingRelease( SecKeyCopyExternalRepresentation(publicKey, &error) );
NSLog(@"%@", publicKeyData);
NSLog(@"%zu", (size_t) publicKeyData.length);
CFRelease(publicKey);
CFRelease(privateKey);
retVal = EXIT_SUCCESS;
}
return retVal;
}
It prints:
… <04e1573d 3e8828b5 d0792606 35346edd e71c1479 82cf3a32 7a26d647 94dfab54 c2b3a91b 60a8da18 46875fda d095895c 0349abdd 1c755b27 fe0c33e0 4856e358 fd>
… 65
So, a 256 bit EC public key is 65 bytes long and starts with an 04. Your key looks nothing like that:
$ hexdump -Cv tmp.dat
00000000 2f 62 3d 7d a8 43 19 7d fb dc 72 cc c4 dd 71 cb |/b=}.C.}..r...q.|
00000010 9a c7 6a af 60 22 23 09 18 ea 1d a4 bc 00 8a 28 |..j.`"#........(|
…
Normally folks run into this problem because their key is inside some ASN.1 wrapper. However that’s not the case here:
$ dumpasn1 -p tmp.dat
Unknown (Reserved) {
Unknown (Reserved) {
[8] {
GraphicString
'..r...q...j.`"#........(.?H..6K8....'.,QZ.m|F .r'
'.....k..w...]G.m...............m..y....*.N......'
'.FbV.lgsX.....\...$...6....)F'
Error: Inconsistent object length, 60 bytes difference.
}
Error: Inconsistent object length, 4 bytes difference.
}
Error: Inconsistent object length, 33 bytes difference.
}
Warning: Further data follows ASN.1 data at position 133.
So I don’t know where this key data comes from but it’s not 256 EC public key. I recommend you have a chat with whoever gave you this key data.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"