aescrypt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 1998-2013, 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(USE_INTEL_AES_IF_PRESENT)
  19. #include "aes_ni.h"
  20. #else
  21. /* map names here to provide the external API ('name' -> 'aes_name') */
  22. #define aes_xi(x) aes_##x
  23. #endif
  24. #if defined(__cplusplus)
  25. extern "C" {
  26. #endif
  27. #define si(y, x, k, c) (s(y, c) = word_in(x, c) ^ (k)[c])
  28. #define so(y, x, c) word_out(y, c, s(x, c))
  29. #if defined(ARRAYS)
  30. #define locals(y, x) x[4], y[4]
  31. #else
  32. #define locals(y, x) x##0, x##1, x##2, x##3, y##0, y##1, y##2, y##3
  33. #endif
  34. #define l_copy(y, x) \
  35. s(y, 0) = s(x, 0); \
  36. s(y, 1) = s(x, 1); \
  37. s(y, 2) = s(x, 2); \
  38. s(y, 3) = s(x, 3);
  39. #define state_in(y, x, k) \
  40. si(y, x, k, 0); \
  41. si(y, x, k, 1); \
  42. si(y, x, k, 2); \
  43. si(y, x, k, 3)
  44. #define state_out(y, x) \
  45. so(y, x, 0); \
  46. so(y, x, 1); \
  47. so(y, x, 2); \
  48. so(y, x, 3)
  49. #define round(rm, y, x, k) \
  50. rm(y, x, k, 0); \
  51. rm(y, x, k, 1); \
  52. rm(y, x, k, 2); \
  53. rm(y, x, k, 3)
  54. #if(FUNCS_IN_C & ENCRYPTION_IN_C)
  55. /* Visual C++ .Net v7.1 provides the fastest encryption code when using
  56. Pentium optimiation with small code but this is poor for decryption
  57. so we need to control this with the following VC++ pragmas
  58. */
  59. #if defined(_MSC_VER) && !defined(_WIN64) && !defined(__clang__)
  60. #pragma optimize("s", on)
  61. #endif
  62. /* Given the column (c) of the output state variable, the following
  63. macros give the input state variables which are needed in its
  64. computation for each row (r) of the state. All the alternative
  65. macros give the same end values but expand into different ways
  66. of calculating these values. In particular the complex macro
  67. used for dynamically variable block sizes is designed to expand
  68. to a compile time constant whenever possible but will expand to
  69. conditional clauses on some branches (I am grateful to Frank
  70. Yellin for this construction)
  71. */
  72. #define fwd_var(x, r, c) \
  73. (r == 0 ? (c == 0 ? s(x, 0) : \
  74. c == 1 ? s(x, 1) : \
  75. c == 2 ? s(x, 2) : \
  76. s(x, 3)) : \
  77. r == 1 ? (c == 0 ? s(x, 1) : \
  78. c == 1 ? s(x, 2) : \
  79. c == 2 ? s(x, 3) : \
  80. s(x, 0)) : \
  81. r == 2 ? (c == 0 ? s(x, 2) : \
  82. c == 1 ? s(x, 3) : \
  83. c == 2 ? s(x, 0) : \
  84. s(x, 1)) : \
  85. (c == 0 ? s(x, 3) : \
  86. c == 1 ? s(x, 0) : \
  87. c == 2 ? s(x, 1) : \
  88. s(x, 2)))
  89. #if defined(FT4_SET)
  90. #undef dec_fmvars
  91. #define fwd_rnd(y, x, k, c) (s(y, c) = (k)[c] ^ four_tables(x, t_use(f, n), fwd_var, rf1, c))
  92. #elif defined(FT1_SET)
  93. #undef dec_fmvars
  94. #define fwd_rnd(y, x, k, c) (s(y, c) = (k)[c] ^ one_table(x, upr, t_use(f, n), fwd_var, rf1, c))
  95. #else
  96. #define fwd_rnd(y, x, k, c) \
  97. (s(y, c) = (k)[c] ^ fwd_mcol(no_table(x, t_use(s, box), fwd_var, rf1, c)))
  98. #endif
  99. #if defined(FL4_SET)
  100. #define fwd_lrnd(y, x, k, c) (s(y, c) = (k)[c] ^ four_tables(x, t_use(f, l), fwd_var, rf1, c))
  101. #elif defined(FL1_SET)
  102. #define fwd_lrnd(y, x, k, c) (s(y, c) = (k)[c] ^ one_table(x, ups, t_use(f, l), fwd_var, rf1, c))
  103. #else
  104. #define fwd_lrnd(y, x, k, c) (s(y, c) = (k)[c] ^ no_table(x, t_use(s, box), fwd_var, rf1, c))
  105. #endif
  106. AES_RETURN
  107. aes_xi(encrypt)(const unsigned char* in, unsigned char* out, const aes_encrypt_ctx cx[1]) {
  108. uint32_t locals(b0, b1);
  109. const uint32_t* kp = NULL;
  110. #if defined(dec_fmvars)
  111. dec_fmvars; /* declare variables for fwd_mcol() if needed */
  112. #endif
  113. if(cx->inf.b[0] != 10 * AES_BLOCK_SIZE && cx->inf.b[0] != 12 * AES_BLOCK_SIZE &&
  114. cx->inf.b[0] != 14 * AES_BLOCK_SIZE)
  115. return EXIT_FAILURE;
  116. kp = cx->ks;
  117. state_in(b0, in, kp);
  118. #if(ENC_UNROLL == FULL)
  119. switch(cx->inf.b[0]) {
  120. case 14 * AES_BLOCK_SIZE:
  121. round(fwd_rnd, b1, b0, kp + 1 * N_COLS);
  122. round(fwd_rnd, b0, b1, kp + 2 * N_COLS);
  123. kp += 2 * N_COLS;
  124. //-fallthrough
  125. case 12 * AES_BLOCK_SIZE:
  126. round(fwd_rnd, b1, b0, kp + 1 * N_COLS);
  127. round(fwd_rnd, b0, b1, kp + 2 * N_COLS);
  128. kp += 2 * N_COLS;
  129. //-fallthrough
  130. case 10 * AES_BLOCK_SIZE:
  131. round(fwd_rnd, b1, b0, kp + 1 * N_COLS);
  132. round(fwd_rnd, b0, b1, kp + 2 * N_COLS);
  133. round(fwd_rnd, b1, b0, kp + 3 * N_COLS);
  134. round(fwd_rnd, b0, b1, kp + 4 * N_COLS);
  135. round(fwd_rnd, b1, b0, kp + 5 * N_COLS);
  136. round(fwd_rnd, b0, b1, kp + 6 * N_COLS);
  137. round(fwd_rnd, b1, b0, kp + 7 * N_COLS);
  138. round(fwd_rnd, b0, b1, kp + 8 * N_COLS);
  139. round(fwd_rnd, b1, b0, kp + 9 * N_COLS);
  140. round(fwd_lrnd, b0, b1, kp + 10 * N_COLS);
  141. //-fallthrough
  142. }
  143. #else
  144. #if(ENC_UNROLL == PARTIAL)
  145. {
  146. uint32_t rnd;
  147. for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd) {
  148. kp += N_COLS;
  149. round(fwd_rnd, b1, b0, kp);
  150. kp += N_COLS;
  151. round(fwd_rnd, b0, b1, kp);
  152. }
  153. kp += N_COLS;
  154. round(fwd_rnd, b1, b0, kp);
  155. #else
  156. {
  157. uint32_t rnd;
  158. for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd) {
  159. kp += N_COLS;
  160. round(fwd_rnd, b1, b0, kp);
  161. l_copy(b0, b1);
  162. }
  163. #endif
  164. kp += N_COLS;
  165. round(fwd_lrnd, b0, b1, kp);
  166. }
  167. #endif
  168. state_out(out, b0);
  169. return EXIT_SUCCESS;
  170. }
  171. #endif
  172. #if(FUNCS_IN_C & DECRYPTION_IN_C)
  173. /* Visual C++ .Net v7.1 provides the fastest encryption code when using
  174. Pentium optimiation with small code but this is poor for decryption
  175. so we need to control this with the following VC++ pragmas
  176. */
  177. #if defined(_MSC_VER) && !defined(_WIN64) && !defined(__clang__)
  178. #pragma optimize("t", on)
  179. #endif
  180. /* Given the column (c) of the output state variable, the following
  181. macros give the input state variables which are needed in its
  182. computation for each row (r) of the state. All the alternative
  183. macros give the same end values but expand into different ways
  184. of calculating these values. In particular the complex macro
  185. used for dynamically variable block sizes is designed to expand
  186. to a compile time constant whenever possible but will expand to
  187. conditional clauses on some branches (I am grateful to Frank
  188. Yellin for this construction)
  189. */
  190. #define inv_var(x, r, c) \
  191. (r == 0 ? (c == 0 ? s(x, 0) : \
  192. c == 1 ? s(x, 1) : \
  193. c == 2 ? s(x, 2) : \
  194. s(x, 3)) : \
  195. r == 1 ? (c == 0 ? s(x, 3) : \
  196. c == 1 ? s(x, 0) : \
  197. c == 2 ? s(x, 1) : \
  198. s(x, 2)) : \
  199. r == 2 ? (c == 0 ? s(x, 2) : \
  200. c == 1 ? s(x, 3) : \
  201. c == 2 ? s(x, 0) : \
  202. s(x, 1)) : \
  203. (c == 0 ? s(x, 1) : \
  204. c == 1 ? s(x, 2) : \
  205. c == 2 ? s(x, 3) : \
  206. s(x, 0)))
  207. #if defined(IT4_SET)
  208. #undef dec_imvars
  209. #define inv_rnd(y, x, k, c) (s(y, c) = (k)[c] ^ four_tables(x, t_use(i, n), inv_var, rf1, c))
  210. #elif defined(IT1_SET)
  211. #undef dec_imvars
  212. #define inv_rnd(y, x, k, c) (s(y, c) = (k)[c] ^ one_table(x, upr, t_use(i, n), inv_var, rf1, c))
  213. #else
  214. #define inv_rnd(y, x, k, c) \
  215. (s(y, c) = inv_mcol((k)[c] ^ no_table(x, t_use(i, box), inv_var, rf1, c)))
  216. #endif
  217. #if defined(IL4_SET)
  218. #define inv_lrnd(y, x, k, c) (s(y, c) = (k)[c] ^ four_tables(x, t_use(i, l), inv_var, rf1, c))
  219. #elif defined(IL1_SET)
  220. #define inv_lrnd(y, x, k, c) (s(y, c) = (k)[c] ^ one_table(x, ups, t_use(i, l), inv_var, rf1, c))
  221. #else
  222. #define inv_lrnd(y, x, k, c) (s(y, c) = (k)[c] ^ no_table(x, t_use(i, box), inv_var, rf1, c))
  223. #endif
  224. /* This code can work with the decryption key schedule in the */
  225. /* order that is used for encrytpion (where the 1st decryption */
  226. /* round key is at the high end ot the schedule) or with a key */
  227. /* schedule that has been reversed to put the 1st decryption */
  228. /* round key at the low end of the schedule in memory (when */
  229. /* AES_REV_DKS is defined) */
  230. #ifdef AES_REV_DKS
  231. #define key_ofs 0
  232. #define rnd_key(n) (kp + n * N_COLS)
  233. #else
  234. #define key_ofs 1
  235. #define rnd_key(n) (kp - n * N_COLS)
  236. #endif
  237. AES_RETURN
  238. aes_xi(decrypt)(const unsigned char* in, unsigned char* out, const aes_decrypt_ctx cx[1]) {
  239. uint32_t locals(b0, b1);
  240. #if defined(dec_imvars)
  241. dec_imvars; /* declare variables for inv_mcol() if needed */
  242. #endif
  243. const uint32_t* kp = NULL;
  244. if(cx->inf.b[0] != 10 * AES_BLOCK_SIZE && cx->inf.b[0] != 12 * AES_BLOCK_SIZE &&
  245. cx->inf.b[0] != 14 * AES_BLOCK_SIZE)
  246. return EXIT_FAILURE;
  247. kp = cx->ks + (key_ofs ? (cx->inf.b[0] >> 2) : 0);
  248. state_in(b0, in, kp);
  249. #if(DEC_UNROLL == FULL)
  250. kp = cx->ks + (key_ofs ? 0 : (cx->inf.b[0] >> 2));
  251. switch(cx->inf.b[0]) {
  252. case 14 * AES_BLOCK_SIZE:
  253. round(inv_rnd, b1, b0, rnd_key(-13));
  254. round(inv_rnd, b0, b1, rnd_key(-12));
  255. //-fallthrough
  256. case 12 * AES_BLOCK_SIZE:
  257. round(inv_rnd, b1, b0, rnd_key(-11));
  258. round(inv_rnd, b0, b1, rnd_key(-10));
  259. //-fallthrough
  260. case 10 * AES_BLOCK_SIZE:
  261. round(inv_rnd, b1, b0, rnd_key(-9));
  262. round(inv_rnd, b0, b1, rnd_key(-8));
  263. round(inv_rnd, b1, b0, rnd_key(-7));
  264. round(inv_rnd, b0, b1, rnd_key(-6));
  265. round(inv_rnd, b1, b0, rnd_key(-5));
  266. round(inv_rnd, b0, b1, rnd_key(-4));
  267. round(inv_rnd, b1, b0, rnd_key(-3));
  268. round(inv_rnd, b0, b1, rnd_key(-2));
  269. round(inv_rnd, b1, b0, rnd_key(-1));
  270. round(inv_lrnd, b0, b1, rnd_key(0));
  271. //-fallthrough
  272. }
  273. #else
  274. #if(DEC_UNROLL == PARTIAL)
  275. {
  276. uint32_t rnd;
  277. for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd) {
  278. kp = rnd_key(1);
  279. round(inv_rnd, b1, b0, kp);
  280. kp = rnd_key(1);
  281. round(inv_rnd, b0, b1, kp);
  282. }
  283. kp = rnd_key(1);
  284. round(inv_rnd, b1, b0, kp);
  285. #else
  286. {
  287. uint32_t rnd;
  288. for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd) {
  289. kp = rnd_key(1);
  290. round(inv_rnd, b1, b0, kp);
  291. l_copy(b0, b1);
  292. }
  293. #endif
  294. kp = rnd_key(1);
  295. round(inv_lrnd, b0, b1, kp);
  296. }
  297. #endif
  298. state_out(out, b0);
  299. return EXIT_SUCCESS;
  300. }
  301. #endif
  302. #if defined(__cplusplus)
  303. }
  304. #endif