aes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. *
  3. * Glue Code for optimized 586 assembler version of AES
  4. *
  5. * Copyright (c) 2002, Dr Brian Gladman <>, Worcester, UK.
  6. * All rights reserved.
  7. *
  8. * LICENSE TERMS
  9. *
  10. * The free distribution and use of this software in both source and binary
  11. * form is allowed (with or without changes) provided that:
  12. *
  13. * 1. distributions of this source code include the above copyright
  14. * notice, this list of conditions and the following disclaimer;
  15. *
  16. * 2. distributions in binary form include the above copyright
  17. * notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other associated materials;
  19. *
  20. * 3. the copyright holder's name is not used to endorse products
  21. * built using this software without specific written permission.
  22. *
  23. * ALTERNATIVELY, provided that this notice is retained in full, this product
  24. * may be distributed under the terms of the GNU General Public License (GPL),
  25. * in which case the provisions of the GPL apply INSTEAD OF those given above.
  26. *
  27. * DISCLAIMER
  28. *
  29. * This software is provided 'as is' with no explicit or implied warranties
  30. * in respect of its properties, including, but not limited to, correctness
  31. * and/or fitness for purpose.
  32. *
  33. * Copyright (c) 2003, Adam J. Richter <adam@yggdrasil.com> (conversion to
  34. * 2.5 API).
  35. * Copyright (c) 2003, 2004 Fruhwirth Clemens <clemens@endorphin.org>
  36. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  37. *
  38. */
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/init.h>
  42. #include <linux/types.h>
  43. #include <linux/crypto.h>
  44. #include <linux/linkage.h>
  45. asmlinkage void aes_enc_blk(const u8 *src, u8 *dst, void *ctx);
  46. asmlinkage void aes_dec_blk(const u8 *src, u8 *dst, void *ctx);
  47. #define AES_MIN_KEY_SIZE 16
  48. #define AES_MAX_KEY_SIZE 32
  49. #define AES_BLOCK_SIZE 16
  50. #define AES_KS_LENGTH 4 * AES_BLOCK_SIZE
  51. #define RC_LENGTH 29
  52. struct aes_ctx {
  53. u32 ekey[AES_KS_LENGTH];
  54. u32 rounds;
  55. u32 dkey[AES_KS_LENGTH];
  56. };
  57. #define WPOLY 0x011b
  58. #define u32_in(x) le32_to_cpup((const __le32 *)(x))
  59. #define bytes2word(b0, b1, b2, b3) \
  60. (((u32)(b3) << 24) | ((u32)(b2) << 16) | ((u32)(b1) << 8) | (b0))
  61. /* define the finite field multiplies required for Rijndael */
  62. #define f2(x) ((x) ? pow[log[x] + 0x19] : 0)
  63. #define f3(x) ((x) ? pow[log[x] + 0x01] : 0)
  64. #define f9(x) ((x) ? pow[log[x] + 0xc7] : 0)
  65. #define fb(x) ((x) ? pow[log[x] + 0x68] : 0)
  66. #define fd(x) ((x) ? pow[log[x] + 0xee] : 0)
  67. #define fe(x) ((x) ? pow[log[x] + 0xdf] : 0)
  68. #define fi(x) ((x) ? pow[255 - log[x]]: 0)
  69. static inline u32 upr(u32 x, int n)
  70. {
  71. return (x << 8 * n) | (x >> (32 - 8 * n));
  72. }
  73. static inline u8 bval(u32 x, int n)
  74. {
  75. return x >> 8 * n;
  76. }
  77. /* The forward and inverse affine transformations used in the S-box */
  78. #define fwd_affine(x) \
  79. (w = (u32)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(u8)(w^(w>>8)))
  80. #define inv_affine(x) \
  81. (w = (u32)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(u8)(w^(w>>8)))
  82. static u32 rcon_tab[RC_LENGTH];
  83. u32 ft_tab[4][256];
  84. u32 fl_tab[4][256];
  85. static u32 ls_tab[4][256];
  86. static u32 im_tab[4][256];
  87. u32 il_tab[4][256];
  88. u32 it_tab[4][256];
  89. static void gen_tabs(void)
  90. {
  91. u32 i, w;
  92. u8 pow[512], log[256];
  93. /*
  94. * log and power tables for GF(2^8) finite field with
  95. * WPOLY as modular polynomial - the simplest primitive
  96. * root is 0x03, used here to generate the tables.
  97. */
  98. i = 0; w = 1;
  99. do {
  100. pow[i] = (u8)w;
  101. pow[i + 255] = (u8)w;
  102. log[w] = (u8)i++;
  103. w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0);
  104. } while (w != 1);
  105. for(i = 0, w = 1; i < RC_LENGTH; ++i) {
  106. rcon_tab[i] = bytes2word(w, 0, 0, 0);
  107. w = f2(w);
  108. }
  109. for(i = 0; i < 256; ++i) {
  110. u8 b;
  111. b = fwd_affine(fi((u8)i));
  112. w = bytes2word(f2(b), b, b, f3(b));
  113. /* tables for a normal encryption round */
  114. ft_tab[0][i] = w;
  115. ft_tab[1][i] = upr(w, 1);
  116. ft_tab[2][i] = upr(w, 2);
  117. ft_tab[3][i] = upr(w, 3);
  118. w = bytes2word(b, 0, 0, 0);
  119. /*
  120. * tables for last encryption round
  121. * (may also be used in the key schedule)
  122. */
  123. fl_tab[0][i] = w;
  124. fl_tab[1][i] = upr(w, 1);
  125. fl_tab[2][i] = upr(w, 2);
  126. fl_tab[3][i] = upr(w, 3);
  127. /*
  128. * table for key schedule if fl_tab above is
  129. * not of the required form
  130. */
  131. ls_tab[0][i] = w;
  132. ls_tab[1][i] = upr(w, 1);
  133. ls_tab[2][i] = upr(w, 2);
  134. ls_tab[3][i] = upr(w, 3);
  135. b = fi(inv_affine((u8)i));
  136. w = bytes2word(fe(b), f9(b), fd(b), fb(b));
  137. /* tables for the inverse mix column operation */
  138. im_tab[0][b] = w;
  139. im_tab[1][b] = upr(w, 1);
  140. im_tab[2][b] = upr(w, 2);
  141. im_tab[3][b] = upr(w, 3);
  142. /* tables for a normal decryption round */
  143. it_tab[0][i] = w;
  144. it_tab[1][i] = upr(w,1);
  145. it_tab[2][i] = upr(w,2);
  146. it_tab[3][i] = upr(w,3);
  147. w = bytes2word(b, 0, 0, 0);
  148. /* tables for last decryption round */
  149. il_tab[0][i] = w;
  150. il_tab[1][i] = upr(w,1);
  151. il_tab[2][i] = upr(w,2);
  152. il_tab[3][i] = upr(w,3);
  153. }
  154. }
  155. #define four_tables(x,tab,vf,rf,c) \
  156. ( tab[0][bval(vf(x,0,c),rf(0,c))] ^ \
  157. tab[1][bval(vf(x,1,c),rf(1,c))] ^ \
  158. tab[2][bval(vf(x,2,c),rf(2,c))] ^ \
  159. tab[3][bval(vf(x,3,c),rf(3,c))] \
  160. )
  161. #define vf1(x,r,c) (x)
  162. #define rf1(r,c) (r)
  163. #define rf2(r,c) ((r-c)&3)
  164. #define inv_mcol(x) four_tables(x,im_tab,vf1,rf1,0)
  165. #define ls_box(x,c) four_tables(x,fl_tab,vf1,rf2,c)
  166. #define ff(x) inv_mcol(x)
  167. #define ke4(k,i) \
  168. { \
  169. k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
  170. k[4*(i)+5] = ss[1] ^= ss[0]; \
  171. k[4*(i)+6] = ss[2] ^= ss[1]; \
  172. k[4*(i)+7] = ss[3] ^= ss[2]; \
  173. }
  174. #define kel4(k,i) \
  175. { \
  176. k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
  177. k[4*(i)+5] = ss[1] ^= ss[0]; \
  178. k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \
  179. }
  180. #define ke6(k,i) \
  181. { \
  182. k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
  183. k[6*(i)+ 7] = ss[1] ^= ss[0]; \
  184. k[6*(i)+ 8] = ss[2] ^= ss[1]; \
  185. k[6*(i)+ 9] = ss[3] ^= ss[2]; \
  186. k[6*(i)+10] = ss[4] ^= ss[3]; \
  187. k[6*(i)+11] = ss[5] ^= ss[4]; \
  188. }
  189. #define kel6(k,i) \
  190. { \
  191. k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
  192. k[6*(i)+ 7] = ss[1] ^= ss[0]; \
  193. k[6*(i)+ 8] = ss[2] ^= ss[1]; \
  194. k[6*(i)+ 9] = ss[3] ^= ss[2]; \
  195. }
  196. #define ke8(k,i) \
  197. { \
  198. k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
  199. k[8*(i)+ 9] = ss[1] ^= ss[0]; \
  200. k[8*(i)+10] = ss[2] ^= ss[1]; \
  201. k[8*(i)+11] = ss[3] ^= ss[2]; \
  202. k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); \
  203. k[8*(i)+13] = ss[5] ^= ss[4]; \
  204. k[8*(i)+14] = ss[6] ^= ss[5]; \
  205. k[8*(i)+15] = ss[7] ^= ss[6]; \
  206. }
  207. #define kel8(k,i) \
  208. { \
  209. k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
  210. k[8*(i)+ 9] = ss[1] ^= ss[0]; \
  211. k[8*(i)+10] = ss[2] ^= ss[1]; \
  212. k[8*(i)+11] = ss[3] ^= ss[2]; \
  213. }
  214. #define kdf4(k,i) \
  215. { \
  216. ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \
  217. ss[1] = ss[1] ^ ss[3]; \
  218. ss[2] = ss[2] ^ ss[3]; \
  219. ss[3] = ss[3]; \
  220. ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
  221. ss[i % 4] ^= ss[4]; \
  222. ss[4] ^= k[4*(i)]; \
  223. k[4*(i)+4] = ff(ss[4]); \
  224. ss[4] ^= k[4*(i)+1]; \
  225. k[4*(i)+5] = ff(ss[4]); \
  226. ss[4] ^= k[4*(i)+2]; \
  227. k[4*(i)+6] = ff(ss[4]); \
  228. ss[4] ^= k[4*(i)+3]; \
  229. k[4*(i)+7] = ff(ss[4]); \
  230. }
  231. #define kd4(k,i) \
  232. { \
  233. ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
  234. ss[i % 4] ^= ss[4]; \
  235. ss[4] = ff(ss[4]); \
  236. k[4*(i)+4] = ss[4] ^= k[4*(i)]; \
  237. k[4*(i)+5] = ss[4] ^= k[4*(i)+1]; \
  238. k[4*(i)+6] = ss[4] ^= k[4*(i)+2]; \
  239. k[4*(i)+7] = ss[4] ^= k[4*(i)+3]; \
  240. }
  241. #define kdl4(k,i) \
  242. { \
  243. ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
  244. ss[i % 4] ^= ss[4]; \
  245. k[4*(i)+4] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \
  246. k[4*(i)+5] = ss[1] ^ ss[3]; \
  247. k[4*(i)+6] = ss[0]; \
  248. k[4*(i)+7] = ss[1]; \
  249. }
  250. #define kdf6(k,i) \
  251. { \
  252. ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
  253. k[6*(i)+ 6] = ff(ss[0]); \
  254. ss[1] ^= ss[0]; \
  255. k[6*(i)+ 7] = ff(ss[1]); \
  256. ss[2] ^= ss[1]; \
  257. k[6*(i)+ 8] = ff(ss[2]); \
  258. ss[3] ^= ss[2]; \
  259. k[6*(i)+ 9] = ff(ss[3]); \
  260. ss[4] ^= ss[3]; \
  261. k[6*(i)+10] = ff(ss[4]); \
  262. ss[5] ^= ss[4]; \
  263. k[6*(i)+11] = ff(ss[5]); \
  264. }
  265. #define kd6(k,i) \
  266. { \
  267. ss[6] = ls_box(ss[5],3) ^ rcon_tab[i]; \
  268. ss[0] ^= ss[6]; ss[6] = ff(ss[6]); \
  269. k[6*(i)+ 6] = ss[6] ^= k[6*(i)]; \
  270. ss[1] ^= ss[0]; \
  271. k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \
  272. ss[2] ^= ss[1]; \
  273. k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \
  274. ss[3] ^= ss[2]; \
  275. k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \
  276. ss[4] ^= ss[3]; \
  277. k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \
  278. ss[5] ^= ss[4]; \
  279. k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \
  280. }
  281. #define kdl6(k,i) \
  282. { \
  283. ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
  284. k[6*(i)+ 6] = ss[0]; \
  285. ss[1] ^= ss[0]; \
  286. k[6*(i)+ 7] = ss[1]; \
  287. ss[2] ^= ss[1]; \
  288. k[6*(i)+ 8] = ss[2]; \
  289. ss[3] ^= ss[2]; \
  290. k[6*(i)+ 9] = ss[3]; \
  291. }
  292. #define kdf8(k,i) \
  293. { \
  294. ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
  295. k[8*(i)+ 8] = ff(ss[0]); \
  296. ss[1] ^= ss[0]; \
  297. k[8*(i)+ 9] = ff(ss[1]); \
  298. ss[2] ^= ss[1]; \
  299. k[8*(i)+10] = ff(ss[2]); \
  300. ss[3] ^= ss[2]; \
  301. k[8*(i)+11] = ff(ss[3]); \
  302. ss[4] ^= ls_box(ss[3],0); \
  303. k[8*(i)+12] = ff(ss[4]); \
  304. ss[5] ^= ss[4]; \
  305. k[8*(i)+13] = ff(ss[5]); \
  306. ss[6] ^= ss[5]; \
  307. k[8*(i)+14] = ff(ss[6]); \
  308. ss[7] ^= ss[6]; \
  309. k[8*(i)+15] = ff(ss[7]); \
  310. }
  311. #define kd8(k,i) \
  312. { \
  313. u32 __g = ls_box(ss[7],3) ^ rcon_tab[i]; \
  314. ss[0] ^= __g; \
  315. __g = ff(__g); \
  316. k[8*(i)+ 8] = __g ^= k[8*(i)]; \
  317. ss[1] ^= ss[0]; \
  318. k[8*(i)+ 9] = __g ^= k[8*(i)+ 1]; \
  319. ss[2] ^= ss[1]; \
  320. k[8*(i)+10] = __g ^= k[8*(i)+ 2]; \
  321. ss[3] ^= ss[2]; \
  322. k[8*(i)+11] = __g ^= k[8*(i)+ 3]; \
  323. __g = ls_box(ss[3],0); \
  324. ss[4] ^= __g; \
  325. __g = ff(__g); \
  326. k[8*(i)+12] = __g ^= k[8*(i)+ 4]; \
  327. ss[5] ^= ss[4]; \
  328. k[8*(i)+13] = __g ^= k[8*(i)+ 5]; \
  329. ss[6] ^= ss[5]; \
  330. k[8*(i)+14] = __g ^= k[8*(i)+ 6]; \
  331. ss[7] ^= ss[6]; \
  332. k[8*(i)+15] = __g ^= k[8*(i)+ 7]; \
  333. }
  334. #define kdl8(k,i) \
  335. { \
  336. ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
  337. k[8*(i)+ 8] = ss[0]; \
  338. ss[1] ^= ss[0]; \
  339. k[8*(i)+ 9] = ss[1]; \
  340. ss[2] ^= ss[1]; \
  341. k[8*(i)+10] = ss[2]; \
  342. ss[3] ^= ss[2]; \
  343. k[8*(i)+11] = ss[3]; \
  344. }
  345. static int
  346. aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
  347. {
  348. int i;
  349. u32 ss[8];
  350. struct aes_ctx *ctx = ctx_arg;
  351. /* encryption schedule */
  352. ctx->ekey[0] = ss[0] = u32_in(in_key);
  353. ctx->ekey[1] = ss[1] = u32_in(in_key + 4);
  354. ctx->ekey[2] = ss[2] = u32_in(in_key + 8);
  355. ctx->ekey[3] = ss[3] = u32_in(in_key + 12);
  356. switch(key_len) {
  357. case 16:
  358. for (i = 0; i < 9; i++)
  359. ke4(ctx->ekey, i);
  360. kel4(ctx->ekey, 9);
  361. ctx->rounds = 10;
  362. break;
  363. case 24:
  364. ctx->ekey[4] = ss[4] = u32_in(in_key + 16);
  365. ctx->ekey[5] = ss[5] = u32_in(in_key + 20);
  366. for (i = 0; i < 7; i++)
  367. ke6(ctx->ekey, i);
  368. kel6(ctx->ekey, 7);
  369. ctx->rounds = 12;
  370. break;
  371. case 32:
  372. ctx->ekey[4] = ss[4] = u32_in(in_key + 16);
  373. ctx->ekey[5] = ss[5] = u32_in(in_key + 20);
  374. ctx->ekey[6] = ss[6] = u32_in(in_key + 24);
  375. ctx->ekey[7] = ss[7] = u32_in(in_key + 28);
  376. for (i = 0; i < 6; i++)
  377. ke8(ctx->ekey, i);
  378. kel8(ctx->ekey, 6);
  379. ctx->rounds = 14;
  380. break;
  381. default:
  382. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  383. return -EINVAL;
  384. }
  385. /* decryption schedule */
  386. ctx->dkey[0] = ss[0] = u32_in(in_key);
  387. ctx->dkey[1] = ss[1] = u32_in(in_key + 4);
  388. ctx->dkey[2] = ss[2] = u32_in(in_key + 8);
  389. ctx->dkey[3] = ss[3] = u32_in(in_key + 12);
  390. switch (key_len) {
  391. case 16:
  392. kdf4(ctx->dkey, 0);
  393. for (i = 1; i < 9; i++)
  394. kd4(ctx->dkey, i);
  395. kdl4(ctx->dkey, 9);
  396. break;
  397. case 24:
  398. ctx->dkey[4] = ff(ss[4] = u32_in(in_key + 16));
  399. ctx->dkey[5] = ff(ss[5] = u32_in(in_key + 20));
  400. kdf6(ctx->dkey, 0);
  401. for (i = 1; i < 7; i++)
  402. kd6(ctx->dkey, i);
  403. kdl6(ctx->dkey, 7);
  404. break;
  405. case 32:
  406. ctx->dkey[4] = ff(ss[4] = u32_in(in_key + 16));
  407. ctx->dkey[5] = ff(ss[5] = u32_in(in_key + 20));
  408. ctx->dkey[6] = ff(ss[6] = u32_in(in_key + 24));
  409. ctx->dkey[7] = ff(ss[7] = u32_in(in_key + 28));
  410. kdf8(ctx->dkey, 0);
  411. for (i = 1; i < 6; i++)
  412. kd8(ctx->dkey, i);
  413. kdl8(ctx->dkey, 6);
  414. break;
  415. }
  416. return 0;
  417. }
  418. static inline void aes_encrypt(void *ctx, u8 *dst, const u8 *src)
  419. {
  420. aes_enc_blk(src, dst, ctx);
  421. }
  422. static inline void aes_decrypt(void *ctx, u8 *dst, const u8 *src)
  423. {
  424. aes_dec_blk(src, dst, ctx);
  425. }
  426. static struct crypto_alg aes_alg = {
  427. .cra_name = "aes",
  428. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  429. .cra_blocksize = AES_BLOCK_SIZE,
  430. .cra_ctxsize = sizeof(struct aes_ctx),
  431. .cra_module = THIS_MODULE,
  432. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  433. .cra_u = {
  434. .cipher = {
  435. .cia_min_keysize = AES_MIN_KEY_SIZE,
  436. .cia_max_keysize = AES_MAX_KEY_SIZE,
  437. .cia_setkey = aes_set_key,
  438. .cia_encrypt = aes_encrypt,
  439. .cia_decrypt = aes_decrypt
  440. }
  441. }
  442. };
  443. static int __init aes_init(void)
  444. {
  445. gen_tabs();
  446. return crypto_register_alg(&aes_alg);
  447. }
  448. static void __exit aes_fini(void)
  449. {
  450. crypto_unregister_alg(&aes_alg);
  451. }
  452. module_init(aes_init);
  453. module_exit(aes_fini);
  454. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, i586 asm optimized");
  455. MODULE_LICENSE("Dual BSD/GPL");
  456. MODULE_AUTHOR("Fruhwirth Clemens, James Morris, Brian Gladman, Adam Richter");
  457. MODULE_ALIAS("aes");