]>
Commit | Line | Data |
---|---|---|
cce9e06d HX |
1 | /* |
2 | * Cryptographic API for algorithms (i.e., low-level API). | |
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 | ||
6bfd4809 | 13 | #include <linux/err.h> |
cce9e06d | 14 | #include <linux/errno.h> |
3133d76f | 15 | #include <linux/fips.h> |
cce9e06d HX |
16 | #include <linux/init.h> |
17 | #include <linux/kernel.h> | |
4cc7720c | 18 | #include <linux/list.h> |
cce9e06d | 19 | #include <linux/module.h> |
7fed0bf2 | 20 | #include <linux/rtnetlink.h> |
5a0e3ad6 | 21 | #include <linux/slab.h> |
cce9e06d HX |
22 | #include <linux/string.h> |
23 | ||
24 | #include "internal.h" | |
25 | ||
4cc7720c HX |
26 | static LIST_HEAD(crypto_template_list); |
27 | ||
cce9e06d HX |
28 | static inline int crypto_set_driver_name(struct crypto_alg *alg) |
29 | { | |
30 | static const char suffix[] = "-generic"; | |
31 | char *driver_name = alg->cra_driver_name; | |
32 | int len; | |
33 | ||
34 | if (*driver_name) | |
35 | return 0; | |
36 | ||
37 | len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); | |
38 | if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME) | |
39 | return -ENAMETOOLONG; | |
40 | ||
41 | memcpy(driver_name + len, suffix, sizeof(suffix)); | |
42 | return 0; | |
43 | } | |
44 | ||
002c77a4 JW |
45 | static inline void crypto_check_module_sig(struct module *mod) |
46 | { | |
59afdc7b | 47 | if (fips_enabled && mod && !module_sig_ok(mod)) |
002c77a4 JW |
48 | panic("Module %s signature verification failed in FIPS mode\n", |
49 | mod->name); | |
002c77a4 JW |
50 | } |
51 | ||
4cc7720c | 52 | static int crypto_check_alg(struct crypto_alg *alg) |
cce9e06d | 53 | { |
002c77a4 JW |
54 | crypto_check_module_sig(alg->cra_module); |
55 | ||
cce9e06d HX |
56 | if (alg->cra_alignmask & (alg->cra_alignmask + 1)) |
57 | return -EINVAL; | |
58 | ||
cce9e06d HX |
59 | if (alg->cra_blocksize > PAGE_SIZE / 8) |
60 | return -EINVAL; | |
61 | ||
62 | if (alg->cra_priority < 0) | |
63 | return -EINVAL; | |
cce9e06d | 64 | |
e9b8e5be HX |
65 | atomic_set(&alg->cra_refcnt, 1); |
66 | ||
4cc7720c HX |
67 | return crypto_set_driver_name(alg); |
68 | } | |
69 | ||
6bfd4809 HX |
70 | static void crypto_destroy_instance(struct crypto_alg *alg) |
71 | { | |
72 | struct crypto_instance *inst = (void *)alg; | |
73 | struct crypto_template *tmpl = inst->tmpl; | |
74 | ||
75 | tmpl->free(inst); | |
76 | crypto_tmpl_put(tmpl); | |
77 | } | |
78 | ||
2bf29016 HX |
79 | static struct list_head *crypto_more_spawns(struct crypto_alg *alg, |
80 | struct list_head *stack, | |
81 | struct list_head *top, | |
82 | struct list_head *secondary_spawns) | |
83 | { | |
84 | struct crypto_spawn *spawn, *n; | |
85 | ||
86 | if (list_empty(stack)) | |
87 | return NULL; | |
88 | ||
89 | spawn = list_first_entry(stack, struct crypto_spawn, list); | |
90 | n = list_entry(spawn->list.next, struct crypto_spawn, list); | |
91 | ||
92 | if (spawn->alg && &n->list != stack && !n->alg) | |
93 | n->alg = (n->list.next == stack) ? alg : | |
94 | &list_entry(n->list.next, struct crypto_spawn, | |
95 | list)->inst->alg; | |
96 | ||
97 | list_move(&spawn->list, secondary_spawns); | |
98 | ||
99 | return &n->list == stack ? top : &n->inst->alg.cra_users; | |
100 | } | |
101 | ||
1f723710 HX |
102 | static void crypto_remove_instance(struct crypto_instance *inst, |
103 | struct list_head *list) | |
6bfd4809 | 104 | { |
a73e6996 | 105 | struct crypto_template *tmpl = inst->tmpl; |
6bfd4809 | 106 | |
a73e6996 HX |
107 | if (crypto_is_dead(&inst->alg)) |
108 | return; | |
6bfd4809 | 109 | |
a73e6996 | 110 | inst->alg.cra_flags |= CRYPTO_ALG_DEAD; |
38cb2419 HX |
111 | if (hlist_unhashed(&inst->list)) |
112 | return; | |
113 | ||
a73e6996 HX |
114 | if (!tmpl || !crypto_tmpl_get(tmpl)) |
115 | return; | |
116 | ||
117 | crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg); | |
118 | list_move(&inst->alg.cra_list, list); | |
119 | hlist_del(&inst->list); | |
120 | inst->alg.cra_destroy = crypto_destroy_instance; | |
121 | ||
2bf29016 | 122 | BUG_ON(!list_empty(&inst->alg.cra_users)); |
a73e6996 HX |
123 | } |
124 | ||
89b596ba SK |
125 | void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list, |
126 | struct crypto_alg *nalg) | |
a73e6996 | 127 | { |
2bf29016 | 128 | u32 new_type = (nalg ?: alg)->cra_flags; |
a73e6996 HX |
129 | struct crypto_spawn *spawn, *n; |
130 | LIST_HEAD(secondary_spawns); | |
2bf29016 HX |
131 | struct list_head *spawns; |
132 | LIST_HEAD(stack); | |
133 | LIST_HEAD(top); | |
6bfd4809 | 134 | |
2bf29016 | 135 | spawns = &alg->cra_users; |
a73e6996 HX |
136 | list_for_each_entry_safe(spawn, n, spawns, list) { |
137 | if ((spawn->alg->cra_flags ^ new_type) & spawn->mask) | |
6bfd4809 HX |
138 | continue; |
139 | ||
2bf29016 | 140 | list_move(&spawn->list, &top); |
a73e6996 | 141 | } |
6bfd4809 | 142 | |
2bf29016 HX |
143 | spawns = ⊤ |
144 | do { | |
145 | while (!list_empty(spawns)) { | |
146 | struct crypto_instance *inst; | |
147 | ||
148 | spawn = list_first_entry(spawns, struct crypto_spawn, | |
149 | list); | |
150 | inst = spawn->inst; | |
151 | ||
152 | BUG_ON(&inst->alg == alg); | |
153 | ||
154 | list_move(&spawn->list, &stack); | |
155 | ||
156 | if (&inst->alg == nalg) | |
157 | break; | |
158 | ||
159 | spawn->alg = NULL; | |
160 | spawns = &inst->alg.cra_users; | |
161 | } | |
162 | } while ((spawns = crypto_more_spawns(alg, &stack, &top, | |
163 | &secondary_spawns))); | |
164 | ||
165 | list_for_each_entry_safe(spawn, n, &secondary_spawns, list) { | |
166 | if (spawn->alg) | |
167 | list_move(&spawn->list, &spawn->alg->cra_users); | |
168 | else | |
1f723710 | 169 | crypto_remove_instance(spawn->inst, list); |
6bfd4809 HX |
170 | } |
171 | } | |
89b596ba | 172 | EXPORT_SYMBOL_GPL(crypto_remove_spawns); |
6bfd4809 | 173 | |
73d3864a | 174 | static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg) |
4cc7720c HX |
175 | { |
176 | struct crypto_alg *q; | |
73d3864a | 177 | struct crypto_larval *larval; |
6bfd4809 HX |
178 | int ret = -EAGAIN; |
179 | ||
180 | if (crypto_is_dead(alg)) | |
73d3864a | 181 | goto err; |
6bfd4809 HX |
182 | |
183 | INIT_LIST_HEAD(&alg->cra_users); | |
184 | ||
73d3864a HX |
185 | /* No cheating! */ |
186 | alg->cra_flags &= ~CRYPTO_ALG_TESTED; | |
187 | ||
6bfd4809 | 188 | ret = -EEXIST; |
4cc7720c | 189 | |
cce9e06d | 190 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
4cc7720c | 191 | if (q == alg) |
73d3864a HX |
192 | goto err; |
193 | ||
b8e15992 HX |
194 | if (crypto_is_moribund(q)) |
195 | continue; | |
196 | ||
73d3864a HX |
197 | if (crypto_is_larval(q)) { |
198 | if (!strcmp(alg->cra_driver_name, q->cra_driver_name)) | |
199 | goto err; | |
200 | continue; | |
201 | } | |
202 | ||
203 | if (!strcmp(q->cra_driver_name, alg->cra_name) || | |
204 | !strcmp(q->cra_name, alg->cra_driver_name)) | |
205 | goto err; | |
206 | } | |
207 | ||
208 | larval = crypto_larval_alloc(alg->cra_name, | |
209 | alg->cra_flags | CRYPTO_ALG_TESTED, 0); | |
210 | if (IS_ERR(larval)) | |
211 | goto out; | |
212 | ||
213 | ret = -ENOENT; | |
214 | larval->adult = crypto_mod_get(alg); | |
215 | if (!larval->adult) | |
216 | goto free_larval; | |
217 | ||
218 | atomic_set(&larval->alg.cra_refcnt, 1); | |
219 | memcpy(larval->alg.cra_driver_name, alg->cra_driver_name, | |
220 | CRYPTO_MAX_ALG_NAME); | |
221 | larval->alg.cra_priority = alg->cra_priority; | |
222 | ||
223 | list_add(&alg->cra_list, &crypto_alg_list); | |
224 | list_add(&larval->alg.cra_list, &crypto_alg_list); | |
225 | ||
5357c6c4 | 226 | out: |
73d3864a HX |
227 | return larval; |
228 | ||
229 | free_larval: | |
230 | kfree(larval); | |
231 | err: | |
232 | larval = ERR_PTR(ret); | |
233 | goto out; | |
234 | } | |
235 | ||
236 | void crypto_alg_tested(const char *name, int err) | |
237 | { | |
238 | struct crypto_larval *test; | |
239 | struct crypto_alg *alg; | |
240 | struct crypto_alg *q; | |
241 | LIST_HEAD(list); | |
242 | ||
243 | down_write(&crypto_alg_sem); | |
244 | list_for_each_entry(q, &crypto_alg_list, cra_list) { | |
b8e15992 | 245 | if (crypto_is_moribund(q) || !crypto_is_larval(q)) |
73d3864a HX |
246 | continue; |
247 | ||
248 | test = (struct crypto_larval *)q; | |
249 | ||
250 | if (!strcmp(q->cra_driver_name, name)) | |
251 | goto found; | |
252 | } | |
253 | ||
254 | printk(KERN_ERR "alg: Unexpected test result for %s: %d\n", name, err); | |
255 | goto unlock; | |
256 | ||
257 | found: | |
b8e15992 | 258 | q->cra_flags |= CRYPTO_ALG_DEAD; |
73d3864a HX |
259 | alg = test->adult; |
260 | if (err || list_empty(&alg->cra_list)) | |
261 | goto complete; | |
262 | ||
263 | alg->cra_flags |= CRYPTO_ALG_TESTED; | |
264 | ||
265 | list_for_each_entry(q, &crypto_alg_list, cra_list) { | |
266 | if (q == alg) | |
267 | continue; | |
6bfd4809 HX |
268 | |
269 | if (crypto_is_moribund(q)) | |
270 | continue; | |
271 | ||
272 | if (crypto_is_larval(q)) { | |
2825982d HX |
273 | struct crypto_larval *larval = (void *)q; |
274 | ||
d8058480 HX |
275 | /* |
276 | * Check to see if either our generic name or | |
277 | * specific name can satisfy the name requested | |
278 | * by the larval entry q. | |
279 | */ | |
6bfd4809 HX |
280 | if (strcmp(alg->cra_name, q->cra_name) && |
281 | strcmp(alg->cra_driver_name, q->cra_name)) | |
282 | continue; | |
283 | ||
284 | if (larval->adult) | |
285 | continue; | |
492e2b63 HX |
286 | if ((q->cra_flags ^ alg->cra_flags) & larval->mask) |
287 | continue; | |
2825982d HX |
288 | if (!crypto_mod_get(alg)) |
289 | continue; | |
6bfd4809 | 290 | |
2825982d | 291 | larval->adult = alg; |
6bfd4809 | 292 | continue; |
2825982d | 293 | } |
6bfd4809 HX |
294 | |
295 | if (strcmp(alg->cra_name, q->cra_name)) | |
296 | continue; | |
297 | ||
298 | if (strcmp(alg->cra_driver_name, q->cra_driver_name) && | |
299 | q->cra_priority > alg->cra_priority) | |
300 | continue; | |
301 | ||
2bf29016 | 302 | crypto_remove_spawns(q, &list, alg); |
cce9e06d | 303 | } |
2825982d | 304 | |
73d3864a HX |
305 | complete: |
306 | complete_all(&test->completion); | |
2825982d | 307 | |
73d3864a HX |
308 | unlock: |
309 | up_write(&crypto_alg_sem); | |
310 | ||
311 | crypto_remove_final(&list); | |
cce9e06d | 312 | } |
73d3864a | 313 | EXPORT_SYMBOL_GPL(crypto_alg_tested); |
4cc7720c | 314 | |
22e5b20b | 315 | void crypto_remove_final(struct list_head *list) |
6bfd4809 HX |
316 | { |
317 | struct crypto_alg *alg; | |
318 | struct crypto_alg *n; | |
319 | ||
320 | list_for_each_entry_safe(alg, n, list, cra_list) { | |
321 | list_del_init(&alg->cra_list); | |
322 | crypto_alg_put(alg); | |
323 | } | |
324 | } | |
22e5b20b | 325 | EXPORT_SYMBOL_GPL(crypto_remove_final); |
6bfd4809 | 326 | |
73d3864a HX |
327 | static void crypto_wait_for_test(struct crypto_larval *larval) |
328 | { | |
329 | int err; | |
330 | ||
331 | err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult); | |
332 | if (err != NOTIFY_STOP) { | |
333 | if (WARN_ON(err != NOTIFY_DONE)) | |
334 | goto out; | |
335 | crypto_alg_tested(larval->alg.cra_driver_name, 0); | |
336 | } | |
337 | ||
338 | err = wait_for_completion_interruptible(&larval->completion); | |
339 | WARN_ON(err); | |
340 | ||
341 | out: | |
342 | crypto_larval_kill(&larval->alg); | |
343 | } | |
344 | ||
4cc7720c HX |
345 | int crypto_register_alg(struct crypto_alg *alg) |
346 | { | |
73d3864a | 347 | struct crypto_larval *larval; |
4cc7720c HX |
348 | int err; |
349 | ||
350 | err = crypto_check_alg(alg); | |
351 | if (err) | |
352 | return err; | |
353 | ||
354 | down_write(&crypto_alg_sem); | |
73d3864a | 355 | larval = __crypto_register_alg(alg); |
4cc7720c HX |
356 | up_write(&crypto_alg_sem); |
357 | ||
73d3864a HX |
358 | if (IS_ERR(larval)) |
359 | return PTR_ERR(larval); | |
360 | ||
361 | crypto_wait_for_test(larval); | |
362 | return 0; | |
4cc7720c | 363 | } |
cce9e06d HX |
364 | EXPORT_SYMBOL_GPL(crypto_register_alg); |
365 | ||
6bfd4809 HX |
366 | static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list) |
367 | { | |
368 | if (unlikely(list_empty(&alg->cra_list))) | |
369 | return -ENOENT; | |
370 | ||
371 | alg->cra_flags |= CRYPTO_ALG_DEAD; | |
372 | ||
373 | crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg); | |
374 | list_del_init(&alg->cra_list); | |
2bf29016 | 375 | crypto_remove_spawns(alg, list, NULL); |
6bfd4809 HX |
376 | |
377 | return 0; | |
378 | } | |
379 | ||
cce9e06d HX |
380 | int crypto_unregister_alg(struct crypto_alg *alg) |
381 | { | |
6bfd4809 HX |
382 | int ret; |
383 | LIST_HEAD(list); | |
5357c6c4 | 384 | |
cce9e06d | 385 | down_write(&crypto_alg_sem); |
6bfd4809 | 386 | ret = crypto_remove_alg(alg, &list); |
cce9e06d HX |
387 | up_write(&crypto_alg_sem); |
388 | ||
389 | if (ret) | |
390 | return ret; | |
391 | ||
392 | BUG_ON(atomic_read(&alg->cra_refcnt) != 1); | |
393 | if (alg->cra_destroy) | |
394 | alg->cra_destroy(alg); | |
395 | ||
6bfd4809 | 396 | crypto_remove_final(&list); |
cce9e06d HX |
397 | return 0; |
398 | } | |
399 | EXPORT_SYMBOL_GPL(crypto_unregister_alg); | |
400 | ||
4b004346 MB |
401 | int crypto_register_algs(struct crypto_alg *algs, int count) |
402 | { | |
403 | int i, ret; | |
404 | ||
405 | for (i = 0; i < count; i++) { | |
406 | ret = crypto_register_alg(&algs[i]); | |
407 | if (ret) | |
408 | goto err; | |
409 | } | |
410 | ||
411 | return 0; | |
412 | ||
413 | err: | |
414 | for (--i; i >= 0; --i) | |
415 | crypto_unregister_alg(&algs[i]); | |
416 | ||
417 | return ret; | |
418 | } | |
419 | EXPORT_SYMBOL_GPL(crypto_register_algs); | |
420 | ||
421 | int crypto_unregister_algs(struct crypto_alg *algs, int count) | |
422 | { | |
423 | int i, ret; | |
424 | ||
425 | for (i = 0; i < count; i++) { | |
426 | ret = crypto_unregister_alg(&algs[i]); | |
427 | if (ret) | |
428 | pr_err("Failed to unregister %s %s: %d\n", | |
429 | algs[i].cra_driver_name, algs[i].cra_name, ret); | |
430 | } | |
431 | ||
432 | return 0; | |
433 | } | |
434 | EXPORT_SYMBOL_GPL(crypto_unregister_algs); | |
435 | ||
4cc7720c HX |
436 | int crypto_register_template(struct crypto_template *tmpl) |
437 | { | |
438 | struct crypto_template *q; | |
439 | int err = -EEXIST; | |
440 | ||
441 | down_write(&crypto_alg_sem); | |
442 | ||
002c77a4 JW |
443 | crypto_check_module_sig(tmpl->module); |
444 | ||
4cc7720c HX |
445 | list_for_each_entry(q, &crypto_template_list, list) { |
446 | if (q == tmpl) | |
447 | goto out; | |
448 | } | |
449 | ||
450 | list_add(&tmpl->list, &crypto_template_list); | |
2825982d | 451 | crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl); |
4cc7720c HX |
452 | err = 0; |
453 | out: | |
454 | up_write(&crypto_alg_sem); | |
455 | return err; | |
456 | } | |
457 | EXPORT_SYMBOL_GPL(crypto_register_template); | |
458 | ||
459 | void crypto_unregister_template(struct crypto_template *tmpl) | |
460 | { | |
461 | struct crypto_instance *inst; | |
b67bfe0d | 462 | struct hlist_node *n; |
4cc7720c | 463 | struct hlist_head *list; |
6bfd4809 | 464 | LIST_HEAD(users); |
4cc7720c HX |
465 | |
466 | down_write(&crypto_alg_sem); | |
467 | ||
468 | BUG_ON(list_empty(&tmpl->list)); | |
469 | list_del_init(&tmpl->list); | |
470 | ||
471 | list = &tmpl->instances; | |
b67bfe0d | 472 | hlist_for_each_entry(inst, list, list) { |
6bfd4809 | 473 | int err = crypto_remove_alg(&inst->alg, &users); |
0efcb8d5 | 474 | |
6bfd4809 | 475 | BUG_ON(err); |
4cc7720c HX |
476 | } |
477 | ||
2825982d HX |
478 | crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl); |
479 | ||
4cc7720c HX |
480 | up_write(&crypto_alg_sem); |
481 | ||
b67bfe0d | 482 | hlist_for_each_entry_safe(inst, n, list, list) { |
4cc7720c HX |
483 | BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1); |
484 | tmpl->free(inst); | |
485 | } | |
6bfd4809 | 486 | crypto_remove_final(&users); |
4cc7720c HX |
487 | } |
488 | EXPORT_SYMBOL_GPL(crypto_unregister_template); | |
489 | ||
490 | static struct crypto_template *__crypto_lookup_template(const char *name) | |
491 | { | |
492 | struct crypto_template *q, *tmpl = NULL; | |
493 | ||
494 | down_read(&crypto_alg_sem); | |
495 | list_for_each_entry(q, &crypto_template_list, list) { | |
496 | if (strcmp(q->name, name)) | |
497 | continue; | |
498 | if (unlikely(!crypto_tmpl_get(q))) | |
499 | continue; | |
500 | ||
501 | tmpl = q; | |
502 | break; | |
503 | } | |
504 | up_read(&crypto_alg_sem); | |
505 | ||
506 | return tmpl; | |
507 | } | |
508 | ||
509 | struct crypto_template *crypto_lookup_template(const char *name) | |
510 | { | |
4943ba16 KC |
511 | return try_then_request_module(__crypto_lookup_template(name), |
512 | "crypto-%s", name); | |
4cc7720c HX |
513 | } |
514 | EXPORT_SYMBOL_GPL(crypto_lookup_template); | |
515 | ||
516 | int crypto_register_instance(struct crypto_template *tmpl, | |
517 | struct crypto_instance *inst) | |
518 | { | |
73d3864a HX |
519 | struct crypto_larval *larval; |
520 | int err; | |
4cc7720c | 521 | |
4cc7720c HX |
522 | err = crypto_check_alg(&inst->alg); |
523 | if (err) | |
9c521a20 SM |
524 | return err; |
525 | ||
4cc7720c | 526 | inst->alg.cra_module = tmpl->module; |
64a947b1 | 527 | inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE; |
4cc7720c | 528 | |
34c9a0ff HX |
529 | if (unlikely(!crypto_mod_get(&inst->alg))) |
530 | return -EAGAIN; | |
531 | ||
4cc7720c HX |
532 | down_write(&crypto_alg_sem); |
533 | ||
73d3864a HX |
534 | larval = __crypto_register_alg(&inst->alg); |
535 | if (IS_ERR(larval)) | |
4cc7720c HX |
536 | goto unlock; |
537 | ||
538 | hlist_add_head(&inst->list, &tmpl->instances); | |
539 | inst->tmpl = tmpl; | |
540 | ||
541 | unlock: | |
542 | up_write(&crypto_alg_sem); | |
543 | ||
73d3864a HX |
544 | err = PTR_ERR(larval); |
545 | if (IS_ERR(larval)) | |
546 | goto err; | |
547 | ||
548 | crypto_wait_for_test(larval); | |
9c521a20 SM |
549 | |
550 | /* Remove instance if test failed */ | |
551 | if (!(inst->alg.cra_flags & CRYPTO_ALG_TESTED)) | |
552 | crypto_unregister_instance(inst); | |
73d3864a | 553 | err = 0; |
6bfd4809 | 554 | |
4cc7720c | 555 | err: |
9c521a20 | 556 | crypto_mod_put(&inst->alg); |
4cc7720c HX |
557 | return err; |
558 | } | |
559 | EXPORT_SYMBOL_GPL(crypto_register_instance); | |
ce3fd840 | 560 | |
87b16756 | 561 | int crypto_unregister_instance(struct crypto_instance *inst) |
ce3fd840 | 562 | { |
1f723710 | 563 | LIST_HEAD(list); |
ce3fd840 | 564 | |
ce3fd840 SK |
565 | down_write(&crypto_alg_sem); |
566 | ||
87b16756 | 567 | crypto_remove_spawns(&inst->alg, &list, NULL); |
1f723710 | 568 | crypto_remove_instance(inst, &list); |
ce3fd840 SK |
569 | |
570 | up_write(&crypto_alg_sem); | |
571 | ||
1f723710 | 572 | crypto_remove_final(&list); |
ce3fd840 SK |
573 | |
574 | return 0; | |
575 | } | |
576 | EXPORT_SYMBOL_GPL(crypto_unregister_instance); | |
4cc7720c | 577 | |
6bfd4809 | 578 | int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg, |
a73e6996 | 579 | struct crypto_instance *inst, u32 mask) |
6bfd4809 HX |
580 | { |
581 | int err = -EAGAIN; | |
582 | ||
583 | spawn->inst = inst; | |
a73e6996 | 584 | spawn->mask = mask; |
6bfd4809 HX |
585 | |
586 | down_write(&crypto_alg_sem); | |
587 | if (!crypto_is_moribund(alg)) { | |
588 | list_add(&spawn->list, &alg->cra_users); | |
589 | spawn->alg = alg; | |
590 | err = 0; | |
591 | } | |
592 | up_write(&crypto_alg_sem); | |
593 | ||
594 | return err; | |
595 | } | |
596 | EXPORT_SYMBOL_GPL(crypto_init_spawn); | |
597 | ||
97eedce1 HX |
598 | int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg, |
599 | struct crypto_instance *inst, | |
600 | const struct crypto_type *frontend) | |
601 | { | |
602 | int err = -EINVAL; | |
603 | ||
c614e109 | 604 | if ((alg->cra_flags ^ frontend->type) & frontend->maskset) |
97eedce1 HX |
605 | goto out; |
606 | ||
607 | spawn->frontend = frontend; | |
608 | err = crypto_init_spawn(spawn, alg, inst, frontend->maskset); | |
609 | ||
610 | out: | |
611 | return err; | |
612 | } | |
613 | EXPORT_SYMBOL_GPL(crypto_init_spawn2); | |
614 | ||
6bfd4809 HX |
615 | void crypto_drop_spawn(struct crypto_spawn *spawn) |
616 | { | |
7ede5a5b HX |
617 | if (!spawn->alg) |
618 | return; | |
619 | ||
6bfd4809 HX |
620 | down_write(&crypto_alg_sem); |
621 | list_del(&spawn->list); | |
622 | up_write(&crypto_alg_sem); | |
623 | } | |
624 | EXPORT_SYMBOL_GPL(crypto_drop_spawn); | |
625 | ||
97eedce1 | 626 | static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn) |
6bfd4809 HX |
627 | { |
628 | struct crypto_alg *alg; | |
629 | struct crypto_alg *alg2; | |
6bfd4809 HX |
630 | |
631 | down_read(&crypto_alg_sem); | |
632 | alg = spawn->alg; | |
633 | alg2 = alg; | |
634 | if (alg2) | |
635 | alg2 = crypto_mod_get(alg2); | |
636 | up_read(&crypto_alg_sem); | |
637 | ||
638 | if (!alg2) { | |
639 | if (alg) | |
640 | crypto_shoot_alg(alg); | |
641 | return ERR_PTR(-EAGAIN); | |
642 | } | |
643 | ||
97eedce1 HX |
644 | return alg; |
645 | } | |
646 | ||
647 | struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type, | |
648 | u32 mask) | |
649 | { | |
650 | struct crypto_alg *alg; | |
651 | struct crypto_tfm *tfm; | |
652 | ||
653 | alg = crypto_spawn_alg(spawn); | |
654 | if (IS_ERR(alg)) | |
655 | return ERR_CAST(alg); | |
656 | ||
2e306ee0 HX |
657 | tfm = ERR_PTR(-EINVAL); |
658 | if (unlikely((alg->cra_flags ^ type) & mask)) | |
659 | goto out_put_alg; | |
660 | ||
27d2a330 | 661 | tfm = __crypto_alloc_tfm(alg, type, mask); |
6bfd4809 | 662 | if (IS_ERR(tfm)) |
2e306ee0 HX |
663 | goto out_put_alg; |
664 | ||
665 | return tfm; | |
6bfd4809 | 666 | |
2e306ee0 HX |
667 | out_put_alg: |
668 | crypto_mod_put(alg); | |
6bfd4809 HX |
669 | return tfm; |
670 | } | |
671 | EXPORT_SYMBOL_GPL(crypto_spawn_tfm); | |
672 | ||
97eedce1 HX |
673 | void *crypto_spawn_tfm2(struct crypto_spawn *spawn) |
674 | { | |
675 | struct crypto_alg *alg; | |
676 | struct crypto_tfm *tfm; | |
677 | ||
678 | alg = crypto_spawn_alg(spawn); | |
679 | if (IS_ERR(alg)) | |
680 | return ERR_CAST(alg); | |
681 | ||
682 | tfm = crypto_create_tfm(alg, spawn->frontend); | |
683 | if (IS_ERR(tfm)) | |
684 | goto out_put_alg; | |
685 | ||
686 | return tfm; | |
687 | ||
688 | out_put_alg: | |
689 | crypto_mod_put(alg); | |
690 | return tfm; | |
691 | } | |
692 | EXPORT_SYMBOL_GPL(crypto_spawn_tfm2); | |
693 | ||
2825982d HX |
694 | int crypto_register_notifier(struct notifier_block *nb) |
695 | { | |
696 | return blocking_notifier_chain_register(&crypto_chain, nb); | |
697 | } | |
698 | EXPORT_SYMBOL_GPL(crypto_register_notifier); | |
699 | ||
700 | int crypto_unregister_notifier(struct notifier_block *nb) | |
701 | { | |
702 | return blocking_notifier_chain_unregister(&crypto_chain, nb); | |
703 | } | |
704 | EXPORT_SYMBOL_GPL(crypto_unregister_notifier); | |
705 | ||
ebc610e5 | 706 | struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb) |
7fed0bf2 | 707 | { |
39e1ee01 | 708 | struct rtattr *rta = tb[0]; |
ebc610e5 HX |
709 | struct crypto_attr_type *algt; |
710 | ||
711 | if (!rta) | |
712 | return ERR_PTR(-ENOENT); | |
713 | if (RTA_PAYLOAD(rta) < sizeof(*algt)) | |
714 | return ERR_PTR(-EINVAL); | |
39e1ee01 HX |
715 | if (rta->rta_type != CRYPTOA_TYPE) |
716 | return ERR_PTR(-EINVAL); | |
ebc610e5 HX |
717 | |
718 | algt = RTA_DATA(rta); | |
719 | ||
720 | return algt; | |
721 | } | |
722 | EXPORT_SYMBOL_GPL(crypto_get_attr_type); | |
723 | ||
724 | int crypto_check_attr_type(struct rtattr **tb, u32 type) | |
725 | { | |
726 | struct crypto_attr_type *algt; | |
727 | ||
728 | algt = crypto_get_attr_type(tb); | |
729 | if (IS_ERR(algt)) | |
730 | return PTR_ERR(algt); | |
731 | ||
732 | if ((algt->type ^ type) & algt->mask) | |
733 | return -EINVAL; | |
734 | ||
735 | return 0; | |
736 | } | |
737 | EXPORT_SYMBOL_GPL(crypto_check_attr_type); | |
738 | ||
68b6c7d6 | 739 | const char *crypto_attr_alg_name(struct rtattr *rta) |
ebc610e5 | 740 | { |
7fed0bf2 HX |
741 | struct crypto_attr_alg *alga; |
742 | ||
ebc610e5 HX |
743 | if (!rta) |
744 | return ERR_PTR(-ENOENT); | |
745 | if (RTA_PAYLOAD(rta) < sizeof(*alga)) | |
7fed0bf2 | 746 | return ERR_PTR(-EINVAL); |
39e1ee01 HX |
747 | if (rta->rta_type != CRYPTOA_ALG) |
748 | return ERR_PTR(-EINVAL); | |
7fed0bf2 HX |
749 | |
750 | alga = RTA_DATA(rta); | |
751 | alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0; | |
752 | ||
68b6c7d6 HX |
753 | return alga->name; |
754 | } | |
755 | EXPORT_SYMBOL_GPL(crypto_attr_alg_name); | |
756 | ||
d06854f0 HX |
757 | struct crypto_alg *crypto_attr_alg2(struct rtattr *rta, |
758 | const struct crypto_type *frontend, | |
759 | u32 type, u32 mask) | |
68b6c7d6 HX |
760 | { |
761 | const char *name; | |
68b6c7d6 HX |
762 | |
763 | name = crypto_attr_alg_name(rta); | |
68b6c7d6 | 764 | if (IS_ERR(name)) |
3e8afe35 | 765 | return ERR_CAST(name); |
68b6c7d6 | 766 | |
d06854f0 | 767 | return crypto_find_alg(name, frontend, type, mask); |
7fed0bf2 | 768 | } |
d06854f0 | 769 | EXPORT_SYMBOL_GPL(crypto_attr_alg2); |
3c09f17c HX |
770 | |
771 | int crypto_attr_u32(struct rtattr *rta, u32 *num) | |
772 | { | |
773 | struct crypto_attr_u32 *nu32; | |
774 | ||
775 | if (!rta) | |
776 | return -ENOENT; | |
777 | if (RTA_PAYLOAD(rta) < sizeof(*nu32)) | |
778 | return -EINVAL; | |
779 | if (rta->rta_type != CRYPTOA_U32) | |
780 | return -EINVAL; | |
781 | ||
782 | nu32 = RTA_DATA(rta); | |
783 | *num = nu32->num; | |
784 | ||
785 | return 0; | |
786 | } | |
787 | EXPORT_SYMBOL_GPL(crypto_attr_u32); | |
7fed0bf2 | 788 | |
70ec7bb9 HX |
789 | void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg, |
790 | unsigned int head) | |
7fed0bf2 HX |
791 | { |
792 | struct crypto_instance *inst; | |
70ec7bb9 | 793 | char *p; |
7fed0bf2 HX |
794 | int err; |
795 | ||
70ec7bb9 HX |
796 | p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn), |
797 | GFP_KERNEL); | |
798 | if (!p) | |
7fed0bf2 HX |
799 | return ERR_PTR(-ENOMEM); |
800 | ||
70ec7bb9 HX |
801 | inst = (void *)(p + head); |
802 | ||
7fed0bf2 HX |
803 | err = -ENAMETOOLONG; |
804 | if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name, | |
805 | alg->cra_name) >= CRYPTO_MAX_ALG_NAME) | |
806 | goto err_free_inst; | |
807 | ||
808 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", | |
809 | name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) | |
810 | goto err_free_inst; | |
811 | ||
70ec7bb9 HX |
812 | return p; |
813 | ||
814 | err_free_inst: | |
815 | kfree(p); | |
816 | return ERR_PTR(err); | |
817 | } | |
818 | EXPORT_SYMBOL_GPL(crypto_alloc_instance2); | |
819 | ||
820 | struct crypto_instance *crypto_alloc_instance(const char *name, | |
821 | struct crypto_alg *alg) | |
822 | { | |
823 | struct crypto_instance *inst; | |
824 | struct crypto_spawn *spawn; | |
825 | int err; | |
826 | ||
827 | inst = crypto_alloc_instance2(name, alg, 0); | |
828 | if (IS_ERR(inst)) | |
829 | goto out; | |
830 | ||
7fed0bf2 | 831 | spawn = crypto_instance_ctx(inst); |
a73e6996 HX |
832 | err = crypto_init_spawn(spawn, alg, inst, |
833 | CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); | |
7fed0bf2 HX |
834 | |
835 | if (err) | |
836 | goto err_free_inst; | |
837 | ||
838 | return inst; | |
839 | ||
840 | err_free_inst: | |
841 | kfree(inst); | |
70ec7bb9 HX |
842 | inst = ERR_PTR(err); |
843 | ||
844 | out: | |
845 | return inst; | |
7fed0bf2 HX |
846 | } |
847 | EXPORT_SYMBOL_GPL(crypto_alloc_instance); | |
848 | ||
b5b7f088 HX |
849 | void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen) |
850 | { | |
851 | INIT_LIST_HEAD(&queue->list); | |
852 | queue->backlog = &queue->list; | |
853 | queue->qlen = 0; | |
854 | queue->max_qlen = max_qlen; | |
855 | } | |
856 | EXPORT_SYMBOL_GPL(crypto_init_queue); | |
857 | ||
858 | int crypto_enqueue_request(struct crypto_queue *queue, | |
859 | struct crypto_async_request *request) | |
860 | { | |
861 | int err = -EINPROGRESS; | |
862 | ||
863 | if (unlikely(queue->qlen >= queue->max_qlen)) { | |
864 | err = -EBUSY; | |
865 | if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) | |
866 | goto out; | |
867 | if (queue->backlog == &queue->list) | |
868 | queue->backlog = &request->list; | |
869 | } | |
870 | ||
871 | queue->qlen++; | |
872 | list_add_tail(&request->list, &queue->list); | |
873 | ||
874 | out: | |
875 | return err; | |
876 | } | |
877 | EXPORT_SYMBOL_GPL(crypto_enqueue_request); | |
878 | ||
0c7d400f | 879 | void *__crypto_dequeue_request(struct crypto_queue *queue, unsigned int offset) |
b5b7f088 HX |
880 | { |
881 | struct list_head *request; | |
882 | ||
883 | if (unlikely(!queue->qlen)) | |
884 | return NULL; | |
885 | ||
886 | queue->qlen--; | |
887 | ||
888 | if (queue->backlog != &queue->list) | |
889 | queue->backlog = queue->backlog->next; | |
890 | ||
891 | request = queue->list.next; | |
892 | list_del(request); | |
893 | ||
0c7d400f HX |
894 | return (char *)list_entry(request, struct crypto_async_request, list) - |
895 | offset; | |
896 | } | |
897 | EXPORT_SYMBOL_GPL(__crypto_dequeue_request); | |
898 | ||
899 | struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue) | |
900 | { | |
901 | return __crypto_dequeue_request(queue, 0); | |
b5b7f088 HX |
902 | } |
903 | EXPORT_SYMBOL_GPL(crypto_dequeue_request); | |
904 | ||
905 | int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm) | |
906 | { | |
907 | struct crypto_async_request *req; | |
908 | ||
909 | list_for_each_entry(req, &queue->list, list) { | |
910 | if (req->tfm == tfm) | |
911 | return 1; | |
912 | } | |
913 | ||
914 | return 0; | |
915 | } | |
916 | EXPORT_SYMBOL_GPL(crypto_tfm_in_queue); | |
917 | ||
7613636d HX |
918 | static inline void crypto_inc_byte(u8 *a, unsigned int size) |
919 | { | |
920 | u8 *b = (a + size); | |
921 | u8 c; | |
922 | ||
923 | for (; size; size--) { | |
924 | c = *--b + 1; | |
925 | *b = c; | |
926 | if (c) | |
927 | break; | |
928 | } | |
929 | } | |
930 | ||
931 | void crypto_inc(u8 *a, unsigned int size) | |
932 | { | |
933 | __be32 *b = (__be32 *)(a + size); | |
934 | u32 c; | |
935 | ||
936 | for (; size >= 4; size -= 4) { | |
937 | c = be32_to_cpu(*--b) + 1; | |
938 | *b = cpu_to_be32(c); | |
939 | if (c) | |
940 | return; | |
941 | } | |
942 | ||
943 | crypto_inc_byte(a, size); | |
944 | } | |
945 | EXPORT_SYMBOL_GPL(crypto_inc); | |
946 | ||
947 | static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size) | |
948 | { | |
949 | for (; size; size--) | |
950 | *a++ ^= *b++; | |
951 | } | |
952 | ||
953 | void crypto_xor(u8 *dst, const u8 *src, unsigned int size) | |
954 | { | |
955 | u32 *a = (u32 *)dst; | |
956 | u32 *b = (u32 *)src; | |
957 | ||
958 | for (; size >= 4; size -= 4) | |
959 | *a++ ^= *b++; | |
960 | ||
961 | crypto_xor_byte((u8 *)a, (u8 *)b, size); | |
962 | } | |
963 | EXPORT_SYMBOL_GPL(crypto_xor); | |
964 | ||
38d21433 HX |
965 | unsigned int crypto_alg_extsize(struct crypto_alg *alg) |
966 | { | |
967 | return alg->cra_ctxsize; | |
968 | } | |
969 | EXPORT_SYMBOL_GPL(crypto_alg_extsize); | |
970 | ||
cce9e06d HX |
971 | static int __init crypto_algapi_init(void) |
972 | { | |
973 | crypto_init_proc(); | |
974 | return 0; | |
975 | } | |
976 | ||
977 | static void __exit crypto_algapi_exit(void) | |
978 | { | |
979 | crypto_exit_proc(); | |
980 | } | |
981 | ||
982 | module_init(crypto_algapi_init); | |
983 | module_exit(crypto_algapi_exit); | |
984 | ||
985 | MODULE_LICENSE("GPL"); | |
986 | MODULE_DESCRIPTION("Cryptographic algorithms API"); |