blob: 3fd2a20a8145726094bdc6ac11c3c2dc84ecb324 [file] [log] [blame]
Herbert Xu124b53d2007-04-16 20:49:20 +10001/*
2 * Software async crypto daemon.
3 *
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
5 *
Adrian Hoban298c9262010-09-20 16:05:12 +08006 * 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 *
Herbert Xu124b53d2007-04-16 20:49:20 +100013 * 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
20#include <crypto/algapi.h>
Herbert Xu18e33e62008-07-10 16:01:22 +080021#include <crypto/internal/hash.h>
Adrian Hoban298c9262010-09-20 16:05:12 +080022#include <crypto/internal/aead.h>
Huang Ying1cac2cb2009-01-18 16:19:46 +110023#include <crypto/cryptd.h>
Huang Ying254eff72009-02-19 14:42:19 +080024#include <crypto/crypto_wq.h>
Herbert Xu81760ea2016-06-21 16:55:13 +080025#include <linux/atomic.h>
Herbert Xu124b53d2007-04-16 20:49:20 +100026#include <linux/err.h>
27#include <linux/init.h>
28#include <linux/kernel.h>
Herbert Xu124b53d2007-04-16 20:49:20 +100029#include <linux/list.h>
30#include <linux/module.h>
Herbert Xu124b53d2007-04-16 20:49:20 +100031#include <linux/scatterlist.h>
32#include <linux/sched.h>
33#include <linux/slab.h>
Herbert Xu124b53d2007-04-16 20:49:20 +100034
Herbert Xu81760ea2016-06-21 16:55:13 +080035#define CRYPTD_MAX_CPU_QLEN 1000
Herbert Xu124b53d2007-04-16 20:49:20 +100036
Huang Ying254eff72009-02-19 14:42:19 +080037struct cryptd_cpu_queue {
Herbert Xu124b53d2007-04-16 20:49:20 +100038 struct crypto_queue queue;
Huang Ying254eff72009-02-19 14:42:19 +080039 struct work_struct work;
40};
41
42struct cryptd_queue {
Tejun Heoa29d8b82010-02-02 14:39:15 +090043 struct cryptd_cpu_queue __percpu *cpu_queue;
Herbert Xu124b53d2007-04-16 20:49:20 +100044};
45
46struct cryptd_instance_ctx {
47 struct crypto_spawn spawn;
Huang Ying254eff72009-02-19 14:42:19 +080048 struct cryptd_queue *queue;
Herbert Xu124b53d2007-04-16 20:49:20 +100049};
50
Herbert Xu46309d82009-07-12 21:38:59 +080051struct hashd_instance_ctx {
52 struct crypto_shash_spawn spawn;
53 struct cryptd_queue *queue;
54};
55
Adrian Hoban298c9262010-09-20 16:05:12 +080056struct aead_instance_ctx {
57 struct crypto_aead_spawn aead_spawn;
58 struct cryptd_queue *queue;
59};
60
Herbert Xu124b53d2007-04-16 20:49:20 +100061struct cryptd_blkcipher_ctx {
Herbert Xu81760ea2016-06-21 16:55:13 +080062 atomic_t refcnt;
Herbert Xu124b53d2007-04-16 20:49:20 +100063 struct crypto_blkcipher *child;
64};
65
66struct cryptd_blkcipher_request_ctx {
67 crypto_completion_t complete;
68};
69
Loc Hob8a28252008-05-14 21:23:00 +080070struct cryptd_hash_ctx {
Herbert Xu81760ea2016-06-21 16:55:13 +080071 atomic_t refcnt;
Herbert Xu46309d82009-07-12 21:38:59 +080072 struct crypto_shash *child;
Loc Hob8a28252008-05-14 21:23:00 +080073};
74
75struct cryptd_hash_request_ctx {
76 crypto_completion_t complete;
Herbert Xu46309d82009-07-12 21:38:59 +080077 struct shash_desc desc;
Loc Hob8a28252008-05-14 21:23:00 +080078};
Herbert Xu124b53d2007-04-16 20:49:20 +100079
Adrian Hoban298c9262010-09-20 16:05:12 +080080struct cryptd_aead_ctx {
Herbert Xu81760ea2016-06-21 16:55:13 +080081 atomic_t refcnt;
Adrian Hoban298c9262010-09-20 16:05:12 +080082 struct crypto_aead *child;
83};
84
85struct cryptd_aead_request_ctx {
86 crypto_completion_t complete;
87};
88
Huang Ying254eff72009-02-19 14:42:19 +080089static void cryptd_queue_worker(struct work_struct *work);
90
91static int cryptd_init_queue(struct cryptd_queue *queue,
92 unsigned int max_cpu_qlen)
93{
94 int cpu;
95 struct cryptd_cpu_queue *cpu_queue;
96
97 queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue);
98 if (!queue->cpu_queue)
99 return -ENOMEM;
100 for_each_possible_cpu(cpu) {
101 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
102 crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
103 INIT_WORK(&cpu_queue->work, cryptd_queue_worker);
104 }
105 return 0;
106}
107
108static void cryptd_fini_queue(struct cryptd_queue *queue)
109{
110 int cpu;
111 struct cryptd_cpu_queue *cpu_queue;
112
113 for_each_possible_cpu(cpu) {
114 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
115 BUG_ON(cpu_queue->queue.qlen);
116 }
117 free_percpu(queue->cpu_queue);
118}
119
120static int cryptd_enqueue_request(struct cryptd_queue *queue,
121 struct crypto_async_request *request)
122{
123 int cpu, err;
124 struct cryptd_cpu_queue *cpu_queue;
Herbert Xu81760ea2016-06-21 16:55:13 +0800125 atomic_t *refcnt;
126 bool may_backlog;
Huang Ying254eff72009-02-19 14:42:19 +0800127
128 cpu = get_cpu();
Christoph Lameter0b44f482009-10-03 19:48:23 +0900129 cpu_queue = this_cpu_ptr(queue->cpu_queue);
Huang Ying254eff72009-02-19 14:42:19 +0800130 err = crypto_enqueue_request(&cpu_queue->queue, request);
Herbert Xu81760ea2016-06-21 16:55:13 +0800131
132 refcnt = crypto_tfm_ctx(request->tfm);
133 may_backlog = request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG;
134
135 if (err == -EBUSY && !may_backlog)
136 goto out_put_cpu;
137
Huang Ying254eff72009-02-19 14:42:19 +0800138 queue_work_on(cpu, kcrypto_wq, &cpu_queue->work);
Herbert Xu81760ea2016-06-21 16:55:13 +0800139
140 if (!atomic_read(refcnt))
141 goto out_put_cpu;
142
Herbert Xu81760ea2016-06-21 16:55:13 +0800143 atomic_inc(refcnt);
144
145out_put_cpu:
Huang Ying254eff72009-02-19 14:42:19 +0800146 put_cpu();
147
148 return err;
149}
150
151/* Called in workqueue context, do one real cryption work (via
152 * req->complete) and reschedule itself if there are more work to
153 * do. */
154static void cryptd_queue_worker(struct work_struct *work)
155{
156 struct cryptd_cpu_queue *cpu_queue;
157 struct crypto_async_request *req, *backlog;
158
159 cpu_queue = container_of(work, struct cryptd_cpu_queue, work);
Jussi Kivilinna9efade12012-10-21 20:42:28 +0300160 /*
161 * Only handle one request at a time to avoid hogging crypto workqueue.
162 * preempt_disable/enable is used to prevent being preempted by
163 * cryptd_enqueue_request(). local_bh_disable/enable is used to prevent
164 * cryptd_enqueue_request() being accessed from software interrupts.
165 */
166 local_bh_disable();
Huang Ying254eff72009-02-19 14:42:19 +0800167 preempt_disable();
168 backlog = crypto_get_backlog(&cpu_queue->queue);
169 req = crypto_dequeue_request(&cpu_queue->queue);
170 preempt_enable();
Jussi Kivilinna9efade12012-10-21 20:42:28 +0300171 local_bh_enable();
Huang Ying254eff72009-02-19 14:42:19 +0800172
173 if (!req)
174 return;
175
176 if (backlog)
177 backlog->complete(backlog, -EINPROGRESS);
178 req->complete(req, 0);
179
180 if (cpu_queue->queue.qlen)
181 queue_work(kcrypto_wq, &cpu_queue->work);
182}
183
184static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
Herbert Xu124b53d2007-04-16 20:49:20 +1000185{
186 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
187 struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
Huang Ying254eff72009-02-19 14:42:19 +0800188 return ictx->queue;
Herbert Xu124b53d2007-04-16 20:49:20 +1000189}
190
Stephan Mueller466a7b92015-03-30 21:57:06 +0200191static inline void cryptd_check_internal(struct rtattr **tb, u32 *type,
192 u32 *mask)
193{
194 struct crypto_attr_type *algt;
195
196 algt = crypto_get_attr_type(tb);
197 if (IS_ERR(algt))
198 return;
Herbert Xuf6da3202015-07-09 07:17:19 +0800199
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800200 *type |= algt->type & CRYPTO_ALG_INTERNAL;
201 *mask |= algt->mask & CRYPTO_ALG_INTERNAL;
Stephan Mueller466a7b92015-03-30 21:57:06 +0200202}
203
Herbert Xu124b53d2007-04-16 20:49:20 +1000204static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent,
205 const u8 *key, unsigned int keylen)
206{
207 struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent);
208 struct crypto_blkcipher *child = ctx->child;
209 int err;
210
211 crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
212 crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
213 CRYPTO_TFM_REQ_MASK);
214 err = crypto_blkcipher_setkey(child, key, keylen);
215 crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) &
216 CRYPTO_TFM_RES_MASK);
217 return err;
218}
219
220static void cryptd_blkcipher_crypt(struct ablkcipher_request *req,
221 struct crypto_blkcipher *child,
222 int err,
223 int (*crypt)(struct blkcipher_desc *desc,
224 struct scatterlist *dst,
225 struct scatterlist *src,
226 unsigned int len))
227{
228 struct cryptd_blkcipher_request_ctx *rctx;
Herbert Xu81760ea2016-06-21 16:55:13 +0800229 struct cryptd_blkcipher_ctx *ctx;
230 struct crypto_ablkcipher *tfm;
Herbert Xu124b53d2007-04-16 20:49:20 +1000231 struct blkcipher_desc desc;
Herbert Xu81760ea2016-06-21 16:55:13 +0800232 int refcnt;
Herbert Xu124b53d2007-04-16 20:49:20 +1000233
234 rctx = ablkcipher_request_ctx(req);
235
Herbert Xu93aa7f82008-05-07 21:10:13 +0800236 if (unlikely(err == -EINPROGRESS))
237 goto out;
Herbert Xu124b53d2007-04-16 20:49:20 +1000238
239 desc.tfm = child;
240 desc.info = req->info;
241 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
242
243 err = crypt(&desc, req->dst, req->src, req->nbytes);
244
245 req->base.complete = rctx->complete;
246
Herbert Xu93aa7f82008-05-07 21:10:13 +0800247out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800248 tfm = crypto_ablkcipher_reqtfm(req);
249 ctx = crypto_ablkcipher_ctx(tfm);
250 refcnt = atomic_read(&ctx->refcnt);
251
Herbert Xu124b53d2007-04-16 20:49:20 +1000252 local_bh_disable();
Herbert Xu93aa7f82008-05-07 21:10:13 +0800253 rctx->complete(&req->base, err);
Herbert Xu124b53d2007-04-16 20:49:20 +1000254 local_bh_enable();
Herbert Xu81760ea2016-06-21 16:55:13 +0800255
256 if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
257 crypto_free_ablkcipher(tfm);
Herbert Xu124b53d2007-04-16 20:49:20 +1000258}
259
260static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err)
261{
262 struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
263 struct crypto_blkcipher *child = ctx->child;
264
265 cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
266 crypto_blkcipher_crt(child)->encrypt);
267}
268
269static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err)
270{
271 struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm);
272 struct crypto_blkcipher *child = ctx->child;
273
274 cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err,
275 crypto_blkcipher_crt(child)->decrypt);
276}
277
278static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700279 crypto_completion_t compl)
Herbert Xu124b53d2007-04-16 20:49:20 +1000280{
281 struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req);
282 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
Huang Ying254eff72009-02-19 14:42:19 +0800283 struct cryptd_queue *queue;
Herbert Xu124b53d2007-04-16 20:49:20 +1000284
Huang Ying254eff72009-02-19 14:42:19 +0800285 queue = cryptd_get_queue(crypto_ablkcipher_tfm(tfm));
Herbert Xu124b53d2007-04-16 20:49:20 +1000286 rctx->complete = req->base.complete;
Mark Rustad3e3dc252014-07-25 02:53:38 -0700287 req->base.complete = compl;
Herbert Xu124b53d2007-04-16 20:49:20 +1000288
Huang Ying254eff72009-02-19 14:42:19 +0800289 return cryptd_enqueue_request(queue, &req->base);
Herbert Xu124b53d2007-04-16 20:49:20 +1000290}
291
292static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req)
293{
294 return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt);
295}
296
297static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req)
298{
299 return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt);
300}
301
302static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm)
303{
304 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
305 struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
306 struct crypto_spawn *spawn = &ictx->spawn;
307 struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
308 struct crypto_blkcipher *cipher;
309
310 cipher = crypto_spawn_blkcipher(spawn);
311 if (IS_ERR(cipher))
312 return PTR_ERR(cipher);
313
314 ctx->child = cipher;
315 tfm->crt_ablkcipher.reqsize =
316 sizeof(struct cryptd_blkcipher_request_ctx);
317 return 0;
318}
319
320static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm)
321{
322 struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu124b53d2007-04-16 20:49:20 +1000323
324 crypto_free_blkcipher(ctx->child);
325}
326
Herbert Xu9b8c4562015-05-21 15:10:57 +0800327static int cryptd_init_instance(struct crypto_instance *inst,
328 struct crypto_alg *alg)
329{
330 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
331 "cryptd(%s)",
332 alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
333 return -ENAMETOOLONG;
334
335 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
336
337 inst->alg.cra_priority = alg->cra_priority + 50;
338 inst->alg.cra_blocksize = alg->cra_blocksize;
339 inst->alg.cra_alignmask = alg->cra_alignmask;
340
341 return 0;
342}
343
Herbert Xu0b535ad2009-07-14 19:11:32 +0800344static void *cryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
345 unsigned int tail)
Herbert Xu124b53d2007-04-16 20:49:20 +1000346{
Herbert Xu0b535ad2009-07-14 19:11:32 +0800347 char *p;
Herbert Xu124b53d2007-04-16 20:49:20 +1000348 struct crypto_instance *inst;
Herbert Xu124b53d2007-04-16 20:49:20 +1000349 int err;
350
Herbert Xu0b535ad2009-07-14 19:11:32 +0800351 p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL);
352 if (!p)
353 return ERR_PTR(-ENOMEM);
354
355 inst = (void *)(p + head);
Herbert Xu124b53d2007-04-16 20:49:20 +1000356
Herbert Xu9b8c4562015-05-21 15:10:57 +0800357 err = cryptd_init_instance(inst, alg);
358 if (err)
Herbert Xu124b53d2007-04-16 20:49:20 +1000359 goto out_free_inst;
360
Herbert Xu124b53d2007-04-16 20:49:20 +1000361out:
Herbert Xu0b535ad2009-07-14 19:11:32 +0800362 return p;
Herbert Xu124b53d2007-04-16 20:49:20 +1000363
364out_free_inst:
Herbert Xu0b535ad2009-07-14 19:11:32 +0800365 kfree(p);
366 p = ERR_PTR(err);
Herbert Xu124b53d2007-04-16 20:49:20 +1000367 goto out;
368}
369
Herbert Xu9cd899a2009-07-14 18:45:45 +0800370static int cryptd_create_blkcipher(struct crypto_template *tmpl,
371 struct rtattr **tb,
372 struct cryptd_queue *queue)
Herbert Xu124b53d2007-04-16 20:49:20 +1000373{
Herbert Xu46309d82009-07-12 21:38:59 +0800374 struct cryptd_instance_ctx *ctx;
Herbert Xu124b53d2007-04-16 20:49:20 +1000375 struct crypto_instance *inst;
376 struct crypto_alg *alg;
Stephan Mueller466a7b92015-03-30 21:57:06 +0200377 u32 type = CRYPTO_ALG_TYPE_BLKCIPHER;
378 u32 mask = CRYPTO_ALG_TYPE_MASK;
Herbert Xu46309d82009-07-12 21:38:59 +0800379 int err;
Herbert Xu124b53d2007-04-16 20:49:20 +1000380
Stephan Mueller466a7b92015-03-30 21:57:06 +0200381 cryptd_check_internal(tb, &type, &mask);
382
383 alg = crypto_get_attr_alg(tb, type, mask);
Herbert Xu124b53d2007-04-16 20:49:20 +1000384 if (IS_ERR(alg))
Herbert Xu9cd899a2009-07-14 18:45:45 +0800385 return PTR_ERR(alg);
Herbert Xu124b53d2007-04-16 20:49:20 +1000386
Herbert Xu0b535ad2009-07-14 19:11:32 +0800387 inst = cryptd_alloc_instance(alg, 0, sizeof(*ctx));
Steffen Klassert05ed8752009-07-15 16:51:04 +0800388 err = PTR_ERR(inst);
Herbert Xu124b53d2007-04-16 20:49:20 +1000389 if (IS_ERR(inst))
390 goto out_put_alg;
391
Herbert Xu46309d82009-07-12 21:38:59 +0800392 ctx = crypto_instance_ctx(inst);
393 ctx->queue = queue;
394
395 err = crypto_init_spawn(&ctx->spawn, alg, inst,
396 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
397 if (err)
398 goto out_free_inst;
399
Stephan Mueller466a7b92015-03-30 21:57:06 +0200400 type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
401 if (alg->cra_flags & CRYPTO_ALG_INTERNAL)
402 type |= CRYPTO_ALG_INTERNAL;
403 inst->alg.cra_flags = type;
Herbert Xu124b53d2007-04-16 20:49:20 +1000404 inst->alg.cra_type = &crypto_ablkcipher_type;
405
406 inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize;
407 inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
408 inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
409
Herbert Xu927eead2007-11-27 21:15:31 +0800410 inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv;
411
Herbert Xu124b53d2007-04-16 20:49:20 +1000412 inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx);
413
414 inst->alg.cra_init = cryptd_blkcipher_init_tfm;
415 inst->alg.cra_exit = cryptd_blkcipher_exit_tfm;
416
417 inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey;
418 inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue;
419 inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue;
420
Herbert Xu9cd899a2009-07-14 18:45:45 +0800421 err = crypto_register_instance(tmpl, inst);
422 if (err) {
423 crypto_drop_spawn(&ctx->spawn);
424out_free_inst:
425 kfree(inst);
426 }
427
Herbert Xu124b53d2007-04-16 20:49:20 +1000428out_put_alg:
429 crypto_mod_put(alg);
Herbert Xu9cd899a2009-07-14 18:45:45 +0800430 return err;
Herbert Xu124b53d2007-04-16 20:49:20 +1000431}
432
Loc Hob8a28252008-05-14 21:23:00 +0800433static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
434{
435 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
Herbert Xu46309d82009-07-12 21:38:59 +0800436 struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
437 struct crypto_shash_spawn *spawn = &ictx->spawn;
Loc Hob8a28252008-05-14 21:23:00 +0800438 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu46309d82009-07-12 21:38:59 +0800439 struct crypto_shash *hash;
Loc Hob8a28252008-05-14 21:23:00 +0800440
Herbert Xu46309d82009-07-12 21:38:59 +0800441 hash = crypto_spawn_shash(spawn);
442 if (IS_ERR(hash))
443 return PTR_ERR(hash);
Loc Hob8a28252008-05-14 21:23:00 +0800444
Herbert Xu46309d82009-07-12 21:38:59 +0800445 ctx->child = hash;
Herbert Xu0d6669e2009-07-12 23:06:33 +0800446 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
447 sizeof(struct cryptd_hash_request_ctx) +
448 crypto_shash_descsize(hash));
Loc Hob8a28252008-05-14 21:23:00 +0800449 return 0;
450}
451
452static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
453{
454 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
Loc Hob8a28252008-05-14 21:23:00 +0800455
Herbert Xu46309d82009-07-12 21:38:59 +0800456 crypto_free_shash(ctx->child);
Loc Hob8a28252008-05-14 21:23:00 +0800457}
458
459static int cryptd_hash_setkey(struct crypto_ahash *parent,
460 const u8 *key, unsigned int keylen)
461{
462 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
Herbert Xu46309d82009-07-12 21:38:59 +0800463 struct crypto_shash *child = ctx->child;
Loc Hob8a28252008-05-14 21:23:00 +0800464 int err;
465
Herbert Xu46309d82009-07-12 21:38:59 +0800466 crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
467 crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
468 CRYPTO_TFM_REQ_MASK);
469 err = crypto_shash_setkey(child, key, keylen);
470 crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
471 CRYPTO_TFM_RES_MASK);
Loc Hob8a28252008-05-14 21:23:00 +0800472 return err;
473}
474
475static int cryptd_hash_enqueue(struct ahash_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700476 crypto_completion_t compl)
Loc Hob8a28252008-05-14 21:23:00 +0800477{
478 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
479 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
Huang Ying254eff72009-02-19 14:42:19 +0800480 struct cryptd_queue *queue =
481 cryptd_get_queue(crypto_ahash_tfm(tfm));
Loc Hob8a28252008-05-14 21:23:00 +0800482
483 rctx->complete = req->base.complete;
Mark Rustad3e3dc252014-07-25 02:53:38 -0700484 req->base.complete = compl;
Loc Hob8a28252008-05-14 21:23:00 +0800485
Huang Ying254eff72009-02-19 14:42:19 +0800486 return cryptd_enqueue_request(queue, &req->base);
Loc Hob8a28252008-05-14 21:23:00 +0800487}
488
Herbert Xu81760ea2016-06-21 16:55:13 +0800489static void cryptd_hash_complete(struct ahash_request *req, int err)
490{
491 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
492 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
493 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
494 int refcnt = atomic_read(&ctx->refcnt);
495
496 local_bh_disable();
497 rctx->complete(&req->base, err);
498 local_bh_enable();
499
500 if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
501 crypto_free_ahash(tfm);
502}
503
Loc Hob8a28252008-05-14 21:23:00 +0800504static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
505{
Herbert Xu46309d82009-07-12 21:38:59 +0800506 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
507 struct crypto_shash *child = ctx->child;
508 struct ahash_request *req = ahash_request_cast(req_async);
509 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
510 struct shash_desc *desc = &rctx->desc;
Loc Hob8a28252008-05-14 21:23:00 +0800511
512 if (unlikely(err == -EINPROGRESS))
513 goto out;
514
Herbert Xu46309d82009-07-12 21:38:59 +0800515 desc->tfm = child;
516 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Loc Hob8a28252008-05-14 21:23:00 +0800517
Herbert Xu46309d82009-07-12 21:38:59 +0800518 err = crypto_shash_init(desc);
Loc Hob8a28252008-05-14 21:23:00 +0800519
520 req->base.complete = rctx->complete;
521
522out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800523 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800524}
525
526static int cryptd_hash_init_enqueue(struct ahash_request *req)
527{
528 return cryptd_hash_enqueue(req, cryptd_hash_init);
529}
530
531static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
532{
Herbert Xu46309d82009-07-12 21:38:59 +0800533 struct ahash_request *req = ahash_request_cast(req_async);
Loc Hob8a28252008-05-14 21:23:00 +0800534 struct cryptd_hash_request_ctx *rctx;
Loc Hob8a28252008-05-14 21:23:00 +0800535
536 rctx = ahash_request_ctx(req);
537
538 if (unlikely(err == -EINPROGRESS))
539 goto out;
540
Herbert Xu46309d82009-07-12 21:38:59 +0800541 err = shash_ahash_update(req, &rctx->desc);
Loc Hob8a28252008-05-14 21:23:00 +0800542
543 req->base.complete = rctx->complete;
544
545out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800546 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800547}
548
549static int cryptd_hash_update_enqueue(struct ahash_request *req)
550{
551 return cryptd_hash_enqueue(req, cryptd_hash_update);
552}
553
554static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
555{
Herbert Xu46309d82009-07-12 21:38:59 +0800556 struct ahash_request *req = ahash_request_cast(req_async);
557 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
Loc Hob8a28252008-05-14 21:23:00 +0800558
559 if (unlikely(err == -EINPROGRESS))
560 goto out;
561
Herbert Xu46309d82009-07-12 21:38:59 +0800562 err = crypto_shash_final(&rctx->desc, req->result);
Loc Hob8a28252008-05-14 21:23:00 +0800563
564 req->base.complete = rctx->complete;
565
566out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800567 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800568}
569
570static int cryptd_hash_final_enqueue(struct ahash_request *req)
571{
572 return cryptd_hash_enqueue(req, cryptd_hash_final);
573}
574
Herbert Xu6fba00d2009-07-22 11:10:22 +0800575static void cryptd_hash_finup(struct crypto_async_request *req_async, int err)
576{
577 struct ahash_request *req = ahash_request_cast(req_async);
578 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
579
580 if (unlikely(err == -EINPROGRESS))
581 goto out;
582
583 err = shash_ahash_finup(req, &rctx->desc);
584
585 req->base.complete = rctx->complete;
586
587out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800588 cryptd_hash_complete(req, err);
Herbert Xu6fba00d2009-07-22 11:10:22 +0800589}
590
591static int cryptd_hash_finup_enqueue(struct ahash_request *req)
592{
593 return cryptd_hash_enqueue(req, cryptd_hash_finup);
594}
595
Loc Hob8a28252008-05-14 21:23:00 +0800596static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
597{
Herbert Xu46309d82009-07-12 21:38:59 +0800598 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
599 struct crypto_shash *child = ctx->child;
600 struct ahash_request *req = ahash_request_cast(req_async);
601 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
602 struct shash_desc *desc = &rctx->desc;
Loc Hob8a28252008-05-14 21:23:00 +0800603
604 if (unlikely(err == -EINPROGRESS))
605 goto out;
606
Herbert Xu46309d82009-07-12 21:38:59 +0800607 desc->tfm = child;
608 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Loc Hob8a28252008-05-14 21:23:00 +0800609
Herbert Xu46309d82009-07-12 21:38:59 +0800610 err = shash_ahash_digest(req, desc);
Loc Hob8a28252008-05-14 21:23:00 +0800611
612 req->base.complete = rctx->complete;
613
614out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800615 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800616}
617
618static int cryptd_hash_digest_enqueue(struct ahash_request *req)
619{
620 return cryptd_hash_enqueue(req, cryptd_hash_digest);
621}
622
Herbert Xu6fba00d2009-07-22 11:10:22 +0800623static int cryptd_hash_export(struct ahash_request *req, void *out)
624{
625 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
626
627 return crypto_shash_export(&rctx->desc, out);
628}
629
630static int cryptd_hash_import(struct ahash_request *req, const void *in)
631{
Ard Biesheuvel0bd22232016-09-01 14:25:43 +0100632 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
633 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
634 struct shash_desc *desc = cryptd_shash_desc(req);
Herbert Xu6fba00d2009-07-22 11:10:22 +0800635
Ard Biesheuvel0bd22232016-09-01 14:25:43 +0100636 desc->tfm = ctx->child;
637 desc->flags = req->base.flags;
638
639 return crypto_shash_import(desc, in);
Herbert Xu6fba00d2009-07-22 11:10:22 +0800640}
641
Herbert Xu9cd899a2009-07-14 18:45:45 +0800642static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
643 struct cryptd_queue *queue)
Loc Hob8a28252008-05-14 21:23:00 +0800644{
Herbert Xu46309d82009-07-12 21:38:59 +0800645 struct hashd_instance_ctx *ctx;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800646 struct ahash_instance *inst;
Herbert Xu46309d82009-07-12 21:38:59 +0800647 struct shash_alg *salg;
Loc Hob8a28252008-05-14 21:23:00 +0800648 struct crypto_alg *alg;
Stephan Mueller466a7b92015-03-30 21:57:06 +0200649 u32 type = 0;
650 u32 mask = 0;
Herbert Xu46309d82009-07-12 21:38:59 +0800651 int err;
Loc Hob8a28252008-05-14 21:23:00 +0800652
Stephan Mueller466a7b92015-03-30 21:57:06 +0200653 cryptd_check_internal(tb, &type, &mask);
654
655 salg = shash_attr_alg(tb[1], type, mask);
Herbert Xu46309d82009-07-12 21:38:59 +0800656 if (IS_ERR(salg))
Herbert Xu9cd899a2009-07-14 18:45:45 +0800657 return PTR_ERR(salg);
Loc Hob8a28252008-05-14 21:23:00 +0800658
Herbert Xu46309d82009-07-12 21:38:59 +0800659 alg = &salg->base;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800660 inst = cryptd_alloc_instance(alg, ahash_instance_headroom(),
661 sizeof(*ctx));
Steffen Klassert05ed8752009-07-15 16:51:04 +0800662 err = PTR_ERR(inst);
Loc Hob8a28252008-05-14 21:23:00 +0800663 if (IS_ERR(inst))
664 goto out_put_alg;
665
Herbert Xu0b535ad2009-07-14 19:11:32 +0800666 ctx = ahash_instance_ctx(inst);
Herbert Xu46309d82009-07-12 21:38:59 +0800667 ctx->queue = queue;
668
Herbert Xu0b535ad2009-07-14 19:11:32 +0800669 err = crypto_init_shash_spawn(&ctx->spawn, salg,
670 ahash_crypto_instance(inst));
Herbert Xu46309d82009-07-12 21:38:59 +0800671 if (err)
672 goto out_free_inst;
673
Stephan Mueller466a7b92015-03-30 21:57:06 +0200674 type = CRYPTO_ALG_ASYNC;
675 if (alg->cra_flags & CRYPTO_ALG_INTERNAL)
676 type |= CRYPTO_ALG_INTERNAL;
677 inst->alg.halg.base.cra_flags = type;
Loc Hob8a28252008-05-14 21:23:00 +0800678
Herbert Xu0b535ad2009-07-14 19:11:32 +0800679 inst->alg.halg.digestsize = salg->digestsize;
Wang, Rui Y1a078342015-11-29 22:45:34 +0800680 inst->alg.halg.statesize = salg->statesize;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800681 inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
Loc Hob8a28252008-05-14 21:23:00 +0800682
Herbert Xu0b535ad2009-07-14 19:11:32 +0800683 inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
684 inst->alg.halg.base.cra_exit = cryptd_hash_exit_tfm;
Loc Hob8a28252008-05-14 21:23:00 +0800685
Herbert Xu0b535ad2009-07-14 19:11:32 +0800686 inst->alg.init = cryptd_hash_init_enqueue;
687 inst->alg.update = cryptd_hash_update_enqueue;
688 inst->alg.final = cryptd_hash_final_enqueue;
Herbert Xu6fba00d2009-07-22 11:10:22 +0800689 inst->alg.finup = cryptd_hash_finup_enqueue;
690 inst->alg.export = cryptd_hash_export;
691 inst->alg.import = cryptd_hash_import;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800692 inst->alg.setkey = cryptd_hash_setkey;
693 inst->alg.digest = cryptd_hash_digest_enqueue;
Loc Hob8a28252008-05-14 21:23:00 +0800694
Herbert Xu0b535ad2009-07-14 19:11:32 +0800695 err = ahash_register_instance(tmpl, inst);
Herbert Xu9cd899a2009-07-14 18:45:45 +0800696 if (err) {
697 crypto_drop_shash(&ctx->spawn);
698out_free_inst:
699 kfree(inst);
700 }
701
Loc Hob8a28252008-05-14 21:23:00 +0800702out_put_alg:
703 crypto_mod_put(alg);
Herbert Xu9cd899a2009-07-14 18:45:45 +0800704 return err;
Loc Hob8a28252008-05-14 21:23:00 +0800705}
706
Herbert Xu92b98762015-05-28 22:08:01 +0800707static int cryptd_aead_setkey(struct crypto_aead *parent,
708 const u8 *key, unsigned int keylen)
709{
710 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
711 struct crypto_aead *child = ctx->child;
712
713 return crypto_aead_setkey(child, key, keylen);
714}
715
716static int cryptd_aead_setauthsize(struct crypto_aead *parent,
717 unsigned int authsize)
718{
719 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
720 struct crypto_aead *child = ctx->child;
721
722 return crypto_aead_setauthsize(child, authsize);
723}
724
Adrian Hoban298c9262010-09-20 16:05:12 +0800725static void cryptd_aead_crypt(struct aead_request *req,
726 struct crypto_aead *child,
727 int err,
728 int (*crypt)(struct aead_request *req))
729{
730 struct cryptd_aead_request_ctx *rctx;
Herbert Xu81760ea2016-06-21 16:55:13 +0800731 struct cryptd_aead_ctx *ctx;
Herbert Xuec9f2002015-07-06 19:11:03 +0800732 crypto_completion_t compl;
Herbert Xu81760ea2016-06-21 16:55:13 +0800733 struct crypto_aead *tfm;
734 int refcnt;
Herbert Xuec9f2002015-07-06 19:11:03 +0800735
Adrian Hoban298c9262010-09-20 16:05:12 +0800736 rctx = aead_request_ctx(req);
Herbert Xuec9f2002015-07-06 19:11:03 +0800737 compl = rctx->complete;
Adrian Hoban298c9262010-09-20 16:05:12 +0800738
Herbert Xu31bd44e2016-08-25 16:49:51 +0800739 tfm = crypto_aead_reqtfm(req);
740
Adrian Hoban298c9262010-09-20 16:05:12 +0800741 if (unlikely(err == -EINPROGRESS))
742 goto out;
743 aead_request_set_tfm(req, child);
744 err = crypt( req );
Herbert Xu81760ea2016-06-21 16:55:13 +0800745
Adrian Hoban298c9262010-09-20 16:05:12 +0800746out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800747 ctx = crypto_aead_ctx(tfm);
748 refcnt = atomic_read(&ctx->refcnt);
749
Adrian Hoban298c9262010-09-20 16:05:12 +0800750 local_bh_disable();
Herbert Xuec9f2002015-07-06 19:11:03 +0800751 compl(&req->base, err);
Adrian Hoban298c9262010-09-20 16:05:12 +0800752 local_bh_enable();
Herbert Xu81760ea2016-06-21 16:55:13 +0800753
754 if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
755 crypto_free_aead(tfm);
Adrian Hoban298c9262010-09-20 16:05:12 +0800756}
757
758static void cryptd_aead_encrypt(struct crypto_async_request *areq, int err)
759{
760 struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
761 struct crypto_aead *child = ctx->child;
762 struct aead_request *req;
763
764 req = container_of(areq, struct aead_request, base);
Herbert Xuba3749a2015-08-13 17:29:02 +0800765 cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->encrypt);
Adrian Hoban298c9262010-09-20 16:05:12 +0800766}
767
768static void cryptd_aead_decrypt(struct crypto_async_request *areq, int err)
769{
770 struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
771 struct crypto_aead *child = ctx->child;
772 struct aead_request *req;
773
774 req = container_of(areq, struct aead_request, base);
Herbert Xuba3749a2015-08-13 17:29:02 +0800775 cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->decrypt);
Adrian Hoban298c9262010-09-20 16:05:12 +0800776}
777
778static int cryptd_aead_enqueue(struct aead_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700779 crypto_completion_t compl)
Adrian Hoban298c9262010-09-20 16:05:12 +0800780{
781 struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req);
782 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
783 struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm));
784
785 rctx->complete = req->base.complete;
Mark Rustad3e3dc252014-07-25 02:53:38 -0700786 req->base.complete = compl;
Adrian Hoban298c9262010-09-20 16:05:12 +0800787 return cryptd_enqueue_request(queue, &req->base);
788}
789
790static int cryptd_aead_encrypt_enqueue(struct aead_request *req)
791{
792 return cryptd_aead_enqueue(req, cryptd_aead_encrypt );
793}
794
795static int cryptd_aead_decrypt_enqueue(struct aead_request *req)
796{
797 return cryptd_aead_enqueue(req, cryptd_aead_decrypt );
798}
799
Herbert Xuf614e542015-05-28 22:08:04 +0800800static int cryptd_aead_init_tfm(struct crypto_aead *tfm)
Adrian Hoban298c9262010-09-20 16:05:12 +0800801{
Herbert Xuf614e542015-05-28 22:08:04 +0800802 struct aead_instance *inst = aead_alg_instance(tfm);
803 struct aead_instance_ctx *ictx = aead_instance_ctx(inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800804 struct crypto_aead_spawn *spawn = &ictx->aead_spawn;
Herbert Xuf614e542015-05-28 22:08:04 +0800805 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
Adrian Hoban298c9262010-09-20 16:05:12 +0800806 struct crypto_aead *cipher;
807
808 cipher = crypto_spawn_aead(spawn);
809 if (IS_ERR(cipher))
810 return PTR_ERR(cipher);
811
Adrian Hoban298c9262010-09-20 16:05:12 +0800812 ctx->child = cipher;
Herbert Xuec9f2002015-07-06 19:11:03 +0800813 crypto_aead_set_reqsize(
814 tfm, max((unsigned)sizeof(struct cryptd_aead_request_ctx),
815 crypto_aead_reqsize(cipher)));
Adrian Hoban298c9262010-09-20 16:05:12 +0800816 return 0;
817}
818
Herbert Xuf614e542015-05-28 22:08:04 +0800819static void cryptd_aead_exit_tfm(struct crypto_aead *tfm)
Adrian Hoban298c9262010-09-20 16:05:12 +0800820{
Herbert Xuf614e542015-05-28 22:08:04 +0800821 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
Adrian Hoban298c9262010-09-20 16:05:12 +0800822 crypto_free_aead(ctx->child);
823}
824
825static int cryptd_create_aead(struct crypto_template *tmpl,
826 struct rtattr **tb,
827 struct cryptd_queue *queue)
828{
829 struct aead_instance_ctx *ctx;
Herbert Xuf614e542015-05-28 22:08:04 +0800830 struct aead_instance *inst;
831 struct aead_alg *alg;
Herbert Xu9b8c4562015-05-21 15:10:57 +0800832 const char *name;
833 u32 type = 0;
Herbert Xuec9f2002015-07-06 19:11:03 +0800834 u32 mask = CRYPTO_ALG_ASYNC;
Adrian Hoban298c9262010-09-20 16:05:12 +0800835 int err;
836
Stephan Mueller466a7b92015-03-30 21:57:06 +0200837 cryptd_check_internal(tb, &type, &mask);
838
Herbert Xu9b8c4562015-05-21 15:10:57 +0800839 name = crypto_attr_alg_name(tb[1]);
840 if (IS_ERR(name))
841 return PTR_ERR(name);
Adrian Hoban298c9262010-09-20 16:05:12 +0800842
Herbert Xu9b8c4562015-05-21 15:10:57 +0800843 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
844 if (!inst)
845 return -ENOMEM;
Adrian Hoban298c9262010-09-20 16:05:12 +0800846
Herbert Xuf614e542015-05-28 22:08:04 +0800847 ctx = aead_instance_ctx(inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800848 ctx->queue = queue;
849
Herbert Xuf614e542015-05-28 22:08:04 +0800850 crypto_set_aead_spawn(&ctx->aead_spawn, aead_crypto_instance(inst));
Herbert Xu9b8c4562015-05-21 15:10:57 +0800851 err = crypto_grab_aead(&ctx->aead_spawn, name, type, mask);
Adrian Hoban298c9262010-09-20 16:05:12 +0800852 if (err)
853 goto out_free_inst;
854
Herbert Xuf614e542015-05-28 22:08:04 +0800855 alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
856 err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
Herbert Xu9b8c4562015-05-21 15:10:57 +0800857 if (err)
858 goto out_drop_aead;
859
Herbert Xuf614e542015-05-28 22:08:04 +0800860 inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800861 (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
Herbert Xuf614e542015-05-28 22:08:04 +0800862 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx);
Adrian Hoban298c9262010-09-20 16:05:12 +0800863
Herbert Xuf614e542015-05-28 22:08:04 +0800864 inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
865 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
866
867 inst->alg.init = cryptd_aead_init_tfm;
868 inst->alg.exit = cryptd_aead_exit_tfm;
869 inst->alg.setkey = cryptd_aead_setkey;
870 inst->alg.setauthsize = cryptd_aead_setauthsize;
871 inst->alg.encrypt = cryptd_aead_encrypt_enqueue;
872 inst->alg.decrypt = cryptd_aead_decrypt_enqueue;
873
874 err = aead_register_instance(tmpl, inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800875 if (err) {
Herbert Xu9b8c4562015-05-21 15:10:57 +0800876out_drop_aead:
877 crypto_drop_aead(&ctx->aead_spawn);
Adrian Hoban298c9262010-09-20 16:05:12 +0800878out_free_inst:
879 kfree(inst);
880 }
Adrian Hoban298c9262010-09-20 16:05:12 +0800881 return err;
882}
883
Huang Ying254eff72009-02-19 14:42:19 +0800884static struct cryptd_queue queue;
Herbert Xu124b53d2007-04-16 20:49:20 +1000885
Herbert Xu9cd899a2009-07-14 18:45:45 +0800886static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu124b53d2007-04-16 20:49:20 +1000887{
888 struct crypto_attr_type *algt;
889
890 algt = crypto_get_attr_type(tb);
891 if (IS_ERR(algt))
Herbert Xu9cd899a2009-07-14 18:45:45 +0800892 return PTR_ERR(algt);
Herbert Xu124b53d2007-04-16 20:49:20 +1000893
894 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
895 case CRYPTO_ALG_TYPE_BLKCIPHER:
Herbert Xu9cd899a2009-07-14 18:45:45 +0800896 return cryptd_create_blkcipher(tmpl, tb, &queue);
Loc Hob8a28252008-05-14 21:23:00 +0800897 case CRYPTO_ALG_TYPE_DIGEST:
Herbert Xu9cd899a2009-07-14 18:45:45 +0800898 return cryptd_create_hash(tmpl, tb, &queue);
Adrian Hoban298c9262010-09-20 16:05:12 +0800899 case CRYPTO_ALG_TYPE_AEAD:
900 return cryptd_create_aead(tmpl, tb, &queue);
Herbert Xu124b53d2007-04-16 20:49:20 +1000901 }
902
Herbert Xu9cd899a2009-07-14 18:45:45 +0800903 return -EINVAL;
Herbert Xu124b53d2007-04-16 20:49:20 +1000904}
905
906static void cryptd_free(struct crypto_instance *inst)
907{
908 struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
Herbert Xu0b535ad2009-07-14 19:11:32 +0800909 struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800910 struct aead_instance_ctx *aead_ctx = crypto_instance_ctx(inst);
Herbert Xu0b535ad2009-07-14 19:11:32 +0800911
912 switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) {
913 case CRYPTO_ALG_TYPE_AHASH:
914 crypto_drop_shash(&hctx->spawn);
915 kfree(ahash_instance(inst));
916 return;
Adrian Hoban298c9262010-09-20 16:05:12 +0800917 case CRYPTO_ALG_TYPE_AEAD:
Herbert Xuf614e542015-05-28 22:08:04 +0800918 crypto_drop_aead(&aead_ctx->aead_spawn);
919 kfree(aead_instance(inst));
Adrian Hoban298c9262010-09-20 16:05:12 +0800920 return;
921 default:
922 crypto_drop_spawn(&ctx->spawn);
923 kfree(inst);
Herbert Xu0b535ad2009-07-14 19:11:32 +0800924 }
Herbert Xu124b53d2007-04-16 20:49:20 +1000925}
926
927static struct crypto_template cryptd_tmpl = {
928 .name = "cryptd",
Herbert Xu9cd899a2009-07-14 18:45:45 +0800929 .create = cryptd_create,
Herbert Xu124b53d2007-04-16 20:49:20 +1000930 .free = cryptd_free,
931 .module = THIS_MODULE,
932};
933
Huang Ying1cac2cb2009-01-18 16:19:46 +1100934struct cryptd_ablkcipher *cryptd_alloc_ablkcipher(const char *alg_name,
935 u32 type, u32 mask)
936{
937 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu81760ea2016-06-21 16:55:13 +0800938 struct cryptd_blkcipher_ctx *ctx;
Huang Ying505fd212009-03-29 15:33:53 +0800939 struct crypto_tfm *tfm;
Huang Ying1cac2cb2009-01-18 16:19:46 +1100940
941 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
942 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
943 return ERR_PTR(-EINVAL);
Alexander Kuleshovc012a792015-11-25 23:48:28 +0600944 type = crypto_skcipher_type(type);
Huang Ying505fd212009-03-29 15:33:53 +0800945 mask &= ~CRYPTO_ALG_TYPE_MASK;
946 mask |= (CRYPTO_ALG_GENIV | CRYPTO_ALG_TYPE_BLKCIPHER_MASK);
947 tfm = crypto_alloc_base(cryptd_alg_name, type, mask);
Huang Ying1cac2cb2009-01-18 16:19:46 +1100948 if (IS_ERR(tfm))
949 return ERR_CAST(tfm);
Huang Ying505fd212009-03-29 15:33:53 +0800950 if (tfm->__crt_alg->cra_module != THIS_MODULE) {
951 crypto_free_tfm(tfm);
Huang Ying1cac2cb2009-01-18 16:19:46 +1100952 return ERR_PTR(-EINVAL);
953 }
954
Herbert Xu81760ea2016-06-21 16:55:13 +0800955 ctx = crypto_tfm_ctx(tfm);
956 atomic_set(&ctx->refcnt, 1);
957
Huang Ying505fd212009-03-29 15:33:53 +0800958 return __cryptd_ablkcipher_cast(__crypto_ablkcipher_cast(tfm));
Huang Ying1cac2cb2009-01-18 16:19:46 +1100959}
960EXPORT_SYMBOL_GPL(cryptd_alloc_ablkcipher);
961
962struct crypto_blkcipher *cryptd_ablkcipher_child(struct cryptd_ablkcipher *tfm)
963{
964 struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
965 return ctx->child;
966}
967EXPORT_SYMBOL_GPL(cryptd_ablkcipher_child);
968
Herbert Xu81760ea2016-06-21 16:55:13 +0800969bool cryptd_ablkcipher_queued(struct cryptd_ablkcipher *tfm)
970{
971 struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
972
973 return atomic_read(&ctx->refcnt) - 1;
974}
975EXPORT_SYMBOL_GPL(cryptd_ablkcipher_queued);
976
Huang Ying1cac2cb2009-01-18 16:19:46 +1100977void cryptd_free_ablkcipher(struct cryptd_ablkcipher *tfm)
978{
Herbert Xu81760ea2016-06-21 16:55:13 +0800979 struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base);
980
981 if (atomic_dec_and_test(&ctx->refcnt))
982 crypto_free_ablkcipher(&tfm->base);
Huang Ying1cac2cb2009-01-18 16:19:46 +1100983}
984EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher);
985
Huang Yingace13662009-08-06 15:35:20 +1000986struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name,
987 u32 type, u32 mask)
988{
989 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu81760ea2016-06-21 16:55:13 +0800990 struct cryptd_hash_ctx *ctx;
Huang Yingace13662009-08-06 15:35:20 +1000991 struct crypto_ahash *tfm;
992
993 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
994 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
995 return ERR_PTR(-EINVAL);
996 tfm = crypto_alloc_ahash(cryptd_alg_name, type, mask);
997 if (IS_ERR(tfm))
998 return ERR_CAST(tfm);
999 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
1000 crypto_free_ahash(tfm);
1001 return ERR_PTR(-EINVAL);
1002 }
1003
Herbert Xu81760ea2016-06-21 16:55:13 +08001004 ctx = crypto_ahash_ctx(tfm);
1005 atomic_set(&ctx->refcnt, 1);
1006
Huang Yingace13662009-08-06 15:35:20 +10001007 return __cryptd_ahash_cast(tfm);
1008}
1009EXPORT_SYMBOL_GPL(cryptd_alloc_ahash);
1010
1011struct crypto_shash *cryptd_ahash_child(struct cryptd_ahash *tfm)
1012{
1013 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
1014
1015 return ctx->child;
1016}
1017EXPORT_SYMBOL_GPL(cryptd_ahash_child);
1018
Huang Ying0e1227d2009-10-19 11:53:06 +09001019struct shash_desc *cryptd_shash_desc(struct ahash_request *req)
1020{
1021 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
1022 return &rctx->desc;
1023}
1024EXPORT_SYMBOL_GPL(cryptd_shash_desc);
1025
Herbert Xu81760ea2016-06-21 16:55:13 +08001026bool cryptd_ahash_queued(struct cryptd_ahash *tfm)
1027{
1028 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
1029
1030 return atomic_read(&ctx->refcnt) - 1;
1031}
1032EXPORT_SYMBOL_GPL(cryptd_ahash_queued);
1033
Huang Yingace13662009-08-06 15:35:20 +10001034void cryptd_free_ahash(struct cryptd_ahash *tfm)
1035{
Herbert Xu81760ea2016-06-21 16:55:13 +08001036 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
1037
1038 if (atomic_dec_and_test(&ctx->refcnt))
1039 crypto_free_ahash(&tfm->base);
Huang Yingace13662009-08-06 15:35:20 +10001040}
1041EXPORT_SYMBOL_GPL(cryptd_free_ahash);
1042
Adrian Hoban298c9262010-09-20 16:05:12 +08001043struct cryptd_aead *cryptd_alloc_aead(const char *alg_name,
1044 u32 type, u32 mask)
1045{
1046 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu81760ea2016-06-21 16:55:13 +08001047 struct cryptd_aead_ctx *ctx;
Adrian Hoban298c9262010-09-20 16:05:12 +08001048 struct crypto_aead *tfm;
1049
1050 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
1051 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
1052 return ERR_PTR(-EINVAL);
1053 tfm = crypto_alloc_aead(cryptd_alg_name, type, mask);
1054 if (IS_ERR(tfm))
1055 return ERR_CAST(tfm);
1056 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
1057 crypto_free_aead(tfm);
1058 return ERR_PTR(-EINVAL);
1059 }
Herbert Xu81760ea2016-06-21 16:55:13 +08001060
1061 ctx = crypto_aead_ctx(tfm);
1062 atomic_set(&ctx->refcnt, 1);
1063
Adrian Hoban298c9262010-09-20 16:05:12 +08001064 return __cryptd_aead_cast(tfm);
1065}
1066EXPORT_SYMBOL_GPL(cryptd_alloc_aead);
1067
1068struct crypto_aead *cryptd_aead_child(struct cryptd_aead *tfm)
1069{
1070 struct cryptd_aead_ctx *ctx;
1071 ctx = crypto_aead_ctx(&tfm->base);
1072 return ctx->child;
1073}
1074EXPORT_SYMBOL_GPL(cryptd_aead_child);
1075
Herbert Xu81760ea2016-06-21 16:55:13 +08001076bool cryptd_aead_queued(struct cryptd_aead *tfm)
1077{
1078 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
1079
1080 return atomic_read(&ctx->refcnt) - 1;
1081}
1082EXPORT_SYMBOL_GPL(cryptd_aead_queued);
1083
Adrian Hoban298c9262010-09-20 16:05:12 +08001084void cryptd_free_aead(struct cryptd_aead *tfm)
1085{
Herbert Xu81760ea2016-06-21 16:55:13 +08001086 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
1087
1088 if (atomic_dec_and_test(&ctx->refcnt))
1089 crypto_free_aead(&tfm->base);
Adrian Hoban298c9262010-09-20 16:05:12 +08001090}
1091EXPORT_SYMBOL_GPL(cryptd_free_aead);
1092
Herbert Xu124b53d2007-04-16 20:49:20 +10001093static int __init cryptd_init(void)
1094{
1095 int err;
1096
Huang Ying254eff72009-02-19 14:42:19 +08001097 err = cryptd_init_queue(&queue, CRYPTD_MAX_CPU_QLEN);
Herbert Xu124b53d2007-04-16 20:49:20 +10001098 if (err)
1099 return err;
1100
1101 err = crypto_register_template(&cryptd_tmpl);
1102 if (err)
Huang Ying254eff72009-02-19 14:42:19 +08001103 cryptd_fini_queue(&queue);
Herbert Xu124b53d2007-04-16 20:49:20 +10001104
1105 return err;
1106}
1107
1108static void __exit cryptd_exit(void)
1109{
Huang Ying254eff72009-02-19 14:42:19 +08001110 cryptd_fini_queue(&queue);
Herbert Xu124b53d2007-04-16 20:49:20 +10001111 crypto_unregister_template(&cryptd_tmpl);
1112}
1113
Herbert Xub2bac6a2011-08-19 16:11:23 +08001114subsys_initcall(cryptd_init);
Herbert Xu124b53d2007-04-16 20:49:20 +10001115module_exit(cryptd_exit);
1116
1117MODULE_LICENSE("GPL");
1118MODULE_DESCRIPTION("Software async crypto daemon");
Kees Cook4943ba12014-11-24 16:32:38 -08001119MODULE_ALIAS_CRYPTO("cryptd");