18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Glue Code for SSE2 assembler versions of Serpent Cipher
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Glue code based on aesni-intel_glue.c by:
88c2ecf20Sopenharmony_ci *  Copyright (C) 2008, Intel Corp.
98c2ecf20Sopenharmony_ci *    Author: Huang Ying <ying.huang@intel.com>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
128c2ecf20Sopenharmony_ci *   Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
138c2ecf20Sopenharmony_ci * CTR part based on code (crypto/ctr.c) by:
148c2ecf20Sopenharmony_ci *   (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci#include <linux/types.h>
198c2ecf20Sopenharmony_ci#include <linux/crypto.h>
208c2ecf20Sopenharmony_ci#include <linux/err.h>
218c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
228c2ecf20Sopenharmony_ci#include <crypto/b128ops.h>
238c2ecf20Sopenharmony_ci#include <crypto/internal/simd.h>
248c2ecf20Sopenharmony_ci#include <crypto/serpent.h>
258c2ecf20Sopenharmony_ci#include <asm/crypto/serpent-sse2.h>
268c2ecf20Sopenharmony_ci#include <asm/crypto/glue_helper.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic int serpent_setkey_skcipher(struct crypto_skcipher *tfm,
298c2ecf20Sopenharmony_ci				   const u8 *key, unsigned int keylen)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	return __serpent_setkey(crypto_skcipher_ctx(tfm), key, keylen);
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic void serpent_decrypt_cbc_xway(const void *ctx, u8 *d, const u8 *s)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	u128 ivs[SERPENT_PARALLEL_BLOCKS - 1];
378c2ecf20Sopenharmony_ci	u128 *dst = (u128 *)d;
388c2ecf20Sopenharmony_ci	const u128 *src = (const u128 *)s;
398c2ecf20Sopenharmony_ci	unsigned int j;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	for (j = 0; j < SERPENT_PARALLEL_BLOCKS - 1; j++)
428c2ecf20Sopenharmony_ci		ivs[j] = src[j];
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	serpent_dec_blk_xway(ctx, (u8 *)dst, (u8 *)src);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	for (j = 0; j < SERPENT_PARALLEL_BLOCKS - 1; j++)
478c2ecf20Sopenharmony_ci		u128_xor(dst + (j + 1), dst + (j + 1), ivs + j);
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic void serpent_crypt_ctr(const void *ctx, u8 *d, const u8 *s, le128 *iv)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	be128 ctrblk;
538c2ecf20Sopenharmony_ci	u128 *dst = (u128 *)d;
548c2ecf20Sopenharmony_ci	const u128 *src = (const u128 *)s;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	le128_to_be128(&ctrblk, iv);
578c2ecf20Sopenharmony_ci	le128_inc(iv);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	__serpent_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
608c2ecf20Sopenharmony_ci	u128_xor(dst, src, (u128 *)&ctrblk);
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic void serpent_crypt_ctr_xway(const void *ctx, u8 *d, const u8 *s,
648c2ecf20Sopenharmony_ci				   le128 *iv)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	be128 ctrblks[SERPENT_PARALLEL_BLOCKS];
678c2ecf20Sopenharmony_ci	u128 *dst = (u128 *)d;
688c2ecf20Sopenharmony_ci	const u128 *src = (const u128 *)s;
698c2ecf20Sopenharmony_ci	unsigned int i;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	for (i = 0; i < SERPENT_PARALLEL_BLOCKS; i++) {
728c2ecf20Sopenharmony_ci		if (dst != src)
738c2ecf20Sopenharmony_ci			dst[i] = src[i];
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci		le128_to_be128(&ctrblks[i], iv);
768c2ecf20Sopenharmony_ci		le128_inc(iv);
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	serpent_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks);
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic const struct common_glue_ctx serpent_enc = {
838c2ecf20Sopenharmony_ci	.num_funcs = 2,
848c2ecf20Sopenharmony_ci	.fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	.funcs = { {
878c2ecf20Sopenharmony_ci		.num_blocks = SERPENT_PARALLEL_BLOCKS,
888c2ecf20Sopenharmony_ci		.fn_u = { .ecb = serpent_enc_blk_xway }
898c2ecf20Sopenharmony_ci	}, {
908c2ecf20Sopenharmony_ci		.num_blocks = 1,
918c2ecf20Sopenharmony_ci		.fn_u = { .ecb = __serpent_encrypt }
928c2ecf20Sopenharmony_ci	} }
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic const struct common_glue_ctx serpent_ctr = {
968c2ecf20Sopenharmony_ci	.num_funcs = 2,
978c2ecf20Sopenharmony_ci	.fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	.funcs = { {
1008c2ecf20Sopenharmony_ci		.num_blocks = SERPENT_PARALLEL_BLOCKS,
1018c2ecf20Sopenharmony_ci		.fn_u = { .ctr = serpent_crypt_ctr_xway }
1028c2ecf20Sopenharmony_ci	}, {
1038c2ecf20Sopenharmony_ci		.num_blocks = 1,
1048c2ecf20Sopenharmony_ci		.fn_u = { .ctr = serpent_crypt_ctr }
1058c2ecf20Sopenharmony_ci	} }
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic const struct common_glue_ctx serpent_dec = {
1098c2ecf20Sopenharmony_ci	.num_funcs = 2,
1108c2ecf20Sopenharmony_ci	.fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	.funcs = { {
1138c2ecf20Sopenharmony_ci		.num_blocks = SERPENT_PARALLEL_BLOCKS,
1148c2ecf20Sopenharmony_ci		.fn_u = { .ecb = serpent_dec_blk_xway }
1158c2ecf20Sopenharmony_ci	}, {
1168c2ecf20Sopenharmony_ci		.num_blocks = 1,
1178c2ecf20Sopenharmony_ci		.fn_u = { .ecb = __serpent_decrypt }
1188c2ecf20Sopenharmony_ci	} }
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic const struct common_glue_ctx serpent_dec_cbc = {
1228c2ecf20Sopenharmony_ci	.num_funcs = 2,
1238c2ecf20Sopenharmony_ci	.fpu_blocks_limit = SERPENT_PARALLEL_BLOCKS,
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	.funcs = { {
1268c2ecf20Sopenharmony_ci		.num_blocks = SERPENT_PARALLEL_BLOCKS,
1278c2ecf20Sopenharmony_ci		.fn_u = { .cbc = serpent_decrypt_cbc_xway }
1288c2ecf20Sopenharmony_ci	}, {
1298c2ecf20Sopenharmony_ci		.num_blocks = 1,
1308c2ecf20Sopenharmony_ci		.fn_u = { .cbc = __serpent_decrypt }
1318c2ecf20Sopenharmony_ci	} }
1328c2ecf20Sopenharmony_ci};
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic int ecb_encrypt(struct skcipher_request *req)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	return glue_ecb_req_128bit(&serpent_enc, req);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic int ecb_decrypt(struct skcipher_request *req)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	return glue_ecb_req_128bit(&serpent_dec, req);
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic int cbc_encrypt(struct skcipher_request *req)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	return glue_cbc_encrypt_req_128bit(__serpent_encrypt,
1478c2ecf20Sopenharmony_ci					   req);
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic int cbc_decrypt(struct skcipher_request *req)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	return glue_cbc_decrypt_req_128bit(&serpent_dec_cbc, req);
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic int ctr_crypt(struct skcipher_request *req)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	return glue_ctr_req_128bit(&serpent_ctr, req);
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic struct skcipher_alg serpent_algs[] = {
1618c2ecf20Sopenharmony_ci	{
1628c2ecf20Sopenharmony_ci		.base.cra_name		= "__ecb(serpent)",
1638c2ecf20Sopenharmony_ci		.base.cra_driver_name	= "__ecb-serpent-sse2",
1648c2ecf20Sopenharmony_ci		.base.cra_priority	= 400,
1658c2ecf20Sopenharmony_ci		.base.cra_flags		= CRYPTO_ALG_INTERNAL,
1668c2ecf20Sopenharmony_ci		.base.cra_blocksize	= SERPENT_BLOCK_SIZE,
1678c2ecf20Sopenharmony_ci		.base.cra_ctxsize	= sizeof(struct serpent_ctx),
1688c2ecf20Sopenharmony_ci		.base.cra_module	= THIS_MODULE,
1698c2ecf20Sopenharmony_ci		.min_keysize		= SERPENT_MIN_KEY_SIZE,
1708c2ecf20Sopenharmony_ci		.max_keysize		= SERPENT_MAX_KEY_SIZE,
1718c2ecf20Sopenharmony_ci		.setkey			= serpent_setkey_skcipher,
1728c2ecf20Sopenharmony_ci		.encrypt		= ecb_encrypt,
1738c2ecf20Sopenharmony_ci		.decrypt		= ecb_decrypt,
1748c2ecf20Sopenharmony_ci	}, {
1758c2ecf20Sopenharmony_ci		.base.cra_name		= "__cbc(serpent)",
1768c2ecf20Sopenharmony_ci		.base.cra_driver_name	= "__cbc-serpent-sse2",
1778c2ecf20Sopenharmony_ci		.base.cra_priority	= 400,
1788c2ecf20Sopenharmony_ci		.base.cra_flags		= CRYPTO_ALG_INTERNAL,
1798c2ecf20Sopenharmony_ci		.base.cra_blocksize	= SERPENT_BLOCK_SIZE,
1808c2ecf20Sopenharmony_ci		.base.cra_ctxsize	= sizeof(struct serpent_ctx),
1818c2ecf20Sopenharmony_ci		.base.cra_module	= THIS_MODULE,
1828c2ecf20Sopenharmony_ci		.min_keysize		= SERPENT_MIN_KEY_SIZE,
1838c2ecf20Sopenharmony_ci		.max_keysize		= SERPENT_MAX_KEY_SIZE,
1848c2ecf20Sopenharmony_ci		.ivsize			= SERPENT_BLOCK_SIZE,
1858c2ecf20Sopenharmony_ci		.setkey			= serpent_setkey_skcipher,
1868c2ecf20Sopenharmony_ci		.encrypt		= cbc_encrypt,
1878c2ecf20Sopenharmony_ci		.decrypt		= cbc_decrypt,
1888c2ecf20Sopenharmony_ci	}, {
1898c2ecf20Sopenharmony_ci		.base.cra_name		= "__ctr(serpent)",
1908c2ecf20Sopenharmony_ci		.base.cra_driver_name	= "__ctr-serpent-sse2",
1918c2ecf20Sopenharmony_ci		.base.cra_priority	= 400,
1928c2ecf20Sopenharmony_ci		.base.cra_flags		= CRYPTO_ALG_INTERNAL,
1938c2ecf20Sopenharmony_ci		.base.cra_blocksize	= 1,
1948c2ecf20Sopenharmony_ci		.base.cra_ctxsize	= sizeof(struct serpent_ctx),
1958c2ecf20Sopenharmony_ci		.base.cra_module	= THIS_MODULE,
1968c2ecf20Sopenharmony_ci		.min_keysize		= SERPENT_MIN_KEY_SIZE,
1978c2ecf20Sopenharmony_ci		.max_keysize		= SERPENT_MAX_KEY_SIZE,
1988c2ecf20Sopenharmony_ci		.ivsize			= SERPENT_BLOCK_SIZE,
1998c2ecf20Sopenharmony_ci		.chunksize		= SERPENT_BLOCK_SIZE,
2008c2ecf20Sopenharmony_ci		.setkey			= serpent_setkey_skcipher,
2018c2ecf20Sopenharmony_ci		.encrypt		= ctr_crypt,
2028c2ecf20Sopenharmony_ci		.decrypt		= ctr_crypt,
2038c2ecf20Sopenharmony_ci	},
2048c2ecf20Sopenharmony_ci};
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic struct simd_skcipher_alg *serpent_simd_algs[ARRAY_SIZE(serpent_algs)];
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic int __init serpent_sse2_init(void)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	if (!boot_cpu_has(X86_FEATURE_XMM2)) {
2118c2ecf20Sopenharmony_ci		printk(KERN_INFO "SSE2 instructions are not detected.\n");
2128c2ecf20Sopenharmony_ci		return -ENODEV;
2138c2ecf20Sopenharmony_ci	}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	return simd_register_skciphers_compat(serpent_algs,
2168c2ecf20Sopenharmony_ci					      ARRAY_SIZE(serpent_algs),
2178c2ecf20Sopenharmony_ci					      serpent_simd_algs);
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic void __exit serpent_sse2_exit(void)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	simd_unregister_skciphers(serpent_algs, ARRAY_SIZE(serpent_algs),
2238c2ecf20Sopenharmony_ci				  serpent_simd_algs);
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cimodule_init(serpent_sse2_init);
2278c2ecf20Sopenharmony_cimodule_exit(serpent_sse2_exit);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Serpent Cipher Algorithm, SSE2 optimized");
2308c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2318c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("serpent");
232