| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | /* |
| 3 | * Public Key Signature Algorithm |
| 4 | * |
| 5 | * Copyright (c) 2023 Herbert Xu <herbert@gondor.apana.org.au> |
| 6 | */ |
| 7 | |
| 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> |
| 15 | |
| 16 | #include "internal.h" |
| 17 | |
| 18 | static void crypto_sig_exit_tfm(struct crypto_tfm *tfm) |
| 19 | { |
| 20 | struct crypto_sig *sig = __crypto_sig_tfm(tfm); |
| 21 | struct sig_alg *alg = crypto_sig_alg(tfm: sig); |
| 22 | |
| 23 | alg->exit(sig); |
| 24 | } |
| 25 | |
| 26 | static int crypto_sig_init_tfm(struct crypto_tfm *tfm) |
| 27 | { |
| 28 | struct crypto_sig *sig = __crypto_sig_tfm(tfm); |
| 29 | struct sig_alg *alg = crypto_sig_alg(tfm: sig); |
| 30 | |
| 31 | if (alg->exit) |
| 32 | sig->base.exit = crypto_sig_exit_tfm; |
| 33 | |
| 34 | if (alg->init) |
| 35 | return alg->init(sig); |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static void crypto_sig_free_instance(struct crypto_instance *inst) |
| 41 | { |
| 42 | struct sig_instance *sig = sig_instance(inst); |
| 43 | |
| 44 | sig->free(sig); |
| 45 | } |
| 46 | |
| 47 | static void __maybe_unused crypto_sig_show(struct seq_file *m, |
| 48 | struct crypto_alg *alg) |
| 49 | { |
| 50 | seq_puts(m, s: "type : sig\n" ); |
| 51 | } |
| 52 | |
| 53 | static int __maybe_unused crypto_sig_report(struct sk_buff *skb, |
| 54 | struct crypto_alg *alg) |
| 55 | { |
| 56 | struct crypto_report_sig rsig = {}; |
| 57 | |
| 58 | strscpy(rsig.type, "sig" , sizeof(rsig.type)); |
| 59 | |
| 60 | return nla_put(skb, attrtype: CRYPTOCFGA_REPORT_SIG, attrlen: sizeof(rsig), data: &rsig); |
| 61 | } |
| 62 | |
| 63 | static const struct crypto_type crypto_sig_type = { |
| 64 | .extsize = crypto_alg_extsize, |
| 65 | .init_tfm = crypto_sig_init_tfm, |
| 66 | .free = crypto_sig_free_instance, |
| 67 | #ifdef CONFIG_PROC_FS |
| 68 | .show = crypto_sig_show, |
| 69 | #endif |
| 70 | #if IS_ENABLED(CONFIG_CRYPTO_USER) |
| 71 | .report = crypto_sig_report, |
| 72 | #endif |
| 73 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
| 74 | .maskset = CRYPTO_ALG_TYPE_MASK, |
| 75 | .type = CRYPTO_ALG_TYPE_SIG, |
| 76 | .tfmsize = offsetof(struct crypto_sig, base), |
| 77 | .algsize = offsetof(struct sig_alg, base), |
| 78 | }; |
| 79 | |
| 80 | struct crypto_sig *crypto_alloc_sig(const char *alg_name, u32 type, u32 mask) |
| 81 | { |
| 82 | return crypto_alloc_tfm(alg_name, frontend: &crypto_sig_type, type, mask); |
| 83 | } |
| 84 | EXPORT_SYMBOL_GPL(crypto_alloc_sig); |
| 85 | |
| 86 | static int sig_default_sign(struct crypto_sig *tfm, |
| 87 | const void *src, unsigned int slen, |
| 88 | void *dst, unsigned int dlen) |
| 89 | { |
| 90 | return -ENOSYS; |
| 91 | } |
| 92 | |
| 93 | static int sig_default_verify(struct crypto_sig *tfm, |
| 94 | const void *src, unsigned int slen, |
| 95 | const void *dst, unsigned int dlen) |
| 96 | { |
| 97 | return -ENOSYS; |
| 98 | } |
| 99 | |
| 100 | static int sig_default_set_key(struct crypto_sig *tfm, |
| 101 | const void *key, unsigned int keylen) |
| 102 | { |
| 103 | return -ENOSYS; |
| 104 | } |
| 105 | |
| 106 | static unsigned int sig_default_size(struct crypto_sig *tfm) |
| 107 | { |
| 108 | return DIV_ROUND_UP_POW2(crypto_sig_keysize(tfm), BITS_PER_BYTE); |
| 109 | } |
| 110 | |
| 111 | static int sig_prepare_alg(struct sig_alg *alg) |
| 112 | { |
| 113 | struct crypto_alg *base = &alg->base; |
| 114 | |
| 115 | if (!alg->sign) |
| 116 | alg->sign = sig_default_sign; |
| 117 | if (!alg->verify) |
| 118 | alg->verify = sig_default_verify; |
| 119 | if (!alg->set_priv_key) |
| 120 | alg->set_priv_key = sig_default_set_key; |
| 121 | if (!alg->set_pub_key) |
| 122 | return -EINVAL; |
| 123 | if (!alg->key_size) |
| 124 | return -EINVAL; |
| 125 | if (!alg->max_size) |
| 126 | alg->max_size = sig_default_size; |
| 127 | if (!alg->digest_size) |
| 128 | alg->digest_size = sig_default_size; |
| 129 | |
| 130 | base->cra_type = &crypto_sig_type; |
| 131 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
| 132 | base->cra_flags |= CRYPTO_ALG_TYPE_SIG; |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | int crypto_register_sig(struct sig_alg *alg) |
| 138 | { |
| 139 | struct crypto_alg *base = &alg->base; |
| 140 | int err; |
| 141 | |
| 142 | err = sig_prepare_alg(alg); |
| 143 | if (err) |
| 144 | return err; |
| 145 | |
| 146 | return crypto_register_alg(alg: base); |
| 147 | } |
| 148 | EXPORT_SYMBOL_GPL(crypto_register_sig); |
| 149 | |
| 150 | void crypto_unregister_sig(struct sig_alg *alg) |
| 151 | { |
| 152 | crypto_unregister_alg(alg: &alg->base); |
| 153 | } |
| 154 | EXPORT_SYMBOL_GPL(crypto_unregister_sig); |
| 155 | |
| 156 | int sig_register_instance(struct crypto_template *tmpl, |
| 157 | struct sig_instance *inst) |
| 158 | { |
| 159 | int err; |
| 160 | |
| 161 | if (WARN_ON(!inst->free)) |
| 162 | return -EINVAL; |
| 163 | |
| 164 | err = sig_prepare_alg(alg: &inst->alg); |
| 165 | if (err) |
| 166 | return err; |
| 167 | |
| 168 | return crypto_register_instance(tmpl, inst: sig_crypto_instance(inst)); |
| 169 | } |
| 170 | EXPORT_SYMBOL_GPL(sig_register_instance); |
| 171 | |
| 172 | int crypto_grab_sig(struct crypto_sig_spawn *spawn, |
| 173 | struct crypto_instance *inst, |
| 174 | const char *name, u32 type, u32 mask) |
| 175 | { |
| 176 | spawn->base.frontend = &crypto_sig_type; |
| 177 | return crypto_grab_spawn(spawn: &spawn->base, inst, name, type, mask); |
| 178 | } |
| 179 | EXPORT_SYMBOL_GPL(crypto_grab_sig); |
| 180 | |
| 181 | MODULE_LICENSE("GPL" ); |
| 182 | MODULE_DESCRIPTION("Public Key Signature Algorithms" ); |
| 183 | |