Açıklama Yok

aescrypt.c 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
  4. The redistribution and use of this software (with or without changes)
  5. is allowed without the payment of fees or royalties provided that:
  6. source code distributions include the above copyright notice, this
  7. list of conditions and the following disclaimer;
  8. binary distributions include the above copyright notice, this list
  9. of conditions and the following disclaimer in their documentation.
  10. This software is provided 'as is' with no explicit or implied warranties
  11. in respect of its operation, including, but not limited to, correctness
  12. and fitness for purpose.
  13. ---------------------------------------------------------------------------
  14. Issue Date: 20/12/2007
  15. */
  16. #include "aesopt.h"
  17. #include "aestab.h"
  18. #if defined(__cplusplus)
  19. extern "C"
  20. {
  21. #endif
  22. #define si(y,x,k,c) (s(y,c) = word_in(x, c) ^ (k)[c])
  23. #define so(y,x,c) word_out(y, c, s(x,c))
  24. #if defined(ARRAYS)
  25. #define locals(y,x) x[4],y[4]
  26. #else
  27. #define locals(y,x) x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3
  28. #endif
  29. #define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  30. s(y,2) = s(x,2); s(y,3) = s(x,3);
  31. #define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3)
  32. #define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3)
  33. #define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3)
  34. #if ( FUNCS_IN_C & ENCRYPTION_IN_C )
  35. /* Visual C++ .Net v7.1 provides the fastest encryption code when using
  36. Pentium optimiation with small code but this is poor for decryption
  37. so we need to control this with the following VC++ pragmas
  38. */
  39. #if defined( _MSC_VER ) && !defined( _WIN64 )
  40. #pragma optimize( "s", on )
  41. #endif
  42. /* Given the column (c) of the output state variable, the following
  43. macros give the input state variables which are needed in its
  44. computation for each row (r) of the state. All the alternative
  45. macros give the same end values but expand into different ways
  46. of calculating these values. In particular the complex macro
  47. used for dynamically variable block sizes is designed to expand
  48. to a compile time constant whenever possible but will expand to
  49. conditional clauses on some branches (I am grateful to Frank
  50. Yellin for this construction)
  51. */
  52. #define fwd_var(x,r,c)\
  53. ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\
  54. : r == 1 ? ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))\
  55. : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\
  56. : ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2)))
  57. #if defined(FT4_SET)
  58. #undef dec_fmvars
  59. #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c))
  60. #elif defined(FT1_SET)
  61. #undef dec_fmvars
  62. #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(f,n),fwd_var,rf1,c))
  63. #else
  64. #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ fwd_mcol(no_table(x,t_use(s,box),fwd_var,rf1,c)))
  65. #endif
  66. #if defined(FL4_SET)
  67. #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,l),fwd_var,rf1,c))
  68. #elif defined(FL1_SET)
  69. #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(f,l),fwd_var,rf1,c))
  70. #else
  71. #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(s,box),fwd_var,rf1,c))
  72. #endif
  73. AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1])
  74. { uint_32t locals(b0, b1);
  75. const uint_32t *kp;
  76. #if defined( dec_fmvars )
  77. dec_fmvars; /* declare variables for fwd_mcol() if needed */
  78. #endif
  79. if( cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16 )
  80. return EXIT_FAILURE;
  81. kp = cx->ks;
  82. state_in(b0, in, kp);
  83. #if (ENC_UNROLL == FULL)
  84. switch(cx->inf.b[0])
  85. {
  86. case 14 * 16:
  87. round(fwd_rnd, b1, b0, kp + 1 * N_COLS);
  88. round(fwd_rnd, b0, b1, kp + 2 * N_COLS);
  89. kp += 2 * N_COLS;
  90. case 12 * 16:
  91. round(fwd_rnd, b1, b0, kp + 1 * N_COLS);
  92. round(fwd_rnd, b0, b1, kp + 2 * N_COLS);
  93. kp += 2 * N_COLS;
  94. case 10 * 16:
  95. round(fwd_rnd, b1, b0, kp + 1 * N_COLS);
  96. round(fwd_rnd, b0, b1, kp + 2 * N_COLS);
  97. round(fwd_rnd, b1, b0, kp + 3 * N_COLS);
  98. round(fwd_rnd, b0, b1, kp + 4 * N_COLS);
  99. round(fwd_rnd, b1, b0, kp + 5 * N_COLS);
  100. round(fwd_rnd, b0, b1, kp + 6 * N_COLS);
  101. round(fwd_rnd, b1, b0, kp + 7 * N_COLS);
  102. round(fwd_rnd, b0, b1, kp + 8 * N_COLS);
  103. round(fwd_rnd, b1, b0, kp + 9 * N_COLS);
  104. round(fwd_lrnd, b0, b1, kp +10 * N_COLS);
  105. }
  106. #else
  107. #if (ENC_UNROLL == PARTIAL)
  108. { uint_32t rnd;
  109. for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd)
  110. {
  111. kp += N_COLS;
  112. round(fwd_rnd, b1, b0, kp);
  113. kp += N_COLS;
  114. round(fwd_rnd, b0, b1, kp);
  115. }
  116. kp += N_COLS;
  117. round(fwd_rnd, b1, b0, kp);
  118. #else
  119. { uint_32t rnd;
  120. for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd)
  121. {
  122. kp += N_COLS;
  123. round(fwd_rnd, b1, b0, kp);
  124. l_copy(b0, b1);
  125. }
  126. #endif
  127. kp += N_COLS;
  128. round(fwd_lrnd, b0, b1, kp);
  129. }
  130. #endif
  131. state_out(out, b0);
  132. return EXIT_SUCCESS;
  133. }
  134. #endif
  135. #if ( FUNCS_IN_C & DECRYPTION_IN_C)
  136. /* Visual C++ .Net v7.1 provides the fastest encryption code when using
  137. Pentium optimiation with small code but this is poor for decryption
  138. so we need to control this with the following VC++ pragmas
  139. */
  140. #if defined( _MSC_VER ) && !defined( _WIN64 )
  141. #pragma optimize( "t", on )
  142. #endif
  143. /* Given the column (c) of the output state variable, the following
  144. macros give the input state variables which are needed in its
  145. computation for each row (r) of the state. All the alternative
  146. macros give the same end values but expand into different ways
  147. of calculating these values. In particular the complex macro
  148. used for dynamically variable block sizes is designed to expand
  149. to a compile time constant whenever possible but will expand to
  150. conditional clauses on some branches (I am grateful to Frank
  151. Yellin for this construction)
  152. */
  153. #define inv_var(x,r,c)\
  154. ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\
  155. : r == 1 ? ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))\
  156. : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\
  157. : ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0)))
  158. #if defined(IT4_SET)
  159. #undef dec_imvars
  160. #define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,n),inv_var,rf1,c))
  161. #elif defined(IT1_SET)
  162. #undef dec_imvars
  163. #define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(i,n),inv_var,rf1,c))
  164. #else
  165. #define inv_rnd(y,x,k,c) (s(y,c) = inv_mcol((k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c)))
  166. #endif
  167. #if defined(IL4_SET)
  168. #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,l),inv_var,rf1,c))
  169. #elif defined(IL1_SET)
  170. #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(i,l),inv_var,rf1,c))
  171. #else
  172. #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c))
  173. #endif
  174. /* This code can work with the decryption key schedule in the */
  175. /* order that is used for encrytpion (where the 1st decryption */
  176. /* round key is at the high end ot the schedule) or with a key */
  177. /* schedule that has been reversed to put the 1st decryption */
  178. /* round key at the low end of the schedule in memory (when */
  179. /* AES_REV_DKS is defined) */
  180. #ifdef AES_REV_DKS
  181. #define key_ofs 0
  182. #define rnd_key(n) (kp + n * N_COLS)
  183. #else
  184. #define key_ofs 1
  185. #define rnd_key(n) (kp - n * N_COLS)
  186. #endif
  187. AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1])
  188. { uint_32t locals(b0, b1);
  189. #if defined( dec_imvars )
  190. dec_imvars; /* declare variables for inv_mcol() if needed */
  191. #endif
  192. const uint_32t *kp;
  193. if( cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16 )
  194. return EXIT_FAILURE;
  195. kp = cx->ks + (key_ofs ? (cx->inf.b[0] >> 2) : 0);
  196. state_in(b0, in, kp);
  197. #if (DEC_UNROLL == FULL)
  198. kp = cx->ks + (key_ofs ? 0 : (cx->inf.b[0] >> 2));
  199. switch(cx->inf.b[0])
  200. {
  201. case 14 * 16:
  202. round(inv_rnd, b1, b0, rnd_key(-13));
  203. round(inv_rnd, b0, b1, rnd_key(-12));
  204. case 12 * 16:
  205. round(inv_rnd, b1, b0, rnd_key(-11));
  206. round(inv_rnd, b0, b1, rnd_key(-10));
  207. case 10 * 16:
  208. round(inv_rnd, b1, b0, rnd_key(-9));
  209. round(inv_rnd, b0, b1, rnd_key(-8));
  210. round(inv_rnd, b1, b0, rnd_key(-7));
  211. round(inv_rnd, b0, b1, rnd_key(-6));
  212. round(inv_rnd, b1, b0, rnd_key(-5));
  213. round(inv_rnd, b0, b1, rnd_key(-4));
  214. round(inv_rnd, b1, b0, rnd_key(-3));
  215. round(inv_rnd, b0, b1, rnd_key(-2));
  216. round(inv_rnd, b1, b0, rnd_key(-1));
  217. round(inv_lrnd, b0, b1, rnd_key( 0));
  218. }
  219. #else
  220. #if (DEC_UNROLL == PARTIAL)
  221. { uint_32t rnd;
  222. for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd)
  223. {
  224. kp = rnd_key(1);
  225. round(inv_rnd, b1, b0, kp);
  226. kp = rnd_key(1);
  227. round(inv_rnd, b0, b1, kp);
  228. }
  229. kp = rnd_key(1);
  230. round(inv_rnd, b1, b0, kp);
  231. #else
  232. { uint_32t rnd;
  233. for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd)
  234. {
  235. kp = rnd_key(1);
  236. round(inv_rnd, b1, b0, kp);
  237. l_copy(b0, b1);
  238. }
  239. #endif
  240. kp = rnd_key(1);
  241. round(inv_lrnd, b0, b1, kp);
  242. }
  243. #endif
  244. state_out(out, b0);
  245. return EXIT_SUCCESS;
  246. }
  247. #endif
  248. #if defined(__cplusplus)
  249. }
  250. #endif