cast5_avx_glue.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Glue Code for the AVX assembler implemention of the Cast5 Cipher
  3. *
  4. * Copyright (C) 2012 Johannes Goetzfried
  5. * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  20. * USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/hardirq.h>
  25. #include <linux/types.h>
  26. #include <linux/crypto.h>
  27. #include <linux/err.h>
  28. #include <crypto/algapi.h>
  29. #include <crypto/cast5.h>
  30. #include <crypto/cryptd.h>
  31. #include <crypto/ctr.h>
  32. #include <asm/xcr.h>
  33. #include <asm/xsave.h>
  34. #include <asm/crypto/ablk_helper.h>
  35. #include <asm/crypto/glue_helper.h>
  36. #define CAST5_PARALLEL_BLOCKS 16
  37. asmlinkage void cast5_ecb_enc_16way(struct cast5_ctx *ctx, u8 *dst,
  38. const u8 *src);
  39. asmlinkage void cast5_ecb_dec_16way(struct cast5_ctx *ctx, u8 *dst,
  40. const u8 *src);
  41. asmlinkage void cast5_cbc_dec_16way(struct cast5_ctx *ctx, u8 *dst,
  42. const u8 *src);
  43. asmlinkage void cast5_ctr_16way(struct cast5_ctx *ctx, u8 *dst, const u8 *src,
  44. __be64 *iv);
  45. static inline bool cast5_fpu_begin(bool fpu_enabled, unsigned int nbytes)
  46. {
  47. return glue_fpu_begin(CAST5_BLOCK_SIZE, CAST5_PARALLEL_BLOCKS,
  48. NULL, fpu_enabled, nbytes);
  49. }
  50. static inline void cast5_fpu_end(bool fpu_enabled)
  51. {
  52. return glue_fpu_end(fpu_enabled);
  53. }
  54. static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  55. bool enc)
  56. {
  57. bool fpu_enabled = false;
  58. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  59. const unsigned int bsize = CAST5_BLOCK_SIZE;
  60. unsigned int nbytes;
  61. void (*fn)(struct cast5_ctx *ctx, u8 *dst, const u8 *src);
  62. int err;
  63. fn = (enc) ? cast5_ecb_enc_16way : cast5_ecb_dec_16way;
  64. err = blkcipher_walk_virt(desc, walk);
  65. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  66. while ((nbytes = walk->nbytes)) {
  67. u8 *wsrc = walk->src.virt.addr;
  68. u8 *wdst = walk->dst.virt.addr;
  69. fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  70. /* Process multi-block batch */
  71. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  72. do {
  73. fn(ctx, wdst, wsrc);
  74. wsrc += bsize * CAST5_PARALLEL_BLOCKS;
  75. wdst += bsize * CAST5_PARALLEL_BLOCKS;
  76. nbytes -= bsize * CAST5_PARALLEL_BLOCKS;
  77. } while (nbytes >= bsize * CAST5_PARALLEL_BLOCKS);
  78. if (nbytes < bsize)
  79. goto done;
  80. }
  81. fn = (enc) ? __cast5_encrypt : __cast5_decrypt;
  82. /* Handle leftovers */
  83. do {
  84. fn(ctx, wdst, wsrc);
  85. wsrc += bsize;
  86. wdst += bsize;
  87. nbytes -= bsize;
  88. } while (nbytes >= bsize);
  89. done:
  90. err = blkcipher_walk_done(desc, walk, nbytes);
  91. }
  92. cast5_fpu_end(fpu_enabled);
  93. return err;
  94. }
  95. static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  96. struct scatterlist *src, unsigned int nbytes)
  97. {
  98. struct blkcipher_walk walk;
  99. blkcipher_walk_init(&walk, dst, src, nbytes);
  100. return ecb_crypt(desc, &walk, true);
  101. }
  102. static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  103. struct scatterlist *src, unsigned int nbytes)
  104. {
  105. struct blkcipher_walk walk;
  106. blkcipher_walk_init(&walk, dst, src, nbytes);
  107. return ecb_crypt(desc, &walk, false);
  108. }
  109. static unsigned int __cbc_encrypt(struct blkcipher_desc *desc,
  110. struct blkcipher_walk *walk)
  111. {
  112. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  113. const unsigned int bsize = CAST5_BLOCK_SIZE;
  114. unsigned int nbytes = walk->nbytes;
  115. u64 *src = (u64 *)walk->src.virt.addr;
  116. u64 *dst = (u64 *)walk->dst.virt.addr;
  117. u64 *iv = (u64 *)walk->iv;
  118. do {
  119. *dst = *src ^ *iv;
  120. __cast5_encrypt(ctx, (u8 *)dst, (u8 *)dst);
  121. iv = dst;
  122. src += 1;
  123. dst += 1;
  124. nbytes -= bsize;
  125. } while (nbytes >= bsize);
  126. *(u64 *)walk->iv = *iv;
  127. return nbytes;
  128. }
  129. static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  130. struct scatterlist *src, unsigned int nbytes)
  131. {
  132. struct blkcipher_walk walk;
  133. int err;
  134. blkcipher_walk_init(&walk, dst, src, nbytes);
  135. err = blkcipher_walk_virt(desc, &walk);
  136. while ((nbytes = walk.nbytes)) {
  137. nbytes = __cbc_encrypt(desc, &walk);
  138. err = blkcipher_walk_done(desc, &walk, nbytes);
  139. }
  140. return err;
  141. }
  142. static unsigned int __cbc_decrypt(struct blkcipher_desc *desc,
  143. struct blkcipher_walk *walk)
  144. {
  145. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  146. const unsigned int bsize = CAST5_BLOCK_SIZE;
  147. unsigned int nbytes = walk->nbytes;
  148. u64 *src = (u64 *)walk->src.virt.addr;
  149. u64 *dst = (u64 *)walk->dst.virt.addr;
  150. u64 last_iv;
  151. /* Start of the last block. */
  152. src += nbytes / bsize - 1;
  153. dst += nbytes / bsize - 1;
  154. last_iv = *src;
  155. /* Process multi-block batch */
  156. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  157. do {
  158. nbytes -= bsize * (CAST5_PARALLEL_BLOCKS - 1);
  159. src -= CAST5_PARALLEL_BLOCKS - 1;
  160. dst -= CAST5_PARALLEL_BLOCKS - 1;
  161. cast5_cbc_dec_16way(ctx, (u8 *)dst, (u8 *)src);
  162. nbytes -= bsize;
  163. if (nbytes < bsize)
  164. goto done;
  165. *dst ^= *(src - 1);
  166. src -= 1;
  167. dst -= 1;
  168. } while (nbytes >= bsize * CAST5_PARALLEL_BLOCKS);
  169. if (nbytes < bsize)
  170. goto done;
  171. }
  172. /* Handle leftovers */
  173. for (;;) {
  174. __cast5_decrypt(ctx, (u8 *)dst, (u8 *)src);
  175. nbytes -= bsize;
  176. if (nbytes < bsize)
  177. break;
  178. *dst ^= *(src - 1);
  179. src -= 1;
  180. dst -= 1;
  181. }
  182. done:
  183. *dst ^= *(u64 *)walk->iv;
  184. *(u64 *)walk->iv = last_iv;
  185. return nbytes;
  186. }
  187. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  188. struct scatterlist *src, unsigned int nbytes)
  189. {
  190. bool fpu_enabled = false;
  191. struct blkcipher_walk walk;
  192. int err;
  193. blkcipher_walk_init(&walk, dst, src, nbytes);
  194. err = blkcipher_walk_virt(desc, &walk);
  195. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  196. while ((nbytes = walk.nbytes)) {
  197. fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  198. nbytes = __cbc_decrypt(desc, &walk);
  199. err = blkcipher_walk_done(desc, &walk, nbytes);
  200. }
  201. cast5_fpu_end(fpu_enabled);
  202. return err;
  203. }
  204. static void ctr_crypt_final(struct blkcipher_desc *desc,
  205. struct blkcipher_walk *walk)
  206. {
  207. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  208. u8 *ctrblk = walk->iv;
  209. u8 keystream[CAST5_BLOCK_SIZE];
  210. u8 *src = walk->src.virt.addr;
  211. u8 *dst = walk->dst.virt.addr;
  212. unsigned int nbytes = walk->nbytes;
  213. __cast5_encrypt(ctx, keystream, ctrblk);
  214. crypto_xor(keystream, src, nbytes);
  215. memcpy(dst, keystream, nbytes);
  216. crypto_inc(ctrblk, CAST5_BLOCK_SIZE);
  217. }
  218. static unsigned int __ctr_crypt(struct blkcipher_desc *desc,
  219. struct blkcipher_walk *walk)
  220. {
  221. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  222. const unsigned int bsize = CAST5_BLOCK_SIZE;
  223. unsigned int nbytes = walk->nbytes;
  224. u64 *src = (u64 *)walk->src.virt.addr;
  225. u64 *dst = (u64 *)walk->dst.virt.addr;
  226. /* Process multi-block batch */
  227. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  228. do {
  229. cast5_ctr_16way(ctx, (u8 *)dst, (u8 *)src,
  230. (__be64 *)walk->iv);
  231. src += CAST5_PARALLEL_BLOCKS;
  232. dst += CAST5_PARALLEL_BLOCKS;
  233. nbytes -= bsize * CAST5_PARALLEL_BLOCKS;
  234. } while (nbytes >= bsize * CAST5_PARALLEL_BLOCKS);
  235. if (nbytes < bsize)
  236. goto done;
  237. }
  238. /* Handle leftovers */
  239. do {
  240. u64 ctrblk;
  241. if (dst != src)
  242. *dst = *src;
  243. ctrblk = *(u64 *)walk->iv;
  244. be64_add_cpu((__be64 *)walk->iv, 1);
  245. __cast5_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
  246. *dst ^= ctrblk;
  247. src += 1;
  248. dst += 1;
  249. nbytes -= bsize;
  250. } while (nbytes >= bsize);
  251. done:
  252. return nbytes;
  253. }
  254. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  255. struct scatterlist *src, unsigned int nbytes)
  256. {
  257. bool fpu_enabled = false;
  258. struct blkcipher_walk walk;
  259. int err;
  260. blkcipher_walk_init(&walk, dst, src, nbytes);
  261. err = blkcipher_walk_virt_block(desc, &walk, CAST5_BLOCK_SIZE);
  262. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  263. while ((nbytes = walk.nbytes) >= CAST5_BLOCK_SIZE) {
  264. fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  265. nbytes = __ctr_crypt(desc, &walk);
  266. err = blkcipher_walk_done(desc, &walk, nbytes);
  267. }
  268. cast5_fpu_end(fpu_enabled);
  269. if (walk.nbytes) {
  270. ctr_crypt_final(desc, &walk);
  271. err = blkcipher_walk_done(desc, &walk, 0);
  272. }
  273. return err;
  274. }
  275. static struct crypto_alg cast5_algs[6] = { {
  276. .cra_name = "__ecb-cast5-avx",
  277. .cra_driver_name = "__driver-ecb-cast5-avx",
  278. .cra_priority = 0,
  279. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  280. .cra_blocksize = CAST5_BLOCK_SIZE,
  281. .cra_ctxsize = sizeof(struct cast5_ctx),
  282. .cra_alignmask = 0,
  283. .cra_type = &crypto_blkcipher_type,
  284. .cra_module = THIS_MODULE,
  285. .cra_u = {
  286. .blkcipher = {
  287. .min_keysize = CAST5_MIN_KEY_SIZE,
  288. .max_keysize = CAST5_MAX_KEY_SIZE,
  289. .setkey = cast5_setkey,
  290. .encrypt = ecb_encrypt,
  291. .decrypt = ecb_decrypt,
  292. },
  293. },
  294. }, {
  295. .cra_name = "__cbc-cast5-avx",
  296. .cra_driver_name = "__driver-cbc-cast5-avx",
  297. .cra_priority = 0,
  298. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  299. .cra_blocksize = CAST5_BLOCK_SIZE,
  300. .cra_ctxsize = sizeof(struct cast5_ctx),
  301. .cra_alignmask = 0,
  302. .cra_type = &crypto_blkcipher_type,
  303. .cra_module = THIS_MODULE,
  304. .cra_u = {
  305. .blkcipher = {
  306. .min_keysize = CAST5_MIN_KEY_SIZE,
  307. .max_keysize = CAST5_MAX_KEY_SIZE,
  308. .setkey = cast5_setkey,
  309. .encrypt = cbc_encrypt,
  310. .decrypt = cbc_decrypt,
  311. },
  312. },
  313. }, {
  314. .cra_name = "__ctr-cast5-avx",
  315. .cra_driver_name = "__driver-ctr-cast5-avx",
  316. .cra_priority = 0,
  317. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  318. .cra_blocksize = 1,
  319. .cra_ctxsize = sizeof(struct cast5_ctx),
  320. .cra_alignmask = 0,
  321. .cra_type = &crypto_blkcipher_type,
  322. .cra_module = THIS_MODULE,
  323. .cra_u = {
  324. .blkcipher = {
  325. .min_keysize = CAST5_MIN_KEY_SIZE,
  326. .max_keysize = CAST5_MAX_KEY_SIZE,
  327. .ivsize = CAST5_BLOCK_SIZE,
  328. .setkey = cast5_setkey,
  329. .encrypt = ctr_crypt,
  330. .decrypt = ctr_crypt,
  331. },
  332. },
  333. }, {
  334. .cra_name = "ecb(cast5)",
  335. .cra_driver_name = "ecb-cast5-avx",
  336. .cra_priority = 200,
  337. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  338. .cra_blocksize = CAST5_BLOCK_SIZE,
  339. .cra_ctxsize = sizeof(struct async_helper_ctx),
  340. .cra_alignmask = 0,
  341. .cra_type = &crypto_ablkcipher_type,
  342. .cra_module = THIS_MODULE,
  343. .cra_init = ablk_init,
  344. .cra_exit = ablk_exit,
  345. .cra_u = {
  346. .ablkcipher = {
  347. .min_keysize = CAST5_MIN_KEY_SIZE,
  348. .max_keysize = CAST5_MAX_KEY_SIZE,
  349. .setkey = ablk_set_key,
  350. .encrypt = ablk_encrypt,
  351. .decrypt = ablk_decrypt,
  352. },
  353. },
  354. }, {
  355. .cra_name = "cbc(cast5)",
  356. .cra_driver_name = "cbc-cast5-avx",
  357. .cra_priority = 200,
  358. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  359. .cra_blocksize = CAST5_BLOCK_SIZE,
  360. .cra_ctxsize = sizeof(struct async_helper_ctx),
  361. .cra_alignmask = 0,
  362. .cra_type = &crypto_ablkcipher_type,
  363. .cra_module = THIS_MODULE,
  364. .cra_init = ablk_init,
  365. .cra_exit = ablk_exit,
  366. .cra_u = {
  367. .ablkcipher = {
  368. .min_keysize = CAST5_MIN_KEY_SIZE,
  369. .max_keysize = CAST5_MAX_KEY_SIZE,
  370. .ivsize = CAST5_BLOCK_SIZE,
  371. .setkey = ablk_set_key,
  372. .encrypt = __ablk_encrypt,
  373. .decrypt = ablk_decrypt,
  374. },
  375. },
  376. }, {
  377. .cra_name = "ctr(cast5)",
  378. .cra_driver_name = "ctr-cast5-avx",
  379. .cra_priority = 200,
  380. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  381. .cra_blocksize = 1,
  382. .cra_ctxsize = sizeof(struct async_helper_ctx),
  383. .cra_alignmask = 0,
  384. .cra_type = &crypto_ablkcipher_type,
  385. .cra_module = THIS_MODULE,
  386. .cra_init = ablk_init,
  387. .cra_exit = ablk_exit,
  388. .cra_u = {
  389. .ablkcipher = {
  390. .min_keysize = CAST5_MIN_KEY_SIZE,
  391. .max_keysize = CAST5_MAX_KEY_SIZE,
  392. .ivsize = CAST5_BLOCK_SIZE,
  393. .setkey = ablk_set_key,
  394. .encrypt = ablk_encrypt,
  395. .decrypt = ablk_encrypt,
  396. .geniv = "chainiv",
  397. },
  398. },
  399. } };
  400. static int __init cast5_init(void)
  401. {
  402. u64 xcr0;
  403. if (!cpu_has_avx || !cpu_has_osxsave) {
  404. pr_info("AVX instructions are not detected.\n");
  405. return -ENODEV;
  406. }
  407. xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
  408. if ((xcr0 & (XSTATE_SSE | XSTATE_YMM)) != (XSTATE_SSE | XSTATE_YMM)) {
  409. pr_info("AVX detected but unusable.\n");
  410. return -ENODEV;
  411. }
  412. return crypto_register_algs(cast5_algs, ARRAY_SIZE(cast5_algs));
  413. }
  414. static void __exit cast5_exit(void)
  415. {
  416. crypto_unregister_algs(cast5_algs, ARRAY_SIZE(cast5_algs));
  417. }
  418. module_init(cast5_init);
  419. module_exit(cast5_exit);
  420. MODULE_DESCRIPTION("Cast5 Cipher Algorithm, AVX optimized");
  421. MODULE_LICENSE("GPL");
  422. MODULE_ALIAS("cast5");