]>
Commit | Line | Data |
---|---|---|
124b53d0 HX |
1 | /* |
2 | * Software async crypto daemon. | |
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 | ||
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> | |
25 | ||
26 | #define CRYPTD_MAX_QLEN 100 | |
27 | ||
28 | struct cryptd_state { | |
29 | spinlock_t lock; | |
30 | struct mutex mutex; | |
31 | struct crypto_queue queue; | |
32 | struct task_struct *task; | |
33 | }; | |
34 | ||
35 | struct cryptd_instance_ctx { | |
36 | struct crypto_spawn spawn; | |
37 | struct cryptd_state *state; | |
38 | }; | |
39 | ||
40 | struct cryptd_blkcipher_ctx { | |
41 | struct crypto_blkcipher *child; | |
42 | }; | |
43 | ||
44 | struct cryptd_blkcipher_request_ctx { | |
45 | crypto_completion_t complete; | |
46 | }; | |
47 | ||
48 | ||
49 | static inline struct cryptd_state *cryptd_get_state(struct crypto_tfm *tfm) | |
50 | { | |
51 | struct crypto_instance *inst = crypto_tfm_alg_instance(tfm); | |
52 | struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst); | |
53 | return ictx->state; | |
54 | } | |
55 | ||
56 | static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent, | |
57 | const u8 *key, unsigned int keylen) | |
58 | { | |
59 | struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent); | |
60 | struct crypto_blkcipher *child = ctx->child; | |
61 | int err; | |
62 | ||
63 | crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); | |
64 | crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) & | |
65 | CRYPTO_TFM_REQ_MASK); | |
66 | err = crypto_blkcipher_setkey(child, key, keylen); | |
67 | crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) & | |
68 | CRYPTO_TFM_RES_MASK); | |
69 | return err; | |
70 | } | |
71 | ||
72 | static void cryptd_blkcipher_crypt(struct ablkcipher_request *req, | |
73 | struct crypto_blkcipher *child, | |
74 | int err, | |
75 | int (*crypt)(struct blkcipher_desc *desc, | |
76 | struct scatterlist *dst, | |
77 | struct scatterlist *src, | |
78 | unsigned int len)) | |
79 | { | |
80 | struct cryptd_blkcipher_request_ctx *rctx; | |
81 | struct blkcipher_desc desc; | |
82 | ||
83 | rctx = ablkcipher_request_ctx(req); | |
84 | ||
93aa7f8a HX |
85 | if (unlikely(err == -EINPROGRESS)) |
86 | goto out; | |
124b53d0 HX |
87 | |
88 | desc.tfm = child; | |
89 | desc.info = req->info; | |
90 | desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; | |
91 | ||
92 | err = crypt(&desc, req->dst, req->src, req->nbytes); | |
93 | ||
94 | req->base.complete = rctx->complete; | |
95 | ||
93aa7f8a | 96 | out: |
124b53d0 | 97 | local_bh_disable(); |
93aa7f8a | 98 | rctx->complete(&req->base, err); |
124b53d0 HX |
99 | local_bh_enable(); |
100 | } | |
101 | ||
102 | static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err) | |
103 | { | |
104 | struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm); | |
105 | struct crypto_blkcipher *child = ctx->child; | |
106 | ||
107 | cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err, | |
108 | crypto_blkcipher_crt(child)->encrypt); | |
109 | } | |
110 | ||
111 | static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err) | |
112 | { | |
113 | struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm); | |
114 | struct crypto_blkcipher *child = ctx->child; | |
115 | ||
116 | cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err, | |
117 | crypto_blkcipher_crt(child)->decrypt); | |
118 | } | |
119 | ||
120 | static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req, | |
121 | crypto_completion_t complete) | |
122 | { | |
123 | struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req); | |
124 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); | |
125 | struct cryptd_state *state = | |
126 | cryptd_get_state(crypto_ablkcipher_tfm(tfm)); | |
127 | int err; | |
128 | ||
129 | rctx->complete = req->base.complete; | |
130 | req->base.complete = complete; | |
131 | ||
132 | spin_lock_bh(&state->lock); | |
2de98e75 | 133 | err = ablkcipher_enqueue_request(&state->queue, req); |
124b53d0 HX |
134 | spin_unlock_bh(&state->lock); |
135 | ||
136 | wake_up_process(state->task); | |
137 | return err; | |
138 | } | |
139 | ||
140 | static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req) | |
141 | { | |
142 | return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt); | |
143 | } | |
144 | ||
145 | static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req) | |
146 | { | |
147 | return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt); | |
148 | } | |
149 | ||
150 | static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm) | |
151 | { | |
152 | struct crypto_instance *inst = crypto_tfm_alg_instance(tfm); | |
153 | struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst); | |
154 | struct crypto_spawn *spawn = &ictx->spawn; | |
155 | struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm); | |
156 | struct crypto_blkcipher *cipher; | |
157 | ||
158 | cipher = crypto_spawn_blkcipher(spawn); | |
159 | if (IS_ERR(cipher)) | |
160 | return PTR_ERR(cipher); | |
161 | ||
162 | ctx->child = cipher; | |
163 | tfm->crt_ablkcipher.reqsize = | |
164 | sizeof(struct cryptd_blkcipher_request_ctx); | |
165 | return 0; | |
166 | } | |
167 | ||
168 | static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm) | |
169 | { | |
170 | struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm); | |
171 | struct cryptd_state *state = cryptd_get_state(tfm); | |
172 | int active; | |
173 | ||
174 | mutex_lock(&state->mutex); | |
2de98e75 HX |
175 | active = ablkcipher_tfm_in_queue(&state->queue, |
176 | __crypto_ablkcipher_cast(tfm)); | |
124b53d0 HX |
177 | mutex_unlock(&state->mutex); |
178 | ||
179 | BUG_ON(active); | |
180 | ||
181 | crypto_free_blkcipher(ctx->child); | |
182 | } | |
183 | ||
184 | static struct crypto_instance *cryptd_alloc_instance(struct crypto_alg *alg, | |
185 | struct cryptd_state *state) | |
186 | { | |
187 | struct crypto_instance *inst; | |
188 | struct cryptd_instance_ctx *ctx; | |
189 | int err; | |
190 | ||
191 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); | |
b1145ce3 JL |
192 | if (!inst) { |
193 | inst = ERR_PTR(-ENOMEM); | |
124b53d0 | 194 | goto out; |
b1145ce3 | 195 | } |
124b53d0 HX |
196 | |
197 | err = -ENAMETOOLONG; | |
198 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, | |
199 | "cryptd(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) | |
200 | goto out_free_inst; | |
201 | ||
202 | ctx = crypto_instance_ctx(inst); | |
203 | err = crypto_init_spawn(&ctx->spawn, alg, inst, | |
204 | CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); | |
205 | if (err) | |
206 | goto out_free_inst; | |
207 | ||
208 | ctx->state = state; | |
209 | ||
210 | memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); | |
211 | ||
212 | inst->alg.cra_priority = alg->cra_priority + 50; | |
213 | inst->alg.cra_blocksize = alg->cra_blocksize; | |
214 | inst->alg.cra_alignmask = alg->cra_alignmask; | |
215 | ||
216 | out: | |
217 | return inst; | |
218 | ||
219 | out_free_inst: | |
220 | kfree(inst); | |
221 | inst = ERR_PTR(err); | |
222 | goto out; | |
223 | } | |
224 | ||
225 | static struct crypto_instance *cryptd_alloc_blkcipher( | |
226 | struct rtattr **tb, struct cryptd_state *state) | |
227 | { | |
228 | struct crypto_instance *inst; | |
229 | struct crypto_alg *alg; | |
230 | ||
231 | alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_BLKCIPHER, | |
332f8840 | 232 | CRYPTO_ALG_TYPE_MASK); |
124b53d0 | 233 | if (IS_ERR(alg)) |
e231c2ee | 234 | return ERR_CAST(alg); |
124b53d0 HX |
235 | |
236 | inst = cryptd_alloc_instance(alg, state); | |
237 | if (IS_ERR(inst)) | |
238 | goto out_put_alg; | |
239 | ||
332f8840 | 240 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC; |
124b53d0 HX |
241 | inst->alg.cra_type = &crypto_ablkcipher_type; |
242 | ||
243 | inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize; | |
244 | inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize; | |
245 | inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize; | |
246 | ||
927eead5 HX |
247 | inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv; |
248 | ||
124b53d0 HX |
249 | inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx); |
250 | ||
251 | inst->alg.cra_init = cryptd_blkcipher_init_tfm; | |
252 | inst->alg.cra_exit = cryptd_blkcipher_exit_tfm; | |
253 | ||
254 | inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey; | |
255 | inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue; | |
256 | inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue; | |
257 | ||
124b53d0 HX |
258 | out_put_alg: |
259 | crypto_mod_put(alg); | |
260 | return inst; | |
261 | } | |
262 | ||
263 | static struct cryptd_state state; | |
264 | ||
265 | static struct crypto_instance *cryptd_alloc(struct rtattr **tb) | |
266 | { | |
267 | struct crypto_attr_type *algt; | |
268 | ||
269 | algt = crypto_get_attr_type(tb); | |
270 | if (IS_ERR(algt)) | |
e231c2ee | 271 | return ERR_CAST(algt); |
124b53d0 HX |
272 | |
273 | switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) { | |
274 | case CRYPTO_ALG_TYPE_BLKCIPHER: | |
275 | return cryptd_alloc_blkcipher(tb, &state); | |
276 | } | |
277 | ||
278 | return ERR_PTR(-EINVAL); | |
279 | } | |
280 | ||
281 | static void cryptd_free(struct crypto_instance *inst) | |
282 | { | |
283 | struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst); | |
284 | ||
285 | crypto_drop_spawn(&ctx->spawn); | |
286 | kfree(inst); | |
287 | } | |
288 | ||
289 | static struct crypto_template cryptd_tmpl = { | |
290 | .name = "cryptd", | |
291 | .alloc = cryptd_alloc, | |
292 | .free = cryptd_free, | |
293 | .module = THIS_MODULE, | |
294 | }; | |
295 | ||
296 | static inline int cryptd_create_thread(struct cryptd_state *state, | |
297 | int (*fn)(void *data), const char *name) | |
298 | { | |
299 | spin_lock_init(&state->lock); | |
300 | mutex_init(&state->mutex); | |
301 | crypto_init_queue(&state->queue, CRYPTD_MAX_QLEN); | |
302 | ||
189fe317 | 303 | state->task = kthread_run(fn, state, name); |
124b53d0 HX |
304 | if (IS_ERR(state->task)) |
305 | return PTR_ERR(state->task); | |
306 | ||
307 | return 0; | |
308 | } | |
309 | ||
310 | static inline void cryptd_stop_thread(struct cryptd_state *state) | |
311 | { | |
312 | BUG_ON(state->queue.qlen); | |
313 | kthread_stop(state->task); | |
314 | } | |
315 | ||
316 | static int cryptd_thread(void *data) | |
317 | { | |
318 | struct cryptd_state *state = data; | |
319 | int stop; | |
320 | ||
189fe317 RW |
321 | current->flags |= PF_NOFREEZE; |
322 | ||
124b53d0 HX |
323 | do { |
324 | struct crypto_async_request *req, *backlog; | |
325 | ||
326 | mutex_lock(&state->mutex); | |
327 | __set_current_state(TASK_INTERRUPTIBLE); | |
328 | ||
329 | spin_lock_bh(&state->lock); | |
330 | backlog = crypto_get_backlog(&state->queue); | |
331 | req = crypto_dequeue_request(&state->queue); | |
332 | spin_unlock_bh(&state->lock); | |
333 | ||
334 | stop = kthread_should_stop(); | |
335 | ||
336 | if (stop || req) { | |
337 | __set_current_state(TASK_RUNNING); | |
338 | if (req) { | |
339 | if (backlog) | |
340 | backlog->complete(backlog, | |
341 | -EINPROGRESS); | |
342 | req->complete(req, 0); | |
343 | } | |
344 | } | |
345 | ||
346 | mutex_unlock(&state->mutex); | |
347 | ||
348 | schedule(); | |
349 | } while (!stop); | |
350 | ||
351 | return 0; | |
352 | } | |
353 | ||
354 | static int __init cryptd_init(void) | |
355 | { | |
356 | int err; | |
357 | ||
358 | err = cryptd_create_thread(&state, cryptd_thread, "cryptd"); | |
359 | if (err) | |
360 | return err; | |
361 | ||
362 | err = crypto_register_template(&cryptd_tmpl); | |
363 | if (err) | |
364 | kthread_stop(state.task); | |
365 | ||
366 | return err; | |
367 | } | |
368 | ||
369 | static void __exit cryptd_exit(void) | |
370 | { | |
371 | cryptd_stop_thread(&state); | |
372 | crypto_unregister_template(&cryptd_tmpl); | |
373 | } | |
374 | ||
375 | module_init(cryptd_init); | |
376 | module_exit(cryptd_exit); | |
377 | ||
378 | MODULE_LICENSE("GPL"); | |
379 | MODULE_DESCRIPTION("Software async crypto daemon"); |