xref: /linux/arch/x86/crypto/camellia_aesni_avx_glue.c (revision d0034a7a4ac7fae708146ac0059b9c47a1543f0d)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2d9b1d2e7SJussi Kivilinna /*
3d9b1d2e7SJussi Kivilinna  * Glue Code for x86_64/AVX/AES-NI assembler optimized version of Camellia
4d9b1d2e7SJussi Kivilinna  *
5b5c5b072SJussi Kivilinna  * Copyright © 2012-2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
6d9b1d2e7SJussi Kivilinna  */
7d9b1d2e7SJussi Kivilinna 
844893bc2SEric Biggers #include <crypto/algapi.h>
944893bc2SEric Biggers #include <crypto/internal/simd.h>
1044893bc2SEric Biggers #include <linux/crypto.h>
1144893bc2SEric Biggers #include <linux/err.h>
1244893bc2SEric Biggers #include <linux/module.h>
1344893bc2SEric Biggers #include <linux/types.h>
14d9b1d2e7SJussi Kivilinna 
15*a04ea6f7SArd Biesheuvel #include "camellia.h"
16407d409aSArd Biesheuvel #include "ecb_cbc_helpers.h"
17407d409aSArd Biesheuvel 
18d9b1d2e7SJussi Kivilinna #define CAMELLIA_AESNI_PARALLEL_BLOCKS 16
19d9b1d2e7SJussi Kivilinna 
20f3f935a7SJussi Kivilinna /* 16-way parallel cipher functions (avx/aes-ni) */
219c1e8836SKees Cook asmlinkage void camellia_ecb_enc_16way(const void *ctx, u8 *dst, const u8 *src);
22f3f935a7SJussi Kivilinna EXPORT_SYMBOL_GPL(camellia_ecb_enc_16way);
23f3f935a7SJussi Kivilinna 
249c1e8836SKees Cook asmlinkage void camellia_ecb_dec_16way(const void *ctx, u8 *dst, const u8 *src);
25f3f935a7SJussi Kivilinna EXPORT_SYMBOL_GPL(camellia_ecb_dec_16way);
26d9b1d2e7SJussi Kivilinna 
279c1e8836SKees Cook asmlinkage void camellia_cbc_dec_16way(const void *ctx, u8 *dst, const u8 *src);
28f3f935a7SJussi Kivilinna EXPORT_SYMBOL_GPL(camellia_cbc_dec_16way);
29f3f935a7SJussi Kivilinna 
camellia_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int keylen)3044893bc2SEric Biggers static int camellia_setkey(struct crypto_skcipher *tfm, const u8 *key,
31451cc493SEric Biggers 			   unsigned int keylen)
32451cc493SEric Biggers {
33674f368aSEric Biggers 	return __camellia_setkey(crypto_skcipher_ctx(tfm), key, keylen);
3444893bc2SEric Biggers }
3544893bc2SEric Biggers 
ecb_encrypt(struct skcipher_request * req)3644893bc2SEric Biggers static int ecb_encrypt(struct skcipher_request *req)
3744893bc2SEric Biggers {
38407d409aSArd Biesheuvel 	ECB_WALK_START(req, CAMELLIA_BLOCK_SIZE, CAMELLIA_AESNI_PARALLEL_BLOCKS);
39407d409aSArd Biesheuvel 	ECB_BLOCK(CAMELLIA_AESNI_PARALLEL_BLOCKS, camellia_ecb_enc_16way);
40407d409aSArd Biesheuvel 	ECB_BLOCK(2, camellia_enc_blk_2way);
41407d409aSArd Biesheuvel 	ECB_BLOCK(1, camellia_enc_blk);
42407d409aSArd Biesheuvel 	ECB_WALK_END();
4344893bc2SEric Biggers }
4444893bc2SEric Biggers 
ecb_decrypt(struct skcipher_request * req)4544893bc2SEric Biggers static int ecb_decrypt(struct skcipher_request *req)
4644893bc2SEric Biggers {
47407d409aSArd Biesheuvel 	ECB_WALK_START(req, CAMELLIA_BLOCK_SIZE, CAMELLIA_AESNI_PARALLEL_BLOCKS);
48407d409aSArd Biesheuvel 	ECB_BLOCK(CAMELLIA_AESNI_PARALLEL_BLOCKS, camellia_ecb_dec_16way);
49407d409aSArd Biesheuvel 	ECB_BLOCK(2, camellia_dec_blk_2way);
50407d409aSArd Biesheuvel 	ECB_BLOCK(1, camellia_dec_blk);
51407d409aSArd Biesheuvel 	ECB_WALK_END();
5244893bc2SEric Biggers }
5344893bc2SEric Biggers 
cbc_encrypt(struct skcipher_request * req)5444893bc2SEric Biggers static int cbc_encrypt(struct skcipher_request *req)
5544893bc2SEric Biggers {
56407d409aSArd Biesheuvel 	CBC_WALK_START(req, CAMELLIA_BLOCK_SIZE, -1);
57407d409aSArd Biesheuvel 	CBC_ENC_BLOCK(camellia_enc_blk);
58407d409aSArd Biesheuvel 	CBC_WALK_END();
5944893bc2SEric Biggers }
6044893bc2SEric Biggers 
cbc_decrypt(struct skcipher_request * req)6144893bc2SEric Biggers static int cbc_decrypt(struct skcipher_request *req)
6244893bc2SEric Biggers {
63407d409aSArd Biesheuvel 	CBC_WALK_START(req, CAMELLIA_BLOCK_SIZE, CAMELLIA_AESNI_PARALLEL_BLOCKS);
64407d409aSArd Biesheuvel 	CBC_DEC_BLOCK(CAMELLIA_AESNI_PARALLEL_BLOCKS, camellia_cbc_dec_16way);
65407d409aSArd Biesheuvel 	CBC_DEC_BLOCK(2, camellia_decrypt_cbc_2way);
66407d409aSArd Biesheuvel 	CBC_DEC_BLOCK(1, camellia_dec_blk);
67407d409aSArd Biesheuvel 	CBC_WALK_END();
6844893bc2SEric Biggers }
6944893bc2SEric Biggers 
7044893bc2SEric Biggers static struct skcipher_alg camellia_algs[] = {
7144893bc2SEric Biggers 	{
7244893bc2SEric Biggers 		.base.cra_name		= "__ecb(camellia)",
7344893bc2SEric Biggers 		.base.cra_driver_name	= "__ecb-camellia-aesni",
7444893bc2SEric Biggers 		.base.cra_priority	= 400,
7544893bc2SEric Biggers 		.base.cra_flags		= CRYPTO_ALG_INTERNAL,
7644893bc2SEric Biggers 		.base.cra_blocksize	= CAMELLIA_BLOCK_SIZE,
7744893bc2SEric Biggers 		.base.cra_ctxsize	= sizeof(struct camellia_ctx),
7844893bc2SEric Biggers 		.base.cra_module	= THIS_MODULE,
79d9b1d2e7SJussi Kivilinna 		.min_keysize		= CAMELLIA_MIN_KEY_SIZE,
80d9b1d2e7SJussi Kivilinna 		.max_keysize		= CAMELLIA_MAX_KEY_SIZE,
81d9b1d2e7SJussi Kivilinna 		.setkey			= camellia_setkey,
82d9b1d2e7SJussi Kivilinna 		.encrypt		= ecb_encrypt,
83d9b1d2e7SJussi Kivilinna 		.decrypt		= ecb_decrypt,
84d9b1d2e7SJussi Kivilinna 	}, {
8544893bc2SEric Biggers 		.base.cra_name		= "__cbc(camellia)",
8644893bc2SEric Biggers 		.base.cra_driver_name	= "__cbc-camellia-aesni",
8744893bc2SEric Biggers 		.base.cra_priority	= 400,
8844893bc2SEric Biggers 		.base.cra_flags		= CRYPTO_ALG_INTERNAL,
8944893bc2SEric Biggers 		.base.cra_blocksize	= CAMELLIA_BLOCK_SIZE,
9044893bc2SEric Biggers 		.base.cra_ctxsize	= sizeof(struct camellia_ctx),
9144893bc2SEric Biggers 		.base.cra_module	= THIS_MODULE,
92d9b1d2e7SJussi Kivilinna 		.min_keysize		= CAMELLIA_MIN_KEY_SIZE,
93d9b1d2e7SJussi Kivilinna 		.max_keysize		= CAMELLIA_MAX_KEY_SIZE,
94d9b1d2e7SJussi Kivilinna 		.ivsize			= CAMELLIA_BLOCK_SIZE,
95d9b1d2e7SJussi Kivilinna 		.setkey			= camellia_setkey,
9644893bc2SEric Biggers 		.encrypt		= cbc_encrypt,
9744893bc2SEric Biggers 		.decrypt		= cbc_decrypt,
9855a7e88fSArd Biesheuvel 	}
9944893bc2SEric Biggers };
10044893bc2SEric Biggers 
10144893bc2SEric Biggers static struct simd_skcipher_alg *camellia_simd_algs[ARRAY_SIZE(camellia_algs)];
102d9b1d2e7SJussi Kivilinna 
camellia_aesni_init(void)103d9b1d2e7SJussi Kivilinna static int __init camellia_aesni_init(void)
104d9b1d2e7SJussi Kivilinna {
105ce4f5f9bSIngo Molnar 	const char *feature_name;
106d9b1d2e7SJussi Kivilinna 
107da154e82SBorislav Petkov 	if (!boot_cpu_has(X86_FEATURE_AVX) ||
1081f4dd793SBorislav Petkov 	    !boot_cpu_has(X86_FEATURE_AES) ||
1091f4dd793SBorislav Petkov 	    !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
11092b27907SBen Hutchings 		pr_info("AVX or AES-NI instructions are not detected.\n");
11192b27907SBen Hutchings 		return -ENODEV;
11292b27907SBen Hutchings 	}
11392b27907SBen Hutchings 
114d91cab78SDave Hansen 	if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
115d91cab78SDave Hansen 				&feature_name)) {
116ce4f5f9bSIngo Molnar 		pr_info("CPU feature '%s' is not supported.\n", feature_name);
117d9b1d2e7SJussi Kivilinna 		return -ENODEV;
118d9b1d2e7SJussi Kivilinna 	}
119d9b1d2e7SJussi Kivilinna 
12044893bc2SEric Biggers 	return simd_register_skciphers_compat(camellia_algs,
12144893bc2SEric Biggers 					      ARRAY_SIZE(camellia_algs),
12244893bc2SEric Biggers 					      camellia_simd_algs);
123d9b1d2e7SJussi Kivilinna }
124d9b1d2e7SJussi Kivilinna 
camellia_aesni_fini(void)125d9b1d2e7SJussi Kivilinna static void __exit camellia_aesni_fini(void)
126d9b1d2e7SJussi Kivilinna {
12744893bc2SEric Biggers 	simd_unregister_skciphers(camellia_algs, ARRAY_SIZE(camellia_algs),
12844893bc2SEric Biggers 				  camellia_simd_algs);
129d9b1d2e7SJussi Kivilinna }
130d9b1d2e7SJussi Kivilinna 
131d9b1d2e7SJussi Kivilinna module_init(camellia_aesni_init);
132d9b1d2e7SJussi Kivilinna module_exit(camellia_aesni_fini);
133d9b1d2e7SJussi Kivilinna 
134d9b1d2e7SJussi Kivilinna MODULE_LICENSE("GPL");
135d9b1d2e7SJussi Kivilinna MODULE_DESCRIPTION("Camellia Cipher Algorithm, AES-NI/AVX optimized");
1365d26a105SKees Cook MODULE_ALIAS_CRYPTO("camellia");
1375d26a105SKees Cook MODULE_ALIAS_CRYPTO("camellia-asm");
138