aescrypt.c 10.0 KB

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