How to compile/run C code to invoke libtomcrypt AES 2KB lookup table based implementation

2

In Libtomcrypt crypto library, AES encryption/decryption are implemented in two different way .

  1. Use of lookup table of size 8KB (encryption)/5KB (decryption).
  2. Use of lookup table of size 2KB (encryption)/2KB (decryption). In this case LTC_SMALL_CODE is true.

This is the source code of aes_tab.c and aes.c.

#ifdef LTC_SMALL_CODE

#define Te0(x) TE0[x]
#define Te1(x) RORc(TE0[x], 8)
#define Te2(x) RORc(TE0[x], 16)
#define Te3(x) RORc(TE0[x], 24)

#define Td0(x) TD0[x]
#define Td1(x) RORc(TD0[x], 8)
#define Td2(x) RORc(TD0[x], 16)
#define Td3(x) RORc(TD0[x], 24)

#define Te4_0 0x000000FF & Te4
#define Te4_1 0x0000FF00 & Te4
#define Te4_2 0x00FF0000 & Te4
#define Te4_3 0xFF000000 & Te4

#else

#define Te0(x) TE0[x]
#define Te1(x) TE1[x]
#define Te2(x) TE2[x]
#define Te3(x) TE3[x]

#define Td0(x) TD0[x]
#define Td1(x) TD1[x]
#define Td2(x) TD2[x]
#define Td3(x) TD3[x]

#endif /* ENCRYPT_ONLY */

#endif /* SMALL CODE */

The following C code performs AES encryption and decryption using libtomcrypt crypto library. However, the code invokes AES implementation that uses 8KB/5KB lookup table (means LTC_SMALL_CODE condition becomes false).

//aes_tom_example.c
#include <tomcrypt.h>

static const unsigned char key[] = {
    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};

int main()
{

    unsigned char text[]="hello world!";
    unsigned char enc_out[80];
    unsigned char dec_out[80];
    symmetric_key skey;
    int keysize = 32;
    int status;

    status = aes_keysize(&keysize);


    status = aes_setup(key, 32, 0, &skey);

    status = aes_ecb_encrypt(text,enc_out,&skey);

    status = aes_ecb_decrypt(enc_out, dec_out, &skey);


    int i;

    printf("original:\t");
    for(i=0;*(text+i)!=0x00;i++)
        printf("%c ",*(text+i));
    printf("\nencrypted:\t");
    for(i=0;*(enc_out+i)!=0x00;i++)
        printf("%X ",*(enc_out+i));
    printf("\ndecrypted:\t");
    for(i=0;*(dec_out+i)!=0x00;i++)
        printf("%c ",*(dec_out+i));
    printf("\n");

    return 0;
} 

Compile and run as below ,

gcc aes_tom_example.c -o aes -ltomcrypt
./aes

Sample Output 

original:   h e l l o   w o r l d ! 
encrypted:  AE 21 D5 A5 5E D5 F1 EF 6D FC E5 30 60 34 3D 12 
decrypted:  h e l l o   w o r l d ! 

My questions are:

  • How to modify this C code so that it invokes the #ifdef LTC_SMALL_CODE condition part (means 2KB lookup table based implementation code invoked )?

  • How to run the above code with LTC_SMALL_CODE condition true?

Do I need some parameter before calling SETUP (aes_setup) function ? Or Do I need to pass some parameters at the time of compilation/run time?

It would be great if anyone can provide some link or sample code.

I am using Ubuntu 16.04 / Debian 8. gcc v-4.9.

c
gcc
encryption
aes
libtomcrypt
asked on Stack Overflow Jan 29, 2018 by bholanath • edited Jan 29, 2018 by Pablo

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0