blob: 7a3df9b4121881a65bd723ee7ef883baaec0e80f [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>
17#include <linux/module.h>
18#include <linux/notifier.h>
19#include <linux/rtnetlink.h>
Herbert Xu6bfd4802006-09-21 11:39:29 +100020#include <linux/sched.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100021#include <linux/string.h>
22#include <linux/workqueue.h>
23
24#include "internal.h"
25
26struct cryptomgr_param {
27 struct work_struct work;
28
Herbert Xuebc610e2007-01-01 18:37:02 +110029 struct rtattr *tb[CRYPTOA_MAX];
30
31 struct {
32 struct rtattr attr;
33 struct crypto_attr_type data;
34 } type;
35
Herbert Xu2b8c19d2006-09-21 11:31:44 +100036 struct {
37 struct rtattr attr;
38 struct crypto_attr_alg data;
39 } alg;
40
41 struct {
42 char name[CRYPTO_MAX_ALG_NAME];
43 } larval;
44
45 char template[CRYPTO_MAX_ALG_NAME];
46};
47
David Howells65f27f32006-11-22 14:55:48 +000048static void cryptomgr_probe(struct work_struct *work)
Herbert Xu2b8c19d2006-09-21 11:31:44 +100049{
David Howells65f27f32006-11-22 14:55:48 +000050 struct cryptomgr_param *param =
51 container_of(work, struct cryptomgr_param, work);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100052 struct crypto_template *tmpl;
53 struct crypto_instance *inst;
Herbert Xu6bfd4802006-09-21 11:39:29 +100054 int err;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100055
56 tmpl = crypto_lookup_template(param->template);
57 if (!tmpl)
58 goto err;
59
Herbert Xu6bfd4802006-09-21 11:39:29 +100060 do {
Herbert Xuebc610e2007-01-01 18:37:02 +110061 inst = tmpl->alloc(param->tb);
Herbert Xu6bfd4802006-09-21 11:39:29 +100062 if (IS_ERR(inst))
63 err = PTR_ERR(inst);
64 else if ((err = crypto_register_instance(tmpl, inst)))
65 tmpl->free(inst);
66 } while (err == -EAGAIN && !signal_pending(current));
Herbert Xu2b8c19d2006-09-21 11:31:44 +100067
68 crypto_tmpl_put(tmpl);
69
Herbert Xu6bfd4802006-09-21 11:39:29 +100070 if (err)
71 goto err;
72
Herbert Xu2b8c19d2006-09-21 11:31:44 +100073out:
74 kfree(param);
75 return;
76
77err:
Herbert Xuebc610e2007-01-01 18:37:02 +110078 crypto_larval_error(param->larval.name, param->type.data.type,
79 param->type.data.mask);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100080 goto out;
81}
82
83static int cryptomgr_schedule_probe(struct crypto_larval *larval)
84{
85 struct cryptomgr_param *param;
86 const char *name = larval->alg.cra_name;
87 const char *p;
88 unsigned int len;
89
Herbert Xuebc610e2007-01-01 18:37:02 +110090 param = kzalloc(sizeof(*param), GFP_KERNEL);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100091 if (!param)
92 goto err;
93
94 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
95 ;
96
97 len = p - name;
98 if (!len || *p != '(')
99 goto err_free_param;
100
101 memcpy(param->template, name, len);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000102
103 name = p + 1;
104 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
105 ;
106
107 len = p - name;
108 if (!len || *p != ')' || p[1])
109 goto err_free_param;
110
Herbert Xuebc610e2007-01-01 18:37:02 +1100111 param->type.attr.rta_len = sizeof(param->type);
112 param->type.attr.rta_type = CRYPTOA_TYPE;
113 param->type.data.type = larval->alg.cra_flags;
114 param->type.data.mask = larval->mask;
115 param->tb[CRYPTOA_TYPE - 1] = &param->type.attr;
116
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000117 param->alg.attr.rta_len = sizeof(param->alg);
118 param->alg.attr.rta_type = CRYPTOA_ALG;
119 memcpy(param->alg.data.name, name, len);
Herbert Xuebc610e2007-01-01 18:37:02 +1100120 param->tb[CRYPTOA_ALG - 1] = &param->alg.attr;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000121
122 memcpy(param->larval.name, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000123
David Howells65f27f32006-11-22 14:55:48 +0000124 INIT_WORK(&param->work, cryptomgr_probe);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000125 schedule_work(&param->work);
126
127 return NOTIFY_STOP;
128
129err_free_param:
130 kfree(param);
131err:
132 return NOTIFY_OK;
133}
134
135static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
136 void *data)
137{
138 switch (msg) {
139 case CRYPTO_MSG_ALG_REQUEST:
140 return cryptomgr_schedule_probe(data);
141 }
142
143 return NOTIFY_DONE;
144}
145
146static struct notifier_block cryptomgr_notifier = {
147 .notifier_call = cryptomgr_notify,
148};
149
150static int __init cryptomgr_init(void)
151{
152 return crypto_register_notifier(&cryptomgr_notifier);
153}
154
155static void __exit cryptomgr_exit(void)
156{
157 int err = crypto_unregister_notifier(&cryptomgr_notifier);
158 BUG_ON(err);
159}
160
161module_init(cryptomgr_init);
162module_exit(cryptomgr_exit);
163
164MODULE_LICENSE("GPL");
165MODULE_DESCRIPTION("Crypto Algorithm Manager");