aes.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 <linux/module.h>
  54. #include <linux/init.h>
  55. #include <linux/types.h>
  56. #include <linux/errno.h>
  57. #include <linux/crypto.h>
  58. #include <asm/byteorder.h>
  59. #define AES_MIN_KEY_SIZE 16
  60. #define AES_MAX_KEY_SIZE 32
  61. #define AES_BLOCK_SIZE 16
  62. /*
  63. * #define byte(x, nr) ((unsigned char)((x) >> (nr*8)))
  64. */
  65. inline static u8
  66. byte(const u32 x, const unsigned n)
  67. {
  68. return x >> (n << 3);
  69. }
  70. #define u32_in(x) le32_to_cpu(*(const u32 *)(x))
  71. #define u32_out(to, from) (*(u32 *)(to) = cpu_to_le32(from))
  72. struct aes_ctx {
  73. int key_length;
  74. u32 E[60];
  75. u32 D[60];
  76. };
  77. #define E_KEY ctx->E
  78. #define D_KEY ctx->D
  79. static u8 pow_tab[256] __initdata;
  80. static u8 log_tab[256] __initdata;
  81. static u8 sbx_tab[256] __initdata;
  82. static u8 isb_tab[256] __initdata;
  83. static u32 rco_tab[10];
  84. static u32 ft_tab[4][256];
  85. static u32 it_tab[4][256];
  86. static u32 fl_tab[4][256];
  87. static u32 il_tab[4][256];
  88. static inline u8 __init
  89. f_mult (u8 a, u8 b)
  90. {
  91. u8 aa = log_tab[a], cc = aa + log_tab[b];
  92. return pow_tab[cc + (cc < aa ? 1 : 0)];
  93. }
  94. #define ff_mult(a,b) (a && b ? f_mult(a, b) : 0)
  95. #define f_rn(bo, bi, n, k) \
  96. bo[n] = ft_tab[0][byte(bi[n],0)] ^ \
  97. ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
  98. ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  99. ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
  100. #define i_rn(bo, bi, n, k) \
  101. bo[n] = it_tab[0][byte(bi[n],0)] ^ \
  102. it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
  103. it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  104. it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
  105. #define ls_box(x) \
  106. ( fl_tab[0][byte(x, 0)] ^ \
  107. fl_tab[1][byte(x, 1)] ^ \
  108. fl_tab[2][byte(x, 2)] ^ \
  109. fl_tab[3][byte(x, 3)] )
  110. #define f_rl(bo, bi, n, k) \
  111. bo[n] = fl_tab[0][byte(bi[n],0)] ^ \
  112. fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
  113. fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  114. fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
  115. #define i_rl(bo, bi, n, k) \
  116. bo[n] = il_tab[0][byte(bi[n],0)] ^ \
  117. il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
  118. il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  119. il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
  120. static void __init
  121. gen_tabs (void)
  122. {
  123. u32 i, t;
  124. u8 p, q;
  125. /* log and power tables for GF(2**8) finite field with
  126. 0x011b as modular polynomial - the simplest primitive
  127. root is 0x03, used here to generate the tables */
  128. for (i = 0, p = 1; i < 256; ++i) {
  129. pow_tab[i] = (u8) p;
  130. log_tab[p] = (u8) i;
  131. p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0);
  132. }
  133. log_tab[1] = 0;
  134. for (i = 0, p = 1; i < 10; ++i) {
  135. rco_tab[i] = p;
  136. p = (p << 1) ^ (p & 0x80 ? 0x01b : 0);
  137. }
  138. for (i = 0; i < 256; ++i) {
  139. p = (i ? pow_tab[255 - log_tab[i]] : 0);
  140. q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2));
  141. p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2));
  142. sbx_tab[i] = p;
  143. isb_tab[p] = (u8) i;
  144. }
  145. for (i = 0; i < 256; ++i) {
  146. p = sbx_tab[i];
  147. t = p;
  148. fl_tab[0][i] = t;
  149. fl_tab[1][i] = rol32(t, 8);
  150. fl_tab[2][i] = rol32(t, 16);
  151. fl_tab[3][i] = rol32(t, 24);
  152. t = ((u32) ff_mult (2, p)) |
  153. ((u32) p << 8) |
  154. ((u32) p << 16) | ((u32) ff_mult (3, p) << 24);
  155. ft_tab[0][i] = t;
  156. ft_tab[1][i] = rol32(t, 8);
  157. ft_tab[2][i] = rol32(t, 16);
  158. ft_tab[3][i] = rol32(t, 24);
  159. p = isb_tab[i];
  160. t = p;
  161. il_tab[0][i] = t;
  162. il_tab[1][i] = rol32(t, 8);
  163. il_tab[2][i] = rol32(t, 16);
  164. il_tab[3][i] = rol32(t, 24);
  165. t = ((u32) ff_mult (14, p)) |
  166. ((u32) ff_mult (9, p) << 8) |
  167. ((u32) ff_mult (13, p) << 16) |
  168. ((u32) ff_mult (11, p) << 24);
  169. it_tab[0][i] = t;
  170. it_tab[1][i] = rol32(t, 8);
  171. it_tab[2][i] = rol32(t, 16);
  172. it_tab[3][i] = rol32(t, 24);
  173. }
  174. }
  175. #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
  176. #define imix_col(y,x) \
  177. u = star_x(x); \
  178. v = star_x(u); \
  179. w = star_x(v); \
  180. t = w ^ (x); \
  181. (y) = u ^ v ^ w; \
  182. (y) ^= ror32(u ^ t, 8) ^ \
  183. ror32(v ^ t, 16) ^ \
  184. ror32(t,24)
  185. /* initialise the key schedule from the user supplied key */
  186. #define loop4(i) \
  187. { t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
  188. t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \
  189. t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \
  190. t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \
  191. t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \
  192. }
  193. #define loop6(i) \
  194. { t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
  195. t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \
  196. t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \
  197. t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \
  198. t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \
  199. t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \
  200. t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \
  201. }
  202. #define loop8(i) \
  203. { t = ror32(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \
  204. t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \
  205. t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \
  206. t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \
  207. t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \
  208. t = E_KEY[8 * i + 4] ^ ls_box(t); \
  209. E_KEY[8 * i + 12] = t; \
  210. t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \
  211. t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \
  212. t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \
  213. }
  214. static int
  215. aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
  216. {
  217. struct aes_ctx *ctx = ctx_arg;
  218. u32 i, t, u, v, w;
  219. if (key_len != 16 && key_len != 24 && key_len != 32) {
  220. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  221. return -EINVAL;
  222. }
  223. ctx->key_length = key_len;
  224. E_KEY[0] = u32_in (in_key);
  225. E_KEY[1] = u32_in (in_key + 4);
  226. E_KEY[2] = u32_in (in_key + 8);
  227. E_KEY[3] = u32_in (in_key + 12);
  228. switch (key_len) {
  229. case 16:
  230. t = E_KEY[3];
  231. for (i = 0; i < 10; ++i)
  232. loop4 (i);
  233. break;
  234. case 24:
  235. E_KEY[4] = u32_in (in_key + 16);
  236. t = E_KEY[5] = u32_in (in_key + 20);
  237. for (i = 0; i < 8; ++i)
  238. loop6 (i);
  239. break;
  240. case 32:
  241. E_KEY[4] = u32_in (in_key + 16);
  242. E_KEY[5] = u32_in (in_key + 20);
  243. E_KEY[6] = u32_in (in_key + 24);
  244. t = E_KEY[7] = u32_in (in_key + 28);
  245. for (i = 0; i < 7; ++i)
  246. loop8 (i);
  247. break;
  248. }
  249. D_KEY[0] = E_KEY[0];
  250. D_KEY[1] = E_KEY[1];
  251. D_KEY[2] = E_KEY[2];
  252. D_KEY[3] = E_KEY[3];
  253. for (i = 4; i < key_len + 24; ++i) {
  254. imix_col (D_KEY[i], E_KEY[i]);
  255. }
  256. return 0;
  257. }
  258. /* encrypt a block of text */
  259. #define f_nround(bo, bi, k) \
  260. f_rn(bo, bi, 0, k); \
  261. f_rn(bo, bi, 1, k); \
  262. f_rn(bo, bi, 2, k); \
  263. f_rn(bo, bi, 3, k); \
  264. k += 4
  265. #define f_lround(bo, bi, k) \
  266. f_rl(bo, bi, 0, k); \
  267. f_rl(bo, bi, 1, k); \
  268. f_rl(bo, bi, 2, k); \
  269. f_rl(bo, bi, 3, k)
  270. static void aes_encrypt(void *ctx_arg, u8 *out, const u8 *in)
  271. {
  272. const struct aes_ctx *ctx = ctx_arg;
  273. u32 b0[4], b1[4];
  274. const u32 *kp = E_KEY + 4;
  275. b0[0] = u32_in (in) ^ E_KEY[0];
  276. b0[1] = u32_in (in + 4) ^ E_KEY[1];
  277. b0[2] = u32_in (in + 8) ^ E_KEY[2];
  278. b0[3] = u32_in (in + 12) ^ E_KEY[3];
  279. if (ctx->key_length > 24) {
  280. f_nround (b1, b0, kp);
  281. f_nround (b0, b1, kp);
  282. }
  283. if (ctx->key_length > 16) {
  284. f_nround (b1, b0, kp);
  285. f_nround (b0, b1, kp);
  286. }
  287. f_nround (b1, b0, kp);
  288. f_nround (b0, b1, kp);
  289. f_nround (b1, b0, kp);
  290. f_nround (b0, b1, kp);
  291. f_nround (b1, b0, kp);
  292. f_nround (b0, b1, kp);
  293. f_nround (b1, b0, kp);
  294. f_nround (b0, b1, kp);
  295. f_nround (b1, b0, kp);
  296. f_lround (b0, b1, kp);
  297. u32_out (out, b0[0]);
  298. u32_out (out + 4, b0[1]);
  299. u32_out (out + 8, b0[2]);
  300. u32_out (out + 12, b0[3]);
  301. }
  302. /* decrypt a block of text */
  303. #define i_nround(bo, bi, k) \
  304. i_rn(bo, bi, 0, k); \
  305. i_rn(bo, bi, 1, k); \
  306. i_rn(bo, bi, 2, k); \
  307. i_rn(bo, bi, 3, k); \
  308. k -= 4
  309. #define i_lround(bo, bi, k) \
  310. i_rl(bo, bi, 0, k); \
  311. i_rl(bo, bi, 1, k); \
  312. i_rl(bo, bi, 2, k); \
  313. i_rl(bo, bi, 3, k)
  314. static void aes_decrypt(void *ctx_arg, u8 *out, const u8 *in)
  315. {
  316. const struct aes_ctx *ctx = ctx_arg;
  317. u32 b0[4], b1[4];
  318. const int key_len = ctx->key_length;
  319. const u32 *kp = D_KEY + key_len + 20;
  320. b0[0] = u32_in (in) ^ E_KEY[key_len + 24];
  321. b0[1] = u32_in (in + 4) ^ E_KEY[key_len + 25];
  322. b0[2] = u32_in (in + 8) ^ E_KEY[key_len + 26];
  323. b0[3] = u32_in (in + 12) ^ E_KEY[key_len + 27];
  324. if (key_len > 24) {
  325. i_nround (b1, b0, kp);
  326. i_nround (b0, b1, kp);
  327. }
  328. if (key_len > 16) {
  329. i_nround (b1, b0, kp);
  330. i_nround (b0, b1, kp);
  331. }
  332. i_nround (b1, b0, kp);
  333. i_nround (b0, b1, kp);
  334. i_nround (b1, b0, kp);
  335. i_nround (b0, b1, kp);
  336. i_nround (b1, b0, kp);
  337. i_nround (b0, b1, kp);
  338. i_nround (b1, b0, kp);
  339. i_nround (b0, b1, kp);
  340. i_nround (b1, b0, kp);
  341. i_lround (b0, b1, kp);
  342. u32_out (out, b0[0]);
  343. u32_out (out + 4, b0[1]);
  344. u32_out (out + 8, b0[2]);
  345. u32_out (out + 12, b0[3]);
  346. }
  347. static struct crypto_alg aes_alg = {
  348. .cra_name = "aes",
  349. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  350. .cra_blocksize = AES_BLOCK_SIZE,
  351. .cra_ctxsize = sizeof(struct aes_ctx),
  352. .cra_module = THIS_MODULE,
  353. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  354. .cra_u = {
  355. .cipher = {
  356. .cia_min_keysize = AES_MIN_KEY_SIZE,
  357. .cia_max_keysize = AES_MAX_KEY_SIZE,
  358. .cia_setkey = aes_set_key,
  359. .cia_encrypt = aes_encrypt,
  360. .cia_decrypt = aes_decrypt
  361. }
  362. }
  363. };
  364. static int __init aes_init(void)
  365. {
  366. gen_tabs();
  367. return crypto_register_alg(&aes_alg);
  368. }
  369. static void __exit aes_fini(void)
  370. {
  371. crypto_unregister_alg(&aes_alg);
  372. }
  373. module_init(aes_init);
  374. module_exit(aes_fini);
  375. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
  376. MODULE_LICENSE("Dual BSD/GPL");