padlock-aes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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'. Fortunately,
  159. * the kernel doesn't use CR0.TS.
  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. count -= initial;
  229. if (initial)
  230. asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
  231. : "+S"(input), "+D"(output)
  232. : "d"(control_word), "b"(key), "c"(initial));
  233. asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
  234. : "+S"(input), "+D"(output)
  235. : "d"(control_word), "b"(key), "c"(count));
  236. }
  237. static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
  238. u8 *iv, void *control_word, u32 count)
  239. {
  240. u32 initial = count & (cbc_fetch_blocks - 1);
  241. if (count < cbc_fetch_blocks)
  242. return cbc_crypt(input, output, key, iv, control_word, count);
  243. count -= initial;
  244. if (initial)
  245. asm volatile (".byte 0xf3,0x0f,0xa7,0xd0" /* rep xcryptcbc */
  246. : "+S" (input), "+D" (output), "+a" (iv)
  247. : "d" (control_word), "b" (key), "c" (initial));
  248. asm volatile (".byte 0xf3,0x0f,0xa7,0xd0" /* rep xcryptcbc */
  249. : "+S" (input), "+D" (output), "+a" (iv)
  250. : "d" (control_word), "b" (key), "c" (count));
  251. return iv;
  252. }
  253. static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  254. {
  255. struct aes_ctx *ctx = aes_ctx(tfm);
  256. padlock_reset_key(&ctx->cword.encrypt);
  257. ecb_crypt(in, out, ctx->E, &ctx->cword.encrypt, 1);
  258. padlock_store_cword(&ctx->cword.encrypt);
  259. }
  260. static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  261. {
  262. struct aes_ctx *ctx = aes_ctx(tfm);
  263. padlock_reset_key(&ctx->cword.encrypt);
  264. ecb_crypt(in, out, ctx->D, &ctx->cword.decrypt, 1);
  265. padlock_store_cword(&ctx->cword.encrypt);
  266. }
  267. static struct crypto_alg aes_alg = {
  268. .cra_name = "aes",
  269. .cra_driver_name = "aes-padlock",
  270. .cra_priority = PADLOCK_CRA_PRIORITY,
  271. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  272. .cra_blocksize = AES_BLOCK_SIZE,
  273. .cra_ctxsize = sizeof(struct aes_ctx),
  274. .cra_alignmask = PADLOCK_ALIGNMENT - 1,
  275. .cra_module = THIS_MODULE,
  276. .cra_u = {
  277. .cipher = {
  278. .cia_min_keysize = AES_MIN_KEY_SIZE,
  279. .cia_max_keysize = AES_MAX_KEY_SIZE,
  280. .cia_setkey = aes_set_key,
  281. .cia_encrypt = aes_encrypt,
  282. .cia_decrypt = aes_decrypt,
  283. }
  284. }
  285. };
  286. static int ecb_aes_encrypt(struct blkcipher_desc *desc,
  287. struct scatterlist *dst, struct scatterlist *src,
  288. unsigned int nbytes)
  289. {
  290. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  291. struct blkcipher_walk walk;
  292. int err;
  293. padlock_reset_key(&ctx->cword.encrypt);
  294. blkcipher_walk_init(&walk, dst, src, nbytes);
  295. err = blkcipher_walk_virt(desc, &walk);
  296. while ((nbytes = walk.nbytes)) {
  297. padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
  298. ctx->E, &ctx->cword.encrypt,
  299. nbytes / AES_BLOCK_SIZE);
  300. nbytes &= AES_BLOCK_SIZE - 1;
  301. err = blkcipher_walk_done(desc, &walk, nbytes);
  302. }
  303. padlock_store_cword(&ctx->cword.encrypt);
  304. return err;
  305. }
  306. static int ecb_aes_decrypt(struct blkcipher_desc *desc,
  307. struct scatterlist *dst, struct scatterlist *src,
  308. unsigned int nbytes)
  309. {
  310. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  311. struct blkcipher_walk walk;
  312. int err;
  313. padlock_reset_key(&ctx->cword.decrypt);
  314. blkcipher_walk_init(&walk, dst, src, nbytes);
  315. err = blkcipher_walk_virt(desc, &walk);
  316. while ((nbytes = walk.nbytes)) {
  317. padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
  318. ctx->D, &ctx->cword.decrypt,
  319. nbytes / AES_BLOCK_SIZE);
  320. nbytes &= AES_BLOCK_SIZE - 1;
  321. err = blkcipher_walk_done(desc, &walk, nbytes);
  322. }
  323. padlock_store_cword(&ctx->cword.encrypt);
  324. return err;
  325. }
  326. static struct crypto_alg ecb_aes_alg = {
  327. .cra_name = "ecb(aes)",
  328. .cra_driver_name = "ecb-aes-padlock",
  329. .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
  330. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  331. .cra_blocksize = AES_BLOCK_SIZE,
  332. .cra_ctxsize = sizeof(struct aes_ctx),
  333. .cra_alignmask = PADLOCK_ALIGNMENT - 1,
  334. .cra_type = &crypto_blkcipher_type,
  335. .cra_module = THIS_MODULE,
  336. .cra_u = {
  337. .blkcipher = {
  338. .min_keysize = AES_MIN_KEY_SIZE,
  339. .max_keysize = AES_MAX_KEY_SIZE,
  340. .setkey = aes_set_key,
  341. .encrypt = ecb_aes_encrypt,
  342. .decrypt = ecb_aes_decrypt,
  343. }
  344. }
  345. };
  346. static int cbc_aes_encrypt(struct blkcipher_desc *desc,
  347. struct scatterlist *dst, struct scatterlist *src,
  348. unsigned int nbytes)
  349. {
  350. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  351. struct blkcipher_walk walk;
  352. int err;
  353. padlock_reset_key(&ctx->cword.encrypt);
  354. blkcipher_walk_init(&walk, dst, src, nbytes);
  355. err = blkcipher_walk_virt(desc, &walk);
  356. while ((nbytes = walk.nbytes)) {
  357. u8 *iv = padlock_xcrypt_cbc(walk.src.virt.addr,
  358. walk.dst.virt.addr, ctx->E,
  359. walk.iv, &ctx->cword.encrypt,
  360. nbytes / AES_BLOCK_SIZE);
  361. memcpy(walk.iv, iv, AES_BLOCK_SIZE);
  362. nbytes &= AES_BLOCK_SIZE - 1;
  363. err = blkcipher_walk_done(desc, &walk, nbytes);
  364. }
  365. padlock_store_cword(&ctx->cword.decrypt);
  366. return err;
  367. }
  368. static int cbc_aes_decrypt(struct blkcipher_desc *desc,
  369. struct scatterlist *dst, struct scatterlist *src,
  370. unsigned int nbytes)
  371. {
  372. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  373. struct blkcipher_walk walk;
  374. int err;
  375. padlock_reset_key(&ctx->cword.encrypt);
  376. blkcipher_walk_init(&walk, dst, src, nbytes);
  377. err = blkcipher_walk_virt(desc, &walk);
  378. while ((nbytes = walk.nbytes)) {
  379. padlock_xcrypt_cbc(walk.src.virt.addr, walk.dst.virt.addr,
  380. ctx->D, walk.iv, &ctx->cword.decrypt,
  381. nbytes / AES_BLOCK_SIZE);
  382. nbytes &= AES_BLOCK_SIZE - 1;
  383. err = blkcipher_walk_done(desc, &walk, nbytes);
  384. }
  385. padlock_store_cword(&ctx->cword.encrypt);
  386. return err;
  387. }
  388. static struct crypto_alg cbc_aes_alg = {
  389. .cra_name = "cbc(aes)",
  390. .cra_driver_name = "cbc-aes-padlock",
  391. .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
  392. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  393. .cra_blocksize = AES_BLOCK_SIZE,
  394. .cra_ctxsize = sizeof(struct aes_ctx),
  395. .cra_alignmask = PADLOCK_ALIGNMENT - 1,
  396. .cra_type = &crypto_blkcipher_type,
  397. .cra_module = THIS_MODULE,
  398. .cra_u = {
  399. .blkcipher = {
  400. .min_keysize = AES_MIN_KEY_SIZE,
  401. .max_keysize = AES_MAX_KEY_SIZE,
  402. .ivsize = AES_BLOCK_SIZE,
  403. .setkey = aes_set_key,
  404. .encrypt = cbc_aes_encrypt,
  405. .decrypt = cbc_aes_decrypt,
  406. }
  407. }
  408. };
  409. static const struct x86_cpu_id padlock_cpu_id[] = {
  410. X86_FEATURE_MATCH(X86_FEATURE_XCRYPT),
  411. {}
  412. };
  413. MODULE_DEVICE_TABLE(x86cpu, padlock_cpu_id);
  414. static int __init padlock_init(void)
  415. {
  416. int ret;
  417. struct cpuinfo_x86 *c = &cpu_data(0);
  418. if (!x86_match_cpu(padlock_cpu_id))
  419. return -ENODEV;
  420. if (!boot_cpu_has(X86_FEATURE_XCRYPT_EN)) {
  421. printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
  422. return -ENODEV;
  423. }
  424. if ((ret = crypto_register_alg(&aes_alg)))
  425. goto aes_err;
  426. if ((ret = crypto_register_alg(&ecb_aes_alg)))
  427. goto ecb_aes_err;
  428. if ((ret = crypto_register_alg(&cbc_aes_alg)))
  429. goto cbc_aes_err;
  430. printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n");
  431. if (c->x86 == 6 && c->x86_model == 15 && c->x86_stepping == 2) {
  432. ecb_fetch_blocks = MAX_ECB_FETCH_BLOCKS;
  433. cbc_fetch_blocks = MAX_CBC_FETCH_BLOCKS;
  434. printk(KERN_NOTICE PFX "VIA Nano stepping 2 detected: enabling workaround.\n");
  435. }
  436. out:
  437. return ret;
  438. cbc_aes_err:
  439. crypto_unregister_alg(&ecb_aes_alg);
  440. ecb_aes_err:
  441. crypto_unregister_alg(&aes_alg);
  442. aes_err:
  443. printk(KERN_ERR PFX "VIA PadLock AES initialization failed.\n");
  444. goto out;
  445. }
  446. static void __exit padlock_fini(void)
  447. {
  448. crypto_unregister_alg(&cbc_aes_alg);
  449. crypto_unregister_alg(&ecb_aes_alg);
  450. crypto_unregister_alg(&aes_alg);
  451. }
  452. module_init(padlock_init);
  453. module_exit(padlock_fini);
  454. MODULE_DESCRIPTION("VIA PadLock AES algorithm support");
  455. MODULE_LICENSE("GPL");
  456. MODULE_AUTHOR("Michal Ludvig");
  457. MODULE_ALIAS_CRYPTO("aes");