Last active
March 14, 2020 03:53
-
-
Save edisonlo/3bffb8d58726fa58ecc8396ef82f8175 to your computer and use it in GitHub Desktop.
Obj-c verify ECDSA signature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?