libtom / libtomcrypt

LibTomCrypt is a fairly comprehensive, modular and portable cryptographic toolkit that provides developers with a vast array of well known published block ciphers, one-way hash functions, chaining modes, pseudo-random number generators, public key cryptography and a plethora of other routines.
https://www.libtom.net
Other
1.51k stars 449 forks source link

Don't get the right answers when cross-compiled on RISC-V processor #578

Open gunslnger42 opened 2 years ago

gunslnger42 commented 2 years ago

Description

I'm cross-compiling the basic library with just AES cipher (CTR, ECB, and GCM modes only) on a RISC-V processor and I'm comparing to a test program running on an Intel PC, but I do not get the same results.

Steps to Reproduce

This is the relevant part of my test program on the PC. The program on the RISC-V is calling the same library functions in the same order. Using the same key, IV, and input, I get different ciphertext output.

symmetric_CTR g_ctr_key;
int cipher_idx = 0;

void StartCtr(unsigned char* key)
{
    unsigned char blank_iv[IVSIZE] = { 0 };
    int err = ctr_start(cipher_idx, blank_iv, key, KEYSIZE, 0, CTR_COUNTER_BIG_ENDIAN, &g_ctr_key);
}

void DoEncryptCtr(unsigned char* IV, unsigned char* inbuf)
{
    unsigned char ciphertext[BLOCKSIZE];
    int err = ctr_setiv(IV, IVSIZE, &g_ctr_key);
    err = ctr_encrypt(inbuf, ciphertext, BLOCKSIZE, &g_ctr_key);
    // print out ciphertext here
}

void StopCtr(void)
{
    int err = ctr_done(&g_ctr_key);
}

void main( void )
{
    unsigned char key[KEYSIZE];
    unsigned char IV[IVSIZE];
    unsigned char inbuf[BLOCKSIZE];

    // register ciphers and get cipher_idx here

    StartCtr(key);
    DoEncryptCtr(IV, inbuf);
    StopCtr();
}

Version

I'm using version 1.18.2 in both programs. I'm using Visual Studio 2019 Community on the PC and the GCC compiler from Microsemi/Microchip that comes with SoftConsole 2021.1. It's using GNU RISC-V Cross Compiler gcc version 8.3.0 "riscv64-unknown-elf-gcc -march=rv32i -mabi=ilp32 -msmall-data-limit=8"