]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - crypto/cryptd.c
Merge branch 'drm-fixes-5.2' of git://people.freedesktop.org/~agd5f/linux into drm...
[thirdparty/kernel/stable.git] / crypto / cryptd.c
CommitLineData
124b53d0
HX
1/*
2 * Software async crypto daemon.
3 *
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
5 *
298c926c
AH
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.
12 *
124b53d0
HX
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)
16 * any later version.
17 *
18 */
19
18e33e6d 20#include <crypto/internal/hash.h>
298c926c 21#include <crypto/internal/aead.h>
4e0958d1 22#include <crypto/internal/skcipher.h>
1cac2cbc 23#include <crypto/cryptd.h>
254eff77 24#include <crypto/crypto_wq.h>
81760ea6 25#include <linux/atomic.h>
124b53d0
HX
26#include <linux/err.h>
27#include <linux/init.h>
28#include <linux/kernel.h>
124b53d0
HX
29#include <linux/list.h>
30#include <linux/module.h>
124b53d0
HX
31#include <linux/scatterlist.h>
32#include <linux/sched.h>
33#include <linux/slab.h>
124b53d0 34
eaf356e4 35static unsigned int cryptd_max_cpu_qlen = 1000;
c3a53605
JM
36module_param(cryptd_max_cpu_qlen, uint, 0);
37MODULE_PARM_DESC(cryptd_max_cpu_qlen, "Set cryptd Max queue depth");
124b53d0 38
254eff77 39struct cryptd_cpu_queue {
124b53d0 40 struct crypto_queue queue;
254eff77
HY
41 struct work_struct work;
42};
43
44struct cryptd_queue {
a29d8b8e 45 struct cryptd_cpu_queue __percpu *cpu_queue;
124b53d0
HX
46};
47
48struct cryptd_instance_ctx {
49 struct crypto_spawn spawn;
254eff77 50 struct cryptd_queue *queue;
124b53d0
HX
51};
52
4e0958d1
HX
53struct skcipherd_instance_ctx {
54 struct crypto_skcipher_spawn spawn;
55 struct cryptd_queue *queue;
56};
57
46309d89
HX
58struct hashd_instance_ctx {
59 struct crypto_shash_spawn spawn;
60 struct cryptd_queue *queue;
61};
62
298c926c
AH
63struct aead_instance_ctx {
64 struct crypto_aead_spawn aead_spawn;
65 struct cryptd_queue *queue;
66};
67
4e0958d1
HX
68struct cryptd_skcipher_ctx {
69 atomic_t refcnt;
36b3875a 70 struct crypto_sync_skcipher *child;
4e0958d1
HX
71};
72
73struct cryptd_skcipher_request_ctx {
74 crypto_completion_t complete;
75};
76
b8a28251 77struct cryptd_hash_ctx {
81760ea6 78 atomic_t refcnt;
46309d89 79 struct crypto_shash *child;
b8a28251
LH
80};
81
82struct cryptd_hash_request_ctx {
83 crypto_completion_t complete;
46309d89 84 struct shash_desc desc;
b8a28251 85};
124b53d0 86
298c926c 87struct cryptd_aead_ctx {
81760ea6 88 atomic_t refcnt;
298c926c
AH
89 struct crypto_aead *child;
90};
91
92struct cryptd_aead_request_ctx {
93 crypto_completion_t complete;
94};
95
254eff77
HY
96static void cryptd_queue_worker(struct work_struct *work);
97
98static int cryptd_init_queue(struct cryptd_queue *queue,
99 unsigned int max_cpu_qlen)
100{
101 int cpu;
102 struct cryptd_cpu_queue *cpu_queue;
103
104 queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue);
105 if (!queue->cpu_queue)
106 return -ENOMEM;
107 for_each_possible_cpu(cpu) {
108 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
109 crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
110 INIT_WORK(&cpu_queue->work, cryptd_queue_worker);
111 }
c3a53605 112 pr_info("cryptd: max_cpu_qlen set to %d\n", max_cpu_qlen);
254eff77
HY
113 return 0;
114}
115
116static void cryptd_fini_queue(struct cryptd_queue *queue)
117{
118 int cpu;
119 struct cryptd_cpu_queue *cpu_queue;
120
121 for_each_possible_cpu(cpu) {
122 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
123 BUG_ON(cpu_queue->queue.qlen);
124 }
125 free_percpu(queue->cpu_queue);
126}
127
128static int cryptd_enqueue_request(struct cryptd_queue *queue,
129 struct crypto_async_request *request)
130{
131 int cpu, err;
132 struct cryptd_cpu_queue *cpu_queue;
81760ea6 133 atomic_t *refcnt;
254eff77
HY
134
135 cpu = get_cpu();
0b44f486 136 cpu_queue = this_cpu_ptr(queue->cpu_queue);
254eff77 137 err = crypto_enqueue_request(&cpu_queue->queue, request);
81760ea6
HX
138
139 refcnt = crypto_tfm_ctx(request->tfm);
81760ea6 140
6b80ea38 141 if (err == -ENOSPC)
81760ea6
HX
142 goto out_put_cpu;
143
254eff77 144 queue_work_on(cpu, kcrypto_wq, &cpu_queue->work);
81760ea6
HX
145
146 if (!atomic_read(refcnt))
147 goto out_put_cpu;
148
81760ea6
HX
149 atomic_inc(refcnt);
150
151out_put_cpu:
254eff77
HY
152 put_cpu();
153
154 return err;
155}
156
157/* Called in workqueue context, do one real cryption work (via
158 * req->complete) and reschedule itself if there are more work to
159 * do. */
160static void cryptd_queue_worker(struct work_struct *work)
161{
162 struct cryptd_cpu_queue *cpu_queue;
163 struct crypto_async_request *req, *backlog;
164
165 cpu_queue = container_of(work, struct cryptd_cpu_queue, work);
9efade1b
JK
166 /*
167 * Only handle one request at a time to avoid hogging crypto workqueue.
168 * preempt_disable/enable is used to prevent being preempted by
169 * cryptd_enqueue_request(). local_bh_disable/enable is used to prevent
170 * cryptd_enqueue_request() being accessed from software interrupts.
171 */
172 local_bh_disable();
254eff77
HY
173 preempt_disable();
174 backlog = crypto_get_backlog(&cpu_queue->queue);
175 req = crypto_dequeue_request(&cpu_queue->queue);
176 preempt_enable();
9efade1b 177 local_bh_enable();
254eff77
HY
178
179 if (!req)
180 return;
181
182 if (backlog)
183 backlog->complete(backlog, -EINPROGRESS);
184 req->complete(req, 0);
185
186 if (cpu_queue->queue.qlen)
187 queue_work(kcrypto_wq, &cpu_queue->work);
188}
189
190static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
124b53d0
HX
191{
192 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
193 struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
254eff77 194 return ictx->queue;
124b53d0
HX
195}
196
466a7b9e
SM
197static inline void cryptd_check_internal(struct rtattr **tb, u32 *type,
198 u32 *mask)
199{
200 struct crypto_attr_type *algt;
201
202 algt = crypto_get_attr_type(tb);
203 if (IS_ERR(algt))
204 return;
f6da3205 205
5e4b8c1f
HX
206 *type |= algt->type & CRYPTO_ALG_INTERNAL;
207 *mask |= algt->mask & CRYPTO_ALG_INTERNAL;
466a7b9e
SM
208}
209
9b8c456e
HX
210static int cryptd_init_instance(struct crypto_instance *inst,
211 struct crypto_alg *alg)
212{
213 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
214 "cryptd(%s)",
215 alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
216 return -ENAMETOOLONG;
217
218 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
219
220 inst->alg.cra_priority = alg->cra_priority + 50;
221 inst->alg.cra_blocksize = alg->cra_blocksize;
222 inst->alg.cra_alignmask = alg->cra_alignmask;
223
224 return 0;
225}
226
0b535adf
HX
227static void *cryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
228 unsigned int tail)
124b53d0 229{
0b535adf 230 char *p;
124b53d0 231 struct crypto_instance *inst;
124b53d0
HX
232 int err;
233
0b535adf
HX
234 p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL);
235 if (!p)
236 return ERR_PTR(-ENOMEM);
237
238 inst = (void *)(p + head);
124b53d0 239
9b8c456e
HX
240 err = cryptd_init_instance(inst, alg);
241 if (err)
124b53d0
HX
242 goto out_free_inst;
243
124b53d0 244out:
0b535adf 245 return p;
124b53d0
HX
246
247out_free_inst:
0b535adf
HX
248 kfree(p);
249 p = ERR_PTR(err);
124b53d0
HX
250 goto out;
251}
252
4e0958d1
HX
253static int cryptd_skcipher_setkey(struct crypto_skcipher *parent,
254 const u8 *key, unsigned int keylen)
255{
256 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(parent);
36b3875a 257 struct crypto_sync_skcipher *child = ctx->child;
4e0958d1
HX
258 int err;
259
36b3875a
KC
260 crypto_sync_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
261 crypto_sync_skcipher_set_flags(child,
262 crypto_skcipher_get_flags(parent) &
4e0958d1 263 CRYPTO_TFM_REQ_MASK);
36b3875a
KC
264 err = crypto_sync_skcipher_setkey(child, key, keylen);
265 crypto_skcipher_set_flags(parent,
266 crypto_sync_skcipher_get_flags(child) &
4e0958d1
HX
267 CRYPTO_TFM_RES_MASK);
268 return err;
269}
270
271static void cryptd_skcipher_complete(struct skcipher_request *req, int err)
272{
273 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
274 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
275 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
276 int refcnt = atomic_read(&ctx->refcnt);
277
278 local_bh_disable();
279 rctx->complete(&req->base, err);
280 local_bh_enable();
281
282 if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
283 crypto_free_skcipher(tfm);
284}
285
286static void cryptd_skcipher_encrypt(struct crypto_async_request *base,
287 int err)
288{
289 struct skcipher_request *req = skcipher_request_cast(base);
290 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
291 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
292 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
36b3875a
KC
293 struct crypto_sync_skcipher *child = ctx->child;
294 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, child);
4e0958d1
HX
295
296 if (unlikely(err == -EINPROGRESS))
297 goto out;
298
36b3875a 299 skcipher_request_set_sync_tfm(subreq, child);
4e0958d1
HX
300 skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
301 NULL, NULL);
302 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
303 req->iv);
304
305 err = crypto_skcipher_encrypt(subreq);
306 skcipher_request_zero(subreq);
307
308 req->base.complete = rctx->complete;
309
310out:
311 cryptd_skcipher_complete(req, err);
312}
313
314static void cryptd_skcipher_decrypt(struct crypto_async_request *base,
315 int err)
316{
317 struct skcipher_request *req = skcipher_request_cast(base);
318 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
319 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
320 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
36b3875a
KC
321 struct crypto_sync_skcipher *child = ctx->child;
322 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, child);
4e0958d1
HX
323
324 if (unlikely(err == -EINPROGRESS))
325 goto out;
326
36b3875a 327 skcipher_request_set_sync_tfm(subreq, child);
4e0958d1
HX
328 skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
329 NULL, NULL);
330 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
331 req->iv);
332
333 err = crypto_skcipher_decrypt(subreq);
334 skcipher_request_zero(subreq);
335
336 req->base.complete = rctx->complete;
337
338out:
339 cryptd_skcipher_complete(req, err);
340}
341
342static int cryptd_skcipher_enqueue(struct skcipher_request *req,
343 crypto_completion_t compl)
344{
345 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
346 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
347 struct cryptd_queue *queue;
348
349 queue = cryptd_get_queue(crypto_skcipher_tfm(tfm));
350 rctx->complete = req->base.complete;
351 req->base.complete = compl;
352
353 return cryptd_enqueue_request(queue, &req->base);
354}
355
356static int cryptd_skcipher_encrypt_enqueue(struct skcipher_request *req)
357{
358 return cryptd_skcipher_enqueue(req, cryptd_skcipher_encrypt);
359}
360
361static int cryptd_skcipher_decrypt_enqueue(struct skcipher_request *req)
362{
363 return cryptd_skcipher_enqueue(req, cryptd_skcipher_decrypt);
364}
365
366static int cryptd_skcipher_init_tfm(struct crypto_skcipher *tfm)
367{
368 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
369 struct skcipherd_instance_ctx *ictx = skcipher_instance_ctx(inst);
370 struct crypto_skcipher_spawn *spawn = &ictx->spawn;
371 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
372 struct crypto_skcipher *cipher;
373
374 cipher = crypto_spawn_skcipher(spawn);
375 if (IS_ERR(cipher))
376 return PTR_ERR(cipher);
377
36b3875a 378 ctx->child = (struct crypto_sync_skcipher *)cipher;
4e0958d1
HX
379 crypto_skcipher_set_reqsize(
380 tfm, sizeof(struct cryptd_skcipher_request_ctx));
381 return 0;
382}
383
384static void cryptd_skcipher_exit_tfm(struct crypto_skcipher *tfm)
385{
386 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
387
36b3875a 388 crypto_free_sync_skcipher(ctx->child);
4e0958d1
HX
389}
390
391static void cryptd_skcipher_free(struct skcipher_instance *inst)
392{
393 struct skcipherd_instance_ctx *ctx = skcipher_instance_ctx(inst);
394
395 crypto_drop_skcipher(&ctx->spawn);
396}
397
398static int cryptd_create_skcipher(struct crypto_template *tmpl,
399 struct rtattr **tb,
400 struct cryptd_queue *queue)
401{
402 struct skcipherd_instance_ctx *ctx;
403 struct skcipher_instance *inst;
404 struct skcipher_alg *alg;
405 const char *name;
406 u32 type;
407 u32 mask;
408 int err;
409
410 type = 0;
411 mask = CRYPTO_ALG_ASYNC;
412
413 cryptd_check_internal(tb, &type, &mask);
414
415 name = crypto_attr_alg_name(tb[1]);
416 if (IS_ERR(name))
417 return PTR_ERR(name);
418
419 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
420 if (!inst)
421 return -ENOMEM;
422
423 ctx = skcipher_instance_ctx(inst);
424 ctx->queue = queue;
425
426 crypto_set_skcipher_spawn(&ctx->spawn, skcipher_crypto_instance(inst));
427 err = crypto_grab_skcipher(&ctx->spawn, name, type, mask);
428 if (err)
429 goto out_free_inst;
430
431 alg = crypto_spawn_skcipher_alg(&ctx->spawn);
432 err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base);
433 if (err)
434 goto out_drop_skcipher;
435
436 inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
437 (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
438
439 inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg);
440 inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
441 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
442 inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
443
444 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_skcipher_ctx);
445
446 inst->alg.init = cryptd_skcipher_init_tfm;
447 inst->alg.exit = cryptd_skcipher_exit_tfm;
448
449 inst->alg.setkey = cryptd_skcipher_setkey;
450 inst->alg.encrypt = cryptd_skcipher_encrypt_enqueue;
451 inst->alg.decrypt = cryptd_skcipher_decrypt_enqueue;
452
453 inst->free = cryptd_skcipher_free;
454
455 err = skcipher_register_instance(tmpl, inst);
456 if (err) {
457out_drop_skcipher:
458 crypto_drop_skcipher(&ctx->spawn);
459out_free_inst:
460 kfree(inst);
461 }
462 return err;
463}
464
b8a28251
LH
465static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
466{
467 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
46309d89
HX
468 struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
469 struct crypto_shash_spawn *spawn = &ictx->spawn;
b8a28251 470 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
46309d89 471 struct crypto_shash *hash;
b8a28251 472
46309d89
HX
473 hash = crypto_spawn_shash(spawn);
474 if (IS_ERR(hash))
475 return PTR_ERR(hash);
b8a28251 476
46309d89 477 ctx->child = hash;
0d6669e2
HX
478 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
479 sizeof(struct cryptd_hash_request_ctx) +
480 crypto_shash_descsize(hash));
b8a28251
LH
481 return 0;
482}
483
484static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
485{
486 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
b8a28251 487
46309d89 488 crypto_free_shash(ctx->child);
b8a28251
LH
489}
490
491static int cryptd_hash_setkey(struct crypto_ahash *parent,
492 const u8 *key, unsigned int keylen)
493{
494 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
46309d89 495 struct crypto_shash *child = ctx->child;
b8a28251
LH
496 int err;
497
46309d89
HX
498 crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
499 crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
500 CRYPTO_TFM_REQ_MASK);
501 err = crypto_shash_setkey(child, key, keylen);
502 crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
503 CRYPTO_TFM_RES_MASK);
b8a28251
LH
504 return err;
505}
506
507static int cryptd_hash_enqueue(struct ahash_request *req,
3e3dc25f 508 crypto_completion_t compl)
b8a28251
LH
509{
510 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
511 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
254eff77
HY
512 struct cryptd_queue *queue =
513 cryptd_get_queue(crypto_ahash_tfm(tfm));
b8a28251
LH
514
515 rctx->complete = req->base.complete;
3e3dc25f 516 req->base.complete = compl;
b8a28251 517
254eff77 518 return cryptd_enqueue_request(queue, &req->base);
b8a28251
LH
519}
520
81760ea6
HX
521static void cryptd_hash_complete(struct ahash_request *req, int err)
522{
523 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
524 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
525 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
526 int refcnt = atomic_read(&ctx->refcnt);
527
528 local_bh_disable();
529 rctx->complete(&req->base, err);
530 local_bh_enable();
531
532 if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
533 crypto_free_ahash(tfm);
534}
535
b8a28251
LH
536static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
537{
46309d89
HX
538 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
539 struct crypto_shash *child = ctx->child;
540 struct ahash_request *req = ahash_request_cast(req_async);
541 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
542 struct shash_desc *desc = &rctx->desc;
b8a28251
LH
543
544 if (unlikely(err == -EINPROGRESS))
545 goto out;
546
46309d89 547 desc->tfm = child;
b8a28251 548
46309d89 549 err = crypto_shash_init(desc);
b8a28251
LH
550
551 req->base.complete = rctx->complete;
552
553out:
81760ea6 554 cryptd_hash_complete(req, err);
b8a28251
LH
555}
556
557static int cryptd_hash_init_enqueue(struct ahash_request *req)
558{
559 return cryptd_hash_enqueue(req, cryptd_hash_init);
560}
561
562static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
563{
46309d89 564 struct ahash_request *req = ahash_request_cast(req_async);
b8a28251 565 struct cryptd_hash_request_ctx *rctx;
b8a28251
LH
566
567 rctx = ahash_request_ctx(req);
568
569 if (unlikely(err == -EINPROGRESS))
570 goto out;
571
46309d89 572 err = shash_ahash_update(req, &rctx->desc);
b8a28251
LH
573
574 req->base.complete = rctx->complete;
575
576out:
81760ea6 577 cryptd_hash_complete(req, err);
b8a28251
LH
578}
579
580static int cryptd_hash_update_enqueue(struct ahash_request *req)
581{
582 return cryptd_hash_enqueue(req, cryptd_hash_update);
583}
584
585static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
586{
46309d89
HX
587 struct ahash_request *req = ahash_request_cast(req_async);
588 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
b8a28251
LH
589
590 if (unlikely(err == -EINPROGRESS))
591 goto out;
592
46309d89 593 err = crypto_shash_final(&rctx->desc, req->result);
b8a28251
LH
594
595 req->base.complete = rctx->complete;
596
597out:
81760ea6 598 cryptd_hash_complete(req, err);
b8a28251
LH
599}
600
601static int cryptd_hash_final_enqueue(struct ahash_request *req)
602{
603 return cryptd_hash_enqueue(req, cryptd_hash_final);
604}
605
6fba00d1
HX
606static void cryptd_hash_finup(struct crypto_async_request *req_async, int err)
607{
608 struct ahash_request *req = ahash_request_cast(req_async);
609 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
610
611 if (unlikely(err == -EINPROGRESS))
612 goto out;
613
614 err = shash_ahash_finup(req, &rctx->desc);
615
616 req->base.complete = rctx->complete;
617
618out:
81760ea6 619 cryptd_hash_complete(req, err);
6fba00d1
HX
620}
621
622static int cryptd_hash_finup_enqueue(struct ahash_request *req)
623{
624 return cryptd_hash_enqueue(req, cryptd_hash_finup);
625}
626
b8a28251
LH
627static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
628{
46309d89
HX
629 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
630 struct crypto_shash *child = ctx->child;
631 struct ahash_request *req = ahash_request_cast(req_async);
632 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
633 struct shash_desc *desc = &rctx->desc;
b8a28251
LH
634
635 if (unlikely(err == -EINPROGRESS))
636 goto out;
637
46309d89 638 desc->tfm = child;
b8a28251 639
46309d89 640 err = shash_ahash_digest(req, desc);
b8a28251
LH
641
642 req->base.complete = rctx->complete;
643
644out:
81760ea6 645 cryptd_hash_complete(req, err);
b8a28251
LH
646}
647
648static int cryptd_hash_digest_enqueue(struct ahash_request *req)
649{
650 return cryptd_hash_enqueue(req, cryptd_hash_digest);
651}
652
6fba00d1
HX
653static int cryptd_hash_export(struct ahash_request *req, void *out)
654{
655 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
656
657 return crypto_shash_export(&rctx->desc, out);
658}
659
660static int cryptd_hash_import(struct ahash_request *req, const void *in)
661{
0bd22235
AB
662 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
663 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
664 struct shash_desc *desc = cryptd_shash_desc(req);
665
666 desc->tfm = ctx->child;
6fba00d1 667
0bd22235 668 return crypto_shash_import(desc, in);
6fba00d1
HX
669}
670
9cd899a3
HX
671static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
672 struct cryptd_queue *queue)
b8a28251 673{
46309d89 674 struct hashd_instance_ctx *ctx;
0b535adf 675 struct ahash_instance *inst;
46309d89 676 struct shash_alg *salg;
b8a28251 677 struct crypto_alg *alg;
466a7b9e
SM
678 u32 type = 0;
679 u32 mask = 0;
46309d89 680 int err;
b8a28251 681
466a7b9e
SM
682 cryptd_check_internal(tb, &type, &mask);
683
684 salg = shash_attr_alg(tb[1], type, mask);
46309d89 685 if (IS_ERR(salg))
9cd899a3 686 return PTR_ERR(salg);
b8a28251 687
46309d89 688 alg = &salg->base;
0b535adf
HX
689 inst = cryptd_alloc_instance(alg, ahash_instance_headroom(),
690 sizeof(*ctx));
05ed8758 691 err = PTR_ERR(inst);
b8a28251
LH
692 if (IS_ERR(inst))
693 goto out_put_alg;
694
0b535adf 695 ctx = ahash_instance_ctx(inst);
46309d89
HX
696 ctx->queue = queue;
697
0b535adf
HX
698 err = crypto_init_shash_spawn(&ctx->spawn, salg,
699 ahash_crypto_instance(inst));
46309d89
HX
700 if (err)
701 goto out_free_inst;
702
a208fa8f
EB
703 inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC |
704 (alg->cra_flags & (CRYPTO_ALG_INTERNAL |
705 CRYPTO_ALG_OPTIONAL_KEY));
b8a28251 706
0b535adf 707 inst->alg.halg.digestsize = salg->digestsize;
1a078340 708 inst->alg.halg.statesize = salg->statesize;
0b535adf 709 inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
b8a28251 710
0b535adf
HX
711 inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
712 inst->alg.halg.base.cra_exit = cryptd_hash_exit_tfm;
b8a28251 713
0b535adf
HX
714 inst->alg.init = cryptd_hash_init_enqueue;
715 inst->alg.update = cryptd_hash_update_enqueue;
716 inst->alg.final = cryptd_hash_final_enqueue;
6fba00d1
HX
717 inst->alg.finup = cryptd_hash_finup_enqueue;
718 inst->alg.export = cryptd_hash_export;
719 inst->alg.import = cryptd_hash_import;
841a3ff3
EB
720 if (crypto_shash_alg_has_setkey(salg))
721 inst->alg.setkey = cryptd_hash_setkey;
0b535adf 722 inst->alg.digest = cryptd_hash_digest_enqueue;
b8a28251 723
0b535adf 724 err = ahash_register_instance(tmpl, inst);
9cd899a3
HX
725 if (err) {
726 crypto_drop_shash(&ctx->spawn);
727out_free_inst:
728 kfree(inst);
729 }
730
b8a28251
LH
731out_put_alg:
732 crypto_mod_put(alg);
9cd899a3 733 return err;
b8a28251
LH
734}
735
92b9876b
HX
736static int cryptd_aead_setkey(struct crypto_aead *parent,
737 const u8 *key, unsigned int keylen)
738{
739 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
740 struct crypto_aead *child = ctx->child;
741
742 return crypto_aead_setkey(child, key, keylen);
743}
744
745static int cryptd_aead_setauthsize(struct crypto_aead *parent,
746 unsigned int authsize)
747{
748 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
749 struct crypto_aead *child = ctx->child;
750
751 return crypto_aead_setauthsize(child, authsize);
752}
753
298c926c
AH
754static void cryptd_aead_crypt(struct aead_request *req,
755 struct crypto_aead *child,
756 int err,
757 int (*crypt)(struct aead_request *req))
758{
759 struct cryptd_aead_request_ctx *rctx;
81760ea6 760 struct cryptd_aead_ctx *ctx;
ec9f2006 761 crypto_completion_t compl;
81760ea6
HX
762 struct crypto_aead *tfm;
763 int refcnt;
ec9f2006 764
298c926c 765 rctx = aead_request_ctx(req);
ec9f2006 766 compl = rctx->complete;
298c926c 767
31bd44e7
HX
768 tfm = crypto_aead_reqtfm(req);
769
298c926c
AH
770 if (unlikely(err == -EINPROGRESS))
771 goto out;
772 aead_request_set_tfm(req, child);
773 err = crypt( req );
81760ea6 774
298c926c 775out:
81760ea6
HX
776 ctx = crypto_aead_ctx(tfm);
777 refcnt = atomic_read(&ctx->refcnt);
778
298c926c 779 local_bh_disable();
ec9f2006 780 compl(&req->base, err);
298c926c 781 local_bh_enable();
81760ea6
HX
782
783 if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
784 crypto_free_aead(tfm);
298c926c
AH
785}
786
787static void cryptd_aead_encrypt(struct crypto_async_request *areq, int err)
788{
789 struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
790 struct crypto_aead *child = ctx->child;
791 struct aead_request *req;
792
793 req = container_of(areq, struct aead_request, base);
ba3749a7 794 cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->encrypt);
298c926c
AH
795}
796
797static void cryptd_aead_decrypt(struct crypto_async_request *areq, int err)
798{
799 struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
800 struct crypto_aead *child = ctx->child;
801 struct aead_request *req;
802
803 req = container_of(areq, struct aead_request, base);
ba3749a7 804 cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->decrypt);
298c926c
AH
805}
806
807static int cryptd_aead_enqueue(struct aead_request *req,
3e3dc25f 808 crypto_completion_t compl)
298c926c
AH
809{
810 struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req);
811 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
812 struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm));
813
814 rctx->complete = req->base.complete;
3e3dc25f 815 req->base.complete = compl;
298c926c
AH
816 return cryptd_enqueue_request(queue, &req->base);
817}
818
819static int cryptd_aead_encrypt_enqueue(struct aead_request *req)
820{
821 return cryptd_aead_enqueue(req, cryptd_aead_encrypt );
822}
823
824static int cryptd_aead_decrypt_enqueue(struct aead_request *req)
825{
826 return cryptd_aead_enqueue(req, cryptd_aead_decrypt );
827}
828
f614e546 829static int cryptd_aead_init_tfm(struct crypto_aead *tfm)
298c926c 830{
f614e546
HX
831 struct aead_instance *inst = aead_alg_instance(tfm);
832 struct aead_instance_ctx *ictx = aead_instance_ctx(inst);
298c926c 833 struct crypto_aead_spawn *spawn = &ictx->aead_spawn;
f614e546 834 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
298c926c
AH
835 struct crypto_aead *cipher;
836
837 cipher = crypto_spawn_aead(spawn);
838 if (IS_ERR(cipher))
839 return PTR_ERR(cipher);
840
298c926c 841 ctx->child = cipher;
ec9f2006
HX
842 crypto_aead_set_reqsize(
843 tfm, max((unsigned)sizeof(struct cryptd_aead_request_ctx),
844 crypto_aead_reqsize(cipher)));
298c926c
AH
845 return 0;
846}
847
f614e546 848static void cryptd_aead_exit_tfm(struct crypto_aead *tfm)
298c926c 849{
f614e546 850 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
298c926c
AH
851 crypto_free_aead(ctx->child);
852}
853
854static int cryptd_create_aead(struct crypto_template *tmpl,
855 struct rtattr **tb,
856 struct cryptd_queue *queue)
857{
858 struct aead_instance_ctx *ctx;
f614e546
HX
859 struct aead_instance *inst;
860 struct aead_alg *alg;
9b8c456e
HX
861 const char *name;
862 u32 type = 0;
ec9f2006 863 u32 mask = CRYPTO_ALG_ASYNC;
298c926c
AH
864 int err;
865
466a7b9e
SM
866 cryptd_check_internal(tb, &type, &mask);
867
9b8c456e
HX
868 name = crypto_attr_alg_name(tb[1]);
869 if (IS_ERR(name))
870 return PTR_ERR(name);
298c926c 871
9b8c456e
HX
872 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
873 if (!inst)
874 return -ENOMEM;
298c926c 875
f614e546 876 ctx = aead_instance_ctx(inst);
298c926c
AH
877 ctx->queue = queue;
878
f614e546 879 crypto_set_aead_spawn(&ctx->aead_spawn, aead_crypto_instance(inst));
9b8c456e 880 err = crypto_grab_aead(&ctx->aead_spawn, name, type, mask);
298c926c
AH
881 if (err)
882 goto out_free_inst;
883
f614e546
HX
884 alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
885 err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
9b8c456e
HX
886 if (err)
887 goto out_drop_aead;
888
f614e546 889 inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
5e4b8c1f 890 (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
f614e546 891 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx);
298c926c 892
f614e546
HX
893 inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
894 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
895
896 inst->alg.init = cryptd_aead_init_tfm;
897 inst->alg.exit = cryptd_aead_exit_tfm;
898 inst->alg.setkey = cryptd_aead_setkey;
899 inst->alg.setauthsize = cryptd_aead_setauthsize;
900 inst->alg.encrypt = cryptd_aead_encrypt_enqueue;
901 inst->alg.decrypt = cryptd_aead_decrypt_enqueue;
902
903 err = aead_register_instance(tmpl, inst);
298c926c 904 if (err) {
9b8c456e
HX
905out_drop_aead:
906 crypto_drop_aead(&ctx->aead_spawn);
298c926c
AH
907out_free_inst:
908 kfree(inst);
909 }
298c926c
AH
910 return err;
911}
912
254eff77 913static struct cryptd_queue queue;
124b53d0 914
9cd899a3 915static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
124b53d0
HX
916{
917 struct crypto_attr_type *algt;
918
919 algt = crypto_get_attr_type(tb);
920 if (IS_ERR(algt))
9cd899a3 921 return PTR_ERR(algt);
124b53d0
HX
922
923 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
924 case CRYPTO_ALG_TYPE_BLKCIPHER:
4e0958d1 925 return cryptd_create_skcipher(tmpl, tb, &queue);
b8a28251 926 case CRYPTO_ALG_TYPE_DIGEST:
9cd899a3 927 return cryptd_create_hash(tmpl, tb, &queue);
298c926c
AH
928 case CRYPTO_ALG_TYPE_AEAD:
929 return cryptd_create_aead(tmpl, tb, &queue);
124b53d0
HX
930 }
931
9cd899a3 932 return -EINVAL;
124b53d0
HX
933}
934
935static void cryptd_free(struct crypto_instance *inst)
936{
937 struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
0b535adf 938 struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst);
298c926c 939 struct aead_instance_ctx *aead_ctx = crypto_instance_ctx(inst);
0b535adf
HX
940
941 switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) {
942 case CRYPTO_ALG_TYPE_AHASH:
943 crypto_drop_shash(&hctx->spawn);
944 kfree(ahash_instance(inst));
945 return;
298c926c 946 case CRYPTO_ALG_TYPE_AEAD:
f614e546
HX
947 crypto_drop_aead(&aead_ctx->aead_spawn);
948 kfree(aead_instance(inst));
298c926c
AH
949 return;
950 default:
951 crypto_drop_spawn(&ctx->spawn);
952 kfree(inst);
0b535adf 953 }
124b53d0
HX
954}
955
956static struct crypto_template cryptd_tmpl = {
957 .name = "cryptd",
9cd899a3 958 .create = cryptd_create,
124b53d0
HX
959 .free = cryptd_free,
960 .module = THIS_MODULE,
961};
962
4e0958d1
HX
963struct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name,
964 u32 type, u32 mask)
965{
966 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
967 struct cryptd_skcipher_ctx *ctx;
968 struct crypto_skcipher *tfm;
969
970 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
971 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
972 return ERR_PTR(-EINVAL);
973
974 tfm = crypto_alloc_skcipher(cryptd_alg_name, type, mask);
975 if (IS_ERR(tfm))
976 return ERR_CAST(tfm);
977
978 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
979 crypto_free_skcipher(tfm);
980 return ERR_PTR(-EINVAL);
981 }
982
983 ctx = crypto_skcipher_ctx(tfm);
984 atomic_set(&ctx->refcnt, 1);
985
986 return container_of(tfm, struct cryptd_skcipher, base);
987}
988EXPORT_SYMBOL_GPL(cryptd_alloc_skcipher);
989
990struct crypto_skcipher *cryptd_skcipher_child(struct cryptd_skcipher *tfm)
991{
992 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
993
36b3875a 994 return &ctx->child->base;
4e0958d1
HX
995}
996EXPORT_SYMBOL_GPL(cryptd_skcipher_child);
997
998bool cryptd_skcipher_queued(struct cryptd_skcipher *tfm)
999{
1000 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
1001
1002 return atomic_read(&ctx->refcnt) - 1;
1003}
1004EXPORT_SYMBOL_GPL(cryptd_skcipher_queued);
1005
1006void cryptd_free_skcipher(struct cryptd_skcipher *tfm)
1007{
1008 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
1009
1010 if (atomic_dec_and_test(&ctx->refcnt))
1011 crypto_free_skcipher(&tfm->base);
1012}
1013EXPORT_SYMBOL_GPL(cryptd_free_skcipher);
1014
ace13663
HY
1015struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name,
1016 u32 type, u32 mask)
1017{
1018 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
81760ea6 1019 struct cryptd_hash_ctx *ctx;
ace13663
HY
1020 struct crypto_ahash *tfm;
1021
1022 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
1023 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
1024 return ERR_PTR(-EINVAL);
1025 tfm = crypto_alloc_ahash(cryptd_alg_name, type, mask);
1026 if (IS_ERR(tfm))
1027 return ERR_CAST(tfm);
1028 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
1029 crypto_free_ahash(tfm);
1030 return ERR_PTR(-EINVAL);
1031 }
1032
81760ea6
HX
1033 ctx = crypto_ahash_ctx(tfm);
1034 atomic_set(&ctx->refcnt, 1);
1035
ace13663
HY
1036 return __cryptd_ahash_cast(tfm);
1037}
1038EXPORT_SYMBOL_GPL(cryptd_alloc_ahash);
1039
1040struct crypto_shash *cryptd_ahash_child(struct cryptd_ahash *tfm)
1041{
1042 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
1043
1044 return ctx->child;
1045}
1046EXPORT_SYMBOL_GPL(cryptd_ahash_child);
1047
0e1227d3
HY
1048struct shash_desc *cryptd_shash_desc(struct ahash_request *req)
1049{
1050 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
1051 return &rctx->desc;
1052}
1053EXPORT_SYMBOL_GPL(cryptd_shash_desc);
1054
81760ea6
HX
1055bool cryptd_ahash_queued(struct cryptd_ahash *tfm)
1056{
1057 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
1058
1059 return atomic_read(&ctx->refcnt) - 1;
1060}
1061EXPORT_SYMBOL_GPL(cryptd_ahash_queued);
1062
ace13663
HY
1063void cryptd_free_ahash(struct cryptd_ahash *tfm)
1064{
81760ea6
HX
1065 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
1066
1067 if (atomic_dec_and_test(&ctx->refcnt))
1068 crypto_free_ahash(&tfm->base);
ace13663
HY
1069}
1070EXPORT_SYMBOL_GPL(cryptd_free_ahash);
1071
298c926c
AH
1072struct cryptd_aead *cryptd_alloc_aead(const char *alg_name,
1073 u32 type, u32 mask)
1074{
1075 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
81760ea6 1076 struct cryptd_aead_ctx *ctx;
298c926c
AH
1077 struct crypto_aead *tfm;
1078
1079 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
1080 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
1081 return ERR_PTR(-EINVAL);
1082 tfm = crypto_alloc_aead(cryptd_alg_name, type, mask);
1083 if (IS_ERR(tfm))
1084 return ERR_CAST(tfm);
1085 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
1086 crypto_free_aead(tfm);
1087 return ERR_PTR(-EINVAL);
1088 }
81760ea6
HX
1089
1090 ctx = crypto_aead_ctx(tfm);
1091 atomic_set(&ctx->refcnt, 1);
1092
298c926c
AH
1093 return __cryptd_aead_cast(tfm);
1094}
1095EXPORT_SYMBOL_GPL(cryptd_alloc_aead);
1096
1097struct crypto_aead *cryptd_aead_child(struct cryptd_aead *tfm)
1098{
1099 struct cryptd_aead_ctx *ctx;
1100 ctx = crypto_aead_ctx(&tfm->base);
1101 return ctx->child;
1102}
1103EXPORT_SYMBOL_GPL(cryptd_aead_child);
1104
81760ea6
HX
1105bool cryptd_aead_queued(struct cryptd_aead *tfm)
1106{
1107 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
1108
1109 return atomic_read(&ctx->refcnt) - 1;
1110}
1111EXPORT_SYMBOL_GPL(cryptd_aead_queued);
1112
298c926c
AH
1113void cryptd_free_aead(struct cryptd_aead *tfm)
1114{
81760ea6
HX
1115 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
1116
1117 if (atomic_dec_and_test(&ctx->refcnt))
1118 crypto_free_aead(&tfm->base);
298c926c
AH
1119}
1120EXPORT_SYMBOL_GPL(cryptd_free_aead);
1121
124b53d0
HX
1122static int __init cryptd_init(void)
1123{
1124 int err;
1125
c3a53605 1126 err = cryptd_init_queue(&queue, cryptd_max_cpu_qlen);
124b53d0
HX
1127 if (err)
1128 return err;
1129
1130 err = crypto_register_template(&cryptd_tmpl);
1131 if (err)
254eff77 1132 cryptd_fini_queue(&queue);
124b53d0
HX
1133
1134 return err;
1135}
1136
1137static void __exit cryptd_exit(void)
1138{
254eff77 1139 cryptd_fini_queue(&queue);
124b53d0
HX
1140 crypto_unregister_template(&cryptd_tmpl);
1141}
1142
b2bac6ac 1143subsys_initcall(cryptd_init);
124b53d0
HX
1144module_exit(cryptd_exit);
1145
1146MODULE_LICENSE("GPL");
1147MODULE_DESCRIPTION("Software async crypto daemon");
4943ba16 1148MODULE_ALIAS_CRYPTO("cryptd");