drm/ast: Only warn about unsupported TX chips on Gen4 and later
[drm/drm-misc.git] / crypto / sig.c
blob5e1f1f739da2ec227ca0839e95f86c6258ae574b
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Public Key Signature Algorithm
5 * Copyright (c) 2023 Herbert Xu <herbert@gondor.apana.org.au>
6 */
8 #include <crypto/internal/sig.h>
9 #include <linux/cryptouser.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/seq_file.h>
13 #include <linux/string.h>
14 #include <net/netlink.h>
16 #include "internal.h"
18 #define CRYPTO_ALG_TYPE_SIG_MASK 0x0000000e
20 static void crypto_sig_exit_tfm(struct crypto_tfm *tfm)
22 struct crypto_sig *sig = __crypto_sig_tfm(tfm);
23 struct sig_alg *alg = crypto_sig_alg(sig);
25 alg->exit(sig);
28 static int crypto_sig_init_tfm(struct crypto_tfm *tfm)
30 struct crypto_sig *sig = __crypto_sig_tfm(tfm);
31 struct sig_alg *alg = crypto_sig_alg(sig);
33 if (alg->exit)
34 sig->base.exit = crypto_sig_exit_tfm;
36 if (alg->init)
37 return alg->init(sig);
39 return 0;
42 static void crypto_sig_free_instance(struct crypto_instance *inst)
44 struct sig_instance *sig = sig_instance(inst);
46 sig->free(sig);
49 static void __maybe_unused crypto_sig_show(struct seq_file *m,
50 struct crypto_alg *alg)
52 seq_puts(m, "type : sig\n");
55 static int __maybe_unused crypto_sig_report(struct sk_buff *skb,
56 struct crypto_alg *alg)
58 struct crypto_report_sig rsig = {};
60 strscpy(rsig.type, "sig", sizeof(rsig.type));
62 return nla_put(skb, CRYPTOCFGA_REPORT_SIG, sizeof(rsig), &rsig);
65 static const struct crypto_type crypto_sig_type = {
66 .extsize = crypto_alg_extsize,
67 .init_tfm = crypto_sig_init_tfm,
68 .free = crypto_sig_free_instance,
69 #ifdef CONFIG_PROC_FS
70 .show = crypto_sig_show,
71 #endif
72 #if IS_ENABLED(CONFIG_CRYPTO_USER)
73 .report = crypto_sig_report,
74 #endif
75 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
76 .maskset = CRYPTO_ALG_TYPE_SIG_MASK,
77 .type = CRYPTO_ALG_TYPE_SIG,
78 .tfmsize = offsetof(struct crypto_sig, base),
81 struct crypto_sig *crypto_alloc_sig(const char *alg_name, u32 type, u32 mask)
83 return crypto_alloc_tfm(alg_name, &crypto_sig_type, type, mask);
85 EXPORT_SYMBOL_GPL(crypto_alloc_sig);
87 static int sig_default_sign(struct crypto_sig *tfm,
88 const void *src, unsigned int slen,
89 void *dst, unsigned int dlen)
91 return -ENOSYS;
94 static int sig_default_verify(struct crypto_sig *tfm,
95 const void *src, unsigned int slen,
96 const void *dst, unsigned int dlen)
98 return -ENOSYS;
101 static int sig_default_set_key(struct crypto_sig *tfm,
102 const void *key, unsigned int keylen)
104 return -ENOSYS;
107 static int sig_prepare_alg(struct sig_alg *alg)
109 struct crypto_alg *base = &alg->base;
111 if (!alg->sign)
112 alg->sign = sig_default_sign;
113 if (!alg->verify)
114 alg->verify = sig_default_verify;
115 if (!alg->set_priv_key)
116 alg->set_priv_key = sig_default_set_key;
117 if (!alg->set_pub_key)
118 return -EINVAL;
119 if (!alg->key_size)
120 return -EINVAL;
121 if (!alg->max_size)
122 alg->max_size = alg->key_size;
123 if (!alg->digest_size)
124 alg->digest_size = alg->key_size;
126 base->cra_type = &crypto_sig_type;
127 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
128 base->cra_flags |= CRYPTO_ALG_TYPE_SIG;
130 return 0;
133 int crypto_register_sig(struct sig_alg *alg)
135 struct crypto_alg *base = &alg->base;
136 int err;
138 err = sig_prepare_alg(alg);
139 if (err)
140 return err;
142 return crypto_register_alg(base);
144 EXPORT_SYMBOL_GPL(crypto_register_sig);
146 void crypto_unregister_sig(struct sig_alg *alg)
148 crypto_unregister_alg(&alg->base);
150 EXPORT_SYMBOL_GPL(crypto_unregister_sig);
152 int sig_register_instance(struct crypto_template *tmpl,
153 struct sig_instance *inst)
155 int err;
157 if (WARN_ON(!inst->free))
158 return -EINVAL;
160 err = sig_prepare_alg(&inst->alg);
161 if (err)
162 return err;
164 return crypto_register_instance(tmpl, sig_crypto_instance(inst));
166 EXPORT_SYMBOL_GPL(sig_register_instance);
168 int crypto_grab_sig(struct crypto_sig_spawn *spawn,
169 struct crypto_instance *inst,
170 const char *name, u32 type, u32 mask)
172 spawn->base.frontend = &crypto_sig_type;
173 return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
175 EXPORT_SYMBOL_GPL(crypto_grab_sig);
177 MODULE_LICENSE("GPL");
178 MODULE_DESCRIPTION("Public Key Signature Algorithms");