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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
   | #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?";
 
  /* Foobar1329 */
  /* C'est la longueur en bytes qui est demandé, pas la longueur de la chaine */
  /* C'est important pour SHA1() plus bas */
  /* unsigned long n = (unsigned long)strlen((char*)d); */
  unsigned long n = (unsigned long)strlen((char*)d) + 1;
 
  unsigned char *md = malloc(SHA_DIGEST_LENGTH);
 
  /* RSA Initialization steps */
 
  RSA *var_RSA = NULL;
  int modulus_size = 1024;
  long public_exponent = 65537;
 
  int V_sign = 0;
  int V_verif = 0;
 
  unsigned char *sigret = NULL;
 
  unsigned int siglen = 0;
 
  /* Generation of the digest */
  SHA1(d,n,md);
 
  /* Foobar1329 */
  /* stdout plutôt ;)  et puis non, md n'est pas une chaine littérale */
  /* fprintf(stdin,"%s\n\n",md); */
 
  /* RSA part */
 
  /* Foobar1329 */
  /* Le RSA_new est inutile, RSA_generate_key renvoie un ptr alloué */
  /* var_RSA = RSA_new(); */
  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");
 
  sigret = malloc(sizeof(RSA_size(var_RSA)));
 
  if(sigret==NULL)
    printf("Error during the allocation of sigret\n\n");
  else
    printf("Everything went fine during the allocation of sigret\n\n");
 
 
  /* Foobar1329 */
  /* md est un message digest, de SHA_DIGEST_LENGTH bytes, et non une chaine 
     caractères, le pb vient de là ;) */
  /*
   V_sign = RSA_sign(NID_sha1,md,strlen((char*)md),sigret,&siglen,var_RSA);
   */
 
  V_sign = RSA_sign(NID_sha1,md,SHA_DIGEST_LENGTH,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);
 
  /* Foobar1329 */
  /* Il faut remplacer les strlen par SHA_DIGEST_LENGTH ici */
  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");
 
  /* Foobar1329 */
  /* Meme pb qu'avec RSA_sign */
  /* 
  V_verif = RSA_verify(NID_sha1,md,strlen((char*)md),sigret,siglen,var_RSA);
  */
  V_verif = RSA_verify(NID_sha1,md,SHA_DIGEST_LENGTH,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;
} |