1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/sha.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
int main(void)
{
unsigned int k = 0;
/* SHA1 Initialization steps */
const unsigned char *d = (unsigned char*)"Hi dude, what about a test?";
unsigned long n = (unsigned long)strlen((char*)d);
unsigned char *md = malloc(SHA_DIGEST_LENGTH);
/* RSA Initialization steps */
RSA *var_RSA = RSA_new();
int modulus_size = 1024;
long public_exponent = 65537;
int V_sign = 0;
int V_verif = 0;
unsigned char *sigret = malloc(sizeof(RSA_size(var_RSA)));
unsigned int siglen = 0;
/* Generation of the digest */
SHA1(d,n,md);
fprintf(stdin,"%s\n\n",md);
/* RSA part */
var_RSA = RSA_generate_key(modulus_size,public_exponent,NULL,NULL);
if(var_RSA==NULL)
printf("Error during the generation of the keys\n\n");
else
printf("Everything went fine during the generation of the keys\n\n");
V_sign = RSA_sign(NID_sha1,md,strlen((char*)md),sigret,&siglen,var_RSA);
if(V_sign==0)
printf("Problem while signing the digest\n\n");
else
printf("Everything went fine while signing the digest\n\n");
for(k=0;k<siglen;k++)
printf("%02X",sigret[k]);
printf("\n\n");
printf("---test---\n");
/* Print all the variables to be given to RSA_verify to check their accuracy */
printf("NID_sha1: %d\n",NID_sha1);
printf("strlen(md): %d\n",strlen((char*)md));
for(k=0;k<strlen((char*)md);k++)
printf("%02X",md[k]);
printf("\n");
printf("siglen: %d\n",siglen);
for(k=0;k<siglen;k++)
printf("%02X",sigret[k]);
printf("\n");
V_verif = RSA_verify(NID_sha1,md,strlen((char*)md),sigret,siglen,var_RSA);
printf("---test---\n");
if(V_verif==0)
printf("Problem while verifying the digest\n\n");
else
printf("Everything went fine while verifying the digest\n\n");
RSA_free(var_RSA);
return 0;
} |
Partager