twofish_avx_glue.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Glue Code for AVX assembler version of Twofish Cipher
  3. *
  4. * Copyright (C) 2012 Johannes Goetzfried
  5. * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
  6. *
  7. * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  22. * USA
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <linux/crypto.h>
  28. #include <linux/err.h>
  29. #include <crypto/algapi.h>
  30. #include <crypto/internal/simd.h>
  31. #include <crypto/twofish.h>
  32. #include <crypto/xts.h>
  33. #include <asm/crypto/glue_helper.h>
  34. #include <asm/crypto/twofish.h>
  35. #define TWOFISH_PARALLEL_BLOCKS 8
  36. /* 8-way parallel cipher functions */
  37. asmlinkage void twofish_ecb_enc_8way(struct twofish_ctx *ctx, u8 *dst,
  38. const u8 *src);
  39. asmlinkage void twofish_ecb_dec_8way(struct twofish_ctx *ctx, u8 *dst,
  40. const u8 *src);
  41. asmlinkage void twofish_cbc_dec_8way(struct twofish_ctx *ctx, u8 *dst,
  42. const u8 *src);
  43. asmlinkage void twofish_ctr_8way(struct twofish_ctx *ctx, u8 *dst,
  44. const u8 *src, le128 *iv);
  45. asmlinkage void twofish_xts_enc_8way(struct twofish_ctx *ctx, u8 *dst,
  46. const u8 *src, le128 *iv);
  47. asmlinkage void twofish_xts_dec_8way(struct twofish_ctx *ctx, u8 *dst,
  48. const u8 *src, le128 *iv);
  49. static int twofish_setkey_skcipher(struct crypto_skcipher *tfm,
  50. const u8 *key, unsigned int keylen)
  51. {
  52. return twofish_setkey(&tfm->base, key, keylen);
  53. }
  54. static inline void twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst,
  55. const u8 *src)
  56. {
  57. __twofish_enc_blk_3way(ctx, dst, src, false);
  58. }
  59. static void twofish_xts_enc(void *ctx, u128 *dst, const u128 *src, le128 *iv)
  60. {
  61. glue_xts_crypt_128bit_one(ctx, dst, src, iv,
  62. GLUE_FUNC_CAST(twofish_enc_blk));
  63. }
  64. static void twofish_xts_dec(void *ctx, u128 *dst, const u128 *src, le128 *iv)
  65. {
  66. glue_xts_crypt_128bit_one(ctx, dst, src, iv,
  67. GLUE_FUNC_CAST(twofish_dec_blk));
  68. }
  69. struct twofish_xts_ctx {
  70. struct twofish_ctx tweak_ctx;
  71. struct twofish_ctx crypt_ctx;
  72. };
  73. static int xts_twofish_setkey(struct crypto_skcipher *tfm, const u8 *key,
  74. unsigned int keylen)
  75. {
  76. struct twofish_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
  77. u32 *flags = &tfm->base.crt_flags;
  78. int err;
  79. err = xts_verify_key(tfm, key, keylen);
  80. if (err)
  81. return err;
  82. /* first half of xts-key is for crypt */
  83. err = __twofish_setkey(&ctx->crypt_ctx, key, keylen / 2, flags);
  84. if (err)
  85. return err;
  86. /* second half of xts-key is for tweak */
  87. return __twofish_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2,
  88. flags);
  89. }
  90. static const struct common_glue_ctx twofish_enc = {
  91. .num_funcs = 3,
  92. .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
  93. .funcs = { {
  94. .num_blocks = TWOFISH_PARALLEL_BLOCKS,
  95. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_ecb_enc_8way) }
  96. }, {
  97. .num_blocks = 3,
  98. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk_3way) }
  99. }, {
  100. .num_blocks = 1,
  101. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk) }
  102. } }
  103. };
  104. static const struct common_glue_ctx twofish_ctr = {
  105. .num_funcs = 3,
  106. .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
  107. .funcs = { {
  108. .num_blocks = TWOFISH_PARALLEL_BLOCKS,
  109. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_ctr_8way) }
  110. }, {
  111. .num_blocks = 3,
  112. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr_3way) }
  113. }, {
  114. .num_blocks = 1,
  115. .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr) }
  116. } }
  117. };
  118. static const struct common_glue_ctx twofish_enc_xts = {
  119. .num_funcs = 2,
  120. .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
  121. .funcs = { {
  122. .num_blocks = TWOFISH_PARALLEL_BLOCKS,
  123. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_enc_8way) }
  124. }, {
  125. .num_blocks = 1,
  126. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_enc) }
  127. } }
  128. };
  129. static const struct common_glue_ctx twofish_dec = {
  130. .num_funcs = 3,
  131. .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
  132. .funcs = { {
  133. .num_blocks = TWOFISH_PARALLEL_BLOCKS,
  134. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_ecb_dec_8way) }
  135. }, {
  136. .num_blocks = 3,
  137. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk_3way) }
  138. }, {
  139. .num_blocks = 1,
  140. .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_dec_blk) }
  141. } }
  142. };
  143. static const struct common_glue_ctx twofish_dec_cbc = {
  144. .num_funcs = 3,
  145. .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
  146. .funcs = { {
  147. .num_blocks = TWOFISH_PARALLEL_BLOCKS,
  148. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_cbc_dec_8way) }
  149. }, {
  150. .num_blocks = 3,
  151. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk_cbc_3way) }
  152. }, {
  153. .num_blocks = 1,
  154. .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk) }
  155. } }
  156. };
  157. static const struct common_glue_ctx twofish_dec_xts = {
  158. .num_funcs = 2,
  159. .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
  160. .funcs = { {
  161. .num_blocks = TWOFISH_PARALLEL_BLOCKS,
  162. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_dec_8way) }
  163. }, {
  164. .num_blocks = 1,
  165. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_dec) }
  166. } }
  167. };
  168. static int ecb_encrypt(struct skcipher_request *req)
  169. {
  170. return glue_ecb_req_128bit(&twofish_enc, req);
  171. }
  172. static int ecb_decrypt(struct skcipher_request *req)
  173. {
  174. return glue_ecb_req_128bit(&twofish_dec, req);
  175. }
  176. static int cbc_encrypt(struct skcipher_request *req)
  177. {
  178. return glue_cbc_encrypt_req_128bit(GLUE_FUNC_CAST(twofish_enc_blk),
  179. req);
  180. }
  181. static int cbc_decrypt(struct skcipher_request *req)
  182. {
  183. return glue_cbc_decrypt_req_128bit(&twofish_dec_cbc, req);
  184. }
  185. static int ctr_crypt(struct skcipher_request *req)
  186. {
  187. return glue_ctr_req_128bit(&twofish_ctr, req);
  188. }
  189. static int xts_encrypt(struct skcipher_request *req)
  190. {
  191. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  192. struct twofish_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
  193. return glue_xts_req_128bit(&twofish_enc_xts, req,
  194. XTS_TWEAK_CAST(twofish_enc_blk),
  195. &ctx->tweak_ctx, &ctx->crypt_ctx);
  196. }
  197. static int xts_decrypt(struct skcipher_request *req)
  198. {
  199. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  200. struct twofish_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
  201. return glue_xts_req_128bit(&twofish_dec_xts, req,
  202. XTS_TWEAK_CAST(twofish_enc_blk),
  203. &ctx->tweak_ctx, &ctx->crypt_ctx);
  204. }
  205. static struct skcipher_alg twofish_algs[] = {
  206. {
  207. .base.cra_name = "__ecb(twofish)",
  208. .base.cra_driver_name = "__ecb-twofish-avx",
  209. .base.cra_priority = 400,
  210. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  211. .base.cra_blocksize = TF_BLOCK_SIZE,
  212. .base.cra_ctxsize = sizeof(struct twofish_ctx),
  213. .base.cra_module = THIS_MODULE,
  214. .min_keysize = TF_MIN_KEY_SIZE,
  215. .max_keysize = TF_MAX_KEY_SIZE,
  216. .setkey = twofish_setkey_skcipher,
  217. .encrypt = ecb_encrypt,
  218. .decrypt = ecb_decrypt,
  219. }, {
  220. .base.cra_name = "__cbc(twofish)",
  221. .base.cra_driver_name = "__cbc-twofish-avx",
  222. .base.cra_priority = 400,
  223. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  224. .base.cra_blocksize = TF_BLOCK_SIZE,
  225. .base.cra_ctxsize = sizeof(struct twofish_ctx),
  226. .base.cra_module = THIS_MODULE,
  227. .min_keysize = TF_MIN_KEY_SIZE,
  228. .max_keysize = TF_MAX_KEY_SIZE,
  229. .ivsize = TF_BLOCK_SIZE,
  230. .setkey = twofish_setkey_skcipher,
  231. .encrypt = cbc_encrypt,
  232. .decrypt = cbc_decrypt,
  233. }, {
  234. .base.cra_name = "__ctr(twofish)",
  235. .base.cra_driver_name = "__ctr-twofish-avx",
  236. .base.cra_priority = 400,
  237. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  238. .base.cra_blocksize = 1,
  239. .base.cra_ctxsize = sizeof(struct twofish_ctx),
  240. .base.cra_module = THIS_MODULE,
  241. .min_keysize = TF_MIN_KEY_SIZE,
  242. .max_keysize = TF_MAX_KEY_SIZE,
  243. .ivsize = TF_BLOCK_SIZE,
  244. .chunksize = TF_BLOCK_SIZE,
  245. .setkey = twofish_setkey_skcipher,
  246. .encrypt = ctr_crypt,
  247. .decrypt = ctr_crypt,
  248. }, {
  249. .base.cra_name = "__xts(twofish)",
  250. .base.cra_driver_name = "__xts-twofish-avx",
  251. .base.cra_priority = 400,
  252. .base.cra_flags = CRYPTO_ALG_INTERNAL,
  253. .base.cra_blocksize = TF_BLOCK_SIZE,
  254. .base.cra_ctxsize = sizeof(struct twofish_xts_ctx),
  255. .base.cra_module = THIS_MODULE,
  256. .min_keysize = 2 * TF_MIN_KEY_SIZE,
  257. .max_keysize = 2 * TF_MAX_KEY_SIZE,
  258. .ivsize = TF_BLOCK_SIZE,
  259. .setkey = xts_twofish_setkey,
  260. .encrypt = xts_encrypt,
  261. .decrypt = xts_decrypt,
  262. },
  263. };
  264. static struct simd_skcipher_alg *twofish_simd_algs[ARRAY_SIZE(twofish_algs)];
  265. static int __init twofish_init(void)
  266. {
  267. const char *feature_name;
  268. if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, &feature_name)) {
  269. pr_info("CPU feature '%s' is not supported.\n", feature_name);
  270. return -ENODEV;
  271. }
  272. return simd_register_skciphers_compat(twofish_algs,
  273. ARRAY_SIZE(twofish_algs),
  274. twofish_simd_algs);
  275. }
  276. static void __exit twofish_exit(void)
  277. {
  278. simd_unregister_skciphers(twofish_algs, ARRAY_SIZE(twofish_algs),
  279. twofish_simd_algs);
  280. }
  281. module_init(twofish_init);
  282. module_exit(twofish_exit);
  283. MODULE_DESCRIPTION("Twofish Cipher Algorithm, AVX optimized");
  284. MODULE_LICENSE("GPL");
  285. MODULE_ALIAS_CRYPTO("twofish");