1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Glue Code for AVX assembler version of Twofish Cipher
5 * Copyright (C) 2012 Johannes Goetzfried
6 * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
8 * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/crypto.h>
14 #include <linux/err.h>
15 #include <crypto/algapi.h>
16 #include <crypto/internal/simd.h>
17 #include <crypto/twofish.h>
18 #include <asm/crypto/glue_helper.h>
19 #include <asm/crypto/twofish.h>
21 #define TWOFISH_PARALLEL_BLOCKS 8
23 /* 8-way parallel cipher functions */
24 asmlinkage void twofish_ecb_enc_8way(const void *ctx, u8 *dst, const u8 *src);
25 asmlinkage void twofish_ecb_dec_8way(const void *ctx, u8 *dst, const u8 *src);
27 asmlinkage void twofish_cbc_dec_8way(const void *ctx, u8 *dst, const u8 *src);
29 static int twofish_setkey_skcipher(struct crypto_skcipher *tfm,
30 const u8 *key, unsigned int keylen)
32 return twofish_setkey(&tfm->base, key, keylen);
35 static inline void twofish_enc_blk_3way(const void *ctx, u8 *dst, const u8 *src)
37 __twofish_enc_blk_3way(ctx, dst, src, false);
40 static const struct common_glue_ctx twofish_enc = {
42 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
45 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
46 .fn_u = { .ecb = twofish_ecb_enc_8way }
49 .fn_u = { .ecb = twofish_enc_blk_3way }
52 .fn_u = { .ecb = twofish_enc_blk }
56 static const struct common_glue_ctx twofish_dec = {
58 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
61 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
62 .fn_u = { .ecb = twofish_ecb_dec_8way }
65 .fn_u = { .ecb = twofish_dec_blk_3way }
68 .fn_u = { .ecb = twofish_dec_blk }
72 static const struct common_glue_ctx twofish_dec_cbc = {
74 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
77 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
78 .fn_u = { .cbc = twofish_cbc_dec_8way }
81 .fn_u = { .cbc = twofish_dec_blk_cbc_3way }
84 .fn_u = { .cbc = twofish_dec_blk }
88 static int ecb_encrypt(struct skcipher_request *req)
90 return glue_ecb_req_128bit(&twofish_enc, req);
93 static int ecb_decrypt(struct skcipher_request *req)
95 return glue_ecb_req_128bit(&twofish_dec, req);
98 static int cbc_encrypt(struct skcipher_request *req)
100 return glue_cbc_encrypt_req_128bit(twofish_enc_blk, req);
103 static int cbc_decrypt(struct skcipher_request *req)
105 return glue_cbc_decrypt_req_128bit(&twofish_dec_cbc, req);
108 static struct skcipher_alg twofish_algs[] = {
110 .base.cra_name = "__ecb(twofish)",
111 .base.cra_driver_name = "__ecb-twofish-avx",
112 .base.cra_priority = 400,
113 .base.cra_flags = CRYPTO_ALG_INTERNAL,
114 .base.cra_blocksize = TF_BLOCK_SIZE,
115 .base.cra_ctxsize = sizeof(struct twofish_ctx),
116 .base.cra_module = THIS_MODULE,
117 .min_keysize = TF_MIN_KEY_SIZE,
118 .max_keysize = TF_MAX_KEY_SIZE,
119 .setkey = twofish_setkey_skcipher,
120 .encrypt = ecb_encrypt,
121 .decrypt = ecb_decrypt,
123 .base.cra_name = "__cbc(twofish)",
124 .base.cra_driver_name = "__cbc-twofish-avx",
125 .base.cra_priority = 400,
126 .base.cra_flags = CRYPTO_ALG_INTERNAL,
127 .base.cra_blocksize = TF_BLOCK_SIZE,
128 .base.cra_ctxsize = sizeof(struct twofish_ctx),
129 .base.cra_module = THIS_MODULE,
130 .min_keysize = TF_MIN_KEY_SIZE,
131 .max_keysize = TF_MAX_KEY_SIZE,
132 .ivsize = TF_BLOCK_SIZE,
133 .setkey = twofish_setkey_skcipher,
134 .encrypt = cbc_encrypt,
135 .decrypt = cbc_decrypt,
139 static struct simd_skcipher_alg *twofish_simd_algs[ARRAY_SIZE(twofish_algs)];
141 static int __init twofish_init(void)
143 const char *feature_name;
145 if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, &feature_name)) {
146 pr_info("CPU feature '%s' is not supported.\n", feature_name);
150 return simd_register_skciphers_compat(twofish_algs,
151 ARRAY_SIZE(twofish_algs),
155 static void __exit twofish_exit(void)
157 simd_unregister_skciphers(twofish_algs, ARRAY_SIZE(twofish_algs),
161 module_init(twofish_init);
162 module_exit(twofish_exit);
164 MODULE_DESCRIPTION("Twofish Cipher Algorithm, AVX optimized");
165 MODULE_LICENSE("GPL");
166 MODULE_ALIAS_CRYPTO("twofish");