aes_32.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 <asm/byteorder.h>
  40. #include <linux/kernel.h>
  41. #include <linux/module.h>
  42. #include <linux/init.h>
  43. #include <linux/types.h>
  44. #include <linux/crypto.h>
  45. #include <linux/linkage.h>
  46. asmlinkage void aes_enc_blk(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  47. asmlinkage void aes_dec_blk(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  48. #define AES_MIN_KEY_SIZE 16
  49. #define AES_MAX_KEY_SIZE 32
  50. #define AES_BLOCK_SIZE 16
  51. #define AES_KS_LENGTH 4 * AES_BLOCK_SIZE
  52. #define RC_LENGTH 29
  53. struct aes_ctx {
  54. u32 ekey[AES_KS_LENGTH];
  55. u32 rounds;
  56. u32 dkey[AES_KS_LENGTH];
  57. };
  58. #define WPOLY 0x011b
  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 im_tab[4][256];
  86. u32 il_tab[4][256];
  87. u32 it_tab[4][256];
  88. static void gen_tabs(void)
  89. {
  90. u32 i, w;
  91. u8 pow[512], log[256];
  92. /*
  93. * log and power tables for GF(2^8) finite field with
  94. * WPOLY as modular polynomial - the simplest primitive
  95. * root is 0x03, used here to generate the tables.
  96. */
  97. i = 0; w = 1;
  98. do {
  99. pow[i] = (u8)w;
  100. pow[i + 255] = (u8)w;
  101. log[w] = (u8)i++;
  102. w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0);
  103. } while (w != 1);
  104. for(i = 0, w = 1; i < RC_LENGTH; ++i) {
  105. rcon_tab[i] = bytes2word(w, 0, 0, 0);
  106. w = f2(w);
  107. }
  108. for(i = 0; i < 256; ++i) {
  109. u8 b;
  110. b = fwd_affine(fi((u8)i));
  111. w = bytes2word(f2(b), b, b, f3(b));
  112. /* tables for a normal encryption round */
  113. ft_tab[0][i] = w;
  114. ft_tab[1][i] = upr(w, 1);
  115. ft_tab[2][i] = upr(w, 2);
  116. ft_tab[3][i] = upr(w, 3);
  117. w = bytes2word(b, 0, 0, 0);
  118. /*
  119. * tables for last encryption round
  120. * (may also be used in the key schedule)
  121. */
  122. fl_tab[0][i] = w;
  123. fl_tab[1][i] = upr(w, 1);
  124. fl_tab[2][i] = upr(w, 2);
  125. fl_tab[3][i] = upr(w, 3);
  126. b = fi(inv_affine((u8)i));
  127. w = bytes2word(fe(b), f9(b), fd(b), fb(b));
  128. /* tables for the inverse mix column operation */
  129. im_tab[0][b] = w;
  130. im_tab[1][b] = upr(w, 1);
  131. im_tab[2][b] = upr(w, 2);
  132. im_tab[3][b] = upr(w, 3);
  133. /* tables for a normal decryption round */
  134. it_tab[0][i] = w;
  135. it_tab[1][i] = upr(w,1);
  136. it_tab[2][i] = upr(w,2);
  137. it_tab[3][i] = upr(w,3);
  138. w = bytes2word(b, 0, 0, 0);
  139. /* tables for last decryption round */
  140. il_tab[0][i] = w;
  141. il_tab[1][i] = upr(w,1);
  142. il_tab[2][i] = upr(w,2);
  143. il_tab[3][i] = upr(w,3);
  144. }
  145. }
  146. #define four_tables(x,tab,vf,rf,c) \
  147. ( tab[0][bval(vf(x,0,c),rf(0,c))] ^ \
  148. tab[1][bval(vf(x,1,c),rf(1,c))] ^ \
  149. tab[2][bval(vf(x,2,c),rf(2,c))] ^ \
  150. tab[3][bval(vf(x,3,c),rf(3,c))] \
  151. )
  152. #define vf1(x,r,c) (x)
  153. #define rf1(r,c) (r)
  154. #define rf2(r,c) ((r-c)&3)
  155. #define inv_mcol(x) four_tables(x,im_tab,vf1,rf1,0)
  156. #define ls_box(x,c) four_tables(x,fl_tab,vf1,rf2,c)
  157. #define ff(x) inv_mcol(x)
  158. #define ke4(k,i) \
  159. { \
  160. k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
  161. k[4*(i)+5] = ss[1] ^= ss[0]; \
  162. k[4*(i)+6] = ss[2] ^= ss[1]; \
  163. k[4*(i)+7] = ss[3] ^= ss[2]; \
  164. }
  165. #define kel4(k,i) \
  166. { \
  167. k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ rcon_tab[i]; \
  168. k[4*(i)+5] = ss[1] ^= ss[0]; \
  169. k[4*(i)+6] = ss[2] ^= ss[1]; k[4*(i)+7] = ss[3] ^= ss[2]; \
  170. }
  171. #define ke6(k,i) \
  172. { \
  173. k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
  174. k[6*(i)+ 7] = ss[1] ^= ss[0]; \
  175. k[6*(i)+ 8] = ss[2] ^= ss[1]; \
  176. k[6*(i)+ 9] = ss[3] ^= ss[2]; \
  177. k[6*(i)+10] = ss[4] ^= ss[3]; \
  178. k[6*(i)+11] = ss[5] ^= ss[4]; \
  179. }
  180. #define kel6(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. }
  187. #define ke8(k,i) \
  188. { \
  189. k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
  190. k[8*(i)+ 9] = ss[1] ^= ss[0]; \
  191. k[8*(i)+10] = ss[2] ^= ss[1]; \
  192. k[8*(i)+11] = ss[3] ^= ss[2]; \
  193. k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); \
  194. k[8*(i)+13] = ss[5] ^= ss[4]; \
  195. k[8*(i)+14] = ss[6] ^= ss[5]; \
  196. k[8*(i)+15] = ss[7] ^= ss[6]; \
  197. }
  198. #define kel8(k,i) \
  199. { \
  200. k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
  201. k[8*(i)+ 9] = ss[1] ^= ss[0]; \
  202. k[8*(i)+10] = ss[2] ^= ss[1]; \
  203. k[8*(i)+11] = ss[3] ^= ss[2]; \
  204. }
  205. #define kdf4(k,i) \
  206. { \
  207. ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \
  208. ss[1] = ss[1] ^ ss[3]; \
  209. ss[2] = ss[2] ^ ss[3]; \
  210. ss[3] = ss[3]; \
  211. ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
  212. ss[i % 4] ^= ss[4]; \
  213. ss[4] ^= k[4*(i)]; \
  214. k[4*(i)+4] = ff(ss[4]); \
  215. ss[4] ^= k[4*(i)+1]; \
  216. k[4*(i)+5] = ff(ss[4]); \
  217. ss[4] ^= k[4*(i)+2]; \
  218. k[4*(i)+6] = ff(ss[4]); \
  219. ss[4] ^= k[4*(i)+3]; \
  220. k[4*(i)+7] = ff(ss[4]); \
  221. }
  222. #define kd4(k,i) \
  223. { \
  224. ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
  225. ss[i % 4] ^= ss[4]; \
  226. ss[4] = ff(ss[4]); \
  227. k[4*(i)+4] = ss[4] ^= k[4*(i)]; \
  228. k[4*(i)+5] = ss[4] ^= k[4*(i)+1]; \
  229. k[4*(i)+6] = ss[4] ^= k[4*(i)+2]; \
  230. k[4*(i)+7] = ss[4] ^= k[4*(i)+3]; \
  231. }
  232. #define kdl4(k,i) \
  233. { \
  234. ss[4] = ls_box(ss[(i+3) % 4], 3) ^ rcon_tab[i]; \
  235. ss[i % 4] ^= ss[4]; \
  236. k[4*(i)+4] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \
  237. k[4*(i)+5] = ss[1] ^ ss[3]; \
  238. k[4*(i)+6] = ss[0]; \
  239. k[4*(i)+7] = ss[1]; \
  240. }
  241. #define kdf6(k,i) \
  242. { \
  243. ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
  244. k[6*(i)+ 6] = ff(ss[0]); \
  245. ss[1] ^= ss[0]; \
  246. k[6*(i)+ 7] = ff(ss[1]); \
  247. ss[2] ^= ss[1]; \
  248. k[6*(i)+ 8] = ff(ss[2]); \
  249. ss[3] ^= ss[2]; \
  250. k[6*(i)+ 9] = ff(ss[3]); \
  251. ss[4] ^= ss[3]; \
  252. k[6*(i)+10] = ff(ss[4]); \
  253. ss[5] ^= ss[4]; \
  254. k[6*(i)+11] = ff(ss[5]); \
  255. }
  256. #define kd6(k,i) \
  257. { \
  258. ss[6] = ls_box(ss[5],3) ^ rcon_tab[i]; \
  259. ss[0] ^= ss[6]; ss[6] = ff(ss[6]); \
  260. k[6*(i)+ 6] = ss[6] ^= k[6*(i)]; \
  261. ss[1] ^= ss[0]; \
  262. k[6*(i)+ 7] = ss[6] ^= k[6*(i)+ 1]; \
  263. ss[2] ^= ss[1]; \
  264. k[6*(i)+ 8] = ss[6] ^= k[6*(i)+ 2]; \
  265. ss[3] ^= ss[2]; \
  266. k[6*(i)+ 9] = ss[6] ^= k[6*(i)+ 3]; \
  267. ss[4] ^= ss[3]; \
  268. k[6*(i)+10] = ss[6] ^= k[6*(i)+ 4]; \
  269. ss[5] ^= ss[4]; \
  270. k[6*(i)+11] = ss[6] ^= k[6*(i)+ 5]; \
  271. }
  272. #define kdl6(k,i) \
  273. { \
  274. ss[0] ^= ls_box(ss[5],3) ^ rcon_tab[i]; \
  275. k[6*(i)+ 6] = ss[0]; \
  276. ss[1] ^= ss[0]; \
  277. k[6*(i)+ 7] = ss[1]; \
  278. ss[2] ^= ss[1]; \
  279. k[6*(i)+ 8] = ss[2]; \
  280. ss[3] ^= ss[2]; \
  281. k[6*(i)+ 9] = ss[3]; \
  282. }
  283. #define kdf8(k,i) \
  284. { \
  285. ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
  286. k[8*(i)+ 8] = ff(ss[0]); \
  287. ss[1] ^= ss[0]; \
  288. k[8*(i)+ 9] = ff(ss[1]); \
  289. ss[2] ^= ss[1]; \
  290. k[8*(i)+10] = ff(ss[2]); \
  291. ss[3] ^= ss[2]; \
  292. k[8*(i)+11] = ff(ss[3]); \
  293. ss[4] ^= ls_box(ss[3],0); \
  294. k[8*(i)+12] = ff(ss[4]); \
  295. ss[5] ^= ss[4]; \
  296. k[8*(i)+13] = ff(ss[5]); \
  297. ss[6] ^= ss[5]; \
  298. k[8*(i)+14] = ff(ss[6]); \
  299. ss[7] ^= ss[6]; \
  300. k[8*(i)+15] = ff(ss[7]); \
  301. }
  302. #define kd8(k,i) \
  303. { \
  304. u32 __g = ls_box(ss[7],3) ^ rcon_tab[i]; \
  305. ss[0] ^= __g; \
  306. __g = ff(__g); \
  307. k[8*(i)+ 8] = __g ^= k[8*(i)]; \
  308. ss[1] ^= ss[0]; \
  309. k[8*(i)+ 9] = __g ^= k[8*(i)+ 1]; \
  310. ss[2] ^= ss[1]; \
  311. k[8*(i)+10] = __g ^= k[8*(i)+ 2]; \
  312. ss[3] ^= ss[2]; \
  313. k[8*(i)+11] = __g ^= k[8*(i)+ 3]; \
  314. __g = ls_box(ss[3],0); \
  315. ss[4] ^= __g; \
  316. __g = ff(__g); \
  317. k[8*(i)+12] = __g ^= k[8*(i)+ 4]; \
  318. ss[5] ^= ss[4]; \
  319. k[8*(i)+13] = __g ^= k[8*(i)+ 5]; \
  320. ss[6] ^= ss[5]; \
  321. k[8*(i)+14] = __g ^= k[8*(i)+ 6]; \
  322. ss[7] ^= ss[6]; \
  323. k[8*(i)+15] = __g ^= k[8*(i)+ 7]; \
  324. }
  325. #define kdl8(k,i) \
  326. { \
  327. ss[0] ^= ls_box(ss[7],3) ^ rcon_tab[i]; \
  328. k[8*(i)+ 8] = ss[0]; \
  329. ss[1] ^= ss[0]; \
  330. k[8*(i)+ 9] = ss[1]; \
  331. ss[2] ^= ss[1]; \
  332. k[8*(i)+10] = ss[2]; \
  333. ss[3] ^= ss[2]; \
  334. k[8*(i)+11] = ss[3]; \
  335. }
  336. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  337. unsigned int key_len)
  338. {
  339. int i;
  340. u32 ss[8];
  341. struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
  342. const __le32 *key = (const __le32 *)in_key;
  343. u32 *flags = &tfm->crt_flags;
  344. /* encryption schedule */
  345. ctx->ekey[0] = ss[0] = le32_to_cpu(key[0]);
  346. ctx->ekey[1] = ss[1] = le32_to_cpu(key[1]);
  347. ctx->ekey[2] = ss[2] = le32_to_cpu(key[2]);
  348. ctx->ekey[3] = ss[3] = le32_to_cpu(key[3]);
  349. switch(key_len) {
  350. case 16:
  351. for (i = 0; i < 9; i++)
  352. ke4(ctx->ekey, i);
  353. kel4(ctx->ekey, 9);
  354. ctx->rounds = 10;
  355. break;
  356. case 24:
  357. ctx->ekey[4] = ss[4] = le32_to_cpu(key[4]);
  358. ctx->ekey[5] = ss[5] = le32_to_cpu(key[5]);
  359. for (i = 0; i < 7; i++)
  360. ke6(ctx->ekey, i);
  361. kel6(ctx->ekey, 7);
  362. ctx->rounds = 12;
  363. break;
  364. case 32:
  365. ctx->ekey[4] = ss[4] = le32_to_cpu(key[4]);
  366. ctx->ekey[5] = ss[5] = le32_to_cpu(key[5]);
  367. ctx->ekey[6] = ss[6] = le32_to_cpu(key[6]);
  368. ctx->ekey[7] = ss[7] = le32_to_cpu(key[7]);
  369. for (i = 0; i < 6; i++)
  370. ke8(ctx->ekey, i);
  371. kel8(ctx->ekey, 6);
  372. ctx->rounds = 14;
  373. break;
  374. default:
  375. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  376. return -EINVAL;
  377. }
  378. /* decryption schedule */
  379. ctx->dkey[0] = ss[0] = le32_to_cpu(key[0]);
  380. ctx->dkey[1] = ss[1] = le32_to_cpu(key[1]);
  381. ctx->dkey[2] = ss[2] = le32_to_cpu(key[2]);
  382. ctx->dkey[3] = ss[3] = le32_to_cpu(key[3]);
  383. switch (key_len) {
  384. case 16:
  385. kdf4(ctx->dkey, 0);
  386. for (i = 1; i < 9; i++)
  387. kd4(ctx->dkey, i);
  388. kdl4(ctx->dkey, 9);
  389. break;
  390. case 24:
  391. ctx->dkey[4] = ff(ss[4] = le32_to_cpu(key[4]));
  392. ctx->dkey[5] = ff(ss[5] = le32_to_cpu(key[5]));
  393. kdf6(ctx->dkey, 0);
  394. for (i = 1; i < 7; i++)
  395. kd6(ctx->dkey, i);
  396. kdl6(ctx->dkey, 7);
  397. break;
  398. case 32:
  399. ctx->dkey[4] = ff(ss[4] = le32_to_cpu(key[4]));
  400. ctx->dkey[5] = ff(ss[5] = le32_to_cpu(key[5]));
  401. ctx->dkey[6] = ff(ss[6] = le32_to_cpu(key[6]));
  402. ctx->dkey[7] = ff(ss[7] = le32_to_cpu(key[7]));
  403. kdf8(ctx->dkey, 0);
  404. for (i = 1; i < 6; i++)
  405. kd8(ctx->dkey, i);
  406. kdl8(ctx->dkey, 6);
  407. break;
  408. }
  409. return 0;
  410. }
  411. static void aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  412. {
  413. aes_enc_blk(tfm, dst, src);
  414. }
  415. static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  416. {
  417. aes_dec_blk(tfm, dst, src);
  418. }
  419. static struct crypto_alg aes_alg = {
  420. .cra_name = "aes",
  421. .cra_driver_name = "aes-i586",
  422. .cra_priority = 200,
  423. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  424. .cra_blocksize = AES_BLOCK_SIZE,
  425. .cra_ctxsize = sizeof(struct aes_ctx),
  426. .cra_module = THIS_MODULE,
  427. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  428. .cra_u = {
  429. .cipher = {
  430. .cia_min_keysize = AES_MIN_KEY_SIZE,
  431. .cia_max_keysize = AES_MAX_KEY_SIZE,
  432. .cia_setkey = aes_set_key,
  433. .cia_encrypt = aes_encrypt,
  434. .cia_decrypt = aes_decrypt
  435. }
  436. }
  437. };
  438. static int __init aes_init(void)
  439. {
  440. gen_tabs();
  441. return crypto_register_alg(&aes_alg);
  442. }
  443. static void __exit aes_fini(void)
  444. {
  445. crypto_unregister_alg(&aes_alg);
  446. }
  447. module_init(aes_init);
  448. module_exit(aes_fini);
  449. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, i586 asm optimized");
  450. MODULE_LICENSE("Dual BSD/GPL");
  451. MODULE_AUTHOR("Fruhwirth Clemens, James Morris, Brian Gladman, Adam Richter");
  452. MODULE_ALIAS("aes");