How to store large numbers?

I have to sign the RSA (on the destination machine) in C on a 32-bit board. I am limited by memory, so I can not store decimals in a vector or something like that.

It would be best if I could store bits and have easy access to them; which storage method would be better?

I have done this:

 #if (CPU_TYPE == CPU_TYPE_32) typedef uint32_t word; #define word_length 32 typedef struct BigNumber { word words[64]; } BigNumber; #elif (CPU_TYPE == CPU_TYPE_16) typedef uint16_t word; #define word_length 16 typedef struct BigNumber { word words[128]; } BigNumber; #else #error Unsupported CPU_TYPE #endif 

It is hard to use. How can I simplify it?

+5
source share
1 answer

You can simply use the BigNumber API from OpenSSL. You can find the full API here .

And you can use this code example as a start:

 #include <stdio.h> #include <openssl/crypto.h> #include <openssl/bn.h> int main(int argc, char *argv[]) { static const char num1[] = "18446744073709551616"; static const char num2[] = "36893488147419103232"; BIGNUM *bn1 = NULL; BIGNUM *bn2 = NULL; BN_CTX *ctx = BN_CTX_new(); BN_dec2bn(&bn1, num1); // convert the string to BIGNUM BN_dec2bn(&bn2, num2); BN_add(bn1, bn1, bn2); // bn1 = bn1 + bn2 char *result_str = BN_bn2dec(bn1); // convert the BIGNUM back to string printf("%s + %s = %s\n", num1, num2, result_str); OPENSSL_free(result_str); BN_free(bn1); BN_free(bn2); BN_CTX_free(ctx); return 0; } 

Compile it with

 #> gcc -Wall -Wextra -g -o sample sample.c -lcrypto 

When doing this, you should get something like this:

 18446744073709551616 + 36893488147419103232 = 55340232221128654848 
+4
source

All Articles