padlock-aes.c 11 KB

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