/* * OpenSSLDemo.cpp */ #include "stdafx.h" #include #include #include #define HASHED_DATA_SIZE 20 #define SIGNATURE_SIZE 128 #define PUBLIC_KEY_SIZE 162 /* * Proper error-checking and buffer guarding omitted for simplicity. */ int main(int argc, char * argv[]) { /* * Allocate arrays to store the raw input bytes. */ unsigned char hashed_data[HASHED_DATA_SIZE]; unsigned char signature[SIGNATURE_SIZE]; unsigned char public_key[PUBLIC_KEY_SIZE]; /* * Read the binary files into their respective arrays. */ FILE * fp = fopen("c:\\hasheddata.bin", "rb"); if (HASHED_DATA_SIZE != fread(hashed_data, 1, HASHED_DATA_SIZE + 1, fp)) { printf("Read of hashed data was NOT exactly 20 bytes.\n"); } fclose(fp); fp = fopen("c:\\signature.bin", "rb"); if (SIGNATURE_SIZE != fread(signature, 1, SIGNATURE_SIZE + 1, fp)) { printf("Read of hashed data was NOT exactly 128 bytes.\n"); } fclose(fp); fp = fopen("c:\\pubkey.bin", "rb"); if (PUBLIC_KEY_SIZE != fread(public_key, 1, PUBLIC_KEY_SIZE + 1, fp)) { printf("Read of hashed data was NOT exactly 162 bytes.\n"); } fclose(fp); /* * Create OpenSSL RSA public key structure from my raw public key * bytes. */ RSA * rsa_struct_ptr = NULL; const unsigned char * public_key_pointer = public_key; if (NULL == d2i_RSA_PUBKEY(&rsa_struct_ptr, &public_key_pointer, PUBLIC_KEY_SIZE)) { printf("Creation of RSA public key structure FAILED!\n"); return -1; } /* * Verify the signature against the hashed data that was previously * signed with my private key. */ if (!RSA_verify(NID_sha1, hashed_data, HASHED_DATA_SIZE, signature, SIGNATURE_SIZE, rsa_struct_ptr)) { printf("Signature verification FAILED!\n"); return -1; } printf("Signature successfully verified.\n"); return 0; }