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 <crypto/xts.h>
19 #include <asm/crypto/glue_helper.h>
20 #include <asm/crypto/twofish.h>
22 #define TWOFISH_PARALLEL_BLOCKS 8
24 /* 8-way parallel cipher functions */
25 asmlinkage void twofish_ecb_enc_8way(const void *ctx, u8 *dst, const u8 *src);
26 asmlinkage void twofish_ecb_dec_8way(const void *ctx, u8 *dst, const u8 *src);
28 asmlinkage void twofish_cbc_dec_8way(const void *ctx, u8 *dst, const u8 *src);
29 asmlinkage void twofish_ctr_8way(const void *ctx, u8 *dst, const u8 *src,
32 asmlinkage void twofish_xts_enc_8way(const void *ctx, u8 *dst, const u8 *src,
34 asmlinkage void twofish_xts_dec_8way(const void *ctx, u8 *dst, const u8 *src,
37 static int twofish_setkey_skcipher(struct crypto_skcipher *tfm,
38 const u8 *key, unsigned int keylen)
40 return twofish_setkey(&tfm->base, key, keylen);
43 static inline void twofish_enc_blk_3way(const void *ctx, u8 *dst, const u8 *src)
45 __twofish_enc_blk_3way(ctx, dst, src, false);
48 static void twofish_xts_enc(const void *ctx, u8 *dst, const u8 *src, le128 *iv)
50 glue_xts_crypt_128bit_one(ctx, dst, src, iv, twofish_enc_blk);
53 static void twofish_xts_dec(const void *ctx, u8 *dst, const u8 *src, le128 *iv)
55 glue_xts_crypt_128bit_one(ctx, dst, src, iv, twofish_dec_blk);
58 struct twofish_xts_ctx {
59 struct twofish_ctx tweak_ctx;
60 struct twofish_ctx crypt_ctx;
63 static int xts_twofish_setkey(struct crypto_skcipher *tfm, const u8 *key,
66 struct twofish_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
67 u32 *flags = &tfm->base.crt_flags;
70 err = xts_verify_key(tfm, key, keylen);
74 /* first half of xts-key is for crypt */
75 err = __twofish_setkey(&ctx->crypt_ctx, key, keylen / 2, flags);
79 /* second half of xts-key is for tweak */
80 return __twofish_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2,
84 static const struct common_glue_ctx twofish_enc = {
86 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
89 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
90 .fn_u = { .ecb = twofish_ecb_enc_8way }
93 .fn_u = { .ecb = twofish_enc_blk_3way }
96 .fn_u = { .ecb = twofish_enc_blk }
100 static const struct common_glue_ctx twofish_ctr = {
102 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
105 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
106 .fn_u = { .ctr = twofish_ctr_8way }
109 .fn_u = { .ctr = twofish_enc_blk_ctr_3way }
112 .fn_u = { .ctr = twofish_enc_blk_ctr }
116 static const struct common_glue_ctx twofish_enc_xts = {
118 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
121 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
122 .fn_u = { .xts = twofish_xts_enc_8way }
125 .fn_u = { .xts = twofish_xts_enc }
129 static const struct common_glue_ctx twofish_dec = {
131 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
134 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
135 .fn_u = { .ecb = twofish_ecb_dec_8way }
138 .fn_u = { .ecb = twofish_dec_blk_3way }
141 .fn_u = { .ecb = twofish_dec_blk }
145 static const struct common_glue_ctx twofish_dec_cbc = {
147 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
150 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
151 .fn_u = { .cbc = twofish_cbc_dec_8way }
154 .fn_u = { .cbc = twofish_dec_blk_cbc_3way }
157 .fn_u = { .cbc = twofish_dec_blk }
161 static const struct common_glue_ctx twofish_dec_xts = {
163 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
166 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
167 .fn_u = { .xts = twofish_xts_dec_8way }
170 .fn_u = { .xts = twofish_xts_dec }
174 static int ecb_encrypt(struct skcipher_request *req)
176 return glue_ecb_req_128bit(&twofish_enc, req);
179 static int ecb_decrypt(struct skcipher_request *req)
181 return glue_ecb_req_128bit(&twofish_dec, req);
184 static int cbc_encrypt(struct skcipher_request *req)
186 return glue_cbc_encrypt_req_128bit(twofish_enc_blk, req);
189 static int cbc_decrypt(struct skcipher_request *req)
191 return glue_cbc_decrypt_req_128bit(&twofish_dec_cbc, req);
194 static int ctr_crypt(struct skcipher_request *req)
196 return glue_ctr_req_128bit(&twofish_ctr, req);
199 static int xts_encrypt(struct skcipher_request *req)
201 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
202 struct twofish_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
204 return glue_xts_req_128bit(&twofish_enc_xts, req, twofish_enc_blk,
205 &ctx->tweak_ctx, &ctx->crypt_ctx, false);
208 static int xts_decrypt(struct skcipher_request *req)
210 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
211 struct twofish_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
213 return glue_xts_req_128bit(&twofish_dec_xts, req, twofish_enc_blk,
214 &ctx->tweak_ctx, &ctx->crypt_ctx, true);
217 static struct skcipher_alg twofish_algs[] = {
219 .base.cra_name = "__ecb(twofish)",
220 .base.cra_driver_name = "__ecb-twofish-avx",
221 .base.cra_priority = 400,
222 .base.cra_flags = CRYPTO_ALG_INTERNAL,
223 .base.cra_blocksize = TF_BLOCK_SIZE,
224 .base.cra_ctxsize = sizeof(struct twofish_ctx),
225 .base.cra_module = THIS_MODULE,
226 .min_keysize = TF_MIN_KEY_SIZE,
227 .max_keysize = TF_MAX_KEY_SIZE,
228 .setkey = twofish_setkey_skcipher,
229 .encrypt = ecb_encrypt,
230 .decrypt = ecb_decrypt,
232 .base.cra_name = "__cbc(twofish)",
233 .base.cra_driver_name = "__cbc-twofish-avx",
234 .base.cra_priority = 400,
235 .base.cra_flags = CRYPTO_ALG_INTERNAL,
236 .base.cra_blocksize = TF_BLOCK_SIZE,
237 .base.cra_ctxsize = sizeof(struct twofish_ctx),
238 .base.cra_module = THIS_MODULE,
239 .min_keysize = TF_MIN_KEY_SIZE,
240 .max_keysize = TF_MAX_KEY_SIZE,
241 .ivsize = TF_BLOCK_SIZE,
242 .setkey = twofish_setkey_skcipher,
243 .encrypt = cbc_encrypt,
244 .decrypt = cbc_decrypt,
246 .base.cra_name = "__ctr(twofish)",
247 .base.cra_driver_name = "__ctr-twofish-avx",
248 .base.cra_priority = 400,
249 .base.cra_flags = CRYPTO_ALG_INTERNAL,
250 .base.cra_blocksize = 1,
251 .base.cra_ctxsize = sizeof(struct twofish_ctx),
252 .base.cra_module = THIS_MODULE,
253 .min_keysize = TF_MIN_KEY_SIZE,
254 .max_keysize = TF_MAX_KEY_SIZE,
255 .ivsize = TF_BLOCK_SIZE,
256 .chunksize = TF_BLOCK_SIZE,
257 .setkey = twofish_setkey_skcipher,
258 .encrypt = ctr_crypt,
259 .decrypt = ctr_crypt,
261 .base.cra_name = "__xts(twofish)",
262 .base.cra_driver_name = "__xts-twofish-avx",
263 .base.cra_priority = 400,
264 .base.cra_flags = CRYPTO_ALG_INTERNAL,
265 .base.cra_blocksize = TF_BLOCK_SIZE,
266 .base.cra_ctxsize = sizeof(struct twofish_xts_ctx),
267 .base.cra_module = THIS_MODULE,
268 .min_keysize = 2 * TF_MIN_KEY_SIZE,
269 .max_keysize = 2 * TF_MAX_KEY_SIZE,
270 .ivsize = TF_BLOCK_SIZE,
271 .setkey = xts_twofish_setkey,
272 .encrypt = xts_encrypt,
273 .decrypt = xts_decrypt,
277 static struct simd_skcipher_alg *twofish_simd_algs[ARRAY_SIZE(twofish_algs)];
279 static int __init twofish_init(void)
281 const char *feature_name;
283 if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, &feature_name)) {
284 pr_info("CPU feature '%s' is not supported.\n", feature_name);
288 return simd_register_skciphers_compat(twofish_algs,
289 ARRAY_SIZE(twofish_algs),
293 static void __exit twofish_exit(void)
295 simd_unregister_skciphers(twofish_algs, ARRAY_SIZE(twofish_algs),
299 module_init(twofish_init);
300 module_exit(twofish_exit);
302 MODULE_DESCRIPTION("Twofish Cipher Algorithm, AVX optimized");
303 MODULE_LICENSE("GPL");
304 MODULE_ALIAS_CRYPTO("twofish");