title: How to compile/run C code to invoke libtomcrypt AES 2KB lookup table based implementation
body: <p>In Libtomcrypt crypto library, AES encryption/decryption are implemented in two different way . </p>
  
  <ol>
  <li>Use of lookup table of size 8KB (encryption)/5KB (decryption).</li>
  <li>Use of lookup table of size 2KB (encryption)/2KB (decryption). In this case <code>LTC_SMALL_CODE</code> is true.</li>
  </ol>
  
  <p>This is the source code of <a href="https://github.com/t-d-k/LibreCrypt/blob/master/src/3rd_party/libtomcrypt/crypt-1.17/src/ciphers/aes/aes_tab.c" rel="nofollow noreferrer"><code>aes_tab.c</code></a> and <a href="https://github.com/nabto/unabto/blob/master/3rdparty/libtomcrypt/src/ciphers/aes/aes.c" rel="nofollow noreferrer"><code>aes.c</code></a>.</p>
  
  <pre><code>#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 &amp; Te4
  #define Te4_1 0x0000FF00 &amp; Te4
  #define Te4_2 0x00FF0000 &amp; Te4
  #define Te4_3 0xFF000000 &amp; 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 */
  </code></pre>
  
  <p>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 <code>LTC_SMALL_CODE</code> condition becomes <code>false</code>).</p>
  
  <pre><code>//aes_tom_example.c
  #include &lt;tomcrypt.h&gt;
  
  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(&amp;keysize);
  
  
      status = aes_setup(key, 32, 0, &amp;skey);
  
      status = aes_ecb_encrypt(text,enc_out,&amp;skey);
  
      status = aes_ecb_decrypt(enc_out, dec_out, &amp;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;
  } 
  </code></pre>
  
  <p>Compile and run as below ,</p>
  
  <pre><code>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 ! 
  </code></pre>
  
  <p>My questions are:</p>
  
  <ul>
  <li><p>How to modify this C code so that it invokes the <code>#ifdef LTC_SMALL_CODE</code> condition part (means 2KB lookup table based implementation code invoked )?</p></li>
  <li><p>How to run the above code with <code>LTC_SMALL_CODE</code> condition <code>true</code>?</p></li>
  </ul>
  
  <p>Do I need some parameter before calling SETUP (<code>aes_setup</code>) function ? Or Do I need to pass some parameters at the time of compilation/run time?</p>
  
  <p>It would be great if anyone can provide some link or sample code.</p>
  
  <p>I am using Ubuntu 16.04 / Debian 8. gcc v-4.9.</p>