padlock-aes.c 15 KB

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