1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Public Key Encryption
5 * Copyright (c) 2015, Intel Corporation
6 * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
8 #include <crypto/internal/akcipher.h>
9 #include <linux/cryptouser.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/seq_file.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <net/netlink.h>
20 static int __maybe_unused crypto_akcipher_report(
21 struct sk_buff *skb, struct crypto_alg *alg)
23 struct crypto_report_akcipher rakcipher;
25 memset(&rakcipher, 0, sizeof(rakcipher));
27 strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
29 return nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
30 sizeof(rakcipher), &rakcipher);
33 static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
36 static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
38 seq_puts(m, "type : akcipher\n");
41 static void crypto_akcipher_exit_tfm(struct crypto_tfm *tfm)
43 struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
44 struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
49 static int crypto_akcipher_init_tfm(struct crypto_tfm *tfm)
51 struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
52 struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
55 akcipher->base.exit = crypto_akcipher_exit_tfm;
58 return alg->init(akcipher);
63 static void crypto_akcipher_free_instance(struct crypto_instance *inst)
65 struct akcipher_instance *akcipher = akcipher_instance(inst);
67 akcipher->free(akcipher);
70 static int __maybe_unused crypto_akcipher_report_stat(
71 struct sk_buff *skb, struct crypto_alg *alg)
73 struct akcipher_alg *akcipher = __crypto_akcipher_alg(alg);
74 struct crypto_istat_akcipher *istat;
75 struct crypto_stat_akcipher rakcipher;
77 istat = akcipher_get_stat(akcipher);
79 memset(&rakcipher, 0, sizeof(rakcipher));
81 strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
82 rakcipher.stat_encrypt_cnt = atomic64_read(&istat->encrypt_cnt);
83 rakcipher.stat_encrypt_tlen = atomic64_read(&istat->encrypt_tlen);
84 rakcipher.stat_decrypt_cnt = atomic64_read(&istat->decrypt_cnt);
85 rakcipher.stat_decrypt_tlen = atomic64_read(&istat->decrypt_tlen);
86 rakcipher.stat_sign_cnt = atomic64_read(&istat->sign_cnt);
87 rakcipher.stat_verify_cnt = atomic64_read(&istat->verify_cnt);
88 rakcipher.stat_err_cnt = atomic64_read(&istat->err_cnt);
90 return nla_put(skb, CRYPTOCFGA_STAT_AKCIPHER,
91 sizeof(rakcipher), &rakcipher);
94 static const struct crypto_type crypto_akcipher_type = {
95 .extsize = crypto_alg_extsize,
96 .init_tfm = crypto_akcipher_init_tfm,
97 .free = crypto_akcipher_free_instance,
99 .show = crypto_akcipher_show,
101 #if IS_ENABLED(CONFIG_CRYPTO_USER)
102 .report = crypto_akcipher_report,
104 #ifdef CONFIG_CRYPTO_STATS
105 .report_stat = crypto_akcipher_report_stat,
107 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
108 .maskset = CRYPTO_ALG_TYPE_MASK,
109 .type = CRYPTO_ALG_TYPE_AKCIPHER,
110 .tfmsize = offsetof(struct crypto_akcipher, base),
113 int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn,
114 struct crypto_instance *inst,
115 const char *name, u32 type, u32 mask)
117 spawn->base.frontend = &crypto_akcipher_type;
118 return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
120 EXPORT_SYMBOL_GPL(crypto_grab_akcipher);
122 struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
125 return crypto_alloc_tfm(alg_name, &crypto_akcipher_type, type, mask);
127 EXPORT_SYMBOL_GPL(crypto_alloc_akcipher);
129 static void akcipher_prepare_alg(struct akcipher_alg *alg)
131 struct crypto_istat_akcipher *istat = akcipher_get_stat(alg);
132 struct crypto_alg *base = &alg->base;
134 base->cra_type = &crypto_akcipher_type;
135 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
136 base->cra_flags |= CRYPTO_ALG_TYPE_AKCIPHER;
138 if (IS_ENABLED(CONFIG_CRYPTO_STATS))
139 memset(istat, 0, sizeof(*istat));
142 static int akcipher_default_op(struct akcipher_request *req)
147 static int akcipher_default_set_key(struct crypto_akcipher *tfm,
148 const void *key, unsigned int keylen)
153 int crypto_register_akcipher(struct akcipher_alg *alg)
155 struct crypto_alg *base = &alg->base;
158 alg->sign = akcipher_default_op;
160 alg->verify = akcipher_default_op;
162 alg->encrypt = akcipher_default_op;
164 alg->decrypt = akcipher_default_op;
165 if (!alg->set_priv_key)
166 alg->set_priv_key = akcipher_default_set_key;
168 akcipher_prepare_alg(alg);
169 return crypto_register_alg(base);
171 EXPORT_SYMBOL_GPL(crypto_register_akcipher);
173 void crypto_unregister_akcipher(struct akcipher_alg *alg)
175 crypto_unregister_alg(&alg->base);
177 EXPORT_SYMBOL_GPL(crypto_unregister_akcipher);
179 int akcipher_register_instance(struct crypto_template *tmpl,
180 struct akcipher_instance *inst)
182 if (WARN_ON(!inst->free))
184 akcipher_prepare_alg(&inst->alg);
185 return crypto_register_instance(tmpl, akcipher_crypto_instance(inst));
187 EXPORT_SYMBOL_GPL(akcipher_register_instance);
189 MODULE_LICENSE("GPL");
190 MODULE_DESCRIPTION("Generic public key cipher type");