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