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