aes_generic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Cryptographic API.
  3. *
  4. * AES Cipher Algorithm.
  5. *
  6. * Based on Brian Gladman's code.
  7. *
  8. * Linux developers:
  9. * Alexander Kjeldaas <astor@fast.no>
  10. * Herbert Valerio Riedel <hvr@hvrlab.org>
  11. * Kyle McMartin <kyle@debian.org>
  12. * Adam J. Richter <adam@yggdrasil.com> (conversion to 2.5 API).
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * ---------------------------------------------------------------------------
  20. * Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
  21. * All rights reserved.
  22. *
  23. * LICENSE TERMS
  24. *
  25. * The free distribution and use of this software in both source and binary
  26. * form is allowed (with or without changes) provided that:
  27. *
  28. * 1. distributions of this source code include the above copyright
  29. * notice, this list of conditions and the following disclaimer;
  30. *
  31. * 2. distributions in binary form include the above copyright
  32. * notice, this list of conditions and the following disclaimer
  33. * in the documentation and/or other associated materials;
  34. *
  35. * 3. the copyright holder's name is not used to endorse products
  36. * built using this software without specific written permission.
  37. *
  38. * ALTERNATIVELY, provided that this notice is retained in full, this product
  39. * may be distributed under the terms of the GNU General Public License (GPL),
  40. * in which case the provisions of the GPL apply INSTEAD OF those given above.
  41. *
  42. * DISCLAIMER
  43. *
  44. * This software is provided 'as is' with no explicit or implied warranties
  45. * in respect of its properties, including, but not limited to, correctness
  46. * and/or fitness for purpose.
  47. * ---------------------------------------------------------------------------
  48. */
  49. /* Some changes from the Gladman version:
  50. s/RIJNDAEL(e_key)/E_KEY/g
  51. s/RIJNDAEL(d_key)/D_KEY/g
  52. */
  53. #include <crypto/aes.h>
  54. #include <linux/module.h>
  55. #include <linux/init.h>
  56. #include <linux/types.h>
  57. #include <linux/errno.h>
  58. #include <linux/crypto.h>
  59. #include <asm/byteorder.h>
  60. /*
  61. * #define byte(x, nr) ((unsigned char)((x) >> (nr*8)))
  62. */
  63. static inline u8
  64. byte(const u32 x, const unsigned n)
  65. {
  66. return x >> (n << 3);
  67. }
  68. struct aes_ctx {
  69. int key_length;
  70. u32 buf[120];
  71. };
  72. #define E_KEY (&ctx->buf[0])
  73. #define D_KEY (&ctx->buf[60])
  74. static u8 pow_tab[256] __initdata;
  75. static u8 log_tab[256] __initdata;
  76. static u8 sbx_tab[256] __initdata;
  77. static u8 isb_tab[256] __initdata;
  78. static u32 rco_tab[10];
  79. static u32 ft_tab[4][256];
  80. static u32 it_tab[4][256];
  81. static u32 fl_tab[4][256];
  82. static u32 il_tab[4][256];
  83. static inline u8 __init
  84. f_mult (u8 a, u8 b)
  85. {
  86. u8 aa = log_tab[a], cc = aa + log_tab[b];
  87. return pow_tab[cc + (cc < aa ? 1 : 0)];
  88. }
  89. #define ff_mult(a,b) (a && b ? f_mult(a, b) : 0)
  90. #define f_rn(bo, bi, n, k) \
  91. bo[n] = ft_tab[0][byte(bi[n],0)] ^ \
  92. ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
  93. ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  94. ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
  95. #define i_rn(bo, bi, n, k) \
  96. bo[n] = it_tab[0][byte(bi[n],0)] ^ \
  97. it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
  98. it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  99. it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
  100. #define ls_box(x) \
  101. ( fl_tab[0][byte(x, 0)] ^ \
  102. fl_tab[1][byte(x, 1)] ^ \
  103. fl_tab[2][byte(x, 2)] ^ \
  104. fl_tab[3][byte(x, 3)] )
  105. #define f_rl(bo, bi, n, k) \
  106. bo[n] = fl_tab[0][byte(bi[n],0)] ^ \
  107. fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
  108. fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  109. fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
  110. #define i_rl(bo, bi, n, k) \
  111. bo[n] = il_tab[0][byte(bi[n],0)] ^ \
  112. il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
  113. il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  114. il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
  115. static void __init
  116. gen_tabs (void)
  117. {
  118. u32 i, t;
  119. u8 p, q;
  120. /* log and power tables for GF(2**8) finite field with
  121. 0x011b as modular polynomial - the simplest primitive
  122. root is 0x03, used here to generate the tables */
  123. for (i = 0, p = 1; i < 256; ++i) {
  124. pow_tab[i] = (u8) p;
  125. log_tab[p] = (u8) i;
  126. p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0);
  127. }
  128. log_tab[1] = 0;
  129. for (i = 0, p = 1; i < 10; ++i) {
  130. rco_tab[i] = p;
  131. p = (p << 1) ^ (p & 0x80 ? 0x01b : 0);
  132. }
  133. for (i = 0; i < 256; ++i) {
  134. p = (i ? pow_tab[255 - log_tab[i]] : 0);
  135. q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2));
  136. p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2));
  137. sbx_tab[i] = p;
  138. isb_tab[p] = (u8) i;
  139. }
  140. for (i = 0; i < 256; ++i) {
  141. p = sbx_tab[i];
  142. t = p;
  143. fl_tab[0][i] = t;
  144. fl_tab[1][i] = rol32(t, 8);
  145. fl_tab[2][i] = rol32(t, 16);
  146. fl_tab[3][i] = rol32(t, 24);
  147. t = ((u32) ff_mult (2, p)) |
  148. ((u32) p << 8) |
  149. ((u32) p << 16) | ((u32) ff_mult (3, p) << 24);
  150. ft_tab[0][i] = t;
  151. ft_tab[1][i] = rol32(t, 8);
  152. ft_tab[2][i] = rol32(t, 16);
  153. ft_tab[3][i] = rol32(t, 24);
  154. p = isb_tab[i];
  155. t = p;
  156. il_tab[0][i] = t;
  157. il_tab[1][i] = rol32(t, 8);
  158. il_tab[2][i] = rol32(t, 16);
  159. il_tab[3][i] = rol32(t, 24);
  160. t = ((u32) ff_mult (14, p)) |
  161. ((u32) ff_mult (9, p) << 8) |
  162. ((u32) ff_mult (13, p) << 16) |
  163. ((u32) ff_mult (11, p) << 24);
  164. it_tab[0][i] = t;
  165. it_tab[1][i] = rol32(t, 8);
  166. it_tab[2][i] = rol32(t, 16);
  167. it_tab[3][i] = rol32(t, 24);
  168. }
  169. }
  170. #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
  171. #define imix_col(y,x) \
  172. u = star_x(x); \
  173. v = star_x(u); \
  174. w = star_x(v); \
  175. t = w ^ (x); \
  176. (y) = u ^ v ^ w; \
  177. (y) ^= ror32(u ^ t, 8) ^ \
  178. ror32(v ^ t, 16) ^ \
  179. ror32(t,24)
  180. /* initialise the key schedule from the user supplied key */
  181. #define loop4(i) \
  182. { t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
  183. t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \
  184. t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \
  185. t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \
  186. t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \
  187. }
  188. #define loop6(i) \
  189. { t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
  190. t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \
  191. t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \
  192. t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \
  193. t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \
  194. t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \
  195. t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \
  196. }
  197. #define loop8(i) \
  198. { t = ror32(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \
  199. t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \
  200. t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \
  201. t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \
  202. t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \
  203. t = E_KEY[8 * i + 4] ^ ls_box(t); \
  204. E_KEY[8 * i + 12] = t; \
  205. t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \
  206. t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \
  207. t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \
  208. }
  209. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  210. unsigned int key_len)
  211. {
  212. struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
  213. const __le32 *key = (const __le32 *)in_key;
  214. u32 *flags = &tfm->crt_flags;
  215. u32 i, t, u, v, w;
  216. if (key_len % 8) {
  217. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  218. return -EINVAL;
  219. }
  220. ctx->key_length = key_len;
  221. E_KEY[0] = le32_to_cpu(key[0]);
  222. E_KEY[1] = le32_to_cpu(key[1]);
  223. E_KEY[2] = le32_to_cpu(key[2]);
  224. E_KEY[3] = le32_to_cpu(key[3]);
  225. switch (key_len) {
  226. case 16:
  227. t = E_KEY[3];
  228. for (i = 0; i < 10; ++i)
  229. loop4 (i);
  230. break;
  231. case 24:
  232. E_KEY[4] = le32_to_cpu(key[4]);
  233. t = E_KEY[5] = le32_to_cpu(key[5]);
  234. for (i = 0; i < 8; ++i)
  235. loop6 (i);
  236. break;
  237. case 32:
  238. E_KEY[4] = le32_to_cpu(key[4]);
  239. E_KEY[5] = le32_to_cpu(key[5]);
  240. E_KEY[6] = le32_to_cpu(key[6]);
  241. t = E_KEY[7] = le32_to_cpu(key[7]);
  242. for (i = 0; i < 7; ++i)
  243. loop8 (i);
  244. break;
  245. }
  246. D_KEY[0] = E_KEY[0];
  247. D_KEY[1] = E_KEY[1];
  248. D_KEY[2] = E_KEY[2];
  249. D_KEY[3] = E_KEY[3];
  250. for (i = 4; i < key_len + 24; ++i) {
  251. imix_col (D_KEY[i], E_KEY[i]);
  252. }
  253. return 0;
  254. }
  255. /* encrypt a block of text */
  256. #define f_nround(bo, bi, k) \
  257. f_rn(bo, bi, 0, k); \
  258. f_rn(bo, bi, 1, k); \
  259. f_rn(bo, bi, 2, k); \
  260. f_rn(bo, bi, 3, k); \
  261. k += 4
  262. #define f_lround(bo, bi, k) \
  263. f_rl(bo, bi, 0, k); \
  264. f_rl(bo, bi, 1, k); \
  265. f_rl(bo, bi, 2, k); \
  266. f_rl(bo, bi, 3, k)
  267. static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  268. {
  269. const struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
  270. const __le32 *src = (const __le32 *)in;
  271. __le32 *dst = (__le32 *)out;
  272. u32 b0[4], b1[4];
  273. const u32 *kp = E_KEY + 4;
  274. b0[0] = le32_to_cpu(src[0]) ^ E_KEY[0];
  275. b0[1] = le32_to_cpu(src[1]) ^ E_KEY[1];
  276. b0[2] = le32_to_cpu(src[2]) ^ E_KEY[2];
  277. b0[3] = le32_to_cpu(src[3]) ^ E_KEY[3];
  278. if (ctx->key_length > 24) {
  279. f_nround (b1, b0, kp);
  280. f_nround (b0, b1, kp);
  281. }
  282. if (ctx->key_length > 16) {
  283. f_nround (b1, b0, kp);
  284. f_nround (b0, b1, kp);
  285. }
  286. f_nround (b1, b0, kp);
  287. f_nround (b0, b1, kp);
  288. f_nround (b1, b0, kp);
  289. f_nround (b0, b1, kp);
  290. f_nround (b1, b0, kp);
  291. f_nround (b0, b1, kp);
  292. f_nround (b1, b0, kp);
  293. f_nround (b0, b1, kp);
  294. f_nround (b1, b0, kp);
  295. f_lround (b0, b1, kp);
  296. dst[0] = cpu_to_le32(b0[0]);
  297. dst[1] = cpu_to_le32(b0[1]);
  298. dst[2] = cpu_to_le32(b0[2]);
  299. dst[3] = cpu_to_le32(b0[3]);
  300. }
  301. /* decrypt a block of text */
  302. #define i_nround(bo, bi, k) \
  303. i_rn(bo, bi, 0, k); \
  304. i_rn(bo, bi, 1, k); \
  305. i_rn(bo, bi, 2, k); \
  306. i_rn(bo, bi, 3, k); \
  307. k -= 4
  308. #define i_lround(bo, bi, k) \
  309. i_rl(bo, bi, 0, k); \
  310. i_rl(bo, bi, 1, k); \
  311. i_rl(bo, bi, 2, k); \
  312. i_rl(bo, bi, 3, k)
  313. static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  314. {
  315. const struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
  316. const __le32 *src = (const __le32 *)in;
  317. __le32 *dst = (__le32 *)out;
  318. u32 b0[4], b1[4];
  319. const int key_len = ctx->key_length;
  320. const u32 *kp = D_KEY + key_len + 20;
  321. b0[0] = le32_to_cpu(src[0]) ^ E_KEY[key_len + 24];
  322. b0[1] = le32_to_cpu(src[1]) ^ E_KEY[key_len + 25];
  323. b0[2] = le32_to_cpu(src[2]) ^ E_KEY[key_len + 26];
  324. b0[3] = le32_to_cpu(src[3]) ^ E_KEY[key_len + 27];
  325. if (key_len > 24) {
  326. i_nround (b1, b0, kp);
  327. i_nround (b0, b1, kp);
  328. }
  329. if (key_len > 16) {
  330. i_nround (b1, b0, kp);
  331. i_nround (b0, b1, kp);
  332. }
  333. i_nround (b1, b0, kp);
  334. i_nround (b0, b1, kp);
  335. i_nround (b1, b0, kp);
  336. i_nround (b0, b1, kp);
  337. i_nround (b1, b0, kp);
  338. i_nround (b0, b1, kp);
  339. i_nround (b1, b0, kp);
  340. i_nround (b0, b1, kp);
  341. i_nround (b1, b0, kp);
  342. i_lround (b0, b1, kp);
  343. dst[0] = cpu_to_le32(b0[0]);
  344. dst[1] = cpu_to_le32(b0[1]);
  345. dst[2] = cpu_to_le32(b0[2]);
  346. dst[3] = cpu_to_le32(b0[3]);
  347. }
  348. static struct crypto_alg aes_alg = {
  349. .cra_name = "aes",
  350. .cra_driver_name = "aes-generic",
  351. .cra_priority = 100,
  352. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  353. .cra_blocksize = AES_BLOCK_SIZE,
  354. .cra_ctxsize = sizeof(struct aes_ctx),
  355. .cra_alignmask = 3,
  356. .cra_module = THIS_MODULE,
  357. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  358. .cra_u = {
  359. .cipher = {
  360. .cia_min_keysize = AES_MIN_KEY_SIZE,
  361. .cia_max_keysize = AES_MAX_KEY_SIZE,
  362. .cia_setkey = aes_set_key,
  363. .cia_encrypt = aes_encrypt,
  364. .cia_decrypt = aes_decrypt
  365. }
  366. }
  367. };
  368. static int __init aes_init(void)
  369. {
  370. gen_tabs();
  371. return crypto_register_alg(&aes_alg);
  372. }
  373. static void __exit aes_fini(void)
  374. {
  375. crypto_unregister_alg(&aes_alg);
  376. }
  377. module_init(aes_init);
  378. module_exit(aes_fini);
  379. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
  380. MODULE_LICENSE("Dual BSD/GPL");
  381. MODULE_ALIAS("aes");