Note that there are some explanatory texts on larger screens.

plurals
  1. POBIGNUM strange behavior in a calculation loop
    primarykey
    data
    text
    <p>I'm trying to implement a basic routine to perform some calculation on BIGNUM(s) and I've found a strange behavior. The functions are as follows</p> <pre><code>unsigned char *char_array_as_hex(unsigned char *chr_a, int len) { unsigned char *chr_s = (unsigned char *)malloc(len * 2); char buffer[5]; for (int i = 0; i &lt; len; i++) { sprintf(buffer, "%02X", chr_a[i]); chr_s[(2 * i) + 0] = buffer[0]; chr_s[(2 * i) + 1] = buffer[1]; } return chr_s; } </code></pre> <p>and</p> <pre><code>char *big_number_as_decimal_from_hex_array(unsigned char *chr_a, int len, BN_CTX *bn_ctx) { unsigned char *hex_s = char_array_as_hex(chr_a, len); BIGNUM *big_number = BN_CTX_get(bn_ctx); BN_hex2bn(&amp;big_number, (char *)hex_s); char *big_number_as_decimal = BN_bn2dec(big_number); free(hex_s); BN_free(big_number); return big_number_as_decimal; } </code></pre> <p>and</p> <pre><code>void test_compute_prime256v1() { BN_CTX *bn_ctx = BN_CTX_new(); BN_CTX_start(bn_ctx); unsigned char seed_a[20] = { 0xC4,0x9D,0x36,0x08,0x86,0xE7,0x04,0x93,0x6A,0x66, /* seed */ 0x78,0xE1,0x13,0x9D,0x26,0xB7,0x81,0x9F,0x7E,0x90 }; printf("s = %s\n", big_number_as_decimal_from_hex_array(seed_a, 20, bn_ctx)); unsigned char p_a[32] = { 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,0x00,0x00, /* p */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF }; printf("p = %s\n", big_number_as_decimal_from_hex_array(p_a, 32, bn_ctx)); BN_CTX_end(bn_ctx); BN_CTX_free(bn_ctx); } </code></pre> <p>then I call "test_compute_prime256v1" in an Objective-C method. If I call it once or multiple times with a reasonable delay between each call it produces correct result however, when I call that function in a loop it produces different incorrect values</p> <pre><code>- (IBAction)btnOK_Clicked:(id)sender { for (int i = 1; i &lt; 10; i++) { printf("i = %d\n", i); test_compute_prime256v1(); } } </code></pre> <p>and a sample output was</p> <pre><code>i = 1 s = 1122468115042657169822351801880191947498376363664 p = 115792089210356248762697446949407573530086143415290314195533631308867097853951 i = 2 s = 1122468115042657169822351801880191947498376363664 p = 966134380529368896499052403318808180610643774633026536153469502543482958881555881553276... i = 3 s = 1122468115042657169822351801880191947498376363664 p = 115792089210356248762697446949407573530086143415290314195533631308867097853951 </code></pre> <p>Note: some numbers are trimmed to fit in. I have followed the suggestion in <a href="http://www.openssl.org/docs/crypto/BN_CTX_start.html" rel="nofollow">here</a>. Am I missing something? Is there any mistake somewhere? Anyone can help?</p> <p>Thanks</p> <p>EDITED:</p> <p>I made some modification to code but the issue still exists. I changed <code>big_number_as_decimal_from_hex_array</code> as follows</p> <pre><code>char *big_number_as_decimal_from_hex_array_ex(unsigned char *chr_a, int len) { BN_CTX *bn_ctx = BN_CTX_new(); BN_CTX_start(bn_ctx); unsigned char *hex_s = char_array_as_hex(chr_a, len); BIGNUM *big_number = BN_CTX_get(bn_ctx); BN_hex2bn(&amp;big_number, (char *)hex_s); char *big_number_as_decimal = BN_bn2dec(big_number); free(hex_s); BN_free(big_number); BN_CTX_end(bn_ctx); BN_CTX_free(bn_ctx); return big_number_as_decimal; } </code></pre> <p>and also</p> <pre><code>char *big_number_as_decimal_from_hex_array_ex_2(unsigned char *chr_a, int len) { BN_CTX *bn_ctx = BN_CTX_new(); unsigned char *hex_s = char_array_as_hex(chr_a, len); BIGNUM *big_number = BN_CTX_get(bn_ctx); BN_hex2bn(&amp;big_number, (char *)hex_s); char *big_number_as_decimal = BN_bn2dec(big_number); free(hex_s); BN_free(big_number); BN_CTX_free(bn_ctx); return big_number_as_decimal; } </code></pre> <p>I modified the <code>test_compute_prime256v1</code> as </p> <pre><code>void test_compute_prime256v1_ex() { unsigned char seed_a[20] = {...}; printf("s = %s\n", big_number_as_decimal_from_hex_array_ex(seed_a, 20)); unsigned char p_a[32] = {...}; printf("p = %s\n", big_number_as_decimal_from_hex_array_ex(p_a, 32)); // or unsigned char seed_a[20] = {...}; printf("s = %s\n", big_number_as_decimal_from_hex_array_ex_2(seed_a, 20)); unsigned char p_a[32] = {...}; printf("p = %s\n", big_number_as_decimal_from_hex_array_ex_2(p_a, 32)); } </code></pre> <p>but the code produces the same incorrect result in a looped calculation</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload