padlock-aes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for VIA PadLock hardware crypto engine.
  5. *
  6. * Copyright (c) 2004 Michal Ludvig <michal@logix.cz>
  7. *
  8. * Key expansion routine taken from crypto/aes.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * ---------------------------------------------------------------------------
  16. * Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
  17. * All rights reserved.
  18. *
  19. * LICENSE TERMS
  20. *
  21. * The free distribution and use of this software in both source and binary
  22. * form is allowed (with or without changes) provided that:
  23. *
  24. * 1. distributions of this source code include the above copyright
  25. * notice, this list of conditions and the following disclaimer;
  26. *
  27. * 2. distributions in binary form include the above copyright
  28. * notice, this list of conditions and the following disclaimer
  29. * in the documentation and/or other associated materials;
  30. *
  31. * 3. the copyright holder's name is not used to endorse products
  32. * built using this software without specific written permission.
  33. *
  34. * ALTERNATIVELY, provided that this notice is retained in full, this product
  35. * may be distributed under the terms of the GNU General Public License (GPL),
  36. * in which case the provisions of the GPL apply INSTEAD OF those given above.
  37. *
  38. * DISCLAIMER
  39. *
  40. * This software is provided 'as is' with no explicit or implied warranties
  41. * in respect of its properties, including, but not limited to, correctness
  42. * and/or fitness for purpose.
  43. * ---------------------------------------------------------------------------
  44. */
  45. #include <linux/module.h>
  46. #include <linux/init.h>
  47. #include <linux/types.h>
  48. #include <linux/errno.h>
  49. #include <linux/crypto.h>
  50. #include <linux/interrupt.h>
  51. #include <asm/byteorder.h>
  52. #include "padlock.h"
  53. #define AES_MIN_KEY_SIZE 16 /* in uint8_t units */
  54. #define AES_MAX_KEY_SIZE 32 /* ditto */
  55. #define AES_BLOCK_SIZE 16 /* ditto */
  56. #define AES_EXTENDED_KEY_SIZE 64 /* in uint32_t units */
  57. #define AES_EXTENDED_KEY_SIZE_B (AES_EXTENDED_KEY_SIZE * sizeof(uint32_t))
  58. struct aes_ctx {
  59. uint32_t e_data[AES_EXTENDED_KEY_SIZE+4];
  60. uint32_t d_data[AES_EXTENDED_KEY_SIZE+4];
  61. uint32_t *E;
  62. uint32_t *D;
  63. int key_length;
  64. };
  65. /* ====== Key management routines ====== */
  66. static inline uint32_t
  67. generic_rotr32 (const uint32_t x, const unsigned bits)
  68. {
  69. const unsigned n = bits % 32;
  70. return (x >> n) | (x << (32 - n));
  71. }
  72. static inline uint32_t
  73. generic_rotl32 (const uint32_t x, const unsigned bits)
  74. {
  75. const unsigned n = bits % 32;
  76. return (x << n) | (x >> (32 - n));
  77. }
  78. #define rotl generic_rotl32
  79. #define rotr generic_rotr32
  80. /*
  81. * #define byte(x, nr) ((unsigned char)((x) >> (nr*8)))
  82. */
  83. static inline uint8_t
  84. byte(const uint32_t x, const unsigned n)
  85. {
  86. return x >> (n << 3);
  87. }
  88. #define uint32_t_in(x) le32_to_cpu(*(const uint32_t *)(x))
  89. #define uint32_t_out(to, from) (*(uint32_t *)(to) = cpu_to_le32(from))
  90. #define E_KEY ctx->E
  91. #define D_KEY ctx->D
  92. static uint8_t pow_tab[256];
  93. static uint8_t log_tab[256];
  94. static uint8_t sbx_tab[256];
  95. static uint8_t isb_tab[256];
  96. static uint32_t rco_tab[10];
  97. static uint32_t ft_tab[4][256];
  98. static uint32_t it_tab[4][256];
  99. static uint32_t fl_tab[4][256];
  100. static uint32_t il_tab[4][256];
  101. static inline uint8_t
  102. f_mult (uint8_t a, uint8_t b)
  103. {
  104. uint8_t aa = log_tab[a], cc = aa + log_tab[b];
  105. return pow_tab[cc + (cc < aa ? 1 : 0)];
  106. }
  107. #define ff_mult(a,b) (a && b ? f_mult(a, b) : 0)
  108. #define f_rn(bo, bi, n, k) \
  109. bo[n] = ft_tab[0][byte(bi[n],0)] ^ \
  110. ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
  111. ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  112. ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
  113. #define i_rn(bo, bi, n, k) \
  114. bo[n] = it_tab[0][byte(bi[n],0)] ^ \
  115. it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
  116. it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  117. it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
  118. #define ls_box(x) \
  119. ( fl_tab[0][byte(x, 0)] ^ \
  120. fl_tab[1][byte(x, 1)] ^ \
  121. fl_tab[2][byte(x, 2)] ^ \
  122. fl_tab[3][byte(x, 3)] )
  123. #define f_rl(bo, bi, n, k) \
  124. bo[n] = fl_tab[0][byte(bi[n],0)] ^ \
  125. fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
  126. fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  127. fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
  128. #define i_rl(bo, bi, n, k) \
  129. bo[n] = il_tab[0][byte(bi[n],0)] ^ \
  130. il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
  131. il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  132. il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
  133. static void
  134. gen_tabs (void)
  135. {
  136. uint32_t i, t;
  137. uint8_t p, q;
  138. /* log and power tables for GF(2**8) finite field with
  139. 0x011b as modular polynomial - the simplest prmitive
  140. root is 0x03, used here to generate the tables */
  141. for (i = 0, p = 1; i < 256; ++i) {
  142. pow_tab[i] = (uint8_t) p;
  143. log_tab[p] = (uint8_t) i;
  144. p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0);
  145. }
  146. log_tab[1] = 0;
  147. for (i = 0, p = 1; i < 10; ++i) {
  148. rco_tab[i] = p;
  149. p = (p << 1) ^ (p & 0x80 ? 0x01b : 0);
  150. }
  151. for (i = 0; i < 256; ++i) {
  152. p = (i ? pow_tab[255 - log_tab[i]] : 0);
  153. q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2));
  154. p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2));
  155. sbx_tab[i] = p;
  156. isb_tab[p] = (uint8_t) i;
  157. }
  158. for (i = 0; i < 256; ++i) {
  159. p = sbx_tab[i];
  160. t = p;
  161. fl_tab[0][i] = t;
  162. fl_tab[1][i] = rotl (t, 8);
  163. fl_tab[2][i] = rotl (t, 16);
  164. fl_tab[3][i] = rotl (t, 24);
  165. t = ((uint32_t) ff_mult (2, p)) |
  166. ((uint32_t) p << 8) |
  167. ((uint32_t) p << 16) | ((uint32_t) ff_mult (3, p) << 24);
  168. ft_tab[0][i] = t;
  169. ft_tab[1][i] = rotl (t, 8);
  170. ft_tab[2][i] = rotl (t, 16);
  171. ft_tab[3][i] = rotl (t, 24);
  172. p = isb_tab[i];
  173. t = p;
  174. il_tab[0][i] = t;
  175. il_tab[1][i] = rotl (t, 8);
  176. il_tab[2][i] = rotl (t, 16);
  177. il_tab[3][i] = rotl (t, 24);
  178. t = ((uint32_t) ff_mult (14, p)) |
  179. ((uint32_t) ff_mult (9, p) << 8) |
  180. ((uint32_t) ff_mult (13, p) << 16) |
  181. ((uint32_t) ff_mult (11, p) << 24);
  182. it_tab[0][i] = t;
  183. it_tab[1][i] = rotl (t, 8);
  184. it_tab[2][i] = rotl (t, 16);
  185. it_tab[3][i] = rotl (t, 24);
  186. }
  187. }
  188. #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
  189. #define imix_col(y,x) \
  190. u = star_x(x); \
  191. v = star_x(u); \
  192. w = star_x(v); \
  193. t = w ^ (x); \
  194. (y) = u ^ v ^ w; \
  195. (y) ^= rotr(u ^ t, 8) ^ \
  196. rotr(v ^ t, 16) ^ \
  197. rotr(t,24)
  198. /* initialise the key schedule from the user supplied key */
  199. #define loop4(i) \
  200. { t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \
  201. t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \
  202. t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \
  203. t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \
  204. t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \
  205. }
  206. #define loop6(i) \
  207. { t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \
  208. t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \
  209. t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \
  210. t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \
  211. t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \
  212. t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \
  213. t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \
  214. }
  215. #define loop8(i) \
  216. { t = rotr(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \
  217. t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \
  218. t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \
  219. t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \
  220. t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \
  221. t = E_KEY[8 * i + 4] ^ ls_box(t); \
  222. E_KEY[8 * i + 12] = t; \
  223. t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \
  224. t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \
  225. t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \
  226. }
  227. /* Tells whether the ACE is capable to generate
  228. the extended key for a given key_len. */
  229. static inline int
  230. aes_hw_extkey_available(uint8_t key_len)
  231. {
  232. /* TODO: We should check the actual CPU model/stepping
  233. as it's possible that the capability will be
  234. added in the next CPU revisions. */
  235. if (key_len == 16)
  236. return 1;
  237. return 0;
  238. }
  239. static int
  240. aes_set_key(void *ctx_arg, const uint8_t *in_key, unsigned int key_len, uint32_t *flags)
  241. {
  242. struct aes_ctx *ctx = ctx_arg;
  243. uint32_t i, t, u, v, w;
  244. uint32_t P[AES_EXTENDED_KEY_SIZE];
  245. uint32_t rounds;
  246. if (key_len != 16 && key_len != 24 && key_len != 32) {
  247. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  248. return -EINVAL;
  249. }
  250. ctx->key_length = key_len;
  251. ctx->E = ctx->e_data;
  252. ctx->D = ctx->d_data;
  253. /* Ensure 16-Bytes alignmentation of keys for VIA PadLock. */
  254. if ((int)(ctx->e_data) & 0x0F)
  255. ctx->E += 4 - (((int)(ctx->e_data) & 0x0F) / sizeof (ctx->e_data[0]));
  256. if ((int)(ctx->d_data) & 0x0F)
  257. ctx->D += 4 - (((int)(ctx->d_data) & 0x0F) / sizeof (ctx->d_data[0]));
  258. E_KEY[0] = uint32_t_in (in_key);
  259. E_KEY[1] = uint32_t_in (in_key + 4);
  260. E_KEY[2] = uint32_t_in (in_key + 8);
  261. E_KEY[3] = uint32_t_in (in_key + 12);
  262. /* Don't generate extended keys if the hardware can do it. */
  263. if (aes_hw_extkey_available(key_len))
  264. return 0;
  265. switch (key_len) {
  266. case 16:
  267. t = E_KEY[3];
  268. for (i = 0; i < 10; ++i)
  269. loop4 (i);
  270. break;
  271. case 24:
  272. E_KEY[4] = uint32_t_in (in_key + 16);
  273. t = E_KEY[5] = uint32_t_in (in_key + 20);
  274. for (i = 0; i < 8; ++i)
  275. loop6 (i);
  276. break;
  277. case 32:
  278. E_KEY[4] = uint32_t_in (in_key + 16);
  279. E_KEY[5] = uint32_t_in (in_key + 20);
  280. E_KEY[6] = uint32_t_in (in_key + 24);
  281. t = E_KEY[7] = uint32_t_in (in_key + 28);
  282. for (i = 0; i < 7; ++i)
  283. loop8 (i);
  284. break;
  285. }
  286. D_KEY[0] = E_KEY[0];
  287. D_KEY[1] = E_KEY[1];
  288. D_KEY[2] = E_KEY[2];
  289. D_KEY[3] = E_KEY[3];
  290. for (i = 4; i < key_len + 24; ++i) {
  291. imix_col (D_KEY[i], E_KEY[i]);
  292. }
  293. /* PadLock needs a different format of the decryption key. */
  294. rounds = 10 + (key_len - 16) / 4;
  295. for (i = 0; i < rounds; i++) {
  296. P[((i + 1) * 4) + 0] = D_KEY[((rounds - i - 1) * 4) + 0];
  297. P[((i + 1) * 4) + 1] = D_KEY[((rounds - i - 1) * 4) + 1];
  298. P[((i + 1) * 4) + 2] = D_KEY[((rounds - i - 1) * 4) + 2];
  299. P[((i + 1) * 4) + 3] = D_KEY[((rounds - i - 1) * 4) + 3];
  300. }
  301. P[0] = E_KEY[(rounds * 4) + 0];
  302. P[1] = E_KEY[(rounds * 4) + 1];
  303. P[2] = E_KEY[(rounds * 4) + 2];
  304. P[3] = E_KEY[(rounds * 4) + 3];
  305. memcpy(D_KEY, P, AES_EXTENDED_KEY_SIZE_B);
  306. return 0;
  307. }
  308. /* ====== Encryption/decryption routines ====== */
  309. /* This is the real call to PadLock. */
  310. static inline void
  311. padlock_xcrypt_ecb(uint8_t *input, uint8_t *output, uint8_t *key,
  312. void *control_word, uint32_t count)
  313. {
  314. asm volatile ("pushfl; popfl"); /* enforce key reload. */
  315. asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
  316. : "+S"(input), "+D"(output)
  317. : "d"(control_word), "b"(key), "c"(count));
  318. }
  319. static void
  320. aes_padlock(void *ctx_arg, uint8_t *out_arg, const uint8_t *in_arg, int encdec)
  321. {
  322. /* Don't blindly modify this structure - the items must
  323. fit on 16-Bytes boundaries! */
  324. struct padlock_xcrypt_data {
  325. uint8_t buf[AES_BLOCK_SIZE];
  326. union cword cword;
  327. };
  328. struct aes_ctx *ctx = ctx_arg;
  329. char bigbuf[sizeof(struct padlock_xcrypt_data) + 16];
  330. struct padlock_xcrypt_data *data;
  331. void *key;
  332. /* Place 'data' at the first 16-Bytes aligned address in 'bigbuf'. */
  333. if (((long)bigbuf) & 0x0F)
  334. data = (void*)(bigbuf + 16 - ((long)bigbuf & 0x0F));
  335. else
  336. data = (void*)bigbuf;
  337. /* Prepare Control word. */
  338. memset (data, 0, sizeof(struct padlock_xcrypt_data));
  339. data->cword.b.encdec = !encdec; /* in the rest of cryptoapi ENC=1/DEC=0 */
  340. data->cword.b.rounds = 10 + (ctx->key_length - 16) / 4;
  341. data->cword.b.ksize = (ctx->key_length - 16) / 8;
  342. /* Is the hardware capable to generate the extended key? */
  343. if (!aes_hw_extkey_available(ctx->key_length))
  344. data->cword.b.keygen = 1;
  345. /* ctx->E starts with a plain key - if the hardware is capable
  346. to generate the extended key itself we must supply
  347. the plain key for both Encryption and Decryption. */
  348. if (encdec == CRYPTO_DIR_ENCRYPT || data->cword.b.keygen == 0)
  349. key = ctx->E;
  350. else
  351. key = ctx->D;
  352. memcpy(data->buf, in_arg, AES_BLOCK_SIZE);
  353. padlock_xcrypt_ecb(data->buf, data->buf, key, &data->cword, 1);
  354. memcpy(out_arg, data->buf, AES_BLOCK_SIZE);
  355. }
  356. static void
  357. aes_encrypt(void *ctx_arg, uint8_t *out, const uint8_t *in)
  358. {
  359. aes_padlock(ctx_arg, out, in, CRYPTO_DIR_ENCRYPT);
  360. }
  361. static void
  362. aes_decrypt(void *ctx_arg, uint8_t *out, const uint8_t *in)
  363. {
  364. aes_padlock(ctx_arg, out, in, CRYPTO_DIR_DECRYPT);
  365. }
  366. static struct crypto_alg aes_alg = {
  367. .cra_name = "aes",
  368. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  369. .cra_blocksize = AES_BLOCK_SIZE,
  370. .cra_ctxsize = sizeof(struct aes_ctx),
  371. .cra_module = THIS_MODULE,
  372. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  373. .cra_u = {
  374. .cipher = {
  375. .cia_min_keysize = AES_MIN_KEY_SIZE,
  376. .cia_max_keysize = AES_MAX_KEY_SIZE,
  377. .cia_setkey = aes_set_key,
  378. .cia_encrypt = aes_encrypt,
  379. .cia_decrypt = aes_decrypt
  380. }
  381. }
  382. };
  383. int __init padlock_init_aes(void)
  384. {
  385. printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n");
  386. gen_tabs();
  387. return crypto_register_alg(&aes_alg);
  388. }
  389. void __exit padlock_fini_aes(void)
  390. {
  391. crypto_unregister_alg(&aes_alg);
  392. }