aeskey.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. \brief This file contains the code for implementing the key schedule for AES
  27. (Rijndael) for block and key sizes of 16, 24, and 32 bytes. See aesopt.h
  28. for further details including optimisation.
  29. */
  30. #include "aesopt.h"
  31. #if defined(__cplusplus)
  32. extern "C"
  33. {
  34. #endif
  35. /* Initialise the key schedule from the user supplied key. The key
  36. length can be specified in bytes, with legal values of 16, 24
  37. and 32, or in bits, with legal values of 128, 192 and 256. These
  38. values correspond with Nk values of 4, 6 and 8 respectively.
  39. The following macros implement a single cycle in the key
  40. schedule generation process. The number of cycles needed
  41. for each cx->n_col and nk value is:
  42. nk = 4 5 6 7 8
  43. ------------------------------
  44. cx->n_col = 4 10 9 8 7 7
  45. cx->n_col = 5 14 11 10 9 9
  46. cx->n_col = 6 19 15 12 11 11
  47. cx->n_col = 7 21 19 16 13 14
  48. cx->n_col = 8 29 23 19 17 14
  49. */
  50. #define ke4(k,i) \
  51. { k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+5] = ss[1] ^= ss[0]; \
  52. k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \
  53. }
  54. #define kel4(k,i) \
  55. { k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+5] = ss[1] ^= ss[0]; \
  56. k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \
  57. }
  58. #define ke6(k,i) \
  59. { k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 7] = ss[1] ^= ss[0]; \
  60. k[6*(i)+ 8] = ss[2] ^= ss[1]; k[6*(i)+ 9] = ss[3] ^= ss[2]; \
  61. k[6*(i)+10] = ss[4] ^= ss[3]; k[6*(i)+11] = ss[5] ^= ss[4]; \
  62. }
  63. #define kel6(k,i) \
  64. { k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 7] = ss[1] ^= ss[0]; \
  65. k[6*(i)+ 8] = ss[2] ^= ss[1]; k[6*(i)+ 9] = ss[3] ^= ss[2]; \
  66. }
  67. #define ke8(k,i) \
  68. { k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 9] = ss[1] ^= ss[0]; \
  69. k[8*(i)+10] = ss[2] ^= ss[1]; k[8*(i)+11] = ss[3] ^= ss[2]; \
  70. k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); k[8*(i)+13] = ss[5] ^= ss[4]; \
  71. k[8*(i)+14] = ss[6] ^= ss[5]; k[8*(i)+15] = ss[7] ^= ss[6]; \
  72. }
  73. #define kel8(k,i) \
  74. { k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 9] = ss[1] ^= ss[0]; \
  75. k[8*(i)+10] = ss[2] ^= ss[1]; k[8*(i)+11] = ss[3] ^= ss[2]; \
  76. }
  77. #if defined(ENCRYPTION_KEY_SCHEDULE)
  78. #if defined(AES_128) || defined(AES_VAR)
  79. aes_rval aes_encrypt_key128(const void *in_key, aes_encrypt_ctx cx[1])
  80. { aes_32t ss[4];
  81. cx->ks[0] = ss[0] = word_in(in_key, 0);
  82. cx->ks[1] = ss[1] = word_in(in_key, 1);
  83. cx->ks[2] = ss[2] = word_in(in_key, 2);
  84. cx->ks[3] = ss[3] = word_in(in_key, 3);
  85. #if ENC_UNROLL == NONE
  86. { aes_32t i;
  87. for(i = 0; i < ((11 * N_COLS - 1) / 4); ++i)
  88. ke4(cx->ks, i);
  89. }
  90. #else
  91. ke4(cx->ks, 0); ke4(cx->ks, 1);
  92. ke4(cx->ks, 2); ke4(cx->ks, 3);
  93. ke4(cx->ks, 4); ke4(cx->ks, 5);
  94. ke4(cx->ks, 6); ke4(cx->ks, 7);
  95. ke4(cx->ks, 8); kel4(cx->ks, 9);
  96. #endif
  97. /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */
  98. /* key and must be non-zero for 128 and 192 bits keys */
  99. cx->ks[53] = cx->ks[45] = 0;
  100. cx->ks[52] = 10;
  101. #ifdef AES_ERR_CHK
  102. return aes_good;
  103. #endif
  104. }
  105. #endif
  106. #if defined(AES_192) || defined(AES_VAR)
  107. aes_rval aes_encrypt_key192(const void *in_key, aes_encrypt_ctx cx[1])
  108. { aes_32t ss[6];
  109. cx->ks[0] = ss[0] = word_in(in_key, 0);
  110. cx->ks[1] = ss[1] = word_in(in_key, 1);
  111. cx->ks[2] = ss[2] = word_in(in_key, 2);
  112. cx->ks[3] = ss[3] = word_in(in_key, 3);
  113. cx->ks[4] = ss[4] = word_in(in_key, 4);
  114. cx->ks[5] = ss[5] = word_in(in_key, 5);
  115. #if ENC_UNROLL == NONE
  116. { aes_32t i;
  117. for(i = 0; i < (13 * N_COLS - 1) / 6; ++i)
  118. ke6(cx->ks, i);
  119. }
  120. #else
  121. ke6(cx->ks, 0); ke6(cx->ks, 1);
  122. ke6(cx->ks, 2); ke6(cx->ks, 3);
  123. ke6(cx->ks, 4); ke6(cx->ks, 5);
  124. ke6(cx->ks, 6); kel6(cx->ks, 7);
  125. #endif
  126. /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */
  127. /* key and must be non-zero for 128 and 192 bits keys */
  128. cx->ks[53] = cx->ks[45];
  129. cx->ks[52] = 12;
  130. #ifdef AES_ERR_CHK
  131. return aes_good;
  132. #endif
  133. }
  134. #endif
  135. #if defined(AES_256) || defined(AES_VAR)
  136. aes_rval aes_encrypt_key256(const void *in_key, aes_encrypt_ctx cx[1])
  137. { aes_32t ss[8];
  138. cx->ks[0] = ss[0] = word_in(in_key, 0);
  139. cx->ks[1] = ss[1] = word_in(in_key, 1);
  140. cx->ks[2] = ss[2] = word_in(in_key, 2);
  141. cx->ks[3] = ss[3] = word_in(in_key, 3);
  142. cx->ks[4] = ss[4] = word_in(in_key, 4);
  143. cx->ks[5] = ss[5] = word_in(in_key, 5);
  144. cx->ks[6] = ss[6] = word_in(in_key, 6);
  145. cx->ks[7] = ss[7] = word_in(in_key, 7);
  146. #if ENC_UNROLL == NONE
  147. { aes_32t i;
  148. for(i = 0; i < (15 * N_COLS - 1) / 8; ++i)
  149. ke8(cx->ks, i);
  150. }
  151. #else
  152. ke8(cx->ks, 0); ke8(cx->ks, 1);
  153. ke8(cx->ks, 2); ke8(cx->ks, 3);
  154. ke8(cx->ks, 4); ke8(cx->ks, 5);
  155. kel8(cx->ks, 6);
  156. #endif
  157. #ifdef AES_ERR_CHK
  158. return aes_good;
  159. #endif
  160. }
  161. #endif
  162. #if defined(AES_VAR)
  163. aes_rval aes_encrypt_key(const void *in_key, int key_len, aes_encrypt_ctx cx[1])
  164. {
  165. switch(key_len)
  166. {
  167. #ifdef AES_ERR_CHK
  168. case 16: case 128: return aes_encrypt_key128(in_key, cx);
  169. case 24: case 192: return aes_encrypt_key192(in_key, cx);
  170. case 32: case 256: return aes_encrypt_key256(in_key, cx);
  171. default: return aes_error;
  172. #else
  173. case 16: case 128: aes_encrypt_key128(in_key, cx); return;
  174. case 24: case 192: aes_encrypt_key192(in_key, cx); return;
  175. case 32: case 256: aes_encrypt_key256(in_key, cx); return;
  176. #endif
  177. }
  178. }
  179. #endif
  180. #endif
  181. #if defined(DECRYPTION_KEY_SCHEDULE)
  182. #if DEC_ROUND == NO_TABLES
  183. #define ff(x) (x)
  184. #else
  185. #define ff(x) inv_mcol(x)
  186. #ifdef dec_imvars
  187. #define d_vars dec_imvars
  188. #endif
  189. #endif
  190. #if 1
  191. #define kdf4(k,i) \
  192. { ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; ss[1] = ss[1] ^ ss[3]; ss[2] = ss[2] ^ ss[3]; ss[3] = ss[3]; \
  193. ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \
  194. ss[4] ^= k[4*(i)]; k[4*(i)+4] = ff(ss[4]); ss[4] ^= k[4*(i)+1]; k[4*(i)+5] = ff(ss[4]); \
  195. ss[4] ^= k[4*(i)+2]; k[4*(i)+6] = ff(ss[4]); ss[4] ^= k[4*(i)+3]; k[4*(i)+7] = ff(ss[4]); \
  196. }
  197. #define kd4(k,i) \
  198. { ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; ss[4] = ff(ss[4]); \
  199. k[4*(i)+4] = ss[4] ^= k[4*(i)]; k[4*(i)+5] = ss[4] ^= k[4*(i)+1]; \
  200. k[4*(i)+6] = ss[4] ^= k[4*(i)+2]; k[4*(i)+7] = ss[4] ^= k[4*(i)+3]; \
  201. }
  202. #define kdl4(k,i) \
  203. { ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \
  204. k[4*(i)+4] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; k[4*(i)+5] = ss[1] ^ ss[3]; \
  205. k[4*(i)+6] = ss[0]; k[4*(i)+7] = ss[1]; \
  206. }
  207. #else
  208. #define kdf4(k,i) \
  209. { ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+ 4] = ff(ss[0]); ss[1] ^= ss[0]; k[4*(i)+ 5] = ff(ss[1]); \
  210. ss[2] ^= ss[1]; k[4*(i)+ 6] = ff(ss[2]); ss[3] ^= ss[2]; k[4*(i)+ 7] = ff(ss[3]); \
  211. }
  212. #define kd4(k,i) \
  213. { ss[4] = ls_box(ss[3],3) ^ t_use(r,c)[i]; \
  214. ss[0] ^= ss[4]; ss[4] = ff(ss[4]); k[4*(i)+ 4] = ss[4] ^= k[4*(i)]; \
  215. ss[1] ^= ss[0]; k[4*(i)+ 5] = ss[4] ^= k[4*(i)+ 1]; \
  216. ss[2] ^= ss[1]; k[4*(i)+ 6] = ss[4] ^= k[4*(i)+ 2]; \
  217. ss[3] ^= ss[2]; k[4*(i)+ 7] = ss[4] ^= k[4*(i)+ 3]; \
  218. }
  219. #define kdl4(k,i) \
  220. { ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[4*(i)+ 4] = ss[0]; ss[1] ^= ss[0]; k[4*(i)+ 5] = ss[1]; \
  221. ss[2] ^= ss[1]; k[4*(i)+ 6] = ss[2]; ss[3] ^= ss[2]; k[4*(i)+ 7] = ss[3]; \
  222. }
  223. #endif
  224. #define kdf6(k,i) \
  225. { ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 6] = ff(ss[0]); ss[1] ^= ss[0]; k[6*(i)+ 7] = ff(ss[1]); \
  226. ss[2] ^= ss[1]; k[6*(i)+ 8] = ff(ss[2]); ss[3] ^= ss[2]; k[6*(i)+ 9] = ff(ss[3]); \
  227. ss[4] ^= ss[3]; k[6*(i)+10] = ff(ss[4]); ss[5] ^= ss[4]; k[6*(i)+11] = ff(ss[5]); \
  228. }
  229. #define kd6(k,i) \
  230. { ss[6] = ls_box(ss[5],3) ^ t_use(r,c)[i]; \
  231. ss[0] ^= ss[6]; ss[6] = ff(ss[6]); k[6*(i)+ 6] = ss[6] ^= k[6*(i)]; \
  232. ss[1] ^= ss[0]; k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \
  233. ss[2] ^= ss[1]; k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \
  234. ss[3] ^= ss[2]; k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \
  235. ss[4] ^= ss[3]; k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \
  236. ss[5] ^= ss[4]; k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \
  237. }
  238. #define kdl6(k,i) \
  239. { ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[6*(i)+ 6] = ss[0]; ss[1] ^= ss[0]; k[6*(i)+ 7] = ss[1]; \
  240. ss[2] ^= ss[1]; k[6*(i)+ 8] = ss[2]; ss[3] ^= ss[2]; k[6*(i)+ 9] = ss[3]; \
  241. }
  242. #define kdf8(k,i) \
  243. { ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 8] = ff(ss[0]); ss[1] ^= ss[0]; k[8*(i)+ 9] = ff(ss[1]); \
  244. ss[2] ^= ss[1]; k[8*(i)+10] = ff(ss[2]); ss[3] ^= ss[2]; k[8*(i)+11] = ff(ss[3]); \
  245. ss[4] ^= ls_box(ss[3],0); k[8*(i)+12] = ff(ss[4]); ss[5] ^= ss[4]; k[8*(i)+13] = ff(ss[5]); \
  246. ss[6] ^= ss[5]; k[8*(i)+14] = ff(ss[6]); ss[7] ^= ss[6]; k[8*(i)+15] = ff(ss[7]); \
  247. }
  248. #define kd8(k,i) \
  249. { aes_32t g = ls_box(ss[7],3) ^ t_use(r,c)[i]; \
  250. ss[0] ^= g; g = ff(g); k[8*(i)+ 8] = g ^= k[8*(i)]; \
  251. ss[1] ^= ss[0]; k[8*(i)+ 9] = g ^= k[8*(i)+ 1]; \
  252. ss[2] ^= ss[1]; k[8*(i)+10] = g ^= k[8*(i)+ 2]; \
  253. ss[3] ^= ss[2]; k[8*(i)+11] = g ^= k[8*(i)+ 3]; \
  254. g = ls_box(ss[3],0); \
  255. ss[4] ^= g; g = ff(g); k[8*(i)+12] = g ^= k[8*(i)+ 4]; \
  256. ss[5] ^= ss[4]; k[8*(i)+13] = g ^= k[8*(i)+ 5]; \
  257. ss[6] ^= ss[5]; k[8*(i)+14] = g ^= k[8*(i)+ 6]; \
  258. ss[7] ^= ss[6]; k[8*(i)+15] = g ^= k[8*(i)+ 7]; \
  259. }
  260. #define kdl8(k,i) \
  261. { ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[8*(i)+ 8] = ss[0]; ss[1] ^= ss[0]; k[8*(i)+ 9] = ss[1]; \
  262. ss[2] ^= ss[1]; k[8*(i)+10] = ss[2]; ss[3] ^= ss[2]; k[8*(i)+11] = ss[3]; \
  263. }
  264. #if defined(AES_128) || defined(AES_VAR)
  265. aes_rval aes_decrypt_key128(const void *in_key, aes_decrypt_ctx cx[1])
  266. { aes_32t ss[5];
  267. #ifdef d_vars
  268. d_vars;
  269. #endif
  270. cx->ks[0] = ss[0] = word_in(in_key, 0);
  271. cx->ks[1] = ss[1] = word_in(in_key, 1);
  272. cx->ks[2] = ss[2] = word_in(in_key, 2);
  273. cx->ks[3] = ss[3] = word_in(in_key, 3);
  274. #if DEC_UNROLL == NONE
  275. { aes_32t i;
  276. for(i = 0; i < (11 * N_COLS - 1) / 4; ++i)
  277. ke4(cx->ks, i);
  278. #if !(DEC_ROUND == NO_TABLES)
  279. for(i = N_COLS; i < 10 * N_COLS; ++i)
  280. cx->ks[i] = inv_mcol(cx->ks[i]);
  281. #endif
  282. }
  283. #else
  284. kdf4(cx->ks, 0); kd4(cx->ks, 1);
  285. kd4(cx->ks, 2); kd4(cx->ks, 3);
  286. kd4(cx->ks, 4); kd4(cx->ks, 5);
  287. kd4(cx->ks, 6); kd4(cx->ks, 7);
  288. kd4(cx->ks, 8); kdl4(cx->ks, 9);
  289. #endif
  290. /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */
  291. /* key and must be non-zero for 128 and 192 bits keys */
  292. cx->ks[53] = cx->ks[45] = 0;
  293. cx->ks[52] = 10;
  294. #ifdef AES_ERR_CHK
  295. return aes_good;
  296. #endif
  297. }
  298. #endif
  299. #if defined(AES_192) || defined(AES_VAR)
  300. aes_rval aes_decrypt_key192(const void *in_key, aes_decrypt_ctx cx[1])
  301. { aes_32t ss[7];
  302. #ifdef d_vars
  303. d_vars;
  304. #endif
  305. cx->ks[0] = ss[0] = word_in(in_key, 0);
  306. cx->ks[1] = ss[1] = word_in(in_key, 1);
  307. cx->ks[2] = ss[2] = word_in(in_key, 2);
  308. cx->ks[3] = ss[3] = word_in(in_key, 3);
  309. #if DEC_UNROLL == NONE
  310. cx->ks[4] = ss[4] = word_in(in_key, 4);
  311. cx->ks[5] = ss[5] = word_in(in_key, 5);
  312. { aes_32t i;
  313. for(i = 0; i < (13 * N_COLS - 1) / 6; ++i)
  314. ke6(cx->ks, i);
  315. #if !(DEC_ROUND == NO_TABLES)
  316. for(i = N_COLS; i < 12 * N_COLS; ++i)
  317. cx->ks[i] = inv_mcol(cx->ks[i]);
  318. #endif
  319. }
  320. #else
  321. cx->ks[4] = ff(ss[4] = word_in(in_key, 4));
  322. cx->ks[5] = ff(ss[5] = word_in(in_key, 5));
  323. kdf6(cx->ks, 0); kd6(cx->ks, 1);
  324. kd6(cx->ks, 2); kd6(cx->ks, 3);
  325. kd6(cx->ks, 4); kd6(cx->ks, 5);
  326. kd6(cx->ks, 6); kdl6(cx->ks, 7);
  327. #endif
  328. /* cx->ks[45] ^ cx->ks[52] ^ cx->ks[53] is zero for a 256 bit */
  329. /* key and must be non-zero for 128 and 192 bits keys */
  330. cx->ks[53] = cx->ks[45];
  331. cx->ks[52] = 12;
  332. #ifdef AES_ERR_CHK
  333. return aes_good;
  334. #endif
  335. }
  336. #endif
  337. #if defined(AES_256) || defined(AES_VAR)
  338. aes_rval aes_decrypt_key256(const void *in_key, aes_decrypt_ctx cx[1])
  339. { aes_32t ss[8];
  340. #ifdef d_vars
  341. d_vars;
  342. #endif
  343. cx->ks[0] = ss[0] = word_in(in_key, 0);
  344. cx->ks[1] = ss[1] = word_in(in_key, 1);
  345. cx->ks[2] = ss[2] = word_in(in_key, 2);
  346. cx->ks[3] = ss[3] = word_in(in_key, 3);
  347. #if DEC_UNROLL == NONE
  348. cx->ks[4] = ss[4] = word_in(in_key, 4);
  349. cx->ks[5] = ss[5] = word_in(in_key, 5);
  350. cx->ks[6] = ss[6] = word_in(in_key, 6);
  351. cx->ks[7] = ss[7] = word_in(in_key, 7);
  352. { aes_32t i;
  353. for(i = 0; i < (15 * N_COLS - 1) / 8; ++i)
  354. ke8(cx->ks, i);
  355. #if !(DEC_ROUND == NO_TABLES)
  356. for(i = N_COLS; i < 14 * N_COLS; ++i)
  357. cx->ks[i] = inv_mcol(cx->ks[i]);
  358. #endif
  359. }
  360. #else
  361. cx->ks[4] = ff(ss[4] = word_in(in_key, 4));
  362. cx->ks[5] = ff(ss[5] = word_in(in_key, 5));
  363. cx->ks[6] = ff(ss[6] = word_in(in_key, 6));
  364. cx->ks[7] = ff(ss[7] = word_in(in_key, 7));
  365. kdf8(cx->ks, 0); kd8(cx->ks, 1);
  366. kd8(cx->ks, 2); kd8(cx->ks, 3);
  367. kd8(cx->ks, 4); kd8(cx->ks, 5);
  368. kdl8(cx->ks, 6);
  369. #endif
  370. #ifdef AES_ERR_CHK
  371. return aes_good;
  372. #endif
  373. }
  374. #endif
  375. #if defined(AES_VAR)
  376. aes_rval aes_decrypt_key(const void *in_key, int key_len, aes_decrypt_ctx cx[1])
  377. {
  378. switch(key_len)
  379. {
  380. #ifdef AES_ERR_CHK
  381. case 16: case 128: return aes_decrypt_key128(in_key, cx);
  382. case 24: case 192: return aes_decrypt_key192(in_key, cx);
  383. case 32: case 256: return aes_decrypt_key256(in_key, cx);
  384. default: return aes_error;
  385. #else
  386. case 16: case 128: aes_decrypt_key128(in_key, cx); return;
  387. case 24: case 192: aes_decrypt_key192(in_key, cx); return;
  388. case 32: case 256: aes_decrypt_key256(in_key, cx); return;
  389. #endif
  390. }
  391. }
  392. #endif
  393. #endif
  394. #if defined(__cplusplus)
  395. }
  396. #endif