aes_glue.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /* Glue code for AES encryption optimized for sparc64 crypto opcodes.
  2. *
  3. * This is based largely upon arch/x86/crypto/aesni-intel_glue.c
  4. *
  5. * Copyright (C) 2008, Intel Corp.
  6. * Author: Huang Ying <ying.huang@intel.com>
  7. *
  8. * Added RFC4106 AES-GCM support for 128-bit keys under the AEAD
  9. * interface for 64-bit kernels.
  10. * Authors: Adrian Hoban <adrian.hoban@intel.com>
  11. * Gabriele Paoloni <gabriele.paoloni@intel.com>
  12. * Tadeusz Struk (tadeusz.struk@intel.com)
  13. * Aidan O'Mahony (aidan.o.mahony@intel.com)
  14. * Copyright (c) 2010, Intel Corporation.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/crypto.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/mm.h>
  21. #include <linux/types.h>
  22. #include <crypto/algapi.h>
  23. #include <crypto/aes.h>
  24. #include <asm/fpumacro.h>
  25. #include <asm/pstate.h>
  26. #include <asm/elf.h>
  27. #include "opcodes.h"
  28. struct aes_ops {
  29. void (*encrypt)(const u64 *key, const u32 *input, u32 *output);
  30. void (*decrypt)(const u64 *key, const u32 *input, u32 *output);
  31. void (*load_encrypt_keys)(const u64 *key);
  32. void (*load_decrypt_keys)(const u64 *key);
  33. void (*ecb_encrypt)(const u64 *key, const u64 *input, u64 *output,
  34. unsigned int len);
  35. void (*ecb_decrypt)(const u64 *key, const u64 *input, u64 *output,
  36. unsigned int len);
  37. void (*cbc_encrypt)(const u64 *key, const u64 *input, u64 *output,
  38. unsigned int len, u64 *iv);
  39. void (*cbc_decrypt)(const u64 *key, const u64 *input, u64 *output,
  40. unsigned int len, u64 *iv);
  41. void (*ctr_crypt)(const u64 *key, const u64 *input, u64 *output,
  42. unsigned int len, u64 *iv);
  43. };
  44. struct crypto_sparc64_aes_ctx {
  45. struct aes_ops *ops;
  46. u64 key[AES_MAX_KEYLENGTH / sizeof(u64)];
  47. u32 key_length;
  48. u32 expanded_key_length;
  49. };
  50. extern void aes_sparc64_encrypt_128(const u64 *key, const u32 *input,
  51. u32 *output);
  52. extern void aes_sparc64_encrypt_192(const u64 *key, const u32 *input,
  53. u32 *output);
  54. extern void aes_sparc64_encrypt_256(const u64 *key, const u32 *input,
  55. u32 *output);
  56. extern void aes_sparc64_decrypt_128(const u64 *key, const u32 *input,
  57. u32 *output);
  58. extern void aes_sparc64_decrypt_192(const u64 *key, const u32 *input,
  59. u32 *output);
  60. extern void aes_sparc64_decrypt_256(const u64 *key, const u32 *input,
  61. u32 *output);
  62. extern void aes_sparc64_load_encrypt_keys_128(const u64 *key);
  63. extern void aes_sparc64_load_encrypt_keys_192(const u64 *key);
  64. extern void aes_sparc64_load_encrypt_keys_256(const u64 *key);
  65. extern void aes_sparc64_load_decrypt_keys_128(const u64 *key);
  66. extern void aes_sparc64_load_decrypt_keys_192(const u64 *key);
  67. extern void aes_sparc64_load_decrypt_keys_256(const u64 *key);
  68. extern void aes_sparc64_ecb_encrypt_128(const u64 *key, const u64 *input,
  69. u64 *output, unsigned int len);
  70. extern void aes_sparc64_ecb_encrypt_192(const u64 *key, const u64 *input,
  71. u64 *output, unsigned int len);
  72. extern void aes_sparc64_ecb_encrypt_256(const u64 *key, const u64 *input,
  73. u64 *output, unsigned int len);
  74. extern void aes_sparc64_ecb_decrypt_128(const u64 *key, const u64 *input,
  75. u64 *output, unsigned int len);
  76. extern void aes_sparc64_ecb_decrypt_192(const u64 *key, const u64 *input,
  77. u64 *output, unsigned int len);
  78. extern void aes_sparc64_ecb_decrypt_256(const u64 *key, const u64 *input,
  79. u64 *output, unsigned int len);
  80. extern void aes_sparc64_cbc_encrypt_128(const u64 *key, const u64 *input,
  81. u64 *output, unsigned int len,
  82. u64 *iv);
  83. extern void aes_sparc64_cbc_encrypt_192(const u64 *key, const u64 *input,
  84. u64 *output, unsigned int len,
  85. u64 *iv);
  86. extern void aes_sparc64_cbc_encrypt_256(const u64 *key, const u64 *input,
  87. u64 *output, unsigned int len,
  88. u64 *iv);
  89. extern void aes_sparc64_cbc_decrypt_128(const u64 *key, const u64 *input,
  90. u64 *output, unsigned int len,
  91. u64 *iv);
  92. extern void aes_sparc64_cbc_decrypt_192(const u64 *key, const u64 *input,
  93. u64 *output, unsigned int len,
  94. u64 *iv);
  95. extern void aes_sparc64_cbc_decrypt_256(const u64 *key, const u64 *input,
  96. u64 *output, unsigned int len,
  97. u64 *iv);
  98. extern void aes_sparc64_ctr_crypt_128(const u64 *key, const u64 *input,
  99. u64 *output, unsigned int len,
  100. u64 *iv);
  101. extern void aes_sparc64_ctr_crypt_192(const u64 *key, const u64 *input,
  102. u64 *output, unsigned int len,
  103. u64 *iv);
  104. extern void aes_sparc64_ctr_crypt_256(const u64 *key, const u64 *input,
  105. u64 *output, unsigned int len,
  106. u64 *iv);
  107. static struct aes_ops aes128_ops = {
  108. .encrypt = aes_sparc64_encrypt_128,
  109. .decrypt = aes_sparc64_decrypt_128,
  110. .load_encrypt_keys = aes_sparc64_load_encrypt_keys_128,
  111. .load_decrypt_keys = aes_sparc64_load_decrypt_keys_128,
  112. .ecb_encrypt = aes_sparc64_ecb_encrypt_128,
  113. .ecb_decrypt = aes_sparc64_ecb_decrypt_128,
  114. .cbc_encrypt = aes_sparc64_cbc_encrypt_128,
  115. .cbc_decrypt = aes_sparc64_cbc_decrypt_128,
  116. .ctr_crypt = aes_sparc64_ctr_crypt_128,
  117. };
  118. static struct aes_ops aes192_ops = {
  119. .encrypt = aes_sparc64_encrypt_192,
  120. .decrypt = aes_sparc64_decrypt_192,
  121. .load_encrypt_keys = aes_sparc64_load_encrypt_keys_192,
  122. .load_decrypt_keys = aes_sparc64_load_decrypt_keys_192,
  123. .ecb_encrypt = aes_sparc64_ecb_encrypt_192,
  124. .ecb_decrypt = aes_sparc64_ecb_decrypt_192,
  125. .cbc_encrypt = aes_sparc64_cbc_encrypt_192,
  126. .cbc_decrypt = aes_sparc64_cbc_decrypt_192,
  127. .ctr_crypt = aes_sparc64_ctr_crypt_192,
  128. };
  129. static struct aes_ops aes256_ops = {
  130. .encrypt = aes_sparc64_encrypt_256,
  131. .decrypt = aes_sparc64_decrypt_256,
  132. .load_encrypt_keys = aes_sparc64_load_encrypt_keys_256,
  133. .load_decrypt_keys = aes_sparc64_load_decrypt_keys_256,
  134. .ecb_encrypt = aes_sparc64_ecb_encrypt_256,
  135. .ecb_decrypt = aes_sparc64_ecb_decrypt_256,
  136. .cbc_encrypt = aes_sparc64_cbc_encrypt_256,
  137. .cbc_decrypt = aes_sparc64_cbc_decrypt_256,
  138. .ctr_crypt = aes_sparc64_ctr_crypt_256,
  139. };
  140. extern void aes_sparc64_key_expand(const u32 *in_key, u64 *output_key,
  141. unsigned int key_len);
  142. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  143. unsigned int key_len)
  144. {
  145. struct crypto_sparc64_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  146. u32 *flags = &tfm->crt_flags;
  147. switch (key_len) {
  148. case AES_KEYSIZE_128:
  149. ctx->expanded_key_length = 0xb0;
  150. ctx->ops = &aes128_ops;
  151. break;
  152. case AES_KEYSIZE_192:
  153. ctx->expanded_key_length = 0xd0;
  154. ctx->ops = &aes192_ops;
  155. break;
  156. case AES_KEYSIZE_256:
  157. ctx->expanded_key_length = 0xf0;
  158. ctx->ops = &aes256_ops;
  159. break;
  160. default:
  161. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  162. return -EINVAL;
  163. }
  164. aes_sparc64_key_expand((const u32 *)in_key, &ctx->key[0], key_len);
  165. ctx->key_length = key_len;
  166. return 0;
  167. }
  168. static void aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  169. {
  170. struct crypto_sparc64_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  171. ctx->ops->encrypt(&ctx->key[0], (const u32 *) src, (u32 *) dst);
  172. }
  173. static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  174. {
  175. struct crypto_sparc64_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  176. ctx->ops->decrypt(&ctx->key[0], (const u32 *) src, (u32 *) dst);
  177. }
  178. #define AES_BLOCK_MASK (~(AES_BLOCK_SIZE-1))
  179. static int ecb_encrypt(struct blkcipher_desc *desc,
  180. struct scatterlist *dst, struct scatterlist *src,
  181. unsigned int nbytes)
  182. {
  183. struct crypto_sparc64_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  184. struct blkcipher_walk walk;
  185. int err;
  186. blkcipher_walk_init(&walk, dst, src, nbytes);
  187. err = blkcipher_walk_virt(desc, &walk);
  188. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  189. ctx->ops->load_encrypt_keys(&ctx->key[0]);
  190. while ((nbytes = walk.nbytes)) {
  191. unsigned int block_len = nbytes & AES_BLOCK_MASK;
  192. if (likely(block_len)) {
  193. ctx->ops->ecb_encrypt(&ctx->key[0],
  194. (const u64 *)walk.src.virt.addr,
  195. (u64 *) walk.dst.virt.addr,
  196. block_len);
  197. }
  198. nbytes &= AES_BLOCK_SIZE - 1;
  199. err = blkcipher_walk_done(desc, &walk, nbytes);
  200. }
  201. fprs_write(0);
  202. return err;
  203. }
  204. static int ecb_decrypt(struct blkcipher_desc *desc,
  205. struct scatterlist *dst, struct scatterlist *src,
  206. unsigned int nbytes)
  207. {
  208. struct crypto_sparc64_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  209. struct blkcipher_walk walk;
  210. u64 *key_end;
  211. int err;
  212. blkcipher_walk_init(&walk, dst, src, nbytes);
  213. err = blkcipher_walk_virt(desc, &walk);
  214. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  215. ctx->ops->load_decrypt_keys(&ctx->key[0]);
  216. key_end = &ctx->key[ctx->expanded_key_length / sizeof(u64)];
  217. while ((nbytes = walk.nbytes)) {
  218. unsigned int block_len = nbytes & AES_BLOCK_MASK;
  219. if (likely(block_len)) {
  220. ctx->ops->ecb_decrypt(key_end,
  221. (const u64 *) walk.src.virt.addr,
  222. (u64 *) walk.dst.virt.addr, block_len);
  223. }
  224. nbytes &= AES_BLOCK_SIZE - 1;
  225. err = blkcipher_walk_done(desc, &walk, nbytes);
  226. }
  227. fprs_write(0);
  228. return err;
  229. }
  230. static int cbc_encrypt(struct blkcipher_desc *desc,
  231. struct scatterlist *dst, struct scatterlist *src,
  232. unsigned int nbytes)
  233. {
  234. struct crypto_sparc64_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  235. struct blkcipher_walk walk;
  236. int err;
  237. blkcipher_walk_init(&walk, dst, src, nbytes);
  238. err = blkcipher_walk_virt(desc, &walk);
  239. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  240. ctx->ops->load_encrypt_keys(&ctx->key[0]);
  241. while ((nbytes = walk.nbytes)) {
  242. unsigned int block_len = nbytes & AES_BLOCK_MASK;
  243. if (likely(block_len)) {
  244. ctx->ops->cbc_encrypt(&ctx->key[0],
  245. (const u64 *)walk.src.virt.addr,
  246. (u64 *) walk.dst.virt.addr,
  247. block_len, (u64 *) walk.iv);
  248. }
  249. nbytes &= AES_BLOCK_SIZE - 1;
  250. err = blkcipher_walk_done(desc, &walk, nbytes);
  251. }
  252. fprs_write(0);
  253. return err;
  254. }
  255. static int cbc_decrypt(struct blkcipher_desc *desc,
  256. struct scatterlist *dst, struct scatterlist *src,
  257. unsigned int nbytes)
  258. {
  259. struct crypto_sparc64_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  260. struct blkcipher_walk walk;
  261. u64 *key_end;
  262. int err;
  263. blkcipher_walk_init(&walk, dst, src, nbytes);
  264. err = blkcipher_walk_virt(desc, &walk);
  265. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  266. ctx->ops->load_decrypt_keys(&ctx->key[0]);
  267. key_end = &ctx->key[ctx->expanded_key_length / sizeof(u64)];
  268. while ((nbytes = walk.nbytes)) {
  269. unsigned int block_len = nbytes & AES_BLOCK_MASK;
  270. if (likely(block_len)) {
  271. ctx->ops->cbc_decrypt(key_end,
  272. (const u64 *) walk.src.virt.addr,
  273. (u64 *) walk.dst.virt.addr,
  274. block_len, (u64 *) walk.iv);
  275. }
  276. nbytes &= AES_BLOCK_SIZE - 1;
  277. err = blkcipher_walk_done(desc, &walk, nbytes);
  278. }
  279. fprs_write(0);
  280. return err;
  281. }
  282. static void ctr_crypt_final(struct crypto_sparc64_aes_ctx *ctx,
  283. struct blkcipher_walk *walk)
  284. {
  285. u8 *ctrblk = walk->iv;
  286. u64 keystream[AES_BLOCK_SIZE / sizeof(u64)];
  287. u8 *src = walk->src.virt.addr;
  288. u8 *dst = walk->dst.virt.addr;
  289. unsigned int nbytes = walk->nbytes;
  290. ctx->ops->ecb_encrypt(&ctx->key[0], (const u64 *)ctrblk,
  291. keystream, AES_BLOCK_SIZE);
  292. crypto_xor((u8 *) keystream, src, nbytes);
  293. memcpy(dst, keystream, nbytes);
  294. crypto_inc(ctrblk, AES_BLOCK_SIZE);
  295. }
  296. static int ctr_crypt(struct blkcipher_desc *desc,
  297. struct scatterlist *dst, struct scatterlist *src,
  298. unsigned int nbytes)
  299. {
  300. struct crypto_sparc64_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  301. struct blkcipher_walk walk;
  302. int err;
  303. blkcipher_walk_init(&walk, dst, src, nbytes);
  304. err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  305. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  306. ctx->ops->load_encrypt_keys(&ctx->key[0]);
  307. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  308. unsigned int block_len = nbytes & AES_BLOCK_MASK;
  309. if (likely(block_len)) {
  310. ctx->ops->ctr_crypt(&ctx->key[0],
  311. (const u64 *)walk.src.virt.addr,
  312. (u64 *) walk.dst.virt.addr,
  313. block_len, (u64 *) walk.iv);
  314. }
  315. nbytes &= AES_BLOCK_SIZE - 1;
  316. err = blkcipher_walk_done(desc, &walk, nbytes);
  317. }
  318. if (walk.nbytes) {
  319. ctr_crypt_final(ctx, &walk);
  320. err = blkcipher_walk_done(desc, &walk, 0);
  321. }
  322. fprs_write(0);
  323. return err;
  324. }
  325. static struct crypto_alg algs[] = { {
  326. .cra_name = "aes",
  327. .cra_driver_name = "aes-sparc64",
  328. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  329. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  330. .cra_blocksize = AES_BLOCK_SIZE,
  331. .cra_ctxsize = sizeof(struct crypto_sparc64_aes_ctx),
  332. .cra_alignmask = 3,
  333. .cra_module = THIS_MODULE,
  334. .cra_u = {
  335. .cipher = {
  336. .cia_min_keysize = AES_MIN_KEY_SIZE,
  337. .cia_max_keysize = AES_MAX_KEY_SIZE,
  338. .cia_setkey = aes_set_key,
  339. .cia_encrypt = aes_encrypt,
  340. .cia_decrypt = aes_decrypt
  341. }
  342. }
  343. }, {
  344. .cra_name = "ecb(aes)",
  345. .cra_driver_name = "ecb-aes-sparc64",
  346. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  347. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  348. .cra_blocksize = AES_BLOCK_SIZE,
  349. .cra_ctxsize = sizeof(struct crypto_sparc64_aes_ctx),
  350. .cra_alignmask = 7,
  351. .cra_type = &crypto_blkcipher_type,
  352. .cra_module = THIS_MODULE,
  353. .cra_u = {
  354. .blkcipher = {
  355. .min_keysize = AES_MIN_KEY_SIZE,
  356. .max_keysize = AES_MAX_KEY_SIZE,
  357. .setkey = aes_set_key,
  358. .encrypt = ecb_encrypt,
  359. .decrypt = ecb_decrypt,
  360. },
  361. },
  362. }, {
  363. .cra_name = "cbc(aes)",
  364. .cra_driver_name = "cbc-aes-sparc64",
  365. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  366. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  367. .cra_blocksize = AES_BLOCK_SIZE,
  368. .cra_ctxsize = sizeof(struct crypto_sparc64_aes_ctx),
  369. .cra_alignmask = 7,
  370. .cra_type = &crypto_blkcipher_type,
  371. .cra_module = THIS_MODULE,
  372. .cra_u = {
  373. .blkcipher = {
  374. .min_keysize = AES_MIN_KEY_SIZE,
  375. .max_keysize = AES_MAX_KEY_SIZE,
  376. .ivsize = AES_BLOCK_SIZE,
  377. .setkey = aes_set_key,
  378. .encrypt = cbc_encrypt,
  379. .decrypt = cbc_decrypt,
  380. },
  381. },
  382. }, {
  383. .cra_name = "ctr(aes)",
  384. .cra_driver_name = "ctr-aes-sparc64",
  385. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  386. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  387. .cra_blocksize = 1,
  388. .cra_ctxsize = sizeof(struct crypto_sparc64_aes_ctx),
  389. .cra_alignmask = 7,
  390. .cra_type = &crypto_blkcipher_type,
  391. .cra_module = THIS_MODULE,
  392. .cra_u = {
  393. .blkcipher = {
  394. .min_keysize = AES_MIN_KEY_SIZE,
  395. .max_keysize = AES_MAX_KEY_SIZE,
  396. .ivsize = AES_BLOCK_SIZE,
  397. .setkey = aes_set_key,
  398. .encrypt = ctr_crypt,
  399. .decrypt = ctr_crypt,
  400. },
  401. },
  402. } };
  403. static bool __init sparc64_has_aes_opcode(void)
  404. {
  405. unsigned long cfr;
  406. if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
  407. return false;
  408. __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
  409. if (!(cfr & CFR_AES))
  410. return false;
  411. return true;
  412. }
  413. static int __init aes_sparc64_mod_init(void)
  414. {
  415. int i;
  416. for (i = 0; i < ARRAY_SIZE(algs); i++)
  417. INIT_LIST_HEAD(&algs[i].cra_list);
  418. if (sparc64_has_aes_opcode()) {
  419. pr_info("Using sparc64 aes opcodes optimized AES implementation\n");
  420. return crypto_register_algs(algs, ARRAY_SIZE(algs));
  421. }
  422. pr_info("sparc64 aes opcodes not available.\n");
  423. return -ENODEV;
  424. }
  425. static void __exit aes_sparc64_mod_fini(void)
  426. {
  427. crypto_unregister_algs(algs, ARRAY_SIZE(algs));
  428. }
  429. module_init(aes_sparc64_mod_init);
  430. module_exit(aes_sparc64_mod_fini);
  431. MODULE_LICENSE("GPL");
  432. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, sparc64 aes opcode accelerated");
  433. MODULE_ALIAS_CRYPTO("aes");
  434. #include "crop_devid.c"