Search code examples
copensslbignum

wrong BIGNUM initialization in OpenSSL


I have a code like this:

BIGNUM *p = BN_new(), *B = BN_new(), *a = BN_new(), *ret = BN_new();

BN_bin2bn((uchar*)"\x01\x02\0x03\0",3,p);
BN_bin2bn((uchar*)"\x02\x03\0x04\0",3,B);
BN_bin2bn((uchar*)"\x03\x04\0x05\0",3,a);

Print_Format("p: %s",BN_bn2dec(p)); //expected: 66051, prints 66048
Print_Format("B: %s",BN_bn2dec(B)); //expected: 131844, prints 131840
Print_Format("a: %s",BN_bn2dec(a)); //expected: 197637, prints 197632

The printouts give wrong results. When I convert the results that code gave to hexadecimal number, It looks like this:

010203 => 010200
020304 => 020300
030405 => 030400

Why is BN_bin2n ignoring the last byte of the input? Does someone have any idea?


Solution

  • The error was at:

    BN_bin2bn((uchar*)"\x01\x02\**0x03**\0",3,p);
    BN_bin2bn((uchar*)"\x02\x03\**0x04**\0",3,B);
    BN_bin2bn((uchar*)"\x03\x04\**0x05**\0",3,a);
    

    It should be

    BN_bin2bn((uchar*)"\x01\x02\x03\0",3,p);
    BN_bin2bn((uchar*)"\x02\x03\x04\0",3,B);
    BN_bin2bn((uchar*)"\x03\x04\x05\0",3,a);