[CRYPTO] api: Add cryptomgr
[linux-2.6/x86.git] / crypto / cryptomgr.c
blobe0ebe1b44b994a19574116823153e1a08ce6a4ad
1 /*
2 * Create default crypto algorithm instances.
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
13 #include <linux/crypto.h>
14 #include <linux/ctype.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/notifier.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/string.h>
21 #include <linux/workqueue.h>
23 #include "internal.h"
25 struct cryptomgr_param {
26 struct work_struct work;
28 struct {
29 struct rtattr attr;
30 struct crypto_attr_alg data;
31 } alg;
33 struct {
34 char name[CRYPTO_MAX_ALG_NAME];
35 } larval;
37 char template[CRYPTO_MAX_ALG_NAME];
40 static void cryptomgr_probe(void *data)
42 struct cryptomgr_param *param = data;
43 struct crypto_template *tmpl;
44 struct crypto_instance *inst;
46 tmpl = crypto_lookup_template(param->template);
47 if (!tmpl)
48 goto err;
50 inst = tmpl->alloc(&param->alg, sizeof(param->alg));
51 if (IS_ERR(inst))
52 goto err;
53 else if ((err = crypto_register_instance(tmpl, inst))) {
54 tmpl->free(inst);
55 goto err;
58 crypto_tmpl_put(tmpl);
60 out:
61 kfree(param);
62 return;
64 err:
65 crypto_larval_error(param->larval.name);
66 goto out;
69 static int cryptomgr_schedule_probe(struct crypto_larval *larval)
71 struct cryptomgr_param *param;
72 const char *name = larval->alg.cra_name;
73 const char *p;
74 unsigned int len;
76 param = kmalloc(sizeof(*param), GFP_KERNEL);
77 if (!param)
78 goto err;
80 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
83 len = p - name;
84 if (!len || *p != '(')
85 goto err_free_param;
87 memcpy(param->template, name, len);
88 param->template[len] = 0;
90 name = p + 1;
91 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
94 len = p - name;
95 if (!len || *p != ')' || p[1])
96 goto err_free_param;
98 param->alg.attr.rta_len = sizeof(param->alg);
99 param->alg.attr.rta_type = CRYPTOA_ALG;
100 memcpy(param->alg.data.name, name, len);
101 param->alg.data.name[len] = 0;
103 memcpy(param->larval.name, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
105 INIT_WORK(&param->work, cryptomgr_probe, param);
106 schedule_work(&param->work);
108 return NOTIFY_STOP;
110 err_free_param:
111 kfree(param);
112 err:
113 return NOTIFY_OK;
116 static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
117 void *data)
119 switch (msg) {
120 case CRYPTO_MSG_ALG_REQUEST:
121 return cryptomgr_schedule_probe(data);
124 return NOTIFY_DONE;
127 static struct notifier_block cryptomgr_notifier = {
128 .notifier_call = cryptomgr_notify,
131 static int __init cryptomgr_init(void)
133 return crypto_register_notifier(&cryptomgr_notifier);
136 static void __exit cryptomgr_exit(void)
138 int err = crypto_unregister_notifier(&cryptomgr_notifier);
139 BUG_ON(err);
142 module_init(cryptomgr_init);
143 module_exit(cryptomgr_exit);
145 MODULE_LICENSE("GPL");
146 MODULE_DESCRIPTION("Crypto Algorithm Manager");