Blame view
crypto/algboss.c
6.27 KB
2b8c19dbd [CRYPTO] api: Add... |
1 2 3 4 5 6 7 8 9 10 11 |
/* * Create default crypto algorithm instances. * * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * */ |
6fe4a28d8 crypto: testmgr -... |
12 |
#include <crypto/internal/aead.h> |
398710379 crypto: algapi - ... |
13 |
#include <linux/completion.h> |
2b8c19dbd [CRYPTO] api: Add... |
14 15 16 |
#include <linux/ctype.h> #include <linux/err.h> #include <linux/init.h> |
cf02f5da9 [CRYPTO] cryptomg... |
17 |
#include <linux/kthread.h> |
2b8c19dbd [CRYPTO] api: Add... |
18 19 20 |
#include <linux/module.h> #include <linux/notifier.h> #include <linux/rtnetlink.h> |
6bfd48096 [CRYPTO] api: Add... |
21 |
#include <linux/sched.h> |
5a0e3ad6a include cleanup: ... |
22 |
#include <linux/slab.h> |
2b8c19dbd [CRYPTO] api: Add... |
23 |
#include <linux/string.h> |
2b8c19dbd [CRYPTO] api: Add... |
24 25 26 27 |
#include "internal.h" struct cryptomgr_param { |
39e1ee011 [CRYPTO] api: Add... |
28 |
struct rtattr *tb[CRYPTO_MAX_ATTRS + 2]; |
ebc610e5b [CRYPTO] template... |
29 30 31 32 33 |
struct { struct rtattr attr; struct crypto_attr_type data; } type; |
39e1ee011 [CRYPTO] api: Add... |
34 |
union { |
2b8c19dbd [CRYPTO] api: Add... |
35 |
struct rtattr attr; |
39e1ee011 [CRYPTO] api: Add... |
36 37 38 39 40 41 42 43 44 |
struct { struct rtattr attr; struct crypto_attr_alg data; } alg; struct { struct rtattr attr; struct crypto_attr_u32 data; } nu32; } attrs[CRYPTO_MAX_ATTRS]; |
2b8c19dbd [CRYPTO] api: Add... |
45 |
char template[CRYPTO_MAX_ALG_NAME]; |
73d3864a4 crypto: api - Use... |
46 |
|
939e17799 crypto: algboss -... |
47 |
struct crypto_larval *larval; |
398710379 crypto: algapi - ... |
48 |
|
73d3864a4 crypto: api - Use... |
49 50 51 52 53 54 55 56 |
u32 otype; u32 omask; }; struct crypto_test_param { char driver[CRYPTO_MAX_ALG_NAME]; char alg[CRYPTO_MAX_ALG_NAME]; u32 type; |
2b8c19dbd [CRYPTO] api: Add... |
57 |
}; |
cf02f5da9 [CRYPTO] cryptomg... |
58 |
static int cryptomgr_probe(void *data) |
2b8c19dbd [CRYPTO] api: Add... |
59 |
{ |
cf02f5da9 [CRYPTO] cryptomg... |
60 |
struct cryptomgr_param *param = data; |
2b8c19dbd [CRYPTO] api: Add... |
61 62 |
struct crypto_template *tmpl; struct crypto_instance *inst; |
6bfd48096 [CRYPTO] api: Add... |
63 |
int err; |
2b8c19dbd [CRYPTO] api: Add... |
64 65 66 |
tmpl = crypto_lookup_template(param->template); if (!tmpl) |
398710379 crypto: algapi - ... |
67 |
goto out; |
2b8c19dbd [CRYPTO] api: Add... |
68 |
|
6bfd48096 [CRYPTO] api: Add... |
69 |
do { |
f2ac72e82 crypto: api - Add... |
70 71 72 73 |
if (tmpl->create) { err = tmpl->create(tmpl, param->tb); continue; } |
ebc610e5b [CRYPTO] template... |
74 |
inst = tmpl->alloc(param->tb); |
6bfd48096 [CRYPTO] api: Add... |
75 76 77 78 79 |
if (IS_ERR(inst)) err = PTR_ERR(inst); else if ((err = crypto_register_instance(tmpl, inst))) tmpl->free(inst); } while (err == -EAGAIN && !signal_pending(current)); |
2b8c19dbd [CRYPTO] api: Add... |
80 81 82 83 |
crypto_tmpl_put(tmpl); out: |
939e17799 crypto: algboss -... |
84 85 |
complete_all(¶m->larval->completion); crypto_alg_put(¶m->larval->alg); |
2b8c19dbd [CRYPTO] api: Add... |
86 |
kfree(param); |
cf02f5da9 [CRYPTO] cryptomg... |
87 |
module_put_and_exit(0); |
2b8c19dbd [CRYPTO] api: Add... |
88 89 90 91 |
} static int cryptomgr_schedule_probe(struct crypto_larval *larval) { |
1605b8471 [CRYPTO] cryptomg... |
92 |
struct task_struct *thread; |
2b8c19dbd [CRYPTO] api: Add... |
93 94 95 96 |
struct cryptomgr_param *param; const char *name = larval->alg.cra_name; const char *p; unsigned int len; |
39e1ee011 [CRYPTO] api: Add... |
97 |
int i; |
2b8c19dbd [CRYPTO] api: Add... |
98 |
|
cf02f5da9 [CRYPTO] cryptomg... |
99 100 |
if (!try_module_get(THIS_MODULE)) goto err; |
ebc610e5b [CRYPTO] template... |
101 |
param = kzalloc(sizeof(*param), GFP_KERNEL); |
2b8c19dbd [CRYPTO] api: Add... |
102 |
if (!param) |
cf02f5da9 [CRYPTO] cryptomg... |
103 |
goto err_put_module; |
2b8c19dbd [CRYPTO] api: Add... |
104 105 106 107 108 109 110 111 112 |
for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++) ; len = p - name; if (!len || *p != '(') goto err_free_param; memcpy(param->template, name, len); |
2b8c19dbd [CRYPTO] api: Add... |
113 |
|
39e1ee011 [CRYPTO] api: Add... |
114 115 116 |
i = 0; for (;;) { int notnum = 0; |
2b8c19dbd [CRYPTO] api: Add... |
117 |
|
39e1ee011 [CRYPTO] api: Add... |
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
name = ++p; len = 0; for (; isalnum(*p) || *p == '-' || *p == '_'; p++) notnum |= !isdigit(*p); if (*p == '(') { int recursion = 0; for (;;) { if (!*++p) goto err_free_param; if (*p == '(') recursion++; else if (*p == ')' && !recursion--) break; } notnum = 1; |
720a650f8 [CRYPTO] cryptomg... |
137 |
p++; |
39e1ee011 [CRYPTO] api: Add... |
138 |
} |
cf02f5da9 [CRYPTO] cryptomg... |
139 140 |
len = p - name; |
39e1ee011 [CRYPTO] api: Add... |
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
if (!len) goto err_free_param; if (notnum) { param->attrs[i].alg.attr.rta_len = sizeof(param->attrs[i].alg); param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG; memcpy(param->attrs[i].alg.data.name, name, len); } else { param->attrs[i].nu32.attr.rta_len = sizeof(param->attrs[i].nu32); param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32; param->attrs[i].nu32.data.num = simple_strtol(name, NULL, 0); } param->tb[i + 1] = ¶m->attrs[i].attr; i++; |
720a650f8 [CRYPTO] cryptomg... |
159 |
if (i >= CRYPTO_MAX_ATTRS) |
39e1ee011 [CRYPTO] api: Add... |
160 161 162 163 164 165 166 |
goto err_free_param; if (*p == ')') break; if (*p != ',') goto err_free_param; |
cf02f5da9 [CRYPTO] cryptomg... |
167 |
} |
39e1ee011 [CRYPTO] api: Add... |
168 |
if (!i) |
2b8c19dbd [CRYPTO] api: Add... |
169 |
goto err_free_param; |
39e1ee011 [CRYPTO] api: Add... |
170 |
param->tb[i + 1] = NULL; |
ebc610e5b [CRYPTO] template... |
171 172 |
param->type.attr.rta_len = sizeof(param->type); param->type.attr.rta_type = CRYPTOA_TYPE; |
73d3864a4 crypto: api - Use... |
173 174 |
param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED; param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED; |
39e1ee011 [CRYPTO] api: Add... |
175 |
param->tb[0] = ¶m->type.attr; |
2b8c19dbd [CRYPTO] api: Add... |
176 |
|
73d3864a4 crypto: api - Use... |
177 178 |
param->otype = larval->alg.cra_flags; param->omask = larval->mask; |
939e17799 crypto: algboss -... |
179 180 |
crypto_alg_get(&larval->alg); param->larval = larval; |
398710379 crypto: algapi - ... |
181 |
|
73d3864a4 crypto: api - Use... |
182 183 |
thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe"); if (IS_ERR(thread)) |
939e17799 crypto: algboss -... |
184 |
goto err_put_larval; |
73d3864a4 crypto: api - Use... |
185 |
|
398710379 crypto: algapi - ... |
186 |
wait_for_completion_interruptible(&larval->completion); |
73d3864a4 crypto: api - Use... |
187 |
return NOTIFY_STOP; |
939e17799 crypto: algboss -... |
188 189 |
err_put_larval: crypto_alg_put(&larval->alg); |
73d3864a4 crypto: api - Use... |
190 191 192 193 194 195 196 197 198 199 200 201 202 |
err_free_param: kfree(param); err_put_module: module_put(THIS_MODULE); err: return NOTIFY_OK; } static int cryptomgr_test(void *data) { struct crypto_test_param *param = data; u32 type = param->type; int err = 0; |
326a6346f crypto: testmgr -... |
203 204 205 |
#ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS goto skiptest; #endif |
6fe4a28d8 crypto: testmgr -... |
206 |
if (type & CRYPTO_ALG_TESTED) |
73d3864a4 crypto: api - Use... |
207 |
goto skiptest; |
1aa4ecd95 crypto: cryptomgr... |
208 |
err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED); |
73d3864a4 crypto: api - Use... |
209 210 211 212 213 214 215 216 217 218 219 220 |
skiptest: crypto_alg_tested(param->driver, err); kfree(param); module_put_and_exit(0); } static int cryptomgr_schedule_test(struct crypto_alg *alg) { struct task_struct *thread; struct crypto_test_param *param; |
6fe4a28d8 crypto: testmgr -... |
221 |
u32 type; |
73d3864a4 crypto: api - Use... |
222 223 224 225 226 227 228 229 230 231 |
if (!try_module_get(THIS_MODULE)) goto err; param = kzalloc(sizeof(*param), GFP_KERNEL); if (!param) goto err_put_module; memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver)); memcpy(param->alg, alg->cra_name, sizeof(param->alg)); |
6fe4a28d8 crypto: testmgr -... |
232 233 234 |
type = alg->cra_flags; /* This piece of crap needs to disappear into per-type test hooks. */ |
aa1b6bb7d crypto: algboss -... |
235 236 237 238 239 |
if (!((type ^ CRYPTO_ALG_TYPE_BLKCIPHER) & CRYPTO_ALG_TYPE_BLKCIPHER_MASK) && !(type & CRYPTO_ALG_GENIV) && ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize : alg->cra_ablkcipher.ivsize)) |
6fe4a28d8 crypto: testmgr -... |
240 241 242 |
type |= CRYPTO_ALG_TESTED; param->type = type; |
73d3864a4 crypto: api - Use... |
243 244 |
thread = kthread_run(cryptomgr_test, param, "cryptomgr_test"); |
1605b8471 [CRYPTO] cryptomg... |
245 |
if (IS_ERR(thread)) |
cf02f5da9 [CRYPTO] cryptomg... |
246 |
goto err_free_param; |
2b8c19dbd [CRYPTO] api: Add... |
247 248 249 250 251 |
return NOTIFY_STOP; err_free_param: kfree(param); |
cf02f5da9 [CRYPTO] cryptomg... |
252 253 |
err_put_module: module_put(THIS_MODULE); |
2b8c19dbd [CRYPTO] api: Add... |
254 255 256 257 258 259 260 261 262 263 |
err: return NOTIFY_OK; } static int cryptomgr_notify(struct notifier_block *this, unsigned long msg, void *data) { switch (msg) { case CRYPTO_MSG_ALG_REQUEST: return cryptomgr_schedule_probe(data); |
73d3864a4 crypto: api - Use... |
264 265 |
case CRYPTO_MSG_ALG_REGISTER: return cryptomgr_schedule_test(data); |
2b8c19dbd [CRYPTO] api: Add... |
266 267 268 269 270 271 272 273 274 275 276 |
} return NOTIFY_DONE; } static struct notifier_block cryptomgr_notifier = { .notifier_call = cryptomgr_notify, }; static int __init cryptomgr_init(void) { |
f8b0d4d09 crypto: testmgr -... |
277 |
return crypto_register_notifier(&cryptomgr_notifier); |
2b8c19dbd [CRYPTO] api: Add... |
278 279 280 281 282 283 284 |
} static void __exit cryptomgr_exit(void) { int err = crypto_unregister_notifier(&cryptomgr_notifier); BUG_ON(err); } |
da7f033dd crypto: cryptomgr... |
285 |
subsys_initcall(cryptomgr_init); |
2b8c19dbd [CRYPTO] api: Add... |
286 287 288 289 |
module_exit(cryptomgr_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Crypto Algorithm Manager"); |