aes_generic.c 12 KB

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