twofish_glue_3way.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. /* key consists of keys of equal size concatenated, therefore
  233. * the length must be even
  234. */
  235. if (keylen % 2) {
  236. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  237. return -EINVAL;
  238. }
  239. /* first half of xts-key is for crypt */
  240. err = __twofish_setkey(&ctx->crypt_ctx, key, keylen / 2, flags);
  241. if (err)
  242. return err;
  243. /* second half of xts-key is for tweak */
  244. return __twofish_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2,
  245. flags);
  246. }
  247. EXPORT_SYMBOL_GPL(xts_twofish_setkey);
  248. static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  249. struct scatterlist *src, unsigned int nbytes)
  250. {
  251. struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  252. be128 buf[3];
  253. struct xts_crypt_req req = {
  254. .tbuf = buf,
  255. .tbuflen = sizeof(buf),
  256. .tweak_ctx = &ctx->tweak_ctx,
  257. .tweak_fn = XTS_TWEAK_CAST(twofish_enc_blk),
  258. .crypt_ctx = &ctx->crypt_ctx,
  259. .crypt_fn = encrypt_callback,
  260. };
  261. return xts_crypt(desc, dst, src, nbytes, &req);
  262. }
  263. static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  264. struct scatterlist *src, unsigned int nbytes)
  265. {
  266. struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  267. be128 buf[3];
  268. struct xts_crypt_req req = {
  269. .tbuf = buf,
  270. .tbuflen = sizeof(buf),
  271. .tweak_ctx = &ctx->tweak_ctx,
  272. .tweak_fn = XTS_TWEAK_CAST(twofish_enc_blk),
  273. .crypt_ctx = &ctx->crypt_ctx,
  274. .crypt_fn = decrypt_callback,
  275. };
  276. return xts_crypt(desc, dst, src, nbytes, &req);
  277. }
  278. static struct crypto_alg tf_algs[5] = { {
  279. .cra_name = "ecb(twofish)",
  280. .cra_driver_name = "ecb-twofish-3way",
  281. .cra_priority = 300,
  282. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  283. .cra_blocksize = TF_BLOCK_SIZE,
  284. .cra_ctxsize = sizeof(struct twofish_ctx),
  285. .cra_alignmask = 0,
  286. .cra_type = &crypto_blkcipher_type,
  287. .cra_module = THIS_MODULE,
  288. .cra_u = {
  289. .blkcipher = {
  290. .min_keysize = TF_MIN_KEY_SIZE,
  291. .max_keysize = TF_MAX_KEY_SIZE,
  292. .setkey = twofish_setkey,
  293. .encrypt = ecb_encrypt,
  294. .decrypt = ecb_decrypt,
  295. },
  296. },
  297. }, {
  298. .cra_name = "cbc(twofish)",
  299. .cra_driver_name = "cbc-twofish-3way",
  300. .cra_priority = 300,
  301. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  302. .cra_blocksize = TF_BLOCK_SIZE,
  303. .cra_ctxsize = sizeof(struct twofish_ctx),
  304. .cra_alignmask = 0,
  305. .cra_type = &crypto_blkcipher_type,
  306. .cra_module = THIS_MODULE,
  307. .cra_u = {
  308. .blkcipher = {
  309. .min_keysize = TF_MIN_KEY_SIZE,
  310. .max_keysize = TF_MAX_KEY_SIZE,
  311. .ivsize = TF_BLOCK_SIZE,
  312. .setkey = twofish_setkey,
  313. .encrypt = cbc_encrypt,
  314. .decrypt = cbc_decrypt,
  315. },
  316. },
  317. }, {
  318. .cra_name = "ctr(twofish)",
  319. .cra_driver_name = "ctr-twofish-3way",
  320. .cra_priority = 300,
  321. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  322. .cra_blocksize = 1,
  323. .cra_ctxsize = sizeof(struct twofish_ctx),
  324. .cra_alignmask = 0,
  325. .cra_type = &crypto_blkcipher_type,
  326. .cra_module = THIS_MODULE,
  327. .cra_u = {
  328. .blkcipher = {
  329. .min_keysize = TF_MIN_KEY_SIZE,
  330. .max_keysize = TF_MAX_KEY_SIZE,
  331. .ivsize = TF_BLOCK_SIZE,
  332. .setkey = twofish_setkey,
  333. .encrypt = ctr_crypt,
  334. .decrypt = ctr_crypt,
  335. },
  336. },
  337. }, {
  338. .cra_name = "lrw(twofish)",
  339. .cra_driver_name = "lrw-twofish-3way",
  340. .cra_priority = 300,
  341. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  342. .cra_blocksize = TF_BLOCK_SIZE,
  343. .cra_ctxsize = sizeof(struct twofish_lrw_ctx),
  344. .cra_alignmask = 0,
  345. .cra_type = &crypto_blkcipher_type,
  346. .cra_module = THIS_MODULE,
  347. .cra_exit = lrw_twofish_exit_tfm,
  348. .cra_u = {
  349. .blkcipher = {
  350. .min_keysize = TF_MIN_KEY_SIZE + TF_BLOCK_SIZE,
  351. .max_keysize = TF_MAX_KEY_SIZE + TF_BLOCK_SIZE,
  352. .ivsize = TF_BLOCK_SIZE,
  353. .setkey = lrw_twofish_setkey,
  354. .encrypt = lrw_encrypt,
  355. .decrypt = lrw_decrypt,
  356. },
  357. },
  358. }, {
  359. .cra_name = "xts(twofish)",
  360. .cra_driver_name = "xts-twofish-3way",
  361. .cra_priority = 300,
  362. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  363. .cra_blocksize = TF_BLOCK_SIZE,
  364. .cra_ctxsize = sizeof(struct twofish_xts_ctx),
  365. .cra_alignmask = 0,
  366. .cra_type = &crypto_blkcipher_type,
  367. .cra_module = THIS_MODULE,
  368. .cra_u = {
  369. .blkcipher = {
  370. .min_keysize = TF_MIN_KEY_SIZE * 2,
  371. .max_keysize = TF_MAX_KEY_SIZE * 2,
  372. .ivsize = TF_BLOCK_SIZE,
  373. .setkey = xts_twofish_setkey,
  374. .encrypt = xts_encrypt,
  375. .decrypt = xts_decrypt,
  376. },
  377. },
  378. } };
  379. static bool is_blacklisted_cpu(void)
  380. {
  381. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  382. return false;
  383. if (boot_cpu_data.x86 == 0x06 &&
  384. (boot_cpu_data.x86_model == 0x1c ||
  385. boot_cpu_data.x86_model == 0x26 ||
  386. boot_cpu_data.x86_model == 0x36)) {
  387. /*
  388. * On Atom, twofish-3way is slower than original assembler
  389. * implementation. Twofish-3way trades off some performance in
  390. * storing blocks in 64bit registers to allow three blocks to
  391. * be processed parallel. Parallel operation then allows gaining
  392. * more performance than was trade off, on out-of-order CPUs.
  393. * However Atom does not benefit from this parallellism and
  394. * should be blacklisted.
  395. */
  396. return true;
  397. }
  398. if (boot_cpu_data.x86 == 0x0f) {
  399. /*
  400. * On Pentium 4, twofish-3way is slower than original assembler
  401. * implementation because excessive uses of 64bit rotate and
  402. * left-shifts (which are really slow on P4) needed to store and
  403. * handle 128bit block in two 64bit registers.
  404. */
  405. return true;
  406. }
  407. return false;
  408. }
  409. static int force;
  410. module_param(force, int, 0);
  411. MODULE_PARM_DESC(force, "Force module load, ignore CPU blacklist");
  412. static int __init init(void)
  413. {
  414. if (!force && is_blacklisted_cpu()) {
  415. printk(KERN_INFO
  416. "twofish-x86_64-3way: performance on this CPU "
  417. "would be suboptimal: disabling "
  418. "twofish-x86_64-3way.\n");
  419. return -ENODEV;
  420. }
  421. return crypto_register_algs(tf_algs, ARRAY_SIZE(tf_algs));
  422. }
  423. static void __exit fini(void)
  424. {
  425. crypto_unregister_algs(tf_algs, ARRAY_SIZE(tf_algs));
  426. }
  427. module_init(init);
  428. module_exit(fini);
  429. MODULE_LICENSE("GPL");
  430. MODULE_DESCRIPTION("Twofish Cipher Algorithm, 3-way parallel asm optimized");
  431. MODULE_ALIAS_CRYPTO("twofish");
  432. MODULE_ALIAS_CRYPTO("twofish-asm");