blob: 2154815201a724f473d67c18d7ade8bd2cbdc5e3 [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 Xu73d38642008-08-03 21:15:23 +080024static void crypto_remove_final(struct list_head *list);
25
Herbert Xu4cc77202006-08-06 21:16:34 +100026static LIST_HEAD(crypto_template_list);
27
Herbert Xu492e2b62006-09-21 11:35:17 +100028void crypto_larval_error(const char *name, u32 type, u32 mask)
Herbert Xu28259822006-08-06 21:23:26 +100029{
30 struct crypto_alg *alg;
31
Herbert Xuc51b6c82008-08-04 11:44:59 +080032 alg = crypto_alg_lookup(name, type, mask);
Herbert Xu28259822006-08-06 21:23:26 +100033
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;
Herbert Xu38cb2412007-11-17 22:09:38 +080098 if (hlist_unhashed(&inst->list))
99 return;
100
Herbert Xua73e6992007-04-08 21:31:36 +1000101 if (!tmpl || !crypto_tmpl_get(tmpl))
102 return;
103
104 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
105 list_move(&inst->alg.cra_list, list);
106 hlist_del(&inst->list);
107 inst->alg.cra_destroy = crypto_destroy_instance;
108
109 list_splice(&inst->alg.cra_users, secondary_spawns);
110}
111
Herbert Xu6bfd4802006-09-21 11:39:29 +1000112static void crypto_remove_spawns(struct list_head *spawns,
Herbert Xua73e6992007-04-08 21:31:36 +1000113 struct list_head *list, u32 new_type)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000114{
115 struct crypto_spawn *spawn, *n;
Herbert Xua73e6992007-04-08 21:31:36 +1000116 LIST_HEAD(secondary_spawns);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000117
118 list_for_each_entry_safe(spawn, n, spawns, list) {
Herbert Xua73e6992007-04-08 21:31:36 +1000119 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000120 continue;
121
Herbert Xua73e6992007-04-08 21:31:36 +1000122 crypto_remove_spawn(spawn, list, &secondary_spawns);
123 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000124
Herbert Xua73e6992007-04-08 21:31:36 +1000125 while (!list_empty(&secondary_spawns)) {
126 list_for_each_entry_safe(spawn, n, &secondary_spawns, list)
127 crypto_remove_spawn(spawn, list, &secondary_spawns);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000128 }
129}
130
Herbert Xu73d38642008-08-03 21:15:23 +0800131static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
Herbert Xu4cc77202006-08-06 21:16:34 +1000132{
133 struct crypto_alg *q;
Herbert Xu73d38642008-08-03 21:15:23 +0800134 struct crypto_larval *larval;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000135 int ret = -EAGAIN;
136
137 if (crypto_is_dead(alg))
Herbert Xu73d38642008-08-03 21:15:23 +0800138 goto err;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000139
140 INIT_LIST_HEAD(&alg->cra_users);
141
Herbert Xu73d38642008-08-03 21:15:23 +0800142 /* No cheating! */
143 alg->cra_flags &= ~CRYPTO_ALG_TESTED;
144
Herbert Xu6bfd4802006-09-21 11:39:29 +1000145 ret = -EEXIST;
Herbert Xu4cc77202006-08-06 21:16:34 +1000146
Herbert Xu28259822006-08-06 21:23:26 +1000147 atomic_set(&alg->cra_refcnt, 1);
Herbert Xucce9e062006-08-21 21:08:13 +1000148 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xu4cc77202006-08-06 21:16:34 +1000149 if (q == alg)
Herbert Xu73d38642008-08-03 21:15:23 +0800150 goto err;
151
Herbert Xub8e15992009-01-28 14:09:59 +1100152 if (crypto_is_moribund(q))
153 continue;
154
Herbert Xu73d38642008-08-03 21:15:23 +0800155 if (crypto_is_larval(q)) {
156 if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
157 goto err;
158 continue;
159 }
160
161 if (!strcmp(q->cra_driver_name, alg->cra_name) ||
162 !strcmp(q->cra_name, alg->cra_driver_name))
163 goto err;
164 }
165
166 larval = crypto_larval_alloc(alg->cra_name,
167 alg->cra_flags | CRYPTO_ALG_TESTED, 0);
168 if (IS_ERR(larval))
169 goto out;
170
171 ret = -ENOENT;
172 larval->adult = crypto_mod_get(alg);
173 if (!larval->adult)
174 goto free_larval;
175
176 atomic_set(&larval->alg.cra_refcnt, 1);
177 memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
178 CRYPTO_MAX_ALG_NAME);
179 larval->alg.cra_priority = alg->cra_priority;
180
181 list_add(&alg->cra_list, &crypto_alg_list);
182 list_add(&larval->alg.cra_list, &crypto_alg_list);
183
184out:
185 return larval;
186
187free_larval:
188 kfree(larval);
189err:
190 larval = ERR_PTR(ret);
191 goto out;
192}
193
194void crypto_alg_tested(const char *name, int err)
195{
196 struct crypto_larval *test;
197 struct crypto_alg *alg;
198 struct crypto_alg *q;
199 LIST_HEAD(list);
200
201 down_write(&crypto_alg_sem);
202 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xub8e15992009-01-28 14:09:59 +1100203 if (crypto_is_moribund(q) || !crypto_is_larval(q))
Herbert Xu73d38642008-08-03 21:15:23 +0800204 continue;
205
206 test = (struct crypto_larval *)q;
207
208 if (!strcmp(q->cra_driver_name, name))
209 goto found;
210 }
211
212 printk(KERN_ERR "alg: Unexpected test result for %s: %d\n", name, err);
213 goto unlock;
214
215found:
Herbert Xub8e15992009-01-28 14:09:59 +1100216 q->cra_flags |= CRYPTO_ALG_DEAD;
Herbert Xu73d38642008-08-03 21:15:23 +0800217 alg = test->adult;
218 if (err || list_empty(&alg->cra_list))
219 goto complete;
220
221 alg->cra_flags |= CRYPTO_ALG_TESTED;
222
223 list_for_each_entry(q, &crypto_alg_list, cra_list) {
224 if (q == alg)
225 continue;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000226
227 if (crypto_is_moribund(q))
228 continue;
229
230 if (crypto_is_larval(q)) {
Herbert Xu28259822006-08-06 21:23:26 +1000231 struct crypto_larval *larval = (void *)q;
232
Herbert Xud8058482007-10-02 22:27:29 +0800233 /*
234 * Check to see if either our generic name or
235 * specific name can satisfy the name requested
236 * by the larval entry q.
237 */
Herbert Xu6bfd4802006-09-21 11:39:29 +1000238 if (strcmp(alg->cra_name, q->cra_name) &&
239 strcmp(alg->cra_driver_name, q->cra_name))
240 continue;
241
242 if (larval->adult)
243 continue;
Herbert Xu492e2b62006-09-21 11:35:17 +1000244 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
245 continue;
Herbert Xu28259822006-08-06 21:23:26 +1000246 if (!crypto_mod_get(alg))
247 continue;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000248
Herbert Xu28259822006-08-06 21:23:26 +1000249 larval->adult = alg;
Herbert Xufe3c5202007-05-19 17:51:40 +1000250 complete_all(&larval->completion);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000251 continue;
Herbert Xu28259822006-08-06 21:23:26 +1000252 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000253
254 if (strcmp(alg->cra_name, q->cra_name))
255 continue;
256
257 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
258 q->cra_priority > alg->cra_priority)
259 continue;
260
Herbert Xu73d38642008-08-03 21:15:23 +0800261 crypto_remove_spawns(&q->cra_users, &list, alg->cra_flags);
Herbert Xucce9e062006-08-21 21:08:13 +1000262 }
Herbert Xu28259822006-08-06 21:23:26 +1000263
Herbert Xu73d38642008-08-03 21:15:23 +0800264complete:
265 complete_all(&test->completion);
Herbert Xu28259822006-08-06 21:23:26 +1000266
Herbert Xu73d38642008-08-03 21:15:23 +0800267unlock:
268 up_write(&crypto_alg_sem);
269
270 crypto_remove_final(&list);
Herbert Xucce9e062006-08-21 21:08:13 +1000271}
Herbert Xu73d38642008-08-03 21:15:23 +0800272EXPORT_SYMBOL_GPL(crypto_alg_tested);
Herbert Xu4cc77202006-08-06 21:16:34 +1000273
Herbert Xu6bfd4802006-09-21 11:39:29 +1000274static void crypto_remove_final(struct list_head *list)
275{
276 struct crypto_alg *alg;
277 struct crypto_alg *n;
278
279 list_for_each_entry_safe(alg, n, list, cra_list) {
280 list_del_init(&alg->cra_list);
281 crypto_alg_put(alg);
282 }
283}
284
Herbert Xu73d38642008-08-03 21:15:23 +0800285static void crypto_wait_for_test(struct crypto_larval *larval)
286{
287 int err;
288
289 err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
290 if (err != NOTIFY_STOP) {
291 if (WARN_ON(err != NOTIFY_DONE))
292 goto out;
293 crypto_alg_tested(larval->alg.cra_driver_name, 0);
294 }
295
296 err = wait_for_completion_interruptible(&larval->completion);
297 WARN_ON(err);
298
299out:
300 crypto_larval_kill(&larval->alg);
301}
302
Herbert Xu4cc77202006-08-06 21:16:34 +1000303int crypto_register_alg(struct crypto_alg *alg)
304{
Herbert Xu73d38642008-08-03 21:15:23 +0800305 struct crypto_larval *larval;
Herbert Xu4cc77202006-08-06 21:16:34 +1000306 int err;
307
308 err = crypto_check_alg(alg);
309 if (err)
310 return err;
311
312 down_write(&crypto_alg_sem);
Herbert Xu73d38642008-08-03 21:15:23 +0800313 larval = __crypto_register_alg(alg);
Herbert Xu4cc77202006-08-06 21:16:34 +1000314 up_write(&crypto_alg_sem);
315
Herbert Xu73d38642008-08-03 21:15:23 +0800316 if (IS_ERR(larval))
317 return PTR_ERR(larval);
318
319 crypto_wait_for_test(larval);
320 return 0;
Herbert Xu4cc77202006-08-06 21:16:34 +1000321}
Herbert Xucce9e062006-08-21 21:08:13 +1000322EXPORT_SYMBOL_GPL(crypto_register_alg);
323
Herbert Xu6bfd4802006-09-21 11:39:29 +1000324static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
325{
326 if (unlikely(list_empty(&alg->cra_list)))
327 return -ENOENT;
328
329 alg->cra_flags |= CRYPTO_ALG_DEAD;
330
331 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
332 list_del_init(&alg->cra_list);
Herbert Xua73e6992007-04-08 21:31:36 +1000333 crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000334
335 return 0;
336}
337
Herbert Xucce9e062006-08-21 21:08:13 +1000338int crypto_unregister_alg(struct crypto_alg *alg)
339{
Herbert Xu6bfd4802006-09-21 11:39:29 +1000340 int ret;
341 LIST_HEAD(list);
Herbert Xucce9e062006-08-21 21:08:13 +1000342
343 down_write(&crypto_alg_sem);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000344 ret = crypto_remove_alg(alg, &list);
Herbert Xucce9e062006-08-21 21:08:13 +1000345 up_write(&crypto_alg_sem);
346
347 if (ret)
348 return ret;
349
350 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
351 if (alg->cra_destroy)
352 alg->cra_destroy(alg);
353
Herbert Xu6bfd4802006-09-21 11:39:29 +1000354 crypto_remove_final(&list);
Herbert Xucce9e062006-08-21 21:08:13 +1000355 return 0;
356}
357EXPORT_SYMBOL_GPL(crypto_unregister_alg);
358
Herbert Xu4cc77202006-08-06 21:16:34 +1000359int crypto_register_template(struct crypto_template *tmpl)
360{
361 struct crypto_template *q;
362 int err = -EEXIST;
363
364 down_write(&crypto_alg_sem);
365
366 list_for_each_entry(q, &crypto_template_list, list) {
367 if (q == tmpl)
368 goto out;
369 }
370
371 list_add(&tmpl->list, &crypto_template_list);
Herbert Xu28259822006-08-06 21:23:26 +1000372 crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
Herbert Xu4cc77202006-08-06 21:16:34 +1000373 err = 0;
374out:
375 up_write(&crypto_alg_sem);
376 return err;
377}
378EXPORT_SYMBOL_GPL(crypto_register_template);
379
380void crypto_unregister_template(struct crypto_template *tmpl)
381{
382 struct crypto_instance *inst;
383 struct hlist_node *p, *n;
384 struct hlist_head *list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000385 LIST_HEAD(users);
Herbert Xu4cc77202006-08-06 21:16:34 +1000386
387 down_write(&crypto_alg_sem);
388
389 BUG_ON(list_empty(&tmpl->list));
390 list_del_init(&tmpl->list);
391
392 list = &tmpl->instances;
393 hlist_for_each_entry(inst, p, list, list) {
Herbert Xu6bfd4802006-09-21 11:39:29 +1000394 int err = crypto_remove_alg(&inst->alg, &users);
395 BUG_ON(err);
Herbert Xu4cc77202006-08-06 21:16:34 +1000396 }
397
Herbert Xu28259822006-08-06 21:23:26 +1000398 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
399
Herbert Xu4cc77202006-08-06 21:16:34 +1000400 up_write(&crypto_alg_sem);
401
402 hlist_for_each_entry_safe(inst, p, n, list, list) {
403 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
404 tmpl->free(inst);
405 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000406 crypto_remove_final(&users);
Herbert Xu4cc77202006-08-06 21:16:34 +1000407}
408EXPORT_SYMBOL_GPL(crypto_unregister_template);
409
410static struct crypto_template *__crypto_lookup_template(const char *name)
411{
412 struct crypto_template *q, *tmpl = NULL;
413
414 down_read(&crypto_alg_sem);
415 list_for_each_entry(q, &crypto_template_list, list) {
416 if (strcmp(q->name, name))
417 continue;
418 if (unlikely(!crypto_tmpl_get(q)))
419 continue;
420
421 tmpl = q;
422 break;
423 }
424 up_read(&crypto_alg_sem);
425
426 return tmpl;
427}
428
429struct crypto_template *crypto_lookup_template(const char *name)
430{
431 return try_then_request_module(__crypto_lookup_template(name), name);
432}
433EXPORT_SYMBOL_GPL(crypto_lookup_template);
434
435int crypto_register_instance(struct crypto_template *tmpl,
436 struct crypto_instance *inst)
437{
Herbert Xu73d38642008-08-03 21:15:23 +0800438 struct crypto_larval *larval;
439 int err;
Herbert Xu4cc77202006-08-06 21:16:34 +1000440
Herbert Xu4cc77202006-08-06 21:16:34 +1000441 err = crypto_check_alg(&inst->alg);
442 if (err)
443 goto err;
444
445 inst->alg.cra_module = tmpl->module;
446
447 down_write(&crypto_alg_sem);
448
Herbert Xu73d38642008-08-03 21:15:23 +0800449 larval = __crypto_register_alg(&inst->alg);
450 if (IS_ERR(larval))
Herbert Xu4cc77202006-08-06 21:16:34 +1000451 goto unlock;
452
453 hlist_add_head(&inst->list, &tmpl->instances);
454 inst->tmpl = tmpl;
455
456unlock:
457 up_write(&crypto_alg_sem);
458
Herbert Xu73d38642008-08-03 21:15:23 +0800459 err = PTR_ERR(larval);
460 if (IS_ERR(larval))
461 goto err;
462
463 crypto_wait_for_test(larval);
464 err = 0;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000465
Herbert Xu4cc77202006-08-06 21:16:34 +1000466err:
467 return err;
468}
469EXPORT_SYMBOL_GPL(crypto_register_instance);
470
Herbert Xu6bfd4802006-09-21 11:39:29 +1000471int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
Herbert Xua73e6992007-04-08 21:31:36 +1000472 struct crypto_instance *inst, u32 mask)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000473{
474 int err = -EAGAIN;
475
476 spawn->inst = inst;
Herbert Xua73e6992007-04-08 21:31:36 +1000477 spawn->mask = mask;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000478
479 down_write(&crypto_alg_sem);
480 if (!crypto_is_moribund(alg)) {
481 list_add(&spawn->list, &alg->cra_users);
482 spawn->alg = alg;
483 err = 0;
484 }
485 up_write(&crypto_alg_sem);
486
487 return err;
488}
489EXPORT_SYMBOL_GPL(crypto_init_spawn);
490
Herbert Xu97eedce2009-07-08 15:55:52 +0800491int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,
492 struct crypto_instance *inst,
493 const struct crypto_type *frontend)
494{
495 int err = -EINVAL;
496
497 if (frontend && (alg->cra_flags ^ frontend->type) & frontend->maskset)
498 goto out;
499
500 spawn->frontend = frontend;
501 err = crypto_init_spawn(spawn, alg, inst, frontend->maskset);
502
503out:
504 return err;
505}
506EXPORT_SYMBOL_GPL(crypto_init_spawn2);
507
Herbert Xu6bfd4802006-09-21 11:39:29 +1000508void crypto_drop_spawn(struct crypto_spawn *spawn)
509{
510 down_write(&crypto_alg_sem);
511 list_del(&spawn->list);
512 up_write(&crypto_alg_sem);
513}
514EXPORT_SYMBOL_GPL(crypto_drop_spawn);
515
Herbert Xu97eedce2009-07-08 15:55:52 +0800516static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000517{
518 struct crypto_alg *alg;
519 struct crypto_alg *alg2;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000520
521 down_read(&crypto_alg_sem);
522 alg = spawn->alg;
523 alg2 = alg;
524 if (alg2)
525 alg2 = crypto_mod_get(alg2);
526 up_read(&crypto_alg_sem);
527
528 if (!alg2) {
529 if (alg)
530 crypto_shoot_alg(alg);
531 return ERR_PTR(-EAGAIN);
532 }
533
Herbert Xu97eedce2009-07-08 15:55:52 +0800534 return alg;
535}
536
537struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
538 u32 mask)
539{
540 struct crypto_alg *alg;
541 struct crypto_tfm *tfm;
542
543 alg = crypto_spawn_alg(spawn);
544 if (IS_ERR(alg))
545 return ERR_CAST(alg);
546
Herbert Xu2e306ee2006-12-17 10:05:58 +1100547 tfm = ERR_PTR(-EINVAL);
548 if (unlikely((alg->cra_flags ^ type) & mask))
549 goto out_put_alg;
550
Herbert Xu27d2a332007-01-24 20:50:26 +1100551 tfm = __crypto_alloc_tfm(alg, type, mask);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000552 if (IS_ERR(tfm))
Herbert Xu2e306ee2006-12-17 10:05:58 +1100553 goto out_put_alg;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000554
555 return tfm;
Herbert Xu2e306ee2006-12-17 10:05:58 +1100556
557out_put_alg:
558 crypto_mod_put(alg);
559 return tfm;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000560}
561EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
562
Herbert Xu97eedce2009-07-08 15:55:52 +0800563void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
564{
565 struct crypto_alg *alg;
566 struct crypto_tfm *tfm;
567
568 alg = crypto_spawn_alg(spawn);
569 if (IS_ERR(alg))
570 return ERR_CAST(alg);
571
572 tfm = crypto_create_tfm(alg, spawn->frontend);
573 if (IS_ERR(tfm))
574 goto out_put_alg;
575
576 return tfm;
577
578out_put_alg:
579 crypto_mod_put(alg);
580 return tfm;
581}
582EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
583
Herbert Xu28259822006-08-06 21:23:26 +1000584int crypto_register_notifier(struct notifier_block *nb)
585{
586 return blocking_notifier_chain_register(&crypto_chain, nb);
587}
588EXPORT_SYMBOL_GPL(crypto_register_notifier);
589
590int crypto_unregister_notifier(struct notifier_block *nb)
591{
592 return blocking_notifier_chain_unregister(&crypto_chain, nb);
593}
594EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
595
Herbert Xuebc610e2007-01-01 18:37:02 +1100596struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000597{
Herbert Xu39e1ee012007-08-29 19:27:26 +0800598 struct rtattr *rta = tb[0];
Herbert Xuebc610e2007-01-01 18:37:02 +1100599 struct crypto_attr_type *algt;
600
601 if (!rta)
602 return ERR_PTR(-ENOENT);
603 if (RTA_PAYLOAD(rta) < sizeof(*algt))
604 return ERR_PTR(-EINVAL);
Herbert Xu39e1ee012007-08-29 19:27:26 +0800605 if (rta->rta_type != CRYPTOA_TYPE)
606 return ERR_PTR(-EINVAL);
Herbert Xuebc610e2007-01-01 18:37:02 +1100607
608 algt = RTA_DATA(rta);
609
610 return algt;
611}
612EXPORT_SYMBOL_GPL(crypto_get_attr_type);
613
614int crypto_check_attr_type(struct rtattr **tb, u32 type)
615{
616 struct crypto_attr_type *algt;
617
618 algt = crypto_get_attr_type(tb);
619 if (IS_ERR(algt))
620 return PTR_ERR(algt);
621
622 if ((algt->type ^ type) & algt->mask)
623 return -EINVAL;
624
625 return 0;
626}
627EXPORT_SYMBOL_GPL(crypto_check_attr_type);
628
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800629const char *crypto_attr_alg_name(struct rtattr *rta)
Herbert Xuebc610e2007-01-01 18:37:02 +1100630{
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000631 struct crypto_attr_alg *alga;
632
Herbert Xuebc610e2007-01-01 18:37:02 +1100633 if (!rta)
634 return ERR_PTR(-ENOENT);
635 if (RTA_PAYLOAD(rta) < sizeof(*alga))
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000636 return ERR_PTR(-EINVAL);
Herbert Xu39e1ee012007-08-29 19:27:26 +0800637 if (rta->rta_type != CRYPTOA_ALG)
638 return ERR_PTR(-EINVAL);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000639
640 alga = RTA_DATA(rta);
641 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
642
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800643 return alga->name;
644}
645EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
646
Herbert Xud06854f2009-07-08 17:53:16 +0800647struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
648 const struct crypto_type *frontend,
649 u32 type, u32 mask)
Herbert Xu68b6c7d2007-12-07 20:18:17 +0800650{
651 const char *name;
652 int err;
653
654 name = crypto_attr_alg_name(rta);
655 err = PTR_ERR(name);
656 if (IS_ERR(name))
657 return ERR_PTR(err);
658
Herbert Xud06854f2009-07-08 17:53:16 +0800659 return crypto_find_alg(name, frontend, type, mask);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000660}
Herbert Xud06854f2009-07-08 17:53:16 +0800661EXPORT_SYMBOL_GPL(crypto_attr_alg2);
Herbert Xu3c09f172007-08-30 16:24:15 +0800662
663int crypto_attr_u32(struct rtattr *rta, u32 *num)
664{
665 struct crypto_attr_u32 *nu32;
666
667 if (!rta)
668 return -ENOENT;
669 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
670 return -EINVAL;
671 if (rta->rta_type != CRYPTOA_U32)
672 return -EINVAL;
673
674 nu32 = RTA_DATA(rta);
675 *num = nu32->num;
676
677 return 0;
678}
679EXPORT_SYMBOL_GPL(crypto_attr_u32);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000680
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800681void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
682 unsigned int head)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000683{
684 struct crypto_instance *inst;
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800685 char *p;
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000686 int err;
687
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800688 p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
689 GFP_KERNEL);
690 if (!p)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000691 return ERR_PTR(-ENOMEM);
692
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800693 inst = (void *)(p + head);
694
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000695 err = -ENAMETOOLONG;
696 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
697 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
698 goto err_free_inst;
699
700 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
701 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
702 goto err_free_inst;
703
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800704 return p;
705
706err_free_inst:
707 kfree(p);
708 return ERR_PTR(err);
709}
710EXPORT_SYMBOL_GPL(crypto_alloc_instance2);
711
712struct crypto_instance *crypto_alloc_instance(const char *name,
713 struct crypto_alg *alg)
714{
715 struct crypto_instance *inst;
716 struct crypto_spawn *spawn;
717 int err;
718
719 inst = crypto_alloc_instance2(name, alg, 0);
720 if (IS_ERR(inst))
721 goto out;
722
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000723 spawn = crypto_instance_ctx(inst);
Herbert Xua73e6992007-04-08 21:31:36 +1000724 err = crypto_init_spawn(spawn, alg, inst,
725 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000726
727 if (err)
728 goto err_free_inst;
729
730 return inst;
731
732err_free_inst:
733 kfree(inst);
Herbert Xu70ec7bb2009-07-07 14:07:37 +0800734 inst = ERR_PTR(err);
735
736out:
737 return inst;
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000738}
739EXPORT_SYMBOL_GPL(crypto_alloc_instance);
740
Herbert Xub5b7f082007-04-16 20:48:54 +1000741void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
742{
743 INIT_LIST_HEAD(&queue->list);
744 queue->backlog = &queue->list;
745 queue->qlen = 0;
746 queue->max_qlen = max_qlen;
747}
748EXPORT_SYMBOL_GPL(crypto_init_queue);
749
750int crypto_enqueue_request(struct crypto_queue *queue,
751 struct crypto_async_request *request)
752{
753 int err = -EINPROGRESS;
754
755 if (unlikely(queue->qlen >= queue->max_qlen)) {
756 err = -EBUSY;
757 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
758 goto out;
759 if (queue->backlog == &queue->list)
760 queue->backlog = &request->list;
761 }
762
763 queue->qlen++;
764 list_add_tail(&request->list, &queue->list);
765
766out:
767 return err;
768}
769EXPORT_SYMBOL_GPL(crypto_enqueue_request);
770
771struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
772{
773 struct list_head *request;
774
775 if (unlikely(!queue->qlen))
776 return NULL;
777
778 queue->qlen--;
779
780 if (queue->backlog != &queue->list)
781 queue->backlog = queue->backlog->next;
782
783 request = queue->list.next;
784 list_del(request);
785
786 return list_entry(request, struct crypto_async_request, list);
787}
788EXPORT_SYMBOL_GPL(crypto_dequeue_request);
789
790int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
791{
792 struct crypto_async_request *req;
793
794 list_for_each_entry(req, &queue->list, list) {
795 if (req->tfm == tfm)
796 return 1;
797 }
798
799 return 0;
800}
801EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
802
Herbert Xu76136362007-11-20 17:26:06 +0800803static inline void crypto_inc_byte(u8 *a, unsigned int size)
804{
805 u8 *b = (a + size);
806 u8 c;
807
808 for (; size; size--) {
809 c = *--b + 1;
810 *b = c;
811 if (c)
812 break;
813 }
814}
815
816void crypto_inc(u8 *a, unsigned int size)
817{
818 __be32 *b = (__be32 *)(a + size);
819 u32 c;
820
821 for (; size >= 4; size -= 4) {
822 c = be32_to_cpu(*--b) + 1;
823 *b = cpu_to_be32(c);
824 if (c)
825 return;
826 }
827
828 crypto_inc_byte(a, size);
829}
830EXPORT_SYMBOL_GPL(crypto_inc);
831
832static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
833{
834 for (; size; size--)
835 *a++ ^= *b++;
836}
837
838void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
839{
840 u32 *a = (u32 *)dst;
841 u32 *b = (u32 *)src;
842
843 for (; size >= 4; size -= 4)
844 *a++ ^= *b++;
845
846 crypto_xor_byte((u8 *)a, (u8 *)b, size);
847}
848EXPORT_SYMBOL_GPL(crypto_xor);
849
Herbert Xucce9e062006-08-21 21:08:13 +1000850static int __init crypto_algapi_init(void)
851{
852 crypto_init_proc();
853 return 0;
854}
855
856static void __exit crypto_algapi_exit(void)
857{
858 crypto_exit_proc();
859}
860
861module_init(crypto_algapi_init);
862module_exit(crypto_algapi_exit);
863
864MODULE_LICENSE("GPL");
865MODULE_DESCRIPTION("Cryptographic algorithms API");