twofish_glue_3way.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Glue Code for 3-way parallel assembler optimized version of Twofish
  3. *
  4. * Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  19. * USA
  20. *
  21. */
  22. #include <asm/processor.h>
  23. #include <linux/crypto.h>
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <crypto/algapi.h>
  28. #include <crypto/twofish.h>
  29. #include <crypto/b128ops.h>
  30. #include <asm/crypto/twofish.h>
  31. #include <asm/crypto/glue_helper.h>
  32. #include <crypto/lrw.h>
  33. #include <crypto/xts.h>
  34. EXPORT_SYMBOL_GPL(__twofish_enc_blk_3way);
  35. EXPORT_SYMBOL_GPL(twofish_dec_blk_3way);
  36. static inline void twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst,
  37. const u8 *src)
  38. {
  39. __twofish_enc_blk_3way(ctx, dst, src, false);
  40. }
  41. static inline void twofish_enc_blk_xor_3way(struct twofish_ctx *ctx, u8 *dst,
  42. const u8 *src)
  43. {
  44. __twofish_enc_blk_3way(ctx, dst, src, true);
  45. }
  46. void twofish_dec_blk_cbc_3way(void *ctx, u128 *dst, const u128 *src)
  47. {
  48. u128 ivs[2];
  49. ivs[0] = src[0];
  50. ivs[1] = src[1];
  51. twofish_dec_blk_3way(ctx, (u8 *)dst, (u8 *)src);
  52. u128_xor(&dst[1], &dst[1], &ivs[0]);
  53. u128_xor(&dst[2], &dst[2], &ivs[1]);
  54. }
  55. EXPORT_SYMBOL_GPL(twofish_dec_blk_cbc_3way);
  56. void twofish_enc_blk_ctr(void *ctx, u128 *dst, const u128 *src, le128 *iv)
  57. {
  58. be128 ctrblk;
  59. if (dst != src)
  60. *dst = *src;
  61. le128_to_be128(&ctrblk, iv);
  62. le128_inc(iv);
  63. twofish_enc_blk(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
  64. u128_xor(dst, dst, (u128 *)&ctrblk);
  65. }
  66. EXPORT_SYMBOL_GPL(twofish_enc_blk_ctr);
  67. void twofish_enc_blk_ctr_3way(void *ctx, u128 *dst, const u128 *src,
  68. le128 *iv)
  69. {
  70. be128 ctrblks[3];
  71. if (dst != src) {
  72. dst[0] = src[0];
  73. dst[1] = src[1];
  74. dst[2] = src[2];
  75. }
  76. le128_to_be128(&ctrblks[0], iv);
  77. le128_inc(iv);
  78. le128_to_be128(&ctrblks[1], iv);
  79. le128_inc(iv);
  80. le128_to_be128(&ctrblks[2], iv);
  81. le128_inc(iv);
  82. twofish_enc_blk_xor_3way(ctx, (u8 *)dst, (u8 *)ctrblks);
  83. }
  84. EXPORT_SYMBOL_GPL(twofish_enc_blk_ctr_3way);
  85. static const struct common_glue_ctx twofish_enc = {
  86. .num_funcs = 2,
  87. .fpu_blocks_limit = -1,
  88. .funcs = { {
  89. .num_blocks = 3,
  90. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk_3way) }
  91. }, {
  92. .num_blocks = 1,
  93. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk) }
  94. } }
  95. };
  96. static const struct common_glue_ctx twofish_ctr = {
  97. .num_funcs = 2,
  98. .fpu_blocks_limit = -1,
  99. .funcs = { {
  100. .num_blocks = 3,
  101. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk_ctr_3way) }
  102. }, {
  103. .num_blocks = 1,
  104. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk_ctr) }
  105. } }
  106. };
  107. static const struct common_glue_ctx twofish_dec = {
  108. .num_funcs = 2,
  109. .fpu_blocks_limit = -1,
  110. .funcs = { {
  111. .num_blocks = 3,
  112. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk_3way) }
  113. }, {
  114. .num_blocks = 1,
  115. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk) }
  116. } }
  117. };
  118. static const struct common_glue_ctx twofish_dec_cbc = {
  119. .num_funcs = 2,
  120. .fpu_blocks_limit = -1,
  121. .funcs = { {
  122. .num_blocks = 3,
  123. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk_cbc_3way) }
  124. }, {
  125. .num_blocks = 1,
  126. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk) }
  127. } }
  128. };
  129. static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  130. struct scatterlist *src, unsigned int nbytes)
  131. {
  132. return glue_ecb_crypt_128bit(&twofish_enc, desc, dst, src, nbytes);
  133. }
  134. static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  135. struct scatterlist *src, unsigned int nbytes)
  136. {
  137. return glue_ecb_crypt_128bit(&twofish_dec, desc, dst, src, nbytes);
  138. }
  139. static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  140. struct scatterlist *src, unsigned int nbytes)
  141. {
  142. return glue_cbc_encrypt_128bit(GLUE_FUNC_CAST(twofish_enc_blk), desc,
  143. dst, src, nbytes);
  144. }
  145. static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  146. struct scatterlist *src, unsigned int nbytes)
  147. {
  148. return glue_cbc_decrypt_128bit(&twofish_dec_cbc, desc, dst, src,
  149. nbytes);
  150. }
  151. static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  152. struct scatterlist *src, unsigned int nbytes)
  153. {
  154. return glue_ctr_crypt_128bit(&twofish_ctr, desc, dst, src, nbytes);
  155. }
  156. static void encrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
  157. {
  158. const unsigned int bsize = TF_BLOCK_SIZE;
  159. struct twofish_ctx *ctx = priv;
  160. int i;
  161. if (nbytes == 3 * bsize) {
  162. twofish_enc_blk_3way(ctx, srcdst, srcdst);
  163. return;
  164. }
  165. for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
  166. twofish_enc_blk(ctx, srcdst, srcdst);
  167. }
  168. static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
  169. {
  170. const unsigned int bsize = TF_BLOCK_SIZE;
  171. struct twofish_ctx *ctx = priv;
  172. int i;
  173. if (nbytes == 3 * bsize) {
  174. twofish_dec_blk_3way(ctx, srcdst, srcdst);
  175. return;
  176. }
  177. for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
  178. twofish_dec_blk(ctx, srcdst, srcdst);
  179. }
  180. int lrw_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
  181. unsigned int keylen)
  182. {
  183. struct twofish_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
  184. int err;
  185. err = __twofish_setkey(&ctx->twofish_ctx, key, keylen - TF_BLOCK_SIZE,
  186. &tfm->crt_flags);
  187. if (err)
  188. return err;
  189. return lrw_init_table(&ctx->lrw_table, key + keylen - TF_BLOCK_SIZE);
  190. }
  191. EXPORT_SYMBOL_GPL(lrw_twofish_setkey);
  192. static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  193. struct scatterlist *src, unsigned int nbytes)
  194. {
  195. struct twofish_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  196. be128 buf[3];
  197. struct lrw_crypt_req req = {
  198. .tbuf = buf,
  199. .tbuflen = sizeof(buf),
  200. .table_ctx = &ctx->lrw_table,
  201. .crypt_ctx = &ctx->twofish_ctx,
  202. .crypt_fn = encrypt_callback,
  203. };
  204. return lrw_crypt(desc, dst, src, nbytes, &req);
  205. }
  206. static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  207. struct scatterlist *src, unsigned int nbytes)
  208. {
  209. struct twofish_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  210. be128 buf[3];
  211. struct lrw_crypt_req req = {
  212. .tbuf = buf,
  213. .tbuflen = sizeof(buf),
  214. .table_ctx = &ctx->lrw_table,
  215. .crypt_ctx = &ctx->twofish_ctx,
  216. .crypt_fn = decrypt_callback,
  217. };
  218. return lrw_crypt(desc, dst, src, nbytes, &req);
  219. }
  220. void lrw_twofish_exit_tfm(struct crypto_tfm *tfm)
  221. {
  222. struct twofish_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
  223. lrw_free_table(&ctx->lrw_table);
  224. }
  225. EXPORT_SYMBOL_GPL(lrw_twofish_exit_tfm);
  226. int xts_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
  227. unsigned int keylen)
  228. {
  229. struct twofish_xts_ctx *ctx = crypto_tfm_ctx(tfm);
  230. u32 *flags = &tfm->crt_flags;
  231. int err;
  232. err = xts_check_key(tfm, key, keylen);
  233. if (err)
  234. return err;
  235. /* first half of xts-key is for crypt */
  236. err = __twofish_setkey(&ctx->crypt_ctx, key, keylen / 2, flags);
  237. if (err)
  238. return err;
  239. /* second half of xts-key is for tweak */
  240. return __twofish_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2,
  241. flags);
  242. }
  243. EXPORT_SYMBOL_GPL(xts_twofish_setkey);
  244. static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  245. struct scatterlist *src, unsigned int nbytes)
  246. {
  247. struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  248. be128 buf[3];
  249. struct xts_crypt_req req = {
  250. .tbuf = buf,
  251. .tbuflen = sizeof(buf),
  252. .tweak_ctx = &ctx->tweak_ctx,
  253. .tweak_fn = XTS_TWEAK_CAST(twofish_enc_blk),
  254. .crypt_ctx = &ctx->crypt_ctx,
  255. .crypt_fn = encrypt_callback,
  256. };
  257. return xts_crypt(desc, dst, src, nbytes, &req);
  258. }
  259. static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  260. struct scatterlist *src, unsigned int nbytes)
  261. {
  262. struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  263. be128 buf[3];
  264. struct xts_crypt_req req = {
  265. .tbuf = buf,
  266. .tbuflen = sizeof(buf),
  267. .tweak_ctx = &ctx->tweak_ctx,
  268. .tweak_fn = XTS_TWEAK_CAST(twofish_enc_blk),
  269. .crypt_ctx = &ctx->crypt_ctx,
  270. .crypt_fn = decrypt_callback,
  271. };
  272. return xts_crypt(desc, dst, src, nbytes, &req);
  273. }
  274. static struct crypto_alg tf_algs[5] = { {
  275. .cra_name = "ecb(twofish)",
  276. .cra_driver_name = "ecb-twofish-3way",
  277. .cra_priority = 300,
  278. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  279. .cra_blocksize = TF_BLOCK_SIZE,
  280. .cra_ctxsize = sizeof(struct twofish_ctx),
  281. .cra_alignmask = 0,
  282. .cra_type = &crypto_blkcipher_type,
  283. .cra_module = THIS_MODULE,
  284. .cra_u = {
  285. .blkcipher = {
  286. .min_keysize = TF_MIN_KEY_SIZE,
  287. .max_keysize = TF_MAX_KEY_SIZE,
  288. .setkey = twofish_setkey,
  289. .encrypt = ecb_encrypt,
  290. .decrypt = ecb_decrypt,
  291. },
  292. },
  293. }, {
  294. .cra_name = "cbc(twofish)",
  295. .cra_driver_name = "cbc-twofish-3way",
  296. .cra_priority = 300,
  297. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  298. .cra_blocksize = TF_BLOCK_SIZE,
  299. .cra_ctxsize = sizeof(struct twofish_ctx),
  300. .cra_alignmask = 0,
  301. .cra_type = &crypto_blkcipher_type,
  302. .cra_module = THIS_MODULE,
  303. .cra_u = {
  304. .blkcipher = {
  305. .min_keysize = TF_MIN_KEY_SIZE,
  306. .max_keysize = TF_MAX_KEY_SIZE,
  307. .ivsize = TF_BLOCK_SIZE,
  308. .setkey = twofish_setkey,
  309. .encrypt = cbc_encrypt,
  310. .decrypt = cbc_decrypt,
  311. },
  312. },
  313. }, {
  314. .cra_name = "ctr(twofish)",
  315. .cra_driver_name = "ctr-twofish-3way",
  316. .cra_priority = 300,
  317. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  318. .cra_blocksize = 1,
  319. .cra_ctxsize = sizeof(struct twofish_ctx),
  320. .cra_alignmask = 0,
  321. .cra_type = &crypto_blkcipher_type,
  322. .cra_module = THIS_MODULE,
  323. .cra_u = {
  324. .blkcipher = {
  325. .min_keysize = TF_MIN_KEY_SIZE,
  326. .max_keysize = TF_MAX_KEY_SIZE,
  327. .ivsize = TF_BLOCK_SIZE,
  328. .setkey = twofish_setkey,
  329. .encrypt = ctr_crypt,
  330. .decrypt = ctr_crypt,
  331. },
  332. },
  333. }, {
  334. .cra_name = "lrw(twofish)",
  335. .cra_driver_name = "lrw-twofish-3way",
  336. .cra_priority = 300,
  337. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  338. .cra_blocksize = TF_BLOCK_SIZE,
  339. .cra_ctxsize = sizeof(struct twofish_lrw_ctx),
  340. .cra_alignmask = 0,
  341. .cra_type = &crypto_blkcipher_type,
  342. .cra_module = THIS_MODULE,
  343. .cra_exit = lrw_twofish_exit_tfm,
  344. .cra_u = {
  345. .blkcipher = {
  346. .min_keysize = TF_MIN_KEY_SIZE + TF_BLOCK_SIZE,
  347. .max_keysize = TF_MAX_KEY_SIZE + TF_BLOCK_SIZE,
  348. .ivsize = TF_BLOCK_SIZE,
  349. .setkey = lrw_twofish_setkey,
  350. .encrypt = lrw_encrypt,
  351. .decrypt = lrw_decrypt,
  352. },
  353. },
  354. }, {
  355. .cra_name = "xts(twofish)",
  356. .cra_driver_name = "xts-twofish-3way",
  357. .cra_priority = 300,
  358. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  359. .cra_blocksize = TF_BLOCK_SIZE,
  360. .cra_ctxsize = sizeof(struct twofish_xts_ctx),
  361. .cra_alignmask = 0,
  362. .cra_type = &crypto_blkcipher_type,
  363. .cra_module = THIS_MODULE,
  364. .cra_u = {
  365. .blkcipher = {
  366. .min_keysize = TF_MIN_KEY_SIZE * 2,
  367. .max_keysize = TF_MAX_KEY_SIZE * 2,
  368. .ivsize = TF_BLOCK_SIZE,
  369. .setkey = xts_twofish_setkey,
  370. .encrypt = xts_encrypt,
  371. .decrypt = xts_decrypt,
  372. },
  373. },
  374. } };
  375. static bool is_blacklisted_cpu(void)
  376. {
  377. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  378. return false;
  379. if (boot_cpu_data.x86 == 0x06 &&
  380. (boot_cpu_data.x86_model == 0x1c ||
  381. boot_cpu_data.x86_model == 0x26 ||
  382. boot_cpu_data.x86_model == 0x36)) {
  383. /*
  384. * On Atom, twofish-3way is slower than original assembler
  385. * implementation. Twofish-3way trades off some performance in
  386. * storing blocks in 64bit registers to allow three blocks to
  387. * be processed parallel. Parallel operation then allows gaining
  388. * more performance than was trade off, on out-of-order CPUs.
  389. * However Atom does not benefit from this parallellism and
  390. * should be blacklisted.
  391. */
  392. return true;
  393. }
  394. if (boot_cpu_data.x86 == 0x0f) {
  395. /*
  396. * On Pentium 4, twofish-3way is slower than original assembler
  397. * implementation because excessive uses of 64bit rotate and
  398. * left-shifts (which are really slow on P4) needed to store and
  399. * handle 128bit block in two 64bit registers.
  400. */
  401. return true;
  402. }
  403. return false;
  404. }
  405. static int force;
  406. module_param(force, int, 0);
  407. MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
  408. static int __init init(void)
  409. {
  410. if (!force && is_blacklisted_cpu()) {
  411. printk(KERN_INFO
  412. "twofish-x86_64-3way: performance on this CPU "
  413. "would be suboptimal: disabling "
  414. "twofish-x86_64-3way.\n");
  415. return -ENODEV;
  416. }
  417. return crypto_register_algs(tf_algs, ARRAY_SIZE(tf_algs));
  418. }
  419. static void __exit fini(void)
  420. {
  421. crypto_unregister_algs(tf_algs, ARRAY_SIZE(tf_algs));
  422. }
  423. module_init(init);
  424. module_exit(fini);
  425. MODULE_LICENSE("GPL");
  426. MODULE_DESCRIPTION("Twofish Cipher Algorithm, 3-way parallel asm optimized");
  427. MODULE_ALIAS_CRYPTO("twofish");
  428. MODULE_ALIAS_CRYPTO("twofish-asm");