Search code examples
opensslbignum

Storing OpenSSL BIGNUM operation result


OpenSSL functions over BIGNUMs take as first argument the variable where the result will be stored, like int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);, which calculates r=a+b; Is it safe the usea in the following statement, or should I declare a new variable to hold the result?

BN_add(a, a, b);

Solution

  • It is safe, you can look-up examples in crypto/bn/bntest.c. There you can find code like:

        BN_add(&c, &c, &b);
        BN_sub(&c, &c, &a);
    

    Furthermore, according to https://www.openssl.org/docs/manmaster/crypto/BN_add.html :

    BN_add() adds a and b and places the result in r (r=a+b). r may be the same BIGNUM as a or b.