padlock-aes.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. asm volatile ("pushfl; popfl");
  134. }
  135. static inline void padlock_store_cword(struct cword *cword)
  136. {
  137. per_cpu(last_cword, raw_smp_processor_id()) = cword;
  138. }
  139. /*
  140. * While the padlock instructions don't use FP/SSE registers, they
  141. * generate a spurious DNA fault when cr0.ts is '1'. These instructions
  142. * should be used only inside the irq_ts_save/restore() context
  143. */
  144. static inline void padlock_xcrypt(const u8 *input, u8 *output, void *key,
  145. struct cword *control_word)
  146. {
  147. asm volatile (".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
  148. : "+S"(input), "+D"(output)
  149. : "d"(control_word), "b"(key), "c"(1));
  150. }
  151. static void aes_crypt_copy(const u8 *in, u8 *out, u32 *key, struct cword *cword)
  152. {
  153. u8 buf[AES_BLOCK_SIZE * 2 + PADLOCK_ALIGNMENT - 1];
  154. u8 *tmp = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
  155. memcpy(tmp, in, AES_BLOCK_SIZE);
  156. padlock_xcrypt(tmp, out, key, cword);
  157. }
  158. static inline void aes_crypt(const u8 *in, u8 *out, u32 *key,
  159. struct cword *cword)
  160. {
  161. /* padlock_xcrypt requires at least two blocks of data. */
  162. if (unlikely(!(((unsigned long)in ^ (PAGE_SIZE - AES_BLOCK_SIZE)) &
  163. (PAGE_SIZE - 1)))) {
  164. aes_crypt_copy(in, out, key, cword);
  165. return;
  166. }
  167. padlock_xcrypt(in, out, key, cword);
  168. }
  169. static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key,
  170. void *control_word, u32 count)
  171. {
  172. if (count == 1) {
  173. aes_crypt(input, output, key, control_word);
  174. return;
  175. }
  176. asm volatile ("test $1, %%cl;"
  177. "je 1f;"
  178. "lea -1(%%ecx), %%eax;"
  179. "mov $1, %%ecx;"
  180. ".byte 0xf3,0x0f,0xa7,0xc8;" /* rep xcryptecb */
  181. "mov %%eax, %%ecx;"
  182. "1:"
  183. ".byte 0xf3,0x0f,0xa7,0xc8" /* rep xcryptecb */
  184. : "+S"(input), "+D"(output)
  185. : "d"(control_word), "b"(key), "c"(count)
  186. : "ax");
  187. }
  188. static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
  189. u8 *iv, void *control_word, u32 count)
  190. {
  191. /* rep xcryptcbc */
  192. asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"
  193. : "+S" (input), "+D" (output), "+a" (iv)
  194. : "d" (control_word), "b" (key), "c" (count));
  195. return iv;
  196. }
  197. static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  198. {
  199. struct aes_ctx *ctx = aes_ctx(tfm);
  200. int ts_state;
  201. padlock_reset_key(&ctx->cword.encrypt);
  202. ts_state = irq_ts_save();
  203. aes_crypt(in, out, ctx->E, &ctx->cword.encrypt);
  204. irq_ts_restore(ts_state);
  205. padlock_store_cword(&ctx->cword.encrypt);
  206. }
  207. static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  208. {
  209. struct aes_ctx *ctx = aes_ctx(tfm);
  210. int ts_state;
  211. padlock_reset_key(&ctx->cword.encrypt);
  212. ts_state = irq_ts_save();
  213. aes_crypt(in, out, ctx->D, &ctx->cword.decrypt);
  214. irq_ts_restore(ts_state);
  215. padlock_store_cword(&ctx->cword.encrypt);
  216. }
  217. static struct crypto_alg aes_alg = {
  218. .cra_name = "aes",
  219. .cra_driver_name = "aes-padlock",
  220. .cra_priority = PADLOCK_CRA_PRIORITY,
  221. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  222. .cra_blocksize = AES_BLOCK_SIZE,
  223. .cra_ctxsize = sizeof(struct aes_ctx),
  224. .cra_alignmask = PADLOCK_ALIGNMENT - 1,
  225. .cra_module = THIS_MODULE,
  226. .cra_list = LIST_HEAD_INIT(aes_alg.cra_list),
  227. .cra_u = {
  228. .cipher = {
  229. .cia_min_keysize = AES_MIN_KEY_SIZE,
  230. .cia_max_keysize = AES_MAX_KEY_SIZE,
  231. .cia_setkey = aes_set_key,
  232. .cia_encrypt = aes_encrypt,
  233. .cia_decrypt = aes_decrypt,
  234. }
  235. }
  236. };
  237. static int ecb_aes_encrypt(struct blkcipher_desc *desc,
  238. struct scatterlist *dst, struct scatterlist *src,
  239. unsigned int nbytes)
  240. {
  241. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  242. struct blkcipher_walk walk;
  243. int err;
  244. int ts_state;
  245. padlock_reset_key(&ctx->cword.encrypt);
  246. blkcipher_walk_init(&walk, dst, src, nbytes);
  247. err = blkcipher_walk_virt(desc, &walk);
  248. ts_state = irq_ts_save();
  249. while ((nbytes = walk.nbytes)) {
  250. padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
  251. ctx->E, &ctx->cword.encrypt,
  252. nbytes / AES_BLOCK_SIZE);
  253. nbytes &= AES_BLOCK_SIZE - 1;
  254. err = blkcipher_walk_done(desc, &walk, nbytes);
  255. }
  256. irq_ts_restore(ts_state);
  257. padlock_store_cword(&ctx->cword.encrypt);
  258. return err;
  259. }
  260. static int ecb_aes_decrypt(struct blkcipher_desc *desc,
  261. struct scatterlist *dst, struct scatterlist *src,
  262. unsigned int nbytes)
  263. {
  264. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  265. struct blkcipher_walk walk;
  266. int err;
  267. int ts_state;
  268. padlock_reset_key(&ctx->cword.decrypt);
  269. blkcipher_walk_init(&walk, dst, src, nbytes);
  270. err = blkcipher_walk_virt(desc, &walk);
  271. ts_state = irq_ts_save();
  272. while ((nbytes = walk.nbytes)) {
  273. padlock_xcrypt_ecb(walk.src.virt.addr, walk.dst.virt.addr,
  274. ctx->D, &ctx->cword.decrypt,
  275. nbytes / AES_BLOCK_SIZE);
  276. nbytes &= AES_BLOCK_SIZE - 1;
  277. err = blkcipher_walk_done(desc, &walk, nbytes);
  278. }
  279. irq_ts_restore(ts_state);
  280. padlock_store_cword(&ctx->cword.encrypt);
  281. return err;
  282. }
  283. static struct crypto_alg ecb_aes_alg = {
  284. .cra_name = "ecb(aes)",
  285. .cra_driver_name = "ecb-aes-padlock",
  286. .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
  287. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  288. .cra_blocksize = AES_BLOCK_SIZE,
  289. .cra_ctxsize = sizeof(struct aes_ctx),
  290. .cra_alignmask = PADLOCK_ALIGNMENT - 1,
  291. .cra_type = &crypto_blkcipher_type,
  292. .cra_module = THIS_MODULE,
  293. .cra_list = LIST_HEAD_INIT(ecb_aes_alg.cra_list),
  294. .cra_u = {
  295. .blkcipher = {
  296. .min_keysize = AES_MIN_KEY_SIZE,
  297. .max_keysize = AES_MAX_KEY_SIZE,
  298. .setkey = aes_set_key,
  299. .encrypt = ecb_aes_encrypt,
  300. .decrypt = ecb_aes_decrypt,
  301. }
  302. }
  303. };
  304. static int cbc_aes_encrypt(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. int ts_state;
  312. padlock_reset_key(&ctx->cword.encrypt);
  313. blkcipher_walk_init(&walk, dst, src, nbytes);
  314. err = blkcipher_walk_virt(desc, &walk);
  315. ts_state = irq_ts_save();
  316. while ((nbytes = walk.nbytes)) {
  317. u8 *iv = padlock_xcrypt_cbc(walk.src.virt.addr,
  318. walk.dst.virt.addr, ctx->E,
  319. walk.iv, &ctx->cword.encrypt,
  320. nbytes / AES_BLOCK_SIZE);
  321. memcpy(walk.iv, iv, AES_BLOCK_SIZE);
  322. nbytes &= AES_BLOCK_SIZE - 1;
  323. err = blkcipher_walk_done(desc, &walk, nbytes);
  324. }
  325. irq_ts_restore(ts_state);
  326. padlock_store_cword(&ctx->cword.decrypt);
  327. return err;
  328. }
  329. static int cbc_aes_decrypt(struct blkcipher_desc *desc,
  330. struct scatterlist *dst, struct scatterlist *src,
  331. unsigned int nbytes)
  332. {
  333. struct aes_ctx *ctx = blk_aes_ctx(desc->tfm);
  334. struct blkcipher_walk walk;
  335. int err;
  336. int ts_state;
  337. padlock_reset_key(&ctx->cword.encrypt);
  338. blkcipher_walk_init(&walk, dst, src, nbytes);
  339. err = blkcipher_walk_virt(desc, &walk);
  340. ts_state = irq_ts_save();
  341. while ((nbytes = walk.nbytes)) {
  342. padlock_xcrypt_cbc(walk.src.virt.addr, walk.dst.virt.addr,
  343. ctx->D, walk.iv, &ctx->cword.decrypt,
  344. nbytes / AES_BLOCK_SIZE);
  345. nbytes &= AES_BLOCK_SIZE - 1;
  346. err = blkcipher_walk_done(desc, &walk, nbytes);
  347. }
  348. irq_ts_restore(ts_state);
  349. padlock_store_cword(&ctx->cword.encrypt);
  350. return err;
  351. }
  352. static struct crypto_alg cbc_aes_alg = {
  353. .cra_name = "cbc(aes)",
  354. .cra_driver_name = "cbc-aes-padlock",
  355. .cra_priority = PADLOCK_COMPOSITE_PRIORITY,
  356. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  357. .cra_blocksize = AES_BLOCK_SIZE,
  358. .cra_ctxsize = sizeof(struct aes_ctx),
  359. .cra_alignmask = PADLOCK_ALIGNMENT - 1,
  360. .cra_type = &crypto_blkcipher_type,
  361. .cra_module = THIS_MODULE,
  362. .cra_list = LIST_HEAD_INIT(cbc_aes_alg.cra_list),
  363. .cra_u = {
  364. .blkcipher = {
  365. .min_keysize = AES_MIN_KEY_SIZE,
  366. .max_keysize = AES_MAX_KEY_SIZE,
  367. .ivsize = AES_BLOCK_SIZE,
  368. .setkey = aes_set_key,
  369. .encrypt = cbc_aes_encrypt,
  370. .decrypt = cbc_aes_decrypt,
  371. }
  372. }
  373. };
  374. static int __init padlock_init(void)
  375. {
  376. int ret;
  377. if (!cpu_has_xcrypt) {
  378. printk(KERN_NOTICE PFX "VIA PadLock not detected.\n");
  379. return -ENODEV;
  380. }
  381. if (!cpu_has_xcrypt_enabled) {
  382. printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
  383. return -ENODEV;
  384. }
  385. if ((ret = crypto_register_alg(&aes_alg)))
  386. goto aes_err;
  387. if ((ret = crypto_register_alg(&ecb_aes_alg)))
  388. goto ecb_aes_err;
  389. if ((ret = crypto_register_alg(&cbc_aes_alg)))
  390. goto cbc_aes_err;
  391. printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n");
  392. out:
  393. return ret;
  394. cbc_aes_err:
  395. crypto_unregister_alg(&ecb_aes_alg);
  396. ecb_aes_err:
  397. crypto_unregister_alg(&aes_alg);
  398. aes_err:
  399. printk(KERN_ERR PFX "VIA PadLock AES initialization failed.\n");
  400. goto out;
  401. }
  402. static void __exit padlock_fini(void)
  403. {
  404. crypto_unregister_alg(&cbc_aes_alg);
  405. crypto_unregister_alg(&ecb_aes_alg);
  406. crypto_unregister_alg(&aes_alg);
  407. }
  408. module_init(padlock_init);
  409. module_exit(padlock_fini);
  410. MODULE_DESCRIPTION("VIA PadLock AES algorithm support");
  411. MODULE_LICENSE("GPL");
  412. MODULE_AUTHOR("Michal Ludvig");
  413. MODULE_ALIAS("aes");