• 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/fips.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 
24 #include "internal.h"
25 
26 static LIST_HEAD(crypto_template_list);
27 
crypto_set_driver_name(struct crypto_alg * alg)28 static 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 
crypto_check_module_sig(struct module * mod)45 static inline void crypto_check_module_sig(struct module *mod)
46 {
47 	if (fips_enabled && mod && !module_sig_ok(mod))
48 		panic("Module %s signature verification failed in FIPS mode\n",
49 		      module_name(mod));
50 }
51 
crypto_check_alg(struct crypto_alg * alg)52 static int crypto_check_alg(struct crypto_alg *alg)
53 {
54 	crypto_check_module_sig(alg->cra_module);
55 
56 	if (alg->cra_alignmask & (alg->cra_alignmask + 1))
57 		return -EINVAL;
58 
59 	if (alg->cra_blocksize > PAGE_SIZE / 8)
60 		return -EINVAL;
61 
62 	if (alg->cra_priority < 0)
63 		return -EINVAL;
64 
65 	atomic_set(&alg->cra_refcnt, 1);
66 
67 	return crypto_set_driver_name(alg);
68 }
69 
crypto_free_instance(struct crypto_instance * inst)70 static 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 
crypto_destroy_instance(struct crypto_alg * alg)80 static void crypto_destroy_instance(struct crypto_alg *alg)
81 {
82 	struct crypto_instance *inst = (void *)alg;
83 	struct crypto_template *tmpl = inst->tmpl;
84 
85 	crypto_free_instance(inst);
86 	crypto_tmpl_put(tmpl);
87 }
88 
crypto_more_spawns(struct crypto_alg * alg,struct list_head * stack,struct list_head * top,struct list_head * secondary_spawns)89 static 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 
crypto_remove_instance(struct crypto_instance * inst,struct list_head * list)112 static void crypto_remove_instance(struct crypto_instance *inst,
113 				   struct list_head *list)
114 {
115 	struct crypto_template *tmpl = inst->tmpl;
116 
117 	if (crypto_is_dead(&inst->alg))
118 		return;
119 
120 	inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
121 	if (hlist_unhashed(&inst->list))
122 		return;
123 
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 
132 	BUG_ON(!list_empty(&inst->alg.cra_users));
133 }
134 
crypto_remove_spawns(struct crypto_alg * alg,struct list_head * list,struct crypto_alg * nalg)135 void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
136 			  struct crypto_alg *nalg)
137 {
138 	u32 new_type = (nalg ?: alg)->cra_flags;
139 	struct crypto_spawn *spawn, *n;
140 	LIST_HEAD(secondary_spawns);
141 	struct list_head *spawns;
142 	LIST_HEAD(stack);
143 	LIST_HEAD(top);
144 
145 	spawns = &alg->cra_users;
146 	list_for_each_entry_safe(spawn, n, spawns, list) {
147 		if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
148 			continue;
149 
150 		list_move(&spawn->list, &top);
151 	}
152 
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;
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;
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
191 			crypto_remove_instance(spawn->inst, list);
192 	}
193 }
194 EXPORT_SYMBOL_GPL(crypto_remove_spawns);
195 
__crypto_register_alg(struct crypto_alg * alg)196 static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
197 {
198 	struct crypto_alg *q;
199 	struct crypto_larval *larval;
200 	int ret = -EAGAIN;
201 
202 	if (crypto_is_dead(alg))
203 		goto err;
204 
205 	INIT_LIST_HEAD(&alg->cra_users);
206 
207 	/* No cheating! */
208 	alg->cra_flags &= ~CRYPTO_ALG_TESTED;
209 
210 	ret = -EEXIST;
211 
212 	list_for_each_entry(q, &crypto_alg_list, cra_list) {
213 		if (q == alg)
214 			goto err;
215 
216 		if (crypto_is_moribund(q))
217 			continue;
218 
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 
248 out:
249 	return larval;
250 
251 free_larval:
252 	kfree(larval);
253 err:
254 	larval = ERR_PTR(ret);
255 	goto out;
256 }
257 
crypto_alg_tested(const char * name,int err)258 void 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) {
267 		if (crypto_is_moribund(q) || !crypto_is_larval(q))
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 
279 found:
280 	q->cra_flags |= CRYPTO_ALG_DEAD;
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;
290 
291 		if (crypto_is_moribund(q))
292 			continue;
293 
294 		if (crypto_is_larval(q)) {
295 			struct crypto_larval *larval = (void *)q;
296 
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 			 */
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;
308 			if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
309 				continue;
310 			if (!crypto_mod_get(alg))
311 				continue;
312 
313 			larval->adult = alg;
314 			continue;
315 		}
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 
324 		crypto_remove_spawns(q, &list, alg);
325 	}
326 
327 complete:
328 	complete_all(&test->completion);
329 
330 unlock:
331 	up_write(&crypto_alg_sem);
332 
333 	crypto_remove_final(&list);
334 }
335 EXPORT_SYMBOL_GPL(crypto_alg_tested);
336 
crypto_remove_final(struct list_head * list)337 void crypto_remove_final(struct list_head *list)
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 }
347 EXPORT_SYMBOL_GPL(crypto_remove_final);
348 
crypto_wait_for_test(struct crypto_larval * larval)349 static 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 
360 	err = wait_for_completion_killable(&larval->completion);
361 	WARN_ON(err);
362 
363 out:
364 	crypto_larval_kill(&larval->alg);
365 }
366 
crypto_register_alg(struct crypto_alg * alg)367 int crypto_register_alg(struct crypto_alg *alg)
368 {
369 	struct crypto_larval *larval;
370 	int err;
371 
372 	alg->cra_flags &= ~CRYPTO_ALG_DEAD;
373 	err = crypto_check_alg(alg);
374 	if (err)
375 		return err;
376 
377 	down_write(&crypto_alg_sem);
378 	larval = __crypto_register_alg(alg);
379 	up_write(&crypto_alg_sem);
380 
381 	if (IS_ERR(larval))
382 		return PTR_ERR(larval);
383 
384 	crypto_wait_for_test(larval);
385 	return 0;
386 }
387 EXPORT_SYMBOL_GPL(crypto_register_alg);
388 
crypto_remove_alg(struct crypto_alg * alg,struct list_head * list)389 static 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);
398 	crypto_remove_spawns(alg, list, NULL);
399 
400 	return 0;
401 }
402 
crypto_unregister_alg(struct crypto_alg * alg)403 int crypto_unregister_alg(struct crypto_alg *alg)
404 {
405 	int ret;
406 	LIST_HEAD(list);
407 
408 	down_write(&crypto_alg_sem);
409 	ret = crypto_remove_alg(alg, &list);
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 
419 	crypto_remove_final(&list);
420 	return 0;
421 }
422 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
423 
crypto_register_algs(struct crypto_alg * algs,int count)424 int 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 
436 err:
437 	for (--i; i >= 0; --i)
438 		crypto_unregister_alg(&algs[i]);
439 
440 	return ret;
441 }
442 EXPORT_SYMBOL_GPL(crypto_register_algs);
443 
crypto_unregister_algs(struct crypto_alg * algs,int count)444 int 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 }
457 EXPORT_SYMBOL_GPL(crypto_unregister_algs);
458 
crypto_register_template(struct crypto_template * tmpl)459 int 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 
466 	crypto_check_module_sig(tmpl->module);
467 
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);
474 	crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
475 	err = 0;
476 out:
477 	up_write(&crypto_alg_sem);
478 	return err;
479 }
480 EXPORT_SYMBOL_GPL(crypto_register_template);
481 
crypto_unregister_template(struct crypto_template * tmpl)482 void crypto_unregister_template(struct crypto_template *tmpl)
483 {
484 	struct crypto_instance *inst;
485 	struct hlist_node *n;
486 	struct hlist_head *list;
487 	LIST_HEAD(users);
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;
495 	hlist_for_each_entry(inst, list, list) {
496 		int err = crypto_remove_alg(&inst->alg, &users);
497 
498 		BUG_ON(err);
499 	}
500 
501 	crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
502 
503 	up_write(&crypto_alg_sem);
504 
505 	hlist_for_each_entry_safe(inst, n, list, list) {
506 		BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
507 		crypto_free_instance(inst);
508 	}
509 	crypto_remove_final(&users);
510 }
511 EXPORT_SYMBOL_GPL(crypto_unregister_template);
512 
__crypto_lookup_template(const char * name)513 static 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 
crypto_lookup_template(const char * name)532 struct crypto_template *crypto_lookup_template(const char *name)
533 {
534 	return try_then_request_module(__crypto_lookup_template(name),
535 				       "crypto-%s", name);
536 }
537 EXPORT_SYMBOL_GPL(crypto_lookup_template);
538 
crypto_register_instance(struct crypto_template * tmpl,struct crypto_instance * inst)539 int crypto_register_instance(struct crypto_template *tmpl,
540 			     struct crypto_instance *inst)
541 {
542 	struct crypto_larval *larval;
543 	int err;
544 
545 	err = crypto_check_alg(&inst->alg);
546 	if (err)
547 		return err;
548 
549 	inst->alg.cra_module = tmpl->module;
550 	inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
551 
552 	if (unlikely(!crypto_mod_get(&inst->alg)))
553 		return -EAGAIN;
554 
555 	down_write(&crypto_alg_sem);
556 
557 	larval = __crypto_register_alg(&inst->alg);
558 	if (IS_ERR(larval))
559 		goto unlock;
560 
561 	hlist_add_head(&inst->list, &tmpl->instances);
562 	inst->tmpl = tmpl;
563 
564 unlock:
565 	up_write(&crypto_alg_sem);
566 
567 	err = PTR_ERR(larval);
568 	if (IS_ERR(larval))
569 		goto err;
570 
571 	crypto_wait_for_test(larval);
572 
573 	/* Remove instance if test failed */
574 	if (!(inst->alg.cra_flags & CRYPTO_ALG_TESTED))
575 		crypto_unregister_instance(inst);
576 	err = 0;
577 
578 err:
579 	crypto_mod_put(&inst->alg);
580 	return err;
581 }
582 EXPORT_SYMBOL_GPL(crypto_register_instance);
583 
crypto_unregister_instance(struct crypto_instance * inst)584 int crypto_unregister_instance(struct crypto_instance *inst)
585 {
586 	LIST_HEAD(list);
587 
588 	down_write(&crypto_alg_sem);
589 
590 	crypto_remove_spawns(&inst->alg, &list, NULL);
591 	crypto_remove_instance(inst, &list);
592 
593 	up_write(&crypto_alg_sem);
594 
595 	crypto_remove_final(&list);
596 
597 	return 0;
598 }
599 EXPORT_SYMBOL_GPL(crypto_unregister_instance);
600 
crypto_init_spawn(struct crypto_spawn * spawn,struct crypto_alg * alg,struct crypto_instance * inst,u32 mask)601 int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
602 		      struct crypto_instance *inst, u32 mask)
603 {
604 	int err = -EAGAIN;
605 
606 	spawn->inst = inst;
607 	spawn->mask = mask;
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 }
619 EXPORT_SYMBOL_GPL(crypto_init_spawn);
620 
crypto_init_spawn2(struct crypto_spawn * spawn,struct crypto_alg * alg,struct crypto_instance * inst,const struct crypto_type * frontend)621 int 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 
627 	if ((alg->cra_flags ^ frontend->type) & frontend->maskset)
628 		goto out;
629 
630 	spawn->frontend = frontend;
631 	err = crypto_init_spawn(spawn, alg, inst, frontend->maskset);
632 
633 out:
634 	return err;
635 }
636 EXPORT_SYMBOL_GPL(crypto_init_spawn2);
637 
crypto_grab_spawn(struct crypto_spawn * spawn,const char * name,u32 type,u32 mask)638 int 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 }
652 EXPORT_SYMBOL_GPL(crypto_grab_spawn);
653 
crypto_drop_spawn(struct crypto_spawn * spawn)654 void crypto_drop_spawn(struct crypto_spawn *spawn)
655 {
656 	down_write(&crypto_alg_sem);
657 	if (spawn->alg)
658 		list_del(&spawn->list);
659 	up_write(&crypto_alg_sem);
660 }
661 EXPORT_SYMBOL_GPL(crypto_drop_spawn);
662 
crypto_spawn_alg(struct crypto_spawn * spawn)663 static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
664 {
665 	struct crypto_alg *alg;
666 
667 	down_read(&crypto_alg_sem);
668 	alg = spawn->alg;
669 	if (alg && !crypto_mod_get(alg)) {
670 		alg->cra_flags |= CRYPTO_ALG_DYING;
671 		alg = NULL;
672 	}
673 	up_read(&crypto_alg_sem);
674 
675 	return alg ?: ERR_PTR(-EAGAIN);
676 }
677 
crypto_spawn_tfm(struct crypto_spawn * spawn,u32 type,u32 mask)678 struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
679 				    u32 mask)
680 {
681 	struct crypto_alg *alg;
682 	struct crypto_tfm *tfm;
683 
684 	alg = crypto_spawn_alg(spawn);
685 	if (IS_ERR(alg))
686 		return ERR_CAST(alg);
687 
688 	tfm = ERR_PTR(-EINVAL);
689 	if (unlikely((alg->cra_flags ^ type) & mask))
690 		goto out_put_alg;
691 
692 	tfm = __crypto_alloc_tfm(alg, type, mask);
693 	if (IS_ERR(tfm))
694 		goto out_put_alg;
695 
696 	return tfm;
697 
698 out_put_alg:
699 	crypto_mod_put(alg);
700 	return tfm;
701 }
702 EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
703 
crypto_spawn_tfm2(struct crypto_spawn * spawn)704 void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
705 {
706 	struct crypto_alg *alg;
707 	struct crypto_tfm *tfm;
708 
709 	alg = crypto_spawn_alg(spawn);
710 	if (IS_ERR(alg))
711 		return ERR_CAST(alg);
712 
713 	tfm = crypto_create_tfm(alg, spawn->frontend);
714 	if (IS_ERR(tfm))
715 		goto out_put_alg;
716 
717 	return tfm;
718 
719 out_put_alg:
720 	crypto_mod_put(alg);
721 	return tfm;
722 }
723 EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
724 
crypto_register_notifier(struct notifier_block * nb)725 int crypto_register_notifier(struct notifier_block *nb)
726 {
727 	return blocking_notifier_chain_register(&crypto_chain, nb);
728 }
729 EXPORT_SYMBOL_GPL(crypto_register_notifier);
730 
crypto_unregister_notifier(struct notifier_block * nb)731 int crypto_unregister_notifier(struct notifier_block *nb)
732 {
733 	return blocking_notifier_chain_unregister(&crypto_chain, nb);
734 }
735 EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
736 
crypto_get_attr_type(struct rtattr ** tb)737 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
738 {
739 	struct rtattr *rta = tb[0];
740 	struct crypto_attr_type *algt;
741 
742 	if (!rta)
743 		return ERR_PTR(-ENOENT);
744 	if (RTA_PAYLOAD(rta) < sizeof(*algt))
745 		return ERR_PTR(-EINVAL);
746 	if (rta->rta_type != CRYPTOA_TYPE)
747 		return ERR_PTR(-EINVAL);
748 
749 	algt = RTA_DATA(rta);
750 
751 	return algt;
752 }
753 EXPORT_SYMBOL_GPL(crypto_get_attr_type);
754 
crypto_check_attr_type(struct rtattr ** tb,u32 type)755 int crypto_check_attr_type(struct rtattr **tb, u32 type)
756 {
757 	struct crypto_attr_type *algt;
758 
759 	algt = crypto_get_attr_type(tb);
760 	if (IS_ERR(algt))
761 		return PTR_ERR(algt);
762 
763 	if ((algt->type ^ type) & algt->mask)
764 		return -EINVAL;
765 
766 	return 0;
767 }
768 EXPORT_SYMBOL_GPL(crypto_check_attr_type);
769 
crypto_attr_alg_name(struct rtattr * rta)770 const char *crypto_attr_alg_name(struct rtattr *rta)
771 {
772 	struct crypto_attr_alg *alga;
773 
774 	if (!rta)
775 		return ERR_PTR(-ENOENT);
776 	if (RTA_PAYLOAD(rta) < sizeof(*alga))
777 		return ERR_PTR(-EINVAL);
778 	if (rta->rta_type != CRYPTOA_ALG)
779 		return ERR_PTR(-EINVAL);
780 
781 	alga = RTA_DATA(rta);
782 	alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
783 
784 	return alga->name;
785 }
786 EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
787 
crypto_attr_alg2(struct rtattr * rta,const struct crypto_type * frontend,u32 type,u32 mask)788 struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
789 				    const struct crypto_type *frontend,
790 				    u32 type, u32 mask)
791 {
792 	const char *name;
793 
794 	name = crypto_attr_alg_name(rta);
795 	if (IS_ERR(name))
796 		return ERR_CAST(name);
797 
798 	return crypto_find_alg(name, frontend, type, mask);
799 }
800 EXPORT_SYMBOL_GPL(crypto_attr_alg2);
801 
crypto_attr_u32(struct rtattr * rta,u32 * num)802 int crypto_attr_u32(struct rtattr *rta, u32 *num)
803 {
804 	struct crypto_attr_u32 *nu32;
805 
806 	if (!rta)
807 		return -ENOENT;
808 	if (RTA_PAYLOAD(rta) < sizeof(*nu32))
809 		return -EINVAL;
810 	if (rta->rta_type != CRYPTOA_U32)
811 		return -EINVAL;
812 
813 	nu32 = RTA_DATA(rta);
814 	*num = nu32->num;
815 
816 	return 0;
817 }
818 EXPORT_SYMBOL_GPL(crypto_attr_u32);
819 
crypto_alloc_instance2(const char * name,struct crypto_alg * alg,unsigned int head)820 void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
821 			     unsigned int head)
822 {
823 	struct crypto_instance *inst;
824 	char *p;
825 	int err;
826 
827 	p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
828 		    GFP_KERNEL);
829 	if (!p)
830 		return ERR_PTR(-ENOMEM);
831 
832 	inst = (void *)(p + head);
833 
834 	err = -ENAMETOOLONG;
835 	if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
836 		     alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
837 		goto err_free_inst;
838 
839 	if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
840 		     name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
841 		goto err_free_inst;
842 
843 	return p;
844 
845 err_free_inst:
846 	kfree(p);
847 	return ERR_PTR(err);
848 }
849 EXPORT_SYMBOL_GPL(crypto_alloc_instance2);
850 
crypto_alloc_instance(const char * name,struct crypto_alg * alg)851 struct crypto_instance *crypto_alloc_instance(const char *name,
852 					      struct crypto_alg *alg)
853 {
854 	struct crypto_instance *inst;
855 	struct crypto_spawn *spawn;
856 	int err;
857 
858 	inst = crypto_alloc_instance2(name, alg, 0);
859 	if (IS_ERR(inst))
860 		goto out;
861 
862 	spawn = crypto_instance_ctx(inst);
863 	err = crypto_init_spawn(spawn, alg, inst,
864 				CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
865 
866 	if (err)
867 		goto err_free_inst;
868 
869 	return inst;
870 
871 err_free_inst:
872 	kfree(inst);
873 	inst = ERR_PTR(err);
874 
875 out:
876 	return inst;
877 }
878 EXPORT_SYMBOL_GPL(crypto_alloc_instance);
879 
crypto_init_queue(struct crypto_queue * queue,unsigned int max_qlen)880 void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
881 {
882 	INIT_LIST_HEAD(&queue->list);
883 	queue->backlog = &queue->list;
884 	queue->qlen = 0;
885 	queue->max_qlen = max_qlen;
886 }
887 EXPORT_SYMBOL_GPL(crypto_init_queue);
888 
crypto_enqueue_request(struct crypto_queue * queue,struct crypto_async_request * request)889 int crypto_enqueue_request(struct crypto_queue *queue,
890 			   struct crypto_async_request *request)
891 {
892 	int err = -EINPROGRESS;
893 
894 	if (unlikely(queue->qlen >= queue->max_qlen)) {
895 		err = -EBUSY;
896 		if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
897 			goto out;
898 		if (queue->backlog == &queue->list)
899 			queue->backlog = &request->list;
900 	}
901 
902 	queue->qlen++;
903 	list_add_tail(&request->list, &queue->list);
904 
905 out:
906 	return err;
907 }
908 EXPORT_SYMBOL_GPL(crypto_enqueue_request);
909 
crypto_dequeue_request(struct crypto_queue * queue)910 struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
911 {
912 	struct list_head *request;
913 
914 	if (unlikely(!queue->qlen))
915 		return NULL;
916 
917 	queue->qlen--;
918 
919 	if (queue->backlog != &queue->list)
920 		queue->backlog = queue->backlog->next;
921 
922 	request = queue->list.next;
923 	list_del(request);
924 
925 	return list_entry(request, struct crypto_async_request, list);
926 }
927 EXPORT_SYMBOL_GPL(crypto_dequeue_request);
928 
crypto_tfm_in_queue(struct crypto_queue * queue,struct crypto_tfm * tfm)929 int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
930 {
931 	struct crypto_async_request *req;
932 
933 	list_for_each_entry(req, &queue->list, list) {
934 		if (req->tfm == tfm)
935 			return 1;
936 	}
937 
938 	return 0;
939 }
940 EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
941 
crypto_inc_byte(u8 * a,unsigned int size)942 static inline void crypto_inc_byte(u8 *a, unsigned int size)
943 {
944 	u8 *b = (a + size);
945 	u8 c;
946 
947 	for (; size; size--) {
948 		c = *--b + 1;
949 		*b = c;
950 		if (c)
951 			break;
952 	}
953 }
954 
crypto_inc(u8 * a,unsigned int size)955 void crypto_inc(u8 *a, unsigned int size)
956 {
957 	__be32 *b = (__be32 *)(a + size);
958 	u32 c;
959 
960 	for (; size >= 4; size -= 4) {
961 		c = be32_to_cpu(*--b) + 1;
962 		*b = cpu_to_be32(c);
963 		if (c)
964 			return;
965 	}
966 
967 	crypto_inc_byte(a, size);
968 }
969 EXPORT_SYMBOL_GPL(crypto_inc);
970 
crypto_xor_byte(u8 * a,const u8 * b,unsigned int size)971 static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
972 {
973 	for (; size; size--)
974 		*a++ ^= *b++;
975 }
976 
crypto_xor(u8 * dst,const u8 * src,unsigned int size)977 void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
978 {
979 	u32 *a = (u32 *)dst;
980 	u32 *b = (u32 *)src;
981 
982 	for (; size >= 4; size -= 4)
983 		*a++ ^= *b++;
984 
985 	crypto_xor_byte((u8 *)a, (u8 *)b, size);
986 }
987 EXPORT_SYMBOL_GPL(crypto_xor);
988 
crypto_alg_extsize(struct crypto_alg * alg)989 unsigned int crypto_alg_extsize(struct crypto_alg *alg)
990 {
991 	return alg->cra_ctxsize +
992 	       (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
993 }
994 EXPORT_SYMBOL_GPL(crypto_alg_extsize);
995 
crypto_algapi_init(void)996 static int __init crypto_algapi_init(void)
997 {
998 	crypto_init_proc();
999 	return 0;
1000 }
1001 
crypto_algapi_exit(void)1002 static void __exit crypto_algapi_exit(void)
1003 {
1004 	crypto_exit_proc();
1005 }
1006 
1007 module_init(crypto_algapi_init);
1008 module_exit(crypto_algapi_exit);
1009 
1010 MODULE_LICENSE("GPL");
1011 MODULE_DESCRIPTION("Cryptographic algorithms API");
1012