162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Glue Code for x86_64/AVX2 assembler optimized version of Serpent
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright © 2012-2013 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/module.h>
962306a36Sopenharmony_ci#include <linux/types.h>
1062306a36Sopenharmony_ci#include <linux/crypto.h>
1162306a36Sopenharmony_ci#include <linux/err.h>
1262306a36Sopenharmony_ci#include <crypto/algapi.h>
1362306a36Sopenharmony_ci#include <crypto/internal/simd.h>
1462306a36Sopenharmony_ci#include <crypto/serpent.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include "serpent-avx.h"
1762306a36Sopenharmony_ci#include "ecb_cbc_helpers.h"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#define SERPENT_AVX2_PARALLEL_BLOCKS 16
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/* 16-way AVX2 parallel cipher functions */
2262306a36Sopenharmony_ciasmlinkage void serpent_ecb_enc_16way(const void *ctx, u8 *dst, const u8 *src);
2362306a36Sopenharmony_ciasmlinkage void serpent_ecb_dec_16way(const void *ctx, u8 *dst, const u8 *src);
2462306a36Sopenharmony_ciasmlinkage void serpent_cbc_dec_16way(const void *ctx, u8 *dst, const u8 *src);
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistatic int serpent_setkey_skcipher(struct crypto_skcipher *tfm,
2762306a36Sopenharmony_ci				   const u8 *key, unsigned int keylen)
2862306a36Sopenharmony_ci{
2962306a36Sopenharmony_ci	return __serpent_setkey(crypto_skcipher_ctx(tfm), key, keylen);
3062306a36Sopenharmony_ci}
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistatic int ecb_encrypt(struct skcipher_request *req)
3362306a36Sopenharmony_ci{
3462306a36Sopenharmony_ci	ECB_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS);
3562306a36Sopenharmony_ci	ECB_BLOCK(SERPENT_AVX2_PARALLEL_BLOCKS, serpent_ecb_enc_16way);
3662306a36Sopenharmony_ci	ECB_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_ecb_enc_8way_avx);
3762306a36Sopenharmony_ci	ECB_BLOCK(1, __serpent_encrypt);
3862306a36Sopenharmony_ci	ECB_WALK_END();
3962306a36Sopenharmony_ci}
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistatic int ecb_decrypt(struct skcipher_request *req)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	ECB_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS);
4462306a36Sopenharmony_ci	ECB_BLOCK(SERPENT_AVX2_PARALLEL_BLOCKS, serpent_ecb_dec_16way);
4562306a36Sopenharmony_ci	ECB_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_ecb_dec_8way_avx);
4662306a36Sopenharmony_ci	ECB_BLOCK(1, __serpent_decrypt);
4762306a36Sopenharmony_ci	ECB_WALK_END();
4862306a36Sopenharmony_ci}
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistatic int cbc_encrypt(struct skcipher_request *req)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	CBC_WALK_START(req, SERPENT_BLOCK_SIZE, -1);
5362306a36Sopenharmony_ci	CBC_ENC_BLOCK(__serpent_encrypt);
5462306a36Sopenharmony_ci	CBC_WALK_END();
5562306a36Sopenharmony_ci}
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistatic int cbc_decrypt(struct skcipher_request *req)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	CBC_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS);
6062306a36Sopenharmony_ci	CBC_DEC_BLOCK(SERPENT_AVX2_PARALLEL_BLOCKS, serpent_cbc_dec_16way);
6162306a36Sopenharmony_ci	CBC_DEC_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_cbc_dec_8way_avx);
6262306a36Sopenharmony_ci	CBC_DEC_BLOCK(1, __serpent_decrypt);
6362306a36Sopenharmony_ci	CBC_WALK_END();
6462306a36Sopenharmony_ci}
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_cistatic struct skcipher_alg serpent_algs[] = {
6762306a36Sopenharmony_ci	{
6862306a36Sopenharmony_ci		.base.cra_name		= "__ecb(serpent)",
6962306a36Sopenharmony_ci		.base.cra_driver_name	= "__ecb-serpent-avx2",
7062306a36Sopenharmony_ci		.base.cra_priority	= 600,
7162306a36Sopenharmony_ci		.base.cra_flags		= CRYPTO_ALG_INTERNAL,
7262306a36Sopenharmony_ci		.base.cra_blocksize	= SERPENT_BLOCK_SIZE,
7362306a36Sopenharmony_ci		.base.cra_ctxsize	= sizeof(struct serpent_ctx),
7462306a36Sopenharmony_ci		.base.cra_module	= THIS_MODULE,
7562306a36Sopenharmony_ci		.min_keysize		= SERPENT_MIN_KEY_SIZE,
7662306a36Sopenharmony_ci		.max_keysize		= SERPENT_MAX_KEY_SIZE,
7762306a36Sopenharmony_ci		.setkey			= serpent_setkey_skcipher,
7862306a36Sopenharmony_ci		.encrypt		= ecb_encrypt,
7962306a36Sopenharmony_ci		.decrypt		= ecb_decrypt,
8062306a36Sopenharmony_ci	}, {
8162306a36Sopenharmony_ci		.base.cra_name		= "__cbc(serpent)",
8262306a36Sopenharmony_ci		.base.cra_driver_name	= "__cbc-serpent-avx2",
8362306a36Sopenharmony_ci		.base.cra_priority	= 600,
8462306a36Sopenharmony_ci		.base.cra_flags		= CRYPTO_ALG_INTERNAL,
8562306a36Sopenharmony_ci		.base.cra_blocksize	= SERPENT_BLOCK_SIZE,
8662306a36Sopenharmony_ci		.base.cra_ctxsize	= sizeof(struct serpent_ctx),
8762306a36Sopenharmony_ci		.base.cra_module	= THIS_MODULE,
8862306a36Sopenharmony_ci		.min_keysize		= SERPENT_MIN_KEY_SIZE,
8962306a36Sopenharmony_ci		.max_keysize		= SERPENT_MAX_KEY_SIZE,
9062306a36Sopenharmony_ci		.ivsize			= SERPENT_BLOCK_SIZE,
9162306a36Sopenharmony_ci		.setkey			= serpent_setkey_skcipher,
9262306a36Sopenharmony_ci		.encrypt		= cbc_encrypt,
9362306a36Sopenharmony_ci		.decrypt		= cbc_decrypt,
9462306a36Sopenharmony_ci	},
9562306a36Sopenharmony_ci};
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_cistatic struct simd_skcipher_alg *serpent_simd_algs[ARRAY_SIZE(serpent_algs)];
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_cistatic int __init serpent_avx2_init(void)
10062306a36Sopenharmony_ci{
10162306a36Sopenharmony_ci	const char *feature_name;
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	if (!boot_cpu_has(X86_FEATURE_AVX2) || !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
10462306a36Sopenharmony_ci		pr_info("AVX2 instructions are not detected.\n");
10562306a36Sopenharmony_ci		return -ENODEV;
10662306a36Sopenharmony_ci	}
10762306a36Sopenharmony_ci	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
10862306a36Sopenharmony_ci				&feature_name)) {
10962306a36Sopenharmony_ci		pr_info("CPU feature '%s' is not supported.\n", feature_name);
11062306a36Sopenharmony_ci		return -ENODEV;
11162306a36Sopenharmony_ci	}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	return simd_register_skciphers_compat(serpent_algs,
11462306a36Sopenharmony_ci					      ARRAY_SIZE(serpent_algs),
11562306a36Sopenharmony_ci					      serpent_simd_algs);
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic void __exit serpent_avx2_fini(void)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	simd_unregister_skciphers(serpent_algs, ARRAY_SIZE(serpent_algs),
12162306a36Sopenharmony_ci				  serpent_simd_algs);
12262306a36Sopenharmony_ci}
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_cimodule_init(serpent_avx2_init);
12562306a36Sopenharmony_cimodule_exit(serpent_avx2_fini);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ciMODULE_LICENSE("GPL");
12862306a36Sopenharmony_ciMODULE_DESCRIPTION("Serpent Cipher Algorithm, AVX2 optimized");
12962306a36Sopenharmony_ciMODULE_ALIAS_CRYPTO("serpent");
13062306a36Sopenharmony_ciMODULE_ALIAS_CRYPTO("serpent-asm");
131