Brak opisu

aeskey.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 1998-2010, 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. #ifdef USE_VIA_ACE_IF_PRESENT
  19. # include "aes_via_ace.h"
  20. #endif
  21. #if defined(__cplusplus)
  22. extern "C"
  23. {
  24. #endif
  25. /* Initialise the key schedule from the user supplied key. The key
  26. length can be specified in bytes, with legal values of 16, 24
  27. and 32, or in bits, with legal values of 128, 192 and 256. These
  28. values correspond with Nk values of 4, 6 and 8 respectively.
  29. The following macros implement a single cycle in the key
  30. schedule generation process. The number of cycles needed
  31. for each cx->n_col and nk value is:
  32. nk = 4 5 6 7 8
  33. ------------------------------
  34. cx->n_col = 4 10 9 8 7 7
  35. cx->n_col = 5 14 11 10 9 9
  36. cx->n_col = 6 19 15 12 11 11
  37. cx->n_col = 7 21 19 16 13 14
  38. cx->n_col = 8 29 23 19 17 14
  39. */
  40. #if defined( REDUCE_CODE_SIZE )
  41. # define ls_box ls_sub
  42. uint_32t ls_sub(const uint_32t t, const uint_32t n);
  43. # define inv_mcol im_sub
  44. uint_32t im_sub(const uint_32t x);
  45. # ifdef ENC_KS_UNROLL
  46. # undef ENC_KS_UNROLL
  47. # endif
  48. # ifdef DEC_KS_UNROLL
  49. # undef DEC_KS_UNROLL
  50. # endif
  51. #endif
  52. #if (FUNCS_IN_C & ENC_KEYING_IN_C)
  53. #if defined(AES_128) || defined( AES_VAR )
  54. #define ke4(k,i) \
  55. { k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; \
  56. k[4*(i)+5] = ss[1] ^= ss[0]; \
  57. k[4*(i)+6] = ss[2] ^= ss[1]; \
  58. k[4*(i)+7] = ss[3] ^= ss[2]; \
  59. }
  60. AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1])
  61. { uint_32t ss[4];
  62. cx->ks[0] = ss[0] = word_in(key, 0);
  63. cx->ks[1] = ss[1] = word_in(key, 1);
  64. cx->ks[2] = ss[2] = word_in(key, 2);
  65. cx->ks[3] = ss[3] = word_in(key, 3);
  66. #ifdef ENC_KS_UNROLL
  67. ke4(cx->ks, 0); ke4(cx->ks, 1);
  68. ke4(cx->ks, 2); ke4(cx->ks, 3);
  69. ke4(cx->ks, 4); ke4(cx->ks, 5);
  70. ke4(cx->ks, 6); ke4(cx->ks, 7);
  71. ke4(cx->ks, 8);
  72. #else
  73. { uint_32t i;
  74. for(i = 0; i < 9; ++i)
  75. ke4(cx->ks, i);
  76. }
  77. #endif
  78. ke4(cx->ks, 9);
  79. cx->inf.l = 0;
  80. cx->inf.b[0] = 10 * 16;
  81. #ifdef USE_VIA_ACE_IF_PRESENT
  82. if(VIA_ACE_AVAILABLE)
  83. cx->inf.b[1] = 0xff;
  84. #endif
  85. return EXIT_SUCCESS;
  86. }
  87. #endif
  88. #if defined(AES_192) || defined( AES_VAR )
  89. #define kef6(k,i) \
  90. { k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; \
  91. k[6*(i)+ 7] = ss[1] ^= ss[0]; \
  92. k[6*(i)+ 8] = ss[2] ^= ss[1]; \
  93. k[6*(i)+ 9] = ss[3] ^= ss[2]; \
  94. }
  95. #define ke6(k,i) \
  96. { kef6(k,i); \
  97. k[6*(i)+10] = ss[4] ^= ss[3]; \
  98. k[6*(i)+11] = ss[5] ^= ss[4]; \
  99. }
  100. AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1])
  101. { uint_32t ss[6];
  102. cx->ks[0] = ss[0] = word_in(key, 0);
  103. cx->ks[1] = ss[1] = word_in(key, 1);
  104. cx->ks[2] = ss[2] = word_in(key, 2);
  105. cx->ks[3] = ss[3] = word_in(key, 3);
  106. cx->ks[4] = ss[4] = word_in(key, 4);
  107. cx->ks[5] = ss[5] = word_in(key, 5);
  108. #ifdef ENC_KS_UNROLL
  109. ke6(cx->ks, 0); ke6(cx->ks, 1);
  110. ke6(cx->ks, 2); ke6(cx->ks, 3);
  111. ke6(cx->ks, 4); ke6(cx->ks, 5);
  112. ke6(cx->ks, 6);
  113. #else
  114. { uint_32t i;
  115. for(i = 0; i < 7; ++i)
  116. ke6(cx->ks, i);
  117. }
  118. #endif
  119. kef6(cx->ks, 7);
  120. cx->inf.l = 0;
  121. cx->inf.b[0] = 12 * 16;
  122. #ifdef USE_VIA_ACE_IF_PRESENT
  123. if(VIA_ACE_AVAILABLE)
  124. cx->inf.b[1] = 0xff;
  125. #endif
  126. return EXIT_SUCCESS;
  127. }
  128. #endif
  129. #if defined(AES_256) || defined( AES_VAR )
  130. #define kef8(k,i) \
  131. { k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; \
  132. k[8*(i)+ 9] = ss[1] ^= ss[0]; \
  133. k[8*(i)+10] = ss[2] ^= ss[1]; \
  134. k[8*(i)+11] = ss[3] ^= ss[2]; \
  135. }
  136. #define ke8(k,i) \
  137. { kef8(k,i); \
  138. k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); \
  139. k[8*(i)+13] = ss[5] ^= ss[4]; \
  140. k[8*(i)+14] = ss[6] ^= ss[5]; \
  141. k[8*(i)+15] = ss[7] ^= ss[6]; \
  142. }
  143. AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1])
  144. { uint_32t ss[8];
  145. cx->ks[0] = ss[0] = word_in(key, 0);
  146. cx->ks[1] = ss[1] = word_in(key, 1);
  147. cx->ks[2] = ss[2] = word_in(key, 2);
  148. cx->ks[3] = ss[3] = word_in(key, 3);
  149. cx->ks[4] = ss[4] = word_in(key, 4);
  150. cx->ks[5] = ss[5] = word_in(key, 5);
  151. cx->ks[6] = ss[6] = word_in(key, 6);
  152. cx->ks[7] = ss[7] = word_in(key, 7);
  153. #ifdef ENC_KS_UNROLL
  154. ke8(cx->ks, 0); ke8(cx->ks, 1);
  155. ke8(cx->ks, 2); ke8(cx->ks, 3);
  156. ke8(cx->ks, 4); ke8(cx->ks, 5);
  157. #else
  158. { uint_32t i;
  159. for(i = 0; i < 6; ++i)
  160. ke8(cx->ks, i);
  161. }
  162. #endif
  163. kef8(cx->ks, 6);
  164. cx->inf.l = 0;
  165. cx->inf.b[0] = 14 * 16;
  166. #ifdef USE_VIA_ACE_IF_PRESENT
  167. if(VIA_ACE_AVAILABLE)
  168. cx->inf.b[1] = 0xff;
  169. #endif
  170. return EXIT_SUCCESS;
  171. }
  172. #endif
  173. #if defined( AES_VAR )
  174. AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1])
  175. {
  176. switch(key_len)
  177. {
  178. case 16: case 128: return aes_encrypt_key128(key, cx);
  179. case 24: case 192: return aes_encrypt_key192(key, cx);
  180. case 32: case 256: return aes_encrypt_key256(key, cx);
  181. default: return EXIT_FAILURE;
  182. }
  183. }
  184. #endif
  185. #endif
  186. #if (FUNCS_IN_C & DEC_KEYING_IN_C)
  187. /* this is used to store the decryption round keys */
  188. /* in forward or reverse order */
  189. #ifdef AES_REV_DKS
  190. #define v(n,i) ((n) - (i) + 2 * ((i) & 3))
  191. #else
  192. #define v(n,i) (i)
  193. #endif
  194. #if DEC_ROUND == NO_TABLES
  195. #define ff(x) (x)
  196. #else
  197. #define ff(x) inv_mcol(x)
  198. #if defined( dec_imvars )
  199. #define d_vars dec_imvars
  200. #endif
  201. #endif
  202. #if defined(AES_128) || defined( AES_VAR )
  203. #define k4e(k,i) \
  204. { k[v(40,(4*(i))+4)] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; \
  205. k[v(40,(4*(i))+5)] = ss[1] ^= ss[0]; \
  206. k[v(40,(4*(i))+6)] = ss[2] ^= ss[1]; \
  207. k[v(40,(4*(i))+7)] = ss[3] ^= ss[2]; \
  208. }
  209. #if 1
  210. #define kdf4(k,i) \
  211. { ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \
  212. ss[1] = ss[1] ^ ss[3]; \
  213. ss[2] = ss[2] ^ ss[3]; \
  214. ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; \
  215. ss[i % 4] ^= ss[4]; \
  216. ss[4] ^= k[v(40,(4*(i)))]; k[v(40,(4*(i))+4)] = ff(ss[4]); \
  217. ss[4] ^= k[v(40,(4*(i))+1)]; k[v(40,(4*(i))+5)] = ff(ss[4]); \
  218. ss[4] ^= k[v(40,(4*(i))+2)]; k[v(40,(4*(i))+6)] = ff(ss[4]); \
  219. ss[4] ^= k[v(40,(4*(i))+3)]; k[v(40,(4*(i))+7)] = ff(ss[4]); \
  220. }
  221. #define kd4(k,i) \
  222. { ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; \
  223. ss[i % 4] ^= ss[4]; ss[4] = ff(ss[4]); \
  224. k[v(40,(4*(i))+4)] = ss[4] ^= k[v(40,(4*(i)))]; \
  225. k[v(40,(4*(i))+5)] = ss[4] ^= k[v(40,(4*(i))+1)]; \
  226. k[v(40,(4*(i))+6)] = ss[4] ^= k[v(40,(4*(i))+2)]; \
  227. k[v(40,(4*(i))+7)] = ss[4] ^= k[v(40,(4*(i))+3)]; \
  228. }
  229. #define kdl4(k,i) \
  230. { ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \
  231. k[v(40,(4*(i))+4)] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \
  232. k[v(40,(4*(i))+5)] = ss[1] ^ ss[3]; \
  233. k[v(40,(4*(i))+6)] = ss[0]; \
  234. k[v(40,(4*(i))+7)] = ss[1]; \
  235. }
  236. #else
  237. #define kdf4(k,i) \
  238. { ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[v(40,(4*(i))+ 4)] = ff(ss[0]); \
  239. ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ff(ss[1]); \
  240. ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ff(ss[2]); \
  241. ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ff(ss[3]); \
  242. }
  243. #define kd4(k,i) \
  244. { ss[4] = ls_box(ss[3],3) ^ t_use(r,c)[i]; \
  245. ss[0] ^= ss[4]; ss[4] = ff(ss[4]); k[v(40,(4*(i))+ 4)] = ss[4] ^= k[v(40,(4*(i)))]; \
  246. ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ss[4] ^= k[v(40,(4*(i))+ 1)]; \
  247. ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ss[4] ^= k[v(40,(4*(i))+ 2)]; \
  248. ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ss[4] ^= k[v(40,(4*(i))+ 3)]; \
  249. }
  250. #define kdl4(k,i) \
  251. { ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[v(40,(4*(i))+ 4)] = ss[0]; \
  252. ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ss[1]; \
  253. ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ss[2]; \
  254. ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ss[3]; \
  255. }
  256. #endif
  257. AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1])
  258. { uint_32t ss[5];
  259. #if defined( d_vars )
  260. d_vars;
  261. #endif
  262. cx->ks[v(40,(0))] = ss[0] = word_in(key, 0);
  263. cx->ks[v(40,(1))] = ss[1] = word_in(key, 1);
  264. cx->ks[v(40,(2))] = ss[2] = word_in(key, 2);
  265. cx->ks[v(40,(3))] = ss[3] = word_in(key, 3);
  266. #ifdef DEC_KS_UNROLL
  267. kdf4(cx->ks, 0); kd4(cx->ks, 1);
  268. kd4(cx->ks, 2); kd4(cx->ks, 3);
  269. kd4(cx->ks, 4); kd4(cx->ks, 5);
  270. kd4(cx->ks, 6); kd4(cx->ks, 7);
  271. kd4(cx->ks, 8); kdl4(cx->ks, 9);
  272. #else
  273. { uint_32t i;
  274. for(i = 0; i < 10; ++i)
  275. k4e(cx->ks, i);
  276. #if !(DEC_ROUND == NO_TABLES)
  277. for(i = N_COLS; i < 10 * N_COLS; ++i)
  278. cx->ks[i] = inv_mcol(cx->ks[i]);
  279. #endif
  280. }
  281. #endif
  282. cx->inf.l = 0;
  283. cx->inf.b[0] = 10 * 16;
  284. #ifdef USE_VIA_ACE_IF_PRESENT
  285. if(VIA_ACE_AVAILABLE)
  286. cx->inf.b[1] = 0xff;
  287. #endif
  288. return EXIT_SUCCESS;
  289. }
  290. #endif
  291. #if defined(AES_192) || defined( AES_VAR )
  292. #define k6ef(k,i) \
  293. { k[v(48,(6*(i))+ 6)] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; \
  294. k[v(48,(6*(i))+ 7)] = ss[1] ^= ss[0]; \
  295. k[v(48,(6*(i))+ 8)] = ss[2] ^= ss[1]; \
  296. k[v(48,(6*(i))+ 9)] = ss[3] ^= ss[2]; \
  297. }
  298. #define k6e(k,i) \
  299. { k6ef(k,i); \
  300. k[v(48,(6*(i))+10)] = ss[4] ^= ss[3]; \
  301. k[v(48,(6*(i))+11)] = ss[5] ^= ss[4]; \
  302. }
  303. #define kdf6(k,i) \
  304. { ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[v(48,(6*(i))+ 6)] = ff(ss[0]); \
  305. ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ff(ss[1]); \
  306. ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ff(ss[2]); \
  307. ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ff(ss[3]); \
  308. ss[4] ^= ss[3]; k[v(48,(6*(i))+10)] = ff(ss[4]); \
  309. ss[5] ^= ss[4]; k[v(48,(6*(i))+11)] = ff(ss[5]); \
  310. }
  311. #define kd6(k,i) \
  312. { ss[6] = ls_box(ss[5],3) ^ t_use(r,c)[i]; \
  313. ss[0] ^= ss[6]; ss[6] = ff(ss[6]); k[v(48,(6*(i))+ 6)] = ss[6] ^= k[v(48,(6*(i)))]; \
  314. ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ss[6] ^= k[v(48,(6*(i))+ 1)]; \
  315. ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ss[6] ^= k[v(48,(6*(i))+ 2)]; \
  316. ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ss[6] ^= k[v(48,(6*(i))+ 3)]; \
  317. ss[4] ^= ss[3]; k[v(48,(6*(i))+10)] = ss[6] ^= k[v(48,(6*(i))+ 4)]; \
  318. ss[5] ^= ss[4]; k[v(48,(6*(i))+11)] = ss[6] ^= k[v(48,(6*(i))+ 5)]; \
  319. }
  320. #define kdl6(k,i) \
  321. { ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[v(48,(6*(i))+ 6)] = ss[0]; \
  322. ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ss[1]; \
  323. ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ss[2]; \
  324. ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ss[3]; \
  325. }
  326. AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1])
  327. { uint_32t ss[7];
  328. #if defined( d_vars )
  329. d_vars;
  330. #endif
  331. cx->ks[v(48,(0))] = ss[0] = word_in(key, 0);
  332. cx->ks[v(48,(1))] = ss[1] = word_in(key, 1);
  333. cx->ks[v(48,(2))] = ss[2] = word_in(key, 2);
  334. cx->ks[v(48,(3))] = ss[3] = word_in(key, 3);
  335. #ifdef DEC_KS_UNROLL
  336. cx->ks[v(48,(4))] = ff(ss[4] = word_in(key, 4));
  337. cx->ks[v(48,(5))] = ff(ss[5] = word_in(key, 5));
  338. kdf6(cx->ks, 0); kd6(cx->ks, 1);
  339. kd6(cx->ks, 2); kd6(cx->ks, 3);
  340. kd6(cx->ks, 4); kd6(cx->ks, 5);
  341. kd6(cx->ks, 6); kdl6(cx->ks, 7);
  342. #else
  343. cx->ks[v(48,(4))] = ss[4] = word_in(key, 4);
  344. cx->ks[v(48,(5))] = ss[5] = word_in(key, 5);
  345. { uint_32t i;
  346. for(i = 0; i < 7; ++i)
  347. k6e(cx->ks, i);
  348. k6ef(cx->ks, 7);
  349. #if !(DEC_ROUND == NO_TABLES)
  350. for(i = N_COLS; i < 12 * N_COLS; ++i)
  351. cx->ks[i] = inv_mcol(cx->ks[i]);
  352. #endif
  353. }
  354. #endif
  355. cx->inf.l = 0;
  356. cx->inf.b[0] = 12 * 16;
  357. #ifdef USE_VIA_ACE_IF_PRESENT
  358. if(VIA_ACE_AVAILABLE)
  359. cx->inf.b[1] = 0xff;
  360. #endif
  361. return EXIT_SUCCESS;
  362. }
  363. #endif
  364. #if defined(AES_256) || defined( AES_VAR )
  365. #define k8ef(k,i) \
  366. { k[v(56,(8*(i))+ 8)] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; \
  367. k[v(56,(8*(i))+ 9)] = ss[1] ^= ss[0]; \
  368. k[v(56,(8*(i))+10)] = ss[2] ^= ss[1]; \
  369. k[v(56,(8*(i))+11)] = ss[3] ^= ss[2]; \
  370. }
  371. #define k8e(k,i) \
  372. { k8ef(k,i); \
  373. k[v(56,(8*(i))+12)] = ss[4] ^= ls_box(ss[3],0); \
  374. k[v(56,(8*(i))+13)] = ss[5] ^= ss[4]; \
  375. k[v(56,(8*(i))+14)] = ss[6] ^= ss[5]; \
  376. k[v(56,(8*(i))+15)] = ss[7] ^= ss[6]; \
  377. }
  378. #define kdf8(k,i) \
  379. { ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[v(56,(8*(i))+ 8)] = ff(ss[0]); \
  380. ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ff(ss[1]); \
  381. ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ff(ss[2]); \
  382. ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ff(ss[3]); \
  383. ss[4] ^= ls_box(ss[3],0); k[v(56,(8*(i))+12)] = ff(ss[4]); \
  384. ss[5] ^= ss[4]; k[v(56,(8*(i))+13)] = ff(ss[5]); \
  385. ss[6] ^= ss[5]; k[v(56,(8*(i))+14)] = ff(ss[6]); \
  386. ss[7] ^= ss[6]; k[v(56,(8*(i))+15)] = ff(ss[7]); \
  387. }
  388. #define kd8(k,i) \
  389. { ss[8] = ls_box(ss[7],3) ^ t_use(r,c)[i]; \
  390. ss[0] ^= ss[8]; ss[8] = ff(ss[8]); k[v(56,(8*(i))+ 8)] = ss[8] ^= k[v(56,(8*(i)))]; \
  391. ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ss[8] ^= k[v(56,(8*(i))+ 1)]; \
  392. ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ss[8] ^= k[v(56,(8*(i))+ 2)]; \
  393. ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ss[8] ^= k[v(56,(8*(i))+ 3)]; \
  394. ss[8] = ls_box(ss[3],0); \
  395. ss[4] ^= ss[8]; ss[8] = ff(ss[8]); k[v(56,(8*(i))+12)] = ss[8] ^= k[v(56,(8*(i))+ 4)]; \
  396. ss[5] ^= ss[4]; k[v(56,(8*(i))+13)] = ss[8] ^= k[v(56,(8*(i))+ 5)]; \
  397. ss[6] ^= ss[5]; k[v(56,(8*(i))+14)] = ss[8] ^= k[v(56,(8*(i))+ 6)]; \
  398. ss[7] ^= ss[6]; k[v(56,(8*(i))+15)] = ss[8] ^= k[v(56,(8*(i))+ 7)]; \
  399. }
  400. #define kdl8(k,i) \
  401. { ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[v(56,(8*(i))+ 8)] = ss[0]; \
  402. ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ss[1]; \
  403. ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ss[2]; \
  404. ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ss[3]; \
  405. }
  406. AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1])
  407. { uint_32t ss[9];
  408. #if defined( d_vars )
  409. d_vars;
  410. #endif
  411. cx->ks[v(56,(0))] = ss[0] = word_in(key, 0);
  412. cx->ks[v(56,(1))] = ss[1] = word_in(key, 1);
  413. cx->ks[v(56,(2))] = ss[2] = word_in(key, 2);
  414. cx->ks[v(56,(3))] = ss[3] = word_in(key, 3);
  415. #ifdef DEC_KS_UNROLL
  416. cx->ks[v(56,(4))] = ff(ss[4] = word_in(key, 4));
  417. cx->ks[v(56,(5))] = ff(ss[5] = word_in(key, 5));
  418. cx->ks[v(56,(6))] = ff(ss[6] = word_in(key, 6));
  419. cx->ks[v(56,(7))] = ff(ss[7] = word_in(key, 7));
  420. kdf8(cx->ks, 0); kd8(cx->ks, 1);
  421. kd8(cx->ks, 2); kd8(cx->ks, 3);
  422. kd8(cx->ks, 4); kd8(cx->ks, 5);
  423. kdl8(cx->ks, 6);
  424. #else
  425. cx->ks[v(56,(4))] = ss[4] = word_in(key, 4);
  426. cx->ks[v(56,(5))] = ss[5] = word_in(key, 5);
  427. cx->ks[v(56,(6))] = ss[6] = word_in(key, 6);
  428. cx->ks[v(56,(7))] = ss[7] = word_in(key, 7);
  429. { uint_32t i;
  430. for(i = 0; i < 6; ++i)
  431. k8e(cx->ks, i);
  432. k8ef(cx->ks, 6);
  433. #if !(DEC_ROUND == NO_TABLES)
  434. for(i = N_COLS; i < 14 * N_COLS; ++i)
  435. cx->ks[i] = inv_mcol(cx->ks[i]);
  436. #endif
  437. }
  438. #endif
  439. cx->inf.l = 0;
  440. cx->inf.b[0] = 14 * 16;
  441. #ifdef USE_VIA_ACE_IF_PRESENT
  442. if(VIA_ACE_AVAILABLE)
  443. cx->inf.b[1] = 0xff;
  444. #endif
  445. return EXIT_SUCCESS;
  446. }
  447. #endif
  448. #if defined( AES_VAR )
  449. AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1])
  450. {
  451. switch(key_len)
  452. {
  453. case 16: case 128: return aes_decrypt_key128(key, cx);
  454. case 24: case 192: return aes_decrypt_key192(key, cx);
  455. case 32: case 256: return aes_decrypt_key256(key, cx);
  456. default: return EXIT_FAILURE;
  457. }
  458. }
  459. #endif
  460. #endif
  461. #if defined(__cplusplus)
  462. }
  463. #endif