Back to home page

LXR

 
 

    


0001 /*
0002  * Public Key Encryption
0003  *
0004  * Copyright (c) 2015, Intel Corporation
0005  * Authors: Tadeusz Struk <tadeusz.struk@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/akcipher.h>
0024 #include <crypto/internal/akcipher.h>
0025 #include "internal.h"
0026 
0027 #ifdef CONFIG_NET
0028 static int crypto_akcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
0029 {
0030     struct crypto_report_akcipher rakcipher;
0031 
0032     strncpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
0033 
0034     if (nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
0035             sizeof(struct crypto_report_akcipher), &rakcipher))
0036         goto nla_put_failure;
0037     return 0;
0038 
0039 nla_put_failure:
0040     return -EMSGSIZE;
0041 }
0042 #else
0043 static int crypto_akcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
0044 {
0045     return -ENOSYS;
0046 }
0047 #endif
0048 
0049 static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
0050     __attribute__ ((unused));
0051 
0052 static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
0053 {
0054     seq_puts(m, "type         : akcipher\n");
0055 }
0056 
0057 static void crypto_akcipher_exit_tfm(struct crypto_tfm *tfm)
0058 {
0059     struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
0060     struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
0061 
0062     alg->exit(akcipher);
0063 }
0064 
0065 static int crypto_akcipher_init_tfm(struct crypto_tfm *tfm)
0066 {
0067     struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
0068     struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
0069 
0070     if (alg->exit)
0071         akcipher->base.exit = crypto_akcipher_exit_tfm;
0072 
0073     if (alg->init)
0074         return alg->init(akcipher);
0075 
0076     return 0;
0077 }
0078 
0079 static void crypto_akcipher_free_instance(struct crypto_instance *inst)
0080 {
0081     struct akcipher_instance *akcipher = akcipher_instance(inst);
0082 
0083     akcipher->free(akcipher);
0084 }
0085 
0086 static const struct crypto_type crypto_akcipher_type = {
0087     .extsize = crypto_alg_extsize,
0088     .init_tfm = crypto_akcipher_init_tfm,
0089     .free = crypto_akcipher_free_instance,
0090 #ifdef CONFIG_PROC_FS
0091     .show = crypto_akcipher_show,
0092 #endif
0093     .report = crypto_akcipher_report,
0094     .maskclear = ~CRYPTO_ALG_TYPE_MASK,
0095     .maskset = CRYPTO_ALG_TYPE_MASK,
0096     .type = CRYPTO_ALG_TYPE_AKCIPHER,
0097     .tfmsize = offsetof(struct crypto_akcipher, base),
0098 };
0099 
0100 int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn, const char *name,
0101              u32 type, u32 mask)
0102 {
0103     spawn->base.frontend = &crypto_akcipher_type;
0104     return crypto_grab_spawn(&spawn->base, name, type, mask);
0105 }
0106 EXPORT_SYMBOL_GPL(crypto_grab_akcipher);
0107 
0108 struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
0109                           u32 mask)
0110 {
0111     return crypto_alloc_tfm(alg_name, &crypto_akcipher_type, type, mask);
0112 }
0113 EXPORT_SYMBOL_GPL(crypto_alloc_akcipher);
0114 
0115 static void akcipher_prepare_alg(struct akcipher_alg *alg)
0116 {
0117     struct crypto_alg *base = &alg->base;
0118 
0119     base->cra_type = &crypto_akcipher_type;
0120     base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
0121     base->cra_flags |= CRYPTO_ALG_TYPE_AKCIPHER;
0122 }
0123 
0124 int crypto_register_akcipher(struct akcipher_alg *alg)
0125 {
0126     struct crypto_alg *base = &alg->base;
0127 
0128     akcipher_prepare_alg(alg);
0129     return crypto_register_alg(base);
0130 }
0131 EXPORT_SYMBOL_GPL(crypto_register_akcipher);
0132 
0133 void crypto_unregister_akcipher(struct akcipher_alg *alg)
0134 {
0135     crypto_unregister_alg(&alg->base);
0136 }
0137 EXPORT_SYMBOL_GPL(crypto_unregister_akcipher);
0138 
0139 int akcipher_register_instance(struct crypto_template *tmpl,
0140                    struct akcipher_instance *inst)
0141 {
0142     akcipher_prepare_alg(&inst->alg);
0143     return crypto_register_instance(tmpl, akcipher_crypto_instance(inst));
0144 }
0145 EXPORT_SYMBOL_GPL(akcipher_register_instance);
0146 
0147 MODULE_LICENSE("GPL");
0148 MODULE_DESCRIPTION("Generic public key cipher type");