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.

Pablo
  • 13,271
  • 4
  • 39
  • 59
bholanath
  • 1,699
  • 1
  • 22
  • 40
  • 1
    By defining the `LTC_SMALL_CODE` macro? – Pablo Jan 29 '18 at 16:54
  • @Pablo Thanks for your prompt reply. The macro is already defined inside the crypto library. So, how to invoke at run time ? – bholanath Jan 29 '18 at 16:55
  • You could insert the line `#define LTC_SMALL_CODE` at the beginning of the file. A better way is to supply the `-DLTC_SMALL_CODE` on the compiler command line. – President James K. Polk Jan 29 '18 at 16:56
  • You don't, you can't, macros are resolved and evaluated at compile time. If you want a piece of code that is encapsulated between `#ifndef VAR` or `#ifdef VAR` macros, then you have to set `VAR` at compile time. – Pablo Jan 29 '18 at 16:58
  • @JamesKPolk, -DLTC_SMALL_CODE at the time of build libtomcrypt library . Am I right ? Let me check by building with that option. – bholanath Jan 29 '18 at 17:01
  • 2
    @JamesKPolk, I have built libtomcrypt library with -DLTC_SMALL_CODE option as (make -f makefile.shared CFLAGS="-DLTC_SMALL_CODE") and it worked as expected. Thank you so much. You may write an answer I will accept that. – bholanath Jan 29 '18 at 17:24
  • Nah, you can go ahead and answer your own question and accept your answer. – President James K. Polk Jan 29 '18 at 17:25
  • Note that ECB mode is insecure. You could try AES-CTR or AES-GCM instead (don't forget the nonce). A possible reason why the faster AES mode is only available during encryption (according to your question) could be that AES-CTR only uses the AES block cipher one way. – Maarten Bodewes Jan 29 '18 at 18:54

0 Answers0