padlock-aes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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/i387.h>
  21. #include "padlock.h"
  22. /* Control word. */
  23. struct cword {
  24. unsigned int __attribute__ ((__packed__))
  25. rounds:4,
  26. algo:3,
  27. keygen:1,
  28. interm:1,
  29. encdec:1,
  30. ksize:2;
  31. } __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
  32. /* Whenever making any changes to the following
  33. * structure *make sure* you keep E, d_data
  34. * and cword aligned on 16 Bytes boundaries and
  35. * the Hardware can access 16 * 16 bytes of E and d_data
  36. * (only the first 15 * 16 bytes matter but the HW reads
  37. * more).
  38. */
  39. struct aes_ctx {
  40. u32 E[AES_MAX_KEYLENGTH_U32]
  41. __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
  42. u32 d_data[AES_MAX_KEYLENGTH_U32]
  43. __attribute__ ((__aligned__(PADLOCK_ALIGNMENT)));
  44. struct {
  45. struct cword encrypt;
  46. struct cword decrypt;
  47. } cword;
  48. u32 *D;
  49. };
  50. static DEFINE_PER_CPU(struct cword *, last_cword);
  51. /* Tells whether the ACE is capable to generate
  52. the extended key for a given key_len. */
  53. static inline int
  54. aes_hw_extkey_available(uint8_t key_len)
  55. {
  56. /* TODO: We should check the actual CPU model/stepping
  57. as it's possible that the capability will be
  58. added in the next CPU revisions. */
  59. if (key_len == 16)
  60. return 1;
  61. return 0;
  62. }
  63. static inline struct aes_ctx *aes_ctx_common(void *ctx)
  64. {
  65. unsigned long addr = (unsigned long)ctx;
  66. unsigned long align = PADLOCK_ALIGNMENT;
  67. if (align <= crypto_tfm_ctx_alignment())
  68. align = 1;
  69. return (struct aes_ctx *)ALIGN(addr, align);
  70. }
  71. static inline struct aes_ctx *aes_ctx(struct crypto_tfm *tfm)
  72. {
  73. return aes_ctx_common(crypto_tfm_ctx(tfm));
  74. }
  75. static inline struct aes_ctx *blk_aes_ctx(struct crypto_blkcipher *tfm)
  76. {
  77. return aes_ctx_common(crypto_blkcipher_ctx(tfm));
  78. }
  79. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  80. unsigned int key_len)
  81. {
  82. struct aes_ctx *ctx = aes_ctx(tfm);
  83. const __le32 *key = (const __le32 *)in_key;
  84. u32 *flags = &tfm->crt_flags;
  85. struct crypto_aes_ctx gen_aes;
  86. int cpu;
  87. if (key_len % 8) {
  88. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  89. return -EINVAL;
  90. }
  91. /*
  92. * If the hardware is capable of generating the extended key
  93. * itself we must supply the plain key for both encryption
  94. * and decryption.
  95. */
  96. ctx->D = ctx->E;
  97. ctx->E[0] = le32_to_cpu(key[0]);
  98. ctx->E[1] = le32_to_cpu(key[1]);
  99. ctx->E[2] = le32_to_cpu(key[2]);
  100. ctx->E[3] = le32_to_cpu(key[3]);
  101. /* Prepare control words. */
  102. memset(&ctx->cword, 0, sizeof(ctx->cword));
  103. ctx->cword.decrypt.encdec = 1;
  104. ctx->cword.encrypt.rounds = 10 + (key_len - 16) / 4;
  105. ctx->cword.decrypt.rounds = ctx->cword.encrypt.rounds;
  106. ctx->cword.encrypt.ksize = (key_len - 16) / 8;
  107. ctx->cword.decrypt.ksize = ctx->cword.encrypt.ksize;
  108. /* Don't generate extended keys if the hardware can do it. */
  109. if (aes_hw_extkey_available(key_len))
  110. goto ok;
  111. ctx->D = ctx->d_data;
  112. ctx->cword.encrypt.keygen = 1;
  113. ctx->cword.decrypt.keygen = 1;
  114. if (crypto_aes_expand_key(&gen_aes, in_key, key_len)) {
  115. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  116. return -EINVAL;
  117. }
  118. memcpy(ctx->E, gen_aes.key_enc, AES_MAX_KEYLENGTH);
  119. memcpy(ctx->D, gen_aes.key_dec, AES_MAX_KEYLENGTH);
  120. ok:
  121. for_each_online_cpu(cpu)
  122. if (&ctx->cword.encrypt == per_cpu(last_cword, cpu) ||
  123. &ctx->cword.decrypt == per_cpu(last_cword, cpu))
  124. per_cpu(last_cword, cpu) = NULL;
  125. return 0;
  126. }
  127. /* ====== Encryption/decryption routines ====== */
  128. /* These are the real call to PadLock. */
  129. static inline void padlock_reset_key(struct cword *cword)
  130. {
  131. int cpu = raw_smp_processor_id();
  132. if (cword != per_cpu(last_cword, cpu))
  133. #ifndef CONFIG_X86_64
  134. asm volatile ("pushfl; popfl");
  135. #else
  136. asm volatile ("pushfq; popfq");
  137. #endif
  138. }
  139. static inline void padlock_store_cword(struct cword *cword)
  140. {
  141. per_cpu(last_cword, raw_smp_processor_id()) = cword;
  142. }
  143. /*
  144. * While the padlock instructions don't use FP/SSE registers, they
  145. * generate a spurious DNA fault when cr0.ts is '1'. These instructions
  146. * should be used only inside the irq_ts_save/restore() context
  147. */
  148. static inline void padlock_xcrypt(const u8 *input, u8 *output, void *key,
  149. struct cword *control_word)
  150. {
  151. asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
  152. : "+S"(input), "+D"(output)
  153. : "d"(control_word), "b"(key), "c"(1));
  154. }
  155. static void aes_crypt_copy(const u8 *in, u8 *out, u32 *key, struct cword *cword)
  156. {
  157. u8 buf[AES_BLOCK_SIZE * 2 + PADLOCK_ALIGNMENT - 1];
  158. u8 *tmp = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
  159. memcpy(tmp, in, AES_BLOCK_SIZE);
  160. padlock_xcrypt(tmp, out, key, cword);
  161. }
  162. static inline void aes_crypt(const u8 *in, u8 *out, u32 *key,
  163. struct cword *cword)
  164. {
  165. /* padlock_xcrypt requires at least two blocks of data. */
  166. if (unlikely(!(((unsigned long)in ^ (PAGE_SIZE - AES_BLOCK_SIZE)) &
  167. (PAGE_SIZE - 1)))) {
  168. aes_crypt_copy(in, out, key, cword);
  169. return;
  170. }
  171. padlock_xcrypt(in, out, key, cword);
  172. }
  173. static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key,
  174. void *control_word, u32 count)
  175. {
  176. if (count == 1) {
  177. aes_crypt(input, output, key, control_word);
  178. return;
  179. }
  180. asm volatile ("test $1, %%cl;"
  181. "je 1f;"
  182. #ifndef CONFIG_X86_64
  183. "lea -1(%%ecx), %%eax;"
  184. "mov $1, %%ecx;"
  185. #else
  186. "lea -1(%%rcx), %%rax;"
  187. "mov $1, %%rcx;"
  188. #endif
  189. ".byte 0xf3,0x0f,0xa7,0xc8;" /* rep xcryptecb */
  190. #ifndef CONFIG_X86_64
  191. "mov %%eax, %%ecx;"
  192. #else
  193. "mov %%rax, %%rcx;"
  194. #endif
  195. "1:"
  196. ".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
  197. : "+S"(input), "+D"(output)
  198. : "d"(control_word), "b"(key), "c"(count)
  199. : "ax");
  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);
  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);
  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. if (!cpu_has_xcrypt) {
  391. printk(KERN_NOTICE PFX "VIA PadLock not detected.\n");
  392. return -ENODEV;
  393. }
  394. if (!cpu_has_xcrypt_enabled) {
  395. printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
  396. return -ENODEV;
  397. }
  398. if ((ret = crypto_register_alg(&aes_alg)))
  399. goto aes_err;
  400. if ((ret = crypto_register_alg(&ecb_aes_alg)))
  401. goto ecb_aes_err;
  402. if ((ret = crypto_register_alg(&cbc_aes_alg)))
  403. goto cbc_aes_err;
  404. printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n");
  405. out:
  406. return ret;
  407. cbc_aes_err:
  408. crypto_unregister_alg(&ecb_aes_alg);
  409. ecb_aes_err:
  410. crypto_unregister_alg(&aes_alg);
  411. aes_err:
  412. printk(KERN_ERR PFX "VIA PadLock AES initialization failed.\n");
  413. goto out;
  414. }
  415. static void __exit padlock_fini(void)
  416. {
  417. crypto_unregister_alg(&cbc_aes_alg);
  418. crypto_unregister_alg(&ecb_aes_alg);
  419. crypto_unregister_alg(&aes_alg);
  420. }
  421. module_init(padlock_init);
  422. module_exit(padlock_fini);
  423. MODULE_DESCRIPTION("VIA PadLock AES algorithm support");
  424. MODULE_LICENSE("GPL");
  425. MODULE_AUTHOR("Michal Ludvig");
  426. MODULE_ALIAS("aes");