[CRYPTO] api: Allow algorithm lookup by type
[linux-2.6-block.git] / crypto / cryptomgr.c
CommitLineData
2b8c19db
HX
1/*
2 * Create default crypto algorithm instances.
3 *
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
5 *
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.
10 *
11 */
12
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>
22
23#include "internal.h"
24
25struct cryptomgr_param {
26 struct work_struct work;
27
28 struct {
29 struct rtattr attr;
30 struct crypto_attr_alg data;
31 } alg;
32
33 struct {
492e2b63
HX
34 u32 type;
35 u32 mask;
2b8c19db
HX
36 char name[CRYPTO_MAX_ALG_NAME];
37 } larval;
38
39 char template[CRYPTO_MAX_ALG_NAME];
40};
41
42static void cryptomgr_probe(void *data)
43{
44 struct cryptomgr_param *param = data;
45 struct crypto_template *tmpl;
46 struct crypto_instance *inst;
47
48 tmpl = crypto_lookup_template(param->template);
49 if (!tmpl)
50 goto err;
51
52 inst = tmpl->alloc(&param->alg, sizeof(param->alg));
53 if (IS_ERR(inst))
54 goto err;
55 else if ((err = crypto_register_instance(tmpl, inst))) {
56 tmpl->free(inst);
57 goto err;
58 }
59
60 crypto_tmpl_put(tmpl);
61
62out:
63 kfree(param);
64 return;
65
66err:
492e2b63
HX
67 crypto_larval_error(param->larval.name, param->larval.type,
68 param->larval.mask);
2b8c19db
HX
69 goto out;
70}
71
72static int cryptomgr_schedule_probe(struct crypto_larval *larval)
73{
74 struct cryptomgr_param *param;
75 const char *name = larval->alg.cra_name;
76 const char *p;
77 unsigned int len;
78
79 param = kmalloc(sizeof(*param), GFP_KERNEL);
80 if (!param)
81 goto err;
82
83 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
84 ;
85
86 len = p - name;
87 if (!len || *p != '(')
88 goto err_free_param;
89
90 memcpy(param->template, name, len);
91 param->template[len] = 0;
92
93 name = p + 1;
94 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
95 ;
96
97 len = p - name;
98 if (!len || *p != ')' || p[1])
99 goto err_free_param;
100
101 param->alg.attr.rta_len = sizeof(param->alg);
102 param->alg.attr.rta_type = CRYPTOA_ALG;
103 memcpy(param->alg.data.name, name, len);
104 param->alg.data.name[len] = 0;
105
106 memcpy(param->larval.name, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
492e2b63
HX
107 param->larval.type = larval->alg.cra_flags;
108 param->larval.mask = larval->mask;
2b8c19db
HX
109
110 INIT_WORK(&param->work, cryptomgr_probe, param);
111 schedule_work(&param->work);
112
113 return NOTIFY_STOP;
114
115err_free_param:
116 kfree(param);
117err:
118 return NOTIFY_OK;
119}
120
121static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
122 void *data)
123{
124 switch (msg) {
125 case CRYPTO_MSG_ALG_REQUEST:
126 return cryptomgr_schedule_probe(data);
127 }
128
129 return NOTIFY_DONE;
130}
131
132static struct notifier_block cryptomgr_notifier = {
133 .notifier_call = cryptomgr_notify,
134};
135
136static int __init cryptomgr_init(void)
137{
138 return crypto_register_notifier(&cryptomgr_notifier);
139}
140
141static void __exit cryptomgr_exit(void)
142{
143 int err = crypto_unregister_notifier(&cryptomgr_notifier);
144 BUG_ON(err);
145}
146
147module_init(cryptomgr_init);
148module_exit(cryptomgr_exit);
149
150MODULE_LICENSE("GPL");
151MODULE_DESCRIPTION("Crypto Algorithm Manager");