109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
281658ad0SDavid S. Miller /* Glue code for CAMELLIA encryption optimized for sparc64 crypto opcodes.
381658ad0SDavid S. Miller  *
481658ad0SDavid S. Miller  * Copyright (C) 2012 David S. Miller <davem@davemloft.net>
581658ad0SDavid S. Miller  */
681658ad0SDavid S. Miller 
771741680SDavid S. Miller #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
871741680SDavid S. Miller 
981658ad0SDavid S. Miller #include <linux/crypto.h>
1081658ad0SDavid S. Miller #include <linux/init.h>
1181658ad0SDavid S. Miller #include <linux/module.h>
1281658ad0SDavid S. Miller #include <linux/mm.h>
1381658ad0SDavid S. Miller #include <linux/types.h>
1481658ad0SDavid S. Miller #include <crypto/algapi.h>
15c72a26efSEric Biggers #include <crypto/internal/skcipher.h>
1681658ad0SDavid S. Miller 
1781658ad0SDavid S. Miller #include <asm/fpumacro.h>
1881658ad0SDavid S. Miller #include <asm/pstate.h>
1981658ad0SDavid S. Miller #include <asm/elf.h>
2081658ad0SDavid S. Miller 
2110803624SDavid S. Miller #include "opcodes.h"
2210803624SDavid S. Miller 
2381658ad0SDavid S. Miller #define CAMELLIA_MIN_KEY_SIZE        16
2481658ad0SDavid S. Miller #define CAMELLIA_MAX_KEY_SIZE        32
2581658ad0SDavid S. Miller #define CAMELLIA_BLOCK_SIZE          16
2681658ad0SDavid S. Miller #define CAMELLIA_TABLE_BYTE_LEN     272
2781658ad0SDavid S. Miller 
2881658ad0SDavid S. Miller struct camellia_sparc64_ctx {
2981658ad0SDavid S. Miller 	u64 encrypt_key[CAMELLIA_TABLE_BYTE_LEN / sizeof(u64)];
3081658ad0SDavid S. Miller 	u64 decrypt_key[CAMELLIA_TABLE_BYTE_LEN / sizeof(u64)];
3181658ad0SDavid S. Miller 	int key_len;
3281658ad0SDavid S. Miller };
3381658ad0SDavid S. Miller 
3481658ad0SDavid S. Miller extern void camellia_sparc64_key_expand(const u32 *in_key, u64 *encrypt_key,
3581658ad0SDavid S. Miller 					unsigned int key_len, u64 *decrypt_key);
3681658ad0SDavid S. Miller 
camellia_set_key(struct crypto_tfm * tfm,const u8 * _in_key,unsigned int key_len)3781658ad0SDavid S. Miller static int camellia_set_key(struct crypto_tfm *tfm, const u8 *_in_key,
3881658ad0SDavid S. Miller 			    unsigned int key_len)
3981658ad0SDavid S. Miller {
4081658ad0SDavid S. Miller 	struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
4181658ad0SDavid S. Miller 	const u32 *in_key = (const u32 *) _in_key;
4281658ad0SDavid S. Miller 
43674f368aSEric Biggers 	if (key_len != 16 && key_len != 24 && key_len != 32)
4481658ad0SDavid S. Miller 		return -EINVAL;
4581658ad0SDavid S. Miller 
4681658ad0SDavid S. Miller 	ctx->key_len = key_len;
4781658ad0SDavid S. Miller 
4881658ad0SDavid S. Miller 	camellia_sparc64_key_expand(in_key, &ctx->encrypt_key[0],
4981658ad0SDavid S. Miller 				    key_len, &ctx->decrypt_key[0]);
5081658ad0SDavid S. Miller 	return 0;
5181658ad0SDavid S. Miller }
5281658ad0SDavid S. Miller 
camellia_set_key_skcipher(struct crypto_skcipher * tfm,const u8 * in_key,unsigned int key_len)53c72a26efSEric Biggers static int camellia_set_key_skcipher(struct crypto_skcipher *tfm,
54c72a26efSEric Biggers 				     const u8 *in_key, unsigned int key_len)
55c72a26efSEric Biggers {
56c72a26efSEric Biggers 	return camellia_set_key(crypto_skcipher_tfm(tfm), in_key, key_len);
57c72a26efSEric Biggers }
58c72a26efSEric Biggers 
5981658ad0SDavid S. Miller extern void camellia_sparc64_crypt(const u64 *key, const u32 *input,
6081658ad0SDavid S. Miller 				   u32 *output, unsigned int key_len);
6181658ad0SDavid S. Miller 
camellia_encrypt(struct crypto_tfm * tfm,u8 * dst,const u8 * src)6281658ad0SDavid S. Miller static void camellia_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
6381658ad0SDavid S. Miller {
6481658ad0SDavid S. Miller 	struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
6581658ad0SDavid S. Miller 
6681658ad0SDavid S. Miller 	camellia_sparc64_crypt(&ctx->encrypt_key[0],
6781658ad0SDavid S. Miller 			       (const u32 *) src,
6881658ad0SDavid S. Miller 			       (u32 *) dst, ctx->key_len);
6981658ad0SDavid S. Miller }
7081658ad0SDavid S. Miller 
camellia_decrypt(struct crypto_tfm * tfm,u8 * dst,const u8 * src)7181658ad0SDavid S. Miller static void camellia_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
7281658ad0SDavid S. Miller {
7381658ad0SDavid S. Miller 	struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
7481658ad0SDavid S. Miller 
7581658ad0SDavid S. Miller 	camellia_sparc64_crypt(&ctx->decrypt_key[0],
7681658ad0SDavid S. Miller 			       (const u32 *) src,
7781658ad0SDavid S. Miller 			       (u32 *) dst, ctx->key_len);
7881658ad0SDavid S. Miller }
7981658ad0SDavid S. Miller 
8081658ad0SDavid S. Miller extern void camellia_sparc64_load_keys(const u64 *key, unsigned int key_len);
8181658ad0SDavid S. Miller 
8281658ad0SDavid S. Miller typedef void ecb_crypt_op(const u64 *input, u64 *output, unsigned int len,
8381658ad0SDavid S. Miller 			  const u64 *key);
8481658ad0SDavid S. Miller 
8581658ad0SDavid S. Miller extern ecb_crypt_op camellia_sparc64_ecb_crypt_3_grand_rounds;
8681658ad0SDavid S. Miller extern ecb_crypt_op camellia_sparc64_ecb_crypt_4_grand_rounds;
8781658ad0SDavid S. Miller 
__ecb_crypt(struct skcipher_request * req,bool encrypt)88c72a26efSEric Biggers static int __ecb_crypt(struct skcipher_request *req, bool encrypt)
8981658ad0SDavid S. Miller {
90c72a26efSEric Biggers 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
91c72a26efSEric Biggers 	const struct camellia_sparc64_ctx *ctx = crypto_skcipher_ctx(tfm);
92c72a26efSEric Biggers 	struct skcipher_walk walk;
9381658ad0SDavid S. Miller 	ecb_crypt_op *op;
9481658ad0SDavid S. Miller 	const u64 *key;
95c72a26efSEric Biggers 	unsigned int nbytes;
9681658ad0SDavid S. Miller 	int err;
9781658ad0SDavid S. Miller 
9881658ad0SDavid S. Miller 	op = camellia_sparc64_ecb_crypt_3_grand_rounds;
9981658ad0SDavid S. Miller 	if (ctx->key_len != 16)
10081658ad0SDavid S. Miller 		op = camellia_sparc64_ecb_crypt_4_grand_rounds;
10181658ad0SDavid S. Miller 
102c72a26efSEric Biggers 	err = skcipher_walk_virt(&walk, req, true);
103c72a26efSEric Biggers 	if (err)
104c72a26efSEric Biggers 		return err;
10581658ad0SDavid S. Miller 
10681658ad0SDavid S. Miller 	if (encrypt)
10781658ad0SDavid S. Miller 		key = &ctx->encrypt_key[0];
10881658ad0SDavid S. Miller 	else
10981658ad0SDavid S. Miller 		key = &ctx->decrypt_key[0];
11081658ad0SDavid S. Miller 	camellia_sparc64_load_keys(key, ctx->key_len);
111c72a26efSEric Biggers 	while ((nbytes = walk.nbytes) != 0) {
112c72a26efSEric Biggers 		op(walk.src.virt.addr, walk.dst.virt.addr,
113c72a26efSEric Biggers 		   round_down(nbytes, CAMELLIA_BLOCK_SIZE), key);
114c72a26efSEric Biggers 		err = skcipher_walk_done(&walk, nbytes % CAMELLIA_BLOCK_SIZE);
11581658ad0SDavid S. Miller 	}
11681658ad0SDavid S. Miller 	fprs_write(0);
11781658ad0SDavid S. Miller 	return err;
11881658ad0SDavid S. Miller }
11981658ad0SDavid S. Miller 
ecb_encrypt(struct skcipher_request * req)120c72a26efSEric Biggers static int ecb_encrypt(struct skcipher_request *req)
12181658ad0SDavid S. Miller {
122c72a26efSEric Biggers 	return __ecb_crypt(req, true);
12381658ad0SDavid S. Miller }
12481658ad0SDavid S. Miller 
ecb_decrypt(struct skcipher_request * req)125c72a26efSEric Biggers static int ecb_decrypt(struct skcipher_request *req)
12681658ad0SDavid S. Miller {
127c72a26efSEric Biggers 	return __ecb_crypt(req, false);
12881658ad0SDavid S. Miller }
12981658ad0SDavid S. Miller 
13081658ad0SDavid S. Miller typedef void cbc_crypt_op(const u64 *input, u64 *output, unsigned int len,
13181658ad0SDavid S. Miller 			  const u64 *key, u64 *iv);
13281658ad0SDavid S. Miller 
13381658ad0SDavid S. Miller extern cbc_crypt_op camellia_sparc64_cbc_encrypt_3_grand_rounds;
13481658ad0SDavid S. Miller extern cbc_crypt_op camellia_sparc64_cbc_encrypt_4_grand_rounds;
13581658ad0SDavid S. Miller extern cbc_crypt_op camellia_sparc64_cbc_decrypt_3_grand_rounds;
13681658ad0SDavid S. Miller extern cbc_crypt_op camellia_sparc64_cbc_decrypt_4_grand_rounds;
13781658ad0SDavid S. Miller 
cbc_encrypt(struct skcipher_request * req)138c72a26efSEric Biggers static int cbc_encrypt(struct skcipher_request *req)
13981658ad0SDavid S. Miller {
140c72a26efSEric Biggers 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
141c72a26efSEric Biggers 	const struct camellia_sparc64_ctx *ctx = crypto_skcipher_ctx(tfm);
142c72a26efSEric Biggers 	struct skcipher_walk walk;
14381658ad0SDavid S. Miller 	cbc_crypt_op *op;
14481658ad0SDavid S. Miller 	const u64 *key;
145c72a26efSEric Biggers 	unsigned int nbytes;
14681658ad0SDavid S. Miller 	int err;
14781658ad0SDavid S. Miller 
14881658ad0SDavid S. Miller 	op = camellia_sparc64_cbc_encrypt_3_grand_rounds;
14981658ad0SDavid S. Miller 	if (ctx->key_len != 16)
15081658ad0SDavid S. Miller 		op = camellia_sparc64_cbc_encrypt_4_grand_rounds;
15181658ad0SDavid S. Miller 
152c72a26efSEric Biggers 	err = skcipher_walk_virt(&walk, req, true);
153c72a26efSEric Biggers 	if (err)
154c72a26efSEric Biggers 		return err;
15581658ad0SDavid S. Miller 
15681658ad0SDavid S. Miller 	key = &ctx->encrypt_key[0];
15781658ad0SDavid S. Miller 	camellia_sparc64_load_keys(key, ctx->key_len);
158c72a26efSEric Biggers 	while ((nbytes = walk.nbytes) != 0) {
159c72a26efSEric Biggers 		op(walk.src.virt.addr, walk.dst.virt.addr,
160c72a26efSEric Biggers 		   round_down(nbytes, CAMELLIA_BLOCK_SIZE), key, walk.iv);
161c72a26efSEric Biggers 		err = skcipher_walk_done(&walk, nbytes % CAMELLIA_BLOCK_SIZE);
16281658ad0SDavid S. Miller 	}
16381658ad0SDavid S. Miller 	fprs_write(0);
16481658ad0SDavid S. Miller 	return err;
16581658ad0SDavid S. Miller }
16681658ad0SDavid S. Miller 
cbc_decrypt(struct skcipher_request * req)167c72a26efSEric Biggers static int cbc_decrypt(struct skcipher_request *req)
16881658ad0SDavid S. Miller {
169c72a26efSEric Biggers 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
170c72a26efSEric Biggers 	const struct camellia_sparc64_ctx *ctx = crypto_skcipher_ctx(tfm);
171c72a26efSEric Biggers 	struct skcipher_walk walk;
17281658ad0SDavid S. Miller 	cbc_crypt_op *op;
17381658ad0SDavid S. Miller 	const u64 *key;
174c72a26efSEric Biggers 	unsigned int nbytes;
17581658ad0SDavid S. Miller 	int err;
17681658ad0SDavid S. Miller 
17781658ad0SDavid S. Miller 	op = camellia_sparc64_cbc_decrypt_3_grand_rounds;
17881658ad0SDavid S. Miller 	if (ctx->key_len != 16)
17981658ad0SDavid S. Miller 		op = camellia_sparc64_cbc_decrypt_4_grand_rounds;
18081658ad0SDavid S. Miller 
181c72a26efSEric Biggers 	err = skcipher_walk_virt(&walk, req, true);
182c72a26efSEric Biggers 	if (err)
183c72a26efSEric Biggers 		return err;
18481658ad0SDavid S. Miller 
18581658ad0SDavid S. Miller 	key = &ctx->decrypt_key[0];
18681658ad0SDavid S. Miller 	camellia_sparc64_load_keys(key, ctx->key_len);
187c72a26efSEric Biggers 	while ((nbytes = walk.nbytes) != 0) {
188c72a26efSEric Biggers 		op(walk.src.virt.addr, walk.dst.virt.addr,
189c72a26efSEric Biggers 		   round_down(nbytes, CAMELLIA_BLOCK_SIZE), key, walk.iv);
190c72a26efSEric Biggers 		err = skcipher_walk_done(&walk, nbytes % CAMELLIA_BLOCK_SIZE);
19181658ad0SDavid S. Miller 	}
19281658ad0SDavid S. Miller 	fprs_write(0);
19381658ad0SDavid S. Miller 	return err;
19481658ad0SDavid S. Miller }
19581658ad0SDavid S. Miller 
196c72a26efSEric Biggers static struct crypto_alg cipher_alg = {
19781658ad0SDavid S. Miller 	.cra_name		= "camellia",
19881658ad0SDavid S. Miller 	.cra_driver_name	= "camellia-sparc64",
19910803624SDavid S. Miller 	.cra_priority		= SPARC_CR_OPCODE_PRIORITY,
20081658ad0SDavid S. Miller 	.cra_flags		= CRYPTO_ALG_TYPE_CIPHER,
20181658ad0SDavid S. Miller 	.cra_blocksize		= CAMELLIA_BLOCK_SIZE,
20281658ad0SDavid S. Miller 	.cra_ctxsize		= sizeof(struct camellia_sparc64_ctx),
20381658ad0SDavid S. Miller 	.cra_alignmask		= 3,
20481658ad0SDavid S. Miller 	.cra_module		= THIS_MODULE,
20581658ad0SDavid S. Miller 	.cra_u	= {
20681658ad0SDavid S. Miller 		.cipher	= {
20781658ad0SDavid S. Miller 			.cia_min_keysize	= CAMELLIA_MIN_KEY_SIZE,
20881658ad0SDavid S. Miller 			.cia_max_keysize	= CAMELLIA_MAX_KEY_SIZE,
20981658ad0SDavid S. Miller 			.cia_setkey		= camellia_set_key,
21081658ad0SDavid S. Miller 			.cia_encrypt		= camellia_encrypt,
21181658ad0SDavid S. Miller 			.cia_decrypt		= camellia_decrypt
21281658ad0SDavid S. Miller 		}
21381658ad0SDavid S. Miller 	}
214c72a26efSEric Biggers };
215c72a26efSEric Biggers 
216c72a26efSEric Biggers static struct skcipher_alg skcipher_algs[] = {
217c72a26efSEric Biggers 	{
218c72a26efSEric Biggers 		.base.cra_name		= "ecb(camellia)",
219c72a26efSEric Biggers 		.base.cra_driver_name	= "ecb-camellia-sparc64",
220c72a26efSEric Biggers 		.base.cra_priority	= SPARC_CR_OPCODE_PRIORITY,
221c72a26efSEric Biggers 		.base.cra_blocksize	= CAMELLIA_BLOCK_SIZE,
222c72a26efSEric Biggers 		.base.cra_ctxsize	= sizeof(struct camellia_sparc64_ctx),
223c72a26efSEric Biggers 		.base.cra_alignmask	= 7,
224c72a26efSEric Biggers 		.base.cra_module	= THIS_MODULE,
22581658ad0SDavid S. Miller 		.min_keysize		= CAMELLIA_MIN_KEY_SIZE,
22681658ad0SDavid S. Miller 		.max_keysize		= CAMELLIA_MAX_KEY_SIZE,
227c72a26efSEric Biggers 		.setkey			= camellia_set_key_skcipher,
22881658ad0SDavid S. Miller 		.encrypt		= ecb_encrypt,
22981658ad0SDavid S. Miller 		.decrypt		= ecb_decrypt,
23081658ad0SDavid S. Miller 	}, {
231c72a26efSEric Biggers 		.base.cra_name		= "cbc(camellia)",
232c72a26efSEric Biggers 		.base.cra_driver_name	= "cbc-camellia-sparc64",
233c72a26efSEric Biggers 		.base.cra_priority	= SPARC_CR_OPCODE_PRIORITY,
234c72a26efSEric Biggers 		.base.cra_blocksize	= CAMELLIA_BLOCK_SIZE,
235c72a26efSEric Biggers 		.base.cra_ctxsize	= sizeof(struct camellia_sparc64_ctx),
236c72a26efSEric Biggers 		.base.cra_alignmask	= 7,
237c72a26efSEric Biggers 		.base.cra_module	= THIS_MODULE,
23881658ad0SDavid S. Miller 		.min_keysize		= CAMELLIA_MIN_KEY_SIZE,
23981658ad0SDavid S. Miller 		.max_keysize		= CAMELLIA_MAX_KEY_SIZE,
240a66d7f72SDave Kleikamp 		.ivsize			= CAMELLIA_BLOCK_SIZE,
241c72a26efSEric Biggers 		.setkey			= camellia_set_key_skcipher,
24281658ad0SDavid S. Miller 		.encrypt		= cbc_encrypt,
24381658ad0SDavid S. Miller 		.decrypt		= cbc_decrypt,
24481658ad0SDavid S. Miller 	}
24581658ad0SDavid S. Miller };
24681658ad0SDavid S. Miller 
sparc64_has_camellia_opcode(void)24781658ad0SDavid S. Miller static bool __init sparc64_has_camellia_opcode(void)
24881658ad0SDavid S. Miller {
24981658ad0SDavid S. Miller 	unsigned long cfr;
25081658ad0SDavid S. Miller 
25181658ad0SDavid S. Miller 	if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
25281658ad0SDavid S. Miller 		return false;
25381658ad0SDavid S. Miller 
25481658ad0SDavid S. Miller 	__asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
25581658ad0SDavid S. Miller 	if (!(cfr & CFR_CAMELLIA))
25681658ad0SDavid S. Miller 		return false;
25781658ad0SDavid S. Miller 
25881658ad0SDavid S. Miller 	return true;
25981658ad0SDavid S. Miller }
26081658ad0SDavid S. Miller 
camellia_sparc64_mod_init(void)26181658ad0SDavid S. Miller static int __init camellia_sparc64_mod_init(void)
26281658ad0SDavid S. Miller {
263c72a26efSEric Biggers 	int err;
264c72a26efSEric Biggers 
265c72a26efSEric Biggers 	if (!sparc64_has_camellia_opcode()) {
26681658ad0SDavid S. Miller 		pr_info("sparc64 camellia opcodes not available.\n");
26781658ad0SDavid S. Miller 		return -ENODEV;
26881658ad0SDavid S. Miller 	}
269c72a26efSEric Biggers 	pr_info("Using sparc64 camellia opcodes optimized CAMELLIA implementation\n");
270c72a26efSEric Biggers 	err = crypto_register_alg(&cipher_alg);
271c72a26efSEric Biggers 	if (err)
272c72a26efSEric Biggers 		return err;
273c72a26efSEric Biggers 	err = crypto_register_skciphers(skcipher_algs,
274c72a26efSEric Biggers 					ARRAY_SIZE(skcipher_algs));
275c72a26efSEric Biggers 	if (err)
276c72a26efSEric Biggers 		crypto_unregister_alg(&cipher_alg);
277c72a26efSEric Biggers 	return err;
278c72a26efSEric Biggers }
27981658ad0SDavid S. Miller 
camellia_sparc64_mod_fini(void)28081658ad0SDavid S. Miller static void __exit camellia_sparc64_mod_fini(void)
28181658ad0SDavid S. Miller {
282c72a26efSEric Biggers 	crypto_unregister_alg(&cipher_alg);
283c72a26efSEric Biggers 	crypto_unregister_skciphers(skcipher_algs, ARRAY_SIZE(skcipher_algs));
28481658ad0SDavid S. Miller }
28581658ad0SDavid S. Miller 
28681658ad0SDavid S. Miller module_init(camellia_sparc64_mod_init);
28781658ad0SDavid S. Miller module_exit(camellia_sparc64_mod_fini);
28881658ad0SDavid S. Miller 
28981658ad0SDavid S. Miller MODULE_LICENSE("GPL");
29081658ad0SDavid S. Miller MODULE_DESCRIPTION("Camellia Cipher Algorithm, sparc64 camellia opcode accelerated");
29181658ad0SDavid S. Miller 
2927d676fdbSMathias Krause MODULE_ALIAS_CRYPTO("camellia");
293226f7ceaSDavid S. Miller 
294226f7ceaSDavid S. Miller #include "crop_devid.c"
295