aescrypt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. /*! \file
  26. \brief This file contains the code for implementing encryption and decryption
  27. for AES (Rijndael) for block and key sizes of 16, 24 and 32 bytes. It
  28. can optionally be replaced by code written in assembler using NASM. For
  29. further details see the file aesopt.h
  30. */
  31. #include "aesopt.h"
  32. #if defined(__cplusplus)
  33. extern "C"
  34. {
  35. #endif
  36. #define si(y,x,k,c) (s(y,c) = word_in(x, c) ^ (k)[c])
  37. #define so(y,x,c) word_out(y, c, s(x,c))
  38. #if defined(ARRAYS)
  39. #define locals(y,x) x[4],y[4]
  40. #else
  41. #define locals(y,x) x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3
  42. #endif
  43. #define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  44. s(y,2) = s(x,2); s(y,3) = s(x,3);
  45. #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)
  46. #define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3)
  47. #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)
  48. #if defined(ENCRYPTION) && !defined(AES_ASM)
  49. /* Visual C++ .Net v7.1 provides the fastest encryption code when using
  50. Pentium optimiation with small code but this is poor for decryption
  51. so we need to control this with the following VC++ pragmas
  52. */
  53. #if defined(_MSC_VER)
  54. #pragma optimize( "s", on )
  55. #endif
  56. /* Given the column (c) of the output state variable, the following
  57. macros give the input state variables which are needed in its
  58. computation for each row (r) of the state. All the alternative
  59. macros give the same end values but expand into different ways
  60. of calculating these values. In particular the complex macro
  61. used for dynamically variable block sizes is designed to expand
  62. to a compile time constant whenever possible but will expand to
  63. conditional clauses on some branches (I am grateful to Frank
  64. Yellin for this construction)
  65. */
  66. #define fwd_var(x,r,c)\
  67. ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\
  68. : r == 1 ? ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))\
  69. : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\
  70. : ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2)))
  71. #if defined(FT4_SET)
  72. #undef dec_fmvars
  73. #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c))
  74. #elif defined(FT1_SET)
  75. #undef dec_fmvars
  76. #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(f,n),fwd_var,rf1,c))
  77. #else
  78. #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)))
  79. #endif
  80. #if defined(FL4_SET)
  81. #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,l),fwd_var,rf1,c))
  82. #elif defined(FL1_SET)
  83. #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(f,l),fwd_var,rf1,c))
  84. #else
  85. #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(s,box),fwd_var,rf1,c))
  86. #endif
  87. aes_rval aes_encrypt(const void *in_blk, void *out_blk, const aes_encrypt_ctx cx[1])
  88. { aes_32t locals(b0, b1);
  89. const aes_32t *kp = cx->ks;
  90. #ifdef dec_fmvars
  91. dec_fmvars; /* declare variables for fwd_mcol() if needed */
  92. #endif
  93. aes_32t nr = (kp[45] ^ kp[52] ^ kp[53] ? kp[52] : 14);
  94. #ifdef AES_ERR_CHK
  95. if( (nr != 10 || !(kp[0] | kp[3] | kp[4]))
  96. && (nr != 12 || !(kp[0] | kp[5] | kp[6]))
  97. && (nr != 14 || !(kp[0] | kp[7] | kp[8])) )
  98. return aes_error;
  99. #endif
  100. state_in(b0, in_blk, kp);
  101. #if (ENC_UNROLL == FULL)
  102. switch(nr)
  103. {
  104. case 14:
  105. round(fwd_rnd, b1, b0, kp + 1 * N_COLS);
  106. round(fwd_rnd, b0, b1, kp + 2 * N_COLS);
  107. kp += 2 * N_COLS;
  108. case 12:
  109. round(fwd_rnd, b1, b0, kp + 1 * N_COLS);
  110. round(fwd_rnd, b0, b1, kp + 2 * N_COLS);
  111. kp += 2 * N_COLS;
  112. case 10:
  113. round(fwd_rnd, b1, b0, kp + 1 * N_COLS);
  114. round(fwd_rnd, b0, b1, kp + 2 * N_COLS);
  115. round(fwd_rnd, b1, b0, kp + 3 * N_COLS);
  116. round(fwd_rnd, b0, b1, kp + 4 * N_COLS);
  117. round(fwd_rnd, b1, b0, kp + 5 * N_COLS);
  118. round(fwd_rnd, b0, b1, kp + 6 * N_COLS);
  119. round(fwd_rnd, b1, b0, kp + 7 * N_COLS);
  120. round(fwd_rnd, b0, b1, kp + 8 * N_COLS);
  121. round(fwd_rnd, b1, b0, kp + 9 * N_COLS);
  122. round(fwd_lrnd, b0, b1, kp +10 * N_COLS);
  123. }
  124. #else
  125. #if (ENC_UNROLL == PARTIAL)
  126. { aes_32t rnd;
  127. for(rnd = 0; rnd < (nr >> 1) - 1; ++rnd)
  128. {
  129. kp += N_COLS;
  130. round(fwd_rnd, b1, b0, kp);
  131. kp += N_COLS;
  132. round(fwd_rnd, b0, b1, kp);
  133. }
  134. kp += N_COLS;
  135. round(fwd_rnd, b1, b0, kp);
  136. #else
  137. { aes_32t rnd;
  138. for(rnd = 0; rnd < nr - 1; ++rnd)
  139. {
  140. kp += N_COLS;
  141. round(fwd_rnd, b1, b0, kp);
  142. l_copy(b0, b1);
  143. }
  144. #endif
  145. kp += N_COLS;
  146. round(fwd_lrnd, b0, b1, kp);
  147. }
  148. #endif
  149. state_out(out_blk, b0);
  150. #ifdef AES_ERR_CHK
  151. return aes_good;
  152. #endif
  153. }
  154. #endif
  155. #if defined(DECRYPTION) && !defined(AES_ASM)
  156. /* Visual C++ .Net v7.1 provides the fastest encryption code when using
  157. Pentium optimiation with small code but this is poor for decryption
  158. so we need to control this with the following VC++ pragmas
  159. */
  160. #if defined(_MSC_VER)
  161. #pragma optimize( "t", on )
  162. #endif
  163. /* Given the column (c) of the output state variable, the following
  164. macros give the input state variables which are needed in its
  165. computation for each row (r) of the state. All the alternative
  166. macros give the same end values but expand into different ways
  167. of calculating these values. In particular the complex macro
  168. used for dynamically variable block sizes is designed to expand
  169. to a compile time constant whenever possible but will expand to
  170. conditional clauses on some branches (I am grateful to Frank
  171. Yellin for this construction)
  172. */
  173. #define inv_var(x,r,c)\
  174. ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\
  175. : r == 1 ? ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))\
  176. : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\
  177. : ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0)))
  178. #if defined(IT4_SET)
  179. #undef dec_imvars
  180. #define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,n),inv_var,rf1,c))
  181. #elif defined(IT1_SET)
  182. #undef dec_imvars
  183. #define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(i,n),inv_var,rf1,c))
  184. #else
  185. #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)))
  186. #endif
  187. #if defined(IL4_SET)
  188. #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,l),inv_var,rf1,c))
  189. #elif defined(IL1_SET)
  190. #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(i,l),inv_var,rf1,c))
  191. #else
  192. #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c))
  193. #endif
  194. aes_rval aes_decrypt(const void *in_blk, void *out_blk, const aes_decrypt_ctx cx[1])
  195. { aes_32t locals(b0, b1);
  196. #ifdef dec_imvars
  197. dec_imvars; /* declare variables for inv_mcol() if needed */
  198. #endif
  199. aes_32t nr = (cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] ? cx->ks[52] : 14);
  200. const aes_32t *kp = cx->ks + nr * N_COLS;
  201. #ifdef AES_ERR_CHK
  202. if( (nr != 10 || !(cx->ks[0] | cx->ks[3] | cx->ks[4]))
  203. && (nr != 12 || !(cx->ks[0] | cx->ks[5] | cx->ks[6]))
  204. && (nr != 14 || !(cx->ks[0] | cx->ks[7] | cx->ks[8])) )
  205. return aes_error;
  206. #endif
  207. state_in(b0, in_blk, kp);
  208. #if (DEC_UNROLL == FULL)
  209. switch(nr)
  210. {
  211. case 14:
  212. round(inv_rnd, b1, b0, kp - 1 * N_COLS);
  213. round(inv_rnd, b0, b1, kp - 2 * N_COLS);
  214. kp -= 2 * N_COLS;
  215. case 12:
  216. round(inv_rnd, b1, b0, kp - 1 * N_COLS);
  217. round(inv_rnd, b0, b1, kp - 2 * N_COLS);
  218. kp -= 2 * N_COLS;
  219. case 10:
  220. round(inv_rnd, b1, b0, kp - 1 * N_COLS);
  221. round(inv_rnd, b0, b1, kp - 2 * N_COLS);
  222. round(inv_rnd, b1, b0, kp - 3 * N_COLS);
  223. round(inv_rnd, b0, b1, kp - 4 * N_COLS);
  224. round(inv_rnd, b1, b0, kp - 5 * N_COLS);
  225. round(inv_rnd, b0, b1, kp - 6 * N_COLS);
  226. round(inv_rnd, b1, b0, kp - 7 * N_COLS);
  227. round(inv_rnd, b0, b1, kp - 8 * N_COLS);
  228. round(inv_rnd, b1, b0, kp - 9 * N_COLS);
  229. round(inv_lrnd, b0, b1, kp - 10 * N_COLS);
  230. }
  231. #else
  232. #if (DEC_UNROLL == PARTIAL)
  233. { aes_32t rnd;
  234. for(rnd = 0; rnd < (nr >> 1) - 1; ++rnd)
  235. {
  236. kp -= N_COLS;
  237. round(inv_rnd, b1, b0, kp);
  238. kp -= N_COLS;
  239. round(inv_rnd, b0, b1, kp);
  240. }
  241. kp -= N_COLS;
  242. round(inv_rnd, b1, b0, kp);
  243. #else
  244. { aes_32t rnd;
  245. for(rnd = 0; rnd < nr - 1; ++rnd)
  246. {
  247. kp -= N_COLS;
  248. round(inv_rnd, b1, b0, kp);
  249. l_copy(b0, b1);
  250. }
  251. #endif
  252. kp -= N_COLS;
  253. round(inv_lrnd, b0, b1, kp);
  254. }
  255. #endif
  256. state_out(out_blk, b0);
  257. #ifdef AES_ERR_CHK
  258. return aes_good;
  259. #endif
  260. }
  261. #endif
  262. #if defined(__cplusplus)
  263. }
  264. #endif