crypto: cryptomgr - Add test infrastructure
[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
4cc7720c
HX
24static LIST_HEAD(crypto_template_list);
25
492e2b63 26void crypto_larval_error(const char *name, u32 type, u32 mask)
2825982d
HX
27{
28 struct crypto_alg *alg;
29
c51b6c81 30 alg = crypto_alg_lookup(name, type, mask);
2825982d
HX
31
32 if (alg) {
33 if (crypto_is_larval(alg)) {
34 struct crypto_larval *larval = (void *)alg;
fe3c5206 35 complete_all(&larval->completion);
2825982d
HX
36 }
37 crypto_mod_put(alg);
38 }
39}
40EXPORT_SYMBOL_GPL(crypto_larval_error);
41
cce9e06d
HX
42static inline int crypto_set_driver_name(struct crypto_alg *alg)
43{
44 static const char suffix[] = "-generic";
45 char *driver_name = alg->cra_driver_name;
46 int len;
47
48 if (*driver_name)
49 return 0;
50
51 len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
52 if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
53 return -ENAMETOOLONG;
54
55 memcpy(driver_name + len, suffix, sizeof(suffix));
56 return 0;
57}
58
4cc7720c 59static int crypto_check_alg(struct crypto_alg *alg)
cce9e06d 60{
cce9e06d
HX
61 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
62 return -EINVAL;
63
cce9e06d
HX
64 if (alg->cra_blocksize > PAGE_SIZE / 8)
65 return -EINVAL;
66
67 if (alg->cra_priority < 0)
68 return -EINVAL;
cce9e06d 69
4cc7720c
HX
70 return crypto_set_driver_name(alg);
71}
72
6bfd4809
HX
73static void crypto_destroy_instance(struct crypto_alg *alg)
74{
75 struct crypto_instance *inst = (void *)alg;
76 struct crypto_template *tmpl = inst->tmpl;
77
78 tmpl->free(inst);
79 crypto_tmpl_put(tmpl);
80}
81
a73e6996
HX
82static void crypto_remove_spawn(struct crypto_spawn *spawn,
83 struct list_head *list,
84 struct list_head *secondary_spawns)
6bfd4809 85{
a73e6996
HX
86 struct crypto_instance *inst = spawn->inst;
87 struct crypto_template *tmpl = inst->tmpl;
6bfd4809 88
a73e6996
HX
89 list_del_init(&spawn->list);
90 spawn->alg = NULL;
6bfd4809 91
a73e6996
HX
92 if (crypto_is_dead(&inst->alg))
93 return;
6bfd4809 94
a73e6996 95 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
38cb2419
HX
96 if (hlist_unhashed(&inst->list))
97 return;
98
a73e6996
HX
99 if (!tmpl || !crypto_tmpl_get(tmpl))
100 return;
101
102 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
103 list_move(&inst->alg.cra_list, list);
104 hlist_del(&inst->list);
105 inst->alg.cra_destroy = crypto_destroy_instance;
106
107 list_splice(&inst->alg.cra_users, secondary_spawns);
108}
109
110static void crypto_remove_spawns(struct list_head *spawns,
111 struct list_head *list, u32 new_type)
112{
113 struct crypto_spawn *spawn, *n;
114 LIST_HEAD(secondary_spawns);
6bfd4809 115
a73e6996
HX
116 list_for_each_entry_safe(spawn, n, spawns, list) {
117 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
6bfd4809
HX
118 continue;
119
a73e6996
HX
120 crypto_remove_spawn(spawn, list, &secondary_spawns);
121 }
6bfd4809 122
a73e6996
HX
123 while (!list_empty(&secondary_spawns)) {
124 list_for_each_entry_safe(spawn, n, &secondary_spawns, list)
125 crypto_remove_spawn(spawn, list, &secondary_spawns);
6bfd4809
HX
126 }
127}
128
129static int __crypto_register_alg(struct crypto_alg *alg,
130 struct list_head *list)
4cc7720c
HX
131{
132 struct crypto_alg *q;
6bfd4809
HX
133 int ret = -EAGAIN;
134
135 if (crypto_is_dead(alg))
136 goto out;
137
138 INIT_LIST_HEAD(&alg->cra_users);
139
140 ret = -EEXIST;
4cc7720c 141
2825982d 142 atomic_set(&alg->cra_refcnt, 1);
cce9e06d 143 list_for_each_entry(q, &crypto_alg_list, cra_list) {
4cc7720c 144 if (q == alg)
cce9e06d 145 goto out;
6bfd4809
HX
146
147 if (crypto_is_moribund(q))
148 continue;
149
150 if (crypto_is_larval(q)) {
2825982d
HX
151 struct crypto_larval *larval = (void *)q;
152
d8058480
HX
153 /*
154 * Check to see if either our generic name or
155 * specific name can satisfy the name requested
156 * by the larval entry q.
157 */
6bfd4809
HX
158 if (strcmp(alg->cra_name, q->cra_name) &&
159 strcmp(alg->cra_driver_name, q->cra_name))
160 continue;
161
162 if (larval->adult)
163 continue;
492e2b63
HX
164 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
165 continue;
2825982d
HX
166 if (!crypto_mod_get(alg))
167 continue;
6bfd4809 168
2825982d 169 larval->adult = alg;
fe3c5206 170 complete_all(&larval->completion);
6bfd4809 171 continue;
2825982d 172 }
6bfd4809
HX
173
174 if (strcmp(alg->cra_name, q->cra_name))
175 continue;
176
177 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
178 q->cra_priority > alg->cra_priority)
179 continue;
180
a73e6996 181 crypto_remove_spawns(&q->cra_users, list, alg->cra_flags);
cce9e06d
HX
182 }
183
184 list_add(&alg->cra_list, &crypto_alg_list);
2825982d
HX
185
186 crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
4cc7720c 187 ret = 0;
2825982d 188
cce9e06d 189out:
cce9e06d
HX
190 return ret;
191}
4cc7720c 192
6bfd4809
HX
193static void crypto_remove_final(struct list_head *list)
194{
195 struct crypto_alg *alg;
196 struct crypto_alg *n;
197
198 list_for_each_entry_safe(alg, n, list, cra_list) {
199 list_del_init(&alg->cra_list);
200 crypto_alg_put(alg);
201 }
202}
203
4cc7720c
HX
204int crypto_register_alg(struct crypto_alg *alg)
205{
6bfd4809 206 LIST_HEAD(list);
4cc7720c
HX
207 int err;
208
209 err = crypto_check_alg(alg);
210 if (err)
211 return err;
212
213 down_write(&crypto_alg_sem);
6bfd4809 214 err = __crypto_register_alg(alg, &list);
4cc7720c
HX
215 up_write(&crypto_alg_sem);
216
6bfd4809 217 crypto_remove_final(&list);
4cc7720c
HX
218 return err;
219}
cce9e06d
HX
220EXPORT_SYMBOL_GPL(crypto_register_alg);
221
6bfd4809
HX
222static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
223{
224 if (unlikely(list_empty(&alg->cra_list)))
225 return -ENOENT;
226
227 alg->cra_flags |= CRYPTO_ALG_DEAD;
228
229 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
230 list_del_init(&alg->cra_list);
a73e6996 231 crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags);
6bfd4809
HX
232
233 return 0;
234}
235
cce9e06d
HX
236int crypto_unregister_alg(struct crypto_alg *alg)
237{
6bfd4809
HX
238 int ret;
239 LIST_HEAD(list);
cce9e06d
HX
240
241 down_write(&crypto_alg_sem);
6bfd4809 242 ret = crypto_remove_alg(alg, &list);
cce9e06d
HX
243 up_write(&crypto_alg_sem);
244
245 if (ret)
246 return ret;
247
248 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
249 if (alg->cra_destroy)
250 alg->cra_destroy(alg);
251
6bfd4809 252 crypto_remove_final(&list);
cce9e06d
HX
253 return 0;
254}
255EXPORT_SYMBOL_GPL(crypto_unregister_alg);
256
4cc7720c
HX
257int crypto_register_template(struct crypto_template *tmpl)
258{
259 struct crypto_template *q;
260 int err = -EEXIST;
261
262 down_write(&crypto_alg_sem);
263
264 list_for_each_entry(q, &crypto_template_list, list) {
265 if (q == tmpl)
266 goto out;
267 }
268
269 list_add(&tmpl->list, &crypto_template_list);
2825982d 270 crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
4cc7720c
HX
271 err = 0;
272out:
273 up_write(&crypto_alg_sem);
274 return err;
275}
276EXPORT_SYMBOL_GPL(crypto_register_template);
277
278void crypto_unregister_template(struct crypto_template *tmpl)
279{
280 struct crypto_instance *inst;
281 struct hlist_node *p, *n;
282 struct hlist_head *list;
6bfd4809 283 LIST_HEAD(users);
4cc7720c
HX
284
285 down_write(&crypto_alg_sem);
286
287 BUG_ON(list_empty(&tmpl->list));
288 list_del_init(&tmpl->list);
289
290 list = &tmpl->instances;
291 hlist_for_each_entry(inst, p, list, list) {
6bfd4809
HX
292 int err = crypto_remove_alg(&inst->alg, &users);
293 BUG_ON(err);
4cc7720c
HX
294 }
295
2825982d
HX
296 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
297
4cc7720c
HX
298 up_write(&crypto_alg_sem);
299
300 hlist_for_each_entry_safe(inst, p, n, list, list) {
301 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
302 tmpl->free(inst);
303 }
6bfd4809 304 crypto_remove_final(&users);
4cc7720c
HX
305}
306EXPORT_SYMBOL_GPL(crypto_unregister_template);
307
308static struct crypto_template *__crypto_lookup_template(const char *name)
309{
310 struct crypto_template *q, *tmpl = NULL;
311
312 down_read(&crypto_alg_sem);
313 list_for_each_entry(q, &crypto_template_list, list) {
314 if (strcmp(q->name, name))
315 continue;
316 if (unlikely(!crypto_tmpl_get(q)))
317 continue;
318
319 tmpl = q;
320 break;
321 }
322 up_read(&crypto_alg_sem);
323
324 return tmpl;
325}
326
327struct crypto_template *crypto_lookup_template(const char *name)
328{
329 return try_then_request_module(__crypto_lookup_template(name), name);
330}
331EXPORT_SYMBOL_GPL(crypto_lookup_template);
332
333int crypto_register_instance(struct crypto_template *tmpl,
334 struct crypto_instance *inst)
335{
6bfd4809 336 LIST_HEAD(list);
4cc7720c
HX
337 int err = -EINVAL;
338
4cc7720c
HX
339 err = crypto_check_alg(&inst->alg);
340 if (err)
341 goto err;
342
343 inst->alg.cra_module = tmpl->module;
344
345 down_write(&crypto_alg_sem);
346
6bfd4809 347 err = __crypto_register_alg(&inst->alg, &list);
4cc7720c
HX
348 if (err)
349 goto unlock;
350
351 hlist_add_head(&inst->list, &tmpl->instances);
352 inst->tmpl = tmpl;
353
354unlock:
355 up_write(&crypto_alg_sem);
356
6bfd4809
HX
357 crypto_remove_final(&list);
358
4cc7720c
HX
359err:
360 return err;
361}
362EXPORT_SYMBOL_GPL(crypto_register_instance);
363
6bfd4809 364int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
a73e6996 365 struct crypto_instance *inst, u32 mask)
6bfd4809
HX
366{
367 int err = -EAGAIN;
368
369 spawn->inst = inst;
a73e6996 370 spawn->mask = mask;
6bfd4809
HX
371
372 down_write(&crypto_alg_sem);
373 if (!crypto_is_moribund(alg)) {
374 list_add(&spawn->list, &alg->cra_users);
375 spawn->alg = alg;
376 err = 0;
377 }
378 up_write(&crypto_alg_sem);
379
380 return err;
381}
382EXPORT_SYMBOL_GPL(crypto_init_spawn);
383
384void crypto_drop_spawn(struct crypto_spawn *spawn)
385{
386 down_write(&crypto_alg_sem);
387 list_del(&spawn->list);
388 up_write(&crypto_alg_sem);
389}
390EXPORT_SYMBOL_GPL(crypto_drop_spawn);
391
2e306ee0
HX
392struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
393 u32 mask)
6bfd4809
HX
394{
395 struct crypto_alg *alg;
396 struct crypto_alg *alg2;
397 struct crypto_tfm *tfm;
398
399 down_read(&crypto_alg_sem);
400 alg = spawn->alg;
401 alg2 = alg;
402 if (alg2)
403 alg2 = crypto_mod_get(alg2);
404 up_read(&crypto_alg_sem);
405
406 if (!alg2) {
407 if (alg)
408 crypto_shoot_alg(alg);
409 return ERR_PTR(-EAGAIN);
410 }
411
2e306ee0
HX
412 tfm = ERR_PTR(-EINVAL);
413 if (unlikely((alg->cra_flags ^ type) & mask))
414 goto out_put_alg;
415
27d2a330 416 tfm = __crypto_alloc_tfm(alg, type, mask);
6bfd4809 417 if (IS_ERR(tfm))
2e306ee0
HX
418 goto out_put_alg;
419
420 return tfm;
6bfd4809 421
2e306ee0
HX
422out_put_alg:
423 crypto_mod_put(alg);
6bfd4809
HX
424 return tfm;
425}
426EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
427
2825982d
HX
428int crypto_register_notifier(struct notifier_block *nb)
429{
430 return blocking_notifier_chain_register(&crypto_chain, nb);
431}
432EXPORT_SYMBOL_GPL(crypto_register_notifier);
433
434int crypto_unregister_notifier(struct notifier_block *nb)
435{
436 return blocking_notifier_chain_unregister(&crypto_chain, nb);
437}
438EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
439
ebc610e5 440struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
7fed0bf2 441{
39e1ee01 442 struct rtattr *rta = tb[0];
ebc610e5
HX
443 struct crypto_attr_type *algt;
444
445 if (!rta)
446 return ERR_PTR(-ENOENT);
447 if (RTA_PAYLOAD(rta) < sizeof(*algt))
448 return ERR_PTR(-EINVAL);
39e1ee01
HX
449 if (rta->rta_type != CRYPTOA_TYPE)
450 return ERR_PTR(-EINVAL);
ebc610e5
HX
451
452 algt = RTA_DATA(rta);
453
454 return algt;
455}
456EXPORT_SYMBOL_GPL(crypto_get_attr_type);
457
458int crypto_check_attr_type(struct rtattr **tb, u32 type)
459{
460 struct crypto_attr_type *algt;
461
462 algt = crypto_get_attr_type(tb);
463 if (IS_ERR(algt))
464 return PTR_ERR(algt);
465
466 if ((algt->type ^ type) & algt->mask)
467 return -EINVAL;
468
469 return 0;
470}
471EXPORT_SYMBOL_GPL(crypto_check_attr_type);
472
68b6c7d6 473const char *crypto_attr_alg_name(struct rtattr *rta)
ebc610e5 474{
7fed0bf2
HX
475 struct crypto_attr_alg *alga;
476
ebc610e5
HX
477 if (!rta)
478 return ERR_PTR(-ENOENT);
479 if (RTA_PAYLOAD(rta) < sizeof(*alga))
7fed0bf2 480 return ERR_PTR(-EINVAL);
39e1ee01
HX
481 if (rta->rta_type != CRYPTOA_ALG)
482 return ERR_PTR(-EINVAL);
7fed0bf2
HX
483
484 alga = RTA_DATA(rta);
485 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
486
68b6c7d6
HX
487 return alga->name;
488}
489EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
490
491struct crypto_alg *crypto_attr_alg(struct rtattr *rta, u32 type, u32 mask)
492{
493 const char *name;
494 int err;
495
496 name = crypto_attr_alg_name(rta);
497 err = PTR_ERR(name);
498 if (IS_ERR(name))
499 return ERR_PTR(err);
500
501 return crypto_alg_mod_lookup(name, type, mask);
7fed0bf2 502}
3c09f17c
HX
503EXPORT_SYMBOL_GPL(crypto_attr_alg);
504
505int crypto_attr_u32(struct rtattr *rta, u32 *num)
506{
507 struct crypto_attr_u32 *nu32;
508
509 if (!rta)
510 return -ENOENT;
511 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
512 return -EINVAL;
513 if (rta->rta_type != CRYPTOA_U32)
514 return -EINVAL;
515
516 nu32 = RTA_DATA(rta);
517 *num = nu32->num;
518
519 return 0;
520}
521EXPORT_SYMBOL_GPL(crypto_attr_u32);
7fed0bf2
HX
522
523struct crypto_instance *crypto_alloc_instance(const char *name,
524 struct crypto_alg *alg)
525{
526 struct crypto_instance *inst;
527 struct crypto_spawn *spawn;
528 int err;
529
530 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
531 if (!inst)
532 return ERR_PTR(-ENOMEM);
533
534 err = -ENAMETOOLONG;
535 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
536 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
537 goto err_free_inst;
538
539 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
540 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
541 goto err_free_inst;
542
543 spawn = crypto_instance_ctx(inst);
a73e6996
HX
544 err = crypto_init_spawn(spawn, alg, inst,
545 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
7fed0bf2
HX
546
547 if (err)
548 goto err_free_inst;
549
550 return inst;
551
552err_free_inst:
553 kfree(inst);
554 return ERR_PTR(err);
555}
556EXPORT_SYMBOL_GPL(crypto_alloc_instance);
557
b5b7f088
HX
558void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
559{
560 INIT_LIST_HEAD(&queue->list);
561 queue->backlog = &queue->list;
562 queue->qlen = 0;
563 queue->max_qlen = max_qlen;
564}
565EXPORT_SYMBOL_GPL(crypto_init_queue);
566
567int crypto_enqueue_request(struct crypto_queue *queue,
568 struct crypto_async_request *request)
569{
570 int err = -EINPROGRESS;
571
572 if (unlikely(queue->qlen >= queue->max_qlen)) {
573 err = -EBUSY;
574 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
575 goto out;
576 if (queue->backlog == &queue->list)
577 queue->backlog = &request->list;
578 }
579
580 queue->qlen++;
581 list_add_tail(&request->list, &queue->list);
582
583out:
584 return err;
585}
586EXPORT_SYMBOL_GPL(crypto_enqueue_request);
587
588struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
589{
590 struct list_head *request;
591
592 if (unlikely(!queue->qlen))
593 return NULL;
594
595 queue->qlen--;
596
597 if (queue->backlog != &queue->list)
598 queue->backlog = queue->backlog->next;
599
600 request = queue->list.next;
601 list_del(request);
602
603 return list_entry(request, struct crypto_async_request, list);
604}
605EXPORT_SYMBOL_GPL(crypto_dequeue_request);
606
607int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
608{
609 struct crypto_async_request *req;
610
611 list_for_each_entry(req, &queue->list, list) {
612 if (req->tfm == tfm)
613 return 1;
614 }
615
616 return 0;
617}
618EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
619
7613636d
HX
620static inline void crypto_inc_byte(u8 *a, unsigned int size)
621{
622 u8 *b = (a + size);
623 u8 c;
624
625 for (; size; size--) {
626 c = *--b + 1;
627 *b = c;
628 if (c)
629 break;
630 }
631}
632
633void crypto_inc(u8 *a, unsigned int size)
634{
635 __be32 *b = (__be32 *)(a + size);
636 u32 c;
637
638 for (; size >= 4; size -= 4) {
639 c = be32_to_cpu(*--b) + 1;
640 *b = cpu_to_be32(c);
641 if (c)
642 return;
643 }
644
645 crypto_inc_byte(a, size);
646}
647EXPORT_SYMBOL_GPL(crypto_inc);
648
649static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
650{
651 for (; size; size--)
652 *a++ ^= *b++;
653}
654
655void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
656{
657 u32 *a = (u32 *)dst;
658 u32 *b = (u32 *)src;
659
660 for (; size >= 4; size -= 4)
661 *a++ ^= *b++;
662
663 crypto_xor_byte((u8 *)a, (u8 *)b, size);
664}
665EXPORT_SYMBOL_GPL(crypto_xor);
666
cce9e06d
HX
667static int __init crypto_algapi_init(void)
668{
669 crypto_init_proc();
670 return 0;
671}
672
673static void __exit crypto_algapi_exit(void)
674{
675 crypto_exit_proc();
676}
677
678module_init(crypto_algapi_init);
679module_exit(crypto_algapi_exit);
680
681MODULE_LICENSE("GPL");
682MODULE_DESCRIPTION("Cryptographic algorithms API");