cast5_avx_glue.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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/fpu/api.h>
  34. #include <asm/crypto/glue_helper.h>
  35. #define CAST5_PARALLEL_BLOCKS 16
  36. asmlinkage void cast5_ecb_enc_16way(struct cast5_ctx *ctx, u8 *dst,
  37. const u8 *src);
  38. asmlinkage void cast5_ecb_dec_16way(struct cast5_ctx *ctx, u8 *dst,
  39. const u8 *src);
  40. asmlinkage void cast5_cbc_dec_16way(struct cast5_ctx *ctx, u8 *dst,
  41. const u8 *src);
  42. asmlinkage void cast5_ctr_16way(struct cast5_ctx *ctx, u8 *dst, const u8 *src,
  43. __be64 *iv);
  44. static inline bool cast5_fpu_begin(bool fpu_enabled, unsigned int nbytes)
  45. {
  46. return glue_fpu_begin(CAST5_BLOCK_SIZE, CAST5_PARALLEL_BLOCKS,
  47. NULL, fpu_enabled, nbytes);
  48. }
  49. static inline void cast5_fpu_end(bool fpu_enabled)
  50. {
  51. return glue_fpu_end(fpu_enabled);
  52. }
  53. static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
  54. bool enc)
  55. {
  56. bool fpu_enabled = false;
  57. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  58. const unsigned int bsize = CAST5_BLOCK_SIZE;
  59. unsigned int nbytes;
  60. void (*fn)(struct cast5_ctx *ctx, u8 *dst, const u8 *src);
  61. int err;
  62. err = blkcipher_walk_virt(desc, walk);
  63. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  64. while ((nbytes = walk->nbytes)) {
  65. u8 *wsrc = walk->src.virt.addr;
  66. u8 *wdst = walk->dst.virt.addr;
  67. fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  68. /* Process multi-block batch */
  69. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  70. fn = (enc) ? cast5_ecb_enc_16way : cast5_ecb_dec_16way;
  71. do {
  72. fn(ctx, wdst, wsrc);
  73. wsrc += bsize * CAST5_PARALLEL_BLOCKS;
  74. wdst += bsize * CAST5_PARALLEL_BLOCKS;
  75. nbytes -= bsize * CAST5_PARALLEL_BLOCKS;
  76. } while (nbytes >= bsize * CAST5_PARALLEL_BLOCKS);
  77. if (nbytes < bsize)
  78. goto done;
  79. }
  80. fn = (enc) ? __cast5_encrypt : __cast5_decrypt;
  81. /* Handle leftovers */
  82. do {
  83. fn(ctx, wdst, wsrc);
  84. wsrc += bsize;
  85. wdst += bsize;
  86. nbytes -= bsize;
  87. } while (nbytes >= bsize);
  88. done:
  89. err = blkcipher_walk_done(desc, walk, nbytes);
  90. }
  91. cast5_fpu_end(fpu_enabled);
  92. return err;
  93. }
  94. static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  95. struct scatterlist *src, unsigned int nbytes)
  96. {
  97. struct blkcipher_walk walk;
  98. blkcipher_walk_init(&walk, dst, src, nbytes);
  99. return ecb_crypt(desc, &walk, true);
  100. }
  101. static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  102. struct scatterlist *src, unsigned int nbytes)
  103. {
  104. struct blkcipher_walk walk;
  105. blkcipher_walk_init(&walk, dst, src, nbytes);
  106. return ecb_crypt(desc, &walk, false);
  107. }
  108. static unsigned int __cbc_encrypt(struct blkcipher_desc *desc,
  109. struct blkcipher_walk *walk)
  110. {
  111. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  112. const unsigned int bsize = CAST5_BLOCK_SIZE;
  113. unsigned int nbytes = walk->nbytes;
  114. u64 *src = (u64 *)walk->src.virt.addr;
  115. u64 *dst = (u64 *)walk->dst.virt.addr;
  116. u64 *iv = (u64 *)walk->iv;
  117. do {
  118. *dst = *src ^ *iv;
  119. __cast5_encrypt(ctx, (u8 *)dst, (u8 *)dst);
  120. iv = dst;
  121. src += 1;
  122. dst += 1;
  123. nbytes -= bsize;
  124. } while (nbytes >= bsize);
  125. *(u64 *)walk->iv = *iv;
  126. return nbytes;
  127. }
  128. static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  129. struct scatterlist *src, unsigned int nbytes)
  130. {
  131. struct blkcipher_walk walk;
  132. int err;
  133. blkcipher_walk_init(&walk, dst, src, nbytes);
  134. err = blkcipher_walk_virt(desc, &walk);
  135. while ((nbytes = walk.nbytes)) {
  136. nbytes = __cbc_encrypt(desc, &walk);
  137. err = blkcipher_walk_done(desc, &walk, nbytes);
  138. }
  139. return err;
  140. }
  141. static unsigned int __cbc_decrypt(struct blkcipher_desc *desc,
  142. struct blkcipher_walk *walk)
  143. {
  144. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  145. const unsigned int bsize = CAST5_BLOCK_SIZE;
  146. unsigned int nbytes = walk->nbytes;
  147. u64 *src = (u64 *)walk->src.virt.addr;
  148. u64 *dst = (u64 *)walk->dst.virt.addr;
  149. u64 last_iv;
  150. /* Start of the last block. */
  151. src += nbytes / bsize - 1;
  152. dst += nbytes / bsize - 1;
  153. last_iv = *src;
  154. /* Process multi-block batch */
  155. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  156. do {
  157. nbytes -= bsize * (CAST5_PARALLEL_BLOCKS - 1);
  158. src -= CAST5_PARALLEL_BLOCKS - 1;
  159. dst -= CAST5_PARALLEL_BLOCKS - 1;
  160. cast5_cbc_dec_16way(ctx, (u8 *)dst, (u8 *)src);
  161. nbytes -= bsize;
  162. if (nbytes < bsize)
  163. goto done;
  164. *dst ^= *(src - 1);
  165. src -= 1;
  166. dst -= 1;
  167. } while (nbytes >= bsize * CAST5_PARALLEL_BLOCKS);
  168. }
  169. /* Handle leftovers */
  170. for (;;) {
  171. __cast5_decrypt(ctx, (u8 *)dst, (u8 *)src);
  172. nbytes -= bsize;
  173. if (nbytes < bsize)
  174. break;
  175. *dst ^= *(src - 1);
  176. src -= 1;
  177. dst -= 1;
  178. }
  179. done:
  180. *dst ^= *(u64 *)walk->iv;
  181. *(u64 *)walk->iv = last_iv;
  182. return nbytes;
  183. }
  184. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  185. struct scatterlist *src, unsigned int nbytes)
  186. {
  187. bool fpu_enabled = false;
  188. struct blkcipher_walk walk;
  189. int err;
  190. blkcipher_walk_init(&walk, dst, src, nbytes);
  191. err = blkcipher_walk_virt(desc, &walk);
  192. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  193. while ((nbytes = walk.nbytes)) {
  194. fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  195. nbytes = __cbc_decrypt(desc, &walk);
  196. err = blkcipher_walk_done(desc, &walk, nbytes);
  197. }
  198. cast5_fpu_end(fpu_enabled);
  199. return err;
  200. }
  201. static void ctr_crypt_final(struct blkcipher_desc *desc,
  202. struct blkcipher_walk *walk)
  203. {
  204. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  205. u8 *ctrblk = walk->iv;
  206. u8 keystream[CAST5_BLOCK_SIZE];
  207. u8 *src = walk->src.virt.addr;
  208. u8 *dst = walk->dst.virt.addr;
  209. unsigned int nbytes = walk->nbytes;
  210. __cast5_encrypt(ctx, keystream, ctrblk);
  211. crypto_xor(keystream, src, nbytes);
  212. memcpy(dst, keystream, nbytes);
  213. crypto_inc(ctrblk, CAST5_BLOCK_SIZE);
  214. }
  215. static unsigned int __ctr_crypt(struct blkcipher_desc *desc,
  216. struct blkcipher_walk *walk)
  217. {
  218. struct cast5_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  219. const unsigned int bsize = CAST5_BLOCK_SIZE;
  220. unsigned int nbytes = walk->nbytes;
  221. u64 *src = (u64 *)walk->src.virt.addr;
  222. u64 *dst = (u64 *)walk->dst.virt.addr;
  223. /* Process multi-block batch */
  224. if (nbytes >= bsize * CAST5_PARALLEL_BLOCKS) {
  225. do {
  226. cast5_ctr_16way(ctx, (u8 *)dst, (u8 *)src,
  227. (__be64 *)walk->iv);
  228. src += CAST5_PARALLEL_BLOCKS;
  229. dst += CAST5_PARALLEL_BLOCKS;
  230. nbytes -= bsize * CAST5_PARALLEL_BLOCKS;
  231. } while (nbytes >= bsize * CAST5_PARALLEL_BLOCKS);
  232. if (nbytes < bsize)
  233. goto done;
  234. }
  235. /* Handle leftovers */
  236. do {
  237. u64 ctrblk;
  238. if (dst != src)
  239. *dst = *src;
  240. ctrblk = *(u64 *)walk->iv;
  241. be64_add_cpu((__be64 *)walk->iv, 1);
  242. __cast5_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
  243. *dst ^= ctrblk;
  244. src += 1;
  245. dst += 1;
  246. nbytes -= bsize;
  247. } while (nbytes >= bsize);
  248. done:
  249. return nbytes;
  250. }
  251. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  252. struct scatterlist *src, unsigned int nbytes)
  253. {
  254. bool fpu_enabled = false;
  255. struct blkcipher_walk walk;
  256. int err;
  257. blkcipher_walk_init(&walk, dst, src, nbytes);
  258. err = blkcipher_walk_virt_block(desc, &walk, CAST5_BLOCK_SIZE);
  259. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  260. while ((nbytes = walk.nbytes) >= CAST5_BLOCK_SIZE) {
  261. fpu_enabled = cast5_fpu_begin(fpu_enabled, nbytes);
  262. nbytes = __ctr_crypt(desc, &walk);
  263. err = blkcipher_walk_done(desc, &walk, nbytes);
  264. }
  265. cast5_fpu_end(fpu_enabled);
  266. if (walk.nbytes) {
  267. ctr_crypt_final(desc, &walk);
  268. err = blkcipher_walk_done(desc, &walk, 0);
  269. }
  270. return err;
  271. }
  272. static struct crypto_alg cast5_algs[6] = { {
  273. .cra_name = "__ecb-cast5-avx",
  274. .cra_driver_name = "__driver-ecb-cast5-avx",
  275. .cra_priority = 0,
  276. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  277. CRYPTO_ALG_INTERNAL,
  278. .cra_blocksize = CAST5_BLOCK_SIZE,
  279. .cra_ctxsize = sizeof(struct cast5_ctx),
  280. .cra_alignmask = 0,
  281. .cra_type = &crypto_blkcipher_type,
  282. .cra_module = THIS_MODULE,
  283. .cra_u = {
  284. .blkcipher = {
  285. .min_keysize = CAST5_MIN_KEY_SIZE,
  286. .max_keysize = CAST5_MAX_KEY_SIZE,
  287. .setkey = cast5_setkey,
  288. .encrypt = ecb_encrypt,
  289. .decrypt = ecb_decrypt,
  290. },
  291. },
  292. }, {
  293. .cra_name = "__cbc-cast5-avx",
  294. .cra_driver_name = "__driver-cbc-cast5-avx",
  295. .cra_priority = 0,
  296. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  297. CRYPTO_ALG_INTERNAL,
  298. .cra_blocksize = CAST5_BLOCK_SIZE,
  299. .cra_ctxsize = sizeof(struct cast5_ctx),
  300. .cra_alignmask = 0,
  301. .cra_type = &crypto_blkcipher_type,
  302. .cra_module = THIS_MODULE,
  303. .cra_u = {
  304. .blkcipher = {
  305. .min_keysize = CAST5_MIN_KEY_SIZE,
  306. .max_keysize = CAST5_MAX_KEY_SIZE,
  307. .setkey = cast5_setkey,
  308. .encrypt = cbc_encrypt,
  309. .decrypt = cbc_decrypt,
  310. },
  311. },
  312. }, {
  313. .cra_name = "__ctr-cast5-avx",
  314. .cra_driver_name = "__driver-ctr-cast5-avx",
  315. .cra_priority = 0,
  316. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  317. CRYPTO_ALG_INTERNAL,
  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. const char *feature_name;
  403. if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
  404. &feature_name)) {
  405. pr_info("CPU feature '%s' is not supported.\n", feature_name);
  406. return -ENODEV;
  407. }
  408. return crypto_register_algs(cast5_algs, ARRAY_SIZE(cast5_algs));
  409. }
  410. static void __exit cast5_exit(void)
  411. {
  412. crypto_unregister_algs(cast5_algs, ARRAY_SIZE(cast5_algs));
  413. }
  414. module_init(cast5_init);
  415. module_exit(cast5_exit);
  416. MODULE_DESCRIPTION("Cast5 Cipher Algorithm, AVX optimized");
  417. MODULE_LICENSE("GPL");
  418. MODULE_ALIAS_CRYPTO("cast5");