2 * Software async crypto daemon.
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
6 * Added AEAD support to cryptd.
7 * Authors: Tadeusz Struk (tadeusz.struk@intel.com)
8 * Adrian Hoban <adrian.hoban@intel.com>
9 * Gabriele Paoloni <gabriele.paoloni@intel.com>
10 * Aidan O'Mahony (aidan.o.mahony@intel.com)
11 * Copyright (c) 2010, Intel Corporation.
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
20 #include <crypto/internal/hash.h>
21 #include <crypto/internal/aead.h>
22 #include <crypto/internal/skcipher.h>
23 #include <crypto/cryptd.h>
24 #include <crypto/crypto_wq.h>
25 #include <linux/atomic.h>
26 #include <linux/err.h>
27 #include <linux/init.h>
28 #include <linux/kernel.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/scatterlist.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
35 #define CRYPTD_MAX_CPU_QLEN 1000
37 struct cryptd_cpu_queue
{
38 struct crypto_queue queue
;
39 struct work_struct work
;
43 struct cryptd_cpu_queue __percpu
*cpu_queue
;
46 struct cryptd_instance_ctx
{
47 struct crypto_spawn spawn
;
48 struct cryptd_queue
*queue
;
51 struct skcipherd_instance_ctx
{
52 struct crypto_skcipher_spawn spawn
;
53 struct cryptd_queue
*queue
;
56 struct hashd_instance_ctx
{
57 struct crypto_shash_spawn spawn
;
58 struct cryptd_queue
*queue
;
61 struct aead_instance_ctx
{
62 struct crypto_aead_spawn aead_spawn
;
63 struct cryptd_queue
*queue
;
66 struct cryptd_blkcipher_ctx
{
68 struct crypto_blkcipher
*child
;
71 struct cryptd_blkcipher_request_ctx
{
72 crypto_completion_t complete
;
75 struct cryptd_skcipher_ctx
{
77 struct crypto_skcipher
*child
;
80 struct cryptd_skcipher_request_ctx
{
81 crypto_completion_t complete
;
84 struct cryptd_hash_ctx
{
86 struct crypto_shash
*child
;
89 struct cryptd_hash_request_ctx
{
90 crypto_completion_t complete
;
91 struct shash_desc desc
;
94 struct cryptd_aead_ctx
{
96 struct crypto_aead
*child
;
99 struct cryptd_aead_request_ctx
{
100 crypto_completion_t complete
;
103 static void cryptd_queue_worker(struct work_struct
*work
);
105 static int cryptd_init_queue(struct cryptd_queue
*queue
,
106 unsigned int max_cpu_qlen
)
109 struct cryptd_cpu_queue
*cpu_queue
;
111 queue
->cpu_queue
= alloc_percpu(struct cryptd_cpu_queue
);
112 if (!queue
->cpu_queue
)
114 for_each_possible_cpu(cpu
) {
115 cpu_queue
= per_cpu_ptr(queue
->cpu_queue
, cpu
);
116 crypto_init_queue(&cpu_queue
->queue
, max_cpu_qlen
);
117 INIT_WORK(&cpu_queue
->work
, cryptd_queue_worker
);
122 static void cryptd_fini_queue(struct cryptd_queue
*queue
)
125 struct cryptd_cpu_queue
*cpu_queue
;
127 for_each_possible_cpu(cpu
) {
128 cpu_queue
= per_cpu_ptr(queue
->cpu_queue
, cpu
);
129 BUG_ON(cpu_queue
->queue
.qlen
);
131 free_percpu(queue
->cpu_queue
);
134 static int cryptd_enqueue_request(struct cryptd_queue
*queue
,
135 struct crypto_async_request
*request
)
138 struct cryptd_cpu_queue
*cpu_queue
;
142 cpu_queue
= this_cpu_ptr(queue
->cpu_queue
);
143 err
= crypto_enqueue_request(&cpu_queue
->queue
, request
);
145 refcnt
= crypto_tfm_ctx(request
->tfm
);
150 queue_work_on(cpu
, kcrypto_wq
, &cpu_queue
->work
);
152 if (!atomic_read(refcnt
))
163 /* Called in workqueue context, do one real cryption work (via
164 * req->complete) and reschedule itself if there are more work to
166 static void cryptd_queue_worker(struct work_struct
*work
)
168 struct cryptd_cpu_queue
*cpu_queue
;
169 struct crypto_async_request
*req
, *backlog
;
171 cpu_queue
= container_of(work
, struct cryptd_cpu_queue
, work
);
173 * Only handle one request at a time to avoid hogging crypto workqueue.
174 * preempt_disable/enable is used to prevent being preempted by
175 * cryptd_enqueue_request(). local_bh_disable/enable is used to prevent
176 * cryptd_enqueue_request() being accessed from software interrupts.
180 backlog
= crypto_get_backlog(&cpu_queue
->queue
);
181 req
= crypto_dequeue_request(&cpu_queue
->queue
);
189 backlog
->complete(backlog
, -EINPROGRESS
);
190 req
->complete(req
, 0);
192 if (cpu_queue
->queue
.qlen
)
193 queue_work(kcrypto_wq
, &cpu_queue
->work
);
196 static inline struct cryptd_queue
*cryptd_get_queue(struct crypto_tfm
*tfm
)
198 struct crypto_instance
*inst
= crypto_tfm_alg_instance(tfm
);
199 struct cryptd_instance_ctx
*ictx
= crypto_instance_ctx(inst
);
203 static inline void cryptd_check_internal(struct rtattr
**tb
, u32
*type
,
206 struct crypto_attr_type
*algt
;
208 algt
= crypto_get_attr_type(tb
);
212 *type
|= algt
->type
& CRYPTO_ALG_INTERNAL
;
213 *mask
|= algt
->mask
& CRYPTO_ALG_INTERNAL
;
216 static int cryptd_blkcipher_setkey(struct crypto_ablkcipher
*parent
,
217 const u8
*key
, unsigned int keylen
)
219 struct cryptd_blkcipher_ctx
*ctx
= crypto_ablkcipher_ctx(parent
);
220 struct crypto_blkcipher
*child
= ctx
->child
;
223 crypto_blkcipher_clear_flags(child
, CRYPTO_TFM_REQ_MASK
);
224 crypto_blkcipher_set_flags(child
, crypto_ablkcipher_get_flags(parent
) &
225 CRYPTO_TFM_REQ_MASK
);
226 err
= crypto_blkcipher_setkey(child
, key
, keylen
);
227 crypto_ablkcipher_set_flags(parent
, crypto_blkcipher_get_flags(child
) &
228 CRYPTO_TFM_RES_MASK
);
232 static void cryptd_blkcipher_crypt(struct ablkcipher_request
*req
,
233 struct crypto_blkcipher
*child
,
235 int (*crypt
)(struct blkcipher_desc
*desc
,
236 struct scatterlist
*dst
,
237 struct scatterlist
*src
,
240 struct cryptd_blkcipher_request_ctx
*rctx
;
241 struct cryptd_blkcipher_ctx
*ctx
;
242 struct crypto_ablkcipher
*tfm
;
243 struct blkcipher_desc desc
;
246 rctx
= ablkcipher_request_ctx(req
);
248 if (unlikely(err
== -EINPROGRESS
))
252 desc
.info
= req
->info
;
253 desc
.flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
255 err
= crypt(&desc
, req
->dst
, req
->src
, req
->nbytes
);
257 req
->base
.complete
= rctx
->complete
;
260 tfm
= crypto_ablkcipher_reqtfm(req
);
261 ctx
= crypto_ablkcipher_ctx(tfm
);
262 refcnt
= atomic_read(&ctx
->refcnt
);
265 rctx
->complete(&req
->base
, err
);
268 if (err
!= -EINPROGRESS
&& refcnt
&& atomic_dec_and_test(&ctx
->refcnt
))
269 crypto_free_ablkcipher(tfm
);
272 static void cryptd_blkcipher_encrypt(struct crypto_async_request
*req
, int err
)
274 struct cryptd_blkcipher_ctx
*ctx
= crypto_tfm_ctx(req
->tfm
);
275 struct crypto_blkcipher
*child
= ctx
->child
;
277 cryptd_blkcipher_crypt(ablkcipher_request_cast(req
), child
, err
,
278 crypto_blkcipher_crt(child
)->encrypt
);
281 static void cryptd_blkcipher_decrypt(struct crypto_async_request
*req
, int err
)
283 struct cryptd_blkcipher_ctx
*ctx
= crypto_tfm_ctx(req
->tfm
);
284 struct crypto_blkcipher
*child
= ctx
->child
;
286 cryptd_blkcipher_crypt(ablkcipher_request_cast(req
), child
, err
,
287 crypto_blkcipher_crt(child
)->decrypt
);
290 static int cryptd_blkcipher_enqueue(struct ablkcipher_request
*req
,
291 crypto_completion_t
compl)
293 struct cryptd_blkcipher_request_ctx
*rctx
= ablkcipher_request_ctx(req
);
294 struct crypto_ablkcipher
*tfm
= crypto_ablkcipher_reqtfm(req
);
295 struct cryptd_queue
*queue
;
297 queue
= cryptd_get_queue(crypto_ablkcipher_tfm(tfm
));
298 rctx
->complete
= req
->base
.complete
;
299 req
->base
.complete
= compl;
301 return cryptd_enqueue_request(queue
, &req
->base
);
304 static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request
*req
)
306 return cryptd_blkcipher_enqueue(req
, cryptd_blkcipher_encrypt
);
309 static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request
*req
)
311 return cryptd_blkcipher_enqueue(req
, cryptd_blkcipher_decrypt
);
314 static int cryptd_blkcipher_init_tfm(struct crypto_tfm
*tfm
)
316 struct crypto_instance
*inst
= crypto_tfm_alg_instance(tfm
);
317 struct cryptd_instance_ctx
*ictx
= crypto_instance_ctx(inst
);
318 struct crypto_spawn
*spawn
= &ictx
->spawn
;
319 struct cryptd_blkcipher_ctx
*ctx
= crypto_tfm_ctx(tfm
);
320 struct crypto_blkcipher
*cipher
;
322 cipher
= crypto_spawn_blkcipher(spawn
);
324 return PTR_ERR(cipher
);
327 tfm
->crt_ablkcipher
.reqsize
=
328 sizeof(struct cryptd_blkcipher_request_ctx
);
332 static void cryptd_blkcipher_exit_tfm(struct crypto_tfm
*tfm
)
334 struct cryptd_blkcipher_ctx
*ctx
= crypto_tfm_ctx(tfm
);
336 crypto_free_blkcipher(ctx
->child
);
339 static int cryptd_init_instance(struct crypto_instance
*inst
,
340 struct crypto_alg
*alg
)
342 if (snprintf(inst
->alg
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
,
344 alg
->cra_driver_name
) >= CRYPTO_MAX_ALG_NAME
)
345 return -ENAMETOOLONG
;
347 memcpy(inst
->alg
.cra_name
, alg
->cra_name
, CRYPTO_MAX_ALG_NAME
);
349 inst
->alg
.cra_priority
= alg
->cra_priority
+ 50;
350 inst
->alg
.cra_blocksize
= alg
->cra_blocksize
;
351 inst
->alg
.cra_alignmask
= alg
->cra_alignmask
;
356 static void *cryptd_alloc_instance(struct crypto_alg
*alg
, unsigned int head
,
360 struct crypto_instance
*inst
;
363 p
= kzalloc(head
+ sizeof(*inst
) + tail
, GFP_KERNEL
);
365 return ERR_PTR(-ENOMEM
);
367 inst
= (void *)(p
+ head
);
369 err
= cryptd_init_instance(inst
, alg
);
382 static int cryptd_create_blkcipher(struct crypto_template
*tmpl
,
384 struct cryptd_queue
*queue
)
386 struct cryptd_instance_ctx
*ctx
;
387 struct crypto_instance
*inst
;
388 struct crypto_alg
*alg
;
389 u32 type
= CRYPTO_ALG_TYPE_BLKCIPHER
;
390 u32 mask
= CRYPTO_ALG_TYPE_MASK
;
393 cryptd_check_internal(tb
, &type
, &mask
);
395 alg
= crypto_get_attr_alg(tb
, type
, mask
);
399 inst
= cryptd_alloc_instance(alg
, 0, sizeof(*ctx
));
404 ctx
= crypto_instance_ctx(inst
);
407 err
= crypto_init_spawn(&ctx
->spawn
, alg
, inst
,
408 CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_ASYNC
);
412 type
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
;
413 if (alg
->cra_flags
& CRYPTO_ALG_INTERNAL
)
414 type
|= CRYPTO_ALG_INTERNAL
;
415 inst
->alg
.cra_flags
= type
;
416 inst
->alg
.cra_type
= &crypto_ablkcipher_type
;
418 inst
->alg
.cra_ablkcipher
.ivsize
= alg
->cra_blkcipher
.ivsize
;
419 inst
->alg
.cra_ablkcipher
.min_keysize
= alg
->cra_blkcipher
.min_keysize
;
420 inst
->alg
.cra_ablkcipher
.max_keysize
= alg
->cra_blkcipher
.max_keysize
;
422 inst
->alg
.cra_ablkcipher
.geniv
= alg
->cra_blkcipher
.geniv
;
424 inst
->alg
.cra_ctxsize
= sizeof(struct cryptd_blkcipher_ctx
);
426 inst
->alg
.cra_init
= cryptd_blkcipher_init_tfm
;
427 inst
->alg
.cra_exit
= cryptd_blkcipher_exit_tfm
;
429 inst
->alg
.cra_ablkcipher
.setkey
= cryptd_blkcipher_setkey
;
430 inst
->alg
.cra_ablkcipher
.encrypt
= cryptd_blkcipher_encrypt_enqueue
;
431 inst
->alg
.cra_ablkcipher
.decrypt
= cryptd_blkcipher_decrypt_enqueue
;
433 err
= crypto_register_instance(tmpl
, inst
);
435 crypto_drop_spawn(&ctx
->spawn
);
445 static int cryptd_skcipher_setkey(struct crypto_skcipher
*parent
,
446 const u8
*key
, unsigned int keylen
)
448 struct cryptd_skcipher_ctx
*ctx
= crypto_skcipher_ctx(parent
);
449 struct crypto_skcipher
*child
= ctx
->child
;
452 crypto_skcipher_clear_flags(child
, CRYPTO_TFM_REQ_MASK
);
453 crypto_skcipher_set_flags(child
, crypto_skcipher_get_flags(parent
) &
454 CRYPTO_TFM_REQ_MASK
);
455 err
= crypto_skcipher_setkey(child
, key
, keylen
);
456 crypto_skcipher_set_flags(parent
, crypto_skcipher_get_flags(child
) &
457 CRYPTO_TFM_RES_MASK
);
461 static void cryptd_skcipher_complete(struct skcipher_request
*req
, int err
)
463 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
464 struct cryptd_skcipher_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
465 struct cryptd_skcipher_request_ctx
*rctx
= skcipher_request_ctx(req
);
466 int refcnt
= atomic_read(&ctx
->refcnt
);
469 rctx
->complete(&req
->base
, err
);
472 if (err
!= -EINPROGRESS
&& refcnt
&& atomic_dec_and_test(&ctx
->refcnt
))
473 crypto_free_skcipher(tfm
);
476 static void cryptd_skcipher_encrypt(struct crypto_async_request
*base
,
479 struct skcipher_request
*req
= skcipher_request_cast(base
);
480 struct cryptd_skcipher_request_ctx
*rctx
= skcipher_request_ctx(req
);
481 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
482 struct cryptd_skcipher_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
483 struct crypto_skcipher
*child
= ctx
->child
;
484 SKCIPHER_REQUEST_ON_STACK(subreq
, child
);
486 if (unlikely(err
== -EINPROGRESS
))
489 skcipher_request_set_tfm(subreq
, child
);
490 skcipher_request_set_callback(subreq
, CRYPTO_TFM_REQ_MAY_SLEEP
,
492 skcipher_request_set_crypt(subreq
, req
->src
, req
->dst
, req
->cryptlen
,
495 err
= crypto_skcipher_encrypt(subreq
);
496 skcipher_request_zero(subreq
);
498 req
->base
.complete
= rctx
->complete
;
501 cryptd_skcipher_complete(req
, err
);
504 static void cryptd_skcipher_decrypt(struct crypto_async_request
*base
,
507 struct skcipher_request
*req
= skcipher_request_cast(base
);
508 struct cryptd_skcipher_request_ctx
*rctx
= skcipher_request_ctx(req
);
509 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
510 struct cryptd_skcipher_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
511 struct crypto_skcipher
*child
= ctx
->child
;
512 SKCIPHER_REQUEST_ON_STACK(subreq
, child
);
514 if (unlikely(err
== -EINPROGRESS
))
517 skcipher_request_set_tfm(subreq
, child
);
518 skcipher_request_set_callback(subreq
, CRYPTO_TFM_REQ_MAY_SLEEP
,
520 skcipher_request_set_crypt(subreq
, req
->src
, req
->dst
, req
->cryptlen
,
523 err
= crypto_skcipher_decrypt(subreq
);
524 skcipher_request_zero(subreq
);
526 req
->base
.complete
= rctx
->complete
;
529 cryptd_skcipher_complete(req
, err
);
532 static int cryptd_skcipher_enqueue(struct skcipher_request
*req
,
533 crypto_completion_t
compl)
535 struct cryptd_skcipher_request_ctx
*rctx
= skcipher_request_ctx(req
);
536 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
537 struct cryptd_queue
*queue
;
539 queue
= cryptd_get_queue(crypto_skcipher_tfm(tfm
));
540 rctx
->complete
= req
->base
.complete
;
541 req
->base
.complete
= compl;
543 return cryptd_enqueue_request(queue
, &req
->base
);
546 static int cryptd_skcipher_encrypt_enqueue(struct skcipher_request
*req
)
548 return cryptd_skcipher_enqueue(req
, cryptd_skcipher_encrypt
);
551 static int cryptd_skcipher_decrypt_enqueue(struct skcipher_request
*req
)
553 return cryptd_skcipher_enqueue(req
, cryptd_skcipher_decrypt
);
556 static int cryptd_skcipher_init_tfm(struct crypto_skcipher
*tfm
)
558 struct skcipher_instance
*inst
= skcipher_alg_instance(tfm
);
559 struct skcipherd_instance_ctx
*ictx
= skcipher_instance_ctx(inst
);
560 struct crypto_skcipher_spawn
*spawn
= &ictx
->spawn
;
561 struct cryptd_skcipher_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
562 struct crypto_skcipher
*cipher
;
564 cipher
= crypto_spawn_skcipher(spawn
);
566 return PTR_ERR(cipher
);
569 crypto_skcipher_set_reqsize(
570 tfm
, sizeof(struct cryptd_skcipher_request_ctx
));
574 static void cryptd_skcipher_exit_tfm(struct crypto_skcipher
*tfm
)
576 struct cryptd_skcipher_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
578 crypto_free_skcipher(ctx
->child
);
581 static void cryptd_skcipher_free(struct skcipher_instance
*inst
)
583 struct skcipherd_instance_ctx
*ctx
= skcipher_instance_ctx(inst
);
585 crypto_drop_skcipher(&ctx
->spawn
);
588 static int cryptd_create_skcipher(struct crypto_template
*tmpl
,
590 struct cryptd_queue
*queue
)
592 struct skcipherd_instance_ctx
*ctx
;
593 struct skcipher_instance
*inst
;
594 struct skcipher_alg
*alg
;
601 mask
= CRYPTO_ALG_ASYNC
;
603 cryptd_check_internal(tb
, &type
, &mask
);
605 name
= crypto_attr_alg_name(tb
[1]);
607 return PTR_ERR(name
);
609 inst
= kzalloc(sizeof(*inst
) + sizeof(*ctx
), GFP_KERNEL
);
613 ctx
= skcipher_instance_ctx(inst
);
616 crypto_set_skcipher_spawn(&ctx
->spawn
, skcipher_crypto_instance(inst
));
617 err
= crypto_grab_skcipher(&ctx
->spawn
, name
, type
, mask
);
621 alg
= crypto_spawn_skcipher_alg(&ctx
->spawn
);
622 err
= cryptd_init_instance(skcipher_crypto_instance(inst
), &alg
->base
);
624 goto out_drop_skcipher
;
626 inst
->alg
.base
.cra_flags
= CRYPTO_ALG_ASYNC
|
627 (alg
->base
.cra_flags
& CRYPTO_ALG_INTERNAL
);
629 inst
->alg
.ivsize
= crypto_skcipher_alg_ivsize(alg
);
630 inst
->alg
.chunksize
= crypto_skcipher_alg_chunksize(alg
);
631 inst
->alg
.min_keysize
= crypto_skcipher_alg_min_keysize(alg
);
632 inst
->alg
.max_keysize
= crypto_skcipher_alg_max_keysize(alg
);
634 inst
->alg
.base
.cra_ctxsize
= sizeof(struct cryptd_skcipher_ctx
);
636 inst
->alg
.init
= cryptd_skcipher_init_tfm
;
637 inst
->alg
.exit
= cryptd_skcipher_exit_tfm
;
639 inst
->alg
.setkey
= cryptd_skcipher_setkey
;
640 inst
->alg
.encrypt
= cryptd_skcipher_encrypt_enqueue
;
641 inst
->alg
.decrypt
= cryptd_skcipher_decrypt_enqueue
;
643 inst
->free
= cryptd_skcipher_free
;
645 err
= skcipher_register_instance(tmpl
, inst
);
648 crypto_drop_skcipher(&ctx
->spawn
);
655 static int cryptd_hash_init_tfm(struct crypto_tfm
*tfm
)
657 struct crypto_instance
*inst
= crypto_tfm_alg_instance(tfm
);
658 struct hashd_instance_ctx
*ictx
= crypto_instance_ctx(inst
);
659 struct crypto_shash_spawn
*spawn
= &ictx
->spawn
;
660 struct cryptd_hash_ctx
*ctx
= crypto_tfm_ctx(tfm
);
661 struct crypto_shash
*hash
;
663 hash
= crypto_spawn_shash(spawn
);
665 return PTR_ERR(hash
);
668 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
669 sizeof(struct cryptd_hash_request_ctx
) +
670 crypto_shash_descsize(hash
));
674 static void cryptd_hash_exit_tfm(struct crypto_tfm
*tfm
)
676 struct cryptd_hash_ctx
*ctx
= crypto_tfm_ctx(tfm
);
678 crypto_free_shash(ctx
->child
);
681 static int cryptd_hash_setkey(struct crypto_ahash
*parent
,
682 const u8
*key
, unsigned int keylen
)
684 struct cryptd_hash_ctx
*ctx
= crypto_ahash_ctx(parent
);
685 struct crypto_shash
*child
= ctx
->child
;
688 crypto_shash_clear_flags(child
, CRYPTO_TFM_REQ_MASK
);
689 crypto_shash_set_flags(child
, crypto_ahash_get_flags(parent
) &
690 CRYPTO_TFM_REQ_MASK
);
691 err
= crypto_shash_setkey(child
, key
, keylen
);
692 crypto_ahash_set_flags(parent
, crypto_shash_get_flags(child
) &
693 CRYPTO_TFM_RES_MASK
);
697 static int cryptd_hash_enqueue(struct ahash_request
*req
,
698 crypto_completion_t
compl)
700 struct cryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
701 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
702 struct cryptd_queue
*queue
=
703 cryptd_get_queue(crypto_ahash_tfm(tfm
));
705 rctx
->complete
= req
->base
.complete
;
706 req
->base
.complete
= compl;
708 return cryptd_enqueue_request(queue
, &req
->base
);
711 static void cryptd_hash_complete(struct ahash_request
*req
, int err
)
713 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
714 struct cryptd_hash_ctx
*ctx
= crypto_ahash_ctx(tfm
);
715 struct cryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
716 int refcnt
= atomic_read(&ctx
->refcnt
);
719 rctx
->complete(&req
->base
, err
);
722 if (err
!= -EINPROGRESS
&& refcnt
&& atomic_dec_and_test(&ctx
->refcnt
))
723 crypto_free_ahash(tfm
);
726 static void cryptd_hash_init(struct crypto_async_request
*req_async
, int err
)
728 struct cryptd_hash_ctx
*ctx
= crypto_tfm_ctx(req_async
->tfm
);
729 struct crypto_shash
*child
= ctx
->child
;
730 struct ahash_request
*req
= ahash_request_cast(req_async
);
731 struct cryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
732 struct shash_desc
*desc
= &rctx
->desc
;
734 if (unlikely(err
== -EINPROGRESS
))
738 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
740 err
= crypto_shash_init(desc
);
742 req
->base
.complete
= rctx
->complete
;
745 cryptd_hash_complete(req
, err
);
748 static int cryptd_hash_init_enqueue(struct ahash_request
*req
)
750 return cryptd_hash_enqueue(req
, cryptd_hash_init
);
753 static void cryptd_hash_update(struct crypto_async_request
*req_async
, int err
)
755 struct ahash_request
*req
= ahash_request_cast(req_async
);
756 struct cryptd_hash_request_ctx
*rctx
;
758 rctx
= ahash_request_ctx(req
);
760 if (unlikely(err
== -EINPROGRESS
))
763 err
= shash_ahash_update(req
, &rctx
->desc
);
765 req
->base
.complete
= rctx
->complete
;
768 cryptd_hash_complete(req
, err
);
771 static int cryptd_hash_update_enqueue(struct ahash_request
*req
)
773 return cryptd_hash_enqueue(req
, cryptd_hash_update
);
776 static void cryptd_hash_final(struct crypto_async_request
*req_async
, int err
)
778 struct ahash_request
*req
= ahash_request_cast(req_async
);
779 struct cryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
781 if (unlikely(err
== -EINPROGRESS
))
784 err
= crypto_shash_final(&rctx
->desc
, req
->result
);
786 req
->base
.complete
= rctx
->complete
;
789 cryptd_hash_complete(req
, err
);
792 static int cryptd_hash_final_enqueue(struct ahash_request
*req
)
794 return cryptd_hash_enqueue(req
, cryptd_hash_final
);
797 static void cryptd_hash_finup(struct crypto_async_request
*req_async
, int err
)
799 struct ahash_request
*req
= ahash_request_cast(req_async
);
800 struct cryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
802 if (unlikely(err
== -EINPROGRESS
))
805 err
= shash_ahash_finup(req
, &rctx
->desc
);
807 req
->base
.complete
= rctx
->complete
;
810 cryptd_hash_complete(req
, err
);
813 static int cryptd_hash_finup_enqueue(struct ahash_request
*req
)
815 return cryptd_hash_enqueue(req
, cryptd_hash_finup
);
818 static void cryptd_hash_digest(struct crypto_async_request
*req_async
, int err
)
820 struct cryptd_hash_ctx
*ctx
= crypto_tfm_ctx(req_async
->tfm
);
821 struct crypto_shash
*child
= ctx
->child
;
822 struct ahash_request
*req
= ahash_request_cast(req_async
);
823 struct cryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
824 struct shash_desc
*desc
= &rctx
->desc
;
826 if (unlikely(err
== -EINPROGRESS
))
830 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
832 err
= shash_ahash_digest(req
, desc
);
834 req
->base
.complete
= rctx
->complete
;
837 cryptd_hash_complete(req
, err
);
840 static int cryptd_hash_digest_enqueue(struct ahash_request
*req
)
842 return cryptd_hash_enqueue(req
, cryptd_hash_digest
);
845 static int cryptd_hash_export(struct ahash_request
*req
, void *out
)
847 struct cryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
849 return crypto_shash_export(&rctx
->desc
, out
);
852 static int cryptd_hash_import(struct ahash_request
*req
, const void *in
)
854 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
855 struct cryptd_hash_ctx
*ctx
= crypto_ahash_ctx(tfm
);
856 struct shash_desc
*desc
= cryptd_shash_desc(req
);
858 desc
->tfm
= ctx
->child
;
859 desc
->flags
= req
->base
.flags
;
861 return crypto_shash_import(desc
, in
);
864 static int cryptd_create_hash(struct crypto_template
*tmpl
, struct rtattr
**tb
,
865 struct cryptd_queue
*queue
)
867 struct hashd_instance_ctx
*ctx
;
868 struct ahash_instance
*inst
;
869 struct shash_alg
*salg
;
870 struct crypto_alg
*alg
;
875 cryptd_check_internal(tb
, &type
, &mask
);
877 salg
= shash_attr_alg(tb
[1], type
, mask
);
879 return PTR_ERR(salg
);
882 inst
= cryptd_alloc_instance(alg
, ahash_instance_headroom(),
888 ctx
= ahash_instance_ctx(inst
);
891 err
= crypto_init_shash_spawn(&ctx
->spawn
, salg
,
892 ahash_crypto_instance(inst
));
896 type
= CRYPTO_ALG_ASYNC
;
897 if (alg
->cra_flags
& CRYPTO_ALG_INTERNAL
)
898 type
|= CRYPTO_ALG_INTERNAL
;
899 inst
->alg
.halg
.base
.cra_flags
= type
;
901 inst
->alg
.halg
.digestsize
= salg
->digestsize
;
902 inst
->alg
.halg
.statesize
= salg
->statesize
;
903 inst
->alg
.halg
.base
.cra_ctxsize
= sizeof(struct cryptd_hash_ctx
);
905 inst
->alg
.halg
.base
.cra_init
= cryptd_hash_init_tfm
;
906 inst
->alg
.halg
.base
.cra_exit
= cryptd_hash_exit_tfm
;
908 inst
->alg
.init
= cryptd_hash_init_enqueue
;
909 inst
->alg
.update
= cryptd_hash_update_enqueue
;
910 inst
->alg
.final
= cryptd_hash_final_enqueue
;
911 inst
->alg
.finup
= cryptd_hash_finup_enqueue
;
912 inst
->alg
.export
= cryptd_hash_export
;
913 inst
->alg
.import
= cryptd_hash_import
;
914 inst
->alg
.setkey
= cryptd_hash_setkey
;
915 inst
->alg
.digest
= cryptd_hash_digest_enqueue
;
917 err
= ahash_register_instance(tmpl
, inst
);
919 crypto_drop_shash(&ctx
->spawn
);
929 static int cryptd_aead_setkey(struct crypto_aead
*parent
,
930 const u8
*key
, unsigned int keylen
)
932 struct cryptd_aead_ctx
*ctx
= crypto_aead_ctx(parent
);
933 struct crypto_aead
*child
= ctx
->child
;
935 return crypto_aead_setkey(child
, key
, keylen
);
938 static int cryptd_aead_setauthsize(struct crypto_aead
*parent
,
939 unsigned int authsize
)
941 struct cryptd_aead_ctx
*ctx
= crypto_aead_ctx(parent
);
942 struct crypto_aead
*child
= ctx
->child
;
944 return crypto_aead_setauthsize(child
, authsize
);
947 static void cryptd_aead_crypt(struct aead_request
*req
,
948 struct crypto_aead
*child
,
950 int (*crypt
)(struct aead_request
*req
))
952 struct cryptd_aead_request_ctx
*rctx
;
953 struct cryptd_aead_ctx
*ctx
;
954 crypto_completion_t
compl;
955 struct crypto_aead
*tfm
;
958 rctx
= aead_request_ctx(req
);
959 compl = rctx
->complete
;
961 tfm
= crypto_aead_reqtfm(req
);
963 if (unlikely(err
== -EINPROGRESS
))
965 aead_request_set_tfm(req
, child
);
969 ctx
= crypto_aead_ctx(tfm
);
970 refcnt
= atomic_read(&ctx
->refcnt
);
973 compl(&req
->base
, err
);
976 if (err
!= -EINPROGRESS
&& refcnt
&& atomic_dec_and_test(&ctx
->refcnt
))
977 crypto_free_aead(tfm
);
980 static void cryptd_aead_encrypt(struct crypto_async_request
*areq
, int err
)
982 struct cryptd_aead_ctx
*ctx
= crypto_tfm_ctx(areq
->tfm
);
983 struct crypto_aead
*child
= ctx
->child
;
984 struct aead_request
*req
;
986 req
= container_of(areq
, struct aead_request
, base
);
987 cryptd_aead_crypt(req
, child
, err
, crypto_aead_alg(child
)->encrypt
);
990 static void cryptd_aead_decrypt(struct crypto_async_request
*areq
, int err
)
992 struct cryptd_aead_ctx
*ctx
= crypto_tfm_ctx(areq
->tfm
);
993 struct crypto_aead
*child
= ctx
->child
;
994 struct aead_request
*req
;
996 req
= container_of(areq
, struct aead_request
, base
);
997 cryptd_aead_crypt(req
, child
, err
, crypto_aead_alg(child
)->decrypt
);
1000 static int cryptd_aead_enqueue(struct aead_request
*req
,
1001 crypto_completion_t
compl)
1003 struct cryptd_aead_request_ctx
*rctx
= aead_request_ctx(req
);
1004 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1005 struct cryptd_queue
*queue
= cryptd_get_queue(crypto_aead_tfm(tfm
));
1007 rctx
->complete
= req
->base
.complete
;
1008 req
->base
.complete
= compl;
1009 return cryptd_enqueue_request(queue
, &req
->base
);
1012 static int cryptd_aead_encrypt_enqueue(struct aead_request
*req
)
1014 return cryptd_aead_enqueue(req
, cryptd_aead_encrypt
);
1017 static int cryptd_aead_decrypt_enqueue(struct aead_request
*req
)
1019 return cryptd_aead_enqueue(req
, cryptd_aead_decrypt
);
1022 static int cryptd_aead_init_tfm(struct crypto_aead
*tfm
)
1024 struct aead_instance
*inst
= aead_alg_instance(tfm
);
1025 struct aead_instance_ctx
*ictx
= aead_instance_ctx(inst
);
1026 struct crypto_aead_spawn
*spawn
= &ictx
->aead_spawn
;
1027 struct cryptd_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1028 struct crypto_aead
*cipher
;
1030 cipher
= crypto_spawn_aead(spawn
);
1032 return PTR_ERR(cipher
);
1034 ctx
->child
= cipher
;
1035 crypto_aead_set_reqsize(
1036 tfm
, max((unsigned)sizeof(struct cryptd_aead_request_ctx
),
1037 crypto_aead_reqsize(cipher
)));
1041 static void cryptd_aead_exit_tfm(struct crypto_aead
*tfm
)
1043 struct cryptd_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1044 crypto_free_aead(ctx
->child
);
1047 static int cryptd_create_aead(struct crypto_template
*tmpl
,
1049 struct cryptd_queue
*queue
)
1051 struct aead_instance_ctx
*ctx
;
1052 struct aead_instance
*inst
;
1053 struct aead_alg
*alg
;
1056 u32 mask
= CRYPTO_ALG_ASYNC
;
1059 cryptd_check_internal(tb
, &type
, &mask
);
1061 name
= crypto_attr_alg_name(tb
[1]);
1063 return PTR_ERR(name
);
1065 inst
= kzalloc(sizeof(*inst
) + sizeof(*ctx
), GFP_KERNEL
);
1069 ctx
= aead_instance_ctx(inst
);
1072 crypto_set_aead_spawn(&ctx
->aead_spawn
, aead_crypto_instance(inst
));
1073 err
= crypto_grab_aead(&ctx
->aead_spawn
, name
, type
, mask
);
1077 alg
= crypto_spawn_aead_alg(&ctx
->aead_spawn
);
1078 err
= cryptd_init_instance(aead_crypto_instance(inst
), &alg
->base
);
1082 inst
->alg
.base
.cra_flags
= CRYPTO_ALG_ASYNC
|
1083 (alg
->base
.cra_flags
& CRYPTO_ALG_INTERNAL
);
1084 inst
->alg
.base
.cra_ctxsize
= sizeof(struct cryptd_aead_ctx
);
1086 inst
->alg
.ivsize
= crypto_aead_alg_ivsize(alg
);
1087 inst
->alg
.maxauthsize
= crypto_aead_alg_maxauthsize(alg
);
1089 inst
->alg
.init
= cryptd_aead_init_tfm
;
1090 inst
->alg
.exit
= cryptd_aead_exit_tfm
;
1091 inst
->alg
.setkey
= cryptd_aead_setkey
;
1092 inst
->alg
.setauthsize
= cryptd_aead_setauthsize
;
1093 inst
->alg
.encrypt
= cryptd_aead_encrypt_enqueue
;
1094 inst
->alg
.decrypt
= cryptd_aead_decrypt_enqueue
;
1096 err
= aead_register_instance(tmpl
, inst
);
1099 crypto_drop_aead(&ctx
->aead_spawn
);
1106 static struct cryptd_queue queue
;
1108 static int cryptd_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
1110 struct crypto_attr_type
*algt
;
1112 algt
= crypto_get_attr_type(tb
);
1114 return PTR_ERR(algt
);
1116 switch (algt
->type
& algt
->mask
& CRYPTO_ALG_TYPE_MASK
) {
1117 case CRYPTO_ALG_TYPE_BLKCIPHER
:
1118 if ((algt
->type
& CRYPTO_ALG_TYPE_MASK
) ==
1119 CRYPTO_ALG_TYPE_BLKCIPHER
)
1120 return cryptd_create_blkcipher(tmpl
, tb
, &queue
);
1122 return cryptd_create_skcipher(tmpl
, tb
, &queue
);
1123 case CRYPTO_ALG_TYPE_DIGEST
:
1124 return cryptd_create_hash(tmpl
, tb
, &queue
);
1125 case CRYPTO_ALG_TYPE_AEAD
:
1126 return cryptd_create_aead(tmpl
, tb
, &queue
);
1132 static void cryptd_free(struct crypto_instance
*inst
)
1134 struct cryptd_instance_ctx
*ctx
= crypto_instance_ctx(inst
);
1135 struct hashd_instance_ctx
*hctx
= crypto_instance_ctx(inst
);
1136 struct aead_instance_ctx
*aead_ctx
= crypto_instance_ctx(inst
);
1138 switch (inst
->alg
.cra_flags
& CRYPTO_ALG_TYPE_MASK
) {
1139 case CRYPTO_ALG_TYPE_AHASH
:
1140 crypto_drop_shash(&hctx
->spawn
);
1141 kfree(ahash_instance(inst
));
1143 case CRYPTO_ALG_TYPE_AEAD
:
1144 crypto_drop_aead(&aead_ctx
->aead_spawn
);
1145 kfree(aead_instance(inst
));
1148 crypto_drop_spawn(&ctx
->spawn
);
1153 static struct crypto_template cryptd_tmpl
= {
1155 .create
= cryptd_create
,
1156 .free
= cryptd_free
,
1157 .module
= THIS_MODULE
,
1160 struct cryptd_ablkcipher
*cryptd_alloc_ablkcipher(const char *alg_name
,
1163 char cryptd_alg_name
[CRYPTO_MAX_ALG_NAME
];
1164 struct cryptd_blkcipher_ctx
*ctx
;
1165 struct crypto_tfm
*tfm
;
1167 if (snprintf(cryptd_alg_name
, CRYPTO_MAX_ALG_NAME
,
1168 "cryptd(%s)", alg_name
) >= CRYPTO_MAX_ALG_NAME
)
1169 return ERR_PTR(-EINVAL
);
1170 type
= crypto_skcipher_type(type
);
1171 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
1172 mask
|= (CRYPTO_ALG_GENIV
| CRYPTO_ALG_TYPE_BLKCIPHER_MASK
);
1173 tfm
= crypto_alloc_base(cryptd_alg_name
, type
, mask
);
1175 return ERR_CAST(tfm
);
1176 if (tfm
->__crt_alg
->cra_module
!= THIS_MODULE
) {
1177 crypto_free_tfm(tfm
);
1178 return ERR_PTR(-EINVAL
);
1181 ctx
= crypto_tfm_ctx(tfm
);
1182 atomic_set(&ctx
->refcnt
, 1);
1184 return __cryptd_ablkcipher_cast(__crypto_ablkcipher_cast(tfm
));
1186 EXPORT_SYMBOL_GPL(cryptd_alloc_ablkcipher
);
1188 struct crypto_blkcipher
*cryptd_ablkcipher_child(struct cryptd_ablkcipher
*tfm
)
1190 struct cryptd_blkcipher_ctx
*ctx
= crypto_ablkcipher_ctx(&tfm
->base
);
1193 EXPORT_SYMBOL_GPL(cryptd_ablkcipher_child
);
1195 bool cryptd_ablkcipher_queued(struct cryptd_ablkcipher
*tfm
)
1197 struct cryptd_blkcipher_ctx
*ctx
= crypto_ablkcipher_ctx(&tfm
->base
);
1199 return atomic_read(&ctx
->refcnt
) - 1;
1201 EXPORT_SYMBOL_GPL(cryptd_ablkcipher_queued
);
1203 void cryptd_free_ablkcipher(struct cryptd_ablkcipher
*tfm
)
1205 struct cryptd_blkcipher_ctx
*ctx
= crypto_ablkcipher_ctx(&tfm
->base
);
1207 if (atomic_dec_and_test(&ctx
->refcnt
))
1208 crypto_free_ablkcipher(&tfm
->base
);
1210 EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher
);
1212 struct cryptd_skcipher
*cryptd_alloc_skcipher(const char *alg_name
,
1215 char cryptd_alg_name
[CRYPTO_MAX_ALG_NAME
];
1216 struct cryptd_skcipher_ctx
*ctx
;
1217 struct crypto_skcipher
*tfm
;
1219 if (snprintf(cryptd_alg_name
, CRYPTO_MAX_ALG_NAME
,
1220 "cryptd(%s)", alg_name
) >= CRYPTO_MAX_ALG_NAME
)
1221 return ERR_PTR(-EINVAL
);
1223 tfm
= crypto_alloc_skcipher(cryptd_alg_name
, type
, mask
);
1225 return ERR_CAST(tfm
);
1227 if (tfm
->base
.__crt_alg
->cra_module
!= THIS_MODULE
) {
1228 crypto_free_skcipher(tfm
);
1229 return ERR_PTR(-EINVAL
);
1232 ctx
= crypto_skcipher_ctx(tfm
);
1233 atomic_set(&ctx
->refcnt
, 1);
1235 return container_of(tfm
, struct cryptd_skcipher
, base
);
1237 EXPORT_SYMBOL_GPL(cryptd_alloc_skcipher
);
1239 struct crypto_skcipher
*cryptd_skcipher_child(struct cryptd_skcipher
*tfm
)
1241 struct cryptd_skcipher_ctx
*ctx
= crypto_skcipher_ctx(&tfm
->base
);
1245 EXPORT_SYMBOL_GPL(cryptd_skcipher_child
);
1247 bool cryptd_skcipher_queued(struct cryptd_skcipher
*tfm
)
1249 struct cryptd_skcipher_ctx
*ctx
= crypto_skcipher_ctx(&tfm
->base
);
1251 return atomic_read(&ctx
->refcnt
) - 1;
1253 EXPORT_SYMBOL_GPL(cryptd_skcipher_queued
);
1255 void cryptd_free_skcipher(struct cryptd_skcipher
*tfm
)
1257 struct cryptd_skcipher_ctx
*ctx
= crypto_skcipher_ctx(&tfm
->base
);
1259 if (atomic_dec_and_test(&ctx
->refcnt
))
1260 crypto_free_skcipher(&tfm
->base
);
1262 EXPORT_SYMBOL_GPL(cryptd_free_skcipher
);
1264 struct cryptd_ahash
*cryptd_alloc_ahash(const char *alg_name
,
1267 char cryptd_alg_name
[CRYPTO_MAX_ALG_NAME
];
1268 struct cryptd_hash_ctx
*ctx
;
1269 struct crypto_ahash
*tfm
;
1271 if (snprintf(cryptd_alg_name
, CRYPTO_MAX_ALG_NAME
,
1272 "cryptd(%s)", alg_name
) >= CRYPTO_MAX_ALG_NAME
)
1273 return ERR_PTR(-EINVAL
);
1274 tfm
= crypto_alloc_ahash(cryptd_alg_name
, type
, mask
);
1276 return ERR_CAST(tfm
);
1277 if (tfm
->base
.__crt_alg
->cra_module
!= THIS_MODULE
) {
1278 crypto_free_ahash(tfm
);
1279 return ERR_PTR(-EINVAL
);
1282 ctx
= crypto_ahash_ctx(tfm
);
1283 atomic_set(&ctx
->refcnt
, 1);
1285 return __cryptd_ahash_cast(tfm
);
1287 EXPORT_SYMBOL_GPL(cryptd_alloc_ahash
);
1289 struct crypto_shash
*cryptd_ahash_child(struct cryptd_ahash
*tfm
)
1291 struct cryptd_hash_ctx
*ctx
= crypto_ahash_ctx(&tfm
->base
);
1295 EXPORT_SYMBOL_GPL(cryptd_ahash_child
);
1297 struct shash_desc
*cryptd_shash_desc(struct ahash_request
*req
)
1299 struct cryptd_hash_request_ctx
*rctx
= ahash_request_ctx(req
);
1302 EXPORT_SYMBOL_GPL(cryptd_shash_desc
);
1304 bool cryptd_ahash_queued(struct cryptd_ahash
*tfm
)
1306 struct cryptd_hash_ctx
*ctx
= crypto_ahash_ctx(&tfm
->base
);
1308 return atomic_read(&ctx
->refcnt
) - 1;
1310 EXPORT_SYMBOL_GPL(cryptd_ahash_queued
);
1312 void cryptd_free_ahash(struct cryptd_ahash
*tfm
)
1314 struct cryptd_hash_ctx
*ctx
= crypto_ahash_ctx(&tfm
->base
);
1316 if (atomic_dec_and_test(&ctx
->refcnt
))
1317 crypto_free_ahash(&tfm
->base
);
1319 EXPORT_SYMBOL_GPL(cryptd_free_ahash
);
1321 struct cryptd_aead
*cryptd_alloc_aead(const char *alg_name
,
1324 char cryptd_alg_name
[CRYPTO_MAX_ALG_NAME
];
1325 struct cryptd_aead_ctx
*ctx
;
1326 struct crypto_aead
*tfm
;
1328 if (snprintf(cryptd_alg_name
, CRYPTO_MAX_ALG_NAME
,
1329 "cryptd(%s)", alg_name
) >= CRYPTO_MAX_ALG_NAME
)
1330 return ERR_PTR(-EINVAL
);
1331 tfm
= crypto_alloc_aead(cryptd_alg_name
, type
, mask
);
1333 return ERR_CAST(tfm
);
1334 if (tfm
->base
.__crt_alg
->cra_module
!= THIS_MODULE
) {
1335 crypto_free_aead(tfm
);
1336 return ERR_PTR(-EINVAL
);
1339 ctx
= crypto_aead_ctx(tfm
);
1340 atomic_set(&ctx
->refcnt
, 1);
1342 return __cryptd_aead_cast(tfm
);
1344 EXPORT_SYMBOL_GPL(cryptd_alloc_aead
);
1346 struct crypto_aead
*cryptd_aead_child(struct cryptd_aead
*tfm
)
1348 struct cryptd_aead_ctx
*ctx
;
1349 ctx
= crypto_aead_ctx(&tfm
->base
);
1352 EXPORT_SYMBOL_GPL(cryptd_aead_child
);
1354 bool cryptd_aead_queued(struct cryptd_aead
*tfm
)
1356 struct cryptd_aead_ctx
*ctx
= crypto_aead_ctx(&tfm
->base
);
1358 return atomic_read(&ctx
->refcnt
) - 1;
1360 EXPORT_SYMBOL_GPL(cryptd_aead_queued
);
1362 void cryptd_free_aead(struct cryptd_aead
*tfm
)
1364 struct cryptd_aead_ctx
*ctx
= crypto_aead_ctx(&tfm
->base
);
1366 if (atomic_dec_and_test(&ctx
->refcnt
))
1367 crypto_free_aead(&tfm
->base
);
1369 EXPORT_SYMBOL_GPL(cryptd_free_aead
);
1371 static int __init
cryptd_init(void)
1375 err
= cryptd_init_queue(&queue
, CRYPTD_MAX_CPU_QLEN
);
1379 err
= crypto_register_template(&cryptd_tmpl
);
1381 cryptd_fini_queue(&queue
);
1386 static void __exit
cryptd_exit(void)
1388 cryptd_fini_queue(&queue
);
1389 crypto_unregister_template(&cryptd_tmpl
);
1392 subsys_initcall(cryptd_init
);
1393 module_exit(cryptd_exit
);
1395 MODULE_LICENSE("GPL");
1396 MODULE_DESCRIPTION("Software async crypto daemon");
1397 MODULE_ALIAS_CRYPTO("cryptd");