aestab.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #ifndef HAVE_CRYPTO
  30. #define DO_TABLES
  31. #include "aesopt.h"
  32. #if defined(FIXED_TABLES)
  33. /* implemented in case of wrong call for fixed tables */
  34. void gen_tabs(void)
  35. {
  36. }
  37. #else /* dynamic table generation */
  38. #if !defined(FF_TABLES)
  39. /* Generate the tables for the dynamic table option
  40. It will generally be sensible to use tables to compute finite
  41. field multiplies and inverses but where memory is scarse this
  42. code might sometimes be better. But it only has effect during
  43. initialisation so its pretty unimportant in overall terms.
  44. */
  45. /* return 2 ^ (n - 1) where n is the bit number of the highest bit
  46. set in x with x in the range 1 < x < 0x00000200. This form is
  47. used so that locals within fi can be bytes rather than words
  48. */
  49. static aes_08t hibit(const aes_32t x)
  50. { aes_08t r = (aes_08t)((x >> 1) | (x >> 2));
  51. r |= (r >> 2);
  52. r |= (r >> 4);
  53. return (r + 1) >> 1;
  54. }
  55. /* return the inverse of the finite field element x */
  56. static aes_08t fi(const aes_08t x)
  57. { aes_08t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;
  58. if(x < 2) return x;
  59. for(;;)
  60. {
  61. if(!n1) return v1;
  62. while(n2 >= n1)
  63. {
  64. n2 /= n1; p2 ^= p1 * n2; v2 ^= v1 * n2; n2 = hibit(p2);
  65. }
  66. if(!n2) return v2;
  67. while(n1 >= n2)
  68. {
  69. n1 /= n2; p1 ^= p2 * n1; v1 ^= v2 * n1; n1 = hibit(p1);
  70. }
  71. }
  72. }
  73. #endif
  74. /* The forward and inverse affine transformations used in the S-box */
  75. #define fwd_affine(x) \
  76. (w = (aes_32t)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(aes_08t)(w^(w>>8)))
  77. #define inv_affine(x) \
  78. (w = (aes_32t)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(aes_08t)(w^(w>>8)))
  79. static int init = 0;
  80. void gen_tabs(void)
  81. { aes_32t i, w;
  82. #if defined(FF_TABLES)
  83. aes_08t pow[512], log[256];
  84. if(init) return;
  85. /* log and power tables for GF(2^8) finite field with
  86. WPOLY as modular polynomial - the simplest primitive
  87. root is 0x03, used here to generate the tables
  88. */
  89. i = 0; w = 1;
  90. do
  91. {
  92. pow[i] = (aes_08t)w;
  93. pow[i + 255] = (aes_08t)w;
  94. log[w] = (aes_08t)i++;
  95. w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0);
  96. }
  97. while (w != 1);
  98. #else
  99. if(init) return;
  100. #endif
  101. for(i = 0, w = 1; i < RC_LENGTH; ++i)
  102. {
  103. t_set(r,c)[i] = bytes2word(w, 0, 0, 0);
  104. w = f2(w);
  105. }
  106. for(i = 0; i < 256; ++i)
  107. { aes_08t b;
  108. b = fwd_affine(fi((aes_08t)i));
  109. w = bytes2word(f2(b), b, b, f3(b));
  110. #ifdef SBX_SET
  111. t_set(s,box)[i] = b;
  112. #endif
  113. #ifdef FT1_SET /* tables for a normal encryption round */
  114. t_set(f,n)[i] = w;
  115. #endif
  116. #ifdef FT4_SET
  117. t_set(f,n)[0][i] = w;
  118. t_set(f,n)[1][i] = upr(w,1);
  119. t_set(f,n)[2][i] = upr(w,2);
  120. t_set(f,n)[3][i] = upr(w,3);
  121. #endif
  122. w = bytes2word(b, 0, 0, 0);
  123. #ifdef FL1_SET /* tables for last encryption round (may also */
  124. t_set(f,l)[i] = w; /* be used in the key schedule) */
  125. #endif
  126. #ifdef FL4_SET
  127. t_set(f,l)[0][i] = w;
  128. t_set(f,l)[1][i] = upr(w,1);
  129. t_set(f,l)[2][i] = upr(w,2);
  130. t_set(f,l)[3][i] = upr(w,3);
  131. #endif
  132. #ifdef LS1_SET /* table for key schedule if t_set(f,l) above is */
  133. t_set(l,s)[i] = w; /* not of the required form */
  134. #endif
  135. #ifdef LS4_SET
  136. t_set(l,s)[0][i] = w;
  137. t_set(l,s)[1][i] = upr(w,1);
  138. t_set(l,s)[2][i] = upr(w,2);
  139. t_set(l,s)[3][i] = upr(w,3);
  140. #endif
  141. b = fi(inv_affine((aes_08t)i));
  142. w = bytes2word(fe(b), f9(b), fd(b), fb(b));
  143. #ifdef IM1_SET /* tables for the inverse mix column operation */
  144. t_set(i,m)[b] = w;
  145. #endif
  146. #ifdef IM4_SET
  147. t_set(i,m)[0][b] = w;
  148. t_set(i,m)[1][b] = upr(w,1);
  149. t_set(i,m)[2][b] = upr(w,2);
  150. t_set(i,m)[3][b] = upr(w,3);
  151. #endif
  152. #ifdef ISB_SET
  153. t_set(i,box)[i] = b;
  154. #endif
  155. #ifdef IT1_SET /* tables for a normal decryption round */
  156. t_set(i,n)[i] = w;
  157. #endif
  158. #ifdef IT4_SET
  159. t_set(i,n)[0][i] = w;
  160. t_set(i,n)[1][i] = upr(w,1);
  161. t_set(i,n)[2][i] = upr(w,2);
  162. t_set(i,n)[3][i] = upr(w,3);
  163. #endif
  164. w = bytes2word(b, 0, 0, 0);
  165. #ifdef IL1_SET /* tables for last decryption round */
  166. t_set(i,l)[i] = w;
  167. #endif
  168. #ifdef IL4_SET
  169. t_set(i,l)[0][i] = w;
  170. t_set(i,l)[1][i] = upr(w,1);
  171. t_set(i,l)[2][i] = upr(w,2);
  172. t_set(i,l)[3][i] = upr(w,3);
  173. #endif
  174. }
  175. init = 1;
  176. }
  177. #endif
  178. #endif /* !HAVE_CRYPTO */
  179. #if defined(__cplusplus)
  180. }
  181. #endif