]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - crypto/algapi.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[mirror_ubuntu-bionic-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
4cc7720c
HX
67 return crypto_set_driver_name(alg);
68}
69
6bfd4809
HX
70static 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
79static 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
a73e6996 102static void crypto_remove_spawn(struct crypto_spawn *spawn,
2bf29016 103 struct list_head *list)
6bfd4809 104{
a73e6996
HX
105 struct crypto_instance *inst = spawn->inst;
106 struct crypto_template *tmpl = inst->tmpl;
6bfd4809 107
a73e6996
HX
108 if (crypto_is_dead(&inst->alg))
109 return;
6bfd4809 110
a73e6996 111 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
38cb2419
HX
112 if (hlist_unhashed(&inst->list))
113 return;
114
a73e6996
HX
115 if (!tmpl || !crypto_tmpl_get(tmpl))
116 return;
117
118 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
119 list_move(&inst->alg.cra_list, list);
120 hlist_del(&inst->list);
121 inst->alg.cra_destroy = crypto_destroy_instance;
122
2bf29016 123 BUG_ON(!list_empty(&inst->alg.cra_users));
a73e6996
HX
124}
125
89b596ba
SK
126void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
127 struct crypto_alg *nalg)
a73e6996 128{
2bf29016 129 u32 new_type = (nalg ?: alg)->cra_flags;
a73e6996
HX
130 struct crypto_spawn *spawn, *n;
131 LIST_HEAD(secondary_spawns);
2bf29016
HX
132 struct list_head *spawns;
133 LIST_HEAD(stack);
134 LIST_HEAD(top);
6bfd4809 135
2bf29016 136 spawns = &alg->cra_users;
a73e6996
HX
137 list_for_each_entry_safe(spawn, n, spawns, list) {
138 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
6bfd4809
HX
139 continue;
140
2bf29016 141 list_move(&spawn->list, &top);
a73e6996 142 }
6bfd4809 143
2bf29016
HX
144 spawns = &top;
145 do {
146 while (!list_empty(spawns)) {
147 struct crypto_instance *inst;
148
149 spawn = list_first_entry(spawns, struct crypto_spawn,
150 list);
151 inst = spawn->inst;
152
153 BUG_ON(&inst->alg == alg);
154
155 list_move(&spawn->list, &stack);
156
157 if (&inst->alg == nalg)
158 break;
159
160 spawn->alg = NULL;
161 spawns = &inst->alg.cra_users;
162 }
163 } while ((spawns = crypto_more_spawns(alg, &stack, &top,
164 &secondary_spawns)));
165
166 list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
167 if (spawn->alg)
168 list_move(&spawn->list, &spawn->alg->cra_users);
169 else
170 crypto_remove_spawn(spawn, list);
6bfd4809
HX
171 }
172}
89b596ba 173EXPORT_SYMBOL_GPL(crypto_remove_spawns);
6bfd4809 174
73d3864a 175static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
4cc7720c
HX
176{
177 struct crypto_alg *q;
73d3864a 178 struct crypto_larval *larval;
6bfd4809
HX
179 int ret = -EAGAIN;
180
181 if (crypto_is_dead(alg))
73d3864a 182 goto err;
6bfd4809
HX
183
184 INIT_LIST_HEAD(&alg->cra_users);
185
73d3864a
HX
186 /* No cheating! */
187 alg->cra_flags &= ~CRYPTO_ALG_TESTED;
188
6bfd4809 189 ret = -EEXIST;
4cc7720c 190
2825982d 191 atomic_set(&alg->cra_refcnt, 1);
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
HX
475 int err = crypto_remove_alg(&inst->alg, &users);
476 BUG_ON(err);
4cc7720c
HX
477 }
478
2825982d
HX
479 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
480
4cc7720c
HX
481 up_write(&crypto_alg_sem);
482
b67bfe0d 483 hlist_for_each_entry_safe(inst, n, list, list) {
4cc7720c
HX
484 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
485 tmpl->free(inst);
486 }
6bfd4809 487 crypto_remove_final(&users);
4cc7720c
HX
488}
489EXPORT_SYMBOL_GPL(crypto_unregister_template);
490
491static struct crypto_template *__crypto_lookup_template(const char *name)
492{
493 struct crypto_template *q, *tmpl = NULL;
494
495 down_read(&crypto_alg_sem);
496 list_for_each_entry(q, &crypto_template_list, list) {
497 if (strcmp(q->name, name))
498 continue;
499 if (unlikely(!crypto_tmpl_get(q)))
500 continue;
501
502 tmpl = q;
503 break;
504 }
505 up_read(&crypto_alg_sem);
506
507 return tmpl;
508}
509
510struct crypto_template *crypto_lookup_template(const char *name)
511{
1c8fca1d
KC
512 return try_then_request_module(__crypto_lookup_template(name), "%s",
513 name);
4cc7720c
HX
514}
515EXPORT_SYMBOL_GPL(crypto_lookup_template);
516
517int crypto_register_instance(struct crypto_template *tmpl,
518 struct crypto_instance *inst)
519{
73d3864a
HX
520 struct crypto_larval *larval;
521 int err;
4cc7720c 522
4cc7720c
HX
523 err = crypto_check_alg(&inst->alg);
524 if (err)
525 goto err;
526
527 inst->alg.cra_module = tmpl->module;
64a947b1 528 inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
4cc7720c
HX
529
530 down_write(&crypto_alg_sem);
531
73d3864a
HX
532 larval = __crypto_register_alg(&inst->alg);
533 if (IS_ERR(larval))
4cc7720c
HX
534 goto unlock;
535
536 hlist_add_head(&inst->list, &tmpl->instances);
537 inst->tmpl = tmpl;
538
539unlock:
540 up_write(&crypto_alg_sem);
541
73d3864a
HX
542 err = PTR_ERR(larval);
543 if (IS_ERR(larval))
544 goto err;
545
546 crypto_wait_for_test(larval);
547 err = 0;
6bfd4809 548
4cc7720c
HX
549err:
550 return err;
551}
552EXPORT_SYMBOL_GPL(crypto_register_instance);
ce3fd840
SK
553
554int crypto_unregister_instance(struct crypto_alg *alg)
555{
556 int err;
557 struct crypto_instance *inst = (void *)alg;
558 struct crypto_template *tmpl = inst->tmpl;
559 LIST_HEAD(users);
560
561 if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
562 return -EINVAL;
563
564 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
565
566 down_write(&crypto_alg_sem);
567
568 hlist_del_init(&inst->list);
569 err = crypto_remove_alg(alg, &users);
570
571 up_write(&crypto_alg_sem);
572
573 if (err)
574 return err;
575
576 tmpl->free(inst);
577 crypto_remove_final(&users);
578
579 return 0;
580}
581EXPORT_SYMBOL_GPL(crypto_unregister_instance);
4cc7720c 582
6bfd4809 583int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
a73e6996 584 struct crypto_instance *inst, u32 mask)
6bfd4809
HX
585{
586 int err = -EAGAIN;
587
588 spawn->inst = inst;
a73e6996 589 spawn->mask = mask;
6bfd4809
HX
590
591 down_write(&crypto_alg_sem);
592 if (!crypto_is_moribund(alg)) {
593 list_add(&spawn->list, &alg->cra_users);
594 spawn->alg = alg;
595 err = 0;
596 }
597 up_write(&crypto_alg_sem);
598
599 return err;
600}
601EXPORT_SYMBOL_GPL(crypto_init_spawn);
602
97eedce1
HX
603int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,
604 struct crypto_instance *inst,
605 const struct crypto_type *frontend)
606{
607 int err = -EINVAL;
608
c614e109 609 if ((alg->cra_flags ^ frontend->type) & frontend->maskset)
97eedce1
HX
610 goto out;
611
612 spawn->frontend = frontend;
613 err = crypto_init_spawn(spawn, alg, inst, frontend->maskset);
614
615out:
616 return err;
617}
618EXPORT_SYMBOL_GPL(crypto_init_spawn2);
619
6bfd4809
HX
620void crypto_drop_spawn(struct crypto_spawn *spawn)
621{
7ede5a5b
HX
622 if (!spawn->alg)
623 return;
624
6bfd4809
HX
625 down_write(&crypto_alg_sem);
626 list_del(&spawn->list);
627 up_write(&crypto_alg_sem);
628}
629EXPORT_SYMBOL_GPL(crypto_drop_spawn);
630
97eedce1 631static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
6bfd4809
HX
632{
633 struct crypto_alg *alg;
634 struct crypto_alg *alg2;
6bfd4809
HX
635
636 down_read(&crypto_alg_sem);
637 alg = spawn->alg;
638 alg2 = alg;
639 if (alg2)
640 alg2 = crypto_mod_get(alg2);
641 up_read(&crypto_alg_sem);
642
643 if (!alg2) {
644 if (alg)
645 crypto_shoot_alg(alg);
646 return ERR_PTR(-EAGAIN);
647 }
648
97eedce1
HX
649 return alg;
650}
651
652struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
653 u32 mask)
654{
655 struct crypto_alg *alg;
656 struct crypto_tfm *tfm;
657
658 alg = crypto_spawn_alg(spawn);
659 if (IS_ERR(alg))
660 return ERR_CAST(alg);
661
2e306ee0
HX
662 tfm = ERR_PTR(-EINVAL);
663 if (unlikely((alg->cra_flags ^ type) & mask))
664 goto out_put_alg;
665
27d2a330 666 tfm = __crypto_alloc_tfm(alg, type, mask);
6bfd4809 667 if (IS_ERR(tfm))
2e306ee0
HX
668 goto out_put_alg;
669
670 return tfm;
6bfd4809 671
2e306ee0
HX
672out_put_alg:
673 crypto_mod_put(alg);
6bfd4809
HX
674 return tfm;
675}
676EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
677
97eedce1
HX
678void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
679{
680 struct crypto_alg *alg;
681 struct crypto_tfm *tfm;
682
683 alg = crypto_spawn_alg(spawn);
684 if (IS_ERR(alg))
685 return ERR_CAST(alg);
686
687 tfm = crypto_create_tfm(alg, spawn->frontend);
688 if (IS_ERR(tfm))
689 goto out_put_alg;
690
691 return tfm;
692
693out_put_alg:
694 crypto_mod_put(alg);
695 return tfm;
696}
697EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
698
2825982d
HX
699int crypto_register_notifier(struct notifier_block *nb)
700{
701 return blocking_notifier_chain_register(&crypto_chain, nb);
702}
703EXPORT_SYMBOL_GPL(crypto_register_notifier);
704
705int crypto_unregister_notifier(struct notifier_block *nb)
706{
707 return blocking_notifier_chain_unregister(&crypto_chain, nb);
708}
709EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
710
ebc610e5 711struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
7fed0bf2 712{
39e1ee01 713 struct rtattr *rta = tb[0];
ebc610e5
HX
714 struct crypto_attr_type *algt;
715
716 if (!rta)
717 return ERR_PTR(-ENOENT);
718 if (RTA_PAYLOAD(rta) < sizeof(*algt))
719 return ERR_PTR(-EINVAL);
39e1ee01
HX
720 if (rta->rta_type != CRYPTOA_TYPE)
721 return ERR_PTR(-EINVAL);
ebc610e5
HX
722
723 algt = RTA_DATA(rta);
724
725 return algt;
726}
727EXPORT_SYMBOL_GPL(crypto_get_attr_type);
728
729int crypto_check_attr_type(struct rtattr **tb, u32 type)
730{
731 struct crypto_attr_type *algt;
732
733 algt = crypto_get_attr_type(tb);
734 if (IS_ERR(algt))
735 return PTR_ERR(algt);
736
737 if ((algt->type ^ type) & algt->mask)
738 return -EINVAL;
739
740 return 0;
741}
742EXPORT_SYMBOL_GPL(crypto_check_attr_type);
743
68b6c7d6 744const char *crypto_attr_alg_name(struct rtattr *rta)
ebc610e5 745{
7fed0bf2
HX
746 struct crypto_attr_alg *alga;
747
ebc610e5
HX
748 if (!rta)
749 return ERR_PTR(-ENOENT);
750 if (RTA_PAYLOAD(rta) < sizeof(*alga))
7fed0bf2 751 return ERR_PTR(-EINVAL);
39e1ee01
HX
752 if (rta->rta_type != CRYPTOA_ALG)
753 return ERR_PTR(-EINVAL);
7fed0bf2
HX
754
755 alga = RTA_DATA(rta);
756 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
757
68b6c7d6
HX
758 return alga->name;
759}
760EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
761
d06854f0
HX
762struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
763 const struct crypto_type *frontend,
764 u32 type, u32 mask)
68b6c7d6
HX
765{
766 const char *name;
68b6c7d6
HX
767
768 name = crypto_attr_alg_name(rta);
68b6c7d6 769 if (IS_ERR(name))
3e8afe35 770 return ERR_CAST(name);
68b6c7d6 771
d06854f0 772 return crypto_find_alg(name, frontend, type, mask);
7fed0bf2 773}
d06854f0 774EXPORT_SYMBOL_GPL(crypto_attr_alg2);
3c09f17c
HX
775
776int crypto_attr_u32(struct rtattr *rta, u32 *num)
777{
778 struct crypto_attr_u32 *nu32;
779
780 if (!rta)
781 return -ENOENT;
782 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
783 return -EINVAL;
784 if (rta->rta_type != CRYPTOA_U32)
785 return -EINVAL;
786
787 nu32 = RTA_DATA(rta);
788 *num = nu32->num;
789
790 return 0;
791}
792EXPORT_SYMBOL_GPL(crypto_attr_u32);
7fed0bf2 793
70ec7bb9
HX
794void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
795 unsigned int head)
7fed0bf2
HX
796{
797 struct crypto_instance *inst;
70ec7bb9 798 char *p;
7fed0bf2
HX
799 int err;
800
70ec7bb9
HX
801 p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
802 GFP_KERNEL);
803 if (!p)
7fed0bf2
HX
804 return ERR_PTR(-ENOMEM);
805
70ec7bb9
HX
806 inst = (void *)(p + head);
807
7fed0bf2
HX
808 err = -ENAMETOOLONG;
809 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
810 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
811 goto err_free_inst;
812
813 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
814 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
815 goto err_free_inst;
816
70ec7bb9
HX
817 return p;
818
819err_free_inst:
820 kfree(p);
821 return ERR_PTR(err);
822}
823EXPORT_SYMBOL_GPL(crypto_alloc_instance2);
824
825struct crypto_instance *crypto_alloc_instance(const char *name,
826 struct crypto_alg *alg)
827{
828 struct crypto_instance *inst;
829 struct crypto_spawn *spawn;
830 int err;
831
832 inst = crypto_alloc_instance2(name, alg, 0);
833 if (IS_ERR(inst))
834 goto out;
835
7fed0bf2 836 spawn = crypto_instance_ctx(inst);
a73e6996
HX
837 err = crypto_init_spawn(spawn, alg, inst,
838 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
7fed0bf2
HX
839
840 if (err)
841 goto err_free_inst;
842
843 return inst;
844
845err_free_inst:
846 kfree(inst);
70ec7bb9
HX
847 inst = ERR_PTR(err);
848
849out:
850 return inst;
7fed0bf2
HX
851}
852EXPORT_SYMBOL_GPL(crypto_alloc_instance);
853
b5b7f088
HX
854void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
855{
856 INIT_LIST_HEAD(&queue->list);
857 queue->backlog = &queue->list;
858 queue->qlen = 0;
859 queue->max_qlen = max_qlen;
860}
861EXPORT_SYMBOL_GPL(crypto_init_queue);
862
863int crypto_enqueue_request(struct crypto_queue *queue,
864 struct crypto_async_request *request)
865{
866 int err = -EINPROGRESS;
867
868 if (unlikely(queue->qlen >= queue->max_qlen)) {
869 err = -EBUSY;
870 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
871 goto out;
872 if (queue->backlog == &queue->list)
873 queue->backlog = &request->list;
874 }
875
876 queue->qlen++;
877 list_add_tail(&request->list, &queue->list);
878
879out:
880 return err;
881}
882EXPORT_SYMBOL_GPL(crypto_enqueue_request);
883
0c7d400f 884void *__crypto_dequeue_request(struct crypto_queue *queue, unsigned int offset)
b5b7f088
HX
885{
886 struct list_head *request;
887
888 if (unlikely(!queue->qlen))
889 return NULL;
890
891 queue->qlen--;
892
893 if (queue->backlog != &queue->list)
894 queue->backlog = queue->backlog->next;
895
896 request = queue->list.next;
897 list_del(request);
898
0c7d400f
HX
899 return (char *)list_entry(request, struct crypto_async_request, list) -
900 offset;
901}
902EXPORT_SYMBOL_GPL(__crypto_dequeue_request);
903
904struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
905{
906 return __crypto_dequeue_request(queue, 0);
b5b7f088
HX
907}
908EXPORT_SYMBOL_GPL(crypto_dequeue_request);
909
910int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
911{
912 struct crypto_async_request *req;
913
914 list_for_each_entry(req, &queue->list, list) {
915 if (req->tfm == tfm)
916 return 1;
917 }
918
919 return 0;
920}
921EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
922
7613636d
HX
923static inline void crypto_inc_byte(u8 *a, unsigned int size)
924{
925 u8 *b = (a + size);
926 u8 c;
927
928 for (; size; size--) {
929 c = *--b + 1;
930 *b = c;
931 if (c)
932 break;
933 }
934}
935
936void crypto_inc(u8 *a, unsigned int size)
937{
938 __be32 *b = (__be32 *)(a + size);
939 u32 c;
940
941 for (; size >= 4; size -= 4) {
942 c = be32_to_cpu(*--b) + 1;
943 *b = cpu_to_be32(c);
944 if (c)
945 return;
946 }
947
948 crypto_inc_byte(a, size);
949}
950EXPORT_SYMBOL_GPL(crypto_inc);
951
952static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
953{
954 for (; size; size--)
955 *a++ ^= *b++;
956}
957
958void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
959{
960 u32 *a = (u32 *)dst;
961 u32 *b = (u32 *)src;
962
963 for (; size >= 4; size -= 4)
964 *a++ ^= *b++;
965
966 crypto_xor_byte((u8 *)a, (u8 *)b, size);
967}
968EXPORT_SYMBOL_GPL(crypto_xor);
969
cce9e06d
HX
970static int __init crypto_algapi_init(void)
971{
972 crypto_init_proc();
973 return 0;
974}
975
976static void __exit crypto_algapi_exit(void)
977{
978 crypto_exit_proc();
979}
980
981module_init(crypto_algapi_init);
982module_exit(crypto_algapi_exit);
983
984MODULE_LICENSE("GPL");
985MODULE_DESCRIPTION("Cryptographic algorithms API");