Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Public Key Encryption |
| 4 | * |
| 5 | * Copyright (c) 2015, Intel Corporation |
| 6 | * Authors: Tadeusz Struk <tadeusz.struk@intel.com> |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 7 | */ |
Herbert Xu | 035d78a | 2023-02-16 18:35:13 +0800 | [diff] [blame] | 8 | #include <crypto/internal/akcipher.h> |
| 9 | #include <linux/cryptouser.h> |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 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> |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 16 | #include <net/netlink.h> |
Herbert Xu | 035d78a | 2023-02-16 18:35:13 +0800 | [diff] [blame] | 17 | |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 18 | #include "internal.h" |
| 19 | |
Herbert Xu | c0f9e01 | 2023-02-16 18:35:28 +0800 | [diff] [blame] | 20 | static int __maybe_unused crypto_akcipher_report( |
| 21 | struct sk_buff *skb, struct crypto_alg *alg) |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 22 | { |
| 23 | struct crypto_report_akcipher rakcipher; |
| 24 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 25 | memset(&rakcipher, 0, sizeof(rakcipher)); |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 26 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 27 | strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type)); |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 28 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 29 | return nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER, |
| 30 | sizeof(rakcipher), &rakcipher); |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 31 | } |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 32 | |
| 33 | static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg) |
Gideon Israel Dsouza | d8c34b9 | 2016-12-31 21:26:23 +0530 | [diff] [blame] | 34 | __maybe_unused; |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 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 | |
Andrzej Zaborowski | 28a4618 | 2015-12-05 17:09:33 +0100 | [diff] [blame] | 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 | |
Herbert Xu | 035d78a | 2023-02-16 18:35:13 +0800 | [diff] [blame] | 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 | |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 94 | static const struct crypto_type crypto_akcipher_type = { |
| 95 | .extsize = crypto_alg_extsize, |
| 96 | .init_tfm = crypto_akcipher_init_tfm, |
Andrzej Zaborowski | 28a4618 | 2015-12-05 17:09:33 +0100 | [diff] [blame] | 97 | .free = crypto_akcipher_free_instance, |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 98 | #ifdef CONFIG_PROC_FS |
| 99 | .show = crypto_akcipher_show, |
| 100 | #endif |
Ondrej Mosnacek | b8969a1 | 2023-05-02 10:02:33 +0200 | [diff] [blame] | 101 | #if IS_ENABLED(CONFIG_CRYPTO_USER) |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 102 | .report = crypto_akcipher_report, |
Herbert Xu | c0f9e01 | 2023-02-16 18:35:28 +0800 | [diff] [blame] | 103 | #endif |
Herbert Xu | 035d78a | 2023-02-16 18:35:13 +0800 | [diff] [blame] | 104 | #ifdef CONFIG_CRYPTO_STATS |
| 105 | .report_stat = crypto_akcipher_report_stat, |
| 106 | #endif |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 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 | |
Eric Biggers | 73bed26 | 2020-01-02 19:58:47 -0800 | [diff] [blame] | 113 | int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn, |
| 114 | struct crypto_instance *inst, |
| 115 | const char *name, u32 type, u32 mask) |
Andrzej Zaborowski | 28a4618 | 2015-12-05 17:09:33 +0100 | [diff] [blame] | 116 | { |
| 117 | spawn->base.frontend = &crypto_akcipher_type; |
Eric Biggers | de95c95 | 2020-01-02 19:58:48 -0800 | [diff] [blame] | 118 | return crypto_grab_spawn(&spawn->base, inst, name, type, mask); |
Andrzej Zaborowski | 28a4618 | 2015-12-05 17:09:33 +0100 | [diff] [blame] | 119 | } |
| 120 | EXPORT_SYMBOL_GPL(crypto_grab_akcipher); |
| 121 | |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 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 | |
Andrzej Zaborowski | 28a4618 | 2015-12-05 17:09:33 +0100 | [diff] [blame] | 129 | static void akcipher_prepare_alg(struct akcipher_alg *alg) |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 130 | { |
Herbert Xu | 035d78a | 2023-02-16 18:35:13 +0800 | [diff] [blame] | 131 | struct crypto_istat_akcipher *istat = akcipher_get_stat(alg); |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 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; |
Herbert Xu | 035d78a | 2023-02-16 18:35:13 +0800 | [diff] [blame] | 137 | |
| 138 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) |
| 139 | memset(istat, 0, sizeof(*istat)); |
Andrzej Zaborowski | 28a4618 | 2015-12-05 17:09:33 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Vitaly Chikunov | 78a0324f | 2019-04-11 18:51:13 +0300 | [diff] [blame] | 142 | static int akcipher_default_op(struct akcipher_request *req) |
| 143 | { |
| 144 | return -ENOSYS; |
| 145 | } |
| 146 | |
Ignat Korchagin | bc155c6c | 2022-08-31 19:37:06 +0100 | [diff] [blame] | 147 | static int akcipher_default_set_key(struct crypto_akcipher *tfm, |
| 148 | const void *key, unsigned int keylen) |
| 149 | { |
| 150 | return -ENOSYS; |
| 151 | } |
| 152 | |
Andrzej Zaborowski | 28a4618 | 2015-12-05 17:09:33 +0100 | [diff] [blame] | 153 | int crypto_register_akcipher(struct akcipher_alg *alg) |
| 154 | { |
| 155 | struct crypto_alg *base = &alg->base; |
| 156 | |
Vitaly Chikunov | 78a0324f | 2019-04-11 18:51:13 +0300 | [diff] [blame] | 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; |
Ignat Korchagin | bc155c6c | 2022-08-31 19:37:06 +0100 | [diff] [blame] | 165 | if (!alg->set_priv_key) |
| 166 | alg->set_priv_key = akcipher_default_set_key; |
Vitaly Chikunov | 78a0324f | 2019-04-11 18:51:13 +0300 | [diff] [blame] | 167 | |
Andrzej Zaborowski | 28a4618 | 2015-12-05 17:09:33 +0100 | [diff] [blame] | 168 | akcipher_prepare_alg(alg); |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 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 | |
Andrzej Zaborowski | 28a4618 | 2015-12-05 17:09:33 +0100 | [diff] [blame] | 179 | int akcipher_register_instance(struct crypto_template *tmpl, |
| 180 | struct akcipher_instance *inst) |
| 181 | { |
Eric Biggers | d4fdc2d | 2020-01-02 20:04:40 -0800 | [diff] [blame] | 182 | if (WARN_ON(!inst->free)) |
| 183 | return -EINVAL; |
Andrzej Zaborowski | 28a4618 | 2015-12-05 17:09:33 +0100 | [diff] [blame] | 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 | |
Tadeusz Struk | 3c339ab | 2015-06-16 10:30:55 -0700 | [diff] [blame] | 189 | MODULE_LICENSE("GPL"); |
Tadeusz Struk | 338a9de | 2015-06-23 10:18:53 -0700 | [diff] [blame] | 190 | MODULE_DESCRIPTION("Generic public key cipher type"); |