blob: 8ff8c2656d9c492902a5608436c145965f52723f [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>
Herbert Xucce9e062006-08-21 21:08:13 +100020#include <linux/string.h>
21
22#include "internal.h"
23
Herbert Xu4cc77202006-08-06 21:16:34 +100024static LIST_HEAD(crypto_template_list);
25
Herbert Xu492e2b62006-09-21 11:35:17 +100026void crypto_larval_error(const char *name, u32 type, u32 mask)
Herbert Xu28259822006-08-06 21:23:26 +100027{
28 struct crypto_alg *alg;
29
30 down_read(&crypto_alg_sem);
Herbert Xu492e2b62006-09-21 11:35:17 +100031 alg = __crypto_alg_lookup(name, type, mask);
Herbert Xu28259822006-08-06 21:23:26 +100032 up_read(&crypto_alg_sem);
33
34 if (alg) {
35 if (crypto_is_larval(alg)) {
36 struct crypto_larval *larval = (void *)alg;
Herbert Xufe3c5202007-05-19 17:51:40 +100037 complete_all(&larval->completion);
Herbert Xu28259822006-08-06 21:23:26 +100038 }
39 crypto_mod_put(alg);
40 }
41}
42EXPORT_SYMBOL_GPL(crypto_larval_error);
43
Herbert Xucce9e062006-08-21 21:08:13 +100044static inline int crypto_set_driver_name(struct crypto_alg *alg)
45{
46 static const char suffix[] = "-generic";
47 char *driver_name = alg->cra_driver_name;
48 int len;
49
50 if (*driver_name)
51 return 0;
52
53 len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
54 if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
55 return -ENAMETOOLONG;
56
57 memcpy(driver_name + len, suffix, sizeof(suffix));
58 return 0;
59}
60
Herbert Xu4cc77202006-08-06 21:16:34 +100061static int crypto_check_alg(struct crypto_alg *alg)
Herbert Xucce9e062006-08-21 21:08:13 +100062{
Herbert Xucce9e062006-08-21 21:08:13 +100063 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
64 return -EINVAL;
65
Herbert Xucce9e062006-08-21 21:08:13 +100066 if (alg->cra_blocksize > PAGE_SIZE / 8)
67 return -EINVAL;
68
69 if (alg->cra_priority < 0)
70 return -EINVAL;
Herbert Xucce9e062006-08-21 21:08:13 +100071
Herbert Xu4cc77202006-08-06 21:16:34 +100072 return crypto_set_driver_name(alg);
73}
74
Herbert Xu6bfd4802006-09-21 11:39:29 +100075static void crypto_destroy_instance(struct crypto_alg *alg)
76{
77 struct crypto_instance *inst = (void *)alg;
78 struct crypto_template *tmpl = inst->tmpl;
79
80 tmpl->free(inst);
81 crypto_tmpl_put(tmpl);
82}
83
Herbert Xua73e6992007-04-08 21:31:36 +100084static void crypto_remove_spawn(struct crypto_spawn *spawn,
85 struct list_head *list,
86 struct list_head *secondary_spawns)
87{
88 struct crypto_instance *inst = spawn->inst;
89 struct crypto_template *tmpl = inst->tmpl;
90
91 list_del_init(&spawn->list);
92 spawn->alg = NULL;
93
94 if (crypto_is_dead(&inst->alg))
95 return;
96
97 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
98 if (!tmpl || !crypto_tmpl_get(tmpl))
99 return;
100
101 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
102 list_move(&inst->alg.cra_list, list);
103 hlist_del(&inst->list);
104 inst->alg.cra_destroy = crypto_destroy_instance;
105
106 list_splice(&inst->alg.cra_users, secondary_spawns);
107}
108
Herbert Xu6bfd4802006-09-21 11:39:29 +1000109static void crypto_remove_spawns(struct list_head *spawns,
Herbert Xua73e6992007-04-08 21:31:36 +1000110 struct list_head *list, u32 new_type)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000111{
112 struct crypto_spawn *spawn, *n;
Herbert Xua73e6992007-04-08 21:31:36 +1000113 LIST_HEAD(secondary_spawns);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000114
115 list_for_each_entry_safe(spawn, n, spawns, list) {
Herbert Xua73e6992007-04-08 21:31:36 +1000116 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000117 continue;
118
Herbert Xua73e6992007-04-08 21:31:36 +1000119 crypto_remove_spawn(spawn, list, &secondary_spawns);
120 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000121
Herbert Xua73e6992007-04-08 21:31:36 +1000122 while (!list_empty(&secondary_spawns)) {
123 list_for_each_entry_safe(spawn, n, &secondary_spawns, list)
124 crypto_remove_spawn(spawn, list, &secondary_spawns);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000125 }
126}
127
128static int __crypto_register_alg(struct crypto_alg *alg,
129 struct list_head *list)
Herbert Xu4cc77202006-08-06 21:16:34 +1000130{
131 struct crypto_alg *q;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000132 int ret = -EAGAIN;
133
134 if (crypto_is_dead(alg))
135 goto out;
136
137 INIT_LIST_HEAD(&alg->cra_users);
138
139 ret = -EEXIST;
Herbert Xu4cc77202006-08-06 21:16:34 +1000140
Herbert Xu28259822006-08-06 21:23:26 +1000141 atomic_set(&alg->cra_refcnt, 1);
Herbert Xucce9e062006-08-21 21:08:13 +1000142 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xu4cc77202006-08-06 21:16:34 +1000143 if (q == alg)
Herbert Xucce9e062006-08-21 21:08:13 +1000144 goto out;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000145
146 if (crypto_is_moribund(q))
147 continue;
148
149 if (crypto_is_larval(q)) {
Herbert Xu28259822006-08-06 21:23:26 +1000150 struct crypto_larval *larval = (void *)q;
151
Herbert Xud8058482007-10-02 22:27:29 +0800152 /*
153 * Check to see if either our generic name or
154 * specific name can satisfy the name requested
155 * by the larval entry q.
156 */
Herbert Xu6bfd4802006-09-21 11:39:29 +1000157 if (strcmp(alg->cra_name, q->cra_name) &&
158 strcmp(alg->cra_driver_name, q->cra_name))
159 continue;
160
161 if (larval->adult)
162 continue;
Herbert Xu492e2b62006-09-21 11:35:17 +1000163 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
164 continue;
Herbert Xu28259822006-08-06 21:23:26 +1000165 if (!crypto_mod_get(alg))
166 continue;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000167
Herbert Xu28259822006-08-06 21:23:26 +1000168 larval->adult = alg;
Herbert Xufe3c5202007-05-19 17:51:40 +1000169 complete_all(&larval->completion);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000170 continue;
Herbert Xu28259822006-08-06 21:23:26 +1000171 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000172
173 if (strcmp(alg->cra_name, q->cra_name))
174 continue;
175
176 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
177 q->cra_priority > alg->cra_priority)
178 continue;
179
Herbert Xua73e6992007-04-08 21:31:36 +1000180 crypto_remove_spawns(&q->cra_users, list, alg->cra_flags);
Herbert Xucce9e062006-08-21 21:08:13 +1000181 }
182
183 list_add(&alg->cra_list, &crypto_alg_list);
Herbert Xu28259822006-08-06 21:23:26 +1000184
185 crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
Herbert Xu4cc77202006-08-06 21:16:34 +1000186 ret = 0;
Herbert Xu28259822006-08-06 21:23:26 +1000187
Herbert Xucce9e062006-08-21 21:08:13 +1000188out:
Herbert Xucce9e062006-08-21 21:08:13 +1000189 return ret;
190}
Herbert Xu4cc77202006-08-06 21:16:34 +1000191
Herbert Xu6bfd4802006-09-21 11:39:29 +1000192static void crypto_remove_final(struct list_head *list)
193{
194 struct crypto_alg *alg;
195 struct crypto_alg *n;
196
197 list_for_each_entry_safe(alg, n, list, cra_list) {
198 list_del_init(&alg->cra_list);
199 crypto_alg_put(alg);
200 }
201}
202
Herbert Xu4cc77202006-08-06 21:16:34 +1000203int crypto_register_alg(struct crypto_alg *alg)
204{
Herbert Xu6bfd4802006-09-21 11:39:29 +1000205 LIST_HEAD(list);
Herbert Xu4cc77202006-08-06 21:16:34 +1000206 int err;
207
208 err = crypto_check_alg(alg);
209 if (err)
210 return err;
211
212 down_write(&crypto_alg_sem);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000213 err = __crypto_register_alg(alg, &list);
Herbert Xu4cc77202006-08-06 21:16:34 +1000214 up_write(&crypto_alg_sem);
215
Herbert Xu6bfd4802006-09-21 11:39:29 +1000216 crypto_remove_final(&list);
Herbert Xu4cc77202006-08-06 21:16:34 +1000217 return err;
218}
Herbert Xucce9e062006-08-21 21:08:13 +1000219EXPORT_SYMBOL_GPL(crypto_register_alg);
220
Herbert Xu6bfd4802006-09-21 11:39:29 +1000221static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
222{
223 if (unlikely(list_empty(&alg->cra_list)))
224 return -ENOENT;
225
226 alg->cra_flags |= CRYPTO_ALG_DEAD;
227
228 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
229 list_del_init(&alg->cra_list);
Herbert Xua73e6992007-04-08 21:31:36 +1000230 crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000231
232 return 0;
233}
234
Herbert Xucce9e062006-08-21 21:08:13 +1000235int crypto_unregister_alg(struct crypto_alg *alg)
236{
Herbert Xu6bfd4802006-09-21 11:39:29 +1000237 int ret;
238 LIST_HEAD(list);
Herbert Xucce9e062006-08-21 21:08:13 +1000239
240 down_write(&crypto_alg_sem);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000241 ret = crypto_remove_alg(alg, &list);
Herbert Xucce9e062006-08-21 21:08:13 +1000242 up_write(&crypto_alg_sem);
243
244 if (ret)
245 return ret;
246
247 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
248 if (alg->cra_destroy)
249 alg->cra_destroy(alg);
250
Herbert Xu6bfd4802006-09-21 11:39:29 +1000251 crypto_remove_final(&list);
Herbert Xucce9e062006-08-21 21:08:13 +1000252 return 0;
253}
254EXPORT_SYMBOL_GPL(crypto_unregister_alg);
255
Herbert Xu4cc77202006-08-06 21:16:34 +1000256int crypto_register_template(struct crypto_template *tmpl)
257{
258 struct crypto_template *q;
259 int err = -EEXIST;
260
261 down_write(&crypto_alg_sem);
262
263 list_for_each_entry(q, &crypto_template_list, list) {
264 if (q == tmpl)
265 goto out;
266 }
267
268 list_add(&tmpl->list, &crypto_template_list);
Herbert Xu28259822006-08-06 21:23:26 +1000269 crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
Herbert Xu4cc77202006-08-06 21:16:34 +1000270 err = 0;
271out:
272 up_write(&crypto_alg_sem);
273 return err;
274}
275EXPORT_SYMBOL_GPL(crypto_register_template);
276
277void crypto_unregister_template(struct crypto_template *tmpl)
278{
279 struct crypto_instance *inst;
280 struct hlist_node *p, *n;
281 struct hlist_head *list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000282 LIST_HEAD(users);
Herbert Xu4cc77202006-08-06 21:16:34 +1000283
284 down_write(&crypto_alg_sem);
285
286 BUG_ON(list_empty(&tmpl->list));
287 list_del_init(&tmpl->list);
288
289 list = &tmpl->instances;
290 hlist_for_each_entry(inst, p, list, list) {
Herbert Xu6bfd4802006-09-21 11:39:29 +1000291 int err = crypto_remove_alg(&inst->alg, &users);
292 BUG_ON(err);
Herbert Xu4cc77202006-08-06 21:16:34 +1000293 }
294
Herbert Xu28259822006-08-06 21:23:26 +1000295 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
296
Herbert Xu4cc77202006-08-06 21:16:34 +1000297 up_write(&crypto_alg_sem);
298
299 hlist_for_each_entry_safe(inst, p, n, list, list) {
300 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
301 tmpl->free(inst);
302 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000303 crypto_remove_final(&users);
Herbert Xu4cc77202006-08-06 21:16:34 +1000304}
305EXPORT_SYMBOL_GPL(crypto_unregister_template);
306
307static struct crypto_template *__crypto_lookup_template(const char *name)
308{
309 struct crypto_template *q, *tmpl = NULL;
310
311 down_read(&crypto_alg_sem);
312 list_for_each_entry(q, &crypto_template_list, list) {
313 if (strcmp(q->name, name))
314 continue;
315 if (unlikely(!crypto_tmpl_get(q)))
316 continue;
317
318 tmpl = q;
319 break;
320 }
321 up_read(&crypto_alg_sem);
322
323 return tmpl;
324}
325
326struct crypto_template *crypto_lookup_template(const char *name)
327{
328 return try_then_request_module(__crypto_lookup_template(name), name);
329}
330EXPORT_SYMBOL_GPL(crypto_lookup_template);
331
332int crypto_register_instance(struct crypto_template *tmpl,
333 struct crypto_instance *inst)
334{
Herbert Xu6bfd4802006-09-21 11:39:29 +1000335 LIST_HEAD(list);
Herbert Xu4cc77202006-08-06 21:16:34 +1000336 int err = -EINVAL;
337
338 if (inst->alg.cra_destroy)
339 goto err;
340
341 err = crypto_check_alg(&inst->alg);
342 if (err)
343 goto err;
344
345 inst->alg.cra_module = tmpl->module;
346
347 down_write(&crypto_alg_sem);
348
Herbert Xu6bfd4802006-09-21 11:39:29 +1000349 err = __crypto_register_alg(&inst->alg, &list);
Herbert Xu4cc77202006-08-06 21:16:34 +1000350 if (err)
351 goto unlock;
352
353 hlist_add_head(&inst->list, &tmpl->instances);
354 inst->tmpl = tmpl;
355
356unlock:
357 up_write(&crypto_alg_sem);
358
Herbert Xu6bfd4802006-09-21 11:39:29 +1000359 crypto_remove_final(&list);
360
Herbert Xu4cc77202006-08-06 21:16:34 +1000361err:
362 return err;
363}
364EXPORT_SYMBOL_GPL(crypto_register_instance);
365
Herbert Xu6bfd4802006-09-21 11:39:29 +1000366int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
Herbert Xua73e6992007-04-08 21:31:36 +1000367 struct crypto_instance *inst, u32 mask)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000368{
369 int err = -EAGAIN;
370
371 spawn->inst = inst;
Herbert Xua73e6992007-04-08 21:31:36 +1000372 spawn->mask = mask;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000373
374 down_write(&crypto_alg_sem);
375 if (!crypto_is_moribund(alg)) {
376 list_add(&spawn->list, &alg->cra_users);
377 spawn->alg = alg;
378 err = 0;
379 }
380 up_write(&crypto_alg_sem);
381
382 return err;
383}
384EXPORT_SYMBOL_GPL(crypto_init_spawn);
385
386void crypto_drop_spawn(struct crypto_spawn *spawn)
387{
388 down_write(&crypto_alg_sem);
389 list_del(&spawn->list);
390 up_write(&crypto_alg_sem);
391}
392EXPORT_SYMBOL_GPL(crypto_drop_spawn);
393
Herbert Xu2e306ee2006-12-17 10:05:58 +1100394struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
395 u32 mask)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000396{
397 struct crypto_alg *alg;
398 struct crypto_alg *alg2;
399 struct crypto_tfm *tfm;
400
401 down_read(&crypto_alg_sem);
402 alg = spawn->alg;
403 alg2 = alg;
404 if (alg2)
405 alg2 = crypto_mod_get(alg2);
406 up_read(&crypto_alg_sem);
407
408 if (!alg2) {
409 if (alg)
410 crypto_shoot_alg(alg);
411 return ERR_PTR(-EAGAIN);
412 }
413
Herbert Xu2e306ee2006-12-17 10:05:58 +1100414 tfm = ERR_PTR(-EINVAL);
415 if (unlikely((alg->cra_flags ^ type) & mask))
416 goto out_put_alg;
417
Herbert Xu27d2a332007-01-24 20:50:26 +1100418 tfm = __crypto_alloc_tfm(alg, type, mask);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000419 if (IS_ERR(tfm))
Herbert Xu2e306ee2006-12-17 10:05:58 +1100420 goto out_put_alg;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000421
422 return tfm;
Herbert Xu2e306ee2006-12-17 10:05:58 +1100423
424out_put_alg:
425 crypto_mod_put(alg);
426 return tfm;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000427}
428EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
429
Herbert Xu28259822006-08-06 21:23:26 +1000430int crypto_register_notifier(struct notifier_block *nb)
431{
432 return blocking_notifier_chain_register(&crypto_chain, nb);
433}
434EXPORT_SYMBOL_GPL(crypto_register_notifier);
435
436int crypto_unregister_notifier(struct notifier_block *nb)
437{
438 return blocking_notifier_chain_unregister(&crypto_chain, nb);
439}
440EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
441
Herbert Xuebc610e2007-01-01 18:37:02 +1100442struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000443{
Herbert Xu39e1ee012007-08-29 19:27:26 +0800444 struct rtattr *rta = tb[0];
Herbert Xuebc610e2007-01-01 18:37:02 +1100445 struct crypto_attr_type *algt;
446
447 if (!rta)
448 return ERR_PTR(-ENOENT);
449 if (RTA_PAYLOAD(rta) < sizeof(*algt))
450 return ERR_PTR(-EINVAL);
Herbert Xu39e1ee012007-08-29 19:27:26 +0800451 if (rta->rta_type != CRYPTOA_TYPE)
452 return ERR_PTR(-EINVAL);
Herbert Xuebc610e2007-01-01 18:37:02 +1100453
454 algt = RTA_DATA(rta);
455
456 return algt;
457}
458EXPORT_SYMBOL_GPL(crypto_get_attr_type);
459
460int crypto_check_attr_type(struct rtattr **tb, u32 type)
461{
462 struct crypto_attr_type *algt;
463
464 algt = crypto_get_attr_type(tb);
465 if (IS_ERR(algt))
466 return PTR_ERR(algt);
467
468 if ((algt->type ^ type) & algt->mask)
469 return -EINVAL;
470
471 return 0;
472}
473EXPORT_SYMBOL_GPL(crypto_check_attr_type);
474
Herbert Xu3c09f172007-08-30 16:24:15 +0800475struct crypto_alg *crypto_attr_alg(struct rtattr *rta, u32 type, u32 mask)
Herbert Xuebc610e2007-01-01 18:37:02 +1100476{
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000477 struct crypto_attr_alg *alga;
478
Herbert Xuebc610e2007-01-01 18:37:02 +1100479 if (!rta)
480 return ERR_PTR(-ENOENT);
481 if (RTA_PAYLOAD(rta) < sizeof(*alga))
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000482 return ERR_PTR(-EINVAL);
Herbert Xu39e1ee012007-08-29 19:27:26 +0800483 if (rta->rta_type != CRYPTOA_ALG)
484 return ERR_PTR(-EINVAL);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000485
486 alga = RTA_DATA(rta);
487 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
488
489 return crypto_alg_mod_lookup(alga->name, type, mask);
490}
Herbert Xu3c09f172007-08-30 16:24:15 +0800491EXPORT_SYMBOL_GPL(crypto_attr_alg);
492
493int crypto_attr_u32(struct rtattr *rta, u32 *num)
494{
495 struct crypto_attr_u32 *nu32;
496
497 if (!rta)
498 return -ENOENT;
499 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
500 return -EINVAL;
501 if (rta->rta_type != CRYPTOA_U32)
502 return -EINVAL;
503
504 nu32 = RTA_DATA(rta);
505 *num = nu32->num;
506
507 return 0;
508}
509EXPORT_SYMBOL_GPL(crypto_attr_u32);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000510
511struct crypto_instance *crypto_alloc_instance(const char *name,
512 struct crypto_alg *alg)
513{
514 struct crypto_instance *inst;
515 struct crypto_spawn *spawn;
516 int err;
517
518 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
519 if (!inst)
520 return ERR_PTR(-ENOMEM);
521
522 err = -ENAMETOOLONG;
523 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
524 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
525 goto err_free_inst;
526
527 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
528 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
529 goto err_free_inst;
530
531 spawn = crypto_instance_ctx(inst);
Herbert Xua73e6992007-04-08 21:31:36 +1000532 err = crypto_init_spawn(spawn, alg, inst,
533 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000534
535 if (err)
536 goto err_free_inst;
537
538 return inst;
539
540err_free_inst:
541 kfree(inst);
542 return ERR_PTR(err);
543}
544EXPORT_SYMBOL_GPL(crypto_alloc_instance);
545
Herbert Xub5b7f082007-04-16 20:48:54 +1000546void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
547{
548 INIT_LIST_HEAD(&queue->list);
549 queue->backlog = &queue->list;
550 queue->qlen = 0;
551 queue->max_qlen = max_qlen;
552}
553EXPORT_SYMBOL_GPL(crypto_init_queue);
554
555int crypto_enqueue_request(struct crypto_queue *queue,
556 struct crypto_async_request *request)
557{
558 int err = -EINPROGRESS;
559
560 if (unlikely(queue->qlen >= queue->max_qlen)) {
561 err = -EBUSY;
562 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
563 goto out;
564 if (queue->backlog == &queue->list)
565 queue->backlog = &request->list;
566 }
567
568 queue->qlen++;
569 list_add_tail(&request->list, &queue->list);
570
571out:
572 return err;
573}
574EXPORT_SYMBOL_GPL(crypto_enqueue_request);
575
576struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
577{
578 struct list_head *request;
579
580 if (unlikely(!queue->qlen))
581 return NULL;
582
583 queue->qlen--;
584
585 if (queue->backlog != &queue->list)
586 queue->backlog = queue->backlog->next;
587
588 request = queue->list.next;
589 list_del(request);
590
591 return list_entry(request, struct crypto_async_request, list);
592}
593EXPORT_SYMBOL_GPL(crypto_dequeue_request);
594
595int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
596{
597 struct crypto_async_request *req;
598
599 list_for_each_entry(req, &queue->list, list) {
600 if (req->tfm == tfm)
601 return 1;
602 }
603
604 return 0;
605}
606EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
607
Herbert Xucce9e062006-08-21 21:08:13 +1000608static int __init crypto_algapi_init(void)
609{
610 crypto_init_proc();
611 return 0;
612}
613
614static void __exit crypto_algapi_exit(void)
615{
616 crypto_exit_proc();
617}
618
619module_init(crypto_algapi_init);
620module_exit(crypto_algapi_exit);
621
622MODULE_LICENSE("GPL");
623MODULE_DESCRIPTION("Cryptographic algorithms API");