padlock-aes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 <linux/kernel.h>
  52. #include <asm/byteorder.h>
  53. #include "padlock.h"
  54. #define AES_MIN_KEY_SIZE 16 /* in uint8_t units */
  55. #define AES_MAX_KEY_SIZE 32 /* ditto */
  56. #define AES_BLOCK_SIZE 16 /* ditto */
  57. #define AES_EXTENDED_KEY_SIZE 64 /* in uint32_t units */
  58. #define AES_EXTENDED_KEY_SIZE_B (AES_EXTENDED_KEY_SIZE * sizeof(uint32_t))
  59. struct aes_ctx {
  60. struct {
  61. struct cword encrypt;
  62. struct cword decrypt;
  63. } cword;
  64. u32 *D;
  65. int key_length;
  66. u32 E[AES_EXTENDED_KEY_SIZE];
  67. u32 d_data[AES_EXTENDED_KEY_SIZE];
  68. };
  69. /* ====== Key management routines ====== */
  70. static inline uint32_t
  71. generic_rotr32 (const uint32_t x, const unsigned bits)
  72. {
  73. const unsigned n = bits % 32;
  74. return (x >> n) | (x << (32 - n));
  75. }
  76. static inline uint32_t
  77. generic_rotl32 (const uint32_t x, const unsigned bits)
  78. {
  79. const unsigned n = bits % 32;
  80. return (x << n) | (x >> (32 - n));
  81. }
  82. #define rotl generic_rotl32
  83. #define rotr generic_rotr32
  84. /*
  85. * #define byte(x, nr) ((unsigned char)((x) >> (nr*8)))
  86. */
  87. static inline uint8_t
  88. byte(const uint32_t x, const unsigned n)
  89. {
  90. return x >> (n << 3);
  91. }
  92. #define E_KEY ctx->E
  93. #define D_KEY ctx->D
  94. static uint8_t pow_tab[256];
  95. static uint8_t log_tab[256];
  96. static uint8_t sbx_tab[256];
  97. static uint8_t isb_tab[256];
  98. static uint32_t rco_tab[10];
  99. static uint32_t ft_tab[4][256];
  100. static uint32_t it_tab[4][256];
  101. static uint32_t fl_tab[4][256];
  102. static uint32_t il_tab[4][256];
  103. static inline uint8_t
  104. f_mult (uint8_t a, uint8_t b)
  105. {
  106. uint8_t aa = log_tab[a], cc = aa + log_tab[b];
  107. return pow_tab[cc + (cc < aa ? 1 : 0)];
  108. }
  109. #define ff_mult(a,b) (a && b ? f_mult(a, b) : 0)
  110. #define f_rn(bo, bi, n, k) \
  111. bo[n] = ft_tab[0][byte(bi[n],0)] ^ \
  112. ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
  113. ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  114. ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
  115. #define i_rn(bo, bi, n, k) \
  116. bo[n] = it_tab[0][byte(bi[n],0)] ^ \
  117. it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
  118. it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  119. it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
  120. #define ls_box(x) \
  121. ( fl_tab[0][byte(x, 0)] ^ \
  122. fl_tab[1][byte(x, 1)] ^ \
  123. fl_tab[2][byte(x, 2)] ^ \
  124. fl_tab[3][byte(x, 3)] )
  125. #define f_rl(bo, bi, n, k) \
  126. bo[n] = fl_tab[0][byte(bi[n],0)] ^ \
  127. fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
  128. fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  129. fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
  130. #define i_rl(bo, bi, n, k) \
  131. bo[n] = il_tab[0][byte(bi[n],0)] ^ \
  132. il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
  133. il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  134. il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
  135. static void
  136. gen_tabs (void)
  137. {
  138. uint32_t i, t;
  139. uint8_t p, q;
  140. /* log and power tables for GF(2**8) finite field with
  141. 0x011b as modular polynomial - the simplest prmitive
  142. root is 0x03, used here to generate the tables */
  143. for (i = 0, p = 1; i < 256; ++i) {
  144. pow_tab[i] = (uint8_t) p;
  145. log_tab[p] = (uint8_t) i;
  146. p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0);
  147. }
  148. log_tab[1] = 0;
  149. for (i = 0, p = 1; i < 10; ++i) {
  150. rco_tab[i] = p;
  151. p = (p << 1) ^ (p & 0x80 ? 0x01b : 0);
  152. }
  153. for (i = 0; i < 256; ++i) {
  154. p = (i ? pow_tab[255 - log_tab[i]] : 0);
  155. q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2));
  156. p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2));
  157. sbx_tab[i] = p;
  158. isb_tab[p] = (uint8_t) i;
  159. }
  160. for (i = 0; i < 256; ++i) {
  161. p = sbx_tab[i];
  162. t = p;
  163. fl_tab[0][i] = t;
  164. fl_tab[1][i] = rotl (t, 8);
  165. fl_tab[2][i] = rotl (t, 16);
  166. fl_tab[3][i] = rotl (t, 24);
  167. t = ((uint32_t) ff_mult (2, p)) |
  168. ((uint32_t) p << 8) |
  169. ((uint32_t) p << 16) | ((uint32_t) ff_mult (3, p) << 24);
  170. ft_tab[0][i] = t;
  171. ft_tab[1][i] = rotl (t, 8);
  172. ft_tab[2][i] = rotl (t, 16);
  173. ft_tab[3][i] = rotl (t, 24);
  174. p = isb_tab[i];
  175. t = p;
  176. il_tab[0][i] = t;
  177. il_tab[1][i] = rotl (t, 8);
  178. il_tab[2][i] = rotl (t, 16);
  179. il_tab[3][i] = rotl (t, 24);
  180. t = ((uint32_t) ff_mult (14, p)) |
  181. ((uint32_t) ff_mult (9, p) << 8) |
  182. ((uint32_t) ff_mult (13, p) << 16) |
  183. ((uint32_t) ff_mult (11, p) << 24);
  184. it_tab[0][i] = t;
  185. it_tab[1][i] = rotl (t, 8);
  186. it_tab[2][i] = rotl (t, 16);
  187. it_tab[3][i] = rotl (t, 24);
  188. }
  189. }
  190. #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
  191. #define imix_col(y,x) \
  192. u = star_x(x); \
  193. v = star_x(u); \
  194. w = star_x(v); \
  195. t = w ^ (x); \
  196. (y) = u ^ v ^ w; \
  197. (y) ^= rotr(u ^ t, 8) ^ \
  198. rotr(v ^ t, 16) ^ \
  199. rotr(t,24)
  200. /* initialise the key schedule from the user supplied key */
  201. #define loop4(i) \
  202. { t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \
  203. t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \
  204. t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \
  205. t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \
  206. t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \
  207. }
  208. #define loop6(i) \
  209. { t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \
  210. t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \
  211. t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \
  212. t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \
  213. t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \
  214. t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \
  215. t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \
  216. }
  217. #define loop8(i) \
  218. { t = rotr(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \
  219. t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \
  220. t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \
  221. t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \
  222. t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \
  223. t = E_KEY[8 * i + 4] ^ ls_box(t); \
  224. E_KEY[8 * i + 12] = t; \
  225. t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \
  226. t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \
  227. t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \
  228. }
  229. /* Tells whether the ACE is capable to generate
  230. the extended key for a given key_len. */
  231. static inline int
  232. aes_hw_extkey_available(uint8_t key_len)
  233. {
  234. /* TODO: We should check the actual CPU model/stepping
  235. as it's possible that the capability will be
  236. added in the next CPU revisions. */
  237. if (key_len == 16)
  238. return 1;
  239. return 0;
  240. }
  241. static inline struct aes_ctx *aes_ctx(struct crypto_tfm *tfm)
  242. {
  243. unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm);
  244. unsigned long align = PADLOCK_ALIGNMENT;
  245. if (align <= crypto_tfm_ctx_alignment())
  246. align = 1;
  247. return (struct aes_ctx *)ALIGN(addr, align);
  248. }
  249. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  250. unsigned int key_len, u32 *flags)
  251. {
  252. struct aes_ctx *ctx = aes_ctx(tfm);
  253. const __le32 *key = (const __le32 *)in_key;
  254. uint32_t i, t, u, v, w;
  255. uint32_t P[AES_EXTENDED_KEY_SIZE];
  256. uint32_t rounds;
  257. if (key_len != 16 && key_len != 24 && key_len != 32) {
  258. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  259. return -EINVAL;
  260. }
  261. ctx->key_length = key_len;
  262. /*
  263. * If the hardware is capable of generating the extended key
  264. * itself we must supply the plain key for both encryption
  265. * and decryption.
  266. */
  267. ctx->D = ctx->E;
  268. E_KEY[0] = le32_to_cpu(key[0]);
  269. E_KEY[1] = le32_to_cpu(key[1]);
  270. E_KEY[2] = le32_to_cpu(key[2]);
  271. E_KEY[3] = le32_to_cpu(key[3]);
  272. /* Prepare control words. */
  273. memset(&ctx->cword, 0, sizeof(ctx->cword));
  274. ctx->cword.decrypt.encdec = 1;
  275. ctx->cword.encrypt.rounds = 10 + (key_len - 16) / 4;
  276. ctx->cword.decrypt.rounds = ctx->cword.encrypt.rounds;
  277. ctx->cword.encrypt.ksize = (key_len - 16) / 8;
  278. ctx->cword.decrypt.ksize = ctx->cword.encrypt.ksize;
  279. /* Don't generate extended keys if the hardware can do it. */
  280. if (aes_hw_extkey_available(key_len))
  281. return 0;
  282. ctx->D = ctx->d_data;
  283. ctx->cword.encrypt.keygen = 1;
  284. ctx->cword.decrypt.keygen = 1;
  285. switch (key_len) {
  286. case 16:
  287. t = E_KEY[3];
  288. for (i = 0; i < 10; ++i)
  289. loop4 (i);
  290. break;
  291. case 24:
  292. E_KEY[4] = le32_to_cpu(key[4]);
  293. t = E_KEY[5] = le32_to_cpu(key[5]);
  294. for (i = 0; i < 8; ++i)
  295. loop6 (i);
  296. break;
  297. case 32:
  298. E_KEY[4] = le32_to_cpu(key[4]);
  299. E_KEY[5] = le32_to_cpu(key[5]);
  300. E_KEY[6] = le32_to_cpu(key[6]);
  301. t = E_KEY[7] = le32_to_cpu(key[7]);
  302. for (i = 0; i < 7; ++i)
  303. loop8 (i);
  304. break;
  305. }
  306. D_KEY[0] = E_KEY[0];
  307. D_KEY[1] = E_KEY[1];
  308. D_KEY[2] = E_KEY[2];
  309. D_KEY[3] = E_KEY[3];
  310. for (i = 4; i < key_len + 24; ++i) {
  311. imix_col (D_KEY[i], E_KEY[i]);
  312. }
  313. /* PadLock needs a different format of the decryption key. */
  314. rounds = 10 + (key_len - 16) / 4;
  315. for (i = 0; i < rounds; i++) {
  316. P[((i + 1) * 4) + 0] = D_KEY[((rounds - i - 1) * 4) + 0];
  317. P[((i + 1) * 4) + 1] = D_KEY[((rounds - i - 1) * 4) + 1];
  318. P[((i + 1) * 4) + 2] = D_KEY[((rounds - i - 1) * 4) + 2];
  319. P[((i + 1) * 4) + 3] = D_KEY[((rounds - i - 1) * 4) + 3];
  320. }
  321. P[0] = E_KEY[(rounds * 4) + 0];
  322. P[1] = E_KEY[(rounds * 4) + 1];
  323. P[2] = E_KEY[(rounds * 4) + 2];
  324. P[3] = E_KEY[(rounds * 4) + 3];
  325. memcpy(D_KEY, P, AES_EXTENDED_KEY_SIZE_B);
  326. return 0;
  327. }
  328. /* ====== Encryption/decryption routines ====== */
  329. /* These are the real call to PadLock. */
  330. static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key,
  331. void *control_word, u32 count)
  332. {
  333. asm volatile ("pushfl; popfl"); /* enforce key reload. */
  334. asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
  335. : "+S"(input), "+D"(output)
  336. : "d"(control_word), "b"(key), "c"(count));
  337. }
  338. static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
  339. u8 *iv, void *control_word, u32 count)
  340. {
  341. /* Enforce key reload. */
  342. asm volatile ("pushfl; popfl");
  343. /* rep xcryptcbc */
  344. asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"
  345. : "+S" (input), "+D" (output), "+a" (iv)
  346. : "d" (control_word), "b" (key), "c" (count));
  347. return iv;
  348. }
  349. static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  350. {
  351. struct aes_ctx *ctx = aes_ctx(tfm);
  352. padlock_xcrypt_ecb(in, out, ctx->E, &ctx->cword.encrypt, 1);
  353. }
  354. static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  355. {
  356. struct aes_ctx *ctx = aes_ctx(tfm);
  357. padlock_xcrypt_ecb(in, out, ctx->D, &ctx->cword.decrypt, 1);
  358. }
  359. static unsigned int aes_encrypt_ecb(const struct cipher_desc *desc, u8 *out,
  360. const u8 *in, unsigned int nbytes)
  361. {
  362. struct aes_ctx *ctx = aes_ctx(desc->tfm);
  363. padlock_xcrypt_ecb(in, out, ctx->E, &ctx->cword.encrypt,
  364. nbytes / AES_BLOCK_SIZE);
  365. return nbytes & ~(AES_BLOCK_SIZE - 1);
  366. }
  367. static unsigned int aes_decrypt_ecb(const struct cipher_desc *desc, u8 *out,
  368. const u8 *in, unsigned int nbytes)
  369. {
  370. struct aes_ctx *ctx = aes_ctx(desc->tfm);
  371. padlock_xcrypt_ecb(in, out, ctx->D, &ctx->cword.decrypt,
  372. nbytes / AES_BLOCK_SIZE);
  373. return nbytes & ~(AES_BLOCK_SIZE - 1);
  374. }
  375. static unsigned int aes_encrypt_cbc(const struct cipher_desc *desc, u8 *out,
  376. const u8 *in, unsigned int nbytes)
  377. {
  378. struct aes_ctx *ctx = aes_ctx(desc->tfm);
  379. u8 *iv;
  380. iv = padlock_xcrypt_cbc(in, out, ctx->E, desc->info,
  381. &ctx->cword.encrypt, nbytes / AES_BLOCK_SIZE);
  382. memcpy(desc->info, iv, AES_BLOCK_SIZE);
  383. return nbytes & ~(AES_BLOCK_SIZE - 1);
  384. }
  385. static unsigned int aes_decrypt_cbc(const struct cipher_desc *desc, u8 *out,
  386. const u8 *in, unsigned int nbytes)
  387. {
  388. struct aes_ctx *ctx = aes_ctx(desc->tfm);
  389. padlock_xcrypt_cbc(in, out, ctx->D, desc->info, &ctx->cword.decrypt,
  390. nbytes / AES_BLOCK_SIZE);
  391. return nbytes & ~(AES_BLOCK_SIZE - 1);
  392. }
  393. static struct crypto_alg aes_alg = {
  394. .cra_name = "aes",
  395. .cra_driver_name = "aes-padlock",
  396. .cra_priority = 300,
  397. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  398. .cra_blocksize = AES_BLOCK_SIZE,
  399. .cra_ctxsize = sizeof(struct aes_ctx),
  400. .cra_alignmask = PADLOCK_ALIGNMENT - 1,
  401. .cra_module = THIS_MODULE,
  402. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  403. .cra_u = {
  404. .cipher = {
  405. .cia_min_keysize = AES_MIN_KEY_SIZE,
  406. .cia_max_keysize = AES_MAX_KEY_SIZE,
  407. .cia_setkey = aes_set_key,
  408. .cia_encrypt = aes_encrypt,
  409. .cia_decrypt = aes_decrypt,
  410. .cia_encrypt_ecb = aes_encrypt_ecb,
  411. .cia_decrypt_ecb = aes_decrypt_ecb,
  412. .cia_encrypt_cbc = aes_encrypt_cbc,
  413. .cia_decrypt_cbc = aes_decrypt_cbc,
  414. }
  415. }
  416. };
  417. int __init padlock_init_aes(void)
  418. {
  419. printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n");
  420. gen_tabs();
  421. return crypto_register_alg(&aes_alg);
  422. }
  423. void __exit padlock_fini_aes(void)
  424. {
  425. crypto_unregister_alg(&aes_alg);
  426. }