I was wondering if it is possible that a big number modulo a small integer in Openssl?
Say I generate two big prime numbers:
BN_generate_prime(p,512,0,0,0,0,0);
BN_generate_prime(q,512,0,0,0,0,0);
and calculate the product N:
BN_mul(N,p,q,ctx);
I would like to test if N is a “Blum integer” (N mod 4==3), however I can’t figure out how to do this since function BN_mod only support big numbers.
Yes it’s possible.
The best and efficient way is given in jww’s answer, which is to call BN_mod_word().
A less efficient way is to do it by converting a small integer a
BIGNUMfirst. It’s cumbersome, but not difficult. I’ll show you two ways to create theBIGNUMs by computing11 mod 3withBN_mod. First, declare a BIGNUM for your numbers.Method 1: Convert your number to a string, and then the string to a BIGNUM.
(In C you can do
char buf[12]; sprintf(buf, "%d", n); BN_dec2bn(&N, buf);)Method 2: Give your number as an array of bytes, but beware that OpenSSL wants your bytes in big endian format, and will always interpret your bytes as a positive number.
And then just use your OpenSSL function as normal.
And free your
BIGNUMs.