| 1 | |
| 2 | |
| 3 | * Key-agreement Protocol Primitives (KPP)
|
| 4 | *
|
| 5 | * Copyright (c) 2016, Intel Corporation
|
| 6 | * Authors: Salvatore Benedetto <[email protected]>
|
| 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 | .type = "kpp",
|
| 25 | };
|
| 26 |
|
| 27 | return nla_put(skb, CRYPTOCFGA_REPORT_KPP, sizeof(rkpp), &rkpp);
|
| 28 | }
|
| 29 |
|
| 30 | static void __maybe_unused crypto_kpp_show(struct seq_file *m,
|
| 31 | struct crypto_alg *alg)
|
| 32 | {
|
| 33 | seq_puts(m, "type : kpp\n");
|
| 34 | }
|
| 35 |
|
| 36 | static void crypto_kpp_exit_tfm(struct crypto_tfm *tfm)
|
| 37 | {
|
| 38 | struct crypto_kpp *kpp = __crypto_kpp_tfm(tfm);
|
| 39 | struct kpp_alg *alg = crypto_kpp_alg(kpp);
|
| 40 |
|
| 41 | alg->exit(kpp);
|
| 42 | }
|
| 43 |
|
| 44 | static int crypto_kpp_init_tfm(struct crypto_tfm *tfm)
|
| 45 | {
|
| 46 | struct crypto_kpp *kpp = __crypto_kpp_tfm(tfm);
|
| 47 | struct kpp_alg *alg = crypto_kpp_alg(kpp);
|
| 48 |
|
| 49 | if (alg->exit)
|
| 50 | kpp->base.exit = crypto_kpp_exit_tfm;
|
| 51 |
|
| 52 | if (alg->init)
|
| 53 | return alg->init(kpp);
|
| 54 |
|
| 55 | return 0;
|
| 56 | }
|
| 57 |
|
| 58 | static void crypto_kpp_free_instance(struct crypto_instance *inst)
|
| 59 | {
|
| 60 | struct kpp_instance *kpp = kpp_instance(inst);
|
| 61 |
|
| 62 | kpp->free(kpp);
|
| 63 | }
|
| 64 |
|
| 65 | static const struct crypto_type crypto_kpp_type = {
|
| 66 | .extsize = crypto_alg_extsize,
|
| 67 | .init_tfm = crypto_kpp_init_tfm,
|
| 68 | .free = crypto_kpp_free_instance,
|
| 69 | #ifdef CONFIG_PROC_FS
|
| 70 | .show = crypto_kpp_show,
|
| 71 | #endif
|
| 72 | #if IS_ENABLED(CONFIG_CRYPTO_USER)
|
| 73 | .report = crypto_kpp_report,
|
| 74 | #endif
|
| 75 | .maskclear = ~CRYPTO_ALG_TYPE_MASK,
|
| 76 | .maskset = CRYPTO_ALG_TYPE_MASK,
|
| 77 | .type = CRYPTO_ALG_TYPE_KPP,
|
| 78 | .tfmsize = offsetof(struct crypto_kpp, base),
|
| 79 | .algsize = offsetof(struct kpp_alg, base),
|
| 80 | };
|
| 81 |
|
| 82 | struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask)
|
| 83 | {
|
| 84 | return crypto_alloc_tfm(alg_name, &crypto_kpp_type, type, mask);
|
| 85 | }
|
| 86 | EXPORT_SYMBOL_GPL(crypto_alloc_kpp);
|
| 87 |
|
| 88 | int crypto_grab_kpp(struct crypto_kpp_spawn *spawn,
|
| 89 | struct crypto_instance *inst,
|
| 90 | const char *name, u32 type, u32 mask)
|
| 91 | {
|
| 92 | spawn->base.frontend = &crypto_kpp_type;
|
| 93 | return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
|
| 94 | }
|
| 95 | EXPORT_SYMBOL_GPL(crypto_grab_kpp);
|
| 96 |
|
| 97 | int crypto_has_kpp(const char *alg_name, u32 type, u32 mask)
|
| 98 | {
|
| 99 | return crypto_type_has_alg(alg_name, &crypto_kpp_type, type, mask);
|
| 100 | }
|
| 101 | EXPORT_SYMBOL_GPL(crypto_has_kpp);
|
| 102 |
|
| 103 | static void kpp_prepare_alg(struct kpp_alg *alg)
|
| 104 | {
|
| 105 | struct crypto_alg *base = &alg->base;
|
| 106 |
|
| 107 | base->cra_type = &crypto_kpp_type;
|
| 108 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
|
| 109 | base->cra_flags |= CRYPTO_ALG_TYPE_KPP;
|
| 110 | }
|
| 111 |
|
| 112 | int crypto_register_kpp(struct kpp_alg *alg)
|
| 113 | {
|
| 114 | struct crypto_alg *base = &alg->base;
|
| 115 |
|
| 116 | kpp_prepare_alg(alg);
|
| 117 | return crypto_register_alg(base);
|
| 118 | }
|
| 119 | EXPORT_SYMBOL_GPL(crypto_register_kpp);
|
| 120 |
|
| 121 | void crypto_unregister_kpp(struct kpp_alg *alg)
|
| 122 | {
|
| 123 | crypto_unregister_alg(&alg->base);
|
| 124 | }
|
| 125 | EXPORT_SYMBOL_GPL(crypto_unregister_kpp);
|
| 126 |
|
| 127 | int kpp_register_instance(struct crypto_template *tmpl,
|
| 128 | struct kpp_instance *inst)
|
| 129 | {
|
| 130 | if (WARN_ON(!inst->free))
|
| 131 | return -EINVAL;
|
| 132 |
|
| 133 | kpp_prepare_alg(&inst->alg);
|
| 134 |
|
| 135 | return crypto_register_instance(tmpl, kpp_crypto_instance(inst));
|
| 136 | }
|
| 137 | EXPORT_SYMBOL_GPL(kpp_register_instance);
|
| 138 |
|
| 139 | MODULE_LICENSE("GPL");
|
| 140 | MODULE_DESCRIPTION("Key-agreement Protocol Primitives");
|