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 #define CRYPTO_ALG_TYPE_SIG_MASK 0x0000000e 19 20 static void crypto_sig_exit_tfm(struct crypto_tfm *tfm) 21 { 22 struct crypto_sig *sig = __crypto_sig_tfm(tfm); 23 struct sig_alg *alg = crypto_sig_alg(sig); 24 25 alg->exit(sig); 26 } 27 28 static int crypto_sig_init_tfm(struct crypto_tfm *tfm) 29 { 30 struct crypto_sig *sig = __crypto_sig_tfm(tfm); 31 struct sig_alg *alg = crypto_sig_alg(sig); 32 33 if (alg->exit) 34 sig->base.exit = crypto_sig_exit_tfm; 35 36 if (alg->init) 37 return alg->init(sig); 38 39 return 0; 40 } 41 42 static void crypto_sig_free_instance(struct crypto_instance *inst) 43 { 44 struct sig_instance *sig = sig_instance(inst); 45 46 sig->free(sig); 47 } 48 49 static void __maybe_unused crypto_sig_show(struct seq_file *m, 50 struct crypto_alg *alg) 51 { 52 seq_puts(m, "type : sig\n"); 53 } 54 55 static int __maybe_unused crypto_sig_report(struct sk_buff *skb, 56 struct crypto_alg *alg) 57 { 58 struct crypto_report_sig rsig = {}; 59 60 strscpy(rsig.type, "sig", sizeof(rsig.type)); 61 62 return nla_put(skb, CRYPTOCFGA_REPORT_SIG, sizeof(rsig), &rsig); 63 } 64 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), 79 }; 80 81 struct crypto_sig *crypto_alloc_sig(const char *alg_name, u32 type, u32 mask) 82 { 83 return crypto_alloc_tfm(alg_name, &crypto_sig_type, type, mask); 84 } 85 EXPORT_SYMBOL_GPL(crypto_alloc_sig); 86 87 static int sig_default_sign(struct crypto_sig *tfm, 88 const void *src, unsigned int slen, 89 void *dst, unsigned int dlen) 90 { 91 return -ENOSYS; 92 } 93 94 static int sig_default_verify(struct crypto_sig *tfm, 95 const void *src, unsigned int slen, 96 const void *dst, unsigned int dlen) 97 { 98 return -ENOSYS; 99 } 100 101 static int sig_default_set_key(struct crypto_sig *tfm, 102 const void *key, unsigned int keylen) 103 { 104 return -ENOSYS; 105 } 106 107 static int sig_prepare_alg(struct sig_alg *alg) 108 { 109 struct crypto_alg *base = &alg->base; 110 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; 125 126 base->cra_type = &crypto_sig_type; 127 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; 128 base->cra_flags |= CRYPTO_ALG_TYPE_SIG; 129 130 return 0; 131 } 132 133 int crypto_register_sig(struct sig_alg *alg) 134 { 135 struct crypto_alg *base = &alg->base; 136 int err; 137 138 err = sig_prepare_alg(alg); 139 if (err) 140 return err; 141 142 return crypto_register_alg(base); 143 } 144 EXPORT_SYMBOL_GPL(crypto_register_sig); 145 146 void crypto_unregister_sig(struct sig_alg *alg) 147 { 148 crypto_unregister_alg(&alg->base); 149 } 150 EXPORT_SYMBOL_GPL(crypto_unregister_sig); 151 152 int sig_register_instance(struct crypto_template *tmpl, 153 struct sig_instance *inst) 154 { 155 int err; 156 157 if (WARN_ON(!inst->free)) 158 return -EINVAL; 159 160 err = sig_prepare_alg(&inst->alg); 161 if (err) 162 return err; 163 164 return crypto_register_instance(tmpl, sig_crypto_instance(inst)); 165 } 166 EXPORT_SYMBOL_GPL(sig_register_instance); 167 168 int crypto_grab_sig(struct crypto_sig_spawn *spawn, 169 struct crypto_instance *inst, 170 const char *name, u32 type, u32 mask) 171 { 172 spawn->base.frontend = &crypto_sig_type; 173 return crypto_grab_spawn(&spawn->base, inst, name, type, mask); 174 } 175 EXPORT_SYMBOL_GPL(crypto_grab_sig); 176 177 MODULE_LICENSE("GPL"); 178 MODULE_DESCRIPTION("Public Key Signature Algorithms"); 179