padlock-aes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. */
  9. #include <crypto/algapi.h>
  10. #include <crypto/aes.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/types.h>
  14. #include <linux/errno.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/percpu.h>
  18. #include <linux/smp.h>
  19. #include <asm/byteorder.h>
  20. #include <asm/processor.h>
  21. #include <asm/i387.h>
  22. #include "padlock.h"
  23. /*
  24. * Number of data blocks actually fetched for each xcrypt insn.
  25. * Processors with prefetch errata will fetch extra blocks.
  26. */
  27. static unsigned int ecb_fetch_blocks = 2;
  28. #define MAX_ECB_FETCH_BLOCKS (8)
  29. #define ecb_fetch_bytes (ecb_fetch_blocks * AES_BLOCK_SIZE)
  30. static unsigned int cbc_fetch_blocks = 1;
  31. #define MAX_CBC_FETCH_BLOCKS (4)
  32. #define cbc_fetch_bytes (cbc_fetch_blocks * AES_BLOCK_SIZE)
  33. /* Control word. */
  34. struct cword {
  35. unsigned int __attribute__ ((__packed__))
  36. rounds:4,
  37. algo:3,
  38. keygen:1,
  39. interm:1,
  40. encdec:1,
  41. ksize:2;
  42. } __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
  43. /* Whenever making any changes to the following
  44. * structure *make sure* you keep E, d_data
  45. * and cword aligned on 16 Bytes boundaries and
  46. * the Hardware can access 16 * 16 bytes of E and d_data
  47. * (only the first 15 * 16 bytes matter but the HW reads
  48. * more).
  49. */
  50. struct aes_ctx {
  51. u32 E[AES_MAX_KEYLENGTH_U32]
  52. __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
  53. u32 d_data[AES_MAX_KEYLENGTH_U32]
  54. __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
  55. struct {
  56. struct cword encrypt;
  57. struct cword decrypt;
  58. } cword;
  59. u32 *D;
  60. };
  61. static DEFINE_PER_CPU(struct cword *, last_cword);
  62. /* Tells whether the ACE is capable to generate
  63. the extended key for a given key_len. */
  64. static inline int
  65. aes_hw_extkey_available(uint8_t key_len)
  66. {
  67. /* TODO: We should check the actual CPU model/stepping
  68. as it's possible that the capability will be
  69. added in the next CPU revisions. */
  70. if (key_len == 16)
  71. return 1;
  72. return 0;
  73. }
  74. static inline struct aes_ctx *aes_ctx_common(void *ctx)
  75. {
  76. unsigned long addr = (unsigned long)ctx;
  77. unsigned long align = PADLOCK_ALIGNMENT;
  78. if (align <= crypto_tfm_ctx_alignment())
  79. align = 1;
  80. return (struct aes_ctx *)ALIGN(addr, align);
  81. }
  82. static inline struct aes_ctx *aes_ctx(struct crypto_tfm *tfm)
  83. {
  84. return aes_ctx_common(crypto_tfm_ctx(tfm));
  85. }
  86. static inline struct aes_ctx *blk_aes_ctx(struct crypto_blkcipher *tfm)
  87. {
  88. return aes_ctx_common(crypto_blkcipher_ctx(tfm));
  89. }
  90. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  91. unsigned int key_len)
  92. {
  93. struct aes_ctx *ctx = aes_ctx(tfm);
  94. const __le32 *key = (const __le32 *)in_key;
  95. u32 *flags = &tfm->crt_flags;
  96. struct crypto_aes_ctx gen_aes;
  97. int cpu;
  98. if (key_len % 8) {
  99. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  100. return -EINVAL;
  101. }
  102. /*
  103. * If the hardware is capable of generating the extended key
  104. * itself we must supply the plain key for both encryption
  105. * and decryption.
  106. */
  107. ctx->D = ctx->E;
  108. ctx->E[0] = le32_to_cpu(key[0]);
  109. ctx->E[1] = le32_to_cpu(key[1]);
  110. ctx->E[2] = le32_to_cpu(key[2]);
  111. ctx->E[3] = le32_to_cpu(key[3]);
  112. /* Prepare control words. */
  113. memset(&ctx->cword, 0, sizeof(ctx->cword));
  114. ctx->cword.decrypt.encdec = 1;
  115. ctx->cword.encrypt.rounds = 10 + (key_len - 16) / 4;
  116. ctx->cword.decrypt.rounds = ctx->cword.encrypt.rounds;
  117. ctx->cword.encrypt.ksize = (key_len - 16) / 8;
  118. ctx->cword.decrypt.ksize = ctx->cword.encrypt.ksize;
  119. /* Don't generate extended keys if the hardware can do it. */
  120. if (aes_hw_extkey_available(key_len))
  121. goto ok;
  122. ctx->D = ctx->d_data;
  123. ctx->cword.encrypt.keygen = 1;
  124. ctx->cword.decrypt.keygen = 1;
  125. if (crypto_aes_expand_key(&gen_aes, in_key, key_len)) {
  126. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  127. return -EINVAL;
  128. }
  129. memcpy(ctx->E, gen_aes.key_enc, AES_MAX_KEYLENGTH);
  130. memcpy(ctx->D, gen_aes.key_dec, AES_MAX_KEYLENGTH);
  131. ok:
  132. for_each_online_cpu(cpu)
  133. if (&ctx->cword.encrypt == per_cpu(last_cword, cpu) ||
  134. &ctx->cword.decrypt == per_cpu(last_cword, cpu))
  135. per_cpu(last_cword, cpu) = NULL;
  136. return 0;
  137. }
  138. /* ====== Encryption/decryption routines ====== */
  139. /* These are the real call to PadLock. */
  140. static inline void padlock_reset_key(struct cword *cword)
  141. {
  142. int cpu = raw_smp_processor_id();
  143. if (cword != per_cpu(last_cword, cpu))
  144. #ifndef CONFIG_X86_64
  145. asm volatile ("pushfl; popfl");
  146. #else
  147. asm volatile ("pushfq; popfq");
  148. #endif
  149. }
  150. static inline void padlock_store_cword(struct cword *cword)
  151. {
  152. per_cpu(last_cword, raw_smp_processor_id()) = cword;
  153. }
  154. /*
  155. * While the padlock instructions don't use FP/SSE registers, they
  156. * generate a spurious DNA fault when cr0.ts is '1'. These instructions
  157. * should be used only inside the irq_ts_save/restore() context
  158. */
  159. static inline void rep_xcrypt_ecb(const u8 *input, u8 *output, void *key,
  160. struct cword *control_word, int count)
  161. {
  162. asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
  163. : "+S"(input), "+D"(output)
  164. : "d"(control_word), "b"(key), "c"(count));
  165. }
  166. static inline u8 *rep_xcrypt_cbc(const u8 *input, u8 *output, void *key,
  167. u8 *iv, struct cword *control_word, int count)
  168. {
  169. asm volatile (".byte 0xf3,0x0f,0xa7,0xd0" /* rep xcryptcbc */
  170. : "+S" (input), "+D" (output), "+a" (iv)
  171. : "d" (control_word), "b" (key), "c" (count));
  172. return iv;
  173. }
  174. static void ecb_crypt_copy(const u8 *in, u8 *out, u32 *key,
  175. struct cword *cword, int count)
  176. {
  177. /*
  178. * Padlock prefetches extra data so we must provide mapped input buffers.
  179. * Assume there are at least 16 bytes of stack already in use.
  180. */
  181. u8 buf[AES_BLOCK_SIZE * (MAX_ECB_FETCH_BLOCKS - 1) + PADLOCK_ALIGNMENT - 1];
  182. u8 *tmp = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
  183. memcpy(tmp, in, count * AES_BLOCK_SIZE);
  184. rep_xcrypt_ecb(tmp, out, key, cword, count);
  185. }
  186. static u8 *cbc_crypt_copy(const u8 *in, u8 *out, u32 *key,
  187. u8 *iv, struct cword *cword, int count)
  188. {
  189. /*
  190. * Padlock prefetches extra data so we must provide mapped input buffers.
  191. * Assume there are at least 16 bytes of stack already in use.
  192. */
  193. u8 buf[AES_BLOCK_SIZE * (MAX_CBC_FETCH_BLOCKS - 1) + PADLOCK_ALIGNMENT - 1];
  194. u8 *tmp = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
  195. memcpy(tmp, in, count * AES_BLOCK_SIZE);
  196. return rep_xcrypt_cbc(tmp, out, key, iv, cword, count);
  197. }
  198. static inline void ecb_crypt(const u8 *in, u8 *out, u32 *key,
  199. struct cword *cword, int count)
  200. {
  201. /* Padlock in ECB mode fetches at least ecb_fetch_bytes of data.
  202. * We could avoid some copying here but it's probably not worth it.
  203. */
  204. if (unlikely(((unsigned long)in & PAGE_SIZE) + ecb_fetch_bytes > PAGE_SIZE)) {
  205. ecb_crypt_copy(in, out, key, cword, count);
  206. return;
  207. }
  208. rep_xcrypt_ecb(in, out, key, cword, count);
  209. }
  210. static inline u8 *cbc_crypt(const u8 *in, u8 *out, u32 *key,
  211. u8 *iv, struct cword *cword, int count)
  212. {
  213. /* Padlock in CBC mode fetches at least cbc_fetch_bytes of data. */
  214. if (unlikely(((unsigned long)in & PAGE_SIZE) + cbc_fetch_bytes > PAGE_SIZE))
  215. return cbc_crypt_copy(in, out, key, iv, cword, count);
  216. return rep_xcrypt_cbc(in, out, key, iv, cword, count);
  217. }
  218. static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key,
  219. void *control_word, u32 count)
  220. {
  221. u32 initial = count & (ecb_fetch_blocks - 1);
  222. if (count < ecb_fetch_blocks) {
  223. ecb_crypt(input, output, key, control_word, count);
  224. return;
  225. }
  226. if (initial)
  227. asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
  228. : "+S"(input), "+D"(output)
  229. : "d"(control_word), "b"(key), "c"(initial));
  230. asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
  231. : "+S"(input), "+D"(output)
  232. : "d"(control_word), "b"(key), "c"(count - initial));
  233. }
  234. static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
  235. u8 *iv, void *control_word, u32 count)
  236. {
  237. u32 initial = count & (cbc_fetch_blocks - 1);
  238. if (count < cbc_fetch_blocks)
  239. return cbc_crypt(input, output, key, iv, control_word, count);
  240. if (initial)
  241. asm volatile (".byte 0xf3,0x0f,0xa7,0xd0" /* rep xcryptcbc */
  242. : "+S" (input), "+D" (output), "+a" (iv)
  243. : "d" (control_word), "b" (key), "c" (count));
  244. asm volatile (".byte 0xf3,0x0f,0xa7,0xd0" /* rep xcryptcbc */
  245. : "+S" (input), "+D" (output), "+a" (iv)
  246. : "d" (control_word), "b" (key), "c" (count-initial));
  247. return iv;
  248. }
  249. static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  250. {
  251. struct aes_ctx *ctx = aes_ctx(tfm);
  252. int ts_state;
  253. padlock_reset_key(&ctx->cword.encrypt);
  254. ts_state = irq_ts_save();
  255. ecb_crypt(in, out, ctx->E, &ctx->cword.encrypt, 1);
  256. irq_ts_restore(ts_state);
  257. padlock_store_cword(&ctx->cword.encrypt);
  258. }
  259. static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  260. {
  261. struct aes_ctx *ctx = aes_ctx(tfm);
  262. int ts_state;
  263. padlock_reset_key(&ctx->cword.encrypt);
  264. ts_state = irq_ts_save();
  265. ecb_crypt(in, out, ctx->D, &ctx->cword.decrypt, 1);
  266. irq_ts_restore(ts_state);
  267. padlock_store_cword(&ctx->cword.encrypt);
  268. }
  269. static struct crypto_alg aes_alg = {
  270. .cra_name = "aes",
  271. .cra_driver_name = "aes-padlock",
  272. .cra_priority = PADLOCK_CRA_PRIORITY,
  273. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  274. .cra_blocksize = AES_BLOCK_SIZE,
  275. .cra_ctxsize = sizeof(struct aes_ctx),
  276. .cra_alignmask = PADLOCK_ALIGNMENT - 1,
  277. .cra_module = THIS_MODULE,
  278. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  279. .cra_u = {
  280. .cipher = {
  281. .cia_min_keysize = AES_MIN_KEY_SIZE,
  282. .cia_max_keysize = AES_MAX_KEY_SIZE,
  283. .cia_setkey = aes_set_key,
  284. .cia_encrypt = aes_encrypt,
  285. .cia_decrypt = aes_decrypt,
  286. }
  287. }
  288. };
  289. static int ecb_aes_encrypt(struct blkcipher_desc *desc,
  290. struct scatterlist *dst, struct scatterlist *src,
  291. unsigned int nbytes)
  292. {
  293. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  294. struct blkcipher_walk walk;
  295. int err;
  296. int ts_state;
  297. padlock_reset_key(&ctx->cword.encrypt);
  298. blkcipher_walk_init(&walk, dst, src, nbytes);
  299. err = blkcipher_walk_virt(desc, &walk);
  300. ts_state = irq_ts_save();
  301. while ((nbytes = walk.nbytes)) {
  302. padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
  303. ctx->E, &ctx->cword.encrypt,
  304. nbytes / AES_BLOCK_SIZE);
  305. nbytes &= AES_BLOCK_SIZE - 1;
  306. err = blkcipher_walk_done(desc, &walk, nbytes);
  307. }
  308. irq_ts_restore(ts_state);
  309. padlock_store_cword(&ctx->cword.encrypt);
  310. return err;
  311. }
  312. static int ecb_aes_decrypt(struct blkcipher_desc *desc,
  313. struct scatterlist *dst, struct scatterlist *src,
  314. unsigned int nbytes)
  315. {
  316. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  317. struct blkcipher_walk walk;
  318. int err;
  319. int ts_state;
  320. padlock_reset_key(&ctx->cword.decrypt);
  321. blkcipher_walk_init(&walk, dst, src, nbytes);
  322. err = blkcipher_walk_virt(desc, &walk);
  323. ts_state = irq_ts_save();
  324. while ((nbytes = walk.nbytes)) {
  325. padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
  326. ctx->D, &ctx->cword.decrypt,
  327. nbytes / AES_BLOCK_SIZE);
  328. nbytes &= AES_BLOCK_SIZE - 1;
  329. err = blkcipher_walk_done(desc, &walk, nbytes);
  330. }
  331. irq_ts_restore(ts_state);
  332. padlock_store_cword(&ctx->cword.encrypt);
  333. return err;
  334. }
  335. static struct crypto_alg ecb_aes_alg = {
  336. .cra_name = "ecb(aes)",
  337. .cra_driver_name = "ecb-aes-padlock",
  338. .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
  339. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  340. .cra_blocksize = AES_BLOCK_SIZE,
  341. .cra_ctxsize = sizeof(struct aes_ctx),
  342. .cra_alignmask = PADLOCK_ALIGNMENT - 1,
  343. .cra_type = &crypto_blkcipher_type,
  344. .cra_module = THIS_MODULE,
  345. .cra_list = LIST_HEAD_INIT(ecb_aes_alg.cra_list),
  346. .cra_u = {
  347. .blkcipher = {
  348. .min_keysize = AES_MIN_KEY_SIZE,
  349. .max_keysize = AES_MAX_KEY_SIZE,
  350. .setkey = aes_set_key,
  351. .encrypt = ecb_aes_encrypt,
  352. .decrypt = ecb_aes_decrypt,
  353. }
  354. }
  355. };
  356. static int cbc_aes_encrypt(struct blkcipher_desc *desc,
  357. struct scatterlist *dst, struct scatterlist *src,
  358. unsigned int nbytes)
  359. {
  360. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  361. struct blkcipher_walk walk;
  362. int err;
  363. int ts_state;
  364. padlock_reset_key(&ctx->cword.encrypt);
  365. blkcipher_walk_init(&walk, dst, src, nbytes);
  366. err = blkcipher_walk_virt(desc, &walk);
  367. ts_state = irq_ts_save();
  368. while ((nbytes = walk.nbytes)) {
  369. u8 *iv = padlock_xcrypt_cbc(walk.src.virt.addr,
  370. walk.dst.virt.addr, ctx->E,
  371. walk.iv, &ctx->cword.encrypt,
  372. nbytes / AES_BLOCK_SIZE);
  373. memcpy(walk.iv, iv, AES_BLOCK_SIZE);
  374. nbytes &= AES_BLOCK_SIZE - 1;
  375. err = blkcipher_walk_done(desc, &walk, nbytes);
  376. }
  377. irq_ts_restore(ts_state);
  378. padlock_store_cword(&ctx->cword.decrypt);
  379. return err;
  380. }
  381. static int cbc_aes_decrypt(struct blkcipher_desc *desc,
  382. struct scatterlist *dst, struct scatterlist *src,
  383. unsigned int nbytes)
  384. {
  385. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  386. struct blkcipher_walk walk;
  387. int err;
  388. int ts_state;
  389. padlock_reset_key(&ctx->cword.encrypt);
  390. blkcipher_walk_init(&walk, dst, src, nbytes);
  391. err = blkcipher_walk_virt(desc, &walk);
  392. ts_state = irq_ts_save();
  393. while ((nbytes = walk.nbytes)) {
  394. padlock_xcrypt_cbc(walk.src.virt.addr, walk.dst.virt.addr,
  395. ctx->D, walk.iv, &ctx->cword.decrypt,
  396. nbytes / AES_BLOCK_SIZE);
  397. nbytes &= AES_BLOCK_SIZE - 1;
  398. err = blkcipher_walk_done(desc, &walk, nbytes);
  399. }
  400. irq_ts_restore(ts_state);
  401. padlock_store_cword(&ctx->cword.encrypt);
  402. return err;
  403. }
  404. static struct crypto_alg cbc_aes_alg = {
  405. .cra_name = "cbc(aes)",
  406. .cra_driver_name = "cbc-aes-padlock",
  407. .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
  408. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  409. .cra_blocksize = AES_BLOCK_SIZE,
  410. .cra_ctxsize = sizeof(struct aes_ctx),
  411. .cra_alignmask = PADLOCK_ALIGNMENT - 1,
  412. .cra_type = &crypto_blkcipher_type,
  413. .cra_module = THIS_MODULE,
  414. .cra_list = LIST_HEAD_INIT(cbc_aes_alg.cra_list),
  415. .cra_u = {
  416. .blkcipher = {
  417. .min_keysize = AES_MIN_KEY_SIZE,
  418. .max_keysize = AES_MAX_KEY_SIZE,
  419. .ivsize = AES_BLOCK_SIZE,
  420. .setkey = aes_set_key,
  421. .encrypt = cbc_aes_encrypt,
  422. .decrypt = cbc_aes_decrypt,
  423. }
  424. }
  425. };
  426. static int __init padlock_init(void)
  427. {
  428. int ret;
  429. struct cpuinfo_x86 *c = &cpu_data(0);
  430. if (!cpu_has_xcrypt) {
  431. printk(KERN_NOTICE PFX "VIA PadLock not detected.\n");
  432. return -ENODEV;
  433. }
  434. if (!cpu_has_xcrypt_enabled) {
  435. printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
  436. return -ENODEV;
  437. }
  438. if ((ret = crypto_register_alg(&aes_alg)))
  439. goto aes_err;
  440. if ((ret = crypto_register_alg(&ecb_aes_alg)))
  441. goto ecb_aes_err;
  442. if ((ret = crypto_register_alg(&cbc_aes_alg)))
  443. goto cbc_aes_err;
  444. printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n");
  445. if (c->x86 == 6 && c->x86_model == 15 && c->x86_mask == 2) {
  446. ecb_fetch_blocks = MAX_ECB_FETCH_BLOCKS;
  447. cbc_fetch_blocks = MAX_CBC_FETCH_BLOCKS;
  448. printk(KERN_NOTICE PFX "VIA Nano stepping 2 detected: enabling workaround.\n");
  449. }
  450. out:
  451. return ret;
  452. cbc_aes_err:
  453. crypto_unregister_alg(&ecb_aes_alg);
  454. ecb_aes_err:
  455. crypto_unregister_alg(&aes_alg);
  456. aes_err:
  457. printk(KERN_ERR PFX "VIA PadLock AES initialization failed.\n");
  458. goto out;
  459. }
  460. static void __exit padlock_fini(void)
  461. {
  462. crypto_unregister_alg(&cbc_aes_alg);
  463. crypto_unregister_alg(&ecb_aes_alg);
  464. crypto_unregister_alg(&aes_alg);
  465. }
  466. module_init(padlock_init);
  467. module_exit(padlock_fini);
  468. MODULE_DESCRIPTION("VIA PadLock AES algorithm support");
  469. MODULE_LICENSE("GPL");
  470. MODULE_AUTHOR("Michal Ludvig");
  471. MODULE_ALIAS("aes");