On Thu, Mar 03, 2005, Vishwas wrote:
> Dear SSLites,
> Please take a look at my code. It is meant for calculating square
> of a big number. To test the things I started by providing integers on
> command line and tried to obtain the values back in integer. But could
> not get success. Can you figure out my mistake?
>
> #include<openssl/bn.h>
> #include<string.h>
> #include<math.h>
>
> int main(int argc, char **argv) {
> int i = atoi(argv[1]); //collect integer
> BIGNUM *I = BN_new();
> BIGNUM *II = BN_new();
> BN_CTX *CTX = BN_CTX_new();
>
> BN_CTX_init(CTX);
> tmp_str = (unsigned char *)malloc(20000);
>
> sprintf(tmp_str, "%d", i); // itoa();
> BN_bin2bn(tmp_str, (int)strlen(tmp_str), I); // int to BN
> if(!BN_sqr(II, I, CTX)) printf("ERROR while doing BN_sqr\n"); // BN square
> BN_bn2bin(II, tmp_str); // BN to int
> printf("i SQUARE = %s\n", tmp_str); // am getting garbage
>
> BN_CTX_free(CTX);
> BN_free(I);
> BN_free(II);
> free(tmp_str);
> return 1;
> }
>
>
BN_bin2bin() and BN_bin2bn() don't operate on ASCII decimal versions of a
BIGNUM they use a big endian buffer. That's why you appear to be getting
garbage.
BN_dec2bn(), and BN_bn2dec() should be used instead or BN_set_word() if you
want to set a BIGNUM directly from a long type.
Steve.
--
Dr Stephen N. Henson. Email, S/MIME and PGP keys: see homepage
OpenSSL project core developer and freelance consultant.
Funding needed! Details on homepage.
Homepage: http://www.drh-consultancy.demon.co.uk
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List [email protected]
Automated List Manager [EMAIL PROTECTED]