block: constify partition prober array
[linux-block.git] / crypto / akcipher.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
3c339ab8
TS
2/*
3 * Public Key Encryption
4 *
5 * Copyright (c) 2015, Intel Corporation
6 * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
3c339ab8 7 */
035d78a1
HX
8#include <crypto/internal/akcipher.h>
9#include <linux/cryptouser.h>
3c339ab8
TS
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>
3c339ab8 16#include <net/netlink.h>
035d78a1 17
3c339ab8
TS
18#include "internal.h"
19
c0f9e01d
HX
20static int __maybe_unused crypto_akcipher_report(
21 struct sk_buff *skb, struct crypto_alg *alg)
3c339ab8
TS
22{
23 struct crypto_report_akcipher rakcipher;
24
37db69e0 25 memset(&rakcipher, 0, sizeof(rakcipher));
3c339ab8 26
37db69e0 27 strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
3c339ab8 28
37db69e0
EB
29 return nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
30 sizeof(rakcipher), &rakcipher);
3c339ab8 31}
3c339ab8
TS
32
33static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
d8c34b94 34 __maybe_unused;
3c339ab8
TS
35
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
28a4618a
AZ
63static 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
035d78a1
HX
70static 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
3c339ab8
TS
94static const struct crypto_type crypto_akcipher_type = {
95 .extsize = crypto_alg_extsize,
96 .init_tfm = crypto_akcipher_init_tfm,
28a4618a 97 .free = crypto_akcipher_free_instance,
3c339ab8
TS
98#ifdef CONFIG_PROC_FS
99 .show = crypto_akcipher_show,
100#endif
b8969a1b 101#if IS_ENABLED(CONFIG_CRYPTO_USER)
3c339ab8 102 .report = crypto_akcipher_report,
c0f9e01d 103#endif
035d78a1
HX
104#ifdef CONFIG_CRYPTO_STATS
105 .report_stat = crypto_akcipher_report_stat,
106#endif
3c339ab8
TS
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),
111};
112
73bed26f
EB
113int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn,
114 struct crypto_instance *inst,
115 const char *name, u32 type, u32 mask)
28a4618a
AZ
116{
117 spawn->base.frontend = &crypto_akcipher_type;
de95c957 118 return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
28a4618a
AZ
119}
120EXPORT_SYMBOL_GPL(crypto_grab_akcipher);
121
3c339ab8
TS
122struct 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
28a4618a 129static void akcipher_prepare_alg(struct akcipher_alg *alg)
3c339ab8 130{
035d78a1 131 struct crypto_istat_akcipher *istat = akcipher_get_stat(alg);
3c339ab8
TS
132 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;
035d78a1
HX
137
138 if (IS_ENABLED(CONFIG_CRYPTO_STATS))
139 memset(istat, 0, sizeof(*istat));
28a4618a
AZ
140}
141
78a0324f
VC
142static int akcipher_default_op(struct akcipher_request *req)
143{
144 return -ENOSYS;
145}
146
bc155c6c
IK
147static int akcipher_default_set_key(struct crypto_akcipher *tfm,
148 const void *key, unsigned int keylen)
149{
150 return -ENOSYS;
151}
152
28a4618a
AZ
153int crypto_register_akcipher(struct akcipher_alg *alg)
154{
155 struct crypto_alg *base = &alg->base;
156
78a0324f
VC
157 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;
bc155c6c
IK
165 if (!alg->set_priv_key)
166 alg->set_priv_key = akcipher_default_set_key;
78a0324f 167
28a4618a 168 akcipher_prepare_alg(alg);
3c339ab8
TS
169 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
28a4618a
AZ
179int akcipher_register_instance(struct crypto_template *tmpl,
180 struct akcipher_instance *inst)
181{
d4fdc2df
EB
182 if (WARN_ON(!inst->free))
183 return -EINVAL;
28a4618a
AZ
184 akcipher_prepare_alg(&inst->alg);
185 return crypto_register_instance(tmpl, akcipher_crypto_instance(inst));
186}
187EXPORT_SYMBOL_GPL(akcipher_register_instance);
188
3c339ab8 189MODULE_LICENSE("GPL");
338a9de0 190MODULE_DESCRIPTION("Generic public key cipher type");