padlock-aes.c 15 KB

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