18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Glue Code for AVX assembler version of Twofish Cipher
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Johannes Goetzfried
68c2ecf20Sopenharmony_ci *     <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/types.h>
138c2ecf20Sopenharmony_ci#include <linux/crypto.h>
148c2ecf20Sopenharmony_ci#include <linux/err.h>
158c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
168c2ecf20Sopenharmony_ci#include <crypto/internal/simd.h>
178c2ecf20Sopenharmony_ci#include <crypto/twofish.h>
188c2ecf20Sopenharmony_ci#include <crypto/xts.h>
198c2ecf20Sopenharmony_ci#include <asm/crypto/glue_helper.h>
208c2ecf20Sopenharmony_ci#include <asm/crypto/twofish.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define TWOFISH_PARALLEL_BLOCKS 8
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* 8-way parallel cipher functions */
258c2ecf20Sopenharmony_ciasmlinkage void twofish_ecb_enc_8way(const void *ctx, u8 *dst, const u8 *src);
268c2ecf20Sopenharmony_ciasmlinkage void twofish_ecb_dec_8way(const void *ctx, u8 *dst, const u8 *src);
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciasmlinkage void twofish_cbc_dec_8way(const void *ctx, u8 *dst, const u8 *src);
298c2ecf20Sopenharmony_ciasmlinkage void twofish_ctr_8way(const void *ctx, u8 *dst, const u8 *src,
308c2ecf20Sopenharmony_ci				 le128 *iv);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ciasmlinkage void twofish_xts_enc_8way(const void *ctx, u8 *dst, const u8 *src,
338c2ecf20Sopenharmony_ci				     le128 *iv);
348c2ecf20Sopenharmony_ciasmlinkage void twofish_xts_dec_8way(const void *ctx, u8 *dst, const u8 *src,
358c2ecf20Sopenharmony_ci				     le128 *iv);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic int twofish_setkey_skcipher(struct crypto_skcipher *tfm,
388c2ecf20Sopenharmony_ci				   const u8 *key, unsigned int keylen)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	return twofish_setkey(&tfm->base, key, keylen);
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic inline void twofish_enc_blk_3way(const void *ctx, u8 *dst, const u8 *src)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	__twofish_enc_blk_3way(ctx, dst, src, false);
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic void twofish_xts_enc(const void *ctx, u8 *dst, const u8 *src, le128 *iv)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	glue_xts_crypt_128bit_one(ctx, dst, src, iv, twofish_enc_blk);
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic void twofish_xts_dec(const void *ctx, u8 *dst, const u8 *src, le128 *iv)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	glue_xts_crypt_128bit_one(ctx, dst, src, iv, twofish_dec_blk);
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistruct twofish_xts_ctx {
598c2ecf20Sopenharmony_ci	struct twofish_ctx tweak_ctx;
608c2ecf20Sopenharmony_ci	struct twofish_ctx crypt_ctx;
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic int xts_twofish_setkey(struct crypto_skcipher *tfm, const u8 *key,
648c2ecf20Sopenharmony_ci			      unsigned int keylen)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	struct twofish_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
678c2ecf20Sopenharmony_ci	int err;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	err = xts_verify_key(tfm, key, keylen);
708c2ecf20Sopenharmony_ci	if (err)
718c2ecf20Sopenharmony_ci		return err;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	/* first half of xts-key is for crypt */
748c2ecf20Sopenharmony_ci	err = __twofish_setkey(&ctx->crypt_ctx, key, keylen / 2);
758c2ecf20Sopenharmony_ci	if (err)
768c2ecf20Sopenharmony_ci		return err;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/* second half of xts-key is for tweak */
798c2ecf20Sopenharmony_ci	return __twofish_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2);
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic const struct common_glue_ctx twofish_enc = {
838c2ecf20Sopenharmony_ci	.num_funcs = 3,
848c2ecf20Sopenharmony_ci	.fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	.funcs = { {
878c2ecf20Sopenharmony_ci		.num_blocks = TWOFISH_PARALLEL_BLOCKS,
888c2ecf20Sopenharmony_ci		.fn_u = { .ecb = twofish_ecb_enc_8way }
898c2ecf20Sopenharmony_ci	}, {
908c2ecf20Sopenharmony_ci		.num_blocks = 3,
918c2ecf20Sopenharmony_ci		.fn_u = { .ecb = twofish_enc_blk_3way }
928c2ecf20Sopenharmony_ci	}, {
938c2ecf20Sopenharmony_ci		.num_blocks = 1,
948c2ecf20Sopenharmony_ci		.fn_u = { .ecb = twofish_enc_blk }
958c2ecf20Sopenharmony_ci	} }
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic const struct common_glue_ctx twofish_ctr = {
998c2ecf20Sopenharmony_ci	.num_funcs = 3,
1008c2ecf20Sopenharmony_ci	.fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	.funcs = { {
1038c2ecf20Sopenharmony_ci		.num_blocks = TWOFISH_PARALLEL_BLOCKS,
1048c2ecf20Sopenharmony_ci		.fn_u = { .ctr = twofish_ctr_8way }
1058c2ecf20Sopenharmony_ci	}, {
1068c2ecf20Sopenharmony_ci		.num_blocks = 3,
1078c2ecf20Sopenharmony_ci		.fn_u = { .ctr = twofish_enc_blk_ctr_3way }
1088c2ecf20Sopenharmony_ci	}, {
1098c2ecf20Sopenharmony_ci		.num_blocks = 1,
1108c2ecf20Sopenharmony_ci		.fn_u = { .ctr = twofish_enc_blk_ctr }
1118c2ecf20Sopenharmony_ci	} }
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic const struct common_glue_ctx twofish_enc_xts = {
1158c2ecf20Sopenharmony_ci	.num_funcs = 2,
1168c2ecf20Sopenharmony_ci	.fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	.funcs = { {
1198c2ecf20Sopenharmony_ci		.num_blocks = TWOFISH_PARALLEL_BLOCKS,
1208c2ecf20Sopenharmony_ci		.fn_u = { .xts = twofish_xts_enc_8way }
1218c2ecf20Sopenharmony_ci	}, {
1228c2ecf20Sopenharmony_ci		.num_blocks = 1,
1238c2ecf20Sopenharmony_ci		.fn_u = { .xts = twofish_xts_enc }
1248c2ecf20Sopenharmony_ci	} }
1258c2ecf20Sopenharmony_ci};
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic const struct common_glue_ctx twofish_dec = {
1288c2ecf20Sopenharmony_ci	.num_funcs = 3,
1298c2ecf20Sopenharmony_ci	.fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	.funcs = { {
1328c2ecf20Sopenharmony_ci		.num_blocks = TWOFISH_PARALLEL_BLOCKS,
1338c2ecf20Sopenharmony_ci		.fn_u = { .ecb = twofish_ecb_dec_8way }
1348c2ecf20Sopenharmony_ci	}, {
1358c2ecf20Sopenharmony_ci		.num_blocks = 3,
1368c2ecf20Sopenharmony_ci		.fn_u = { .ecb = twofish_dec_blk_3way }
1378c2ecf20Sopenharmony_ci	}, {
1388c2ecf20Sopenharmony_ci		.num_blocks = 1,
1398c2ecf20Sopenharmony_ci		.fn_u = { .ecb = twofish_dec_blk }
1408c2ecf20Sopenharmony_ci	} }
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic const struct common_glue_ctx twofish_dec_cbc = {
1448c2ecf20Sopenharmony_ci	.num_funcs = 3,
1458c2ecf20Sopenharmony_ci	.fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	.funcs = { {
1488c2ecf20Sopenharmony_ci		.num_blocks = TWOFISH_PARALLEL_BLOCKS,
1498c2ecf20Sopenharmony_ci		.fn_u = { .cbc = twofish_cbc_dec_8way }
1508c2ecf20Sopenharmony_ci	}, {
1518c2ecf20Sopenharmony_ci		.num_blocks = 3,
1528c2ecf20Sopenharmony_ci		.fn_u = { .cbc = twofish_dec_blk_cbc_3way }
1538c2ecf20Sopenharmony_ci	}, {
1548c2ecf20Sopenharmony_ci		.num_blocks = 1,
1558c2ecf20Sopenharmony_ci		.fn_u = { .cbc = twofish_dec_blk }
1568c2ecf20Sopenharmony_ci	} }
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic const struct common_glue_ctx twofish_dec_xts = {
1608c2ecf20Sopenharmony_ci	.num_funcs = 2,
1618c2ecf20Sopenharmony_ci	.fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	.funcs = { {
1648c2ecf20Sopenharmony_ci		.num_blocks = TWOFISH_PARALLEL_BLOCKS,
1658c2ecf20Sopenharmony_ci		.fn_u = { .xts = twofish_xts_dec_8way }
1668c2ecf20Sopenharmony_ci	}, {
1678c2ecf20Sopenharmony_ci		.num_blocks = 1,
1688c2ecf20Sopenharmony_ci		.fn_u = { .xts = twofish_xts_dec }
1698c2ecf20Sopenharmony_ci	} }
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic int ecb_encrypt(struct skcipher_request *req)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	return glue_ecb_req_128bit(&twofish_enc, req);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic int ecb_decrypt(struct skcipher_request *req)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	return glue_ecb_req_128bit(&twofish_dec, req);
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic int cbc_encrypt(struct skcipher_request *req)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	return glue_cbc_encrypt_req_128bit(twofish_enc_blk, req);
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic int cbc_decrypt(struct skcipher_request *req)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	return glue_cbc_decrypt_req_128bit(&twofish_dec_cbc, req);
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic int ctr_crypt(struct skcipher_request *req)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	return glue_ctr_req_128bit(&twofish_ctr, req);
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cistatic int xts_encrypt(struct skcipher_request *req)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2008c2ecf20Sopenharmony_ci	struct twofish_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	return glue_xts_req_128bit(&twofish_enc_xts, req, twofish_enc_blk,
2038c2ecf20Sopenharmony_ci				   &ctx->tweak_ctx, &ctx->crypt_ctx, false);
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic int xts_decrypt(struct skcipher_request *req)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2098c2ecf20Sopenharmony_ci	struct twofish_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	return glue_xts_req_128bit(&twofish_dec_xts, req, twofish_enc_blk,
2128c2ecf20Sopenharmony_ci				   &ctx->tweak_ctx, &ctx->crypt_ctx, true);
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic struct skcipher_alg twofish_algs[] = {
2168c2ecf20Sopenharmony_ci	{
2178c2ecf20Sopenharmony_ci		.base.cra_name		= "__ecb(twofish)",
2188c2ecf20Sopenharmony_ci		.base.cra_driver_name	= "__ecb-twofish-avx",
2198c2ecf20Sopenharmony_ci		.base.cra_priority	= 400,
2208c2ecf20Sopenharmony_ci		.base.cra_flags		= CRYPTO_ALG_INTERNAL,
2218c2ecf20Sopenharmony_ci		.base.cra_blocksize	= TF_BLOCK_SIZE,
2228c2ecf20Sopenharmony_ci		.base.cra_ctxsize	= sizeof(struct twofish_ctx),
2238c2ecf20Sopenharmony_ci		.base.cra_module	= THIS_MODULE,
2248c2ecf20Sopenharmony_ci		.min_keysize		= TF_MIN_KEY_SIZE,
2258c2ecf20Sopenharmony_ci		.max_keysize		= TF_MAX_KEY_SIZE,
2268c2ecf20Sopenharmony_ci		.setkey			= twofish_setkey_skcipher,
2278c2ecf20Sopenharmony_ci		.encrypt		= ecb_encrypt,
2288c2ecf20Sopenharmony_ci		.decrypt		= ecb_decrypt,
2298c2ecf20Sopenharmony_ci	}, {
2308c2ecf20Sopenharmony_ci		.base.cra_name		= "__cbc(twofish)",
2318c2ecf20Sopenharmony_ci		.base.cra_driver_name	= "__cbc-twofish-avx",
2328c2ecf20Sopenharmony_ci		.base.cra_priority	= 400,
2338c2ecf20Sopenharmony_ci		.base.cra_flags		= CRYPTO_ALG_INTERNAL,
2348c2ecf20Sopenharmony_ci		.base.cra_blocksize	= TF_BLOCK_SIZE,
2358c2ecf20Sopenharmony_ci		.base.cra_ctxsize	= sizeof(struct twofish_ctx),
2368c2ecf20Sopenharmony_ci		.base.cra_module	= THIS_MODULE,
2378c2ecf20Sopenharmony_ci		.min_keysize		= TF_MIN_KEY_SIZE,
2388c2ecf20Sopenharmony_ci		.max_keysize		= TF_MAX_KEY_SIZE,
2398c2ecf20Sopenharmony_ci		.ivsize			= TF_BLOCK_SIZE,
2408c2ecf20Sopenharmony_ci		.setkey			= twofish_setkey_skcipher,
2418c2ecf20Sopenharmony_ci		.encrypt		= cbc_encrypt,
2428c2ecf20Sopenharmony_ci		.decrypt		= cbc_decrypt,
2438c2ecf20Sopenharmony_ci	}, {
2448c2ecf20Sopenharmony_ci		.base.cra_name		= "__ctr(twofish)",
2458c2ecf20Sopenharmony_ci		.base.cra_driver_name	= "__ctr-twofish-avx",
2468c2ecf20Sopenharmony_ci		.base.cra_priority	= 400,
2478c2ecf20Sopenharmony_ci		.base.cra_flags		= CRYPTO_ALG_INTERNAL,
2488c2ecf20Sopenharmony_ci		.base.cra_blocksize	= 1,
2498c2ecf20Sopenharmony_ci		.base.cra_ctxsize	= sizeof(struct twofish_ctx),
2508c2ecf20Sopenharmony_ci		.base.cra_module	= THIS_MODULE,
2518c2ecf20Sopenharmony_ci		.min_keysize		= TF_MIN_KEY_SIZE,
2528c2ecf20Sopenharmony_ci		.max_keysize		= TF_MAX_KEY_SIZE,
2538c2ecf20Sopenharmony_ci		.ivsize			= TF_BLOCK_SIZE,
2548c2ecf20Sopenharmony_ci		.chunksize		= TF_BLOCK_SIZE,
2558c2ecf20Sopenharmony_ci		.setkey			= twofish_setkey_skcipher,
2568c2ecf20Sopenharmony_ci		.encrypt		= ctr_crypt,
2578c2ecf20Sopenharmony_ci		.decrypt		= ctr_crypt,
2588c2ecf20Sopenharmony_ci	}, {
2598c2ecf20Sopenharmony_ci		.base.cra_name		= "__xts(twofish)",
2608c2ecf20Sopenharmony_ci		.base.cra_driver_name	= "__xts-twofish-avx",
2618c2ecf20Sopenharmony_ci		.base.cra_priority	= 400,
2628c2ecf20Sopenharmony_ci		.base.cra_flags		= CRYPTO_ALG_INTERNAL,
2638c2ecf20Sopenharmony_ci		.base.cra_blocksize	= TF_BLOCK_SIZE,
2648c2ecf20Sopenharmony_ci		.base.cra_ctxsize	= sizeof(struct twofish_xts_ctx),
2658c2ecf20Sopenharmony_ci		.base.cra_module	= THIS_MODULE,
2668c2ecf20Sopenharmony_ci		.min_keysize		= 2 * TF_MIN_KEY_SIZE,
2678c2ecf20Sopenharmony_ci		.max_keysize		= 2 * TF_MAX_KEY_SIZE,
2688c2ecf20Sopenharmony_ci		.ivsize			= TF_BLOCK_SIZE,
2698c2ecf20Sopenharmony_ci		.setkey			= xts_twofish_setkey,
2708c2ecf20Sopenharmony_ci		.encrypt		= xts_encrypt,
2718c2ecf20Sopenharmony_ci		.decrypt		= xts_decrypt,
2728c2ecf20Sopenharmony_ci	},
2738c2ecf20Sopenharmony_ci};
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic struct simd_skcipher_alg *twofish_simd_algs[ARRAY_SIZE(twofish_algs)];
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic int __init twofish_init(void)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	const char *feature_name;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, &feature_name)) {
2828c2ecf20Sopenharmony_ci		pr_info("CPU feature '%s' is not supported.\n", feature_name);
2838c2ecf20Sopenharmony_ci		return -ENODEV;
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	return simd_register_skciphers_compat(twofish_algs,
2878c2ecf20Sopenharmony_ci					      ARRAY_SIZE(twofish_algs),
2888c2ecf20Sopenharmony_ci					      twofish_simd_algs);
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic void __exit twofish_exit(void)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	simd_unregister_skciphers(twofish_algs, ARRAY_SIZE(twofish_algs),
2948c2ecf20Sopenharmony_ci				  twofish_simd_algs);
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_cimodule_init(twofish_init);
2988c2ecf20Sopenharmony_cimodule_exit(twofish_exit);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Twofish Cipher Algorithm, AVX optimized");
3018c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
3028c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("twofish");
303