Question

I am using a toolkit to do some Elliptical Curve Cryptography on an ATMega2560. When trying to use the print functions in the toolkit I am getting an empty string. I know the print functions work because the x86 version prints the variables without a problem. I am not experienced with ATMega and would love any help on this matter. The print code is included below.

Code to print a big number (it itself calls a util_print)

void bn_print(bn_t a) {
int i;

if (a->sign == BN_NEG) {
    util_print("-");
}
if (a->used == 0) {
    util_print("0\n");
} else {
#if WORD == 64
    util_print("%lX", (unsigned long int)a->dp[a->used - 1]);
    for (i = a->used - 2; i >= 0; i--) {
        util_print("%.*lX", (int)(2 * (BN_DIGIT / 8)),
                (unsigned long int)a->dp[i]);
    }
#else
    util_print("%llX", (unsigned long long int)a->dp[a->used - 1]);
    for (i = a->used - 2; i >= 0; i--) {
        util_print("%.*llX", (int)(2 * (BN_DIGIT / 8)),
                (unsigned long long int)a->dp[i]);
    }
#endif
    util_print("\n");
}
}

The code to actually print a big number variable:

static char buffer[64 + 1];
void util_printf(char *format, ...) {
#ifndef QUIET
#if ARCH == AVR
char *pointer = &buffer[1];
va_list list;
va_start(list, format);
vsnprintf(pointer, 128, format, list);
buffer[0] = (unsigned char)2;
va_end(list);
#elif ARCH == MSP
va_list list;
va_start(list, format);
vprintf(format, list);
va_end(list);
#else
va_list list;
va_start(list, format);
vprintf(format, list);
fflush(stdout);
va_end(list);
#endif
#endif
}

edit: I do have UART initialized and can output printf statments to a console.

Was it helpful?

Solution 3

Alright so I found a workaround to print the bn numbers to a stdout on an ATMega2560. The toolkit comes with a function that writes a variable to a string (bn_write_str). So I implemented my own print function as such:

void print_bn(bn_t a)
{
    char print[BN_SIZE]; // max precision of a bn number
    int bi = bn_bits(a); // get the number of bits of the number
    bn_write_str(print, bi, a, 16) // 16 indicates the radix (hexadecimal)
    printf("%s\n"), print);
}

This function will print a bn number in hexadecimal format. Hope this helps anyone using the RELIC toolkit with an AVR.

This skips the util_print calls.

OTHER TIPS

I'm one of the authors of the RELIC toolkit. The current util_printf() function is used to print inside the Avrora simulator, for debugging purposes. I'm glad that you could adapt the code to your purposes. As a side note, the buffer size problem was already fixed in more recent releases of the toolkit.

Let me know you have further problems with the library. You can either contact me personally or write directly to the discussion group.

Thank you!

vsnprintf store it's output on the given buffer (which in this case is the address point by pointer variable), in order for it to show on the console (through UART) you must send your buffer using printf (try to add printf("%s", pointer) after vsnprintf), if you're using avr-libc don't forget to initialized std stream first before making any call to printf function

oh btw your code is vulnerable to buffer overflow attack, buffer[64 + 1] means your buffer size is only 65 bytes, vsnprintf(pointer, 128, format, list); means that the maximum buffer defined by your application is 128 bytes, try to change it below 65 bytes in order to avoid overflow

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top