lokiuox
lokiuox

Reputation: 11

Print a big integer stored as an unsigned long long array

I'm reviewing the security of an app for a University project, the app encrypts a file using RSA, specifically it uses this library: https://github.com/ilansmith/rsa (DO NOT use this, it has serious vulnerabilities).

(If you want to take a look, most of the operations between these numbers are implemented in the rsa_num.c file.)

This tool uses arrays of unsigned long long to store the big numbers needed for RSA (n, e and d):

typedef struct {
    u64 arr[17]; //u64 is defined as unsigned long long
    int top;     //points to the last occupied slot of the array
} u1024_t;

The problem is that I don't understand how the numbers are stored in this format. What I need is being able to print the real numbers in some way, or at least a way to recover the numbers from the components of the arrays.

I tried just concatenating them like strings, but it doesn't seem right.

Thanks to whoever will be able to help!

Upvotes: 0

Views: 93

Answers (1)

lokiuox
lokiuox

Reputation: 11

Thank you @Matthieu! Your comment worked. I needed to concatenate the unsigned long longs in reverse order and reversing their bytes due to endianness.

Following his solution, I implemented this function, which works perfectly:

void print_u1024(u1024_t number) {
    int size = (number.top + 1) * sizeof(u64);
    for (int i = size-1; i >= 0; i--) {
        printf("%02x", ((unsigned char*)number.arr)[i]);
    }
    printf("\n");
}

Please note that this solution will probably only work on little-endian systems (most PCs).

Upvotes: 1

Related Questions