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