162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Glue Code for SSE2 assembler versions of Serpent Cipher
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Glue code based on aesni-intel_glue.c by:
862306a36Sopenharmony_ci *  Copyright (C) 2008, Intel Corp.
962306a36Sopenharmony_ci *    Author: Huang Ying <ying.huang@intel.com>
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
1262306a36Sopenharmony_ci *   Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
1362306a36Sopenharmony_ci */
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <linux/module.h>
1662306a36Sopenharmony_ci#include <linux/types.h>
1762306a36Sopenharmony_ci#include <linux/crypto.h>
1862306a36Sopenharmony_ci#include <linux/err.h>
1962306a36Sopenharmony_ci#include <crypto/algapi.h>
2062306a36Sopenharmony_ci#include <crypto/b128ops.h>
2162306a36Sopenharmony_ci#include <crypto/internal/simd.h>
2262306a36Sopenharmony_ci#include <crypto/serpent.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#include "serpent-sse2.h"
2562306a36Sopenharmony_ci#include "ecb_cbc_helpers.h"
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic int serpent_setkey_skcipher(struct crypto_skcipher *tfm,
2862306a36Sopenharmony_ci				   const u8 *key, unsigned int keylen)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	return __serpent_setkey(crypto_skcipher_ctx(tfm), key, keylen);
3162306a36Sopenharmony_ci}
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistatic void serpent_decrypt_cbc_xway(const void *ctx, u8 *dst, const u8 *src)
3462306a36Sopenharmony_ci{
3562306a36Sopenharmony_ci	u8 buf[SERPENT_PARALLEL_BLOCKS - 1][SERPENT_BLOCK_SIZE];
3662306a36Sopenharmony_ci	const u8 *s = src;
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	if (dst == src)
3962306a36Sopenharmony_ci		s = memcpy(buf, src, sizeof(buf));
4062306a36Sopenharmony_ci	serpent_dec_blk_xway(ctx, dst, src);
4162306a36Sopenharmony_ci	crypto_xor(dst + SERPENT_BLOCK_SIZE, s, sizeof(buf));
4262306a36Sopenharmony_ci}
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_cistatic int ecb_encrypt(struct skcipher_request *req)
4562306a36Sopenharmony_ci{
4662306a36Sopenharmony_ci	ECB_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS);
4762306a36Sopenharmony_ci	ECB_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_enc_blk_xway);
4862306a36Sopenharmony_ci	ECB_BLOCK(1, __serpent_encrypt);
4962306a36Sopenharmony_ci	ECB_WALK_END();
5062306a36Sopenharmony_ci}
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_cistatic int ecb_decrypt(struct skcipher_request *req)
5362306a36Sopenharmony_ci{
5462306a36Sopenharmony_ci	ECB_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS);
5562306a36Sopenharmony_ci	ECB_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_dec_blk_xway);
5662306a36Sopenharmony_ci	ECB_BLOCK(1, __serpent_decrypt);
5762306a36Sopenharmony_ci	ECB_WALK_END();
5862306a36Sopenharmony_ci}
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic int cbc_encrypt(struct skcipher_request *req)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	CBC_WALK_START(req, SERPENT_BLOCK_SIZE, -1);
6362306a36Sopenharmony_ci	CBC_ENC_BLOCK(__serpent_encrypt);
6462306a36Sopenharmony_ci	CBC_WALK_END();
6562306a36Sopenharmony_ci}
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_cistatic int cbc_decrypt(struct skcipher_request *req)
6862306a36Sopenharmony_ci{
6962306a36Sopenharmony_ci	CBC_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS);
7062306a36Sopenharmony_ci	CBC_DEC_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_decrypt_cbc_xway);
7162306a36Sopenharmony_ci	CBC_DEC_BLOCK(1, __serpent_decrypt);
7262306a36Sopenharmony_ci	CBC_WALK_END();
7362306a36Sopenharmony_ci}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cistatic struct skcipher_alg serpent_algs[] = {
7662306a36Sopenharmony_ci	{
7762306a36Sopenharmony_ci		.base.cra_name		= "__ecb(serpent)",
7862306a36Sopenharmony_ci		.base.cra_driver_name	= "__ecb-serpent-sse2",
7962306a36Sopenharmony_ci		.base.cra_priority	= 400,
8062306a36Sopenharmony_ci		.base.cra_flags		= CRYPTO_ALG_INTERNAL,
8162306a36Sopenharmony_ci		.base.cra_blocksize	= SERPENT_BLOCK_SIZE,
8262306a36Sopenharmony_ci		.base.cra_ctxsize	= sizeof(struct serpent_ctx),
8362306a36Sopenharmony_ci		.base.cra_module	= THIS_MODULE,
8462306a36Sopenharmony_ci		.min_keysize		= SERPENT_MIN_KEY_SIZE,
8562306a36Sopenharmony_ci		.max_keysize		= SERPENT_MAX_KEY_SIZE,
8662306a36Sopenharmony_ci		.setkey			= serpent_setkey_skcipher,
8762306a36Sopenharmony_ci		.encrypt		= ecb_encrypt,
8862306a36Sopenharmony_ci		.decrypt		= ecb_decrypt,
8962306a36Sopenharmony_ci	}, {
9062306a36Sopenharmony_ci		.base.cra_name		= "__cbc(serpent)",
9162306a36Sopenharmony_ci		.base.cra_driver_name	= "__cbc-serpent-sse2",
9262306a36Sopenharmony_ci		.base.cra_priority	= 400,
9362306a36Sopenharmony_ci		.base.cra_flags		= CRYPTO_ALG_INTERNAL,
9462306a36Sopenharmony_ci		.base.cra_blocksize	= SERPENT_BLOCK_SIZE,
9562306a36Sopenharmony_ci		.base.cra_ctxsize	= sizeof(struct serpent_ctx),
9662306a36Sopenharmony_ci		.base.cra_module	= THIS_MODULE,
9762306a36Sopenharmony_ci		.min_keysize		= SERPENT_MIN_KEY_SIZE,
9862306a36Sopenharmony_ci		.max_keysize		= SERPENT_MAX_KEY_SIZE,
9962306a36Sopenharmony_ci		.ivsize			= SERPENT_BLOCK_SIZE,
10062306a36Sopenharmony_ci		.setkey			= serpent_setkey_skcipher,
10162306a36Sopenharmony_ci		.encrypt		= cbc_encrypt,
10262306a36Sopenharmony_ci		.decrypt		= cbc_decrypt,
10362306a36Sopenharmony_ci	},
10462306a36Sopenharmony_ci};
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistatic struct simd_skcipher_alg *serpent_simd_algs[ARRAY_SIZE(serpent_algs)];
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_cistatic int __init serpent_sse2_init(void)
10962306a36Sopenharmony_ci{
11062306a36Sopenharmony_ci	if (!boot_cpu_has(X86_FEATURE_XMM2)) {
11162306a36Sopenharmony_ci		printk(KERN_INFO "SSE2 instructions are not detected.\n");
11262306a36Sopenharmony_ci		return -ENODEV;
11362306a36Sopenharmony_ci	}
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	return simd_register_skciphers_compat(serpent_algs,
11662306a36Sopenharmony_ci					      ARRAY_SIZE(serpent_algs),
11762306a36Sopenharmony_ci					      serpent_simd_algs);
11862306a36Sopenharmony_ci}
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_cistatic void __exit serpent_sse2_exit(void)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci	simd_unregister_skciphers(serpent_algs, ARRAY_SIZE(serpent_algs),
12362306a36Sopenharmony_ci				  serpent_simd_algs);
12462306a36Sopenharmony_ci}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_cimodule_init(serpent_sse2_init);
12762306a36Sopenharmony_cimodule_exit(serpent_sse2_exit);
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ciMODULE_DESCRIPTION("Serpent Cipher Algorithm, SSE2 optimized");
13062306a36Sopenharmony_ciMODULE_LICENSE("GPL");
13162306a36Sopenharmony_ciMODULE_ALIAS_CRYPTO("serpent");
132