2
0

aeskey.c 16 KB

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