aescrypt.c 10 KB

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