[CRYPTO] blkcipher: Remove alignment restriction on block size
[deliverable/linux.git] / crypto / algapi.c
CommitLineData
cce9e06d
HX
1/*
2 * Cryptographic API for algorithms (i.e., low-level API).
3 *
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
6bfd4809 13#include <linux/err.h>
cce9e06d
HX
14#include <linux/errno.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
4cc7720c 17#include <linux/list.h>
cce9e06d 18#include <linux/module.h>
7fed0bf2 19#include <linux/rtnetlink.h>
cce9e06d
HX
20#include <linux/string.h>
21
22#include "internal.h"
23
4cc7720c
HX
24static LIST_HEAD(crypto_template_list);
25
492e2b63 26void crypto_larval_error(const char *name, u32 type, u32 mask)
2825982d
HX
27{
28 struct crypto_alg *alg;
29
30 down_read(&crypto_alg_sem);
492e2b63 31 alg = __crypto_alg_lookup(name, type, mask);
2825982d
HX
32 up_read(&crypto_alg_sem);
33
34 if (alg) {
35 if (crypto_is_larval(alg)) {
36 struct crypto_larval *larval = (void *)alg;
fe3c5206 37 complete_all(&larval->completion);
2825982d
HX
38 }
39 crypto_mod_put(alg);
40 }
41}
42EXPORT_SYMBOL_GPL(crypto_larval_error);
43
cce9e06d
HX
44static 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
4cc7720c 61static int crypto_check_alg(struct crypto_alg *alg)
cce9e06d 62{
cce9e06d
HX
63 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
64 return -EINVAL;
65
cce9e06d
HX
66 if (alg->cra_blocksize > PAGE_SIZE / 8)
67 return -EINVAL;
68
69 if (alg->cra_priority < 0)
70 return -EINVAL;
cce9e06d 71
4cc7720c
HX
72 return crypto_set_driver_name(alg);
73}
74
6bfd4809
HX
75static 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
a73e6996
HX
84static void crypto_remove_spawn(struct crypto_spawn *spawn,
85 struct list_head *list,
86 struct list_head *secondary_spawns)
6bfd4809 87{
a73e6996
HX
88 struct crypto_instance *inst = spawn->inst;
89 struct crypto_template *tmpl = inst->tmpl;
6bfd4809 90
a73e6996
HX
91 list_del_init(&spawn->list);
92 spawn->alg = NULL;
6bfd4809 93
a73e6996
HX
94 if (crypto_is_dead(&inst->alg))
95 return;
6bfd4809 96
a73e6996
HX
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
109static void crypto_remove_spawns(struct list_head *spawns,
110 struct list_head *list, u32 new_type)
111{
112 struct crypto_spawn *spawn, *n;
113 LIST_HEAD(secondary_spawns);
6bfd4809 114
a73e6996
HX
115 list_for_each_entry_safe(spawn, n, spawns, list) {
116 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
6bfd4809
HX
117 continue;
118
a73e6996
HX
119 crypto_remove_spawn(spawn, list, &secondary_spawns);
120 }
6bfd4809 121
a73e6996
HX
122 while (!list_empty(&secondary_spawns)) {
123 list_for_each_entry_safe(spawn, n, &secondary_spawns, list)
124 crypto_remove_spawn(spawn, list, &secondary_spawns);
6bfd4809
HX
125 }
126}
127
128static int __crypto_register_alg(struct crypto_alg *alg,
129 struct list_head *list)
4cc7720c
HX
130{
131 struct crypto_alg *q;
6bfd4809
HX
132 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;
4cc7720c 140
2825982d 141 atomic_set(&alg->cra_refcnt, 1);
cce9e06d 142 list_for_each_entry(q, &crypto_alg_list, cra_list) {
4cc7720c 143 if (q == alg)
cce9e06d 144 goto out;
6bfd4809
HX
145
146 if (crypto_is_moribund(q))
147 continue;
148
149 if (crypto_is_larval(q)) {
2825982d
HX
150 struct crypto_larval *larval = (void *)q;
151
6bfd4809
HX
152 if (strcmp(alg->cra_name, q->cra_name) &&
153 strcmp(alg->cra_driver_name, q->cra_name))
154 continue;
155
156 if (larval->adult)
157 continue;
492e2b63
HX
158 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
159 continue;
2825982d
HX
160 if (!crypto_mod_get(alg))
161 continue;
6bfd4809 162
2825982d 163 larval->adult = alg;
fe3c5206 164 complete_all(&larval->completion);
6bfd4809 165 continue;
2825982d 166 }
6bfd4809
HX
167
168 if (strcmp(alg->cra_name, q->cra_name))
169 continue;
170
171 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
172 q->cra_priority > alg->cra_priority)
173 continue;
174
a73e6996 175 crypto_remove_spawns(&q->cra_users, list, alg->cra_flags);
cce9e06d
HX
176 }
177
178 list_add(&alg->cra_list, &crypto_alg_list);
2825982d
HX
179
180 crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
4cc7720c 181 ret = 0;
2825982d 182
cce9e06d 183out:
cce9e06d
HX
184 return ret;
185}
4cc7720c 186
6bfd4809
HX
187static void crypto_remove_final(struct list_head *list)
188{
189 struct crypto_alg *alg;
190 struct crypto_alg *n;
191
192 list_for_each_entry_safe(alg, n, list, cra_list) {
193 list_del_init(&alg->cra_list);
194 crypto_alg_put(alg);
195 }
196}
197
4cc7720c
HX
198int crypto_register_alg(struct crypto_alg *alg)
199{
6bfd4809 200 LIST_HEAD(list);
4cc7720c
HX
201 int err;
202
203 err = crypto_check_alg(alg);
204 if (err)
205 return err;
206
207 down_write(&crypto_alg_sem);
6bfd4809 208 err = __crypto_register_alg(alg, &list);
4cc7720c
HX
209 up_write(&crypto_alg_sem);
210
6bfd4809 211 crypto_remove_final(&list);
4cc7720c
HX
212 return err;
213}
cce9e06d
HX
214EXPORT_SYMBOL_GPL(crypto_register_alg);
215
6bfd4809
HX
216static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
217{
218 if (unlikely(list_empty(&alg->cra_list)))
219 return -ENOENT;
220
221 alg->cra_flags |= CRYPTO_ALG_DEAD;
222
223 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
224 list_del_init(&alg->cra_list);
a73e6996 225 crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags);
6bfd4809
HX
226
227 return 0;
228}
229
cce9e06d
HX
230int crypto_unregister_alg(struct crypto_alg *alg)
231{
6bfd4809
HX
232 int ret;
233 LIST_HEAD(list);
cce9e06d
HX
234
235 down_write(&crypto_alg_sem);
6bfd4809 236 ret = crypto_remove_alg(alg, &list);
cce9e06d
HX
237 up_write(&crypto_alg_sem);
238
239 if (ret)
240 return ret;
241
242 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
243 if (alg->cra_destroy)
244 alg->cra_destroy(alg);
245
6bfd4809 246 crypto_remove_final(&list);
cce9e06d
HX
247 return 0;
248}
249EXPORT_SYMBOL_GPL(crypto_unregister_alg);
250
4cc7720c
HX
251int crypto_register_template(struct crypto_template *tmpl)
252{
253 struct crypto_template *q;
254 int err = -EEXIST;
255
256 down_write(&crypto_alg_sem);
257
258 list_for_each_entry(q, &crypto_template_list, list) {
259 if (q == tmpl)
260 goto out;
261 }
262
263 list_add(&tmpl->list, &crypto_template_list);
2825982d 264 crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
4cc7720c
HX
265 err = 0;
266out:
267 up_write(&crypto_alg_sem);
268 return err;
269}
270EXPORT_SYMBOL_GPL(crypto_register_template);
271
272void crypto_unregister_template(struct crypto_template *tmpl)
273{
274 struct crypto_instance *inst;
275 struct hlist_node *p, *n;
276 struct hlist_head *list;
6bfd4809 277 LIST_HEAD(users);
4cc7720c
HX
278
279 down_write(&crypto_alg_sem);
280
281 BUG_ON(list_empty(&tmpl->list));
282 list_del_init(&tmpl->list);
283
284 list = &tmpl->instances;
285 hlist_for_each_entry(inst, p, list, list) {
6bfd4809
HX
286 int err = crypto_remove_alg(&inst->alg, &users);
287 BUG_ON(err);
4cc7720c
HX
288 }
289
2825982d
HX
290 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
291
4cc7720c
HX
292 up_write(&crypto_alg_sem);
293
294 hlist_for_each_entry_safe(inst, p, n, list, list) {
295 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
296 tmpl->free(inst);
297 }
6bfd4809 298 crypto_remove_final(&users);
4cc7720c
HX
299}
300EXPORT_SYMBOL_GPL(crypto_unregister_template);
301
302static struct crypto_template *__crypto_lookup_template(const char *name)
303{
304 struct crypto_template *q, *tmpl = NULL;
305
306 down_read(&crypto_alg_sem);
307 list_for_each_entry(q, &crypto_template_list, list) {
308 if (strcmp(q->name, name))
309 continue;
310 if (unlikely(!crypto_tmpl_get(q)))
311 continue;
312
313 tmpl = q;
314 break;
315 }
316 up_read(&crypto_alg_sem);
317
318 return tmpl;
319}
320
321struct crypto_template *crypto_lookup_template(const char *name)
322{
323 return try_then_request_module(__crypto_lookup_template(name), name);
324}
325EXPORT_SYMBOL_GPL(crypto_lookup_template);
326
327int crypto_register_instance(struct crypto_template *tmpl,
328 struct crypto_instance *inst)
329{
6bfd4809 330 LIST_HEAD(list);
4cc7720c
HX
331 int err = -EINVAL;
332
333 if (inst->alg.cra_destroy)
334 goto err;
335
336 err = crypto_check_alg(&inst->alg);
337 if (err)
338 goto err;
339
340 inst->alg.cra_module = tmpl->module;
341
342 down_write(&crypto_alg_sem);
343
6bfd4809 344 err = __crypto_register_alg(&inst->alg, &list);
4cc7720c
HX
345 if (err)
346 goto unlock;
347
348 hlist_add_head(&inst->list, &tmpl->instances);
349 inst->tmpl = tmpl;
350
351unlock:
352 up_write(&crypto_alg_sem);
353
6bfd4809
HX
354 crypto_remove_final(&list);
355
4cc7720c
HX
356err:
357 return err;
358}
359EXPORT_SYMBOL_GPL(crypto_register_instance);
360
6bfd4809 361int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
a73e6996 362 struct crypto_instance *inst, u32 mask)
6bfd4809
HX
363{
364 int err = -EAGAIN;
365
366 spawn->inst = inst;
a73e6996 367 spawn->mask = mask;
6bfd4809
HX
368
369 down_write(&crypto_alg_sem);
370 if (!crypto_is_moribund(alg)) {
371 list_add(&spawn->list, &alg->cra_users);
372 spawn->alg = alg;
373 err = 0;
374 }
375 up_write(&crypto_alg_sem);
376
377 return err;
378}
379EXPORT_SYMBOL_GPL(crypto_init_spawn);
380
381void crypto_drop_spawn(struct crypto_spawn *spawn)
382{
383 down_write(&crypto_alg_sem);
384 list_del(&spawn->list);
385 up_write(&crypto_alg_sem);
386}
387EXPORT_SYMBOL_GPL(crypto_drop_spawn);
388
2e306ee0
HX
389struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
390 u32 mask)
6bfd4809
HX
391{
392 struct crypto_alg *alg;
393 struct crypto_alg *alg2;
394 struct crypto_tfm *tfm;
395
396 down_read(&crypto_alg_sem);
397 alg = spawn->alg;
398 alg2 = alg;
399 if (alg2)
400 alg2 = crypto_mod_get(alg2);
401 up_read(&crypto_alg_sem);
402
403 if (!alg2) {
404 if (alg)
405 crypto_shoot_alg(alg);
406 return ERR_PTR(-EAGAIN);
407 }
408
2e306ee0
HX
409 tfm = ERR_PTR(-EINVAL);
410 if (unlikely((alg->cra_flags ^ type) & mask))
411 goto out_put_alg;
412
27d2a330 413 tfm = __crypto_alloc_tfm(alg, type, mask);
6bfd4809 414 if (IS_ERR(tfm))
2e306ee0
HX
415 goto out_put_alg;
416
417 return tfm;
6bfd4809 418
2e306ee0
HX
419out_put_alg:
420 crypto_mod_put(alg);
6bfd4809
HX
421 return tfm;
422}
423EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
424
2825982d
HX
425int crypto_register_notifier(struct notifier_block *nb)
426{
427 return blocking_notifier_chain_register(&crypto_chain, nb);
428}
429EXPORT_SYMBOL_GPL(crypto_register_notifier);
430
431int crypto_unregister_notifier(struct notifier_block *nb)
432{
433 return blocking_notifier_chain_unregister(&crypto_chain, nb);
434}
435EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
436
ebc610e5 437struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
7fed0bf2 438{
39e1ee01 439 struct rtattr *rta = tb[0];
ebc610e5
HX
440 struct crypto_attr_type *algt;
441
442 if (!rta)
443 return ERR_PTR(-ENOENT);
444 if (RTA_PAYLOAD(rta) < sizeof(*algt))
445 return ERR_PTR(-EINVAL);
39e1ee01
HX
446 if (rta->rta_type != CRYPTOA_TYPE)
447 return ERR_PTR(-EINVAL);
ebc610e5
HX
448
449 algt = RTA_DATA(rta);
450
451 return algt;
452}
453EXPORT_SYMBOL_GPL(crypto_get_attr_type);
454
455int crypto_check_attr_type(struct rtattr **tb, u32 type)
456{
457 struct crypto_attr_type *algt;
458
459 algt = crypto_get_attr_type(tb);
460 if (IS_ERR(algt))
461 return PTR_ERR(algt);
462
463 if ((algt->type ^ type) & algt->mask)
464 return -EINVAL;
465
466 return 0;
467}
468EXPORT_SYMBOL_GPL(crypto_check_attr_type);
469
3c09f17c 470struct crypto_alg *crypto_attr_alg(struct rtattr *rta, u32 type, u32 mask)
ebc610e5 471{
7fed0bf2
HX
472 struct crypto_attr_alg *alga;
473
ebc610e5
HX
474 if (!rta)
475 return ERR_PTR(-ENOENT);
476 if (RTA_PAYLOAD(rta) < sizeof(*alga))
7fed0bf2 477 return ERR_PTR(-EINVAL);
39e1ee01
HX
478 if (rta->rta_type != CRYPTOA_ALG)
479 return ERR_PTR(-EINVAL);
7fed0bf2
HX
480
481 alga = RTA_DATA(rta);
482 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
483
484 return crypto_alg_mod_lookup(alga->name, type, mask);
485}
3c09f17c
HX
486EXPORT_SYMBOL_GPL(crypto_attr_alg);
487
488int crypto_attr_u32(struct rtattr *rta, u32 *num)
489{
490 struct crypto_attr_u32 *nu32;
491
492 if (!rta)
493 return -ENOENT;
494 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
495 return -EINVAL;
496 if (rta->rta_type != CRYPTOA_U32)
497 return -EINVAL;
498
499 nu32 = RTA_DATA(rta);
500 *num = nu32->num;
501
502 return 0;
503}
504EXPORT_SYMBOL_GPL(crypto_attr_u32);
7fed0bf2
HX
505
506struct crypto_instance *crypto_alloc_instance(const char *name,
507 struct crypto_alg *alg)
508{
509 struct crypto_instance *inst;
510 struct crypto_spawn *spawn;
511 int err;
512
513 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
514 if (!inst)
515 return ERR_PTR(-ENOMEM);
516
517 err = -ENAMETOOLONG;
518 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
519 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
520 goto err_free_inst;
521
522 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
523 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
524 goto err_free_inst;
525
526 spawn = crypto_instance_ctx(inst);
a73e6996
HX
527 err = crypto_init_spawn(spawn, alg, inst,
528 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
7fed0bf2
HX
529
530 if (err)
531 goto err_free_inst;
532
533 return inst;
534
535err_free_inst:
536 kfree(inst);
537 return ERR_PTR(err);
538}
539EXPORT_SYMBOL_GPL(crypto_alloc_instance);
540
b5b7f088
HX
541void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
542{
543 INIT_LIST_HEAD(&queue->list);
544 queue->backlog = &queue->list;
545 queue->qlen = 0;
546 queue->max_qlen = max_qlen;
547}
548EXPORT_SYMBOL_GPL(crypto_init_queue);
549
550int crypto_enqueue_request(struct crypto_queue *queue,
551 struct crypto_async_request *request)
552{
553 int err = -EINPROGRESS;
554
555 if (unlikely(queue->qlen >= queue->max_qlen)) {
556 err = -EBUSY;
557 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
558 goto out;
559 if (queue->backlog == &queue->list)
560 queue->backlog = &request->list;
561 }
562
563 queue->qlen++;
564 list_add_tail(&request->list, &queue->list);
565
566out:
567 return err;
568}
569EXPORT_SYMBOL_GPL(crypto_enqueue_request);
570
571struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
572{
573 struct list_head *request;
574
575 if (unlikely(!queue->qlen))
576 return NULL;
577
578 queue->qlen--;
579
580 if (queue->backlog != &queue->list)
581 queue->backlog = queue->backlog->next;
582
583 request = queue->list.next;
584 list_del(request);
585
586 return list_entry(request, struct crypto_async_request, list);
587}
588EXPORT_SYMBOL_GPL(crypto_dequeue_request);
589
590int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
591{
592 struct crypto_async_request *req;
593
594 list_for_each_entry(req, &queue->list, list) {
595 if (req->tfm == tfm)
596 return 1;
597 }
598
599 return 0;
600}
601EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
602
cce9e06d
HX
603static int __init crypto_algapi_init(void)
604{
605 crypto_init_proc();
606 return 0;
607}
608
609static void __exit crypto_algapi_exit(void)
610{
611 crypto_exit_proc();
612}
613
614module_init(crypto_algapi_init);
615module_exit(crypto_algapi_exit);
616
617MODULE_LICENSE("GPL");
618MODULE_DESCRIPTION("Cryptographic algorithms API");
This page took 0.144507 seconds and 5 git commands to generate.