aes-spe-glue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * Glue code for AES implementation for SPE instructions (PPC)
  3. *
  4. * Based on generic implementation. The assembler module takes care
  5. * about the SPE registers so it can run from interrupt context.
  6. *
  7. * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. */
  15. #include <crypto/aes.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/types.h>
  19. #include <linux/errno.h>
  20. #include <linux/crypto.h>
  21. #include <asm/byteorder.h>
  22. #include <asm/switch_to.h>
  23. #include <crypto/algapi.h>
  24. /*
  25. * MAX_BYTES defines the number of bytes that are allowed to be processed
  26. * between preempt_disable() and preempt_enable(). e500 cores can issue two
  27. * instructions per clock cycle using one 32/64 bit unit (SU1) and one 32
  28. * bit unit (SU2). One of these can be a memory access that is executed via
  29. * a single load and store unit (LSU). XTS-AES-256 takes ~780 operations per
  30. * 16 byte block block or 25 cycles per byte. Thus 768 bytes of input data
  31. * will need an estimated maximum of 20,000 cycles. Headroom for cache misses
  32. * included. Even with the low end model clocked at 667 MHz this equals to a
  33. * critical time window of less than 30us. The value has been choosen to
  34. * process a 512 byte disk block in one or a large 1400 bytes IPsec network
  35. * packet in two runs.
  36. *
  37. */
  38. #define MAX_BYTES 768
  39. struct ppc_aes_ctx {
  40. u32 key_enc[AES_MAX_KEYLENGTH_U32];
  41. u32 key_dec[AES_MAX_KEYLENGTH_U32];
  42. u32 rounds;
  43. };
  44. struct ppc_xts_ctx {
  45. u32 key_enc[AES_MAX_KEYLENGTH_U32];
  46. u32 key_dec[AES_MAX_KEYLENGTH_U32];
  47. u32 key_twk[AES_MAX_KEYLENGTH_U32];
  48. u32 rounds;
  49. };
  50. extern void ppc_encrypt_aes(u8 *out, const u8 *in, u32 *key_enc, u32 rounds);
  51. extern void ppc_decrypt_aes(u8 *out, const u8 *in, u32 *key_dec, u32 rounds);
  52. extern void ppc_encrypt_ecb(u8 *out, const u8 *in, u32 *key_enc, u32 rounds,
  53. u32 bytes);
  54. extern void ppc_decrypt_ecb(u8 *out, const u8 *in, u32 *key_dec, u32 rounds,
  55. u32 bytes);
  56. extern void ppc_encrypt_cbc(u8 *out, const u8 *in, u32 *key_enc, u32 rounds,
  57. u32 bytes, u8 *iv);
  58. extern void ppc_decrypt_cbc(u8 *out, const u8 *in, u32 *key_dec, u32 rounds,
  59. u32 bytes, u8 *iv);
  60. extern void ppc_crypt_ctr (u8 *out, const u8 *in, u32 *key_enc, u32 rounds,
  61. u32 bytes, u8 *iv);
  62. extern void ppc_encrypt_xts(u8 *out, const u8 *in, u32 *key_enc, u32 rounds,
  63. u32 bytes, u8 *iv, u32 *key_twk);
  64. extern void ppc_decrypt_xts(u8 *out, const u8 *in, u32 *key_dec, u32 rounds,
  65. u32 bytes, u8 *iv, u32 *key_twk);
  66. extern void ppc_expand_key_128(u32 *key_enc, const u8 *key);
  67. extern void ppc_expand_key_192(u32 *key_enc, const u8 *key);
  68. extern void ppc_expand_key_256(u32 *key_enc, const u8 *key);
  69. extern void ppc_generate_decrypt_key(u32 *key_dec,u32 *key_enc,
  70. unsigned int key_len);
  71. static void spe_begin(void)
  72. {
  73. /* disable preemption and save users SPE registers if required */
  74. preempt_disable();
  75. enable_kernel_spe();
  76. }
  77. static void spe_end(void)
  78. {
  79. /* reenable preemption */
  80. preempt_enable();
  81. }
  82. static int ppc_aes_setkey(struct crypto_tfm *tfm, const u8 *in_key,
  83. unsigned int key_len)
  84. {
  85. struct ppc_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  86. if (key_len != AES_KEYSIZE_128 &&
  87. key_len != AES_KEYSIZE_192 &&
  88. key_len != AES_KEYSIZE_256) {
  89. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  90. return -EINVAL;
  91. }
  92. switch (key_len) {
  93. case AES_KEYSIZE_128:
  94. ctx->rounds = 4;
  95. ppc_expand_key_128(ctx->key_enc, in_key);
  96. break;
  97. case AES_KEYSIZE_192:
  98. ctx->rounds = 5;
  99. ppc_expand_key_192(ctx->key_enc, in_key);
  100. break;
  101. case AES_KEYSIZE_256:
  102. ctx->rounds = 6;
  103. ppc_expand_key_256(ctx->key_enc, in_key);
  104. break;
  105. }
  106. ppc_generate_decrypt_key(ctx->key_dec, ctx->key_enc, key_len);
  107. return 0;
  108. }
  109. static int ppc_xts_setkey(struct crypto_tfm *tfm, const u8 *in_key,
  110. unsigned int key_len)
  111. {
  112. struct ppc_xts_ctx *ctx = crypto_tfm_ctx(tfm);
  113. key_len >>= 1;
  114. if (key_len != AES_KEYSIZE_128 &&
  115. key_len != AES_KEYSIZE_192 &&
  116. key_len != AES_KEYSIZE_256) {
  117. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  118. return -EINVAL;
  119. }
  120. switch (key_len) {
  121. case AES_KEYSIZE_128:
  122. ctx->rounds = 4;
  123. ppc_expand_key_128(ctx->key_enc, in_key);
  124. ppc_expand_key_128(ctx->key_twk, in_key + AES_KEYSIZE_128);
  125. break;
  126. case AES_KEYSIZE_192:
  127. ctx->rounds = 5;
  128. ppc_expand_key_192(ctx->key_enc, in_key);
  129. ppc_expand_key_192(ctx->key_twk, in_key + AES_KEYSIZE_192);
  130. break;
  131. case AES_KEYSIZE_256:
  132. ctx->rounds = 6;
  133. ppc_expand_key_256(ctx->key_enc, in_key);
  134. ppc_expand_key_256(ctx->key_twk, in_key + AES_KEYSIZE_256);
  135. break;
  136. }
  137. ppc_generate_decrypt_key(ctx->key_dec, ctx->key_enc, key_len);
  138. return 0;
  139. }
  140. static void ppc_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  141. {
  142. struct ppc_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  143. spe_begin();
  144. ppc_encrypt_aes(out, in, ctx->key_enc, ctx->rounds);
  145. spe_end();
  146. }
  147. static void ppc_aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
  148. {
  149. struct ppc_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  150. spe_begin();
  151. ppc_decrypt_aes(out, in, ctx->key_dec, ctx->rounds);
  152. spe_end();
  153. }
  154. static int ppc_ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  155. struct scatterlist *src, unsigned int nbytes)
  156. {
  157. struct ppc_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  158. struct blkcipher_walk walk;
  159. unsigned int ubytes;
  160. int err;
  161. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  162. blkcipher_walk_init(&walk, dst, src, nbytes);
  163. err = blkcipher_walk_virt(desc, &walk);
  164. while ((nbytes = walk.nbytes)) {
  165. ubytes = nbytes > MAX_BYTES ?
  166. nbytes - MAX_BYTES : nbytes & (AES_BLOCK_SIZE - 1);
  167. nbytes -= ubytes;
  168. spe_begin();
  169. ppc_encrypt_ecb(walk.dst.virt.addr, walk.src.virt.addr,
  170. ctx->key_enc, ctx->rounds, nbytes);
  171. spe_end();
  172. err = blkcipher_walk_done(desc, &walk, ubytes);
  173. }
  174. return err;
  175. }
  176. static int ppc_ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  177. struct scatterlist *src, unsigned int nbytes)
  178. {
  179. struct ppc_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  180. struct blkcipher_walk walk;
  181. unsigned int ubytes;
  182. int err;
  183. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  184. blkcipher_walk_init(&walk, dst, src, nbytes);
  185. err = blkcipher_walk_virt(desc, &walk);
  186. while ((nbytes = walk.nbytes)) {
  187. ubytes = nbytes > MAX_BYTES ?
  188. nbytes - MAX_BYTES : nbytes & (AES_BLOCK_SIZE - 1);
  189. nbytes -= ubytes;
  190. spe_begin();
  191. ppc_decrypt_ecb(walk.dst.virt.addr, walk.src.virt.addr,
  192. ctx->key_dec, ctx->rounds, nbytes);
  193. spe_end();
  194. err = blkcipher_walk_done(desc, &walk, ubytes);
  195. }
  196. return err;
  197. }
  198. static int ppc_cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  199. struct scatterlist *src, unsigned int nbytes)
  200. {
  201. struct ppc_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  202. struct blkcipher_walk walk;
  203. unsigned int ubytes;
  204. int err;
  205. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  206. blkcipher_walk_init(&walk, dst, src, nbytes);
  207. err = blkcipher_walk_virt(desc, &walk);
  208. while ((nbytes = walk.nbytes)) {
  209. ubytes = nbytes > MAX_BYTES ?
  210. nbytes - MAX_BYTES : nbytes & (AES_BLOCK_SIZE - 1);
  211. nbytes -= ubytes;
  212. spe_begin();
  213. ppc_encrypt_cbc(walk.dst.virt.addr, walk.src.virt.addr,
  214. ctx->key_enc, ctx->rounds, nbytes, walk.iv);
  215. spe_end();
  216. err = blkcipher_walk_done(desc, &walk, ubytes);
  217. }
  218. return err;
  219. }
  220. static int ppc_cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  221. struct scatterlist *src, unsigned int nbytes)
  222. {
  223. struct ppc_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  224. struct blkcipher_walk walk;
  225. unsigned int ubytes;
  226. int err;
  227. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  228. blkcipher_walk_init(&walk, dst, src, nbytes);
  229. err = blkcipher_walk_virt(desc, &walk);
  230. while ((nbytes = walk.nbytes)) {
  231. ubytes = nbytes > MAX_BYTES ?
  232. nbytes - MAX_BYTES : nbytes & (AES_BLOCK_SIZE - 1);
  233. nbytes -= ubytes;
  234. spe_begin();
  235. ppc_decrypt_cbc(walk.dst.virt.addr, walk.src.virt.addr,
  236. ctx->key_dec, ctx->rounds, nbytes, walk.iv);
  237. spe_end();
  238. err = blkcipher_walk_done(desc, &walk, ubytes);
  239. }
  240. return err;
  241. }
  242. static int ppc_ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  243. struct scatterlist *src, unsigned int nbytes)
  244. {
  245. struct ppc_aes_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  246. struct blkcipher_walk walk;
  247. unsigned int pbytes, ubytes;
  248. int err;
  249. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  250. blkcipher_walk_init(&walk, dst, src, nbytes);
  251. err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  252. while ((pbytes = walk.nbytes)) {
  253. pbytes = pbytes > MAX_BYTES ? MAX_BYTES : pbytes;
  254. pbytes = pbytes == nbytes ?
  255. nbytes : pbytes & ~(AES_BLOCK_SIZE - 1);
  256. ubytes = walk.nbytes - pbytes;
  257. spe_begin();
  258. ppc_crypt_ctr(walk.dst.virt.addr, walk.src.virt.addr,
  259. ctx->key_enc, ctx->rounds, pbytes , walk.iv);
  260. spe_end();
  261. nbytes -= pbytes;
  262. err = blkcipher_walk_done(desc, &walk, ubytes);
  263. }
  264. return err;
  265. }
  266. static int ppc_xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  267. struct scatterlist *src, unsigned int nbytes)
  268. {
  269. struct ppc_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  270. struct blkcipher_walk walk;
  271. unsigned int ubytes;
  272. int err;
  273. u32 *twk;
  274. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  275. blkcipher_walk_init(&walk, dst, src, nbytes);
  276. err = blkcipher_walk_virt(desc, &walk);
  277. twk = ctx->key_twk;
  278. while ((nbytes = walk.nbytes)) {
  279. ubytes = nbytes > MAX_BYTES ?
  280. nbytes - MAX_BYTES : nbytes & (AES_BLOCK_SIZE - 1);
  281. nbytes -= ubytes;
  282. spe_begin();
  283. ppc_encrypt_xts(walk.dst.virt.addr, walk.src.virt.addr,
  284. ctx->key_enc, ctx->rounds, nbytes, walk.iv, twk);
  285. spe_end();
  286. twk = NULL;
  287. err = blkcipher_walk_done(desc, &walk, ubytes);
  288. }
  289. return err;
  290. }
  291. static int ppc_xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  292. struct scatterlist *src, unsigned int nbytes)
  293. {
  294. struct ppc_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  295. struct blkcipher_walk walk;
  296. unsigned int ubytes;
  297. int err;
  298. u32 *twk;
  299. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  300. blkcipher_walk_init(&walk, dst, src, nbytes);
  301. err = blkcipher_walk_virt(desc, &walk);
  302. twk = ctx->key_twk;
  303. while ((nbytes = walk.nbytes)) {
  304. ubytes = nbytes > MAX_BYTES ?
  305. nbytes - MAX_BYTES : nbytes & (AES_BLOCK_SIZE - 1);
  306. nbytes -= ubytes;
  307. spe_begin();
  308. ppc_decrypt_xts(walk.dst.virt.addr, walk.src.virt.addr,
  309. ctx->key_dec, ctx->rounds, nbytes, walk.iv, twk);
  310. spe_end();
  311. twk = NULL;
  312. err = blkcipher_walk_done(desc, &walk, ubytes);
  313. }
  314. return err;
  315. }
  316. /*
  317. * Algorithm definitions. Disabling alignment (cra_alignmask=0) was chosen
  318. * because the e500 platform can handle unaligned reads/writes very efficently.
  319. * This improves IPsec thoughput by another few percent. Additionally we assume
  320. * that AES context is always aligned to at least 8 bytes because it is created
  321. * with kmalloc() in the crypto infrastructure
  322. *
  323. */
  324. static struct crypto_alg aes_algs[] = { {
  325. .cra_name = "aes",
  326. .cra_driver_name = "aes-ppc-spe",
  327. .cra_priority = 300,
  328. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  329. .cra_blocksize = AES_BLOCK_SIZE,
  330. .cra_ctxsize = sizeof(struct ppc_aes_ctx),
  331. .cra_alignmask = 0,
  332. .cra_module = THIS_MODULE,
  333. .cra_u = {
  334. .cipher = {
  335. .cia_min_keysize = AES_MIN_KEY_SIZE,
  336. .cia_max_keysize = AES_MAX_KEY_SIZE,
  337. .cia_setkey = ppc_aes_setkey,
  338. .cia_encrypt = ppc_aes_encrypt,
  339. .cia_decrypt = ppc_aes_decrypt
  340. }
  341. }
  342. }, {
  343. .cra_name = "ecb(aes)",
  344. .cra_driver_name = "ecb-ppc-spe",
  345. .cra_priority = 300,
  346. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  347. .cra_blocksize = AES_BLOCK_SIZE,
  348. .cra_ctxsize = sizeof(struct ppc_aes_ctx),
  349. .cra_alignmask = 0,
  350. .cra_type = &crypto_blkcipher_type,
  351. .cra_module = THIS_MODULE,
  352. .cra_u = {
  353. .blkcipher = {
  354. .min_keysize = AES_MIN_KEY_SIZE,
  355. .max_keysize = AES_MAX_KEY_SIZE,
  356. .ivsize = AES_BLOCK_SIZE,
  357. .setkey = ppc_aes_setkey,
  358. .encrypt = ppc_ecb_encrypt,
  359. .decrypt = ppc_ecb_decrypt,
  360. }
  361. }
  362. }, {
  363. .cra_name = "cbc(aes)",
  364. .cra_driver_name = "cbc-ppc-spe",
  365. .cra_priority = 300,
  366. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  367. .cra_blocksize = AES_BLOCK_SIZE,
  368. .cra_ctxsize = sizeof(struct ppc_aes_ctx),
  369. .cra_alignmask = 0,
  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 = ppc_aes_setkey,
  378. .encrypt = ppc_cbc_encrypt,
  379. .decrypt = ppc_cbc_decrypt,
  380. }
  381. }
  382. }, {
  383. .cra_name = "ctr(aes)",
  384. .cra_driver_name = "ctr-ppc-spe",
  385. .cra_priority = 300,
  386. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  387. .cra_blocksize = 1,
  388. .cra_ctxsize = sizeof(struct ppc_aes_ctx),
  389. .cra_alignmask = 0,
  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 = ppc_aes_setkey,
  398. .encrypt = ppc_ctr_crypt,
  399. .decrypt = ppc_ctr_crypt,
  400. }
  401. }
  402. }, {
  403. .cra_name = "xts(aes)",
  404. .cra_driver_name = "xts-ppc-spe",
  405. .cra_priority = 300,
  406. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  407. .cra_blocksize = AES_BLOCK_SIZE,
  408. .cra_ctxsize = sizeof(struct ppc_xts_ctx),
  409. .cra_alignmask = 0,
  410. .cra_type = &crypto_blkcipher_type,
  411. .cra_module = THIS_MODULE,
  412. .cra_u = {
  413. .blkcipher = {
  414. .min_keysize = AES_MIN_KEY_SIZE * 2,
  415. .max_keysize = AES_MAX_KEY_SIZE * 2,
  416. .ivsize = AES_BLOCK_SIZE,
  417. .setkey = ppc_xts_setkey,
  418. .encrypt = ppc_xts_encrypt,
  419. .decrypt = ppc_xts_decrypt,
  420. }
  421. }
  422. } };
  423. static int __init ppc_aes_mod_init(void)
  424. {
  425. return crypto_register_algs(aes_algs, ARRAY_SIZE(aes_algs));
  426. }
  427. static void __exit ppc_aes_mod_fini(void)
  428. {
  429. crypto_unregister_algs(aes_algs, ARRAY_SIZE(aes_algs));
  430. }
  431. module_init(ppc_aes_mod_init);
  432. module_exit(ppc_aes_mod_fini);
  433. MODULE_LICENSE("GPL");
  434. MODULE_DESCRIPTION("AES-ECB/CBC/CTR/XTS, SPE optimized");
  435. MODULE_ALIAS_CRYPTO("aes");
  436. MODULE_ALIAS_CRYPTO("ecb(aes)");
  437. MODULE_ALIAS_CRYPTO("cbc(aes)");
  438. MODULE_ALIAS_CRYPTO("ctr(aes)");
  439. MODULE_ALIAS_CRYPTO("xts(aes)");
  440. MODULE_ALIAS_CRYPTO("aes-ppc-spe");