aestab.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 2003, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
  4. All rights reserved.
  5. LICENSE TERMS
  6. The free distribution and use of this software in both source and binary
  7. form is allowed (with or without changes) provided that:
  8. 1. distributions of this source code include the above copyright
  9. notice, this list of conditions and the following disclaimer;
  10. 2. distributions in binary form include the above copyright
  11. notice, this list of conditions and the following disclaimer
  12. in the documentation and/or other associated materials;
  13. 3. the copyright holder's name is not used to endorse products
  14. built using this software without specific written permission.
  15. ALTERNATIVELY, provided that this notice is retained in full, this product
  16. may be distributed under the terms of the GNU General Public License (GPL),
  17. in which case the provisions of the GPL apply INSTEAD OF those given above.
  18. DISCLAIMER
  19. This software is provided 'as is' with no explicit or implied warranties
  20. in respect of its properties, including, but not limited to, correctness
  21. and/or fitness for purpose.
  22. ---------------------------------------------------------------------------
  23. Issue Date: 26/08/2003
  24. */
  25. #if defined(__cplusplus)
  26. extern "C"
  27. {
  28. #endif
  29. #define DO_TABLES
  30. #include "aesopt.h"
  31. #if defined(FIXED_TABLES)
  32. /* implemented in case of wrong call for fixed tables */
  33. void gen_tabs(void)
  34. {
  35. }
  36. #else /* dynamic table generation */
  37. #if !defined(FF_TABLES)
  38. /* Generate the tables for the dynamic table option
  39. It will generally be sensible to use tables to compute finite
  40. field multiplies and inverses but where memory is scarse this
  41. code might sometimes be better. But it only has effect during
  42. initialisation so its pretty unimportant in overall terms.
  43. */
  44. /* return 2 ^ (n - 1) where n is the bit number of the highest bit
  45. set in x with x in the range 1 < x < 0x00000200. This form is
  46. used so that locals within fi can be bytes rather than words
  47. */
  48. static aes_08t hibit(const aes_32t x)
  49. { aes_08t r = (aes_08t)((x >> 1) | (x >> 2));
  50. r |= (r >> 2);
  51. r |= (r >> 4);
  52. return (r + 1) >> 1;
  53. }
  54. /* return the inverse of the finite field element x */
  55. static aes_08t fi(const aes_08t x)
  56. { aes_08t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;
  57. if(x < 2) return x;
  58. for(;;)
  59. {
  60. if(!n1) return v1;
  61. while(n2 >= n1)
  62. {
  63. n2 /= n1; p2 ^= p1 * n2; v2 ^= v1 * n2; n2 = hibit(p2);
  64. }
  65. if(!n2) return v2;
  66. while(n1 >= n2)
  67. {
  68. n1 /= n2; p1 ^= p2 * n1; v1 ^= v2 * n1; n1 = hibit(p1);
  69. }
  70. }
  71. }
  72. #endif
  73. /* The forward and inverse affine transformations used in the S-box */
  74. #define fwd_affine(x) \
  75. (w = (aes_32t)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(aes_08t)(w^(w>>8)))
  76. #define inv_affine(x) \
  77. (w = (aes_32t)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(aes_08t)(w^(w>>8)))
  78. static int init = 0;
  79. void gen_tabs(void)
  80. { aes_32t i, w;
  81. #if defined(FF_TABLES)
  82. aes_08t pow[512], log[256];
  83. if(init) return;
  84. /* log and power tables for GF(2^8) finite field with
  85. WPOLY as modular polynomial - the simplest primitive
  86. root is 0x03, used here to generate the tables
  87. */
  88. i = 0; w = 1;
  89. do
  90. {
  91. pow[i] = (aes_08t)w;
  92. pow[i + 255] = (aes_08t)w;
  93. log[w] = (aes_08t)i++;
  94. w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0);
  95. }
  96. while (w != 1);
  97. #else
  98. if(init) return;
  99. #endif
  100. for(i = 0, w = 1; i < RC_LENGTH; ++i)
  101. {
  102. t_set(r,c)[i] = bytes2word(w, 0, 0, 0);
  103. w = f2(w);
  104. }
  105. for(i = 0; i < 256; ++i)
  106. { aes_08t b;
  107. b = fwd_affine(fi((aes_08t)i));
  108. w = bytes2word(f2(b), b, b, f3(b));
  109. #ifdef SBX_SET
  110. t_set(s,box)[i] = b;
  111. #endif
  112. #ifdef FT1_SET /* tables for a normal encryption round */
  113. t_set(f,n)[i] = w;
  114. #endif
  115. #ifdef FT4_SET
  116. t_set(f,n)[0][i] = w;
  117. t_set(f,n)[1][i] = upr(w,1);
  118. t_set(f,n)[2][i] = upr(w,2);
  119. t_set(f,n)[3][i] = upr(w,3);
  120. #endif
  121. w = bytes2word(b, 0, 0, 0);
  122. #ifdef FL1_SET /* tables for last encryption round (may also */
  123. t_set(f,l)[i] = w; /* be used in the key schedule) */
  124. #endif
  125. #ifdef FL4_SET
  126. t_set(f,l)[0][i] = w;
  127. t_set(f,l)[1][i] = upr(w,1);
  128. t_set(f,l)[2][i] = upr(w,2);
  129. t_set(f,l)[3][i] = upr(w,3);
  130. #endif
  131. #ifdef LS1_SET /* table for key schedule if t_set(f,l) above is */
  132. t_set(l,s)[i] = w; /* not of the required form */
  133. #endif
  134. #ifdef LS4_SET
  135. t_set(l,s)[0][i] = w;
  136. t_set(l,s)[1][i] = upr(w,1);
  137. t_set(l,s)[2][i] = upr(w,2);
  138. t_set(l,s)[3][i] = upr(w,3);
  139. #endif
  140. b = fi(inv_affine((aes_08t)i));
  141. w = bytes2word(fe(b), f9(b), fd(b), fb(b));
  142. #ifdef IM1_SET /* tables for the inverse mix column operation */
  143. t_set(i,m)[b] = w;
  144. #endif
  145. #ifdef IM4_SET
  146. t_set(i,m)[0][b] = w;
  147. t_set(i,m)[1][b] = upr(w,1);
  148. t_set(i,m)[2][b] = upr(w,2);
  149. t_set(i,m)[3][b] = upr(w,3);
  150. #endif
  151. #ifdef ISB_SET
  152. t_set(i,box)[i] = b;
  153. #endif
  154. #ifdef IT1_SET /* tables for a normal decryption round */
  155. t_set(i,n)[i] = w;
  156. #endif
  157. #ifdef IT4_SET
  158. t_set(i,n)[0][i] = w;
  159. t_set(i,n)[1][i] = upr(w,1);
  160. t_set(i,n)[2][i] = upr(w,2);
  161. t_set(i,n)[3][i] = upr(w,3);
  162. #endif
  163. w = bytes2word(b, 0, 0, 0);
  164. #ifdef IL1_SET /* tables for last decryption round */
  165. t_set(i,l)[i] = w;
  166. #endif
  167. #ifdef IL4_SET
  168. t_set(i,l)[0][i] = w;
  169. t_set(i,l)[1][i] = upr(w,1);
  170. t_set(i,l)[2][i] = upr(w,2);
  171. t_set(i,l)[3][i] = upr(w,3);
  172. #endif
  173. }
  174. init = 1;
  175. }
  176. #endif
  177. #if defined(__cplusplus)
  178. }
  179. #endif