Back to home page

LXR

 
 

    


0001 /*
0002  * Key-agreement Protocol Primitives (KPP)
0003  *
0004  * Copyright (c) 2016, Intel Corporation
0005  * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
0006  *
0007  * This program is free software; you can redistribute it and/or modify it
0008  * under the terms of the GNU General Public License as published by the Free
0009  * Software Foundation; either version 2 of the License, or (at your option)
0010  * any later version.
0011  *
0012  */
0013 #include <linux/errno.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/seq_file.h>
0017 #include <linux/slab.h>
0018 #include <linux/string.h>
0019 #include <linux/crypto.h>
0020 #include <crypto/algapi.h>
0021 #include <linux/cryptouser.h>
0022 #include <net/netlink.h>
0023 #include <crypto/kpp.h>
0024 #include <crypto/internal/kpp.h>
0025 #include "internal.h"
0026 
0027 #ifdef CONFIG_NET
0028 static int crypto_kpp_report(struct sk_buff *skb, struct crypto_alg *alg)
0029 {
0030     struct crypto_report_kpp rkpp;
0031 
0032     strncpy(rkpp.type, "kpp", sizeof(rkpp.type));
0033 
0034     if (nla_put(skb, CRYPTOCFGA_REPORT_KPP,
0035             sizeof(struct crypto_report_kpp), &rkpp))
0036         goto nla_put_failure;
0037     return 0;
0038 
0039 nla_put_failure:
0040     return -EMSGSIZE;
0041 }
0042 #else
0043 static int crypto_kpp_report(struct sk_buff *skb, struct crypto_alg *alg)
0044 {
0045     return -ENOSYS;
0046 }
0047 #endif
0048 
0049 static void crypto_kpp_show(struct seq_file *m, struct crypto_alg *alg)
0050     __attribute__ ((unused));
0051 
0052 static void crypto_kpp_show(struct seq_file *m, struct crypto_alg *alg)
0053 {
0054     seq_puts(m, "type         : kpp\n");
0055 }
0056 
0057 static void crypto_kpp_exit_tfm(struct crypto_tfm *tfm)
0058 {
0059     struct crypto_kpp *kpp = __crypto_kpp_tfm(tfm);
0060     struct kpp_alg *alg = crypto_kpp_alg(kpp);
0061 
0062     alg->exit(kpp);
0063 }
0064 
0065 static int crypto_kpp_init_tfm(struct crypto_tfm *tfm)
0066 {
0067     struct crypto_kpp *kpp = __crypto_kpp_tfm(tfm);
0068     struct kpp_alg *alg = crypto_kpp_alg(kpp);
0069 
0070     if (alg->exit)
0071         kpp->base.exit = crypto_kpp_exit_tfm;
0072 
0073     if (alg->init)
0074         return alg->init(kpp);
0075 
0076     return 0;
0077 }
0078 
0079 static const struct crypto_type crypto_kpp_type = {
0080     .extsize = crypto_alg_extsize,
0081     .init_tfm = crypto_kpp_init_tfm,
0082 #ifdef CONFIG_PROC_FS
0083     .show = crypto_kpp_show,
0084 #endif
0085     .report = crypto_kpp_report,
0086     .maskclear = ~CRYPTO_ALG_TYPE_MASK,
0087     .maskset = CRYPTO_ALG_TYPE_MASK,
0088     .type = CRYPTO_ALG_TYPE_KPP,
0089     .tfmsize = offsetof(struct crypto_kpp, base),
0090 };
0091 
0092 struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask)
0093 {
0094     return crypto_alloc_tfm(alg_name, &crypto_kpp_type, type, mask);
0095 }
0096 EXPORT_SYMBOL_GPL(crypto_alloc_kpp);
0097 
0098 static void kpp_prepare_alg(struct kpp_alg *alg)
0099 {
0100     struct crypto_alg *base = &alg->base;
0101 
0102     base->cra_type = &crypto_kpp_type;
0103     base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
0104     base->cra_flags |= CRYPTO_ALG_TYPE_KPP;
0105 }
0106 
0107 int crypto_register_kpp(struct kpp_alg *alg)
0108 {
0109     struct crypto_alg *base = &alg->base;
0110 
0111     kpp_prepare_alg(alg);
0112     return crypto_register_alg(base);
0113 }
0114 EXPORT_SYMBOL_GPL(crypto_register_kpp);
0115 
0116 void crypto_unregister_kpp(struct kpp_alg *alg)
0117 {
0118     crypto_unregister_alg(&alg->base);
0119 }
0120 EXPORT_SYMBOL_GPL(crypto_unregister_kpp);
0121 
0122 MODULE_LICENSE("GPL");
0123 MODULE_DESCRIPTION("Key-agreement Protocol Primitives");