Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Key-agreement Protocol Primitives (KPP) |
| 4 | * |
| 5 | * Copyright (c) 2016, Intel Corporation |
| 6 | * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com> |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 7 | */ |
Herbert Xu | e2950bf | 2023-02-16 18:35:19 +0800 | [diff] [blame] | 8 | |
| 9 | #include <crypto/internal/kpp.h> |
| 10 | #include <linux/cryptouser.h> |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 11 | #include <linux/errno.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/seq_file.h> |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 15 | #include <linux/string.h> |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 16 | #include <net/netlink.h> |
Herbert Xu | e2950bf | 2023-02-16 18:35:19 +0800 | [diff] [blame] | 17 | |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [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_kpp_report( |
| 21 | struct sk_buff *skb, struct crypto_alg *alg) |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 22 | { |
| 23 | struct crypto_report_kpp rkpp; |
| 24 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 25 | memset(&rkpp, 0, sizeof(rkpp)); |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 26 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 27 | strscpy(rkpp.type, "kpp", sizeof(rkpp.type)); |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 28 | |
Eric Biggers | 37db69e | 2018-11-03 14:56:03 -0700 | [diff] [blame] | 29 | return nla_put(skb, CRYPTOCFGA_REPORT_KPP, sizeof(rkpp), &rkpp); |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 30 | } |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 31 | |
| 32 | static void crypto_kpp_show(struct seq_file *m, struct crypto_alg *alg) |
Gideon Israel Dsouza | d8c34b9 | 2016-12-31 21:26:23 +0530 | [diff] [blame] | 33 | __maybe_unused; |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 34 | |
| 35 | static void crypto_kpp_show(struct seq_file *m, struct crypto_alg *alg) |
| 36 | { |
| 37 | seq_puts(m, "type : kpp\n"); |
| 38 | } |
| 39 | |
| 40 | static void crypto_kpp_exit_tfm(struct crypto_tfm *tfm) |
| 41 | { |
| 42 | struct crypto_kpp *kpp = __crypto_kpp_tfm(tfm); |
| 43 | struct kpp_alg *alg = crypto_kpp_alg(kpp); |
| 44 | |
| 45 | alg->exit(kpp); |
| 46 | } |
| 47 | |
| 48 | static int crypto_kpp_init_tfm(struct crypto_tfm *tfm) |
| 49 | { |
| 50 | struct crypto_kpp *kpp = __crypto_kpp_tfm(tfm); |
| 51 | struct kpp_alg *alg = crypto_kpp_alg(kpp); |
| 52 | |
| 53 | if (alg->exit) |
| 54 | kpp->base.exit = crypto_kpp_exit_tfm; |
| 55 | |
| 56 | if (alg->init) |
| 57 | return alg->init(kpp); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Nicolai Stange | 1038fd7 | 2022-02-21 13:10:47 +0100 | [diff] [blame] | 62 | static void crypto_kpp_free_instance(struct crypto_instance *inst) |
| 63 | { |
| 64 | struct kpp_instance *kpp = kpp_instance(inst); |
| 65 | |
| 66 | kpp->free(kpp); |
| 67 | } |
| 68 | |
Herbert Xu | e2950bf | 2023-02-16 18:35:19 +0800 | [diff] [blame] | 69 | static int __maybe_unused crypto_kpp_report_stat( |
| 70 | struct sk_buff *skb, struct crypto_alg *alg) |
| 71 | { |
| 72 | struct kpp_alg *kpp = __crypto_kpp_alg(alg); |
| 73 | struct crypto_istat_kpp *istat; |
| 74 | struct crypto_stat_kpp rkpp; |
| 75 | |
| 76 | istat = kpp_get_stat(kpp); |
| 77 | |
| 78 | memset(&rkpp, 0, sizeof(rkpp)); |
| 79 | |
| 80 | strscpy(rkpp.type, "kpp", sizeof(rkpp.type)); |
| 81 | |
| 82 | rkpp.stat_setsecret_cnt = atomic64_read(&istat->setsecret_cnt); |
| 83 | rkpp.stat_generate_public_key_cnt = |
| 84 | atomic64_read(&istat->generate_public_key_cnt); |
| 85 | rkpp.stat_compute_shared_secret_cnt = |
| 86 | atomic64_read(&istat->compute_shared_secret_cnt); |
| 87 | rkpp.stat_err_cnt = atomic64_read(&istat->err_cnt); |
| 88 | |
| 89 | return nla_put(skb, CRYPTOCFGA_STAT_KPP, sizeof(rkpp), &rkpp); |
| 90 | } |
| 91 | |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 92 | static const struct crypto_type crypto_kpp_type = { |
| 93 | .extsize = crypto_alg_extsize, |
| 94 | .init_tfm = crypto_kpp_init_tfm, |
Nicolai Stange | 1038fd7 | 2022-02-21 13:10:47 +0100 | [diff] [blame] | 95 | .free = crypto_kpp_free_instance, |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 96 | #ifdef CONFIG_PROC_FS |
| 97 | .show = crypto_kpp_show, |
| 98 | #endif |
Ondrej Mosnacek | b8969a1 | 2023-05-02 10:02:33 +0200 | [diff] [blame] | 99 | #if IS_ENABLED(CONFIG_CRYPTO_USER) |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 100 | .report = crypto_kpp_report, |
Herbert Xu | c0f9e01 | 2023-02-16 18:35:28 +0800 | [diff] [blame] | 101 | #endif |
Herbert Xu | e2950bf | 2023-02-16 18:35:19 +0800 | [diff] [blame] | 102 | #ifdef CONFIG_CRYPTO_STATS |
| 103 | .report_stat = crypto_kpp_report_stat, |
| 104 | #endif |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 105 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
| 106 | .maskset = CRYPTO_ALG_TYPE_MASK, |
| 107 | .type = CRYPTO_ALG_TYPE_KPP, |
| 108 | .tfmsize = offsetof(struct crypto_kpp, base), |
| 109 | }; |
| 110 | |
| 111 | struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask) |
| 112 | { |
| 113 | return crypto_alloc_tfm(alg_name, &crypto_kpp_type, type, mask); |
| 114 | } |
| 115 | EXPORT_SYMBOL_GPL(crypto_alloc_kpp); |
| 116 | |
Nicolai Stange | 46ed526 | 2022-02-21 13:10:48 +0100 | [diff] [blame] | 117 | int crypto_grab_kpp(struct crypto_kpp_spawn *spawn, |
| 118 | struct crypto_instance *inst, |
| 119 | const char *name, u32 type, u32 mask) |
| 120 | { |
| 121 | spawn->base.frontend = &crypto_kpp_type; |
| 122 | return crypto_grab_spawn(&spawn->base, inst, name, type, mask); |
| 123 | } |
| 124 | EXPORT_SYMBOL_GPL(crypto_grab_kpp); |
| 125 | |
Hannes Reinecke | 9e2f284 | 2022-06-27 11:51:58 +0200 | [diff] [blame] | 126 | int crypto_has_kpp(const char *alg_name, u32 type, u32 mask) |
| 127 | { |
| 128 | return crypto_type_has_alg(alg_name, &crypto_kpp_type, type, mask); |
| 129 | } |
| 130 | EXPORT_SYMBOL_GPL(crypto_has_kpp); |
| 131 | |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 132 | static void kpp_prepare_alg(struct kpp_alg *alg) |
| 133 | { |
Herbert Xu | e2950bf | 2023-02-16 18:35:19 +0800 | [diff] [blame] | 134 | struct crypto_istat_kpp *istat = kpp_get_stat(alg); |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 135 | struct crypto_alg *base = &alg->base; |
| 136 | |
| 137 | base->cra_type = &crypto_kpp_type; |
| 138 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
| 139 | base->cra_flags |= CRYPTO_ALG_TYPE_KPP; |
Herbert Xu | e2950bf | 2023-02-16 18:35:19 +0800 | [diff] [blame] | 140 | |
| 141 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) |
| 142 | memset(istat, 0, sizeof(*istat)); |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int crypto_register_kpp(struct kpp_alg *alg) |
| 146 | { |
| 147 | struct crypto_alg *base = &alg->base; |
| 148 | |
| 149 | kpp_prepare_alg(alg); |
| 150 | return crypto_register_alg(base); |
| 151 | } |
| 152 | EXPORT_SYMBOL_GPL(crypto_register_kpp); |
| 153 | |
| 154 | void crypto_unregister_kpp(struct kpp_alg *alg) |
| 155 | { |
| 156 | crypto_unregister_alg(&alg->base); |
| 157 | } |
| 158 | EXPORT_SYMBOL_GPL(crypto_unregister_kpp); |
| 159 | |
Nicolai Stange | 1038fd7 | 2022-02-21 13:10:47 +0100 | [diff] [blame] | 160 | int kpp_register_instance(struct crypto_template *tmpl, |
| 161 | struct kpp_instance *inst) |
| 162 | { |
| 163 | if (WARN_ON(!inst->free)) |
| 164 | return -EINVAL; |
| 165 | |
| 166 | kpp_prepare_alg(&inst->alg); |
| 167 | |
| 168 | return crypto_register_instance(tmpl, kpp_crypto_instance(inst)); |
| 169 | } |
| 170 | EXPORT_SYMBOL_GPL(kpp_register_instance); |
| 171 | |
Salvatore Benedetto | 4e5f2c4 | 2016-06-22 17:49:13 +0100 | [diff] [blame] | 172 | MODULE_LICENSE("GPL"); |
| 173 | MODULE_DESCRIPTION("Key-agreement Protocol Primitives"); |