blob: f1d0307b1d083f3e374dbb2db4bce45a0f483948 [file] [log] [blame]
Herbert Xucce9e062006-08-21 21:08:13 +10001/*
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
Herbert Xu6bfd4802006-09-21 11:39:29 +100013#include <linux/err.h>
Herbert Xucce9e062006-08-21 21:08:13 +100014#include <linux/errno.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
Herbert Xu4cc77202006-08-06 21:16:34 +100017#include <linux/list.h>
Herbert Xucce9e062006-08-21 21:08:13 +100018#include <linux/module.h>
Herbert Xu7fed0bf2006-08-06 23:10:45 +100019#include <linux/rtnetlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Herbert Xucce9e062006-08-21 21:08:13 +100021#include <linux/string.h>
22
23#include "internal.h"
24
Herbert Xu4cc77202006-08-06 21:16:34 +100025static LIST_HEAD(crypto_template_list);
26
Herbert Xucce9e062006-08-21 21:08:13 +100027static 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
Jarod Wilson002c77a2014-07-02 15:37:30 -040044static 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
Herbert Xu4cc77202006-08-06 21:16:34 +100054static int crypto_check_alg(struct crypto_alg *alg)
Herbert Xucce9e062006-08-21 21:08:13 +100055{
Jarod Wilson002c77a2014-07-02 15:37:30 -040056 crypto_check_module_sig(alg->cra_module);
57
Herbert Xucce9e062006-08-21 21:08:13 +100058 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
59 return -EINVAL;
60
Herbert Xucce9e062006-08-21 21:08:13 +100061 if (alg->cra_blocksize > PAGE_SIZE / 8)
62 return -EINVAL;
63
64 if (alg->cra_priority < 0)
65 return -EINVAL;
Herbert Xucce9e062006-08-21 21:08:13 +100066
Herbert Xu4cc77202006-08-06 21:16:34 +100067 return crypto_set_driver_name(alg);
68}
69
Herbert Xu6bfd4802006-09-21 11:39:29 +100070static 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
Herbert Xu2bf29012009-08-31 15:56:54 +100079static 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
Herbert Xu1f723712015-04-02 22:31:22 +0800102static void crypto_remove_instance(struct crypto_instance *inst,
103 struct list_head *list)
Herbert Xua73e6992007-04-08 21:31:36 +1000104{
Herbert Xua73e6992007-04-08 21:31:36 +1000105 struct crypto_template *tmpl = inst->tmpl;
106
Herbert Xua73e6992007-04-08 21:31:36 +1000107 if (crypto_is_dead(&inst->alg))
108 return;
109
110 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
Herbert Xu38cb2412007-11-17 22:09:38 +0800111 if (hlist_unhashed(&inst->list))
112 return;
113
Herbert Xua73e6992007-04-08 21:31:36 +1000114 if (!tmpl || !crypto_tmpl_get(tmpl))
115 return;
116
117 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
118 list_move(&inst->alg.cra_list, list);
119 hlist_del(&inst->list);
120 inst->alg.cra_destroy = crypto_destroy_instance;
121
Herbert Xu2bf29012009-08-31 15:56:54 +1000122 BUG_ON(!list_empty(&inst->alg.cra_users));
Herbert Xua73e6992007-04-08 21:31:36 +1000123}
124
Steffen Klassert89b596b2011-09-27 07:22:08 +0200125void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
126 struct crypto_alg *nalg)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000127{
Herbert Xu2bf29012009-08-31 15:56:54 +1000128 u32 new_type = (nalg ?: alg)->cra_flags;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000129 struct crypto_spawn *spawn, *n;
Herbert Xua73e6992007-04-08 21:31:36 +1000130 LIST_HEAD(secondary_spawns);
Herbert Xu2bf29012009-08-31 15:56:54 +1000131 struct list_head *spawns;
132 LIST_HEAD(stack);
133 LIST_HEAD(top);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000134
Herbert Xu2bf29012009-08-31 15:56:54 +1000135 spawns = &alg->cra_users;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000136 list_for_each_entry_safe(spawn, n, spawns, list) {
Herbert Xua73e6992007-04-08 21:31:36 +1000137 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000138 continue;
139
Herbert Xu2bf29012009-08-31 15:56:54 +1000140 list_move(&spawn->list, &top);
Herbert Xua73e6992007-04-08 21:31:36 +1000141 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000142
Herbert Xu2bf29012009-08-31 15:56:54 +1000143 spawns = &top;
144 do {
145 while (!list_empty(spawns)) {
146 struct crypto_instance *inst;
147
148 spawn = list_first_entry(spawns, struct crypto_spawn,
149 list);
150 inst = spawn->inst;
151
152 BUG_ON(&inst->alg == alg);
153
154 list_move(&spawn->list, &stack);
155
156 if (&inst->alg == nalg)
157 break;
158
159 spawn->alg = NULL;
160 spawns = &inst->alg.cra_users;
161 }
162 } while ((spawns = crypto_more_spawns(alg, &stack, &top,
163 &secondary_spawns)));
164
165 list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
166 if (spawn->alg)
167 list_move(&spawn->list, &spawn->alg->cra_users);
168 else
Herbert Xu1f723712015-04-02 22:31:22 +0800169 crypto_remove_instance(spawn->inst, list);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000170 }
171}
Steffen Klassert89b596b2011-09-27 07:22:08 +0200172EXPORT_SYMBOL_GPL(crypto_remove_spawns);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000173
Herbert Xu73d38642008-08-03 21:15:23 +0800174static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
Herbert Xu4cc77202006-08-06 21:16:34 +1000175{
176 struct crypto_alg *q;
Herbert Xu73d38642008-08-03 21:15:23 +0800177 struct crypto_larval *larval;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000178 int ret = -EAGAIN;
179
180 if (crypto_is_dead(alg))
Herbert Xu73d38642008-08-03 21:15:23 +0800181 goto err;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000182
183 INIT_LIST_HEAD(&alg->cra_users);
184
Herbert Xu73d38642008-08-03 21:15:23 +0800185 /* No cheating! */
186 alg->cra_flags &= ~CRYPTO_ALG_TESTED;
187
Herbert Xu6bfd4802006-09-21 11:39:29 +1000188 ret = -EEXIST;
Herbert Xu4cc77202006-08-06 21:16:34 +1000189
Herbert Xu28259822006-08-06 21:23:26 +1000190 atomic_set(&alg->cra_refcnt, 1);
Herbert Xucce9e062006-08-21 21:08:13 +1000191 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xu4cc77202006-08-06 21:16:34 +1000192 if (q == alg)
Herbert Xu73d38642008-08-03 21:15:23 +0800193 goto err;
194
Herbert Xub8e15992009-01-28 14:09:59 +1100195 if (crypto_is_moribund(q))
196 continue;
197
Herbert Xu73d38642008-08-03 21:15:23 +0800198 if (crypto_is_larval(q)) {
199 if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
200 goto err;
201 continue;
202 }
203
204 if (!strcmp(q->cra_driver_name, alg->cra_name) ||
205 !strcmp(q->cra_name, alg->cra_driver_name))
206 goto err;
207 }
208
209 larval = crypto_larval_alloc(alg->cra_name,
210 alg->cra_flags | CRYPTO_ALG_TESTED, 0);
211 if (IS_ERR(larval))
212 goto out;
213
214 ret = -ENOENT;
215 larval->adult = crypto_mod_get(alg);
216 if (!larval->adult)
217 goto free_larval;
218
219 atomic_set(&larval->alg.cra_refcnt, 1);
220 memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
221 CRYPTO_MAX_ALG_NAME);
222 larval->alg.cra_priority = alg->cra_priority;
223
224 list_add(&alg->cra_list, &crypto_alg_list);
225 list_add(&larval->alg.cra_list, &crypto_alg_list);
226
Richard Hartmann5357c6c2010-02-16 20:25:21 +0800227out:
Herbert Xu73d38642008-08-03 21:15:23 +0800228 return larval;
229
230free_larval:
231 kfree(larval);
232err:
233 larval = ERR_PTR(ret);
234 goto out;
235}
236
237void crypto_alg_tested(const char *name, int err)
238{
239 struct crypto_larval *test;
240 struct crypto_alg *alg;
241 struct crypto_alg *q;
242 LIST_HEAD(list);
243
244 down_write(&crypto_alg_sem);
245 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xub8e15992009-01-28 14:09:59 +1100246 if (crypto_is_moribund(q) || !crypto_is_larval(q))
Herbert Xu73d38642008-08-03 21:15:23 +0800247 continue;
248
249 test = (struct crypto_larval *)q;
250
251 if (!strcmp(q->cra_driver_name, name))
252 goto found;
253 }
254
255 printk(KERN_ERR "alg: Unexpected test result for %s: %d\n", name, err);
256 goto unlock;
257
258found:
Herbert Xub8e15992009-01-28 14:09:59 +1100259 q->cra_flags |= CRYPTO_ALG_DEAD;
Herbert Xu73d38642008-08-03 21:15:23 +0800260 alg = test->adult;
261 if (err || list_empty(&alg->cra_list))
262 goto complete;
263
264 alg->cra_flags |= CRYPTO_ALG_TESTED;
265
266 list_for_each_entry(q, &crypto_alg_list, cra_list) {
267 if (q == alg)
268 continue;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000269
270 if (crypto_is_moribund(q))
271 continue;
272
273 if (crypto_is_larval(q)) {
Herbert Xu28259822006-08-06 21:23:26 +1000274 struct crypto_larval *larval = (void *)q;
275
Herbert Xud8058482007-10-02 22:27:29 +0800276 /*
277 * Check to see if either our generic name or
278 * specific name can satisfy the name requested
279 * by the larval entry q.
280 */
Herbert Xu6bfd4802006-09-21 11:39:29 +1000281 if (strcmp(alg->cra_name, q->cra_name) &&
282 strcmp(alg->cra_driver_name, q->cra_name))
283 continue;
284
285 if (larval->adult)
286 continue;
Herbert Xu492e2b62006-09-21 11:35:17 +1000287 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
288 continue;
Herbert Xu28259822006-08-06 21:23:26 +1000289 if (!crypto_mod_get(alg))
290 continue;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000291
Herbert Xu28259822006-08-06 21:23:26 +1000292 larval->adult = alg;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000293 continue;
Herbert Xu28259822006-08-06 21:23:26 +1000294 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000295
296 if (strcmp(alg->cra_name, q->cra_name))
297 continue;
298
299 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
300 q->cra_priority > alg->cra_priority)
301 continue;
302
Herbert Xu2bf29012009-08-31 15:56:54 +1000303 crypto_remove_spawns(q, &list, alg);
Herbert Xucce9e062006-08-21 21:08:13 +1000304 }
Herbert Xu28259822006-08-06 21:23:26 +1000305
Herbert Xu73d38642008-08-03 21:15:23 +0800306complete:
307 complete_all(&test->completion);
Herbert Xu28259822006-08-06 21:23:26 +1000308
Herbert Xu73d38642008-08-03 21:15:23 +0800309unlock:
310 up_write(&crypto_alg_sem);
311
312 crypto_remove_final(&list);
Herbert Xucce9e062006-08-21 21:08:13 +1000313}
Herbert Xu73d38642008-08-03 21:15:23 +0800314EXPORT_SYMBOL_GPL(crypto_alg_tested);
Herbert Xu4cc77202006-08-06 21:16:34 +1000315
Steffen Klassert22e5b202011-09-27 07:23:07 +0200316void crypto_remove_final(struct list_head *list)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000317{
318 struct crypto_alg *alg;
319 struct crypto_alg *n;
320
321 list_for_each_entry_safe(alg, n, list, cra_list) {
322 list_del_init(&alg->cra_list);
323 crypto_alg_put(alg);
324 }
325}
Steffen Klassert22e5b202011-09-27 07:23:07 +0200326EXPORT_SYMBOL_GPL(crypto_remove_final);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000327
Herbert Xu73d38642008-08-03 21:15:23 +0800328static void crypto_wait_for_test(struct crypto_larval *larval)
329{
330 int err;
331
332 err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
333 if (err != NOTIFY_STOP) {
334 if (WARN_ON(err != NOTIFY_DONE))
335 goto out;
336 crypto_alg_tested(larval->alg.cra_driver_name, 0);
337 }
338
339 err = wait_for_completion_interruptible(&larval->completion);
340 WARN_ON(err);
341
342out:
343 crypto_larval_kill(&larval->alg);
344}
345
Herbert Xu4cc77202006-08-06 21:16:34 +1000346int crypto_register_alg(struct crypto_alg *alg)
347{
Herbert Xu73d38642008-08-03 21:15:23 +0800348 struct crypto_larval *larval;
Herbert Xu4cc77202006-08-06 21:16:34 +1000349 int err;
350
351 err = crypto_check_alg(alg);
352 if (err)
353 return err;
354
355 down_write(&crypto_alg_sem);
Herbert Xu73d38642008-08-03 21:15:23 +0800356 larval = __crypto_register_alg(alg);
Herbert Xu4cc77202006-08-06 21:16:34 +1000357 up_write(&crypto_alg_sem);
358
Herbert Xu73d38642008-08-03 21:15:23 +0800359 if (IS_ERR(larval))
360 return PTR_ERR(larval);
361
362 crypto_wait_for_test(larval);
363 return 0;
Herbert Xu4cc77202006-08-06 21:16:34 +1000364}
Herbert Xucce9e062006-08-21 21:08:13 +1000365EXPORT_SYMBOL_GPL(crypto_register_alg);
366
Herbert Xu6bfd4802006-09-21 11:39:29 +1000367static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
368{
369 if (unlikely(list_empty(&alg->cra_list)))
370 return -ENOENT;
371
372 alg->cra_flags |= CRYPTO_ALG_DEAD;
373
374 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
375 list_del_init(&alg->cra_list);
Herbert Xu2bf29012009-08-31 15:56:54 +1000376 crypto_remove_spawns(alg, list, NULL);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000377
378 return 0;
379}
380
Herbert Xucce9e062006-08-21 21:08:13 +1000381int crypto_unregister_alg(struct crypto_alg *alg)
382{
Herbert Xu6bfd4802006-09-21 11:39:29 +1000383 int ret;
384 LIST_HEAD(list);
Richard Hartmann5357c6c2010-02-16 20:25:21 +0800385
Herbert Xucce9e062006-08-21 21:08:13 +1000386 down_write(&crypto_alg_sem);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000387 ret = crypto_remove_alg(alg, &list);
Herbert Xucce9e062006-08-21 21:08:13 +1000388 up_write(&crypto_alg_sem);
389
390 if (ret)
391 return ret;
392
393 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
394 if (alg->cra_destroy)
395 alg->cra_destroy(alg);
396
Herbert Xu6bfd4802006-09-21 11:39:29 +1000397 crypto_remove_final(&list);
Herbert Xucce9e062006-08-21 21:08:13 +1000398 return 0;
399}
400EXPORT_SYMBOL_GPL(crypto_unregister_alg);
401
Mark Brown4b004342012-01-17 23:34:26 +0000402int crypto_register_algs(struct crypto_alg *algs, int count)
403{
404 int i, ret;
405
406 for (i = 0; i < count; i++) {
407 ret = crypto_register_alg(&algs[i]);
408 if (ret)
409 goto err;
410 }
411
412 return 0;
413
414err:
415 for (--i; i >= 0; --i)
416 crypto_unregister_alg(&algs[i]);
417
418 return ret;
419}
420EXPORT_SYMBOL_GPL(crypto_register_algs);
421
422int crypto_unregister_algs(struct crypto_alg *algs, int count)
423{
424 int i, ret;
425
426 for (i = 0; i < count; i++) {
427 ret = crypto_unregister_alg(&algs[i]);
428 if (ret)
429 pr_err("Failed to unregister %s %s: %d\n",
430 algs[i].cra_driver_name, algs[i].cra_name, ret);
431 }
432
433 return 0;
434}
435EXPORT_SYMBOL_GPL(crypto_unregister_algs);
436
Herbert Xu4cc77202006-08-06 21:16:34 +1000437int crypto_register_template(struct crypto_template *tmpl)
438{
439 struct crypto_template *q;
440 int err = -EEXIST;
441
442 down_write(&crypto_alg_sem);
443
Jarod Wilson002c77a2014-07-02 15:37:30 -0400444 crypto_check_module_sig(tmpl->module);
445
Herbert Xu4cc77202006-08-06 21:16:34 +1000446 list_for_each_entry(q, &crypto_template_list, list) {
447 if (q == tmpl)
448 goto out;
449 }
450
451 list_add(&tmpl->list, &crypto_template_list);
Herbert Xu28259822006-08-06 21:23:26 +1000452 crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
Herbert Xu4cc77202006-08-06 21:16:34 +1000453 err = 0;
454out:
455 up_write(&crypto_alg_sem);
456 return err;
457}
458EXPORT_SYMBOL_GPL(crypto_register_template);
459
460void crypto_unregister_template(struct crypto_template *tmpl)
461{
462 struct crypto_instance *inst;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800463 struct hlist_node *n;
Herbert Xu4cc77202006-08-06 21:16:34 +1000464 struct hlist_head *list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000465 LIST_HEAD(users);
Herbert Xu4cc77202006-08-06 21:16:34 +1000466
467 down_write(&crypto_alg_sem);
468
469 BUG_ON(list_empty(&tmpl->list));
470 list_del_init(&tmpl->list);
471
472 list = &tmpl->instances;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800473 hlist_for_each_entry(inst, list, list) {
Herbert Xu6bfd4802006-09-21 11:39:29 +1000474 int err = crypto_remove_alg(&inst->alg, &users);
Joshua I. James0efcb8d2014-12-05 15:00:10 +0900475
Herbert Xu6bfd4802006-09-21 11:39:29 +1000476 BUG_ON(err);
Herbert Xu4cc77202006-08-06 21:16:34 +1000477 }
478
Herbert Xu28259822006-08-06 21:23:26 +1000479 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
480
Herbert Xu4cc77202006-08-06 21:16:34 +1000481 up_write(&crypto_alg_sem);
482
Sasha Levinb67bfe02013-02-27 17:06:00 -0800483 hlist_for_each_entry_safe(inst, n, list, list) {
Herbert Xu4cc77202006-08-06 21:16:34 +1000484 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
485 tmpl->free(inst);
486 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000487 crypto_remove_final(&users);
Herbert Xu4cc77202006-08-06 21:16:34 +1000488}
489EXPORT_SYMBOL_GPL(crypto_unregister_template);
490
491static struct crypto_template *__crypto_lookup_template(const char *name)
492{
493 struct crypto_template *q, *tmpl = NULL;
494
495 down_read(&crypto_alg_sem);
496 list_for_each_entry(q, &crypto_template_list, list) {
497 if (strcmp(q->name, name))
498 continue;
499 if (unlikely(!crypto_tmpl_get(q)))
500 continue;
501
502 tmpl = q;
503 break;
504 }
505 up_read(&crypto_alg_sem);
506
507 return tmpl;
508}
509
510struct crypto_template *crypto_lookup_template(const char *name)
511{
Kees Cook4943ba12014-11-24 16:32:38 -0800512 return try_then_request_module(__crypto_lookup_template(name),
513 "crypto-%s", name);
Herbert Xu4cc77202006-08-06 21:16:34 +1000514}
515EXPORT_SYMBOL_GPL(crypto_lookup_template);
516
517int crypto_register_instance(struct crypto_template *tmpl,
518 struct crypto_instance *inst)
519{
Herbert Xu73d38642008-08-03 21:15:23 +0800520 struct crypto_larval *larval;
521 int err;
Herbert Xu4cc77202006-08-06 21:16:34 +1000522
Herbert Xu4cc77202006-08-06 21:16:34 +1000523 err = crypto_check_alg(&inst->alg);
524 if (err)
525 goto err;
526
527 inst->alg.cra_module = tmpl->module;
Steffen Klassert64a947b2011-09-27 07:21:26 +0200528 inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
Herbert Xu4cc77202006-08-06 21:16:34 +1000529
530 down_write(&crypto_alg_sem);
531
Herbert Xu73d38642008-08-03 21:15:23 +0800532 larval = __crypto_register_alg(&inst->alg);
533 if (IS_ERR(larval))
Herbert Xu4cc77202006-08-06 21:16:34 +1000534 goto unlock;
535
536 hlist_add_head(&inst->list, &tmpl->instances);
537 inst->tmpl = tmpl;
538
539unlock:
540 up_write(&crypto_alg_sem);
541
Herbert Xu73d38642008-08-03 21:15:23 +0800542 err = PTR_ERR(larval);
543 if (IS_ERR(larval))
544 goto err;
545
546 crypto_wait_for_test(larval);
547 err = 0;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000548
Herbert Xu4cc77202006-08-06 21:16:34 +1000549err:
550 return err;
551}
552EXPORT_SYMBOL_GPL(crypto_register_instance);
553
Herbert Xu87b16752015-04-02 22:39:40 +0800554int crypto_unregister_instance(struct crypto_instance *inst)
Steffen Klassertce3fd842011-11-08 10:09:17 +0100555{
Herbert Xu1f723712015-04-02 22:31:22 +0800556 LIST_HEAD(list);
Steffen Klassertce3fd842011-11-08 10:09:17 +0100557
Steffen Klassertce3fd842011-11-08 10:09:17 +0100558 down_write(&crypto_alg_sem);
559
Herbert Xu87b16752015-04-02 22:39:40 +0800560 crypto_remove_spawns(&inst->alg, &list, NULL);
Herbert Xu1f723712015-04-02 22:31:22 +0800561 crypto_remove_instance(inst, &list);
Steffen Klassertce3fd842011-11-08 10:09:17 +0100562
563 up_write(&crypto_alg_sem);
564
Herbert Xu1f723712015-04-02 22:31:22 +0800565 crypto_remove_final(&list);
Steffen Klassertce3fd842011-11-08 10:09:17 +0100566
567 return 0;
568}
569EXPORT_SYMBOL_GPL(crypto_unregister_instance);
570
Herbert Xu6bfd4802006-09-21 11:39:29 +1000571int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
Herbert Xua73e6992007-04-08 21:31:36 +1000572 struct crypto_instance *inst, u32 mask)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000573{
574 int err = -EAGAIN;
575
576 spawn->inst = inst;
Herbert Xua73e6992007-04-08 21:31:36 +1000577 spawn->mask = mask;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000578
579 down_write(&crypto_alg_sem);
580 if (!crypto_is_moribund(alg)) {
581 list_add(&spawn->list, &alg->cra_users);
582 spawn->alg = alg;
583 err = 0;
584 }
585 up_write(&crypto_alg_sem);
586
587 return err;
588}
589EXPORT_SYMBOL_GPL(crypto_init_spawn);
590
Herbert Xu97eedce2009-07-08 15:55:52 +0800591int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,
592 struct crypto_instance *inst,
593 const struct crypto_type *frontend)
594{
595 int err = -EINVAL;
596
Dan Carpenterc614e102010-05-03 11:08:15 +0800597 if ((alg->cra_flags ^ frontend->type) & frontend->maskset)
Herbert Xu97eedce2009-07-08 15:55:52 +0800598 goto out;
599
600 spawn->frontend = frontend;
601 err = crypto_init_spawn(spawn, alg, inst, frontend->maskset);
602
603out:
604 return err;
605}
606EXPORT_SYMBOL_GPL(crypto_init_spawn2);
607
Herbert Xu6bfd4802006-09-21 11:39:29 +1000608void crypto_drop_spawn(struct crypto_spawn *spawn)
609{
Herbert Xu7ede5a52009-07-09 11:34:06 +0800610 if (!spawn->alg)
611 return;
612
Herbert Xu6bfd4802006-09-21 11:39:29 +1000613 down_write(&crypto_alg_sem);
614 list_del(&spawn->list);
615 up_write(&crypto_alg_sem);
616}
617EXPORT_SYMBOL_GPL(crypto_drop_spawn);
618
Herbert Xu97eedce2009-07-08 15:55:52 +0800619static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000620{
621 struct crypto_alg *alg;
622 struct crypto_alg *alg2;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000623
624 down_read(&crypto_alg_sem);
625 alg = spawn->alg;
626 alg2 = alg;
627 if (alg2)
628 alg2 = crypto_mod_get(alg2);
629 up_read(&crypto_alg_sem);
630
631 if (!alg2) {
632 if (alg)
633 crypto_shoot_alg(alg);
634 return ERR_PTR(-EAGAIN);
635 }
636
Herbert Xu97eedce2009-07-08 15:55:52 +0800637 return alg;
638}
639
640struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
641 u32 mask)
642{
643 struct crypto_alg *alg;
644 struct crypto_tfm *tfm;
645
646 alg = crypto_spawn_alg(spawn);
647 if (IS_ERR(alg))
648 return ERR_CAST(alg);
649
Herbert Xu2e306ee2006-12-17 10:05:58 +1100650 tfm = ERR_PTR(-EINVAL);
651 if (unlikely((alg->cra_flags ^ type) & mask))
652 goto out_put_alg;
653
Herbert Xu27d2a332007-01-24 20:50:26 +1100654 tfm = __crypto_alloc_tfm(alg, type, mask);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000655 if (IS_ERR(tfm))
Herbert Xu2e306ee2006-12-17 10:05:58 +1100656 goto out_put_alg;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000657
658 return tfm;
Herbert Xu2e306ee2006-12-17 10:05:58 +1100659
660out_put_alg:
661 crypto_mod_put(alg);
662 return tfm;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000663}
664EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
665
Herbert Xu97eedce2009-07-08 15:55:52 +0800666void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
667{
668 struct crypto_alg *alg;
669 struct crypto_tfm *tfm;
670
671 alg = crypto_spawn_alg(spawn);
672 if (IS_ERR(alg))
673 return ERR_CAST(alg);
674
675 tfm = crypto_create_tfm(alg, spawn->frontend);
676 if (IS_ERR(tfm))
677 goto out_put_alg;
678
679 return tfm;
680
681out_put_alg:
682 crypto_mod_put(alg);
683 return tfm;
684}
685EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
686
Herbert Xu28259822006-08-06 21:23:26 +1000687int crypto_register_notifier(struct notifier_block *nb)
688{
689 return blocking_notifier_chain_register(&crypto_chain, nb);
690}
691EXPORT_SYMBOL_GPL(crypto_register_notifier);
692
693int crypto_unregister_notifier(struct notifier_block *nb)
694{
695 return blocking_notifier_chain_unregister(&crypto_chain, nb);
696}
697EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
698
Herbert Xuebc610e2007-01-01 18:37:02 +1100699struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000700{
Herbert Xu39e1ee012007-08-29 19:27:26 +0800701 struct rtattr *rta = tb[0];
Herbert Xuebc610e2007-01-01 18:37:02 +1100702 struct crypto_attr_type *algt;
703
704 if (!rta)
705 return ERR_PTR(-ENOENT);
706 if (RTA_PAYLOAD(rta) < sizeof(*algt))
707 return ERR_PTR(-EINVAL);
Herbert Xu39e1ee012007-08-29 19:27:26 +0800708 if (rta->rta_type != CRYPTOA_TYPE)
709 return ERR_PTR(-EINVAL);
Herbert Xuebc610e2007-01-01 18:37:02 +1100710
711 algt = RTA_DATA(rta);
712
713 return algt;
714}
715EXPORT_SYMBOL_GPL(crypto_get_attr_type);
716
717int crypto_check_attr_type(struct rtattr **tb, u32 type)
718{
719 struct crypto_attr_type *algt;
720
721 algt = crypto_get_attr_type(tb);
722 if (IS_ERR(algt))
723 return PTR_ERR(algt);
724
725 if ((algt->type ^ type) & algt->mask)
726 return -EINVAL;
727
728 return 0;
729}
730EXPORT_SYMBOL_GPL(crypto_check_attr_type);
731
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800732const char *crypto_attr_alg_name(struct rtattr *rta)
Herbert Xuebc610e2007-01-01 18:37:02 +1100733{
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000734 struct crypto_attr_alg *alga;
735
Herbert Xuebc610e2007-01-01 18:37:02 +1100736 if (!rta)
737 return ERR_PTR(-ENOENT);
738 if (RTA_PAYLOAD(rta) < sizeof(*alga))
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000739 return ERR_PTR(-EINVAL);
Herbert Xu39e1ee012007-08-29 19:27:26 +0800740 if (rta->rta_type != CRYPTOA_ALG)
741 return ERR_PTR(-EINVAL);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000742
743 alga = RTA_DATA(rta);
744 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
745
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800746 return alga->name;
747}
748EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
749
Herbert Xud06854f2009-07-08 17:53:16 +0800750struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
751 const struct crypto_type *frontend,
752 u32 type, u32 mask)
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800753{
754 const char *name;
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800755
756 name = crypto_attr_alg_name(rta);
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800757 if (IS_ERR(name))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100758 return ERR_CAST(name);
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800759
Herbert Xud06854f2009-07-08 17:53:16 +0800760 return crypto_find_alg(name, frontend, type, mask);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000761}
Herbert Xud06854f2009-07-08 17:53:16 +0800762EXPORT_SYMBOL_GPL(crypto_attr_alg2);
Herbert Xu3c09f172007-08-30 16:24:15 +0800763
764int crypto_attr_u32(struct rtattr *rta, u32 *num)
765{
766 struct crypto_attr_u32 *nu32;
767
768 if (!rta)
769 return -ENOENT;
770 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
771 return -EINVAL;
772 if (rta->rta_type != CRYPTOA_U32)
773 return -EINVAL;
774
775 nu32 = RTA_DATA(rta);
776 *num = nu32->num;
777
778 return 0;
779}
780EXPORT_SYMBOL_GPL(crypto_attr_u32);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000781
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800782void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
783 unsigned int head)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000784{
785 struct crypto_instance *inst;
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800786 char *p;
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000787 int err;
788
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800789 p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
790 GFP_KERNEL);
791 if (!p)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000792 return ERR_PTR(-ENOMEM);
793
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800794 inst = (void *)(p + head);
795
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000796 err = -ENAMETOOLONG;
797 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
798 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
799 goto err_free_inst;
800
801 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
802 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
803 goto err_free_inst;
804
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800805 return p;
806
807err_free_inst:
808 kfree(p);
809 return ERR_PTR(err);
810}
811EXPORT_SYMBOL_GPL(crypto_alloc_instance2);
812
813struct crypto_instance *crypto_alloc_instance(const char *name,
814 struct crypto_alg *alg)
815{
816 struct crypto_instance *inst;
817 struct crypto_spawn *spawn;
818 int err;
819
820 inst = crypto_alloc_instance2(name, alg, 0);
821 if (IS_ERR(inst))
822 goto out;
823
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000824 spawn = crypto_instance_ctx(inst);
Herbert Xua73e6992007-04-08 21:31:36 +1000825 err = crypto_init_spawn(spawn, alg, inst,
826 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000827
828 if (err)
829 goto err_free_inst;
830
831 return inst;
832
833err_free_inst:
834 kfree(inst);
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800835 inst = ERR_PTR(err);
836
837out:
838 return inst;
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000839}
840EXPORT_SYMBOL_GPL(crypto_alloc_instance);
841
Herbert Xub5b7f082007-04-16 20:48:54 +1000842void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
843{
844 INIT_LIST_HEAD(&queue->list);
845 queue->backlog = &queue->list;
846 queue->qlen = 0;
847 queue->max_qlen = max_qlen;
848}
849EXPORT_SYMBOL_GPL(crypto_init_queue);
850
851int crypto_enqueue_request(struct crypto_queue *queue,
852 struct crypto_async_request *request)
853{
854 int err = -EINPROGRESS;
855
856 if (unlikely(queue->qlen >= queue->max_qlen)) {
857 err = -EBUSY;
858 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
859 goto out;
860 if (queue->backlog == &queue->list)
861 queue->backlog = &request->list;
862 }
863
864 queue->qlen++;
865 list_add_tail(&request->list, &queue->list);
866
867out:
868 return err;
869}
870EXPORT_SYMBOL_GPL(crypto_enqueue_request);
871
Herbert Xu0c7d4002009-08-29 20:44:04 +1000872void *__crypto_dequeue_request(struct crypto_queue *queue, unsigned int offset)
Herbert Xub5b7f082007-04-16 20:48:54 +1000873{
874 struct list_head *request;
875
876 if (unlikely(!queue->qlen))
877 return NULL;
878
879 queue->qlen--;
880
881 if (queue->backlog != &queue->list)
882 queue->backlog = queue->backlog->next;
883
884 request = queue->list.next;
885 list_del(request);
886
Herbert Xu0c7d4002009-08-29 20:44:04 +1000887 return (char *)list_entry(request, struct crypto_async_request, list) -
888 offset;
889}
890EXPORT_SYMBOL_GPL(__crypto_dequeue_request);
891
892struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
893{
894 return __crypto_dequeue_request(queue, 0);
Herbert Xub5b7f082007-04-16 20:48:54 +1000895}
896EXPORT_SYMBOL_GPL(crypto_dequeue_request);
897
898int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
899{
900 struct crypto_async_request *req;
901
902 list_for_each_entry(req, &queue->list, list) {
903 if (req->tfm == tfm)
904 return 1;
905 }
906
907 return 0;
908}
909EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
910
Herbert Xu76136362007-11-20 17:26:06 +0800911static inline void crypto_inc_byte(u8 *a, unsigned int size)
912{
913 u8 *b = (a + size);
914 u8 c;
915
916 for (; size; size--) {
917 c = *--b + 1;
918 *b = c;
919 if (c)
920 break;
921 }
922}
923
924void crypto_inc(u8 *a, unsigned int size)
925{
926 __be32 *b = (__be32 *)(a + size);
927 u32 c;
928
929 for (; size >= 4; size -= 4) {
930 c = be32_to_cpu(*--b) + 1;
931 *b = cpu_to_be32(c);
932 if (c)
933 return;
934 }
935
936 crypto_inc_byte(a, size);
937}
938EXPORT_SYMBOL_GPL(crypto_inc);
939
940static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
941{
942 for (; size; size--)
943 *a++ ^= *b++;
944}
945
946void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
947{
948 u32 *a = (u32 *)dst;
949 u32 *b = (u32 *)src;
950
951 for (; size >= 4; size -= 4)
952 *a++ ^= *b++;
953
954 crypto_xor_byte((u8 *)a, (u8 *)b, size);
955}
956EXPORT_SYMBOL_GPL(crypto_xor);
957
Herbert Xucce9e062006-08-21 21:08:13 +1000958static int __init crypto_algapi_init(void)
959{
960 crypto_init_proc();
961 return 0;
962}
963
964static void __exit crypto_algapi_exit(void)
965{
966 crypto_exit_proc();
967}
968
969module_init(crypto_algapi_init);
970module_exit(crypto_algapi_exit);
971
972MODULE_LICENSE("GPL");
973MODULE_DESCRIPTION("Cryptographic algorithms API");