Reputation:
I'm trying to check if a value c is between 1 and (n-1), where both n and c are big numbers. I'm using the BN_cmp() function to perform the operation, but see an error coming from BN_sub(). My code and error are shown below:
BIGNUM *n = NULL, *c = NULL;
BIGNUM *n_1 = NULL; // variable to store (n-1)
n = BN_bin2bn(N, 256, n); // N is "bff722714050aebb23a9bd018c3e9ba26a47f53816eeac7e10543958702d9265c8d67784fe03c07bfceac05e7f2a434971dfa2a5ea461893450ced52fcb3f143a85fb3a9194417ff220258840a3359a104079ccd201afec091bab6587d4cfe0b95bba34ef74a70b392a92a93f026c9bed41eb4ec80452492a2ad524e6b0333c5787b34ee941829020bb75ee5dd216b3734823ddd547d50f8a7e711f8a24fd7dbc0bd2f062ccaba98cdbf62c15d2521b39ce44c53125604493e482ae35f945c4efff1d01414b0aad33de77b020ea4aedf3d88171fe51b22881bc70c639f8b6f1b5a70ed39aa121a8f44887dcbbfce29e1e508d1b0f0666693b476d81faa6a18bd"
c = BN_bin2bn(cipher, 256, c); // cipher is "534d1f57d948cac580b88b922bc47bc3d64c8cd1262bbf0944b99833ec94d072c1a1496be44d47a9c419dc403855a4b1cb2bb30e56e0cc5fd557d34373d785dbe70d67e30355fc228a353b05432a40874ba84253af5cc52d3ab4118e8ca1e28e6c9c610760e753f87a15912774ccb80b00ca21e85926143c1ed8385a607c4e55fa531f1f208bb3f23bc0c4eff4c272068f9939157bc61f5427cc32f017ef31f6363c8a736ec984da763ebea5eb94d83fa31d70223ec5503cfd97e598d883f43aca5e884b702a2f76d298659181cb5180e25faf56c9aa0ebe49413b9acbbefde95ec102ee4e351a8ff8d5a3fbdcee448ff466dffb45fdc0a0b3d31b3d192bb5cb"
BN_sub(n_1, n, BN_value_one()); // subtract 1 from n and store result in (n_1)
if(BN_is_zero(c) || BN_is_one(c)) { // check if c is 0 or 1
printf("False");
}
if(BN_cmp(c, (n_1)) == -1) { // check if c is less than (n-1)
printf("True");
}
else {
printf("False");
}
Error:
0x00007ffff792f12b in fips_bn_sub (r=0x0, a=0x6aaa20, b=0x7ffff7da7c40 <const_one>) at bn_add.c:301
301 if (bn_wexpand(r,max) == NULL) return(0);
Would appreciate any pointers on how to solve this. I've looked at test_sub() in bntest.c and I've pretty much declared variables in same way. There BN_new() is used to generate new values but I've got the values provided to me which is why I'm doing BN_bin2bn(). I also tried to add in the following lines to see if that fixes the issue but no luck:
bn_correct_top(n);
bn_correct_top(c);
Upvotes: 2
Views: 402
Reputation: 2516
Two issues.
If I understand correctly, you are passing hex strings as a first argument to BN_bin2bn
, e.g. const unsigned char[] N = "FF";
whereas you are supposed to pass binary data such as const unsigned char[] N = "\xFF";
. So you need first to convert it to get proper results.
First argument to BN_sub
needs to be initialized with BN_new()
So, sample code should look like this:
BIGNUM *n = NULL;
BIGNUM *n_1 = BN_new();
unsigned char N[] = "\x01";
n = BN_bin2bn(N, 1 , n);
BN_sub(n_1, n, BN_value_one());
if(BN_is_zero(n_1)){
printf("1 - 1 = 0");
}
Upvotes: 3