Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Create default crypto algorithm instances.
0004  *
0005  * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
0006  */
0007 
0008 #include <crypto/internal/aead.h>
0009 #include <linux/completion.h>
0010 #include <linux/ctype.h>
0011 #include <linux/err.h>
0012 #include <linux/init.h>
0013 #include <linux/kthread.h>
0014 #include <linux/module.h>
0015 #include <linux/notifier.h>
0016 #include <linux/rtnetlink.h>
0017 #include <linux/sched/signal.h>
0018 #include <linux/slab.h>
0019 #include <linux/string.h>
0020 
0021 #include "internal.h"
0022 
0023 struct cryptomgr_param {
0024     struct rtattr *tb[CRYPTO_MAX_ATTRS + 2];
0025 
0026     struct {
0027         struct rtattr attr;
0028         struct crypto_attr_type data;
0029     } type;
0030 
0031     struct {
0032         struct rtattr attr;
0033         struct crypto_attr_alg data;
0034     } attrs[CRYPTO_MAX_ATTRS];
0035 
0036     char template[CRYPTO_MAX_ALG_NAME];
0037 
0038     struct crypto_larval *larval;
0039 
0040     u32 otype;
0041     u32 omask;
0042 };
0043 
0044 struct crypto_test_param {
0045     char driver[CRYPTO_MAX_ALG_NAME];
0046     char alg[CRYPTO_MAX_ALG_NAME];
0047     u32 type;
0048 };
0049 
0050 static int cryptomgr_probe(void *data)
0051 {
0052     struct cryptomgr_param *param = data;
0053     struct crypto_template *tmpl;
0054     int err;
0055 
0056     tmpl = crypto_lookup_template(param->template);
0057     if (!tmpl)
0058         goto out;
0059 
0060     do {
0061         err = tmpl->create(tmpl, param->tb);
0062     } while (err == -EAGAIN && !signal_pending(current));
0063 
0064     crypto_tmpl_put(tmpl);
0065 
0066 out:
0067     complete_all(&param->larval->completion);
0068     crypto_alg_put(&param->larval->alg);
0069     kfree(param);
0070     module_put_and_kthread_exit(0);
0071 }
0072 
0073 static int cryptomgr_schedule_probe(struct crypto_larval *larval)
0074 {
0075     struct task_struct *thread;
0076     struct cryptomgr_param *param;
0077     const char *name = larval->alg.cra_name;
0078     const char *p;
0079     unsigned int len;
0080     int i;
0081 
0082     if (!try_module_get(THIS_MODULE))
0083         goto err;
0084 
0085     param = kzalloc(sizeof(*param), GFP_KERNEL);
0086     if (!param)
0087         goto err_put_module;
0088 
0089     for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
0090         ;
0091 
0092     len = p - name;
0093     if (!len || *p != '(')
0094         goto err_free_param;
0095 
0096     memcpy(param->template, name, len);
0097 
0098     i = 0;
0099     for (;;) {
0100         name = ++p;
0101 
0102         for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
0103             ;
0104 
0105         if (*p == '(') {
0106             int recursion = 0;
0107 
0108             for (;;) {
0109                 if (!*++p)
0110                     goto err_free_param;
0111                 if (*p == '(')
0112                     recursion++;
0113                 else if (*p == ')' && !recursion--)
0114                     break;
0115             }
0116 
0117             p++;
0118         }
0119 
0120         len = p - name;
0121         if (!len)
0122             goto err_free_param;
0123 
0124         param->attrs[i].attr.rta_len = sizeof(param->attrs[i]);
0125         param->attrs[i].attr.rta_type = CRYPTOA_ALG;
0126         memcpy(param->attrs[i].data.name, name, len);
0127 
0128         param->tb[i + 1] = &param->attrs[i].attr;
0129         i++;
0130 
0131         if (i >= CRYPTO_MAX_ATTRS)
0132             goto err_free_param;
0133 
0134         if (*p == ')')
0135             break;
0136 
0137         if (*p != ',')
0138             goto err_free_param;
0139     }
0140 
0141     if (!i)
0142         goto err_free_param;
0143 
0144     param->tb[i + 1] = NULL;
0145 
0146     param->type.attr.rta_len = sizeof(param->type);
0147     param->type.attr.rta_type = CRYPTOA_TYPE;
0148     param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
0149     param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
0150     param->tb[0] = &param->type.attr;
0151 
0152     param->otype = larval->alg.cra_flags;
0153     param->omask = larval->mask;
0154 
0155     crypto_alg_get(&larval->alg);
0156     param->larval = larval;
0157 
0158     thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
0159     if (IS_ERR(thread))
0160         goto err_put_larval;
0161 
0162     return NOTIFY_STOP;
0163 
0164 err_put_larval:
0165     crypto_alg_put(&larval->alg);
0166 err_free_param:
0167     kfree(param);
0168 err_put_module:
0169     module_put(THIS_MODULE);
0170 err:
0171     return NOTIFY_OK;
0172 }
0173 
0174 static int cryptomgr_test(void *data)
0175 {
0176     struct crypto_test_param *param = data;
0177     u32 type = param->type;
0178     int err = 0;
0179 
0180 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
0181     goto skiptest;
0182 #endif
0183 
0184     if (type & CRYPTO_ALG_TESTED)
0185         goto skiptest;
0186 
0187     err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
0188 
0189 skiptest:
0190     crypto_alg_tested(param->driver, err);
0191 
0192     kfree(param);
0193     module_put_and_kthread_exit(0);
0194 }
0195 
0196 static int cryptomgr_schedule_test(struct crypto_alg *alg)
0197 {
0198     struct task_struct *thread;
0199     struct crypto_test_param *param;
0200     u32 type;
0201 
0202     if (!try_module_get(THIS_MODULE))
0203         goto err;
0204 
0205     param = kzalloc(sizeof(*param), GFP_KERNEL);
0206     if (!param)
0207         goto err_put_module;
0208 
0209     memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
0210     memcpy(param->alg, alg->cra_name, sizeof(param->alg));
0211     type = alg->cra_flags;
0212 
0213     /* Do not test internal algorithms. */
0214     if (type & CRYPTO_ALG_INTERNAL)
0215         type |= CRYPTO_ALG_TESTED;
0216 
0217     param->type = type;
0218 
0219     thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
0220     if (IS_ERR(thread))
0221         goto err_free_param;
0222 
0223     return NOTIFY_STOP;
0224 
0225 err_free_param:
0226     kfree(param);
0227 err_put_module:
0228     module_put(THIS_MODULE);
0229 err:
0230     return NOTIFY_OK;
0231 }
0232 
0233 static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
0234                 void *data)
0235 {
0236     switch (msg) {
0237     case CRYPTO_MSG_ALG_REQUEST:
0238         return cryptomgr_schedule_probe(data);
0239     case CRYPTO_MSG_ALG_REGISTER:
0240         return cryptomgr_schedule_test(data);
0241     case CRYPTO_MSG_ALG_LOADED:
0242         break;
0243     }
0244 
0245     return NOTIFY_DONE;
0246 }
0247 
0248 static struct notifier_block cryptomgr_notifier = {
0249     .notifier_call = cryptomgr_notify,
0250 };
0251 
0252 static int __init cryptomgr_init(void)
0253 {
0254     return crypto_register_notifier(&cryptomgr_notifier);
0255 }
0256 
0257 static void __exit cryptomgr_exit(void)
0258 {
0259     int err = crypto_unregister_notifier(&cryptomgr_notifier);
0260     BUG_ON(err);
0261 }
0262 
0263 /*
0264  * This is arch_initcall() so that the crypto self-tests are run on algorithms
0265  * registered early by subsys_initcall().  subsys_initcall() is needed for
0266  * generic implementations so that they're available for comparison tests when
0267  * other implementations are registered later by module_init().
0268  */
0269 arch_initcall(cryptomgr_init);
0270 module_exit(cryptomgr_exit);
0271 
0272 MODULE_LICENSE("GPL");
0273 MODULE_DESCRIPTION("Crypto Algorithm Manager");