padlock-aes.c 12 KB

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