padlock-aes.c 13 KB

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