]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/x86/crypto/twofish_avx_glue.c
crypto: x86/twofish-3way - remove LRW algorithm
[mirror_ubuntu-jammy-kernel.git] / arch / x86 / crypto / twofish_avx_glue.c
CommitLineData
107778b5
JG
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 *
18be4527
JK
7 * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
8 *
107778b5
JG
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
26#include <linux/module.h>
27#include <linux/hardirq.h>
28#include <linux/types.h>
29#include <linux/crypto.h>
30#include <linux/err.h>
801201aa 31#include <crypto/ablk_helper.h>
107778b5
JG
32#include <crypto/algapi.h>
33#include <crypto/twofish.h>
34#include <crypto/cryptd.h>
35#include <crypto/b128ops.h>
36#include <crypto/ctr.h>
37#include <crypto/lrw.h>
38#include <crypto/xts.h>
df6b35f4 39#include <asm/fpu/api.h>
a7378d4e 40#include <asm/crypto/twofish.h>
a7378d4e 41#include <asm/crypto/glue_helper.h>
107778b5
JG
42#include <crypto/scatterwalk.h>
43#include <linux/workqueue.h>
44#include <linux/spinlock.h>
45
107778b5
JG
46#define TWOFISH_PARALLEL_BLOCKS 8
47
107778b5 48/* 8-way parallel cipher functions */
8435a3c3
JK
49asmlinkage void twofish_ecb_enc_8way(struct twofish_ctx *ctx, u8 *dst,
50 const u8 *src);
51asmlinkage void twofish_ecb_dec_8way(struct twofish_ctx *ctx, u8 *dst,
107778b5
JG
52 const u8 *src);
53
8435a3c3
JK
54asmlinkage void twofish_cbc_dec_8way(struct twofish_ctx *ctx, u8 *dst,
55 const u8 *src);
56asmlinkage void twofish_ctr_8way(struct twofish_ctx *ctx, u8 *dst,
57 const u8 *src, le128 *iv);
107778b5 58
18be4527
JK
59asmlinkage void twofish_xts_enc_8way(struct twofish_ctx *ctx, u8 *dst,
60 const u8 *src, le128 *iv);
61asmlinkage void twofish_xts_dec_8way(struct twofish_ctx *ctx, u8 *dst,
62 const u8 *src, le128 *iv);
63
8435a3c3 64static inline void twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst,
107778b5
JG
65 const u8 *src)
66{
8435a3c3 67 __twofish_enc_blk_3way(ctx, dst, src, false);
107778b5
JG
68}
69
99f42f93 70static void twofish_xts_enc(void *ctx, u128 *dst, const u128 *src, le128 *iv)
18be4527
JK
71{
72 glue_xts_crypt_128bit_one(ctx, dst, src, iv,
73 GLUE_FUNC_CAST(twofish_enc_blk));
74}
75
99f42f93 76static void twofish_xts_dec(void *ctx, u128 *dst, const u128 *src, le128 *iv)
18be4527
JK
77{
78 glue_xts_crypt_128bit_one(ctx, dst, src, iv,
79 GLUE_FUNC_CAST(twofish_dec_blk));
80}
81
107778b5 82
a7378d4e
JK
83static const struct common_glue_ctx twofish_enc = {
84 .num_funcs = 3,
85 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
86
87 .funcs = { {
88 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
8435a3c3 89 .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_ecb_enc_8way) }
a7378d4e
JK
90 }, {
91 .num_blocks = 3,
92 .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk_3way) }
93 }, {
94 .num_blocks = 1,
95 .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_enc_blk) }
96 } }
97};
98
99static const struct common_glue_ctx twofish_ctr = {
100 .num_funcs = 3,
101 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
102
103 .funcs = { {
104 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
8435a3c3 105 .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_ctr_8way) }
a7378d4e
JK
106 }, {
107 .num_blocks = 3,
108 .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr_3way) }
109 }, {
110 .num_blocks = 1,
111 .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(twofish_enc_blk_ctr) }
112 } }
113};
114
18be4527
JK
115static const struct common_glue_ctx twofish_enc_xts = {
116 .num_funcs = 2,
117 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
118
119 .funcs = { {
120 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
121 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_enc_8way) }
122 }, {
123 .num_blocks = 1,
124 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_enc) }
125 } }
126};
127
a7378d4e
JK
128static const struct common_glue_ctx twofish_dec = {
129 .num_funcs = 3,
130 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
131
132 .funcs = { {
133 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
8435a3c3 134 .fn_u = { .ecb = GLUE_FUNC_CAST(twofish_ecb_dec_8way) }
a7378d4e
JK
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
144static const struct common_glue_ctx twofish_dec_cbc = {
145 .num_funcs = 3,
146 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
147
148 .funcs = { {
149 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
8435a3c3 150 .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_cbc_dec_8way) }
a7378d4e
JK
151 }, {
152 .num_blocks = 3,
153 .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk_cbc_3way) }
154 }, {
155 .num_blocks = 1,
156 .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(twofish_dec_blk) }
157 } }
158};
159
18be4527
JK
160static const struct common_glue_ctx twofish_dec_xts = {
161 .num_funcs = 2,
162 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
163
164 .funcs = { {
165 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
166 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_dec_8way) }
167 }, {
168 .num_blocks = 1,
169 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_dec) }
170 } }
171};
172
107778b5
JG
173static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
174 struct scatterlist *src, unsigned int nbytes)
175{
a7378d4e 176 return glue_ecb_crypt_128bit(&twofish_enc, desc, dst, src, nbytes);
107778b5
JG
177}
178
179static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
180 struct scatterlist *src, unsigned int nbytes)
181{
a7378d4e 182 return glue_ecb_crypt_128bit(&twofish_dec, desc, dst, src, nbytes);
107778b5
JG
183}
184
185static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
186 struct scatterlist *src, unsigned int nbytes)
187{
a7378d4e
JK
188 return glue_cbc_encrypt_128bit(GLUE_FUNC_CAST(twofish_enc_blk), desc,
189 dst, src, nbytes);
107778b5
JG
190}
191
192static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
193 struct scatterlist *src, unsigned int nbytes)
194{
a7378d4e
JK
195 return glue_cbc_decrypt_128bit(&twofish_dec_cbc, desc, dst, src,
196 nbytes);
107778b5
JG
197}
198
a7378d4e
JK
199static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
200 struct scatterlist *src, unsigned int nbytes)
107778b5 201{
a7378d4e 202 return glue_ctr_crypt_128bit(&twofish_ctr, desc, dst, src, nbytes);
107778b5
JG
203}
204
a7378d4e 205static inline bool twofish_fpu_begin(bool fpu_enabled, unsigned int nbytes)
107778b5 206{
a7378d4e
JK
207 return glue_fpu_begin(TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS, NULL,
208 fpu_enabled, nbytes);
107778b5
JG
209}
210
a7378d4e 211static inline void twofish_fpu_end(bool fpu_enabled)
107778b5 212{
a7378d4e 213 glue_fpu_end(fpu_enabled);
107778b5
JG
214}
215
216struct crypt_priv {
217 struct twofish_ctx *ctx;
218 bool fpu_enabled;
219};
220
221static void encrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
222{
223 const unsigned int bsize = TF_BLOCK_SIZE;
224 struct crypt_priv *ctx = priv;
225 int i;
226
227 ctx->fpu_enabled = twofish_fpu_begin(ctx->fpu_enabled, nbytes);
228
229 if (nbytes == bsize * TWOFISH_PARALLEL_BLOCKS) {
8435a3c3 230 twofish_ecb_enc_8way(ctx->ctx, srcdst, srcdst);
107778b5
JG
231 return;
232 }
233
234 for (i = 0; i < nbytes / (bsize * 3); i++, srcdst += bsize * 3)
235 twofish_enc_blk_3way(ctx->ctx, srcdst, srcdst);
236
237 nbytes %= bsize * 3;
238
239 for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
240 twofish_enc_blk(ctx->ctx, srcdst, srcdst);
241}
242
243static void decrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
244{
245 const unsigned int bsize = TF_BLOCK_SIZE;
246 struct crypt_priv *ctx = priv;
247 int i;
248
249 ctx->fpu_enabled = twofish_fpu_begin(ctx->fpu_enabled, nbytes);
250
251 if (nbytes == bsize * TWOFISH_PARALLEL_BLOCKS) {
8435a3c3 252 twofish_ecb_dec_8way(ctx->ctx, srcdst, srcdst);
107778b5
JG
253 return;
254 }
255
256 for (i = 0; i < nbytes / (bsize * 3); i++, srcdst += bsize * 3)
257 twofish_dec_blk_3way(ctx->ctx, srcdst, srcdst);
258
259 nbytes %= bsize * 3;
260
261 for (i = 0; i < nbytes / bsize; i++, srcdst += bsize)
262 twofish_dec_blk(ctx->ctx, srcdst, srcdst);
263}
264
68bfc492
EB
265struct twofish_lrw_ctx {
266 struct lrw_table_ctx lrw_table;
267 struct twofish_ctx twofish_ctx;
268};
269
270static int lrw_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
271 unsigned int keylen)
272{
273 struct twofish_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
274 int err;
275
276 err = __twofish_setkey(&ctx->twofish_ctx, key, keylen - TF_BLOCK_SIZE,
277 &tfm->crt_flags);
278 if (err)
279 return err;
280
281 return lrw_init_table(&ctx->lrw_table, key + keylen - TF_BLOCK_SIZE);
282}
283
284static void lrw_twofish_exit_tfm(struct crypto_tfm *tfm)
285{
286 struct twofish_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
287
288 lrw_free_table(&ctx->lrw_table);
289}
290
107778b5
JG
291static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
292 struct scatterlist *src, unsigned int nbytes)
293{
294 struct twofish_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
295 be128 buf[TWOFISH_PARALLEL_BLOCKS];
296 struct crypt_priv crypt_ctx = {
297 .ctx = &ctx->twofish_ctx,
298 .fpu_enabled = false,
299 };
300 struct lrw_crypt_req req = {
301 .tbuf = buf,
302 .tbuflen = sizeof(buf),
303
304 .table_ctx = &ctx->lrw_table,
305 .crypt_ctx = &crypt_ctx,
306 .crypt_fn = encrypt_callback,
307 };
308 int ret;
309
310 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
311 ret = lrw_crypt(desc, dst, src, nbytes, &req);
312 twofish_fpu_end(crypt_ctx.fpu_enabled);
313
314 return ret;
315}
316
317static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
318 struct scatterlist *src, unsigned int nbytes)
319{
320 struct twofish_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
321 be128 buf[TWOFISH_PARALLEL_BLOCKS];
322 struct crypt_priv crypt_ctx = {
323 .ctx = &ctx->twofish_ctx,
324 .fpu_enabled = false,
325 };
326 struct lrw_crypt_req req = {
327 .tbuf = buf,
328 .tbuflen = sizeof(buf),
329
330 .table_ctx = &ctx->lrw_table,
331 .crypt_ctx = &crypt_ctx,
332 .crypt_fn = decrypt_callback,
333 };
334 int ret;
335
336 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
337 ret = lrw_crypt(desc, dst, src, nbytes, &req);
338 twofish_fpu_end(crypt_ctx.fpu_enabled);
339
340 return ret;
341}
342
107778b5
JG
343static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
344 struct scatterlist *src, unsigned int nbytes)
345{
346 struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
107778b5 347
18be4527
JK
348 return glue_xts_crypt_128bit(&twofish_enc_xts, desc, dst, src, nbytes,
349 XTS_TWEAK_CAST(twofish_enc_blk),
350 &ctx->tweak_ctx, &ctx->crypt_ctx);
107778b5
JG
351}
352
353static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
354 struct scatterlist *src, unsigned int nbytes)
355{
356 struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
107778b5 357
18be4527
JK
358 return glue_xts_crypt_128bit(&twofish_dec_xts, desc, dst, src, nbytes,
359 XTS_TWEAK_CAST(twofish_enc_blk),
360 &ctx->tweak_ctx, &ctx->crypt_ctx);
107778b5
JG
361}
362
107778b5
JG
363static struct crypto_alg twofish_algs[10] = { {
364 .cra_name = "__ecb-twofish-avx",
365 .cra_driver_name = "__driver-ecb-twofish-avx",
366 .cra_priority = 0,
4dda66f6
SM
367 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
368 CRYPTO_ALG_INTERNAL,
107778b5
JG
369 .cra_blocksize = TF_BLOCK_SIZE,
370 .cra_ctxsize = sizeof(struct twofish_ctx),
371 .cra_alignmask = 0,
372 .cra_type = &crypto_blkcipher_type,
373 .cra_module = THIS_MODULE,
107778b5
JG
374 .cra_u = {
375 .blkcipher = {
376 .min_keysize = TF_MIN_KEY_SIZE,
377 .max_keysize = TF_MAX_KEY_SIZE,
378 .setkey = twofish_setkey,
379 .encrypt = ecb_encrypt,
380 .decrypt = ecb_decrypt,
381 },
382 },
383}, {
384 .cra_name = "__cbc-twofish-avx",
385 .cra_driver_name = "__driver-cbc-twofish-avx",
386 .cra_priority = 0,
4dda66f6
SM
387 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
388 CRYPTO_ALG_INTERNAL,
107778b5
JG
389 .cra_blocksize = TF_BLOCK_SIZE,
390 .cra_ctxsize = sizeof(struct twofish_ctx),
391 .cra_alignmask = 0,
392 .cra_type = &crypto_blkcipher_type,
393 .cra_module = THIS_MODULE,
107778b5
JG
394 .cra_u = {
395 .blkcipher = {
396 .min_keysize = TF_MIN_KEY_SIZE,
397 .max_keysize = TF_MAX_KEY_SIZE,
398 .setkey = twofish_setkey,
399 .encrypt = cbc_encrypt,
400 .decrypt = cbc_decrypt,
401 },
402 },
403}, {
404 .cra_name = "__ctr-twofish-avx",
405 .cra_driver_name = "__driver-ctr-twofish-avx",
406 .cra_priority = 0,
4dda66f6
SM
407 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
408 CRYPTO_ALG_INTERNAL,
107778b5
JG
409 .cra_blocksize = 1,
410 .cra_ctxsize = sizeof(struct twofish_ctx),
411 .cra_alignmask = 0,
412 .cra_type = &crypto_blkcipher_type,
413 .cra_module = THIS_MODULE,
107778b5
JG
414 .cra_u = {
415 .blkcipher = {
416 .min_keysize = TF_MIN_KEY_SIZE,
417 .max_keysize = TF_MAX_KEY_SIZE,
418 .ivsize = TF_BLOCK_SIZE,
419 .setkey = twofish_setkey,
420 .encrypt = ctr_crypt,
421 .decrypt = ctr_crypt,
422 },
423 },
424}, {
425 .cra_name = "__lrw-twofish-avx",
426 .cra_driver_name = "__driver-lrw-twofish-avx",
427 .cra_priority = 0,
4dda66f6
SM
428 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
429 CRYPTO_ALG_INTERNAL,
107778b5
JG
430 .cra_blocksize = TF_BLOCK_SIZE,
431 .cra_ctxsize = sizeof(struct twofish_lrw_ctx),
432 .cra_alignmask = 0,
433 .cra_type = &crypto_blkcipher_type,
434 .cra_module = THIS_MODULE,
a7378d4e 435 .cra_exit = lrw_twofish_exit_tfm,
107778b5
JG
436 .cra_u = {
437 .blkcipher = {
438 .min_keysize = TF_MIN_KEY_SIZE +
439 TF_BLOCK_SIZE,
440 .max_keysize = TF_MAX_KEY_SIZE +
441 TF_BLOCK_SIZE,
442 .ivsize = TF_BLOCK_SIZE,
443 .setkey = lrw_twofish_setkey,
444 .encrypt = lrw_encrypt,
445 .decrypt = lrw_decrypt,
446 },
447 },
448}, {
449 .cra_name = "__xts-twofish-avx",
450 .cra_driver_name = "__driver-xts-twofish-avx",
451 .cra_priority = 0,
4dda66f6
SM
452 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
453 CRYPTO_ALG_INTERNAL,
107778b5
JG
454 .cra_blocksize = TF_BLOCK_SIZE,
455 .cra_ctxsize = sizeof(struct twofish_xts_ctx),
456 .cra_alignmask = 0,
457 .cra_type = &crypto_blkcipher_type,
458 .cra_module = THIS_MODULE,
107778b5
JG
459 .cra_u = {
460 .blkcipher = {
461 .min_keysize = TF_MIN_KEY_SIZE * 2,
462 .max_keysize = TF_MAX_KEY_SIZE * 2,
463 .ivsize = TF_BLOCK_SIZE,
464 .setkey = xts_twofish_setkey,
465 .encrypt = xts_encrypt,
466 .decrypt = xts_decrypt,
467 },
468 },
469}, {
470 .cra_name = "ecb(twofish)",
471 .cra_driver_name = "ecb-twofish-avx",
472 .cra_priority = 400,
473 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
474 .cra_blocksize = TF_BLOCK_SIZE,
30a04008 475 .cra_ctxsize = sizeof(struct async_helper_ctx),
107778b5
JG
476 .cra_alignmask = 0,
477 .cra_type = &crypto_ablkcipher_type,
478 .cra_module = THIS_MODULE,
107778b5
JG
479 .cra_init = ablk_init,
480 .cra_exit = ablk_exit,
481 .cra_u = {
482 .ablkcipher = {
483 .min_keysize = TF_MIN_KEY_SIZE,
484 .max_keysize = TF_MAX_KEY_SIZE,
485 .setkey = ablk_set_key,
486 .encrypt = ablk_encrypt,
487 .decrypt = ablk_decrypt,
488 },
489 },
490}, {
491 .cra_name = "cbc(twofish)",
492 .cra_driver_name = "cbc-twofish-avx",
493 .cra_priority = 400,
494 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
495 .cra_blocksize = TF_BLOCK_SIZE,
30a04008 496 .cra_ctxsize = sizeof(struct async_helper_ctx),
107778b5
JG
497 .cra_alignmask = 0,
498 .cra_type = &crypto_ablkcipher_type,
499 .cra_module = THIS_MODULE,
107778b5
JG
500 .cra_init = ablk_init,
501 .cra_exit = ablk_exit,
502 .cra_u = {
503 .ablkcipher = {
504 .min_keysize = TF_MIN_KEY_SIZE,
505 .max_keysize = TF_MAX_KEY_SIZE,
506 .ivsize = TF_BLOCK_SIZE,
507 .setkey = ablk_set_key,
508 .encrypt = __ablk_encrypt,
509 .decrypt = ablk_decrypt,
510 },
511 },
512}, {
513 .cra_name = "ctr(twofish)",
514 .cra_driver_name = "ctr-twofish-avx",
515 .cra_priority = 400,
516 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
517 .cra_blocksize = 1,
30a04008 518 .cra_ctxsize = sizeof(struct async_helper_ctx),
107778b5
JG
519 .cra_alignmask = 0,
520 .cra_type = &crypto_ablkcipher_type,
521 .cra_module = THIS_MODULE,
107778b5
JG
522 .cra_init = ablk_init,
523 .cra_exit = ablk_exit,
524 .cra_u = {
525 .ablkcipher = {
526 .min_keysize = TF_MIN_KEY_SIZE,
527 .max_keysize = TF_MAX_KEY_SIZE,
528 .ivsize = TF_BLOCK_SIZE,
529 .setkey = ablk_set_key,
530 .encrypt = ablk_encrypt,
531 .decrypt = ablk_encrypt,
532 .geniv = "chainiv",
533 },
534 },
535}, {
536 .cra_name = "lrw(twofish)",
537 .cra_driver_name = "lrw-twofish-avx",
538 .cra_priority = 400,
539 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
540 .cra_blocksize = TF_BLOCK_SIZE,
30a04008 541 .cra_ctxsize = sizeof(struct async_helper_ctx),
107778b5
JG
542 .cra_alignmask = 0,
543 .cra_type = &crypto_ablkcipher_type,
544 .cra_module = THIS_MODULE,
107778b5
JG
545 .cra_init = ablk_init,
546 .cra_exit = ablk_exit,
547 .cra_u = {
548 .ablkcipher = {
549 .min_keysize = TF_MIN_KEY_SIZE +
550 TF_BLOCK_SIZE,
551 .max_keysize = TF_MAX_KEY_SIZE +
552 TF_BLOCK_SIZE,
553 .ivsize = TF_BLOCK_SIZE,
554 .setkey = ablk_set_key,
555 .encrypt = ablk_encrypt,
556 .decrypt = ablk_decrypt,
557 },
558 },
559}, {
560 .cra_name = "xts(twofish)",
561 .cra_driver_name = "xts-twofish-avx",
562 .cra_priority = 400,
563 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
564 .cra_blocksize = TF_BLOCK_SIZE,
30a04008 565 .cra_ctxsize = sizeof(struct async_helper_ctx),
107778b5
JG
566 .cra_alignmask = 0,
567 .cra_type = &crypto_ablkcipher_type,
568 .cra_module = THIS_MODULE,
107778b5
JG
569 .cra_init = ablk_init,
570 .cra_exit = ablk_exit,
571 .cra_u = {
572 .ablkcipher = {
573 .min_keysize = TF_MIN_KEY_SIZE * 2,
574 .max_keysize = TF_MAX_KEY_SIZE * 2,
575 .ivsize = TF_BLOCK_SIZE,
576 .setkey = ablk_set_key,
577 .encrypt = ablk_encrypt,
578 .decrypt = ablk_decrypt,
579 },
580 },
581} };
582
583static int __init twofish_init(void)
584{
4eecd261 585 const char *feature_name;
107778b5 586
158ecc39 587 if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, &feature_name)) {
4eecd261 588 pr_info("CPU feature '%s' is not supported.\n", feature_name);
107778b5
JG
589 return -ENODEV;
590 }
591
592 return crypto_register_algs(twofish_algs, ARRAY_SIZE(twofish_algs));
593}
594
595static void __exit twofish_exit(void)
596{
597 crypto_unregister_algs(twofish_algs, ARRAY_SIZE(twofish_algs));
598}
599
600module_init(twofish_init);
601module_exit(twofish_exit);
602
603MODULE_DESCRIPTION("Twofish Cipher Algorithm, AVX optimized");
604MODULE_LICENSE("GPL");
5d26a105 605MODULE_ALIAS_CRYPTO("twofish");