Back to home page

LXR

 
 

    


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