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