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/ablk_helper.h>
  29. #include <crypto/algapi.h>
  30. #include <crypto/cast5.h>
  31. #include <crypto/cryptd.h>
  32. #include <crypto/ctr.h>
  33. #include <asm/xcr.h>
  34. #include <asm/xsave.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. }
  170. /* Handle leftovers */
  171. for (;;) {
  172. __cast5_decrypt(ctx, (u8 *)dst, (u8 *)src);
  173. nbytes -= bsize;
  174. if (nbytes < bsize)
  175. break;
  176. *dst ^= *(src - 1);
  177. src -= 1;
  178. dst -= 1;
  179. }
  180. done:
  181. *dst ^= *(u64 *)walk->iv;
  182. *(u64 *)walk->iv = last_iv;
  183. return nbytes;
  184. }
  185. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  186. struct scatterlist *src, unsigned int nbytes)
  187. {
  188. bool fpu_enabled = false;
  189. struct blkcipher_walk walk;
  190. int err;
  191. blkcipher_walk_init(&walk, dst, src, nbytes);
  192. err = blkcipher_walk_virt(desc, &walk);
  193. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  194. while ((nbytes = walk.nbytes)) {
  195. fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  196. nbytes = __cbc_decrypt(desc, &walk);
  197. err = blkcipher_walk_done(desc, &walk, nbytes);
  198. }
  199. cast5_fpu_end(fpu_enabled);
  200. return err;
  201. }
  202. static void ctr_crypt_final(struct blkcipher_desc *desc,
  203. struct blkcipher_walk *walk)
  204. {
  205. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  206. u8 *ctrblk = walk->iv;
  207. u8 keystream[CAST5_BLOCK_SIZE];
  208. u8 *src = walk->src.virt.addr;
  209. u8 *dst = walk->dst.virt.addr;
  210. unsigned int nbytes = walk->nbytes;
  211. __cast5_encrypt(ctx, keystream, ctrblk);
  212. crypto_xor(keystream, src, nbytes);
  213. memcpy(dst, keystream, nbytes);
  214. crypto_inc(ctrblk, CAST5_BLOCK_SIZE);
  215. }
  216. static unsigned int __ctr_crypt(struct blkcipher_desc *desc,
  217. struct blkcipher_walk *walk)
  218. {
  219. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  220. const unsigned int bsize = CAST5_BLOCK_SIZE;
  221. unsigned int nbytes = walk->nbytes;
  222. u64 *src = (u64 *)walk->src.virt.addr;
  223. u64 *dst = (u64 *)walk->dst.virt.addr;
  224. /* Process multi-block batch */
  225. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  226. do {
  227. cast5_ctr_16way(ctx, (u8 *)dst, (u8 *)src,
  228. (__be64 *)walk->iv);
  229. src += CAST5_PARALLEL_BLOCKS;
  230. dst += CAST5_PARALLEL_BLOCKS;
  231. nbytes -= bsize * CAST5_PARALLEL_BLOCKS;
  232. } while (nbytes >= bsize * CAST5_PARALLEL_BLOCKS);
  233. if (nbytes < bsize)
  234. goto done;
  235. }
  236. /* Handle leftovers */
  237. do {
  238. u64 ctrblk;
  239. if (dst != src)
  240. *dst = *src;
  241. ctrblk = *(u64 *)walk->iv;
  242. be64_add_cpu((__be64 *)walk->iv, 1);
  243. __cast5_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
  244. *dst ^= ctrblk;
  245. src += 1;
  246. dst += 1;
  247. nbytes -= bsize;
  248. } while (nbytes >= bsize);
  249. done:
  250. return nbytes;
  251. }
  252. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  253. struct scatterlist *src, unsigned int nbytes)
  254. {
  255. bool fpu_enabled = false;
  256. struct blkcipher_walk walk;
  257. int err;
  258. blkcipher_walk_init(&walk, dst, src, nbytes);
  259. err = blkcipher_walk_virt_block(desc, &walk, CAST5_BLOCK_SIZE);
  260. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  261. while ((nbytes = walk.nbytes) >= CAST5_BLOCK_SIZE) {
  262. fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  263. nbytes = __ctr_crypt(desc, &walk);
  264. err = blkcipher_walk_done(desc, &walk, nbytes);
  265. }
  266. cast5_fpu_end(fpu_enabled);
  267. if (walk.nbytes) {
  268. ctr_crypt_final(desc, &walk);
  269. err = blkcipher_walk_done(desc, &walk, 0);
  270. }
  271. return err;
  272. }
  273. static struct crypto_alg cast5_algs[6] = { {
  274. .cra_name = "__ecb-cast5-avx",
  275. .cra_driver_name = "__driver-ecb-cast5-avx",
  276. .cra_priority = 0,
  277. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  278. CRYPTO_ALG_INTERNAL,
  279. .cra_blocksize = CAST5_BLOCK_SIZE,
  280. .cra_ctxsize = sizeof(struct cast5_ctx),
  281. .cra_alignmask = 0,
  282. .cra_type = &crypto_blkcipher_type,
  283. .cra_module = THIS_MODULE,
  284. .cra_u = {
  285. .blkcipher = {
  286. .min_keysize = CAST5_MIN_KEY_SIZE,
  287. .max_keysize = CAST5_MAX_KEY_SIZE,
  288. .setkey = cast5_setkey,
  289. .encrypt = ecb_encrypt,
  290. .decrypt = ecb_decrypt,
  291. },
  292. },
  293. }, {
  294. .cra_name = "__cbc-cast5-avx",
  295. .cra_driver_name = "__driver-cbc-cast5-avx",
  296. .cra_priority = 0,
  297. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  298. CRYPTO_ALG_INTERNAL,
  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. CRYPTO_ALG_INTERNAL,
  319. .cra_blocksize = 1,
  320. .cra_ctxsize = sizeof(struct cast5_ctx),
  321. .cra_alignmask = 0,
  322. .cra_type = &crypto_blkcipher_type,
  323. .cra_module = THIS_MODULE,
  324. .cra_u = {
  325. .blkcipher = {
  326. .min_keysize = CAST5_MIN_KEY_SIZE,
  327. .max_keysize = CAST5_MAX_KEY_SIZE,
  328. .ivsize = CAST5_BLOCK_SIZE,
  329. .setkey = cast5_setkey,
  330. .encrypt = ctr_crypt,
  331. .decrypt = ctr_crypt,
  332. },
  333. },
  334. }, {
  335. .cra_name = "ecb(cast5)",
  336. .cra_driver_name = "ecb-cast5-avx",
  337. .cra_priority = 200,
  338. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  339. .cra_blocksize = CAST5_BLOCK_SIZE,
  340. .cra_ctxsize = sizeof(struct async_helper_ctx),
  341. .cra_alignmask = 0,
  342. .cra_type = &crypto_ablkcipher_type,
  343. .cra_module = THIS_MODULE,
  344. .cra_init = ablk_init,
  345. .cra_exit = ablk_exit,
  346. .cra_u = {
  347. .ablkcipher = {
  348. .min_keysize = CAST5_MIN_KEY_SIZE,
  349. .max_keysize = CAST5_MAX_KEY_SIZE,
  350. .setkey = ablk_set_key,
  351. .encrypt = ablk_encrypt,
  352. .decrypt = ablk_decrypt,
  353. },
  354. },
  355. }, {
  356. .cra_name = "cbc(cast5)",
  357. .cra_driver_name = "cbc-cast5-avx",
  358. .cra_priority = 200,
  359. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  360. .cra_blocksize = CAST5_BLOCK_SIZE,
  361. .cra_ctxsize = sizeof(struct async_helper_ctx),
  362. .cra_alignmask = 0,
  363. .cra_type = &crypto_ablkcipher_type,
  364. .cra_module = THIS_MODULE,
  365. .cra_init = ablk_init,
  366. .cra_exit = ablk_exit,
  367. .cra_u = {
  368. .ablkcipher = {
  369. .min_keysize = CAST5_MIN_KEY_SIZE,
  370. .max_keysize = CAST5_MAX_KEY_SIZE,
  371. .ivsize = CAST5_BLOCK_SIZE,
  372. .setkey = ablk_set_key,
  373. .encrypt = __ablk_encrypt,
  374. .decrypt = ablk_decrypt,
  375. },
  376. },
  377. }, {
  378. .cra_name = "ctr(cast5)",
  379. .cra_driver_name = "ctr-cast5-avx",
  380. .cra_priority = 200,
  381. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  382. .cra_blocksize = 1,
  383. .cra_ctxsize = sizeof(struct async_helper_ctx),
  384. .cra_alignmask = 0,
  385. .cra_type = &crypto_ablkcipher_type,
  386. .cra_module = THIS_MODULE,
  387. .cra_init = ablk_init,
  388. .cra_exit = ablk_exit,
  389. .cra_u = {
  390. .ablkcipher = {
  391. .min_keysize = CAST5_MIN_KEY_SIZE,
  392. .max_keysize = CAST5_MAX_KEY_SIZE,
  393. .ivsize = CAST5_BLOCK_SIZE,
  394. .setkey = ablk_set_key,
  395. .encrypt = ablk_encrypt,
  396. .decrypt = ablk_encrypt,
  397. .geniv = "chainiv",
  398. },
  399. },
  400. } };
  401. static int __init cast5_init(void)
  402. {
  403. u64 xcr0;
  404. if (!cpu_has_avx || !cpu_has_osxsave) {
  405. pr_info("AVX instructions are not detected.\n");
  406. return -ENODEV;
  407. }
  408. xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
  409. if ((xcr0 & (XSTATE_SSE | XSTATE_YMM)) != (XSTATE_SSE | XSTATE_YMM)) {
  410. pr_info("AVX detected but unusable.\n");
  411. return -ENODEV;
  412. }
  413. return crypto_register_algs(cast5_algs, ARRAY_SIZE(cast5_algs));
  414. }
  415. static void __exit cast5_exit(void)
  416. {
  417. crypto_unregister_algs(cast5_algs, ARRAY_SIZE(cast5_algs));
  418. }
  419. module_init(cast5_init);
  420. module_exit(cast5_exit);
  421. MODULE_DESCRIPTION("Cast5 Cipher Algorithm, AVX optimized");
  422. MODULE_LICENSE("GPL");
  423. MODULE_ALIAS_CRYPTO("cast5");