blob: 7960ceb528c3664fb74968f57da9ba754281f324 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Tadeusz Struk3c339ab2015-06-16 10:30:55 -07002/*
3 * Public Key Encryption
4 *
5 * Copyright (c) 2015, Intel Corporation
6 * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
Tadeusz Struk3c339ab2015-06-16 10:30:55 -07007 */
Herbert Xu035d78a2023-02-16 18:35:13 +08008#include <crypto/internal/akcipher.h>
9#include <linux/cryptouser.h>
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070010#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>
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070016#include <net/netlink.h>
Herbert Xu035d78a2023-02-16 18:35:13 +080017
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070018#include "internal.h"
19
Herbert Xuc0f9e012023-02-16 18:35:28 +080020static int __maybe_unused crypto_akcipher_report(
21 struct sk_buff *skb, struct crypto_alg *alg)
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070022{
23 struct crypto_report_akcipher rakcipher;
24
Eric Biggers37db69e2018-11-03 14:56:03 -070025 memset(&rakcipher, 0, sizeof(rakcipher));
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070026
Eric Biggers37db69e2018-11-03 14:56:03 -070027 strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070028
Eric Biggers37db69e2018-11-03 14:56:03 -070029 return nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
30 sizeof(rakcipher), &rakcipher);
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070031}
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070032
33static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
Gideon Israel Dsouzad8c34b92016-12-31 21:26:23 +053034 __maybe_unused;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070035
36static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
37{
38 seq_puts(m, "type : akcipher\n");
39}
40
41static void crypto_akcipher_exit_tfm(struct crypto_tfm *tfm)
42{
43 struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
44 struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
45
46 alg->exit(akcipher);
47}
48
49static int crypto_akcipher_init_tfm(struct crypto_tfm *tfm)
50{
51 struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
52 struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
53
54 if (alg->exit)
55 akcipher->base.exit = crypto_akcipher_exit_tfm;
56
57 if (alg->init)
58 return alg->init(akcipher);
59
60 return 0;
61}
62
Andrzej Zaborowski28a46182015-12-05 17:09:33 +010063static void crypto_akcipher_free_instance(struct crypto_instance *inst)
64{
65 struct akcipher_instance *akcipher = akcipher_instance(inst);
66
67 akcipher->free(akcipher);
68}
69
Herbert Xu035d78a2023-02-16 18:35:13 +080070static int __maybe_unused crypto_akcipher_report_stat(
71 struct sk_buff *skb, struct crypto_alg *alg)
72{
73 struct akcipher_alg *akcipher = __crypto_akcipher_alg(alg);
74 struct crypto_istat_akcipher *istat;
75 struct crypto_stat_akcipher rakcipher;
76
77 istat = akcipher_get_stat(akcipher);
78
79 memset(&rakcipher, 0, sizeof(rakcipher));
80
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);
89
90 return nla_put(skb, CRYPTOCFGA_STAT_AKCIPHER,
91 sizeof(rakcipher), &rakcipher);
92}
93
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070094static const struct crypto_type crypto_akcipher_type = {
95 .extsize = crypto_alg_extsize,
96 .init_tfm = crypto_akcipher_init_tfm,
Andrzej Zaborowski28a46182015-12-05 17:09:33 +010097 .free = crypto_akcipher_free_instance,
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070098#ifdef CONFIG_PROC_FS
99 .show = crypto_akcipher_show,
100#endif
Ondrej Mosnacekb8969a12023-05-02 10:02:33 +0200101#if IS_ENABLED(CONFIG_CRYPTO_USER)
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700102 .report = crypto_akcipher_report,
Herbert Xuc0f9e012023-02-16 18:35:28 +0800103#endif
Herbert Xu035d78a2023-02-16 18:35:13 +0800104#ifdef CONFIG_CRYPTO_STATS
105 .report_stat = crypto_akcipher_report_stat,
106#endif
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700107 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
108 .maskset = CRYPTO_ALG_TYPE_MASK,
109 .type = CRYPTO_ALG_TYPE_AKCIPHER,
110 .tfmsize = offsetof(struct crypto_akcipher, base),
111};
112
Eric Biggers73bed262020-01-02 19:58:47 -0800113int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn,
114 struct crypto_instance *inst,
115 const char *name, u32 type, u32 mask)
Andrzej Zaborowski28a46182015-12-05 17:09:33 +0100116{
117 spawn->base.frontend = &crypto_akcipher_type;
Eric Biggersde95c952020-01-02 19:58:48 -0800118 return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
Andrzej Zaborowski28a46182015-12-05 17:09:33 +0100119}
120EXPORT_SYMBOL_GPL(crypto_grab_akcipher);
121
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700122struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
123 u32 mask)
124{
125 return crypto_alloc_tfm(alg_name, &crypto_akcipher_type, type, mask);
126}
127EXPORT_SYMBOL_GPL(crypto_alloc_akcipher);
128
Andrzej Zaborowski28a46182015-12-05 17:09:33 +0100129static void akcipher_prepare_alg(struct akcipher_alg *alg)
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700130{
Herbert Xu035d78a2023-02-16 18:35:13 +0800131 struct crypto_istat_akcipher *istat = akcipher_get_stat(alg);
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700132 struct crypto_alg *base = &alg->base;
133
134 base->cra_type = &crypto_akcipher_type;
135 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
136 base->cra_flags |= CRYPTO_ALG_TYPE_AKCIPHER;
Herbert Xu035d78a2023-02-16 18:35:13 +0800137
138 if (IS_ENABLED(CONFIG_CRYPTO_STATS))
139 memset(istat, 0, sizeof(*istat));
Andrzej Zaborowski28a46182015-12-05 17:09:33 +0100140}
141
Vitaly Chikunov78a0324f2019-04-11 18:51:13 +0300142static int akcipher_default_op(struct akcipher_request *req)
143{
144 return -ENOSYS;
145}
146
Ignat Korchaginbc155c6c2022-08-31 19:37:06 +0100147static int akcipher_default_set_key(struct crypto_akcipher *tfm,
148 const void *key, unsigned int keylen)
149{
150 return -ENOSYS;
151}
152
Andrzej Zaborowski28a46182015-12-05 17:09:33 +0100153int crypto_register_akcipher(struct akcipher_alg *alg)
154{
155 struct crypto_alg *base = &alg->base;
156
Vitaly Chikunov78a0324f2019-04-11 18:51:13 +0300157 if (!alg->sign)
158 alg->sign = akcipher_default_op;
159 if (!alg->verify)
160 alg->verify = akcipher_default_op;
161 if (!alg->encrypt)
162 alg->encrypt = akcipher_default_op;
163 if (!alg->decrypt)
164 alg->decrypt = akcipher_default_op;
Ignat Korchaginbc155c6c2022-08-31 19:37:06 +0100165 if (!alg->set_priv_key)
166 alg->set_priv_key = akcipher_default_set_key;
Vitaly Chikunov78a0324f2019-04-11 18:51:13 +0300167
Andrzej Zaborowski28a46182015-12-05 17:09:33 +0100168 akcipher_prepare_alg(alg);
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700169 return crypto_register_alg(base);
170}
171EXPORT_SYMBOL_GPL(crypto_register_akcipher);
172
173void crypto_unregister_akcipher(struct akcipher_alg *alg)
174{
175 crypto_unregister_alg(&alg->base);
176}
177EXPORT_SYMBOL_GPL(crypto_unregister_akcipher);
178
Andrzej Zaborowski28a46182015-12-05 17:09:33 +0100179int akcipher_register_instance(struct crypto_template *tmpl,
180 struct akcipher_instance *inst)
181{
Eric Biggersd4fdc2d2020-01-02 20:04:40 -0800182 if (WARN_ON(!inst->free))
183 return -EINVAL;
Andrzej Zaborowski28a46182015-12-05 17:09:33 +0100184 akcipher_prepare_alg(&inst->alg);
185 return crypto_register_instance(tmpl, akcipher_crypto_instance(inst));
186}
187EXPORT_SYMBOL_GPL(akcipher_register_instance);
188
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700189MODULE_LICENSE("GPL");
Tadeusz Struk338a9de2015-06-23 10:18:53 -0700190MODULE_DESCRIPTION("Generic public key cipher type");