title: Hash Ciphers and check with Signature in C
body: <p>I have three binary files: cipher01.bin, cipher02.bin and cipher03.bin.
  Additionally I have a sign.bin and a pubkey.pem file. The task is to hash all three ciphers and compare it to the signature. Therefore I used RSA to decrypt the sign.bin with the public key from pubkey.pem.</p>
  
  <p>The result looks good but none of the cipher-hashes belongs to the signature. But I know, that there is at least one cipher which belongs to the signature because it is a task from our university. Maybe I forgot something but I can't figure out what.</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;openssl/sha.h&gt;
  #include &lt;openssl/err.h&gt;
  #include &lt;openssl/evp.h&gt;
  #include &lt;openssl/pem.h&gt;
  #include &lt;openssl/ssl.h&gt;
  #include &lt;openssl/rsa.h&gt;
  #include &lt;openssl/bio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  /** converts unsigned character to readble string*/
  char *pt(unsigned char *md) {
    int i;
    char *buf = (char*)malloc(sizeof(char)*80);
  
    for(int i = 0; i &lt; SHA_DIGEST_LENGTH;i++) {
      sprintf(&amp;(buf[i*2]),"%02x",md[i]);
    }
  
    return (buf);
  }
  
  /** returns error */
  void err_exit(void) {
    printf("%s\n",ERR_error_string(ERR_get_error(),NULL));
  
    ERR_free_strings();
    exit(EXIT_FAILURE);
  }
  
  /** reads a file */
  char * readFile(char * filename,long int * filesize) {
    FILE *fin;
    char *buf;
  
    if((fin=fopen(filename,"r"))==NULL) {
      printf("Error opening %s.\n",filename);
      exit(EXIT_FAILURE);
    }
  
    fseek(fin,0L,SEEK_END);
    *filesize = ftell(fin);
    rewind(fin);
  
    if(!(buf=malloc(*filesize))) {
      printf("Memory exhausted. Stop.\n");
      exit(EXIT_FAILURE);
    }
  
  
    fread(buf,*filesize,1,fin);
    fclose(fin);
  
    return buf;
  }
  
  /** hash a file with sha1 */
  char * hashBinaryFile(char * filename) {
    long int filesize = 0;
  
    EVP_MD_CTX c;
    unsigned char md[SHA_DIGEST_LENGTH];
  
    ERR_load_crypto_strings();
  
    EVP_MD_CTX_init(&amp;c);
  
    /** reads files into buf */
    char * buf = readFile(filename,&amp;filesize);
  
    if((EVP_DigestInit(&amp;c,EVP_sha1()))==0) {
      err_exit();
    }
  
    if((EVP_DigestUpdate(&amp;c,buf,filesize))==0) {
      err_exit();
    }
  
    if((EVP_DigestFinal(&amp;c,md,NULL))==0) {
      err_exit();
    }
  
    //printf("%s\n",pt(md));
  
    EVP_MD_CTX_cleanup(&amp;c);
    free(buf);
    ERR_free_strings();
  
    return pt(md);
  
  }
  
  int padding = RSA_PKCS1_PADDING;
  
  /** loads public key and creates rsa */
  RSA * createRSAWithFilename(char * filename,int public) {
    FILE * fp = fopen(filename,"rb");
  
    if(fp == NULL) {
      printf("Unable to open file %s \n",filename);
      return NULL;    
    }
  
    RSA *rsa= RSA_new() ;
  
    if(public) {
      rsa = PEM_read_RSA_PUBKEY(fp, &amp;rsa,NULL, NULL);
    } else {
      rsa = PEM_read_RSAPrivateKey(fp, &amp;rsa,NULL, NULL);
    }
  
    return rsa;
  }
  
  /** decrypt signature */
  char * public_decrypt(unsigned char * enc_data,int data_len, unsigned char *decrypted) {
    RSA * rsa = createRSAWithFilename("archieve/pubkey.pem",1);
    int  result = RSA_public_decrypt(data_len,enc_data,decrypted,rsa,padding);
    return pt(decrypted);
  }
  
  int main(int argc,char *argv[]) {
    /** decrypt signature */
    long int encrypted_length;
    long int decrypted_length; 
    unsigned char decrypted[4098]={};
    char * encrypted = readFile("archieve/s72897-sig.bin",&amp;encrypted_length);
    char * sign = public_decrypt(encrypted,encrypted_length, decrypted);
  
    char * cipher01 = hashBinaryFile("archieve/s72897-cipher01.bin");
    char * cipher02 = hashBinaryFile("archieve/s72897-cipher02.bin");
    char * cipher03 = hashBinaryFile("archieve/s72897-cipher03.bin");
  
    if(strcmp(sign,cipher01)==0) {
      printf("cipher01\n");
    } else if(strcmp(sign,cipher02)==0) {
      printf("cipher02\n");
    } else if(strcmp(sign,cipher03)==0) {
      printf("cipher03\n");
    } else {
      printf("No cipher matches the signature\n");
    }
  
    return 0;
  }
  </code></pre>
  
  <p>Thanks for any kind of help.</p>
  
  <p>Edit: fixed some code</p>
  
  <p>Edit2: link to the *.zip <a href="https://ufile.io/tqwoh" rel="nofollow noreferrer">https://ufile.io/tqwoh</a></p>