blob: e5fb7cca5107d742ccd999bc87eaaf024a204f07 [file] [log] [blame]
Herbert Xu2b8c19d2006-09-21 11:31:44 +10001/*
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>
Herbert Xucf02f5d2007-03-29 17:32:59 +100017#include <linux/kthread.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100018#include <linux/module.h>
19#include <linux/notifier.h>
20#include <linux/rtnetlink.h>
Herbert Xu6bfd4802006-09-21 11:39:29 +100021#include <linux/sched.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100022#include <linux/string.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100023
24#include "internal.h"
25
26struct cryptomgr_param {
Herbert Xuebc610e2007-01-01 18:37:02 +110027 struct rtattr *tb[CRYPTOA_MAX];
28
29 struct {
30 struct rtattr attr;
31 struct crypto_attr_type data;
32 } type;
33
Herbert Xu2b8c19d2006-09-21 11:31:44 +100034 struct {
35 struct rtattr attr;
36 struct crypto_attr_alg data;
37 } alg;
38
39 struct {
40 char name[CRYPTO_MAX_ALG_NAME];
41 } larval;
42
43 char template[CRYPTO_MAX_ALG_NAME];
44};
45
Herbert Xucf02f5d2007-03-29 17:32:59 +100046static int cryptomgr_probe(void *data)
Herbert Xu2b8c19d2006-09-21 11:31:44 +100047{
Herbert Xucf02f5d2007-03-29 17:32:59 +100048 struct cryptomgr_param *param = data;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100049 struct crypto_template *tmpl;
50 struct crypto_instance *inst;
Herbert Xu6bfd4802006-09-21 11:39:29 +100051 int err;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100052
53 tmpl = crypto_lookup_template(param->template);
54 if (!tmpl)
55 goto err;
56
Herbert Xu6bfd4802006-09-21 11:39:29 +100057 do {
Herbert Xuebc610e2007-01-01 18:37:02 +110058 inst = tmpl->alloc(param->tb);
Herbert Xu6bfd4802006-09-21 11:39:29 +100059 if (IS_ERR(inst))
60 err = PTR_ERR(inst);
61 else if ((err = crypto_register_instance(tmpl, inst)))
62 tmpl->free(inst);
63 } while (err == -EAGAIN && !signal_pending(current));
Herbert Xu2b8c19d2006-09-21 11:31:44 +100064
65 crypto_tmpl_put(tmpl);
66
Herbert Xu6bfd4802006-09-21 11:39:29 +100067 if (err)
68 goto err;
69
Herbert Xu2b8c19d2006-09-21 11:31:44 +100070out:
71 kfree(param);
Herbert Xucf02f5d2007-03-29 17:32:59 +100072 module_put_and_exit(0);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100073
74err:
Herbert Xuebc610e2007-01-01 18:37:02 +110075 crypto_larval_error(param->larval.name, param->type.data.type,
76 param->type.data.mask);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100077 goto out;
78}
79
80static int cryptomgr_schedule_probe(struct crypto_larval *larval)
81{
Herbert Xu1605b842007-05-09 13:04:39 +100082 struct task_struct *thread;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100083 struct cryptomgr_param *param;
84 const char *name = larval->alg.cra_name;
85 const char *p;
86 unsigned int len;
87
Herbert Xucf02f5d2007-03-29 17:32:59 +100088 if (!try_module_get(THIS_MODULE))
89 goto err;
90
Herbert Xuebc610e2007-01-01 18:37:02 +110091 param = kzalloc(sizeof(*param), GFP_KERNEL);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100092 if (!param)
Herbert Xucf02f5d2007-03-29 17:32:59 +100093 goto err_put_module;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100094
95 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
96 ;
97
98 len = p - name;
99 if (!len || *p != '(')
100 goto err_free_param;
101
102 memcpy(param->template, name, len);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000103
104 name = p + 1;
Herbert Xucf02f5d2007-03-29 17:32:59 +1000105 len = 0;
106 for (p = name; *p; p++) {
107 for (; isalnum(*p) || *p == '-' || *p == '_' || *p == '('; p++)
108 ;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000109
Herbert Xucf02f5d2007-03-29 17:32:59 +1000110 if (*p != ')')
111 goto err_free_param;
112
113 len = p - name;
114 }
115
116 if (!len || name[len + 1])
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000117 goto err_free_param;
118
Herbert Xuebc610e2007-01-01 18:37:02 +1100119 param->type.attr.rta_len = sizeof(param->type);
120 param->type.attr.rta_type = CRYPTOA_TYPE;
121 param->type.data.type = larval->alg.cra_flags;
122 param->type.data.mask = larval->mask;
123 param->tb[CRYPTOA_TYPE - 1] = &param->type.attr;
124
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000125 param->alg.attr.rta_len = sizeof(param->alg);
126 param->alg.attr.rta_type = CRYPTOA_ALG;
127 memcpy(param->alg.data.name, name, len);
Herbert Xuebc610e2007-01-01 18:37:02 +1100128 param->tb[CRYPTOA_ALG - 1] = &param->alg.attr;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000129
130 memcpy(param->larval.name, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000131
Herbert Xu1605b842007-05-09 13:04:39 +1000132 thread = kthread_run(cryptomgr_probe, param, "cryptomgr");
133 if (IS_ERR(thread))
Herbert Xucf02f5d2007-03-29 17:32:59 +1000134 goto err_free_param;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000135
136 return NOTIFY_STOP;
137
138err_free_param:
139 kfree(param);
Herbert Xucf02f5d2007-03-29 17:32:59 +1000140err_put_module:
141 module_put(THIS_MODULE);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000142err:
143 return NOTIFY_OK;
144}
145
146static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
147 void *data)
148{
149 switch (msg) {
150 case CRYPTO_MSG_ALG_REQUEST:
151 return cryptomgr_schedule_probe(data);
152 }
153
154 return NOTIFY_DONE;
155}
156
157static struct notifier_block cryptomgr_notifier = {
158 .notifier_call = cryptomgr_notify,
159};
160
161static int __init cryptomgr_init(void)
162{
163 return crypto_register_notifier(&cryptomgr_notifier);
164}
165
166static void __exit cryptomgr_exit(void)
167{
168 int err = crypto_unregister_notifier(&cryptomgr_notifier);
169 BUG_ON(err);
170}
171
172module_init(cryptomgr_init);
173module_exit(cryptomgr_exit);
174
175MODULE_LICENSE("GPL");
176MODULE_DESCRIPTION("Crypto Algorithm Manager");