Skip to content

Instantly share code, notes, and snippets.

@edisonlo
Last active March 14, 2020 03:53
Show Gist options
  • Save edisonlo/3bffb8d58726fa58ecc8396ef82f8175 to your computer and use it in GitHub Desktop.
Save edisonlo/3bffb8d58726fa58ecc8396ef82f8175 to your computer and use it in GitHub Desktop.
Obj-c verify ECDSA signature
- (IBAction)ecdsaAction:(id)sender {
NSData *privateKeyPEMData = [[NSFileManager defaultManager] contentsAtPath: [[NSBundle mainBundle] pathForResource:@"ec_private_key" ofType:@"p12" ]];
if(privateKeyPEMData){
NSLog(@"privateKeyPEMData NOT NULL");
SecKeyRef privateKey = [self getPrivateKeyRefrenceFromData:privateKeyPEMData password:@"12345678"];
if(privateKey){
NSLog(@"private key is NOT NULL!!");
SecKeyAlgorithm algorithm = kSecKeyAlgorithmECDSASignatureDigestX962SHA512;
BOOL canSign = SecKeyIsAlgorithmSupported(privateKey, kSecKeyOperationTypeSign, algorithm);
NSLog(@"canSign %i\n",canSign);
NSString *plainText = @"Hello World";
NSData *plainTextData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
NSData* signature = nil;
if (canSign) {
CFErrorRef error = NULL;
signature = (NSData*)CFBridgingRelease( // ARC takes ownership
SecKeyCreateSignature(privateKey,
algorithm,
(__bridge CFDataRef)plainTextData,
&error));
if (!signature) {
NSError *err = CFBridgingRelease(error); // ARC takes ownership
// Handle the error. . .
NSLog(@"failed to sign.\n");
}
}
NSString *sigString = [[NSString alloc]initWithData:signature encoding:NSASCIIStringEncoding];
NSLog(@"signature: %@",signature);
NSLog(@"sigString: %@", sigString);
NSData *publicKeyPEMData = [[NSFileManager defaultManager] contentsAtPath: [[NSBundle mainBundle] pathForResource:@"ec_public_key" ofType:@"der" ]];
if(publicKeyPEMData){
NSLog(@"publicKeyPEMData NOT NULL!!");
SecKeyRef publicKey = [self getPublicKeyRefrenceFromeData:publicKeyPEMData]; //[self getPublicKeyRef];
if(PublicKey){
NSLog(@"publicKey is NOT NULL");
BOOL canVerify = SecKeyIsAlgorithmSupported(PublicKey,
kSecKeyOperationTypeVerify,
algorithm);
NSLog(@"canVerify : %i",canVerify);
BOOL result = NO;
if (canVerify) {
result = SecKeyVerifySignature(newPublicKey,
algorithm,
(__bridge CFDataRef)plainTextData,
(__bridge CFDataRef)signature,
&error);
if (!result) {
NSError *err = CFBridgingRelease(error); // ARC takes ownership
// Handle the error. . .
NSLog(@"failed to verify signature");
NSLog(@"error: %@",error);
}else{
NSLog(@"yes signed");
}
}
}else{
NSLog(@"publicKey IS NULL!!");
}
}else{
NSLog(@"publicKeyPEMData is NULL");
}
}else{
NSLog(@"private key is null");
}
}else{
NSLog(@"sad privateKeyPEMData is NULL");
}
}
@dcouturier
Copy link

Shouldn't you be using kSecKeyAlgorithmECDSASignatureMessageX962SHA512 instead of kSecKeyAlgorithmECDSASignatureDigestX962SHA512 if you are using the entire message in SecKeyCreateSignature and SecKeyVerifySignature?

kSecKeyAlgorithmECDSASignatureDigestX962SHA512 is only useful if you're using the message digest?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment