Search code examples
copensslbignum

Multiply a BigNum with an plain integer


I have the following function:

BIGNUM * multiplyWithInt(BIGNUM *bn, int val){
  //Logic Here
}

What I try to do is to calculate the multiplication bn*val. For Multiplication according to documentation (given from command man bn) is the following:

int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);

As you can see I need to somehow toi convert the integer val into openssl's BIGNUM. How I can do that? One approach is to convert it as unsigned char * byte array and use the BN_bin2bn function but will that give me the desired functionality?


Solution

  • The function you're looking for is BN_set_word. This assigns an unsigned long value to a BIGNUM.

    BIGNUM *bn_val = BN_new();
    BN_set_word(bn_val , val);
    

    You can then pass bn_val and bn to BN_mul.