Im writing a program that is supposed to sign a file and then verify a file. Basically your supposed to put in three command prompts,one to indicate whether you want to sign or verify,another for the file to be signed or verified and for another file to be written to, meaning if you sign text.txt it will go to text.bin.Its sort of like if it was encrypting text.txt,the encrypted version would be written to encrypt.txt. My main problem is i dont know if im using the write arguments to write data to a file.
If anyone can help,id appreciate it.
Code:
#include<stdio.h>
#include<string.h>
#include<openssl/evp.h>
#include<openssl/dsa.h>
int main (int argc , char ** argv )
{
EVP_MD_CTX ctx;
DSA *dsa ;
char choice;
unsigned char *sig;
int len , siglen,size ;
char *buf;
unsigned char md[ EVP_MAX_MD_SIZE ];
FILE *file;
/* Compute the SHA1 digest of str */
EVP_DigestInit (& ctx , EVP_sha1 ());
EVP_DigestUpdate (& ctx , file , strlen (file ));
EVP_DigestFinal (& ctx , md , &len );
file=fopen(argv[2],"r");
fseek(file,0,SEEK_END);
size=ftell(file);
buf=(char *)malloc(size);
fread(buf,1,size,file);
fclose(file);
/* Generate DSA parameters */
dsa = DSA_generate_parameters (1024 , NULL , NULL , NULL , NULL ,NULL , NULL );
/* Generate DSA keys */
DSA_generate_key (dsa );
/* Allocate buffer space for signature */
sig = ( unsigned char *) malloc ( DSA_size (dsa ));
if(choice=="sign")
{
DSA_sign (file , md , len , sig , &siglen , dsa );
write_data(argv[3],buf,size);
}
else if(choice=="verify")
{
if ( DSA_verify (file , md , len , sig , siglen , dsa) == 1)
printf (" Signature is verified !\n");
else
printf (" Signature verification failed ...\ n");
write_data(argv[3],buf,size);
}
/* Destroy the DSA object */
DSA_free (dsa );
return 0;
}