Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Public Key Encryption
0004  *
0005  * Copyright (c) 2015, Intel Corporation
0006  * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
0007  */
0008 #include <linux/errno.h>
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/seq_file.h>
0012 #include <linux/slab.h>
0013 #include <linux/string.h>
0014 #include <linux/crypto.h>
0015 #include <linux/compiler.h>
0016 #include <crypto/algapi.h>
0017 #include <linux/cryptouser.h>
0018 #include <net/netlink.h>
0019 #include <crypto/akcipher.h>
0020 #include <crypto/internal/akcipher.h>
0021 #include "internal.h"
0022 
0023 #ifdef CONFIG_NET
0024 static int crypto_akcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
0025 {
0026     struct crypto_report_akcipher rakcipher;
0027 
0028     memset(&rakcipher, 0, sizeof(rakcipher));
0029 
0030     strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
0031 
0032     return nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
0033                sizeof(rakcipher), &rakcipher);
0034 }
0035 #else
0036 static int crypto_akcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
0037 {
0038     return -ENOSYS;
0039 }
0040 #endif
0041 
0042 static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
0043     __maybe_unused;
0044 
0045 static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
0046 {
0047     seq_puts(m, "type         : akcipher\n");
0048 }
0049 
0050 static void crypto_akcipher_exit_tfm(struct crypto_tfm *tfm)
0051 {
0052     struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
0053     struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
0054 
0055     alg->exit(akcipher);
0056 }
0057 
0058 static int crypto_akcipher_init_tfm(struct crypto_tfm *tfm)
0059 {
0060     struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
0061     struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
0062 
0063     if (alg->exit)
0064         akcipher->base.exit = crypto_akcipher_exit_tfm;
0065 
0066     if (alg->init)
0067         return alg->init(akcipher);
0068 
0069     return 0;
0070 }
0071 
0072 static void crypto_akcipher_free_instance(struct crypto_instance *inst)
0073 {
0074     struct akcipher_instance *akcipher = akcipher_instance(inst);
0075 
0076     akcipher->free(akcipher);
0077 }
0078 
0079 static const struct crypto_type crypto_akcipher_type = {
0080     .extsize = crypto_alg_extsize,
0081     .init_tfm = crypto_akcipher_init_tfm,
0082     .free = crypto_akcipher_free_instance,
0083 #ifdef CONFIG_PROC_FS
0084     .show = crypto_akcipher_show,
0085 #endif
0086     .report = crypto_akcipher_report,
0087     .maskclear = ~CRYPTO_ALG_TYPE_MASK,
0088     .maskset = CRYPTO_ALG_TYPE_MASK,
0089     .type = CRYPTO_ALG_TYPE_AKCIPHER,
0090     .tfmsize = offsetof(struct crypto_akcipher, base),
0091 };
0092 
0093 int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn,
0094              struct crypto_instance *inst,
0095              const char *name, u32 type, u32 mask)
0096 {
0097     spawn->base.frontend = &crypto_akcipher_type;
0098     return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
0099 }
0100 EXPORT_SYMBOL_GPL(crypto_grab_akcipher);
0101 
0102 struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
0103                           u32 mask)
0104 {
0105     return crypto_alloc_tfm(alg_name, &crypto_akcipher_type, type, mask);
0106 }
0107 EXPORT_SYMBOL_GPL(crypto_alloc_akcipher);
0108 
0109 static void akcipher_prepare_alg(struct akcipher_alg *alg)
0110 {
0111     struct crypto_alg *base = &alg->base;
0112 
0113     base->cra_type = &crypto_akcipher_type;
0114     base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
0115     base->cra_flags |= CRYPTO_ALG_TYPE_AKCIPHER;
0116 }
0117 
0118 static int akcipher_default_op(struct akcipher_request *req)
0119 {
0120     return -ENOSYS;
0121 }
0122 
0123 int crypto_register_akcipher(struct akcipher_alg *alg)
0124 {
0125     struct crypto_alg *base = &alg->base;
0126 
0127     if (!alg->sign)
0128         alg->sign = akcipher_default_op;
0129     if (!alg->verify)
0130         alg->verify = akcipher_default_op;
0131     if (!alg->encrypt)
0132         alg->encrypt = akcipher_default_op;
0133     if (!alg->decrypt)
0134         alg->decrypt = akcipher_default_op;
0135 
0136     akcipher_prepare_alg(alg);
0137     return crypto_register_alg(base);
0138 }
0139 EXPORT_SYMBOL_GPL(crypto_register_akcipher);
0140 
0141 void crypto_unregister_akcipher(struct akcipher_alg *alg)
0142 {
0143     crypto_unregister_alg(&alg->base);
0144 }
0145 EXPORT_SYMBOL_GPL(crypto_unregister_akcipher);
0146 
0147 int akcipher_register_instance(struct crypto_template *tmpl,
0148                    struct akcipher_instance *inst)
0149 {
0150     if (WARN_ON(!inst->free))
0151         return -EINVAL;
0152     akcipher_prepare_alg(&inst->alg);
0153     return crypto_register_instance(tmpl, akcipher_crypto_instance(inst));
0154 }
0155 EXPORT_SYMBOL_GPL(akcipher_register_instance);
0156 
0157 MODULE_LICENSE("GPL");
0158 MODULE_DESCRIPTION("Generic public key cipher type");