| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Key-agreement Protocol Primitives (KPP) |
| 4 | * |
| 5 | * Copyright (c) 2016, Intel Corporation |
| 6 | * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com> |
| 7 | */ |
| 8 | |
| 9 | #include <crypto/internal/kpp.h> |
| 10 | #include <linux/cryptouser.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/seq_file.h> |
| 15 | #include <linux/string.h> |
| 16 | #include <net/netlink.h> |
| 17 | |
| 18 | #include "internal.h" |
| 19 | |
| 20 | static int __maybe_unused crypto_kpp_report( |
| 21 | struct sk_buff *skb, struct crypto_alg *alg) |
| 22 | { |
| 23 | struct crypto_report_kpp rkpp; |
| 24 | |
| 25 | memset(s: &rkpp, c: 0, n: sizeof(rkpp)); |
| 26 | |
| 27 | strscpy(rkpp.type, "kpp" , sizeof(rkpp.type)); |
| 28 | |
| 29 | return nla_put(skb, attrtype: CRYPTOCFGA_REPORT_KPP, attrlen: sizeof(rkpp), data: &rkpp); |
| 30 | } |
| 31 | |
| 32 | static void crypto_kpp_show(struct seq_file *m, struct crypto_alg *alg) |
| 33 | __maybe_unused; |
| 34 | |
| 35 | static void crypto_kpp_show(struct seq_file *m, struct crypto_alg *alg) |
| 36 | { |
| 37 | seq_puts(m, s: "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(tfm: 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(tfm: 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 | |
| 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 | |
| 69 | static const struct crypto_type crypto_kpp_type = { |
| 70 | .extsize = crypto_alg_extsize, |
| 71 | .init_tfm = crypto_kpp_init_tfm, |
| 72 | .free = crypto_kpp_free_instance, |
| 73 | #ifdef CONFIG_PROC_FS |
| 74 | .show = crypto_kpp_show, |
| 75 | #endif |
| 76 | #if IS_ENABLED(CONFIG_CRYPTO_USER) |
| 77 | .report = crypto_kpp_report, |
| 78 | #endif |
| 79 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
| 80 | .maskset = CRYPTO_ALG_TYPE_MASK, |
| 81 | .type = CRYPTO_ALG_TYPE_KPP, |
| 82 | .tfmsize = offsetof(struct crypto_kpp, base), |
| 83 | .algsize = offsetof(struct kpp_alg, base), |
| 84 | }; |
| 85 | |
| 86 | struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask) |
| 87 | { |
| 88 | return crypto_alloc_tfm(alg_name, frontend: &crypto_kpp_type, type, mask); |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(crypto_alloc_kpp); |
| 91 | |
| 92 | int crypto_grab_kpp(struct crypto_kpp_spawn *spawn, |
| 93 | struct crypto_instance *inst, |
| 94 | const char *name, u32 type, u32 mask) |
| 95 | { |
| 96 | spawn->base.frontend = &crypto_kpp_type; |
| 97 | return crypto_grab_spawn(spawn: &spawn->base, inst, name, type, mask); |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(crypto_grab_kpp); |
| 100 | |
| 101 | int crypto_has_kpp(const char *alg_name, u32 type, u32 mask) |
| 102 | { |
| 103 | return crypto_type_has_alg(name: alg_name, frontend: &crypto_kpp_type, type, mask); |
| 104 | } |
| 105 | EXPORT_SYMBOL_GPL(crypto_has_kpp); |
| 106 | |
| 107 | static void kpp_prepare_alg(struct kpp_alg *alg) |
| 108 | { |
| 109 | struct crypto_alg *base = &alg->base; |
| 110 | |
| 111 | base->cra_type = &crypto_kpp_type; |
| 112 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
| 113 | base->cra_flags |= CRYPTO_ALG_TYPE_KPP; |
| 114 | } |
| 115 | |
| 116 | int crypto_register_kpp(struct kpp_alg *alg) |
| 117 | { |
| 118 | struct crypto_alg *base = &alg->base; |
| 119 | |
| 120 | kpp_prepare_alg(alg); |
| 121 | return crypto_register_alg(alg: base); |
| 122 | } |
| 123 | EXPORT_SYMBOL_GPL(crypto_register_kpp); |
| 124 | |
| 125 | void crypto_unregister_kpp(struct kpp_alg *alg) |
| 126 | { |
| 127 | crypto_unregister_alg(alg: &alg->base); |
| 128 | } |
| 129 | EXPORT_SYMBOL_GPL(crypto_unregister_kpp); |
| 130 | |
| 131 | int kpp_register_instance(struct crypto_template *tmpl, |
| 132 | struct kpp_instance *inst) |
| 133 | { |
| 134 | if (WARN_ON(!inst->free)) |
| 135 | return -EINVAL; |
| 136 | |
| 137 | kpp_prepare_alg(alg: &inst->alg); |
| 138 | |
| 139 | return crypto_register_instance(tmpl, inst: kpp_crypto_instance(inst)); |
| 140 | } |
| 141 | EXPORT_SYMBOL_GPL(kpp_register_instance); |
| 142 | |
| 143 | MODULE_LICENSE("GPL" ); |
| 144 | MODULE_DESCRIPTION("Key-agreement Protocol Primitives" ); |
| 145 | |