Search code examples
apiopensslbignum

Access BIGNUM bits in OpenSSL 1.1?


Prior 1.1 version of OpenSSL API I had access to raw representation of BIGNUM types through "d" field in struct bignum_st:

   struct bignum_st
           {
           BN_ULONG *d;    /* Pointer to an array of 'BN_BITS2' bit chunks. */
           int top;        /* Index of last used d +1. */
           /* The next are internal book keeping for bn_expand. */
           int dmax;       /* Size of the d array. */
           int neg;        /* one if the number is negative */
           int flags;
           };

In my program I need to get lowest byte from BIGNUM after some computations - it is quite easily - simply as:

(bn->d[0] & 0xff)

With API of OpenSSL version 1.1 many BN internals have been made opaque - I cannot get direct access to representation of BIGNUM. I still can get raw representation, but with additional copying - either BN_bn2bin or BN_mask_bits.

There is any way to access lowest byte without extra copying?


Solution

  • There is any way to access lowest byte without extra copying?

    Yes and no. If the BIGNUM is smaller that 0xffffffff, then use BN_get_word(bn) & 0xff;.

    If your BIGNUM is larger than 0xffffffff, then use BN_bn2binpad to copy out a range of bytes.

    Also see Issue 2001, Please provide BIGNUM And, Or and Xor ops in the OpenSSL bug tracker.