2 * Software async crypto daemon.
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
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)
13 #include <crypto/algapi.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/kthread.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/scatterlist.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
26 #define CRYPTD_MAX_QLEN 100
31 struct crypto_queue queue
;
32 struct task_struct
*task
;
35 struct cryptd_instance_ctx
{
36 struct crypto_spawn spawn
;
37 struct cryptd_state
*state
;
40 struct cryptd_blkcipher_ctx
{
41 struct crypto_blkcipher
*child
;
44 struct cryptd_blkcipher_request_ctx
{
45 crypto_completion_t complete
;
49 static inline struct cryptd_state
*cryptd_get_state(struct crypto_tfm
*tfm
)
51 struct crypto_instance
*inst
= crypto_tfm_alg_instance(tfm
);
52 struct cryptd_instance_ctx
*ictx
= crypto_instance_ctx(inst
);
56 static int cryptd_blkcipher_setkey(struct crypto_ablkcipher
*parent
,
57 const u8
*key
, unsigned int keylen
)
59 struct cryptd_blkcipher_ctx
*ctx
= crypto_ablkcipher_ctx(parent
);
60 struct crypto_blkcipher
*child
= ctx
->child
;
63 crypto_blkcipher_clear_flags(child
, CRYPTO_TFM_REQ_MASK
);
64 crypto_blkcipher_set_flags(child
, crypto_ablkcipher_get_flags(parent
) &
66 err
= crypto_blkcipher_setkey(child
, key
, keylen
);
67 crypto_ablkcipher_set_flags(parent
, crypto_blkcipher_get_flags(child
) &
72 static void cryptd_blkcipher_crypt(struct ablkcipher_request
*req
,
73 struct crypto_blkcipher
*child
,
75 int (*crypt
)(struct blkcipher_desc
*desc
,
76 struct scatterlist
*dst
,
77 struct scatterlist
*src
,
80 struct cryptd_blkcipher_request_ctx
*rctx
;
81 struct blkcipher_desc desc
;
83 rctx
= ablkcipher_request_ctx(req
);
85 if (unlikely(err
== -EINPROGRESS
)) {
86 rctx
->complete(&req
->base
, err
);
91 desc
.info
= req
->info
;
92 desc
.flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
94 err
= crypt(&desc
, req
->dst
, req
->src
, req
->nbytes
);
96 req
->base
.complete
= rctx
->complete
;
99 req
->base
.complete(&req
->base
, err
);
103 static void cryptd_blkcipher_encrypt(struct crypto_async_request
*req
, int err
)
105 struct cryptd_blkcipher_ctx
*ctx
= crypto_tfm_ctx(req
->tfm
);
106 struct crypto_blkcipher
*child
= ctx
->child
;
108 cryptd_blkcipher_crypt(ablkcipher_request_cast(req
), child
, err
,
109 crypto_blkcipher_crt(child
)->encrypt
);
112 static void cryptd_blkcipher_decrypt(struct crypto_async_request
*req
, int err
)
114 struct cryptd_blkcipher_ctx
*ctx
= crypto_tfm_ctx(req
->tfm
);
115 struct crypto_blkcipher
*child
= ctx
->child
;
117 cryptd_blkcipher_crypt(ablkcipher_request_cast(req
), child
, err
,
118 crypto_blkcipher_crt(child
)->decrypt
);
121 static int cryptd_blkcipher_enqueue(struct ablkcipher_request
*req
,
122 crypto_completion_t complete
)
124 struct cryptd_blkcipher_request_ctx
*rctx
= ablkcipher_request_ctx(req
);
125 struct crypto_ablkcipher
*tfm
= crypto_ablkcipher_reqtfm(req
);
126 struct cryptd_state
*state
=
127 cryptd_get_state(crypto_ablkcipher_tfm(tfm
));
130 rctx
->complete
= req
->base
.complete
;
131 req
->base
.complete
= complete
;
133 spin_lock_bh(&state
->lock
);
134 err
= ablkcipher_enqueue_request(crypto_ablkcipher_alg(tfm
), req
);
135 spin_unlock_bh(&state
->lock
);
137 wake_up_process(state
->task
);
141 static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request
*req
)
143 return cryptd_blkcipher_enqueue(req
, cryptd_blkcipher_encrypt
);
146 static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request
*req
)
148 return cryptd_blkcipher_enqueue(req
, cryptd_blkcipher_decrypt
);
151 static int cryptd_blkcipher_init_tfm(struct crypto_tfm
*tfm
)
153 struct crypto_instance
*inst
= crypto_tfm_alg_instance(tfm
);
154 struct cryptd_instance_ctx
*ictx
= crypto_instance_ctx(inst
);
155 struct crypto_spawn
*spawn
= &ictx
->spawn
;
156 struct cryptd_blkcipher_ctx
*ctx
= crypto_tfm_ctx(tfm
);
157 struct crypto_blkcipher
*cipher
;
159 cipher
= crypto_spawn_blkcipher(spawn
);
161 return PTR_ERR(cipher
);
164 tfm
->crt_ablkcipher
.reqsize
=
165 sizeof(struct cryptd_blkcipher_request_ctx
);
169 static void cryptd_blkcipher_exit_tfm(struct crypto_tfm
*tfm
)
171 struct cryptd_blkcipher_ctx
*ctx
= crypto_tfm_ctx(tfm
);
172 struct cryptd_state
*state
= cryptd_get_state(tfm
);
175 mutex_lock(&state
->mutex
);
176 active
= ablkcipher_tfm_in_queue(__crypto_ablkcipher_cast(tfm
));
177 mutex_unlock(&state
->mutex
);
181 crypto_free_blkcipher(ctx
->child
);
184 static struct crypto_instance
*cryptd_alloc_instance(struct crypto_alg
*alg
,
185 struct cryptd_state
*state
)
187 struct crypto_instance
*inst
;
188 struct cryptd_instance_ctx
*ctx
;
191 inst
= kzalloc(sizeof(*inst
) + sizeof(*ctx
), GFP_KERNEL
);
196 if (snprintf(inst
->alg
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
,
197 "cryptd(%s)", alg
->cra_driver_name
) >= CRYPTO_MAX_ALG_NAME
)
200 ctx
= crypto_instance_ctx(inst
);
201 err
= crypto_init_spawn(&ctx
->spawn
, alg
, inst
,
202 CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_ASYNC
);
208 memcpy(inst
->alg
.cra_name
, alg
->cra_name
, CRYPTO_MAX_ALG_NAME
);
210 inst
->alg
.cra_priority
= alg
->cra_priority
+ 50;
211 inst
->alg
.cra_blocksize
= alg
->cra_blocksize
;
212 inst
->alg
.cra_alignmask
= alg
->cra_alignmask
;
223 static struct crypto_instance
*cryptd_alloc_blkcipher(
224 struct rtattr
**tb
, struct cryptd_state
*state
)
226 struct crypto_instance
*inst
;
227 struct crypto_alg
*alg
;
229 alg
= crypto_get_attr_alg(tb
, CRYPTO_ALG_TYPE_BLKCIPHER
,
230 CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_ASYNC
);
232 return ERR_PTR(PTR_ERR(alg
));
234 inst
= cryptd_alloc_instance(alg
, state
);
238 inst
->alg
.cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
| CRYPTO_ALG_ASYNC
;
239 inst
->alg
.cra_type
= &crypto_ablkcipher_type
;
241 inst
->alg
.cra_ablkcipher
.ivsize
= alg
->cra_blkcipher
.ivsize
;
242 inst
->alg
.cra_ablkcipher
.min_keysize
= alg
->cra_blkcipher
.min_keysize
;
243 inst
->alg
.cra_ablkcipher
.max_keysize
= alg
->cra_blkcipher
.max_keysize
;
245 inst
->alg
.cra_ctxsize
= sizeof(struct cryptd_blkcipher_ctx
);
247 inst
->alg
.cra_init
= cryptd_blkcipher_init_tfm
;
248 inst
->alg
.cra_exit
= cryptd_blkcipher_exit_tfm
;
250 inst
->alg
.cra_ablkcipher
.setkey
= cryptd_blkcipher_setkey
;
251 inst
->alg
.cra_ablkcipher
.encrypt
= cryptd_blkcipher_encrypt_enqueue
;
252 inst
->alg
.cra_ablkcipher
.decrypt
= cryptd_blkcipher_decrypt_enqueue
;
254 inst
->alg
.cra_ablkcipher
.queue
= &state
->queue
;
261 static struct cryptd_state state
;
263 static struct crypto_instance
*cryptd_alloc(struct rtattr
**tb
)
265 struct crypto_attr_type
*algt
;
267 algt
= crypto_get_attr_type(tb
);
269 return ERR_PTR(PTR_ERR(algt
));
271 switch (algt
->type
& algt
->mask
& CRYPTO_ALG_TYPE_MASK
) {
272 case CRYPTO_ALG_TYPE_BLKCIPHER
:
273 return cryptd_alloc_blkcipher(tb
, &state
);
276 return ERR_PTR(-EINVAL
);
279 static void cryptd_free(struct crypto_instance
*inst
)
281 struct cryptd_instance_ctx
*ctx
= crypto_instance_ctx(inst
);
283 crypto_drop_spawn(&ctx
->spawn
);
287 static struct crypto_template cryptd_tmpl
= {
289 .alloc
= cryptd_alloc
,
291 .module
= THIS_MODULE
,
294 static inline int cryptd_create_thread(struct cryptd_state
*state
,
295 int (*fn
)(void *data
), const char *name
)
297 spin_lock_init(&state
->lock
);
298 mutex_init(&state
->mutex
);
299 crypto_init_queue(&state
->queue
, CRYPTD_MAX_QLEN
);
301 state
->task
= kthread_create(fn
, state
, name
);
302 if (IS_ERR(state
->task
))
303 return PTR_ERR(state
->task
);
308 static inline void cryptd_stop_thread(struct cryptd_state
*state
)
310 BUG_ON(state
->queue
.qlen
);
311 kthread_stop(state
->task
);
314 static int cryptd_thread(void *data
)
316 struct cryptd_state
*state
= data
;
320 struct crypto_async_request
*req
, *backlog
;
322 mutex_lock(&state
->mutex
);
323 __set_current_state(TASK_INTERRUPTIBLE
);
325 spin_lock_bh(&state
->lock
);
326 backlog
= crypto_get_backlog(&state
->queue
);
327 req
= crypto_dequeue_request(&state
->queue
);
328 spin_unlock_bh(&state
->lock
);
330 stop
= kthread_should_stop();
333 __set_current_state(TASK_RUNNING
);
336 backlog
->complete(backlog
,
338 req
->complete(req
, 0);
342 mutex_unlock(&state
->mutex
);
350 static int __init
cryptd_init(void)
354 err
= cryptd_create_thread(&state
, cryptd_thread
, "cryptd");
358 err
= crypto_register_template(&cryptd_tmpl
);
360 kthread_stop(state
.task
);
365 static void __exit
cryptd_exit(void)
367 cryptd_stop_thread(&state
);
368 crypto_unregister_template(&cryptd_tmpl
);
371 module_init(cryptd_init
);
372 module_exit(cryptd_exit
);
374 MODULE_LICENSE("GPL");
375 MODULE_DESCRIPTION("Software async crypto daemon");