18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/* Glue code for CAMELLIA encryption optimized for sparc64 crypto opcodes.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2012 David S. Miller <davem@davemloft.net>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/crypto.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/mm.h>
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
158c2ecf20Sopenharmony_ci#include <crypto/internal/skcipher.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <asm/fpumacro.h>
188c2ecf20Sopenharmony_ci#include <asm/pstate.h>
198c2ecf20Sopenharmony_ci#include <asm/elf.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "opcodes.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define CAMELLIA_MIN_KEY_SIZE        16
248c2ecf20Sopenharmony_ci#define CAMELLIA_MAX_KEY_SIZE        32
258c2ecf20Sopenharmony_ci#define CAMELLIA_BLOCK_SIZE          16
268c2ecf20Sopenharmony_ci#define CAMELLIA_TABLE_BYTE_LEN     272
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistruct camellia_sparc64_ctx {
298c2ecf20Sopenharmony_ci	u64 encrypt_key[CAMELLIA_TABLE_BYTE_LEN / sizeof(u64)];
308c2ecf20Sopenharmony_ci	u64 decrypt_key[CAMELLIA_TABLE_BYTE_LEN / sizeof(u64)];
318c2ecf20Sopenharmony_ci	int key_len;
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ciextern void camellia_sparc64_key_expand(const u32 *in_key, u64 *encrypt_key,
358c2ecf20Sopenharmony_ci					unsigned int key_len, u64 *decrypt_key);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic int camellia_set_key(struct crypto_tfm *tfm, const u8 *_in_key,
388c2ecf20Sopenharmony_ci			    unsigned int key_len)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
418c2ecf20Sopenharmony_ci	const u32 *in_key = (const u32 *) _in_key;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	if (key_len != 16 && key_len != 24 && key_len != 32)
448c2ecf20Sopenharmony_ci		return -EINVAL;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	ctx->key_len = key_len;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	camellia_sparc64_key_expand(in_key, &ctx->encrypt_key[0],
498c2ecf20Sopenharmony_ci				    key_len, &ctx->decrypt_key[0]);
508c2ecf20Sopenharmony_ci	return 0;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic int camellia_set_key_skcipher(struct crypto_skcipher *tfm,
548c2ecf20Sopenharmony_ci				     const u8 *in_key, unsigned int key_len)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	return camellia_set_key(crypto_skcipher_tfm(tfm), in_key, key_len);
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciextern void camellia_sparc64_crypt(const u64 *key, const u32 *input,
608c2ecf20Sopenharmony_ci				   u32 *output, unsigned int key_len);
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic void camellia_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	camellia_sparc64_crypt(&ctx->encrypt_key[0],
678c2ecf20Sopenharmony_ci			       (const u32 *) src,
688c2ecf20Sopenharmony_ci			       (u32 *) dst, ctx->key_len);
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic void camellia_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	camellia_sparc64_crypt(&ctx->decrypt_key[0],
768c2ecf20Sopenharmony_ci			       (const u32 *) src,
778c2ecf20Sopenharmony_ci			       (u32 *) dst, ctx->key_len);
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ciextern void camellia_sparc64_load_keys(const u64 *key, unsigned int key_len);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_citypedef void ecb_crypt_op(const u64 *input, u64 *output, unsigned int len,
838c2ecf20Sopenharmony_ci			  const u64 *key);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciextern ecb_crypt_op camellia_sparc64_ecb_crypt_3_grand_rounds;
868c2ecf20Sopenharmony_ciextern ecb_crypt_op camellia_sparc64_ecb_crypt_4_grand_rounds;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic int __ecb_crypt(struct skcipher_request *req, bool encrypt)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
918c2ecf20Sopenharmony_ci	const struct camellia_sparc64_ctx *ctx = crypto_skcipher_ctx(tfm);
928c2ecf20Sopenharmony_ci	struct skcipher_walk walk;
938c2ecf20Sopenharmony_ci	ecb_crypt_op *op;
948c2ecf20Sopenharmony_ci	const u64 *key;
958c2ecf20Sopenharmony_ci	unsigned int nbytes;
968c2ecf20Sopenharmony_ci	int err;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	op = camellia_sparc64_ecb_crypt_3_grand_rounds;
998c2ecf20Sopenharmony_ci	if (ctx->key_len != 16)
1008c2ecf20Sopenharmony_ci		op = camellia_sparc64_ecb_crypt_4_grand_rounds;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	err = skcipher_walk_virt(&walk, req, true);
1038c2ecf20Sopenharmony_ci	if (err)
1048c2ecf20Sopenharmony_ci		return err;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	if (encrypt)
1078c2ecf20Sopenharmony_ci		key = &ctx->encrypt_key[0];
1088c2ecf20Sopenharmony_ci	else
1098c2ecf20Sopenharmony_ci		key = &ctx->decrypt_key[0];
1108c2ecf20Sopenharmony_ci	camellia_sparc64_load_keys(key, ctx->key_len);
1118c2ecf20Sopenharmony_ci	while ((nbytes = walk.nbytes) != 0) {
1128c2ecf20Sopenharmony_ci		op(walk.src.virt.addr, walk.dst.virt.addr,
1138c2ecf20Sopenharmony_ci		   round_down(nbytes, CAMELLIA_BLOCK_SIZE), key);
1148c2ecf20Sopenharmony_ci		err = skcipher_walk_done(&walk, nbytes % CAMELLIA_BLOCK_SIZE);
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci	fprs_write(0);
1178c2ecf20Sopenharmony_ci	return err;
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic int ecb_encrypt(struct skcipher_request *req)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	return __ecb_crypt(req, true);
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic int ecb_decrypt(struct skcipher_request *req)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	return __ecb_crypt(req, false);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_citypedef void cbc_crypt_op(const u64 *input, u64 *output, unsigned int len,
1318c2ecf20Sopenharmony_ci			  const u64 *key, u64 *iv);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ciextern cbc_crypt_op camellia_sparc64_cbc_encrypt_3_grand_rounds;
1348c2ecf20Sopenharmony_ciextern cbc_crypt_op camellia_sparc64_cbc_encrypt_4_grand_rounds;
1358c2ecf20Sopenharmony_ciextern cbc_crypt_op camellia_sparc64_cbc_decrypt_3_grand_rounds;
1368c2ecf20Sopenharmony_ciextern cbc_crypt_op camellia_sparc64_cbc_decrypt_4_grand_rounds;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int cbc_encrypt(struct skcipher_request *req)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1418c2ecf20Sopenharmony_ci	const struct camellia_sparc64_ctx *ctx = crypto_skcipher_ctx(tfm);
1428c2ecf20Sopenharmony_ci	struct skcipher_walk walk;
1438c2ecf20Sopenharmony_ci	cbc_crypt_op *op;
1448c2ecf20Sopenharmony_ci	const u64 *key;
1458c2ecf20Sopenharmony_ci	unsigned int nbytes;
1468c2ecf20Sopenharmony_ci	int err;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	op = camellia_sparc64_cbc_encrypt_3_grand_rounds;
1498c2ecf20Sopenharmony_ci	if (ctx->key_len != 16)
1508c2ecf20Sopenharmony_ci		op = camellia_sparc64_cbc_encrypt_4_grand_rounds;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	err = skcipher_walk_virt(&walk, req, true);
1538c2ecf20Sopenharmony_ci	if (err)
1548c2ecf20Sopenharmony_ci		return err;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	key = &ctx->encrypt_key[0];
1578c2ecf20Sopenharmony_ci	camellia_sparc64_load_keys(key, ctx->key_len);
1588c2ecf20Sopenharmony_ci	while ((nbytes = walk.nbytes) != 0) {
1598c2ecf20Sopenharmony_ci		op(walk.src.virt.addr, walk.dst.virt.addr,
1608c2ecf20Sopenharmony_ci		   round_down(nbytes, CAMELLIA_BLOCK_SIZE), key, walk.iv);
1618c2ecf20Sopenharmony_ci		err = skcipher_walk_done(&walk, nbytes % CAMELLIA_BLOCK_SIZE);
1628c2ecf20Sopenharmony_ci	}
1638c2ecf20Sopenharmony_ci	fprs_write(0);
1648c2ecf20Sopenharmony_ci	return err;
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic int cbc_decrypt(struct skcipher_request *req)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1708c2ecf20Sopenharmony_ci	const struct camellia_sparc64_ctx *ctx = crypto_skcipher_ctx(tfm);
1718c2ecf20Sopenharmony_ci	struct skcipher_walk walk;
1728c2ecf20Sopenharmony_ci	cbc_crypt_op *op;
1738c2ecf20Sopenharmony_ci	const u64 *key;
1748c2ecf20Sopenharmony_ci	unsigned int nbytes;
1758c2ecf20Sopenharmony_ci	int err;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	op = camellia_sparc64_cbc_decrypt_3_grand_rounds;
1788c2ecf20Sopenharmony_ci	if (ctx->key_len != 16)
1798c2ecf20Sopenharmony_ci		op = camellia_sparc64_cbc_decrypt_4_grand_rounds;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	err = skcipher_walk_virt(&walk, req, true);
1828c2ecf20Sopenharmony_ci	if (err)
1838c2ecf20Sopenharmony_ci		return err;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	key = &ctx->decrypt_key[0];
1868c2ecf20Sopenharmony_ci	camellia_sparc64_load_keys(key, ctx->key_len);
1878c2ecf20Sopenharmony_ci	while ((nbytes = walk.nbytes) != 0) {
1888c2ecf20Sopenharmony_ci		op(walk.src.virt.addr, walk.dst.virt.addr,
1898c2ecf20Sopenharmony_ci		   round_down(nbytes, CAMELLIA_BLOCK_SIZE), key, walk.iv);
1908c2ecf20Sopenharmony_ci		err = skcipher_walk_done(&walk, nbytes % CAMELLIA_BLOCK_SIZE);
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci	fprs_write(0);
1938c2ecf20Sopenharmony_ci	return err;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic struct crypto_alg cipher_alg = {
1978c2ecf20Sopenharmony_ci	.cra_name		= "camellia",
1988c2ecf20Sopenharmony_ci	.cra_driver_name	= "camellia-sparc64",
1998c2ecf20Sopenharmony_ci	.cra_priority		= SPARC_CR_OPCODE_PRIORITY,
2008c2ecf20Sopenharmony_ci	.cra_flags		= CRYPTO_ALG_TYPE_CIPHER,
2018c2ecf20Sopenharmony_ci	.cra_blocksize		= CAMELLIA_BLOCK_SIZE,
2028c2ecf20Sopenharmony_ci	.cra_ctxsize		= sizeof(struct camellia_sparc64_ctx),
2038c2ecf20Sopenharmony_ci	.cra_alignmask		= 3,
2048c2ecf20Sopenharmony_ci	.cra_module		= THIS_MODULE,
2058c2ecf20Sopenharmony_ci	.cra_u	= {
2068c2ecf20Sopenharmony_ci		.cipher	= {
2078c2ecf20Sopenharmony_ci			.cia_min_keysize	= CAMELLIA_MIN_KEY_SIZE,
2088c2ecf20Sopenharmony_ci			.cia_max_keysize	= CAMELLIA_MAX_KEY_SIZE,
2098c2ecf20Sopenharmony_ci			.cia_setkey		= camellia_set_key,
2108c2ecf20Sopenharmony_ci			.cia_encrypt		= camellia_encrypt,
2118c2ecf20Sopenharmony_ci			.cia_decrypt		= camellia_decrypt
2128c2ecf20Sopenharmony_ci		}
2138c2ecf20Sopenharmony_ci	}
2148c2ecf20Sopenharmony_ci};
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic struct skcipher_alg skcipher_algs[] = {
2178c2ecf20Sopenharmony_ci	{
2188c2ecf20Sopenharmony_ci		.base.cra_name		= "ecb(camellia)",
2198c2ecf20Sopenharmony_ci		.base.cra_driver_name	= "ecb-camellia-sparc64",
2208c2ecf20Sopenharmony_ci		.base.cra_priority	= SPARC_CR_OPCODE_PRIORITY,
2218c2ecf20Sopenharmony_ci		.base.cra_blocksize	= CAMELLIA_BLOCK_SIZE,
2228c2ecf20Sopenharmony_ci		.base.cra_ctxsize	= sizeof(struct camellia_sparc64_ctx),
2238c2ecf20Sopenharmony_ci		.base.cra_alignmask	= 7,
2248c2ecf20Sopenharmony_ci		.base.cra_module	= THIS_MODULE,
2258c2ecf20Sopenharmony_ci		.min_keysize		= CAMELLIA_MIN_KEY_SIZE,
2268c2ecf20Sopenharmony_ci		.max_keysize		= CAMELLIA_MAX_KEY_SIZE,
2278c2ecf20Sopenharmony_ci		.setkey			= camellia_set_key_skcipher,
2288c2ecf20Sopenharmony_ci		.encrypt		= ecb_encrypt,
2298c2ecf20Sopenharmony_ci		.decrypt		= ecb_decrypt,
2308c2ecf20Sopenharmony_ci	}, {
2318c2ecf20Sopenharmony_ci		.base.cra_name		= "cbc(camellia)",
2328c2ecf20Sopenharmony_ci		.base.cra_driver_name	= "cbc-camellia-sparc64",
2338c2ecf20Sopenharmony_ci		.base.cra_priority	= SPARC_CR_OPCODE_PRIORITY,
2348c2ecf20Sopenharmony_ci		.base.cra_blocksize	= CAMELLIA_BLOCK_SIZE,
2358c2ecf20Sopenharmony_ci		.base.cra_ctxsize	= sizeof(struct camellia_sparc64_ctx),
2368c2ecf20Sopenharmony_ci		.base.cra_alignmask	= 7,
2378c2ecf20Sopenharmony_ci		.base.cra_module	= THIS_MODULE,
2388c2ecf20Sopenharmony_ci		.min_keysize		= CAMELLIA_MIN_KEY_SIZE,
2398c2ecf20Sopenharmony_ci		.max_keysize		= CAMELLIA_MAX_KEY_SIZE,
2408c2ecf20Sopenharmony_ci		.ivsize			= CAMELLIA_BLOCK_SIZE,
2418c2ecf20Sopenharmony_ci		.setkey			= camellia_set_key_skcipher,
2428c2ecf20Sopenharmony_ci		.encrypt		= cbc_encrypt,
2438c2ecf20Sopenharmony_ci		.decrypt		= cbc_decrypt,
2448c2ecf20Sopenharmony_ci	}
2458c2ecf20Sopenharmony_ci};
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cistatic bool __init sparc64_has_camellia_opcode(void)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	unsigned long cfr;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
2528c2ecf20Sopenharmony_ci		return false;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	__asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
2558c2ecf20Sopenharmony_ci	if (!(cfr & CFR_CAMELLIA))
2568c2ecf20Sopenharmony_ci		return false;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	return true;
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic int __init camellia_sparc64_mod_init(void)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	int err;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	if (!sparc64_has_camellia_opcode()) {
2668c2ecf20Sopenharmony_ci		pr_info("sparc64 camellia opcodes not available.\n");
2678c2ecf20Sopenharmony_ci		return -ENODEV;
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci	pr_info("Using sparc64 camellia opcodes optimized CAMELLIA implementation\n");
2708c2ecf20Sopenharmony_ci	err = crypto_register_alg(&cipher_alg);
2718c2ecf20Sopenharmony_ci	if (err)
2728c2ecf20Sopenharmony_ci		return err;
2738c2ecf20Sopenharmony_ci	err = crypto_register_skciphers(skcipher_algs,
2748c2ecf20Sopenharmony_ci					ARRAY_SIZE(skcipher_algs));
2758c2ecf20Sopenharmony_ci	if (err)
2768c2ecf20Sopenharmony_ci		crypto_unregister_alg(&cipher_alg);
2778c2ecf20Sopenharmony_ci	return err;
2788c2ecf20Sopenharmony_ci}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_cistatic void __exit camellia_sparc64_mod_fini(void)
2818c2ecf20Sopenharmony_ci{
2828c2ecf20Sopenharmony_ci	crypto_unregister_alg(&cipher_alg);
2838c2ecf20Sopenharmony_ci	crypto_unregister_skciphers(skcipher_algs, ARRAY_SIZE(skcipher_algs));
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cimodule_init(camellia_sparc64_mod_init);
2878c2ecf20Sopenharmony_cimodule_exit(camellia_sparc64_mod_fini);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2908c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Camellia Cipher Algorithm, sparc64 camellia opcode accelerated");
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("camellia");
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci#include "crop_devid.c"
295