18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Public Key Encryption
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2015, Intel Corporation
68c2ecf20Sopenharmony_ci * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#include <linux/errno.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/string.h>
148c2ecf20Sopenharmony_ci#include <linux/crypto.h>
158c2ecf20Sopenharmony_ci#include <linux/compiler.h>
168c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
178c2ecf20Sopenharmony_ci#include <linux/cryptouser.h>
188c2ecf20Sopenharmony_ci#include <net/netlink.h>
198c2ecf20Sopenharmony_ci#include <crypto/akcipher.h>
208c2ecf20Sopenharmony_ci#include <crypto/internal/akcipher.h>
218c2ecf20Sopenharmony_ci#include "internal.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#ifdef CONFIG_NET
248c2ecf20Sopenharmony_cistatic int crypto_akcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	struct crypto_report_akcipher rakcipher;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	memset(&rakcipher, 0, sizeof(rakcipher));
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	return nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
338c2ecf20Sopenharmony_ci		       sizeof(rakcipher), &rakcipher);
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci#else
368c2ecf20Sopenharmony_cistatic int crypto_akcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	return -ENOSYS;
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci#endif
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
438c2ecf20Sopenharmony_ci	__maybe_unused;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	seq_puts(m, "type         : akcipher\n");
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic void crypto_akcipher_exit_tfm(struct crypto_tfm *tfm)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
538c2ecf20Sopenharmony_ci	struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	alg->exit(akcipher);
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic int crypto_akcipher_init_tfm(struct crypto_tfm *tfm)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
618c2ecf20Sopenharmony_ci	struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	if (alg->exit)
648c2ecf20Sopenharmony_ci		akcipher->base.exit = crypto_akcipher_exit_tfm;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	if (alg->init)
678c2ecf20Sopenharmony_ci		return alg->init(akcipher);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	return 0;
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic void crypto_akcipher_free_instance(struct crypto_instance *inst)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct akcipher_instance *akcipher = akcipher_instance(inst);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	akcipher->free(akcipher);
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic const struct crypto_type crypto_akcipher_type = {
808c2ecf20Sopenharmony_ci	.extsize = crypto_alg_extsize,
818c2ecf20Sopenharmony_ci	.init_tfm = crypto_akcipher_init_tfm,
828c2ecf20Sopenharmony_ci	.free = crypto_akcipher_free_instance,
838c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
848c2ecf20Sopenharmony_ci	.show = crypto_akcipher_show,
858c2ecf20Sopenharmony_ci#endif
868c2ecf20Sopenharmony_ci	.report = crypto_akcipher_report,
878c2ecf20Sopenharmony_ci	.maskclear = ~CRYPTO_ALG_TYPE_MASK,
888c2ecf20Sopenharmony_ci	.maskset = CRYPTO_ALG_TYPE_MASK,
898c2ecf20Sopenharmony_ci	.type = CRYPTO_ALG_TYPE_AKCIPHER,
908c2ecf20Sopenharmony_ci	.tfmsize = offsetof(struct crypto_akcipher, base),
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ciint crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn,
948c2ecf20Sopenharmony_ci			 struct crypto_instance *inst,
958c2ecf20Sopenharmony_ci			 const char *name, u32 type, u32 mask)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	spawn->base.frontend = &crypto_akcipher_type;
988c2ecf20Sopenharmony_ci	return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_grab_akcipher);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistruct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
1038c2ecf20Sopenharmony_ci					      u32 mask)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	return crypto_alloc_tfm(alg_name, &crypto_akcipher_type, type, mask);
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_alloc_akcipher);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic void akcipher_prepare_alg(struct akcipher_alg *alg)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	struct crypto_alg *base = &alg->base;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	base->cra_type = &crypto_akcipher_type;
1148c2ecf20Sopenharmony_ci	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
1158c2ecf20Sopenharmony_ci	base->cra_flags |= CRYPTO_ALG_TYPE_AKCIPHER;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic int akcipher_default_op(struct akcipher_request *req)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	return -ENOSYS;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic int akcipher_default_set_key(struct crypto_akcipher *tfm,
1248c2ecf20Sopenharmony_ci				     const void *key, unsigned int keylen)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	return -ENOSYS;
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciint crypto_register_akcipher(struct akcipher_alg *alg)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	struct crypto_alg *base = &alg->base;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	if (!alg->sign)
1348c2ecf20Sopenharmony_ci		alg->sign = akcipher_default_op;
1358c2ecf20Sopenharmony_ci	if (!alg->verify)
1368c2ecf20Sopenharmony_ci		alg->verify = akcipher_default_op;
1378c2ecf20Sopenharmony_ci	if (!alg->encrypt)
1388c2ecf20Sopenharmony_ci		alg->encrypt = akcipher_default_op;
1398c2ecf20Sopenharmony_ci	if (!alg->decrypt)
1408c2ecf20Sopenharmony_ci		alg->decrypt = akcipher_default_op;
1418c2ecf20Sopenharmony_ci	if (!alg->set_priv_key)
1428c2ecf20Sopenharmony_ci		alg->set_priv_key = akcipher_default_set_key;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	akcipher_prepare_alg(alg);
1458c2ecf20Sopenharmony_ci	return crypto_register_alg(base);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_register_akcipher);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_civoid crypto_unregister_akcipher(struct akcipher_alg *alg)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	crypto_unregister_alg(&alg->base);
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_unregister_akcipher);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ciint akcipher_register_instance(struct crypto_template *tmpl,
1568c2ecf20Sopenharmony_ci			       struct akcipher_instance *inst)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	if (WARN_ON(!inst->free))
1598c2ecf20Sopenharmony_ci		return -EINVAL;
1608c2ecf20Sopenharmony_ci	akcipher_prepare_alg(&inst->alg);
1618c2ecf20Sopenharmony_ci	return crypto_register_instance(tmpl, akcipher_crypto_instance(inst));
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(akcipher_register_instance);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1668c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Generic public key cipher type");
167