1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Public Key Encryption 4 * 5 * Copyright (c) 2015, Intel Corporation 6 * Authors: Tadeusz Struk <tadeusz.struk@intel.com> 7 */ 8 #include <crypto/internal/akcipher.h> 9 #include <linux/cryptouser.h> 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> 16 #include <net/netlink.h> 17 18 #include "internal.h" 19 20 static int __maybe_unused crypto_akcipher_report( 21 struct sk_buff *skb, struct crypto_alg *alg) 22 { 23 struct crypto_report_akcipher rakcipher; 24 25 memset(&rakcipher, 0, sizeof(rakcipher)); 26 27 strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type)); 28 29 return nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER, 30 sizeof(rakcipher), &rakcipher); 31 } 32 33 static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg) 34 __maybe_unused; 35 36 static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg) 37 { 38 seq_puts(m, "type : akcipher\n"); 39 } 40 41 static 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 49 static 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 63 static 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 70 static 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 94 static const struct crypto_type crypto_akcipher_type = { 95 .extsize = crypto_alg_extsize, 96 .init_tfm = crypto_akcipher_init_tfm, 97 .free = crypto_akcipher_free_instance, 98 #ifdef CONFIG_PROC_FS 99 .show = crypto_akcipher_show, 100 #endif 101 #if IS_ENABLED(CONFIG_CRYPTO_USER) 102 .report = crypto_akcipher_report, 103 #endif 104 #ifdef CONFIG_CRYPTO_STATS 105 .report_stat = crypto_akcipher_report_stat, 106 #endif 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 113 int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn, 114 struct crypto_instance *inst, 115 const char *name, u32 type, u32 mask) 116 { 117 spawn->base.frontend = &crypto_akcipher_type; 118 return crypto_grab_spawn(&spawn->base, inst, name, type, mask); 119 } 120 EXPORT_SYMBOL_GPL(crypto_grab_akcipher); 121 122 struct 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 } 127 EXPORT_SYMBOL_GPL(crypto_alloc_akcipher); 128 129 static void akcipher_prepare_alg(struct akcipher_alg *alg) 130 { 131 struct crypto_istat_akcipher *istat = akcipher_get_stat(alg); 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; 137 138 if (IS_ENABLED(CONFIG_CRYPTO_STATS)) 139 memset(istat, 0, sizeof(*istat)); 140 } 141 142 static int akcipher_default_op(struct akcipher_request *req) 143 { 144 return -ENOSYS; 145 } 146 147 static int akcipher_default_set_key(struct crypto_akcipher *tfm, 148 const void *key, unsigned int keylen) 149 { 150 return -ENOSYS; 151 } 152 153 int crypto_register_akcipher(struct akcipher_alg *alg) 154 { 155 struct crypto_alg *base = &alg->base; 156 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; 165 if (!alg->set_priv_key) 166 alg->set_priv_key = akcipher_default_set_key; 167 168 akcipher_prepare_alg(alg); 169 return crypto_register_alg(base); 170 } 171 EXPORT_SYMBOL_GPL(crypto_register_akcipher); 172 173 void crypto_unregister_akcipher(struct akcipher_alg *alg) 174 { 175 crypto_unregister_alg(&alg->base); 176 } 177 EXPORT_SYMBOL_GPL(crypto_unregister_akcipher); 178 179 int akcipher_register_instance(struct crypto_template *tmpl, 180 struct akcipher_instance *inst) 181 { 182 if (WARN_ON(!inst->free)) 183 return -EINVAL; 184 akcipher_prepare_alg(&inst->alg); 185 return crypto_register_instance(tmpl, akcipher_crypto_instance(inst)); 186 } 187 EXPORT_SYMBOL_GPL(akcipher_register_instance); 188 189 MODULE_LICENSE("GPL"); 190 MODULE_DESCRIPTION("Generic public key cipher type"); 191