5

I have to make a RSA signature (on a state machine) in C on a 32bit board. I am limited on memory so I can not store decimals in a vector or something like that.

The best thing would be if I could store bits and to have easy access to them; what storage method would be best?

I made this one:

#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

This seems hard to use. How can I simplify it?

peterh
  • 11,875
  • 18
  • 85
  • 108
Popovici Sebi
  • 258
  • 3
  • 13
  • 3
    If you cannot use existing libraries (GMP, MPFI, ...), you might want to check how those represent long integers. It's usually an array of unsigneds aligned with the platform. – dhke Aug 24 '16 at 12:58
  • I can not use any library , I think I am going to try something with uint32 , Thank you! – Popovici Sebi Aug 24 '16 at 13:12
  • *"I can not store decimals in a vector or something like that"* - Why? What's the limitation? Any specific size? – Artjom B. Aug 24 '16 at 18:30
  • 1
    I have a RH850 board( https://www.renesas.com/en-us/products/microcontrollers-microprocessors/rh850/softtools.html ) and this is why the memory is limited and the hole algorithm will be structured on a state machine , this is why I should pay more attention on not losing data. – Popovici Sebi Aug 25 '16 at 07:45
  • I made this this structure do you have any better ideas? 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 – Popovici Sebi Aug 25 '16 at 12:21

1 Answers1

4

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

And, you can use this code sample 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

You should get something like this when executing it:

18446744073709551616 + 36893488147419103232 = 55340232221128654848
perror
  • 7,071
  • 16
  • 58
  • 85
  • 1
    Thank you , but I couldn't not use any library because of memory limitations , I used that method that I posted in question , but I still live this question unanswered , in case of a better solution , that will help someone else . – Popovici Sebi Jul 19 '17 at 12:51
  • You can also look at [mbed TLS](https://tls.mbed.org/) is a very similar library to OpenSSL specifically used in embedded systems. The problem with your "memory limitation" is that you never told precisely how strict they were. – perror Jul 19 '17 at 14:07