aes_generic.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. /**
  201. * crypto_aes_expand_key - Expands the AES key as described in FIPS-197
  202. * @ctx: The location where the computed key will be stored.
  203. * @in_key: The supplied key.
  204. * @key_len: The length of the supplied key.
  205. *
  206. * Returns 0 on success. The function fails only if an invalid key size (or
  207. * pointer) is supplied.
  208. * The expanded key size is 240 bytes (max of 14 rounds with a unique 16 bytes
  209. * key schedule plus a 16 bytes key which is used before the first round).
  210. * The decryption key is prepared for the "Equivalent Inverse Cipher" as
  211. * described in FIPS-197. The first slot (16 bytes) of each key (enc or dec) is
  212. * for the initial combination, the second slot for the first round and so on.
  213. */
  214. int crypto_aes_expand_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
  215. unsigned int key_len)
  216. {
  217. const __le32 *key = (const __le32 *)in_key;
  218. u32 i, t, u, v, w, j;
  219. if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
  220. key_len != AES_KEYSIZE_256)
  221. return -EINVAL;
  222. ctx->key_length = key_len;
  223. ctx->key_dec[key_len + 24] = ctx->key_enc[0] = le32_to_cpu(key[0]);
  224. ctx->key_dec[key_len + 25] = ctx->key_enc[1] = le32_to_cpu(key[1]);
  225. ctx->key_dec[key_len + 26] = ctx->key_enc[2] = le32_to_cpu(key[2]);
  226. ctx->key_dec[key_len + 27] = ctx->key_enc[3] = le32_to_cpu(key[3]);
  227. switch (key_len) {
  228. case AES_KEYSIZE_128:
  229. t = ctx->key_enc[3];
  230. for (i = 0; i < 10; ++i)
  231. loop4(i);
  232. break;
  233. case AES_KEYSIZE_192:
  234. ctx->key_enc[4] = le32_to_cpu(key[4]);
  235. t = ctx->key_enc[5] = le32_to_cpu(key[5]);
  236. for (i = 0; i < 8; ++i)
  237. loop6(i);
  238. break;
  239. case AES_KEYSIZE_256:
  240. ctx->key_enc[4] = le32_to_cpu(key[4]);
  241. ctx->key_enc[5] = le32_to_cpu(key[5]);
  242. ctx->key_enc[6] = le32_to_cpu(key[6]);
  243. t = ctx->key_enc[7] = le32_to_cpu(key[7]);
  244. for (i = 0; i < 7; ++i)
  245. loop8(i);
  246. break;
  247. }
  248. ctx->key_dec[0] = ctx->key_enc[key_len + 24];
  249. ctx->key_dec[1] = ctx->key_enc[key_len + 25];
  250. ctx->key_dec[2] = ctx->key_enc[key_len + 26];
  251. ctx->key_dec[3] = ctx->key_enc[key_len + 27];
  252. for (i = 4; i < key_len + 24; ++i) {
  253. j = key_len + 24 - (i & ~3) + (i & 3);
  254. imix_col(ctx->key_dec[j], ctx->key_enc[i]);
  255. }
  256. return 0;
  257. }
  258. EXPORT_SYMBOL_GPL(crypto_aes_expand_key);
  259. /**
  260. * crypto_aes_set_key - Set the AES key.
  261. * @tfm: The %crypto_tfm that is used in the context.
  262. * @in_key: The input key.
  263. * @key_len: The size of the key.
  264. *
  265. * Returns 0 on success, on failure the %CRYPTO_TFM_RES_BAD_KEY_LEN flag in tfm
  266. * is set. The function uses crypto_aes_expand_key() to expand the key.
  267. * &crypto_aes_ctx _must_ be the private data embedded in @tfm which is
  268. * retrieved with crypto_tfm_ctx().
  269. */
  270. int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  271. unsigned int key_len)
  272. {
  273. struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  274. u32 *flags = &tfm->crt_flags;
  275. int ret;
  276. ret = crypto_aes_expand_key(ctx, in_key, key_len);
  277. if (!ret)
  278. return 0;
  279. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  280. return -EINVAL;
  281. }
  282. EXPORT_SYMBOL_GPL(crypto_aes_set_key);
  283. /* encrypt a block of text */
  284. #define f_rn(bo, bi, n, k) do { \
  285. bo[n] = crypto_ft_tab[0][byte(bi[n], 0)] ^ \
  286. crypto_ft_tab[1][byte(bi[(n + 1) & 3], 1)] ^ \
  287. crypto_ft_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \
  288. crypto_ft_tab[3][byte(bi[(n + 3) & 3], 3)] ^ *(k + n); \
  289. } while (0)
  290. #define f_nround(bo, bi, k) do {\
  291. f_rn(bo, bi, 0, k); \
  292. f_rn(bo, bi, 1, k); \
  293. f_rn(bo, bi, 2, k); \
  294. f_rn(bo, bi, 3, k); \
  295. k += 4; \
  296. } while (0)
  297. #define f_rl(bo, bi, n, k) do { \
  298. bo[n] = crypto_fl_tab[0][byte(bi[n], 0)] ^ \
  299. crypto_fl_tab[1][byte(bi[(n + 1) & 3], 1)] ^ \
  300. crypto_fl_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \
  301. crypto_fl_tab[3][byte(bi[(n + 3) & 3], 3)] ^ *(k + n); \
  302. } while (0)
  303. #define f_lround(bo, bi, k) do {\
  304. f_rl(bo, bi, 0, k); \
  305. f_rl(bo, bi, 1, k); \
  306. f_rl(bo, bi, 2, k); \
  307. f_rl(bo, bi, 3, k); \
  308. } while (0)
  309. static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  310. {
  311. const struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  312. const __le32 *src = (const __le32 *)in;
  313. __le32 *dst = (__le32 *)out;
  314. u32 b0[4], b1[4];
  315. const u32 *kp = ctx->key_enc + 4;
  316. const int key_len = ctx->key_length;
  317. b0[0] = le32_to_cpu(src[0]) ^ ctx->key_enc[0];
  318. b0[1] = le32_to_cpu(src[1]) ^ ctx->key_enc[1];
  319. b0[2] = le32_to_cpu(src[2]) ^ ctx->key_enc[2];
  320. b0[3] = le32_to_cpu(src[3]) ^ ctx->key_enc[3];
  321. if (key_len > 24) {
  322. f_nround(b1, b0, kp);
  323. f_nround(b0, b1, kp);
  324. }
  325. if (key_len > 16) {
  326. f_nround(b1, b0, kp);
  327. f_nround(b0, b1, kp);
  328. }
  329. f_nround(b1, b0, kp);
  330. f_nround(b0, b1, kp);
  331. f_nround(b1, b0, kp);
  332. f_nround(b0, b1, kp);
  333. f_nround(b1, b0, kp);
  334. f_nround(b0, b1, kp);
  335. f_nround(b1, b0, kp);
  336. f_nround(b0, b1, kp);
  337. f_nround(b1, b0, kp);
  338. f_lround(b0, b1, kp);
  339. dst[0] = cpu_to_le32(b0[0]);
  340. dst[1] = cpu_to_le32(b0[1]);
  341. dst[2] = cpu_to_le32(b0[2]);
  342. dst[3] = cpu_to_le32(b0[3]);
  343. }
  344. /* decrypt a block of text */
  345. #define i_rn(bo, bi, n, k) do { \
  346. bo[n] = crypto_it_tab[0][byte(bi[n], 0)] ^ \
  347. crypto_it_tab[1][byte(bi[(n + 3) & 3], 1)] ^ \
  348. crypto_it_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \
  349. crypto_it_tab[3][byte(bi[(n + 1) & 3], 3)] ^ *(k + n); \
  350. } while (0)
  351. #define i_nround(bo, bi, k) do {\
  352. i_rn(bo, bi, 0, k); \
  353. i_rn(bo, bi, 1, k); \
  354. i_rn(bo, bi, 2, k); \
  355. i_rn(bo, bi, 3, k); \
  356. k += 4; \
  357. } while (0)
  358. #define i_rl(bo, bi, n, k) do { \
  359. bo[n] = crypto_il_tab[0][byte(bi[n], 0)] ^ \
  360. crypto_il_tab[1][byte(bi[(n + 3) & 3], 1)] ^ \
  361. crypto_il_tab[2][byte(bi[(n + 2) & 3], 2)] ^ \
  362. crypto_il_tab[3][byte(bi[(n + 1) & 3], 3)] ^ *(k + n); \
  363. } while (0)
  364. #define i_lround(bo, bi, k) do {\
  365. i_rl(bo, bi, 0, k); \
  366. i_rl(bo, bi, 1, k); \
  367. i_rl(bo, bi, 2, k); \
  368. i_rl(bo, bi, 3, k); \
  369. } while (0)
  370. static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  371. {
  372. const struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  373. const __le32 *src = (const __le32 *)in;
  374. __le32 *dst = (__le32 *)out;
  375. u32 b0[4], b1[4];
  376. const int key_len = ctx->key_length;
  377. const u32 *kp = ctx->key_dec + 4;
  378. b0[0] = le32_to_cpu(src[0]) ^ ctx->key_dec[0];
  379. b0[1] = le32_to_cpu(src[1]) ^ ctx->key_dec[1];
  380. b0[2] = le32_to_cpu(src[2]) ^ ctx->key_dec[2];
  381. b0[3] = le32_to_cpu(src[3]) ^ ctx->key_dec[3];
  382. if (key_len > 24) {
  383. i_nround(b1, b0, kp);
  384. i_nround(b0, b1, kp);
  385. }
  386. if (key_len > 16) {
  387. i_nround(b1, b0, kp);
  388. i_nround(b0, b1, kp);
  389. }
  390. i_nround(b1, b0, kp);
  391. i_nround(b0, b1, kp);
  392. i_nround(b1, b0, kp);
  393. i_nround(b0, b1, kp);
  394. i_nround(b1, b0, kp);
  395. i_nround(b0, b1, kp);
  396. i_nround(b1, b0, kp);
  397. i_nround(b0, b1, kp);
  398. i_nround(b1, b0, kp);
  399. i_lround(b0, b1, kp);
  400. dst[0] = cpu_to_le32(b0[0]);
  401. dst[1] = cpu_to_le32(b0[1]);
  402. dst[2] = cpu_to_le32(b0[2]);
  403. dst[3] = cpu_to_le32(b0[3]);
  404. }
  405. static struct crypto_alg aes_alg = {
  406. .cra_name = "aes",
  407. .cra_driver_name = "aes-generic",
  408. .cra_priority = 100,
  409. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  410. .cra_blocksize = AES_BLOCK_SIZE,
  411. .cra_ctxsize = sizeof(struct crypto_aes_ctx),
  412. .cra_alignmask = 3,
  413. .cra_module = THIS_MODULE,
  414. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  415. .cra_u = {
  416. .cipher = {
  417. .cia_min_keysize = AES_MIN_KEY_SIZE,
  418. .cia_max_keysize = AES_MAX_KEY_SIZE,
  419. .cia_setkey = crypto_aes_set_key,
  420. .cia_encrypt = aes_encrypt,
  421. .cia_decrypt = aes_decrypt
  422. }
  423. }
  424. };
  425. static int __init aes_init(void)
  426. {
  427. gen_tabs();
  428. return crypto_register_alg(&aes_alg);
  429. }
  430. static void __exit aes_fini(void)
  431. {
  432. crypto_unregister_alg(&aes_alg);
  433. }
  434. module_init(aes_init);
  435. module_exit(aes_fini);
  436. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
  437. MODULE_LICENSE("Dual BSD/GPL");
  438. MODULE_ALIAS("aes");