padlock-aes.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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_generic.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 <crypto/algapi.h>
  46. #include <crypto/aes.h>
  47. #include <linux/module.h>
  48. #include <linux/init.h>
  49. #include <linux/types.h>
  50. #include <linux/errno.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/kernel.h>
  53. #include <asm/byteorder.h>
  54. #include "padlock.h"
  55. #define AES_EXTENDED_KEY_SIZE 64 /* in uint32_t units */
  56. #define AES_EXTENDED_KEY_SIZE_B (AES_EXTENDED_KEY_SIZE * sizeof(uint32_t))
  57. /* Control word. */
  58. struct cword {
  59. unsigned int __attribute__ ((__packed__))
  60. rounds:4,
  61. algo:3,
  62. keygen:1,
  63. interm:1,
  64. encdec:1,
  65. ksize:2;
  66. } __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
  67. /* Whenever making any changes to the following
  68. * structure *make sure* you keep E, d_data
  69. * and cword aligned on 16 Bytes boundaries!!! */
  70. struct aes_ctx {
  71. struct {
  72. struct cword encrypt;
  73. struct cword decrypt;
  74. } cword;
  75. u32 *D;
  76. int key_length;
  77. u32 E[AES_EXTENDED_KEY_SIZE]
  78. __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
  79. u32 d_data[AES_EXTENDED_KEY_SIZE]
  80. __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
  81. };
  82. /* ====== Key management routines ====== */
  83. static inline uint32_t
  84. generic_rotr32 (const uint32_t x, const unsigned bits)
  85. {
  86. const unsigned n = bits % 32;
  87. return (x >> n) | (x << (32 - n));
  88. }
  89. static inline uint32_t
  90. generic_rotl32 (const uint32_t x, const unsigned bits)
  91. {
  92. const unsigned n = bits % 32;
  93. return (x << n) | (x >> (32 - n));
  94. }
  95. #define rotl generic_rotl32
  96. #define rotr generic_rotr32
  97. /*
  98. * #define byte(x, nr) ((unsigned char)((x) >> (nr*8)))
  99. */
  100. static inline uint8_t
  101. byte(const uint32_t x, const unsigned n)
  102. {
  103. return x >> (n << 3);
  104. }
  105. #define E_KEY ctx->E
  106. #define D_KEY ctx->D
  107. static uint8_t pow_tab[256];
  108. static uint8_t log_tab[256];
  109. static uint8_t sbx_tab[256];
  110. static uint8_t isb_tab[256];
  111. static uint32_t rco_tab[10];
  112. static uint32_t ft_tab[4][256];
  113. static uint32_t it_tab[4][256];
  114. static uint32_t fl_tab[4][256];
  115. static uint32_t il_tab[4][256];
  116. static inline uint8_t
  117. f_mult (uint8_t a, uint8_t b)
  118. {
  119. uint8_t aa = log_tab[a], cc = aa + log_tab[b];
  120. return pow_tab[cc + (cc < aa ? 1 : 0)];
  121. }
  122. #define ff_mult(a,b) (a && b ? f_mult(a, b) : 0)
  123. #define f_rn(bo, bi, n, k) \
  124. bo[n] = ft_tab[0][byte(bi[n],0)] ^ \
  125. ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
  126. ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  127. ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
  128. #define i_rn(bo, bi, n, k) \
  129. bo[n] = it_tab[0][byte(bi[n],0)] ^ \
  130. it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
  131. it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  132. it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
  133. #define ls_box(x) \
  134. ( fl_tab[0][byte(x, 0)] ^ \
  135. fl_tab[1][byte(x, 1)] ^ \
  136. fl_tab[2][byte(x, 2)] ^ \
  137. fl_tab[3][byte(x, 3)] )
  138. #define f_rl(bo, bi, n, k) \
  139. bo[n] = fl_tab[0][byte(bi[n],0)] ^ \
  140. fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
  141. fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  142. fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
  143. #define i_rl(bo, bi, n, k) \
  144. bo[n] = il_tab[0][byte(bi[n],0)] ^ \
  145. il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
  146. il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
  147. il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
  148. static void
  149. gen_tabs (void)
  150. {
  151. uint32_t i, t;
  152. uint8_t p, q;
  153. /* log and power tables for GF(2**8) finite field with
  154. 0x011b as modular polynomial - the simplest prmitive
  155. root is 0x03, used here to generate the tables */
  156. for (i = 0, p = 1; i < 256; ++i) {
  157. pow_tab[i] = (uint8_t) p;
  158. log_tab[p] = (uint8_t) i;
  159. p ^= (p << 1) ^ (p & 0x80 ? 0x01b : 0);
  160. }
  161. log_tab[1] = 0;
  162. for (i = 0, p = 1; i < 10; ++i) {
  163. rco_tab[i] = p;
  164. p = (p << 1) ^ (p & 0x80 ? 0x01b : 0);
  165. }
  166. for (i = 0; i < 256; ++i) {
  167. p = (i ? pow_tab[255 - log_tab[i]] : 0);
  168. q = ((p >> 7) | (p << 1)) ^ ((p >> 6) | (p << 2));
  169. p ^= 0x63 ^ q ^ ((q >> 6) | (q << 2));
  170. sbx_tab[i] = p;
  171. isb_tab[p] = (uint8_t) i;
  172. }
  173. for (i = 0; i < 256; ++i) {
  174. p = sbx_tab[i];
  175. t = p;
  176. fl_tab[0][i] = t;
  177. fl_tab[1][i] = rotl (t, 8);
  178. fl_tab[2][i] = rotl (t, 16);
  179. fl_tab[3][i] = rotl (t, 24);
  180. t = ((uint32_t) ff_mult (2, p)) |
  181. ((uint32_t) p << 8) |
  182. ((uint32_t) p << 16) | ((uint32_t) ff_mult (3, p) << 24);
  183. ft_tab[0][i] = t;
  184. ft_tab[1][i] = rotl (t, 8);
  185. ft_tab[2][i] = rotl (t, 16);
  186. ft_tab[3][i] = rotl (t, 24);
  187. p = isb_tab[i];
  188. t = p;
  189. il_tab[0][i] = t;
  190. il_tab[1][i] = rotl (t, 8);
  191. il_tab[2][i] = rotl (t, 16);
  192. il_tab[3][i] = rotl (t, 24);
  193. t = ((uint32_t) ff_mult (14, p)) |
  194. ((uint32_t) ff_mult (9, p) << 8) |
  195. ((uint32_t) ff_mult (13, p) << 16) |
  196. ((uint32_t) ff_mult (11, p) << 24);
  197. it_tab[0][i] = t;
  198. it_tab[1][i] = rotl (t, 8);
  199. it_tab[2][i] = rotl (t, 16);
  200. it_tab[3][i] = rotl (t, 24);
  201. }
  202. }
  203. #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
  204. #define imix_col(y,x) \
  205. u = star_x(x); \
  206. v = star_x(u); \
  207. w = star_x(v); \
  208. t = w ^ (x); \
  209. (y) = u ^ v ^ w; \
  210. (y) ^= rotr(u ^ t, 8) ^ \
  211. rotr(v ^ t, 16) ^ \
  212. rotr(t,24)
  213. /* initialise the key schedule from the user supplied key */
  214. #define loop4(i) \
  215. { t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \
  216. t ^= E_KEY[4 * i]; E_KEY[4 * i + 4] = t; \
  217. t ^= E_KEY[4 * i + 1]; E_KEY[4 * i + 5] = t; \
  218. t ^= E_KEY[4 * i + 2]; E_KEY[4 * i + 6] = t; \
  219. t ^= E_KEY[4 * i + 3]; E_KEY[4 * i + 7] = t; \
  220. }
  221. #define loop6(i) \
  222. { t = rotr(t, 8); t = ls_box(t) ^ rco_tab[i]; \
  223. t ^= E_KEY[6 * i]; E_KEY[6 * i + 6] = t; \
  224. t ^= E_KEY[6 * i + 1]; E_KEY[6 * i + 7] = t; \
  225. t ^= E_KEY[6 * i + 2]; E_KEY[6 * i + 8] = t; \
  226. t ^= E_KEY[6 * i + 3]; E_KEY[6 * i + 9] = t; \
  227. t ^= E_KEY[6 * i + 4]; E_KEY[6 * i + 10] = t; \
  228. t ^= E_KEY[6 * i + 5]; E_KEY[6 * i + 11] = t; \
  229. }
  230. #define loop8(i) \
  231. { t = rotr(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \
  232. t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \
  233. t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \
  234. t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \
  235. t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \
  236. t = E_KEY[8 * i + 4] ^ ls_box(t); \
  237. E_KEY[8 * i + 12] = t; \
  238. t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \
  239. t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \
  240. t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \
  241. }
  242. /* Tells whether the ACE is capable to generate
  243. the extended key for a given key_len. */
  244. static inline int
  245. aes_hw_extkey_available(uint8_t key_len)
  246. {
  247. /* TODO: We should check the actual CPU model/stepping
  248. as it's possible that the capability will be
  249. added in the next CPU revisions. */
  250. if (key_len == 16)
  251. return 1;
  252. return 0;
  253. }
  254. static inline struct aes_ctx *aes_ctx_common(void *ctx)
  255. {
  256. unsigned long addr = (unsigned long)ctx;
  257. unsigned long align = PADLOCK_ALIGNMENT;
  258. if (align <= crypto_tfm_ctx_alignment())
  259. align = 1;
  260. return (struct aes_ctx *)ALIGN(addr, align);
  261. }
  262. static inline struct aes_ctx *aes_ctx(struct crypto_tfm *tfm)
  263. {
  264. return aes_ctx_common(crypto_tfm_ctx(tfm));
  265. }
  266. static inline struct aes_ctx *blk_aes_ctx(struct crypto_blkcipher *tfm)
  267. {
  268. return aes_ctx_common(crypto_blkcipher_ctx(tfm));
  269. }
  270. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  271. unsigned int key_len)
  272. {
  273. struct aes_ctx *ctx = aes_ctx(tfm);
  274. const __le32 *key = (const __le32 *)in_key;
  275. u32 *flags = &tfm->crt_flags;
  276. uint32_t i, t, u, v, w;
  277. uint32_t P[AES_EXTENDED_KEY_SIZE];
  278. uint32_t rounds;
  279. if (key_len % 8) {
  280. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  281. return -EINVAL;
  282. }
  283. ctx->key_length = key_len;
  284. /*
  285. * If the hardware is capable of generating the extended key
  286. * itself we must supply the plain key for both encryption
  287. * and decryption.
  288. */
  289. ctx->D = ctx->E;
  290. E_KEY[0] = le32_to_cpu(key[0]);
  291. E_KEY[1] = le32_to_cpu(key[1]);
  292. E_KEY[2] = le32_to_cpu(key[2]);
  293. E_KEY[3] = le32_to_cpu(key[3]);
  294. /* Prepare control words. */
  295. memset(&ctx->cword, 0, sizeof(ctx->cword));
  296. ctx->cword.decrypt.encdec = 1;
  297. ctx->cword.encrypt.rounds = 10 + (key_len - 16) / 4;
  298. ctx->cword.decrypt.rounds = ctx->cword.encrypt.rounds;
  299. ctx->cword.encrypt.ksize = (key_len - 16) / 8;
  300. ctx->cword.decrypt.ksize = ctx->cword.encrypt.ksize;
  301. /* Don't generate extended keys if the hardware can do it. */
  302. if (aes_hw_extkey_available(key_len))
  303. return 0;
  304. ctx->D = ctx->d_data;
  305. ctx->cword.encrypt.keygen = 1;
  306. ctx->cword.decrypt.keygen = 1;
  307. switch (key_len) {
  308. case 16:
  309. t = E_KEY[3];
  310. for (i = 0; i < 10; ++i)
  311. loop4 (i);
  312. break;
  313. case 24:
  314. E_KEY[4] = le32_to_cpu(key[4]);
  315. t = E_KEY[5] = le32_to_cpu(key[5]);
  316. for (i = 0; i < 8; ++i)
  317. loop6 (i);
  318. break;
  319. case 32:
  320. E_KEY[4] = le32_to_cpu(key[4]);
  321. E_KEY[5] = le32_to_cpu(key[5]);
  322. E_KEY[6] = le32_to_cpu(key[6]);
  323. t = E_KEY[7] = le32_to_cpu(key[7]);
  324. for (i = 0; i < 7; ++i)
  325. loop8 (i);
  326. break;
  327. }
  328. D_KEY[0] = E_KEY[0];
  329. D_KEY[1] = E_KEY[1];
  330. D_KEY[2] = E_KEY[2];
  331. D_KEY[3] = E_KEY[3];
  332. for (i = 4; i < key_len + 24; ++i) {
  333. imix_col (D_KEY[i], E_KEY[i]);
  334. }
  335. /* PadLock needs a different format of the decryption key. */
  336. rounds = 10 + (key_len - 16) / 4;
  337. for (i = 0; i < rounds; i++) {
  338. P[((i + 1) * 4) + 0] = D_KEY[((rounds - i - 1) * 4) + 0];
  339. P[((i + 1) * 4) + 1] = D_KEY[((rounds - i - 1) * 4) + 1];
  340. P[((i + 1) * 4) + 2] = D_KEY[((rounds - i - 1) * 4) + 2];
  341. P[((i + 1) * 4) + 3] = D_KEY[((rounds - i - 1) * 4) + 3];
  342. }
  343. P[0] = E_KEY[(rounds * 4) + 0];
  344. P[1] = E_KEY[(rounds * 4) + 1];
  345. P[2] = E_KEY[(rounds * 4) + 2];
  346. P[3] = E_KEY[(rounds * 4) + 3];
  347. memcpy(D_KEY, P, AES_EXTENDED_KEY_SIZE_B);
  348. return 0;
  349. }
  350. /* ====== Encryption/decryption routines ====== */
  351. /* These are the real call to PadLock. */
  352. static inline void padlock_reset_key(void)
  353. {
  354. asm volatile ("pushfl; popfl");
  355. }
  356. static inline void padlock_xcrypt(const u8 *input, u8 *output, void *key,
  357. void *control_word)
  358. {
  359. asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
  360. : "+S"(input), "+D"(output)
  361. : "d"(control_word), "b"(key), "c"(1));
  362. }
  363. static void aes_crypt_copy(const u8 *in, u8 *out, u32 *key, struct cword *cword)
  364. {
  365. u8 buf[AES_BLOCK_SIZE * 2 + PADLOCK_ALIGNMENT - 1];
  366. u8 *tmp = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
  367. memcpy(tmp, in, AES_BLOCK_SIZE);
  368. padlock_xcrypt(tmp, out, key, cword);
  369. }
  370. static inline void aes_crypt(const u8 *in, u8 *out, u32 *key,
  371. struct cword *cword)
  372. {
  373. /* padlock_xcrypt requires at least two blocks of data. */
  374. if (unlikely(!(((unsigned long)in ^ (PAGE_SIZE - AES_BLOCK_SIZE)) &
  375. (PAGE_SIZE - 1)))) {
  376. aes_crypt_copy(in, out, key, cword);
  377. return;
  378. }
  379. padlock_xcrypt(in, out, key, cword);
  380. }
  381. static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key,
  382. void *control_word, u32 count)
  383. {
  384. if (count == 1) {
  385. aes_crypt(input, output, key, control_word);
  386. return;
  387. }
  388. asm volatile ("test $1, %%cl;"
  389. "je 1f;"
  390. "lea -1(%%ecx), %%eax;"
  391. "mov $1, %%ecx;"
  392. ".byte 0xf3,0x0f,0xa7,0xc8;" /* rep xcryptecb */
  393. "mov %%eax, %%ecx;"
  394. "1:"
  395. ".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
  396. : "+S"(input), "+D"(output)
  397. : "d"(control_word), "b"(key), "c"(count)
  398. : "ax");
  399. }
  400. static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
  401. u8 *iv, void *control_word, u32 count)
  402. {
  403. /* rep xcryptcbc */
  404. asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"
  405. : "+S" (input), "+D" (output), "+a" (iv)
  406. : "d" (control_word), "b" (key), "c" (count));
  407. return iv;
  408. }
  409. static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  410. {
  411. struct aes_ctx *ctx = aes_ctx(tfm);
  412. padlock_reset_key();
  413. aes_crypt(in, out, ctx->E, &ctx->cword.encrypt);
  414. }
  415. static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  416. {
  417. struct aes_ctx *ctx = aes_ctx(tfm);
  418. padlock_reset_key();
  419. aes_crypt(in, out, ctx->D, &ctx->cword.decrypt);
  420. }
  421. static struct crypto_alg aes_alg = {
  422. .cra_name = "aes",
  423. .cra_driver_name = "aes-padlock",
  424. .cra_priority = PADLOCK_CRA_PRIORITY,
  425. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  426. .cra_blocksize = AES_BLOCK_SIZE,
  427. .cra_ctxsize = sizeof(struct aes_ctx),
  428. .cra_alignmask = PADLOCK_ALIGNMENT - 1,
  429. .cra_module = THIS_MODULE,
  430. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  431. .cra_u = {
  432. .cipher = {
  433. .cia_min_keysize = AES_MIN_KEY_SIZE,
  434. .cia_max_keysize = AES_MAX_KEY_SIZE,
  435. .cia_setkey = aes_set_key,
  436. .cia_encrypt = aes_encrypt,
  437. .cia_decrypt = aes_decrypt,
  438. }
  439. }
  440. };
  441. static int ecb_aes_encrypt(struct blkcipher_desc *desc,
  442. struct scatterlist *dst, struct scatterlist *src,
  443. unsigned int nbytes)
  444. {
  445. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  446. struct blkcipher_walk walk;
  447. int err;
  448. padlock_reset_key();
  449. blkcipher_walk_init(&walk, dst, src, nbytes);
  450. err = blkcipher_walk_virt(desc, &walk);
  451. while ((nbytes = walk.nbytes)) {
  452. padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
  453. ctx->E, &ctx->cword.encrypt,
  454. nbytes / AES_BLOCK_SIZE);
  455. nbytes &= AES_BLOCK_SIZE - 1;
  456. err = blkcipher_walk_done(desc, &walk, nbytes);
  457. }
  458. return err;
  459. }
  460. static int ecb_aes_decrypt(struct blkcipher_desc *desc,
  461. struct scatterlist *dst, struct scatterlist *src,
  462. unsigned int nbytes)
  463. {
  464. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  465. struct blkcipher_walk walk;
  466. int err;
  467. padlock_reset_key();
  468. blkcipher_walk_init(&walk, dst, src, nbytes);
  469. err = blkcipher_walk_virt(desc, &walk);
  470. while ((nbytes = walk.nbytes)) {
  471. padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
  472. ctx->D, &ctx->cword.decrypt,
  473. nbytes / AES_BLOCK_SIZE);
  474. nbytes &= AES_BLOCK_SIZE - 1;
  475. err = blkcipher_walk_done(desc, &walk, nbytes);
  476. }
  477. return err;
  478. }
  479. static struct crypto_alg ecb_aes_alg = {
  480. .cra_name = "ecb(aes)",
  481. .cra_driver_name = "ecb-aes-padlock",
  482. .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
  483. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  484. .cra_blocksize = AES_BLOCK_SIZE,
  485. .cra_ctxsize = sizeof(struct aes_ctx),
  486. .cra_alignmask = PADLOCK_ALIGNMENT - 1,
  487. .cra_type = &crypto_blkcipher_type,
  488. .cra_module = THIS_MODULE,
  489. .cra_list = LIST_HEAD_INIT(ecb_aes_alg.cra_list),
  490. .cra_u = {
  491. .blkcipher = {
  492. .min_keysize = AES_MIN_KEY_SIZE,
  493. .max_keysize = AES_MAX_KEY_SIZE,
  494. .setkey = aes_set_key,
  495. .encrypt = ecb_aes_encrypt,
  496. .decrypt = ecb_aes_decrypt,
  497. }
  498. }
  499. };
  500. static int cbc_aes_encrypt(struct blkcipher_desc *desc,
  501. struct scatterlist *dst, struct scatterlist *src,
  502. unsigned int nbytes)
  503. {
  504. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  505. struct blkcipher_walk walk;
  506. int err;
  507. padlock_reset_key();
  508. blkcipher_walk_init(&walk, dst, src, nbytes);
  509. err = blkcipher_walk_virt(desc, &walk);
  510. while ((nbytes = walk.nbytes)) {
  511. u8 *iv = padlock_xcrypt_cbc(walk.src.virt.addr,
  512. walk.dst.virt.addr, ctx->E,
  513. walk.iv, &ctx->cword.encrypt,
  514. nbytes / AES_BLOCK_SIZE);
  515. memcpy(walk.iv, iv, AES_BLOCK_SIZE);
  516. nbytes &= AES_BLOCK_SIZE - 1;
  517. err = blkcipher_walk_done(desc, &walk, nbytes);
  518. }
  519. return err;
  520. }
  521. static int cbc_aes_decrypt(struct blkcipher_desc *desc,
  522. struct scatterlist *dst, struct scatterlist *src,
  523. unsigned int nbytes)
  524. {
  525. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  526. struct blkcipher_walk walk;
  527. int err;
  528. padlock_reset_key();
  529. blkcipher_walk_init(&walk, dst, src, nbytes);
  530. err = blkcipher_walk_virt(desc, &walk);
  531. while ((nbytes = walk.nbytes)) {
  532. padlock_xcrypt_cbc(walk.src.virt.addr, walk.dst.virt.addr,
  533. ctx->D, walk.iv, &ctx->cword.decrypt,
  534. nbytes / AES_BLOCK_SIZE);
  535. nbytes &= AES_BLOCK_SIZE - 1;
  536. err = blkcipher_walk_done(desc, &walk, nbytes);
  537. }
  538. return err;
  539. }
  540. static struct crypto_alg cbc_aes_alg = {
  541. .cra_name = "cbc(aes)",
  542. .cra_driver_name = "cbc-aes-padlock",
  543. .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
  544. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  545. .cra_blocksize = AES_BLOCK_SIZE,
  546. .cra_ctxsize = sizeof(struct aes_ctx),
  547. .cra_alignmask = PADLOCK_ALIGNMENT - 1,
  548. .cra_type = &crypto_blkcipher_type,
  549. .cra_module = THIS_MODULE,
  550. .cra_list = LIST_HEAD_INIT(cbc_aes_alg.cra_list),
  551. .cra_u = {
  552. .blkcipher = {
  553. .min_keysize = AES_MIN_KEY_SIZE,
  554. .max_keysize = AES_MAX_KEY_SIZE,
  555. .ivsize = AES_BLOCK_SIZE,
  556. .setkey = aes_set_key,
  557. .encrypt = cbc_aes_encrypt,
  558. .decrypt = cbc_aes_decrypt,
  559. }
  560. }
  561. };
  562. static int __init padlock_init(void)
  563. {
  564. int ret;
  565. if (!cpu_has_xcrypt) {
  566. printk(KERN_ERR PFX "VIA PadLock not detected.\n");
  567. return -ENODEV;
  568. }
  569. if (!cpu_has_xcrypt_enabled) {
  570. printk(KERN_ERR PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
  571. return -ENODEV;
  572. }
  573. gen_tabs();
  574. if ((ret = crypto_register_alg(&aes_alg)))
  575. goto aes_err;
  576. if ((ret = crypto_register_alg(&ecb_aes_alg)))
  577. goto ecb_aes_err;
  578. if ((ret = crypto_register_alg(&cbc_aes_alg)))
  579. goto cbc_aes_err;
  580. printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n");
  581. out:
  582. return ret;
  583. cbc_aes_err:
  584. crypto_unregister_alg(&ecb_aes_alg);
  585. ecb_aes_err:
  586. crypto_unregister_alg(&aes_alg);
  587. aes_err:
  588. printk(KERN_ERR PFX "VIA PadLock AES initialization failed.\n");
  589. goto out;
  590. }
  591. static void __exit padlock_fini(void)
  592. {
  593. crypto_unregister_alg(&cbc_aes_alg);
  594. crypto_unregister_alg(&ecb_aes_alg);
  595. crypto_unregister_alg(&aes_alg);
  596. }
  597. module_init(padlock_init);
  598. module_exit(padlock_fini);
  599. MODULE_DESCRIPTION("VIA PadLock AES algorithm support");
  600. MODULE_LICENSE("GPL");
  601. MODULE_AUTHOR("Michal Ludvig");
  602. MODULE_ALIAS("aes");