0

I'm trying to perform subtraction using OpenSSL's BN functions. I am expecting a signed integer as the difference, but instead I get either the unsigned integer, or the integer overflows (or if neither I get the desired result).

Am I using the wrong BN function?

int big_num_subtract_example(vector<int> *arr, unsigned int mod, int seed_num) {
    BN_CTX *ctx;
    ctx = BN_CTX_new();

    BIGNUM *product = BN_new();
    BIGNUM *tmp = BN_new();
    BIGNUM *modulo = BN_new();
    BIGNUM *mod_result = BN_new();

    BN_set_word(product, seed_num);
    BN_set_word(modulo, mod);

    for (auto elem : arr) {

        BN_CTX_start(ctx);

        BN_set_word(tmp, elem);

        BN_mul(product, product, tmp, ctx);

        //Start of example
        BIGNUM *sub_one = BN_new();
        BIGNUM *sub_two = BN_new();

        BIGNUM *num_one = BN_new();
        BIGNUM *num_two = BN_new();

        BN_set_word(num_one, -7);
        BN_set_word(num_two, 11);

        BN_sub(sub_one, num_one, num_two);
        BN_sub(sub_two, num_two, num_one);

        cout << "sub_one: " << BN_get_word(sub_one) << "\n"; //18446744073709551598 expected -4
        cout << "sub_two: " << BN_get_word(sub_two) << "\n"; //18446744073709551598 expected 18

        BN_set_word(num_one, 7);
        BN_set_word(num_two, 11);

        BN_sub(sub_one, num_one, num_two);
        BN_sub(sub_two, num_two, num_one);

        cout << "sub_one: " << BN_get_word(sub_one) << "\n"; //4 expected -11
        cout << "sub_two: " << BN_get_word(sub_two) << "\n"; //4 
        //end of example


        //...
        //Code using the product from start of loop and result of subtraction...
        //...

        BN_CTX_end(ctx);

    }

    BN_clear_free(product);
    BN_clear_free(tmp);
    BN_clear_free(modulo);
    BN_clear_free(mod_result);
    BN_CTX_free(ctx);

    return 0;
}

The docs state:

BN_sub() subtracts b from a and places the result in r (r=a-b). r may be the same BIGNUM as a or b.

Colby
  • 313
  • 4
  • 13

0 Answers0