blob: 83b04e0884b10cb108f869164c52458a5e833447 [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 Xua73e6992007-04-08 21:31:36 +1000102static void crypto_remove_spawn(struct crypto_spawn *spawn,
Herbert Xu2bf29012009-08-31 15:56:54 +1000103 struct list_head *list)
Herbert Xua73e6992007-04-08 21:31:36 +1000104{
105 struct crypto_instance *inst = spawn->inst;
106 struct crypto_template *tmpl = inst->tmpl;
107
Herbert Xua73e6992007-04-08 21:31:36 +1000108 if (crypto_is_dead(&inst->alg))
109 return;
110
111 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
Herbert Xu38cb2412007-11-17 22:09:38 +0800112 if (hlist_unhashed(&inst->list))
113 return;
114
Herbert Xua73e6992007-04-08 21:31:36 +1000115 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
Herbert Xu2bf29012009-08-31 15:56:54 +1000123 BUG_ON(!list_empty(&inst->alg.cra_users));
Herbert Xua73e6992007-04-08 21:31:36 +1000124}
125
Steffen Klassert89b596b2011-09-27 07:22:08 +0200126void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
127 struct crypto_alg *nalg)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000128{
Herbert Xu2bf29012009-08-31 15:56:54 +1000129 u32 new_type = (nalg ?: alg)->cra_flags;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000130 struct crypto_spawn *spawn, *n;
Herbert Xua73e6992007-04-08 21:31:36 +1000131 LIST_HEAD(secondary_spawns);
Herbert Xu2bf29012009-08-31 15:56:54 +1000132 struct list_head *spawns;
133 LIST_HEAD(stack);
134 LIST_HEAD(top);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000135
Herbert Xu2bf29012009-08-31 15:56:54 +1000136 spawns = &alg->cra_users;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000137 list_for_each_entry_safe(spawn, n, spawns, list) {
Herbert Xua73e6992007-04-08 21:31:36 +1000138 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000139 continue;
140
Herbert Xu2bf29012009-08-31 15:56:54 +1000141 list_move(&spawn->list, &top);
Herbert Xua73e6992007-04-08 21:31:36 +1000142 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000143
Herbert Xu2bf29012009-08-31 15:56:54 +1000144 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 } while ((spawns = crypto_more_spawns(alg, &stack, &top,
164 &secondary_spawns)));
165
166 list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
167 if (spawn->alg)
168 list_move(&spawn->list, &spawn->alg->cra_users);
169 else
170 crypto_remove_spawn(spawn, list);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000171 }
172}
Steffen Klassert89b596b2011-09-27 07:22:08 +0200173EXPORT_SYMBOL_GPL(crypto_remove_spawns);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000174
Herbert Xu73d38642008-08-03 21:15:23 +0800175static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
Herbert Xu4cc77202006-08-06 21:16:34 +1000176{
177 struct crypto_alg *q;
Herbert Xu73d38642008-08-03 21:15:23 +0800178 struct crypto_larval *larval;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000179 int ret = -EAGAIN;
180
181 if (crypto_is_dead(alg))
Herbert Xu73d38642008-08-03 21:15:23 +0800182 goto err;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000183
184 INIT_LIST_HEAD(&alg->cra_users);
185
Herbert Xu73d38642008-08-03 21:15:23 +0800186 /* No cheating! */
187 alg->cra_flags &= ~CRYPTO_ALG_TESTED;
188
Herbert Xu6bfd4802006-09-21 11:39:29 +1000189 ret = -EEXIST;
Herbert Xu4cc77202006-08-06 21:16:34 +1000190
Herbert Xu28259822006-08-06 21:23:26 +1000191 atomic_set(&alg->cra_refcnt, 1);
Herbert Xucce9e062006-08-21 21:08:13 +1000192 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xu4cc77202006-08-06 21:16:34 +1000193 if (q == alg)
Herbert Xu73d38642008-08-03 21:15:23 +0800194 goto err;
195
Herbert Xub8e15992009-01-28 14:09:59 +1100196 if (crypto_is_moribund(q))
197 continue;
198
Herbert Xu73d38642008-08-03 21:15:23 +0800199 if (crypto_is_larval(q)) {
200 if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
201 goto err;
202 continue;
203 }
204
205 if (!strcmp(q->cra_driver_name, alg->cra_name) ||
206 !strcmp(q->cra_name, alg->cra_driver_name))
207 goto err;
208 }
209
210 larval = crypto_larval_alloc(alg->cra_name,
211 alg->cra_flags | CRYPTO_ALG_TESTED, 0);
212 if (IS_ERR(larval))
213 goto out;
214
215 ret = -ENOENT;
216 larval->adult = crypto_mod_get(alg);
217 if (!larval->adult)
218 goto free_larval;
219
220 atomic_set(&larval->alg.cra_refcnt, 1);
221 memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
222 CRYPTO_MAX_ALG_NAME);
223 larval->alg.cra_priority = alg->cra_priority;
224
225 list_add(&alg->cra_list, &crypto_alg_list);
226 list_add(&larval->alg.cra_list, &crypto_alg_list);
227
Richard Hartmann5357c6c2010-02-16 20:25:21 +0800228out:
Herbert Xu73d38642008-08-03 21:15:23 +0800229 return larval;
230
231free_larval:
232 kfree(larval);
233err:
234 larval = ERR_PTR(ret);
235 goto out;
236}
237
238void crypto_alg_tested(const char *name, int err)
239{
240 struct crypto_larval *test;
241 struct crypto_alg *alg;
242 struct crypto_alg *q;
243 LIST_HEAD(list);
244
245 down_write(&crypto_alg_sem);
246 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xub8e15992009-01-28 14:09:59 +1100247 if (crypto_is_moribund(q) || !crypto_is_larval(q))
Herbert Xu73d38642008-08-03 21:15:23 +0800248 continue;
249
250 test = (struct crypto_larval *)q;
251
252 if (!strcmp(q->cra_driver_name, name))
253 goto found;
254 }
255
256 printk(KERN_ERR "alg: Unexpected test result for %s: %d\n", name, err);
257 goto unlock;
258
259found:
Herbert Xub8e15992009-01-28 14:09:59 +1100260 q->cra_flags |= CRYPTO_ALG_DEAD;
Herbert Xu73d38642008-08-03 21:15:23 +0800261 alg = test->adult;
262 if (err || list_empty(&alg->cra_list))
263 goto complete;
264
265 alg->cra_flags |= CRYPTO_ALG_TESTED;
266
267 list_for_each_entry(q, &crypto_alg_list, cra_list) {
268 if (q == alg)
269 continue;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000270
271 if (crypto_is_moribund(q))
272 continue;
273
274 if (crypto_is_larval(q)) {
Herbert Xu28259822006-08-06 21:23:26 +1000275 struct crypto_larval *larval = (void *)q;
276
Herbert Xud8058482007-10-02 22:27:29 +0800277 /*
278 * Check to see if either our generic name or
279 * specific name can satisfy the name requested
280 * by the larval entry q.
281 */
Herbert Xu6bfd4802006-09-21 11:39:29 +1000282 if (strcmp(alg->cra_name, q->cra_name) &&
283 strcmp(alg->cra_driver_name, q->cra_name))
284 continue;
285
286 if (larval->adult)
287 continue;
Herbert Xu492e2b62006-09-21 11:35:17 +1000288 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
289 continue;
Herbert Xu28259822006-08-06 21:23:26 +1000290 if (!crypto_mod_get(alg))
291 continue;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000292
Herbert Xu28259822006-08-06 21:23:26 +1000293 larval->adult = alg;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000294 continue;
Herbert Xu28259822006-08-06 21:23:26 +1000295 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000296
297 if (strcmp(alg->cra_name, q->cra_name))
298 continue;
299
300 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
301 q->cra_priority > alg->cra_priority)
302 continue;
303
Herbert Xu2bf29012009-08-31 15:56:54 +1000304 crypto_remove_spawns(q, &list, alg);
Herbert Xucce9e062006-08-21 21:08:13 +1000305 }
Herbert Xu28259822006-08-06 21:23:26 +1000306
Herbert Xu73d38642008-08-03 21:15:23 +0800307complete:
308 complete_all(&test->completion);
Herbert Xu28259822006-08-06 21:23:26 +1000309
Herbert Xu73d38642008-08-03 21:15:23 +0800310unlock:
311 up_write(&crypto_alg_sem);
312
313 crypto_remove_final(&list);
Herbert Xucce9e062006-08-21 21:08:13 +1000314}
Herbert Xu73d38642008-08-03 21:15:23 +0800315EXPORT_SYMBOL_GPL(crypto_alg_tested);
Herbert Xu4cc77202006-08-06 21:16:34 +1000316
Steffen Klassert22e5b202011-09-27 07:23:07 +0200317void crypto_remove_final(struct list_head *list)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000318{
319 struct crypto_alg *alg;
320 struct crypto_alg *n;
321
322 list_for_each_entry_safe(alg, n, list, cra_list) {
323 list_del_init(&alg->cra_list);
324 crypto_alg_put(alg);
325 }
326}
Steffen Klassert22e5b202011-09-27 07:23:07 +0200327EXPORT_SYMBOL_GPL(crypto_remove_final);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000328
Herbert Xu73d38642008-08-03 21:15:23 +0800329static void crypto_wait_for_test(struct crypto_larval *larval)
330{
331 int err;
332
333 err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
334 if (err != NOTIFY_STOP) {
335 if (WARN_ON(err != NOTIFY_DONE))
336 goto out;
337 crypto_alg_tested(larval->alg.cra_driver_name, 0);
338 }
339
340 err = wait_for_completion_interruptible(&larval->completion);
341 WARN_ON(err);
342
343out:
344 crypto_larval_kill(&larval->alg);
345}
346
Herbert Xu4cc77202006-08-06 21:16:34 +1000347int crypto_register_alg(struct crypto_alg *alg)
348{
Herbert Xu73d38642008-08-03 21:15:23 +0800349 struct crypto_larval *larval;
Herbert Xu4cc77202006-08-06 21:16:34 +1000350 int err;
351
352 err = crypto_check_alg(alg);
353 if (err)
354 return err;
355
356 down_write(&crypto_alg_sem);
Herbert Xu73d38642008-08-03 21:15:23 +0800357 larval = __crypto_register_alg(alg);
Herbert Xu4cc77202006-08-06 21:16:34 +1000358 up_write(&crypto_alg_sem);
359
Herbert Xu73d38642008-08-03 21:15:23 +0800360 if (IS_ERR(larval))
361 return PTR_ERR(larval);
362
363 crypto_wait_for_test(larval);
364 return 0;
Herbert Xu4cc77202006-08-06 21:16:34 +1000365}
Herbert Xucce9e062006-08-21 21:08:13 +1000366EXPORT_SYMBOL_GPL(crypto_register_alg);
367
Herbert Xu6bfd4802006-09-21 11:39:29 +1000368static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
369{
370 if (unlikely(list_empty(&alg->cra_list)))
371 return -ENOENT;
372
373 alg->cra_flags |= CRYPTO_ALG_DEAD;
374
375 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
376 list_del_init(&alg->cra_list);
Herbert Xu2bf29012009-08-31 15:56:54 +1000377 crypto_remove_spawns(alg, list, NULL);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000378
379 return 0;
380}
381
Herbert Xucce9e062006-08-21 21:08:13 +1000382int crypto_unregister_alg(struct crypto_alg *alg)
383{
Herbert Xu6bfd4802006-09-21 11:39:29 +1000384 int ret;
385 LIST_HEAD(list);
Richard Hartmann5357c6c2010-02-16 20:25:21 +0800386
Herbert Xucce9e062006-08-21 21:08:13 +1000387 down_write(&crypto_alg_sem);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000388 ret = crypto_remove_alg(alg, &list);
Herbert Xucce9e062006-08-21 21:08:13 +1000389 up_write(&crypto_alg_sem);
390
391 if (ret)
392 return ret;
393
394 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
395 if (alg->cra_destroy)
396 alg->cra_destroy(alg);
397
Herbert Xu6bfd4802006-09-21 11:39:29 +1000398 crypto_remove_final(&list);
Herbert Xucce9e062006-08-21 21:08:13 +1000399 return 0;
400}
401EXPORT_SYMBOL_GPL(crypto_unregister_alg);
402
Mark Brown4b004342012-01-17 23:34:26 +0000403int crypto_register_algs(struct crypto_alg *algs, int count)
404{
405 int i, ret;
406
407 for (i = 0; i < count; i++) {
408 ret = crypto_register_alg(&algs[i]);
409 if (ret)
410 goto err;
411 }
412
413 return 0;
414
415err:
416 for (--i; i >= 0; --i)
417 crypto_unregister_alg(&algs[i]);
418
419 return ret;
420}
421EXPORT_SYMBOL_GPL(crypto_register_algs);
422
423int crypto_unregister_algs(struct crypto_alg *algs, int count)
424{
425 int i, ret;
426
427 for (i = 0; i < count; i++) {
428 ret = crypto_unregister_alg(&algs[i]);
429 if (ret)
430 pr_err("Failed to unregister %s %s: %d\n",
431 algs[i].cra_driver_name, algs[i].cra_name, ret);
432 }
433
434 return 0;
435}
436EXPORT_SYMBOL_GPL(crypto_unregister_algs);
437
Herbert Xu4cc77202006-08-06 21:16:34 +1000438int crypto_register_template(struct crypto_template *tmpl)
439{
440 struct crypto_template *q;
441 int err = -EEXIST;
442
443 down_write(&crypto_alg_sem);
444
Jarod Wilson002c77a2014-07-02 15:37:30 -0400445 crypto_check_module_sig(tmpl->module);
446
Herbert Xu4cc77202006-08-06 21:16:34 +1000447 list_for_each_entry(q, &crypto_template_list, list) {
448 if (q == tmpl)
449 goto out;
450 }
451
452 list_add(&tmpl->list, &crypto_template_list);
Herbert Xu28259822006-08-06 21:23:26 +1000453 crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
Herbert Xu4cc77202006-08-06 21:16:34 +1000454 err = 0;
455out:
456 up_write(&crypto_alg_sem);
457 return err;
458}
459EXPORT_SYMBOL_GPL(crypto_register_template);
460
461void crypto_unregister_template(struct crypto_template *tmpl)
462{
463 struct crypto_instance *inst;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800464 struct hlist_node *n;
Herbert Xu4cc77202006-08-06 21:16:34 +1000465 struct hlist_head *list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000466 LIST_HEAD(users);
Herbert Xu4cc77202006-08-06 21:16:34 +1000467
468 down_write(&crypto_alg_sem);
469
470 BUG_ON(list_empty(&tmpl->list));
471 list_del_init(&tmpl->list);
472
473 list = &tmpl->instances;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800474 hlist_for_each_entry(inst, list, list) {
Herbert Xu6bfd4802006-09-21 11:39:29 +1000475 int err = crypto_remove_alg(&inst->alg, &users);
Joshua I. James0efcb8d2014-12-05 15:00:10 +0900476
Herbert Xu6bfd4802006-09-21 11:39:29 +1000477 BUG_ON(err);
Herbert Xu4cc77202006-08-06 21:16:34 +1000478 }
479
Herbert Xu28259822006-08-06 21:23:26 +1000480 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
481
Herbert Xu4cc77202006-08-06 21:16:34 +1000482 up_write(&crypto_alg_sem);
483
Sasha Levinb67bfe02013-02-27 17:06:00 -0800484 hlist_for_each_entry_safe(inst, n, list, list) {
Herbert Xu4cc77202006-08-06 21:16:34 +1000485 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
486 tmpl->free(inst);
487 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000488 crypto_remove_final(&users);
Herbert Xu4cc77202006-08-06 21:16:34 +1000489}
490EXPORT_SYMBOL_GPL(crypto_unregister_template);
491
492static struct crypto_template *__crypto_lookup_template(const char *name)
493{
494 struct crypto_template *q, *tmpl = NULL;
495
496 down_read(&crypto_alg_sem);
497 list_for_each_entry(q, &crypto_template_list, list) {
498 if (strcmp(q->name, name))
499 continue;
500 if (unlikely(!crypto_tmpl_get(q)))
501 continue;
502
503 tmpl = q;
504 break;
505 }
506 up_read(&crypto_alg_sem);
507
508 return tmpl;
509}
510
511struct crypto_template *crypto_lookup_template(const char *name)
512{
Kees Cook4943ba12014-11-24 16:32:38 -0800513 return try_then_request_module(__crypto_lookup_template(name),
514 "crypto-%s", name);
Herbert Xu4cc77202006-08-06 21:16:34 +1000515}
516EXPORT_SYMBOL_GPL(crypto_lookup_template);
517
518int crypto_register_instance(struct crypto_template *tmpl,
519 struct crypto_instance *inst)
520{
Herbert Xu73d38642008-08-03 21:15:23 +0800521 struct crypto_larval *larval;
522 int err;
Herbert Xu4cc77202006-08-06 21:16:34 +1000523
Herbert Xu4cc77202006-08-06 21:16:34 +1000524 err = crypto_check_alg(&inst->alg);
525 if (err)
526 goto err;
527
528 inst->alg.cra_module = tmpl->module;
Steffen Klassert64a947b2011-09-27 07:21:26 +0200529 inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
Herbert Xu4cc77202006-08-06 21:16:34 +1000530
531 down_write(&crypto_alg_sem);
532
Herbert Xu73d38642008-08-03 21:15:23 +0800533 larval = __crypto_register_alg(&inst->alg);
534 if (IS_ERR(larval))
Herbert Xu4cc77202006-08-06 21:16:34 +1000535 goto unlock;
536
537 hlist_add_head(&inst->list, &tmpl->instances);
538 inst->tmpl = tmpl;
539
540unlock:
541 up_write(&crypto_alg_sem);
542
Herbert Xu73d38642008-08-03 21:15:23 +0800543 err = PTR_ERR(larval);
544 if (IS_ERR(larval))
545 goto err;
546
547 crypto_wait_for_test(larval);
548 err = 0;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000549
Herbert Xu4cc77202006-08-06 21:16:34 +1000550err:
551 return err;
552}
553EXPORT_SYMBOL_GPL(crypto_register_instance);
554
Steffen Klassertce3fd842011-11-08 10:09:17 +0100555int crypto_unregister_instance(struct crypto_alg *alg)
556{
557 int err;
558 struct crypto_instance *inst = (void *)alg;
559 struct crypto_template *tmpl = inst->tmpl;
560 LIST_HEAD(users);
561
562 if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
563 return -EINVAL;
564
565 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
566
567 down_write(&crypto_alg_sem);
568
569 hlist_del_init(&inst->list);
570 err = crypto_remove_alg(alg, &users);
571
572 up_write(&crypto_alg_sem);
573
574 if (err)
575 return err;
576
577 tmpl->free(inst);
578 crypto_remove_final(&users);
579
580 return 0;
581}
582EXPORT_SYMBOL_GPL(crypto_unregister_instance);
583
Herbert Xu6bfd4802006-09-21 11:39:29 +1000584int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
Herbert Xua73e6992007-04-08 21:31:36 +1000585 struct crypto_instance *inst, u32 mask)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000586{
587 int err = -EAGAIN;
588
589 spawn->inst = inst;
Herbert Xua73e6992007-04-08 21:31:36 +1000590 spawn->mask = mask;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000591
592 down_write(&crypto_alg_sem);
593 if (!crypto_is_moribund(alg)) {
594 list_add(&spawn->list, &alg->cra_users);
595 spawn->alg = alg;
596 err = 0;
597 }
598 up_write(&crypto_alg_sem);
599
600 return err;
601}
602EXPORT_SYMBOL_GPL(crypto_init_spawn);
603
Herbert Xu97eedce2009-07-08 15:55:52 +0800604int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,
605 struct crypto_instance *inst,
606 const struct crypto_type *frontend)
607{
608 int err = -EINVAL;
609
Dan Carpenterc614e102010-05-03 11:08:15 +0800610 if ((alg->cra_flags ^ frontend->type) & frontend->maskset)
Herbert Xu97eedce2009-07-08 15:55:52 +0800611 goto out;
612
613 spawn->frontend = frontend;
614 err = crypto_init_spawn(spawn, alg, inst, frontend->maskset);
615
616out:
617 return err;
618}
619EXPORT_SYMBOL_GPL(crypto_init_spawn2);
620
Herbert Xu6bfd4802006-09-21 11:39:29 +1000621void crypto_drop_spawn(struct crypto_spawn *spawn)
622{
Herbert Xu7ede5a52009-07-09 11:34:06 +0800623 if (!spawn->alg)
624 return;
625
Herbert Xu6bfd4802006-09-21 11:39:29 +1000626 down_write(&crypto_alg_sem);
627 list_del(&spawn->list);
628 up_write(&crypto_alg_sem);
629}
630EXPORT_SYMBOL_GPL(crypto_drop_spawn);
631
Herbert Xu97eedce2009-07-08 15:55:52 +0800632static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000633{
634 struct crypto_alg *alg;
635 struct crypto_alg *alg2;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000636
637 down_read(&crypto_alg_sem);
638 alg = spawn->alg;
639 alg2 = alg;
640 if (alg2)
641 alg2 = crypto_mod_get(alg2);
642 up_read(&crypto_alg_sem);
643
644 if (!alg2) {
645 if (alg)
646 crypto_shoot_alg(alg);
647 return ERR_PTR(-EAGAIN);
648 }
649
Herbert Xu97eedce2009-07-08 15:55:52 +0800650 return alg;
651}
652
653struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
654 u32 mask)
655{
656 struct crypto_alg *alg;
657 struct crypto_tfm *tfm;
658
659 alg = crypto_spawn_alg(spawn);
660 if (IS_ERR(alg))
661 return ERR_CAST(alg);
662
Herbert Xu2e306ee2006-12-17 10:05:58 +1100663 tfm = ERR_PTR(-EINVAL);
664 if (unlikely((alg->cra_flags ^ type) & mask))
665 goto out_put_alg;
666
Herbert Xu27d2a332007-01-24 20:50:26 +1100667 tfm = __crypto_alloc_tfm(alg, type, mask);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000668 if (IS_ERR(tfm))
Herbert Xu2e306ee2006-12-17 10:05:58 +1100669 goto out_put_alg;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000670
671 return tfm;
Herbert Xu2e306ee2006-12-17 10:05:58 +1100672
673out_put_alg:
674 crypto_mod_put(alg);
675 return tfm;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000676}
677EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
678
Herbert Xu97eedce2009-07-08 15:55:52 +0800679void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
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 = crypto_create_tfm(alg, spawn->frontend);
689 if (IS_ERR(tfm))
690 goto out_put_alg;
691
692 return tfm;
693
694out_put_alg:
695 crypto_mod_put(alg);
696 return tfm;
697}
698EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
699
Herbert Xu28259822006-08-06 21:23:26 +1000700int crypto_register_notifier(struct notifier_block *nb)
701{
702 return blocking_notifier_chain_register(&crypto_chain, nb);
703}
704EXPORT_SYMBOL_GPL(crypto_register_notifier);
705
706int crypto_unregister_notifier(struct notifier_block *nb)
707{
708 return blocking_notifier_chain_unregister(&crypto_chain, nb);
709}
710EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
711
Herbert Xuebc610e2007-01-01 18:37:02 +1100712struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000713{
Herbert Xu39e1ee012007-08-29 19:27:26 +0800714 struct rtattr *rta = tb[0];
Herbert Xuebc610e2007-01-01 18:37:02 +1100715 struct crypto_attr_type *algt;
716
717 if (!rta)
718 return ERR_PTR(-ENOENT);
719 if (RTA_PAYLOAD(rta) < sizeof(*algt))
720 return ERR_PTR(-EINVAL);
Herbert Xu39e1ee012007-08-29 19:27:26 +0800721 if (rta->rta_type != CRYPTOA_TYPE)
722 return ERR_PTR(-EINVAL);
Herbert Xuebc610e2007-01-01 18:37:02 +1100723
724 algt = RTA_DATA(rta);
725
726 return algt;
727}
728EXPORT_SYMBOL_GPL(crypto_get_attr_type);
729
730int crypto_check_attr_type(struct rtattr **tb, u32 type)
731{
732 struct crypto_attr_type *algt;
733
734 algt = crypto_get_attr_type(tb);
735 if (IS_ERR(algt))
736 return PTR_ERR(algt);
737
738 if ((algt->type ^ type) & algt->mask)
739 return -EINVAL;
740
741 return 0;
742}
743EXPORT_SYMBOL_GPL(crypto_check_attr_type);
744
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800745const char *crypto_attr_alg_name(struct rtattr *rta)
Herbert Xuebc610e2007-01-01 18:37:02 +1100746{
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000747 struct crypto_attr_alg *alga;
748
Herbert Xuebc610e2007-01-01 18:37:02 +1100749 if (!rta)
750 return ERR_PTR(-ENOENT);
751 if (RTA_PAYLOAD(rta) < sizeof(*alga))
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000752 return ERR_PTR(-EINVAL);
Herbert Xu39e1ee012007-08-29 19:27:26 +0800753 if (rta->rta_type != CRYPTOA_ALG)
754 return ERR_PTR(-EINVAL);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000755
756 alga = RTA_DATA(rta);
757 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
758
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800759 return alga->name;
760}
761EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
762
Herbert Xud06854f2009-07-08 17:53:16 +0800763struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
764 const struct crypto_type *frontend,
765 u32 type, u32 mask)
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800766{
767 const char *name;
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800768
769 name = crypto_attr_alg_name(rta);
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800770 if (IS_ERR(name))
Julia Lawall3e8afe32013-01-22 12:29:26 +0100771 return ERR_CAST(name);
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800772
Herbert Xud06854f2009-07-08 17:53:16 +0800773 return crypto_find_alg(name, frontend, type, mask);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000774}
Herbert Xud06854f2009-07-08 17:53:16 +0800775EXPORT_SYMBOL_GPL(crypto_attr_alg2);
Herbert Xu3c09f172007-08-30 16:24:15 +0800776
777int crypto_attr_u32(struct rtattr *rta, u32 *num)
778{
779 struct crypto_attr_u32 *nu32;
780
781 if (!rta)
782 return -ENOENT;
783 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
784 return -EINVAL;
785 if (rta->rta_type != CRYPTOA_U32)
786 return -EINVAL;
787
788 nu32 = RTA_DATA(rta);
789 *num = nu32->num;
790
791 return 0;
792}
793EXPORT_SYMBOL_GPL(crypto_attr_u32);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000794
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800795void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
796 unsigned int head)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000797{
798 struct crypto_instance *inst;
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800799 char *p;
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000800 int err;
801
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800802 p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
803 GFP_KERNEL);
804 if (!p)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000805 return ERR_PTR(-ENOMEM);
806
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800807 inst = (void *)(p + head);
808
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000809 err = -ENAMETOOLONG;
810 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
811 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
812 goto err_free_inst;
813
814 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
815 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
816 goto err_free_inst;
817
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800818 return p;
819
820err_free_inst:
821 kfree(p);
822 return ERR_PTR(err);
823}
824EXPORT_SYMBOL_GPL(crypto_alloc_instance2);
825
826struct crypto_instance *crypto_alloc_instance(const char *name,
827 struct crypto_alg *alg)
828{
829 struct crypto_instance *inst;
830 struct crypto_spawn *spawn;
831 int err;
832
833 inst = crypto_alloc_instance2(name, alg, 0);
834 if (IS_ERR(inst))
835 goto out;
836
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000837 spawn = crypto_instance_ctx(inst);
Herbert Xua73e6992007-04-08 21:31:36 +1000838 err = crypto_init_spawn(spawn, alg, inst,
839 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000840
841 if (err)
842 goto err_free_inst;
843
844 return inst;
845
846err_free_inst:
847 kfree(inst);
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800848 inst = ERR_PTR(err);
849
850out:
851 return inst;
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000852}
853EXPORT_SYMBOL_GPL(crypto_alloc_instance);
854
Herbert Xub5b7f082007-04-16 20:48:54 +1000855void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
856{
857 INIT_LIST_HEAD(&queue->list);
858 queue->backlog = &queue->list;
859 queue->qlen = 0;
860 queue->max_qlen = max_qlen;
861}
862EXPORT_SYMBOL_GPL(crypto_init_queue);
863
864int crypto_enqueue_request(struct crypto_queue *queue,
865 struct crypto_async_request *request)
866{
867 int err = -EINPROGRESS;
868
869 if (unlikely(queue->qlen >= queue->max_qlen)) {
870 err = -EBUSY;
871 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
872 goto out;
873 if (queue->backlog == &queue->list)
874 queue->backlog = &request->list;
875 }
876
877 queue->qlen++;
878 list_add_tail(&request->list, &queue->list);
879
880out:
881 return err;
882}
883EXPORT_SYMBOL_GPL(crypto_enqueue_request);
884
Herbert Xu0c7d4002009-08-29 20:44:04 +1000885void *__crypto_dequeue_request(struct crypto_queue *queue, unsigned int offset)
Herbert Xub5b7f082007-04-16 20:48:54 +1000886{
887 struct list_head *request;
888
889 if (unlikely(!queue->qlen))
890 return NULL;
891
892 queue->qlen--;
893
894 if (queue->backlog != &queue->list)
895 queue->backlog = queue->backlog->next;
896
897 request = queue->list.next;
898 list_del(request);
899
Herbert Xu0c7d4002009-08-29 20:44:04 +1000900 return (char *)list_entry(request, struct crypto_async_request, list) -
901 offset;
902}
903EXPORT_SYMBOL_GPL(__crypto_dequeue_request);
904
905struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
906{
907 return __crypto_dequeue_request(queue, 0);
Herbert Xub5b7f082007-04-16 20:48:54 +1000908}
909EXPORT_SYMBOL_GPL(crypto_dequeue_request);
910
911int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
912{
913 struct crypto_async_request *req;
914
915 list_for_each_entry(req, &queue->list, list) {
916 if (req->tfm == tfm)
917 return 1;
918 }
919
920 return 0;
921}
922EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
923
Herbert Xu76136362007-11-20 17:26:06 +0800924static inline void crypto_inc_byte(u8 *a, unsigned int size)
925{
926 u8 *b = (a + size);
927 u8 c;
928
929 for (; size; size--) {
930 c = *--b + 1;
931 *b = c;
932 if (c)
933 break;
934 }
935}
936
937void crypto_inc(u8 *a, unsigned int size)
938{
939 __be32 *b = (__be32 *)(a + size);
940 u32 c;
941
942 for (; size >= 4; size -= 4) {
943 c = be32_to_cpu(*--b) + 1;
944 *b = cpu_to_be32(c);
945 if (c)
946 return;
947 }
948
949 crypto_inc_byte(a, size);
950}
951EXPORT_SYMBOL_GPL(crypto_inc);
952
953static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
954{
955 for (; size; size--)
956 *a++ ^= *b++;
957}
958
959void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
960{
961 u32 *a = (u32 *)dst;
962 u32 *b = (u32 *)src;
963
964 for (; size >= 4; size -= 4)
965 *a++ ^= *b++;
966
967 crypto_xor_byte((u8 *)a, (u8 *)b, size);
968}
969EXPORT_SYMBOL_GPL(crypto_xor);
970
Herbert Xucce9e062006-08-21 21:08:13 +1000971static int __init crypto_algapi_init(void)
972{
973 crypto_init_proc();
974 return 0;
975}
976
977static void __exit crypto_algapi_exit(void)
978{
979 crypto_exit_proc();
980}
981
982module_init(crypto_algapi_init);
983module_exit(crypto_algapi_exit);
984
985MODULE_LICENSE("GPL");
986MODULE_DESCRIPTION("Cryptographic algorithms API");