Read a Bignum from file

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Read a Bignum from file

Angel Martinez Gonzalez
Hello:

I wrote a BIGNUM into a file using the function:

        int BN_print_fp(FILE *fp, const BIGNUM *a);

But, How I can read this bignum from this file?. I don´t know a openssl
function to read a bignum from a file.

Thanks.

______________________________________________________________________
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
|

Re: Read a Bignum from file

Ludovic FLAMENT
Angel Martinez Gonzalez wrote:

>Hello:
>
>I wrote a BIGNUM into a file using the function:
>
>        int BN_print_fp(FILE *fp, const BIGNUM *a);
>
>But, How I can read this bignum from this file?. I don´t know a openssl
>function to read a bignum from a file.
>  
>
    Hello, you can read Bignum from file with this method,

#include <stdio.h>
#include <stdlib.h>
#include <openssl/bn.h>

int main()
{
        BIGNUM  *b;
        FILE    *f;
        char    buff[128]; /* must depend of Bignum size */

        b = BN_new();
        if (b == NULL)
           return 0;

        BN_add_word(b, 123456789);

        f = fopen("bignum", "w");
        if (f == NULL)
                return 0;

        BN_print_fp(f, b);

        BN_free(b);
        fclose(f);

        /* start reading */
        f = fopen("bignum", "r");
        if (f == NULL)
                return 0;

        if (fread(buff, sizeof(char), sizeof(buff), f) <= 0)
       {
             fclose(f);
             return 0;
       }
       fclose(f);

        if (!BN_hex2bn(&b, buff))
                return 0;

        printf("BN read from file : ");
        BN_print_fp(stdout, b);
        printf("\n");

        BN_add_word(b, 1);

        printf("BN read from file + 1 : ");
        BN_print_fp(stdout, b);
        printf("\n");

        BN_free(b);
        return 1;
}

--
Ludovic FLAMENT
______________________________________________________________________
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
|

Re: Read a Bignum from file

Nils Larsch
In reply to this post by Angel Martinez Gonzalez
Angel Martinez Gonzalez wrote:
> Hello:
>
> I wrote a BIGNUM into a file using the function:
>
>         int BN_print_fp(FILE *fp, const BIGNUM *a);
>
> But, How I can read this bignum from this file?. I don´t know a openssl
> function to read a bignum from a file.

read it into memory and use BN_hex2bn

Nils
______________________________________________________________________
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
|

Re: Read a Bignum from file

Stefan Vatev
In reply to this post by Angel Martinez Gonzalez
Citation from the letter of Ludovic FLAMENT
<[hidden email]>:

> Angel Martinez Gonzalez wrote:
>
> >Hello:
> >
> >I wrote a BIGNUM into a file using the function:
> >
> >        int BN_print_fp(FILE *fp, const BIGNUM *a);
> >
> >But, How I can read this bignum from this file?. I don?t
> know a openssl
> >function to read a bignum from a file.
> >
> >
>     Hello, you can read Bignum from file with this
> method,
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <openssl/bn.h>
>
> int main()
> {
>         BIGNUM  *b;
>         FILE    *f;
>         char    buff[128]; /* must depend of Bignum size
> */
>
>         b = BN_new();
>         if (b == NULL)
>            return 0;
>
>         BN_add_word(b, 123456789);
>
>         f = fopen("bignum", "w");
>         if (f == NULL)
>                 return 0;
>
>         BN_print_fp(f, b);
>
>         BN_free(b);
>         fclose(f);
>
>         /* start reading */
>         f = fopen("bignum", "r");
>         if (f == NULL)
>                 return 0;
>
>         if (fread(buff, sizeof(char), sizeof(buff), f) <=
> 0)
>        {
>              fclose(f);
>              return 0;
>        }
>        fclose(f);
>
>         if (!BN_hex2bn(&b, buff))
>                 return 0;
>
>         printf("BN read from file : ");
>         BN_print_fp(stdout, b);
>         printf("\n");
>
>         BN_add_word(b, 1);
>
>         printf("BN read from file + 1 : ");
>         BN_print_fp(stdout, b);
>         printf("\n");
>
>         BN_free(b);
>         return 1;
> }
>
> --
> Ludovic FLAMENT
>
______________________________________________________________________
> OpenSSL Project
> http://www.openssl.org
> User Support Mailing List
> [hidden email]
> Automated List Manager
> [hidden email]
>




Just a minor remark about the sample source:

you should not BN_free(b) before BN_hex2bn(&b, buff)


-----------------------------

Гладен ли си?
Поръчай on-line
от AmAm.bg

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