aescrypt.c 10 KB

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