I have an RSA base64 public key that I must use to verify my digital signature. I do not understand how to initialize RSA with a public key.
My code looks something like this:
unsigned char *signature = "";
char *original = "";
unsigned char sha2HashDigest[SHA256_DIGEST_LENGTH];
SHA256(original, strlen(original), sha2HashDigest);
char *key = "base64encodedKey";
RSA *r = RSA_new();
int result = RSA_verify(NID_sha256, sha2HashDigest, SHA256_DIGEST_LENGTH,
signature, strlen(signatrue), r);
if (result != 1)
Note. I am doing this in an iOS application, but I think it does not matter for the question.
UPDATE: I ended up using EVP as suggested by vond. The public key is a PEM file. This is my code:
FILE *fp = fopen([keyFilePath UTF8String], "r");
if (!fp) return NO;
EVP_PKEY *pubKey = PEM_read_PUBKEY(fp,NULL,NULL,NULL);
EVP_MD_CTX md_ctx;
EVP_MD_CTX_init(&md_ctx);
EVP_VerifyInit(&md_ctx, EVP_sha256());
EVP_VerifyUpdate (&md_ctx, (unsigned char*)[msgData bytes], [msgData length]);
int err = EVP_VerifyFinal (&md_ctx, (unsigned char*) sigData, (unsigned int)[sigData length], pubKey);
EVP_PKEY_free (pubKey);
source
share