aes_64.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. * Andreas Steinmetz <ast@domdv.de> (adapted to x86_64 assembler)
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * ---------------------------------------------------------------------------
  21. * Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
  22. * All rights reserved.
  23. *
  24. * LICENSE TERMS
  25. *
  26. * The free distribution and use of this software in both source and binary
  27. * form is allowed (with or without changes) provided that:
  28. *
  29. * 1. distributions of this source code include the above copyright
  30. * notice, this list of conditions and the following disclaimer;
  31. *
  32. * 2. distributions in binary form include the above copyright
  33. * notice, this list of conditions and the following disclaimer
  34. * in the documentation and/or other associated materials;
  35. *
  36. * 3. the copyright holder's name is not used to endorse products
  37. * built using this software without specific written permission.
  38. *
  39. * ALTERNATIVELY, provided that this notice is retained in full, this product
  40. * may be distributed under the terms of the GNU General Public License (GPL),
  41. * in which case the provisions of the GPL apply INSTEAD OF those given above.
  42. *
  43. * DISCLAIMER
  44. *
  45. * This software is provided 'as is' with no explicit or implied warranties
  46. * in respect of its properties, including, but not limited to, correctness
  47. * and/or fitness for purpose.
  48. * ---------------------------------------------------------------------------
  49. */
  50. /* Some changes from the Gladman version:
  51. s/RIJNDAEL(e_key)/E_KEY/g
  52. s/RIJNDAEL(d_key)/D_KEY/g
  53. */
  54. #include <asm/byteorder.h>
  55. #include <linux/bitops.h>
  56. #include <linux/crypto.h>
  57. #include <linux/errno.h>
  58. #include <linux/init.h>
  59. #include <linux/module.h>
  60. #include <linux/types.h>
  61. #define AES_MIN_KEY_SIZE 16
  62. #define AES_MAX_KEY_SIZE 32
  63. #define AES_BLOCK_SIZE 16
  64. /*
  65. * #define byte(x, nr) ((unsigned char)((x) >> (nr*8)))
  66. */
  67. static inline u8 byte(const u32 x, const unsigned n)
  68. {
  69. return x >> (n << 3);
  70. }
  71. struct aes_ctx
  72. {
  73. u32 key_length;
  74. u32 buf[120];
  75. };
  76. #define E_KEY (&ctx->buf[0])
  77. #define D_KEY (&ctx->buf[60])
  78. static u8 pow_tab[256] __initdata;
  79. static u8 log_tab[256] __initdata;
  80. static u8 sbx_tab[256] __initdata;
  81. static u8 isb_tab[256] __initdata;
  82. static u32 rco_tab[10];
  83. u32 aes_ft_tab[4][256];
  84. u32 aes_it_tab[4][256];
  85. u32 aes_fl_tab[4][256];
  86. u32 aes_il_tab[4][256];
  87. static inline u8 f_mult(u8 a, u8 b)
  88. {
  89. u8 aa = log_tab[a], cc = aa + log_tab[b];
  90. return pow_tab[cc + (cc < aa ? 1 : 0)];
  91. }
  92. #define ff_mult(a, b) (a && b ? f_mult(a, b) : 0)
  93. #define ls_box(x) \
  94. (aes_fl_tab[0][byte(x, 0)] ^ \
  95. aes_fl_tab[1][byte(x, 1)] ^ \
  96. aes_fl_tab[2][byte(x, 2)] ^ \
  97. aes_fl_tab[3][byte(x, 3)])
  98. static void __init gen_tabs(void)
  99. {
  100. u32 i, t;
  101. u8 p, q;
  102. /* log and power tables for GF(2**8) finite field with
  103. 0x011b as modular polynomial - the simplest primitive
  104. root is 0x03, used here to generate the tables */
  105. for (i = 0, p = 1; i < 256; ++i) {
  106. pow_tab[i] = (u8)p;
  107. log_tab[p] = (u8)i;
  108. p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0);
  109. }
  110. log_tab[1] = 0;
  111. for (i = 0, p = 1; i < 10; ++i) {
  112. rco_tab[i] = p;
  113. p = (p << 1) ^ (p & 0x80 ? 0x01b : 0);
  114. }
  115. for (i = 0; i < 256; ++i) {
  116. p = (i ? pow_tab[255 - log_tab[i]] : 0);
  117. q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2));
  118. p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2));
  119. sbx_tab[i] = p;
  120. isb_tab[p] = (u8)i;
  121. }
  122. for (i = 0; i < 256; ++i) {
  123. p = sbx_tab[i];
  124. t = p;
  125. aes_fl_tab[0][i] = t;
  126. aes_fl_tab[1][i] = rol32(t, 8);
  127. aes_fl_tab[2][i] = rol32(t, 16);
  128. aes_fl_tab[3][i] = rol32(t, 24);
  129. t = ((u32)ff_mult(2, p)) |
  130. ((u32)p << 8) |
  131. ((u32)p << 16) | ((u32)ff_mult(3, p) << 24);
  132. aes_ft_tab[0][i] = t;
  133. aes_ft_tab[1][i] = rol32(t, 8);
  134. aes_ft_tab[2][i] = rol32(t, 16);
  135. aes_ft_tab[3][i] = rol32(t, 24);
  136. p = isb_tab[i];
  137. t = p;
  138. aes_il_tab[0][i] = t;
  139. aes_il_tab[1][i] = rol32(t, 8);
  140. aes_il_tab[2][i] = rol32(t, 16);
  141. aes_il_tab[3][i] = rol32(t, 24);
  142. t = ((u32)ff_mult(14, p)) |
  143. ((u32)ff_mult(9, p) << 8) |
  144. ((u32)ff_mult(13, p) << 16) |
  145. ((u32)ff_mult(11, p) << 24);
  146. aes_it_tab[0][i] = t;
  147. aes_it_tab[1][i] = rol32(t, 8);
  148. aes_it_tab[2][i] = rol32(t, 16);
  149. aes_it_tab[3][i] = rol32(t, 24);
  150. }
  151. }
  152. #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
  153. #define imix_col(y, x) \
  154. u = star_x(x); \
  155. v = star_x(u); \
  156. w = star_x(v); \
  157. t = w ^ (x); \
  158. (y) = u ^ v ^ w; \
  159. (y) ^= ror32(u ^ t, 8) ^ \
  160. ror32(v ^ t, 16) ^ \
  161. ror32(t, 24)
  162. /* initialise the key schedule from the user supplied key */
  163. #define loop4(i) \
  164. { \
  165. t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
  166. t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \
  167. t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \
  168. t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \
  169. t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \
  170. }
  171. #define loop6(i) \
  172. { \
  173. t = ror32(t, 8); t = ls_box(t) ^ rco_tab[i]; \
  174. t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \
  175. t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \
  176. t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \
  177. t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \
  178. t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \
  179. t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \
  180. }
  181. #define loop8(i) \
  182. { \
  183. t = ror32(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \
  184. t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \
  185. t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \
  186. t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \
  187. t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \
  188. t = E_KEY[8 * i + 4] ^ ls_box(t); \
  189. E_KEY[8 * i + 12] = t; \
  190. t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \
  191. t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \
  192. t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \
  193. }
  194. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  195. unsigned int key_len)
  196. {
  197. struct aes_ctx *ctx = crypto_tfm_ctx(tfm);
  198. const __le32 *key = (const __le32 *)in_key;
  199. u32 *flags = &tfm->crt_flags;
  200. u32 i, j, t, u, v, w;
  201. if (key_len % 8) {
  202. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  203. return -EINVAL;
  204. }
  205. ctx->key_length = key_len;
  206. D_KEY[key_len + 24] = E_KEY[0] = le32_to_cpu(key[0]);
  207. D_KEY[key_len + 25] = E_KEY[1] = le32_to_cpu(key[1]);
  208. D_KEY[key_len + 26] = E_KEY[2] = le32_to_cpu(key[2]);
  209. D_KEY[key_len + 27] = E_KEY[3] = le32_to_cpu(key[3]);
  210. switch (key_len) {
  211. case 16:
  212. t = E_KEY[3];
  213. for (i = 0; i < 10; ++i)
  214. loop4(i);
  215. break;
  216. case 24:
  217. E_KEY[4] = le32_to_cpu(key[4]);
  218. t = E_KEY[5] = le32_to_cpu(key[5]);
  219. for (i = 0; i < 8; ++i)
  220. loop6 (i);
  221. break;
  222. case 32:
  223. E_KEY[4] = le32_to_cpu(key[4]);
  224. E_KEY[5] = le32_to_cpu(key[5]);
  225. E_KEY[6] = le32_to_cpu(key[6]);
  226. t = E_KEY[7] = le32_to_cpu(key[7]);
  227. for (i = 0; i < 7; ++i)
  228. loop8(i);
  229. break;
  230. }
  231. D_KEY[0] = E_KEY[key_len + 24];
  232. D_KEY[1] = E_KEY[key_len + 25];
  233. D_KEY[2] = E_KEY[key_len + 26];
  234. D_KEY[3] = E_KEY[key_len + 27];
  235. for (i = 4; i < key_len + 24; ++i) {
  236. j = key_len + 24 - (i & ~3) + (i & 3);
  237. imix_col(D_KEY[j], E_KEY[i]);
  238. }
  239. return 0;
  240. }
  241. asmlinkage void aes_enc_blk(struct crypto_tfm *tfm, u8 *out, const u8 *in);
  242. asmlinkage void aes_dec_blk(struct crypto_tfm *tfm, u8 *out, const u8 *in);
  243. static void aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  244. {
  245. aes_enc_blk(tfm, dst, src);
  246. }
  247. static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  248. {
  249. aes_dec_blk(tfm, dst, src);
  250. }
  251. static struct crypto_alg aes_alg = {
  252. .cra_name = "aes",
  253. .cra_driver_name = "aes-x86_64",
  254. .cra_priority = 200,
  255. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  256. .cra_blocksize = AES_BLOCK_SIZE,
  257. .cra_ctxsize = sizeof(struct aes_ctx),
  258. .cra_module = THIS_MODULE,
  259. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  260. .cra_u = {
  261. .cipher = {
  262. .cia_min_keysize = AES_MIN_KEY_SIZE,
  263. .cia_max_keysize = AES_MAX_KEY_SIZE,
  264. .cia_setkey = aes_set_key,
  265. .cia_encrypt = aes_encrypt,
  266. .cia_decrypt = aes_decrypt
  267. }
  268. }
  269. };
  270. static int __init aes_init(void)
  271. {
  272. gen_tabs();
  273. return crypto_register_alg(&aes_alg);
  274. }
  275. static void __exit aes_fini(void)
  276. {
  277. crypto_unregister_alg(&aes_alg);
  278. }
  279. module_init(aes_init);
  280. module_exit(aes_fini);
  281. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
  282. MODULE_LICENSE("GPL");
  283. MODULE_ALIAS("aes");