[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openssl-users
Subject:    Question concerning a small signature code
From:       Lucas Clemente <luke.clemente () googlemail ! com>
Date:       2009-11-22 19:18:43
Message-ID: 55BBA529-47ED-4FFA-B73A-0BE575669039 () gmail ! com
[Download RAW message or body]

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?


[Attachment #3 (unknown)]

<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; \
-webkit-line-break: after-white-space; ">Hi there,<div><br></div><div>First of all, I \
have never used OpenSSL before. So please don't be angry if my code is complete \
useless ;)</div><div><br></div><div>I am trying to do a SHA1 signature of a text \
using an rsa private key. This is my code so far:</div><div><br></div><div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">&nbsp;&nbsp;char data \
[BUFFER_SIZE];</span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;sprintf(data, "test");</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">&nbsp;&nbsp;int data_len = \
strlen(data);</span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: \
12px;"><br></span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: 12px;">&nbsp;&nbsp;//Read \
private key</span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: 12px;">&nbsp;&nbsp;BIO* bio \
= BIO_new(BIO_s_file());</span></font></div><div><font class="Apple-style-span" \
face="Courier" size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;BIO_read_filename(bio, \
"./private.pem");</span></font></div><div><font class="Apple-style-span" \
face="Courier" size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;RSA* rsakey = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, \
"");</span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;BIO_free_all(bio);</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;"><br></span></font></div><div><font class="Apple-style-span" \
face="Courier" size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;//Allocate the result memory</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">&nbsp;&nbsp;unsigned char* sigbuf = \
malloc(RSA_size(rsakey));</span></font></div><div><font class="Apple-style-span" \
face="Courier" size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;unsigned int siglen;</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;"><br></span></font></div><div><font class="Apple-style-span" \
face="Courier" size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;//EVP</span></font></div><div><font class="Apple-style-span" \
face="Courier" size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;EVP_PKEY* pkey = EVP_PKEY_new();</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">&nbsp;&nbsp;EVP_PKEY_assign_RSA(pkey, \
rsakey);</span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: \
12px;"><br></span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;EVP_MD_CTX md_ctx;</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">&nbsp;&nbsp;EVP_MD_CTX_init(&amp;md_ctx);</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">&nbsp;&nbsp;EVP_SignInit(&amp;md_ctx, \
EVP_sha1());</span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;EVP_SignUpdate(&amp;md_ctx, data, \
data_len);</span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;EVP_SignFinal(&amp;md_ctx, sigbuf, &amp;siglen, \
pkey);</span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;EVP_PKEY_free(pkey);</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;"><br></span></font></div><div><font class="Apple-style-span" \
face="Courier" size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;//Base 64</span></font></div><div><font class="Apple-style-span" \
face="Courier" size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;BIO* b64 = BIO_new(BIO_f_base64());</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">&nbsp;&nbsp;bio = BIO_new_fp(stdout, \
BIO_NOCLOSE);</span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: 12px;">&nbsp;&nbsp;bio = \
BIO_push(b64, bio);</span></font></div><div><font class="Apple-style-span" \
face="Courier" size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;BIO_write(bio, sigbuf, siglen);</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">&nbsp;&nbsp;BIO_flush(bio);</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">&nbsp;&nbsp;BIO_free_all(bio);</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;"><br></span></font></div><div><font class="Apple-style-span" \
face="Courier" size="3"><span class="Apple-style-span" style="font-size: \
12px;">&nbsp;&nbsp;free(sigbuf);</span></font></div><div><font \
class="Apple-style-span" face="Courier"><br></font></div><div><font \
class="Apple-style-span" face="Courier"><span class="Apple-style-span" \
style="font-family: Helvetica; "><div>This code does work as intended. However when \
running with valgrind it outputs some jumps depending on uninitialized values. One \
example:</div><div><br></div><div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: 12px;">==14646== &nbsp; \
&nbsp;at 0x511F1EF: BN_mod_inverse (bn_gcd.c:215)</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">==14646== &nbsp; &nbsp;by 0x51228D1: BN_MONT_CTX_set \
(bn_mont.c:406)</span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: 12px;">==14646== &nbsp; \
&nbsp;by 0x5118000: BN_mod_exp_mont (bn_exp.c:417)</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">==14646== &nbsp; &nbsp;by 0x511E40A: \
BN_BLINDING_create_param (bn_blind.c:352)</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">==14646== &nbsp; &nbsp;by 0x5136555: RSA_setup_blinding \
(rsa_lib.c:413)</span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: 12px;">==14646== &nbsp; \
&nbsp;by 0x51344DE: rsa_get_blinding (rsa_eay.c:277)</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">==14646== &nbsp; &nbsp;by 0x5134FF5: RSA_eay_private_encrypt \
(rsa_eay.c:406)</span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: 12px;">==14646== &nbsp; \
&nbsp;by 0x513718F: RSA_sign (rsa_sign.c:132)</span></font></div><div><font \
class="Apple-style-span" face="Courier" size="3"><span class="Apple-style-span" \
style="font-size: 12px;">==14646== &nbsp; &nbsp;by 0x5157DC5: EVP_SignFinal \
(p_sign.c:111)</span></font></div><div><font class="Apple-style-span" face="Courier" \
size="3"><span class="Apple-style-span" style="font-size: 12px;">==14646== &nbsp; \
&nbsp;by 0x400E6A: main (main.c:33)</span></font></div><div><br></div><div>I am a bit \
concerned with that. Is this a problem of my code? How to solfe \
it?</div></div></span></font></div></div></body></html>


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majordomo@openssl.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic