Quantcast

RSA_sign RSA_verify

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RSA_sign RSA_verify

ShaunR
Where am i going wrong here?


[root@devhost1 openssl]# cat sign_verify.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include <openssl/bio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>

int main(void)
{

        char* message = "Hello World";
        unsigned char* signature;
        unsigned int slen;
        unsigned int verified;

        char *privkey = "-----BEGIN RSA PRIVATE
KEY-----\nMIGmAgEAAiAA+sX9Vg6kQR7FbhajVXMLIq5UnpZOasUMrOhVTw3ePQIDAQABAiAA\n+sEXfQHPBL68NES2ti7/B9csC6CUs3gSR8knfq4mkQIQD+2r/g/2nj3aLNlVZ/D5\n1wIQD75otLoIRzxXlMfjgtWuCwIQB3E4RFv3Eda6guwFEKyyPwIQBVoOsdDDW6PR\nicvhNOp6PQIQBIZ8VS7kFbFYbc2BxMQhhg==\n-----END
RSA PRIVATE KEY-----";

        char *pubkey = "-----BEGIN PUBLIC
KEY-----\nMDswDQYJKoZIhvcNAQEBBQADKgAwJwIgAPrF/VYOpEEexW4Wo1VzCyKuVJ6WTmrF\nDKzoVU8N3j0CAwEAAQ==\n-----END
PUBLIC KEY-----";

        RSA *private_key;
        RSA *public_key;

        BIO *priv_bio;
        BIO *pub_bio;

/////////////////////////////////////////////////////

   priv_bio = BIO_new_mem_buf(privkey, -1);
   if(priv_bio == NULL) {
      ERR_print_errors_fp(stdout);
      return 1;
   }

   private_key = PEM_read_bio_RSAPrivateKey(priv_bio, NULL, NULL, NULL);
   if(private_key == NULL) {
      ERR_print_errors_fp(stdout);
   }

        signature = (unsigned char*) malloc(RSA_size(private_key));
        if(RSA_sign(NID_md5, (unsigned char*) message, strlen(message),
signature, &slen, private_key) != 1) {
                ERR_print_errors_fp(stdout);
        }

//////////////////////////////////////////////////

        pub_bio = BIO_new_mem_buf(pubkey, -1);
        if(pub_bio == NULL) {
                ERR_print_errors_fp(stdout);
                return 1;
        }

        public_key = PEM_read_bio_RSA_PUBKEY(pub_bio, NULL, NULL, NULL);
   if(public_key == NULL) {
      ERR_print_errors_fp(stdout);
   }
        verified = RSA_verify(NID_md5, (unsigned char*) message,
strlen(message), signature, slen, public_key);

/////////////////////////////////////////////////////

        printf("VERIFIED: %d\n",verified);

        RSA_free(private_key);

        RSA_free(public_key);

        return 0;
}


[root@devhost1 openssl]# gcc sign_verify.c -lssl
[root@devhost1 openssl]# ./a.out
25493:error:04075070:lib(4):func(117):reason(112):rsa_sign.c:118:
VERIFIED: 0




______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: RSA_sign RSA_verify

JoelKatz

> Where am i going wrong here?

>         char* message = "Hello World";

>         if(RSA_sign(NID_md5, (unsigned char*) message, strlen(message),
> signature, &slen, private_key) != 1) {

The problem is that your RSA key is very small. A 256-bit RSA key can only
sign up to 32 bytes. 11 bytes are lost due to PKCS1 padding. A raw MD5
signature is 5 bytes. Add to that the fact that your key isn't quite 256
bits (it's about 248) and that an X509 signature has overhead, and you hit
the limit.

A typical signature is 36 bytes. Add 11 bytes for PKCS padding and you get
47 bytes, or 376 bits. So a 256-bit RSA key is not going to cut it.

DS


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: RSA_sign RSA_verify

ShaunR
Thanks, thats what i get for being lazy, for testing i was just using a
small key, i just changed it to my 4096 key and it's working..



>> Where am i going wrong here?
>
>>         char* message = "Hello World";
>
>>         if(RSA_sign(NID_md5, (unsigned char*) message, strlen(message),
>> signature, &slen, private_key) != 1) {
>
> The problem is that your RSA key is very small. A 256-bit RSA key can only
> sign up to 32 bytes. 11 bytes are lost due to PKCS1 padding. A raw MD5
> signature is 5 bytes. Add to that the fact that your key isn't quite 256
> bits (it's about 248) and that an X509 signature has overhead, and you hit
> the limit.
>
> A typical signature is 36 bytes. Add 11 bytes for PKCS padding and you get
> 47 bytes, or 376 bits. So a 256-bit RSA key is not going to cut it.
>
> DS
>
>
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [hidden email]
> Automated List Manager                           [hidden email]
>

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Loading...