Hi there,

First of all, I have never used OpenSSL before. So please don't be angry if my 
code is complete useless ;)

I am trying to do a SHA1 signature of a text using an rsa private key. This is 
my code so far:

  char data [BUFFER_SIZE];
  sprintf(data, "test");
  int data_len = strlen(data);

  //Read private key
  BIO* bio = BIO_new(BIO_s_file());
  BIO_read_filename(bio, "./private.pem");
  RSA* rsakey = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, "");
  BIO_free_all(bio);

  //Allocate the result memory
  unsigned char* sigbuf = malloc(RSA_size(rsakey));
  unsigned int siglen;

  //EVP
  EVP_PKEY* pkey = EVP_PKEY_new();
  EVP_PKEY_assign_RSA(pkey, rsakey);

  EVP_MD_CTX md_ctx;
  EVP_MD_CTX_init(&md_ctx);
  EVP_SignInit(&md_ctx, EVP_sha1());
  EVP_SignUpdate(&md_ctx, data, data_len);
  EVP_SignFinal(&md_ctx, sigbuf, &siglen, pkey);
  EVP_PKEY_free(pkey);

  //Base 64
  BIO* b64 = BIO_new(BIO_f_base64());
  bio = BIO_new_fp(stdout, BIO_NOCLOSE);
  bio = BIO_push(b64, bio);
  BIO_write(bio, sigbuf, siglen);
  BIO_flush(bio);
  BIO_free_all(bio);

  free(sigbuf);

This code does work as intended. However when running with valgrind it outputs 
some jumps depending on uninitialized values. One example:

==14646==    at 0x511F1EF: BN_mod_inverse (bn_gcd.c:215)
==14646==    by 0x51228D1: BN_MONT_CTX_set (bn_mont.c:406)
==14646==    by 0x5118000: BN_mod_exp_mont (bn_exp.c:417)
==14646==    by 0x511E40A: BN_BLINDING_create_param (bn_blind.c:352)
==14646==    by 0x5136555: RSA_setup_blinding (rsa_lib.c:413)
==14646==    by 0x51344DE: rsa_get_blinding (rsa_eay.c:277)
==14646==    by 0x5134FF5: RSA_eay_private_encrypt (rsa_eay.c:406)
==14646==    by 0x513718F: RSA_sign (rsa_sign.c:132)
==14646==    by 0x5157DC5: EVP_SignFinal (p_sign.c:111)
==14646==    by 0x400E6A: main (main.c:33)

I am a bit concerned with that. Is this a problem of my code? How to solfe it?

Reply via email to