blob: 1bf777b765127edf5b4fddf9971d96f004a6b299 [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
Herbert Xu18e33e62008-07-10 16:01:22 +080020#include <crypto/internal/hash.h>
Adrian Hoban298c9262010-09-20 16:05:12 +080021#include <crypto/internal/aead.h>
Herbert Xu4e0958d2016-11-22 20:08:23 +080022#include <crypto/internal/skcipher.h>
Huang Ying1cac2cb2009-01-18 16:19:46 +110023#include <crypto/cryptd.h>
Herbert Xu81760ea2016-06-21 16:55:13 +080024#include <linux/atomic.h>
Herbert Xu124b53d2007-04-16 20:49:20 +100025#include <linux/err.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
Herbert Xu124b53d2007-04-16 20:49:20 +100028#include <linux/list.h>
29#include <linux/module.h>
Herbert Xu124b53d2007-04-16 20:49:20 +100030#include <linux/scatterlist.h>
31#include <linux/sched.h>
32#include <linux/slab.h>
Eric Biggers3e56e162019-05-20 09:53:58 -070033#include <linux/workqueue.h>
Herbert Xu124b53d2007-04-16 20:49:20 +100034
Colin Ian Kingeaf356e2017-11-30 11:26:14 +000035static unsigned int cryptd_max_cpu_qlen = 1000;
Jon Maxwellc3a53602017-11-22 16:08:17 +110036module_param(cryptd_max_cpu_qlen, uint, 0);
37MODULE_PARM_DESC(cryptd_max_cpu_qlen, "Set cryptd Max queue depth");
Herbert Xu124b53d2007-04-16 20:49:20 +100038
Eric Biggers3e56e162019-05-20 09:53:58 -070039static struct workqueue_struct *cryptd_wq;
40
Huang Ying254eff72009-02-19 14:42:19 +080041struct cryptd_cpu_queue {
Herbert Xu124b53d2007-04-16 20:49:20 +100042 struct crypto_queue queue;
Huang Ying254eff72009-02-19 14:42:19 +080043 struct work_struct work;
44};
45
46struct cryptd_queue {
Tejun Heoa29d8b82010-02-02 14:39:15 +090047 struct cryptd_cpu_queue __percpu *cpu_queue;
Herbert Xu124b53d2007-04-16 20:49:20 +100048};
49
50struct cryptd_instance_ctx {
51 struct crypto_spawn spawn;
Huang Ying254eff72009-02-19 14:42:19 +080052 struct cryptd_queue *queue;
Herbert Xu124b53d2007-04-16 20:49:20 +100053};
54
Herbert Xu4e0958d2016-11-22 20:08:23 +080055struct skcipherd_instance_ctx {
56 struct crypto_skcipher_spawn spawn;
57 struct cryptd_queue *queue;
58};
59
Herbert Xu46309d82009-07-12 21:38:59 +080060struct hashd_instance_ctx {
61 struct crypto_shash_spawn spawn;
62 struct cryptd_queue *queue;
63};
64
Adrian Hoban298c9262010-09-20 16:05:12 +080065struct aead_instance_ctx {
66 struct crypto_aead_spawn aead_spawn;
67 struct cryptd_queue *queue;
68};
69
Herbert Xu4e0958d2016-11-22 20:08:23 +080070struct cryptd_skcipher_ctx {
71 atomic_t refcnt;
Kees Cook36b38752018-09-18 19:10:52 -070072 struct crypto_sync_skcipher *child;
Herbert Xu4e0958d2016-11-22 20:08:23 +080073};
74
75struct cryptd_skcipher_request_ctx {
76 crypto_completion_t complete;
77};
78
Loc Hob8a28252008-05-14 21:23:00 +080079struct cryptd_hash_ctx {
Herbert Xu81760ea2016-06-21 16:55:13 +080080 atomic_t refcnt;
Herbert Xu46309d82009-07-12 21:38:59 +080081 struct crypto_shash *child;
Loc Hob8a28252008-05-14 21:23:00 +080082};
83
84struct cryptd_hash_request_ctx {
85 crypto_completion_t complete;
Herbert Xu46309d82009-07-12 21:38:59 +080086 struct shash_desc desc;
Loc Hob8a28252008-05-14 21:23:00 +080087};
Herbert Xu124b53d2007-04-16 20:49:20 +100088
Adrian Hoban298c9262010-09-20 16:05:12 +080089struct cryptd_aead_ctx {
Herbert Xu81760ea2016-06-21 16:55:13 +080090 atomic_t refcnt;
Adrian Hoban298c9262010-09-20 16:05:12 +080091 struct crypto_aead *child;
92};
93
94struct cryptd_aead_request_ctx {
95 crypto_completion_t complete;
96};
97
Huang Ying254eff72009-02-19 14:42:19 +080098static void cryptd_queue_worker(struct work_struct *work);
99
100static int cryptd_init_queue(struct cryptd_queue *queue,
101 unsigned int max_cpu_qlen)
102{
103 int cpu;
104 struct cryptd_cpu_queue *cpu_queue;
105
106 queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue);
107 if (!queue->cpu_queue)
108 return -ENOMEM;
109 for_each_possible_cpu(cpu) {
110 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
111 crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
112 INIT_WORK(&cpu_queue->work, cryptd_queue_worker);
113 }
Jon Maxwellc3a53602017-11-22 16:08:17 +1100114 pr_info("cryptd: max_cpu_qlen set to %d\n", max_cpu_qlen);
Huang Ying254eff72009-02-19 14:42:19 +0800115 return 0;
116}
117
118static void cryptd_fini_queue(struct cryptd_queue *queue)
119{
120 int cpu;
121 struct cryptd_cpu_queue *cpu_queue;
122
123 for_each_possible_cpu(cpu) {
124 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
125 BUG_ON(cpu_queue->queue.qlen);
126 }
127 free_percpu(queue->cpu_queue);
128}
129
130static int cryptd_enqueue_request(struct cryptd_queue *queue,
131 struct crypto_async_request *request)
132{
133 int cpu, err;
134 struct cryptd_cpu_queue *cpu_queue;
Herbert Xu81760ea2016-06-21 16:55:13 +0800135 atomic_t *refcnt;
Huang Ying254eff72009-02-19 14:42:19 +0800136
137 cpu = get_cpu();
Christoph Lameter0b44f482009-10-03 19:48:23 +0900138 cpu_queue = this_cpu_ptr(queue->cpu_queue);
Huang Ying254eff72009-02-19 14:42:19 +0800139 err = crypto_enqueue_request(&cpu_queue->queue, request);
Herbert Xu81760ea2016-06-21 16:55:13 +0800140
141 refcnt = crypto_tfm_ctx(request->tfm);
Herbert Xu81760ea2016-06-21 16:55:13 +0800142
Gilad Ben-Yossef6b80ea32017-10-18 08:00:33 +0100143 if (err == -ENOSPC)
Herbert Xu81760ea2016-06-21 16:55:13 +0800144 goto out_put_cpu;
145
Eric Biggers3e56e162019-05-20 09:53:58 -0700146 queue_work_on(cpu, cryptd_wq, &cpu_queue->work);
Herbert Xu81760ea2016-06-21 16:55:13 +0800147
148 if (!atomic_read(refcnt))
149 goto out_put_cpu;
150
Herbert Xu81760ea2016-06-21 16:55:13 +0800151 atomic_inc(refcnt);
152
153out_put_cpu:
Huang Ying254eff72009-02-19 14:42:19 +0800154 put_cpu();
155
156 return err;
157}
158
159/* Called in workqueue context, do one real cryption work (via
160 * req->complete) and reschedule itself if there are more work to
161 * do. */
162static void cryptd_queue_worker(struct work_struct *work)
163{
164 struct cryptd_cpu_queue *cpu_queue;
165 struct crypto_async_request *req, *backlog;
166
167 cpu_queue = container_of(work, struct cryptd_cpu_queue, work);
Jussi Kivilinna9efade12012-10-21 20:42:28 +0300168 /*
169 * Only handle one request at a time to avoid hogging crypto workqueue.
170 * preempt_disable/enable is used to prevent being preempted by
171 * cryptd_enqueue_request(). local_bh_disable/enable is used to prevent
172 * cryptd_enqueue_request() being accessed from software interrupts.
173 */
174 local_bh_disable();
Huang Ying254eff72009-02-19 14:42:19 +0800175 preempt_disable();
176 backlog = crypto_get_backlog(&cpu_queue->queue);
177 req = crypto_dequeue_request(&cpu_queue->queue);
178 preempt_enable();
Jussi Kivilinna9efade12012-10-21 20:42:28 +0300179 local_bh_enable();
Huang Ying254eff72009-02-19 14:42:19 +0800180
181 if (!req)
182 return;
183
184 if (backlog)
185 backlog->complete(backlog, -EINPROGRESS);
186 req->complete(req, 0);
187
188 if (cpu_queue->queue.qlen)
Eric Biggers3e56e162019-05-20 09:53:58 -0700189 queue_work(cryptd_wq, &cpu_queue->work);
Huang Ying254eff72009-02-19 14:42:19 +0800190}
191
192static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
Herbert Xu124b53d2007-04-16 20:49:20 +1000193{
194 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
195 struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
Huang Ying254eff72009-02-19 14:42:19 +0800196 return ictx->queue;
Herbert Xu124b53d2007-04-16 20:49:20 +1000197}
198
Stephan Mueller466a7b92015-03-30 21:57:06 +0200199static inline void cryptd_check_internal(struct rtattr **tb, u32 *type,
200 u32 *mask)
201{
202 struct crypto_attr_type *algt;
203
204 algt = crypto_get_attr_type(tb);
205 if (IS_ERR(algt))
206 return;
Herbert Xuf6da3202015-07-09 07:17:19 +0800207
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800208 *type |= algt->type & CRYPTO_ALG_INTERNAL;
209 *mask |= algt->mask & CRYPTO_ALG_INTERNAL;
Stephan Mueller466a7b92015-03-30 21:57:06 +0200210}
211
Herbert Xu9b8c4562015-05-21 15:10:57 +0800212static int cryptd_init_instance(struct crypto_instance *inst,
213 struct crypto_alg *alg)
214{
215 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
216 "cryptd(%s)",
217 alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
218 return -ENAMETOOLONG;
219
220 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
221
222 inst->alg.cra_priority = alg->cra_priority + 50;
223 inst->alg.cra_blocksize = alg->cra_blocksize;
224 inst->alg.cra_alignmask = alg->cra_alignmask;
225
226 return 0;
227}
228
Herbert Xu0b535ad2009-07-14 19:11:32 +0800229static void *cryptd_alloc_instance(struct crypto_alg *alg, unsigned int head,
230 unsigned int tail)
Herbert Xu124b53d2007-04-16 20:49:20 +1000231{
Herbert Xu0b535ad2009-07-14 19:11:32 +0800232 char *p;
Herbert Xu124b53d2007-04-16 20:49:20 +1000233 struct crypto_instance *inst;
Herbert Xu124b53d2007-04-16 20:49:20 +1000234 int err;
235
Herbert Xu0b535ad2009-07-14 19:11:32 +0800236 p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL);
237 if (!p)
238 return ERR_PTR(-ENOMEM);
239
240 inst = (void *)(p + head);
Herbert Xu124b53d2007-04-16 20:49:20 +1000241
Herbert Xu9b8c4562015-05-21 15:10:57 +0800242 err = cryptd_init_instance(inst, alg);
243 if (err)
Herbert Xu124b53d2007-04-16 20:49:20 +1000244 goto out_free_inst;
245
Herbert Xu124b53d2007-04-16 20:49:20 +1000246out:
Herbert Xu0b535ad2009-07-14 19:11:32 +0800247 return p;
Herbert Xu124b53d2007-04-16 20:49:20 +1000248
249out_free_inst:
Herbert Xu0b535ad2009-07-14 19:11:32 +0800250 kfree(p);
251 p = ERR_PTR(err);
Herbert Xu124b53d2007-04-16 20:49:20 +1000252 goto out;
253}
254
Herbert Xu4e0958d2016-11-22 20:08:23 +0800255static int cryptd_skcipher_setkey(struct crypto_skcipher *parent,
256 const u8 *key, unsigned int keylen)
257{
258 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(parent);
Kees Cook36b38752018-09-18 19:10:52 -0700259 struct crypto_sync_skcipher *child = ctx->child;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800260 int err;
261
Kees Cook36b38752018-09-18 19:10:52 -0700262 crypto_sync_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
263 crypto_sync_skcipher_set_flags(child,
264 crypto_skcipher_get_flags(parent) &
Herbert Xu4e0958d2016-11-22 20:08:23 +0800265 CRYPTO_TFM_REQ_MASK);
Kees Cook36b38752018-09-18 19:10:52 -0700266 err = crypto_sync_skcipher_setkey(child, key, keylen);
267 crypto_skcipher_set_flags(parent,
268 crypto_sync_skcipher_get_flags(child) &
Herbert Xu4e0958d2016-11-22 20:08:23 +0800269 CRYPTO_TFM_RES_MASK);
270 return err;
271}
272
273static void cryptd_skcipher_complete(struct skcipher_request *req, int err)
274{
275 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
276 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
277 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
278 int refcnt = atomic_read(&ctx->refcnt);
279
280 local_bh_disable();
281 rctx->complete(&req->base, err);
282 local_bh_enable();
283
284 if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
285 crypto_free_skcipher(tfm);
286}
287
288static void cryptd_skcipher_encrypt(struct crypto_async_request *base,
289 int err)
290{
291 struct skcipher_request *req = skcipher_request_cast(base);
292 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
293 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
294 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
Kees Cook36b38752018-09-18 19:10:52 -0700295 struct crypto_sync_skcipher *child = ctx->child;
296 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, child);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800297
298 if (unlikely(err == -EINPROGRESS))
299 goto out;
300
Kees Cook36b38752018-09-18 19:10:52 -0700301 skcipher_request_set_sync_tfm(subreq, child);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800302 skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
303 NULL, NULL);
304 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
305 req->iv);
306
307 err = crypto_skcipher_encrypt(subreq);
308 skcipher_request_zero(subreq);
309
310 req->base.complete = rctx->complete;
311
312out:
313 cryptd_skcipher_complete(req, err);
314}
315
316static void cryptd_skcipher_decrypt(struct crypto_async_request *base,
317 int err)
318{
319 struct skcipher_request *req = skcipher_request_cast(base);
320 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
321 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
322 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
Kees Cook36b38752018-09-18 19:10:52 -0700323 struct crypto_sync_skcipher *child = ctx->child;
324 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, child);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800325
326 if (unlikely(err == -EINPROGRESS))
327 goto out;
328
Kees Cook36b38752018-09-18 19:10:52 -0700329 skcipher_request_set_sync_tfm(subreq, child);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800330 skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
331 NULL, NULL);
332 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
333 req->iv);
334
335 err = crypto_skcipher_decrypt(subreq);
336 skcipher_request_zero(subreq);
337
338 req->base.complete = rctx->complete;
339
340out:
341 cryptd_skcipher_complete(req, err);
342}
343
344static int cryptd_skcipher_enqueue(struct skcipher_request *req,
345 crypto_completion_t compl)
346{
347 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
348 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
349 struct cryptd_queue *queue;
350
351 queue = cryptd_get_queue(crypto_skcipher_tfm(tfm));
352 rctx->complete = req->base.complete;
353 req->base.complete = compl;
354
355 return cryptd_enqueue_request(queue, &req->base);
356}
357
358static int cryptd_skcipher_encrypt_enqueue(struct skcipher_request *req)
359{
360 return cryptd_skcipher_enqueue(req, cryptd_skcipher_encrypt);
361}
362
363static int cryptd_skcipher_decrypt_enqueue(struct skcipher_request *req)
364{
365 return cryptd_skcipher_enqueue(req, cryptd_skcipher_decrypt);
366}
367
368static int cryptd_skcipher_init_tfm(struct crypto_skcipher *tfm)
369{
370 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
371 struct skcipherd_instance_ctx *ictx = skcipher_instance_ctx(inst);
372 struct crypto_skcipher_spawn *spawn = &ictx->spawn;
373 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
374 struct crypto_skcipher *cipher;
375
376 cipher = crypto_spawn_skcipher(spawn);
377 if (IS_ERR(cipher))
378 return PTR_ERR(cipher);
379
Kees Cook36b38752018-09-18 19:10:52 -0700380 ctx->child = (struct crypto_sync_skcipher *)cipher;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800381 crypto_skcipher_set_reqsize(
382 tfm, sizeof(struct cryptd_skcipher_request_ctx));
383 return 0;
384}
385
386static void cryptd_skcipher_exit_tfm(struct crypto_skcipher *tfm)
387{
388 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
389
Kees Cook36b38752018-09-18 19:10:52 -0700390 crypto_free_sync_skcipher(ctx->child);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800391}
392
393static void cryptd_skcipher_free(struct skcipher_instance *inst)
394{
395 struct skcipherd_instance_ctx *ctx = skcipher_instance_ctx(inst);
396
397 crypto_drop_skcipher(&ctx->spawn);
398}
399
400static int cryptd_create_skcipher(struct crypto_template *tmpl,
401 struct rtattr **tb,
402 struct cryptd_queue *queue)
403{
404 struct skcipherd_instance_ctx *ctx;
405 struct skcipher_instance *inst;
406 struct skcipher_alg *alg;
407 const char *name;
408 u32 type;
409 u32 mask;
410 int err;
411
412 type = 0;
413 mask = CRYPTO_ALG_ASYNC;
414
415 cryptd_check_internal(tb, &type, &mask);
416
417 name = crypto_attr_alg_name(tb[1]);
418 if (IS_ERR(name))
419 return PTR_ERR(name);
420
421 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
422 if (!inst)
423 return -ENOMEM;
424
425 ctx = skcipher_instance_ctx(inst);
426 ctx->queue = queue;
427
428 crypto_set_skcipher_spawn(&ctx->spawn, skcipher_crypto_instance(inst));
429 err = crypto_grab_skcipher(&ctx->spawn, name, type, mask);
430 if (err)
431 goto out_free_inst;
432
433 alg = crypto_spawn_skcipher_alg(&ctx->spawn);
434 err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base);
435 if (err)
436 goto out_drop_skcipher;
437
438 inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
439 (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
440
441 inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg);
442 inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
443 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
444 inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
445
446 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_skcipher_ctx);
447
448 inst->alg.init = cryptd_skcipher_init_tfm;
449 inst->alg.exit = cryptd_skcipher_exit_tfm;
450
451 inst->alg.setkey = cryptd_skcipher_setkey;
452 inst->alg.encrypt = cryptd_skcipher_encrypt_enqueue;
453 inst->alg.decrypt = cryptd_skcipher_decrypt_enqueue;
454
455 inst->free = cryptd_skcipher_free;
456
457 err = skcipher_register_instance(tmpl, inst);
458 if (err) {
459out_drop_skcipher:
460 crypto_drop_skcipher(&ctx->spawn);
461out_free_inst:
462 kfree(inst);
463 }
464 return err;
465}
466
Loc Hob8a28252008-05-14 21:23:00 +0800467static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
468{
469 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
Herbert Xu46309d82009-07-12 21:38:59 +0800470 struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
471 struct crypto_shash_spawn *spawn = &ictx->spawn;
Loc Hob8a28252008-05-14 21:23:00 +0800472 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu46309d82009-07-12 21:38:59 +0800473 struct crypto_shash *hash;
Loc Hob8a28252008-05-14 21:23:00 +0800474
Herbert Xu46309d82009-07-12 21:38:59 +0800475 hash = crypto_spawn_shash(spawn);
476 if (IS_ERR(hash))
477 return PTR_ERR(hash);
Loc Hob8a28252008-05-14 21:23:00 +0800478
Herbert Xu46309d82009-07-12 21:38:59 +0800479 ctx->child = hash;
Herbert Xu0d6669e2009-07-12 23:06:33 +0800480 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
481 sizeof(struct cryptd_hash_request_ctx) +
482 crypto_shash_descsize(hash));
Loc Hob8a28252008-05-14 21:23:00 +0800483 return 0;
484}
485
486static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
487{
488 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
Loc Hob8a28252008-05-14 21:23:00 +0800489
Herbert Xu46309d82009-07-12 21:38:59 +0800490 crypto_free_shash(ctx->child);
Loc Hob8a28252008-05-14 21:23:00 +0800491}
492
493static int cryptd_hash_setkey(struct crypto_ahash *parent,
494 const u8 *key, unsigned int keylen)
495{
496 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
Herbert Xu46309d82009-07-12 21:38:59 +0800497 struct crypto_shash *child = ctx->child;
Loc Hob8a28252008-05-14 21:23:00 +0800498 int err;
499
Herbert Xu46309d82009-07-12 21:38:59 +0800500 crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
501 crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
502 CRYPTO_TFM_REQ_MASK);
503 err = crypto_shash_setkey(child, key, keylen);
504 crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) &
505 CRYPTO_TFM_RES_MASK);
Loc Hob8a28252008-05-14 21:23:00 +0800506 return err;
507}
508
509static int cryptd_hash_enqueue(struct ahash_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700510 crypto_completion_t compl)
Loc Hob8a28252008-05-14 21:23:00 +0800511{
512 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
513 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
Huang Ying254eff72009-02-19 14:42:19 +0800514 struct cryptd_queue *queue =
515 cryptd_get_queue(crypto_ahash_tfm(tfm));
Loc Hob8a28252008-05-14 21:23:00 +0800516
517 rctx->complete = req->base.complete;
Mark Rustad3e3dc252014-07-25 02:53:38 -0700518 req->base.complete = compl;
Loc Hob8a28252008-05-14 21:23:00 +0800519
Huang Ying254eff72009-02-19 14:42:19 +0800520 return cryptd_enqueue_request(queue, &req->base);
Loc Hob8a28252008-05-14 21:23:00 +0800521}
522
Herbert Xu81760ea2016-06-21 16:55:13 +0800523static void cryptd_hash_complete(struct ahash_request *req, int err)
524{
525 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
526 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
527 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
528 int refcnt = atomic_read(&ctx->refcnt);
529
530 local_bh_disable();
531 rctx->complete(&req->base, err);
532 local_bh_enable();
533
534 if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
535 crypto_free_ahash(tfm);
536}
537
Loc Hob8a28252008-05-14 21:23:00 +0800538static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
539{
Herbert Xu46309d82009-07-12 21:38:59 +0800540 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
541 struct crypto_shash *child = ctx->child;
542 struct ahash_request *req = ahash_request_cast(req_async);
543 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
544 struct shash_desc *desc = &rctx->desc;
Loc Hob8a28252008-05-14 21:23:00 +0800545
546 if (unlikely(err == -EINPROGRESS))
547 goto out;
548
Herbert Xu46309d82009-07-12 21:38:59 +0800549 desc->tfm = child;
Loc Hob8a28252008-05-14 21:23:00 +0800550
Herbert Xu46309d82009-07-12 21:38:59 +0800551 err = crypto_shash_init(desc);
Loc Hob8a28252008-05-14 21:23:00 +0800552
553 req->base.complete = rctx->complete;
554
555out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800556 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800557}
558
559static int cryptd_hash_init_enqueue(struct ahash_request *req)
560{
561 return cryptd_hash_enqueue(req, cryptd_hash_init);
562}
563
564static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
565{
Herbert Xu46309d82009-07-12 21:38:59 +0800566 struct ahash_request *req = ahash_request_cast(req_async);
Loc Hob8a28252008-05-14 21:23:00 +0800567 struct cryptd_hash_request_ctx *rctx;
Loc Hob8a28252008-05-14 21:23:00 +0800568
569 rctx = ahash_request_ctx(req);
570
571 if (unlikely(err == -EINPROGRESS))
572 goto out;
573
Herbert Xu46309d82009-07-12 21:38:59 +0800574 err = shash_ahash_update(req, &rctx->desc);
Loc Hob8a28252008-05-14 21:23:00 +0800575
576 req->base.complete = rctx->complete;
577
578out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800579 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800580}
581
582static int cryptd_hash_update_enqueue(struct ahash_request *req)
583{
584 return cryptd_hash_enqueue(req, cryptd_hash_update);
585}
586
587static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
588{
Herbert Xu46309d82009-07-12 21:38:59 +0800589 struct ahash_request *req = ahash_request_cast(req_async);
590 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
Loc Hob8a28252008-05-14 21:23:00 +0800591
592 if (unlikely(err == -EINPROGRESS))
593 goto out;
594
Herbert Xu46309d82009-07-12 21:38:59 +0800595 err = crypto_shash_final(&rctx->desc, req->result);
Loc Hob8a28252008-05-14 21:23:00 +0800596
597 req->base.complete = rctx->complete;
598
599out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800600 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800601}
602
603static int cryptd_hash_final_enqueue(struct ahash_request *req)
604{
605 return cryptd_hash_enqueue(req, cryptd_hash_final);
606}
607
Herbert Xu6fba00d2009-07-22 11:10:22 +0800608static void cryptd_hash_finup(struct crypto_async_request *req_async, int err)
609{
610 struct ahash_request *req = ahash_request_cast(req_async);
611 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
612
613 if (unlikely(err == -EINPROGRESS))
614 goto out;
615
616 err = shash_ahash_finup(req, &rctx->desc);
617
618 req->base.complete = rctx->complete;
619
620out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800621 cryptd_hash_complete(req, err);
Herbert Xu6fba00d2009-07-22 11:10:22 +0800622}
623
624static int cryptd_hash_finup_enqueue(struct ahash_request *req)
625{
626 return cryptd_hash_enqueue(req, cryptd_hash_finup);
627}
628
Loc Hob8a28252008-05-14 21:23:00 +0800629static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
630{
Herbert Xu46309d82009-07-12 21:38:59 +0800631 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
632 struct crypto_shash *child = ctx->child;
633 struct ahash_request *req = ahash_request_cast(req_async);
634 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
635 struct shash_desc *desc = &rctx->desc;
Loc Hob8a28252008-05-14 21:23:00 +0800636
637 if (unlikely(err == -EINPROGRESS))
638 goto out;
639
Herbert Xu46309d82009-07-12 21:38:59 +0800640 desc->tfm = child;
Loc Hob8a28252008-05-14 21:23:00 +0800641
Herbert Xu46309d82009-07-12 21:38:59 +0800642 err = shash_ahash_digest(req, desc);
Loc Hob8a28252008-05-14 21:23:00 +0800643
644 req->base.complete = rctx->complete;
645
646out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800647 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800648}
649
650static int cryptd_hash_digest_enqueue(struct ahash_request *req)
651{
652 return cryptd_hash_enqueue(req, cryptd_hash_digest);
653}
654
Herbert Xu6fba00d2009-07-22 11:10:22 +0800655static int cryptd_hash_export(struct ahash_request *req, void *out)
656{
657 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
658
659 return crypto_shash_export(&rctx->desc, out);
660}
661
662static int cryptd_hash_import(struct ahash_request *req, const void *in)
663{
Ard Biesheuvel0bd22232016-09-01 14:25:43 +0100664 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
665 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
666 struct shash_desc *desc = cryptd_shash_desc(req);
Herbert Xu6fba00d2009-07-22 11:10:22 +0800667
Ard Biesheuvel0bd22232016-09-01 14:25:43 +0100668 desc->tfm = ctx->child;
Ard Biesheuvel0bd22232016-09-01 14:25:43 +0100669
670 return crypto_shash_import(desc, in);
Herbert Xu6fba00d2009-07-22 11:10:22 +0800671}
672
Herbert Xu9cd899a2009-07-14 18:45:45 +0800673static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
674 struct cryptd_queue *queue)
Loc Hob8a28252008-05-14 21:23:00 +0800675{
Herbert Xu46309d82009-07-12 21:38:59 +0800676 struct hashd_instance_ctx *ctx;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800677 struct ahash_instance *inst;
Herbert Xu46309d82009-07-12 21:38:59 +0800678 struct shash_alg *salg;
Loc Hob8a28252008-05-14 21:23:00 +0800679 struct crypto_alg *alg;
Stephan Mueller466a7b92015-03-30 21:57:06 +0200680 u32 type = 0;
681 u32 mask = 0;
Herbert Xu46309d82009-07-12 21:38:59 +0800682 int err;
Loc Hob8a28252008-05-14 21:23:00 +0800683
Stephan Mueller466a7b92015-03-30 21:57:06 +0200684 cryptd_check_internal(tb, &type, &mask);
685
686 salg = shash_attr_alg(tb[1], type, mask);
Herbert Xu46309d82009-07-12 21:38:59 +0800687 if (IS_ERR(salg))
Herbert Xu9cd899a2009-07-14 18:45:45 +0800688 return PTR_ERR(salg);
Loc Hob8a28252008-05-14 21:23:00 +0800689
Herbert Xu46309d82009-07-12 21:38:59 +0800690 alg = &salg->base;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800691 inst = cryptd_alloc_instance(alg, ahash_instance_headroom(),
692 sizeof(*ctx));
Steffen Klassert05ed8752009-07-15 16:51:04 +0800693 err = PTR_ERR(inst);
Loc Hob8a28252008-05-14 21:23:00 +0800694 if (IS_ERR(inst))
695 goto out_put_alg;
696
Herbert Xu0b535ad2009-07-14 19:11:32 +0800697 ctx = ahash_instance_ctx(inst);
Herbert Xu46309d82009-07-12 21:38:59 +0800698 ctx->queue = queue;
699
Herbert Xu0b535ad2009-07-14 19:11:32 +0800700 err = crypto_init_shash_spawn(&ctx->spawn, salg,
701 ahash_crypto_instance(inst));
Herbert Xu46309d82009-07-12 21:38:59 +0800702 if (err)
703 goto out_free_inst;
704
Eric Biggersa208fa82018-01-03 11:16:26 -0800705 inst->alg.halg.base.cra_flags = CRYPTO_ALG_ASYNC |
706 (alg->cra_flags & (CRYPTO_ALG_INTERNAL |
707 CRYPTO_ALG_OPTIONAL_KEY));
Loc Hob8a28252008-05-14 21:23:00 +0800708
Herbert Xu0b535ad2009-07-14 19:11:32 +0800709 inst->alg.halg.digestsize = salg->digestsize;
Wang, Rui Y1a078342015-11-29 22:45:34 +0800710 inst->alg.halg.statesize = salg->statesize;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800711 inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
Loc Hob8a28252008-05-14 21:23:00 +0800712
Herbert Xu0b535ad2009-07-14 19:11:32 +0800713 inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
714 inst->alg.halg.base.cra_exit = cryptd_hash_exit_tfm;
Loc Hob8a28252008-05-14 21:23:00 +0800715
Herbert Xu0b535ad2009-07-14 19:11:32 +0800716 inst->alg.init = cryptd_hash_init_enqueue;
717 inst->alg.update = cryptd_hash_update_enqueue;
718 inst->alg.final = cryptd_hash_final_enqueue;
Herbert Xu6fba00d2009-07-22 11:10:22 +0800719 inst->alg.finup = cryptd_hash_finup_enqueue;
720 inst->alg.export = cryptd_hash_export;
721 inst->alg.import = cryptd_hash_import;
Eric Biggers841a3ff2018-01-03 11:16:23 -0800722 if (crypto_shash_alg_has_setkey(salg))
723 inst->alg.setkey = cryptd_hash_setkey;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800724 inst->alg.digest = cryptd_hash_digest_enqueue;
Loc Hob8a28252008-05-14 21:23:00 +0800725
Herbert Xu0b535ad2009-07-14 19:11:32 +0800726 err = ahash_register_instance(tmpl, inst);
Herbert Xu9cd899a2009-07-14 18:45:45 +0800727 if (err) {
728 crypto_drop_shash(&ctx->spawn);
729out_free_inst:
730 kfree(inst);
731 }
732
Loc Hob8a28252008-05-14 21:23:00 +0800733out_put_alg:
734 crypto_mod_put(alg);
Herbert Xu9cd899a2009-07-14 18:45:45 +0800735 return err;
Loc Hob8a28252008-05-14 21:23:00 +0800736}
737
Herbert Xu92b98762015-05-28 22:08:01 +0800738static int cryptd_aead_setkey(struct crypto_aead *parent,
739 const u8 *key, unsigned int keylen)
740{
741 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
742 struct crypto_aead *child = ctx->child;
743
744 return crypto_aead_setkey(child, key, keylen);
745}
746
747static int cryptd_aead_setauthsize(struct crypto_aead *parent,
748 unsigned int authsize)
749{
750 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
751 struct crypto_aead *child = ctx->child;
752
753 return crypto_aead_setauthsize(child, authsize);
754}
755
Adrian Hoban298c9262010-09-20 16:05:12 +0800756static void cryptd_aead_crypt(struct aead_request *req,
757 struct crypto_aead *child,
758 int err,
759 int (*crypt)(struct aead_request *req))
760{
761 struct cryptd_aead_request_ctx *rctx;
Herbert Xu81760ea2016-06-21 16:55:13 +0800762 struct cryptd_aead_ctx *ctx;
Herbert Xuec9f2002015-07-06 19:11:03 +0800763 crypto_completion_t compl;
Herbert Xu81760ea2016-06-21 16:55:13 +0800764 struct crypto_aead *tfm;
765 int refcnt;
Herbert Xuec9f2002015-07-06 19:11:03 +0800766
Adrian Hoban298c9262010-09-20 16:05:12 +0800767 rctx = aead_request_ctx(req);
Herbert Xuec9f2002015-07-06 19:11:03 +0800768 compl = rctx->complete;
Adrian Hoban298c9262010-09-20 16:05:12 +0800769
Herbert Xu31bd44e2016-08-25 16:49:51 +0800770 tfm = crypto_aead_reqtfm(req);
771
Adrian Hoban298c9262010-09-20 16:05:12 +0800772 if (unlikely(err == -EINPROGRESS))
773 goto out;
774 aead_request_set_tfm(req, child);
775 err = crypt( req );
Herbert Xu81760ea2016-06-21 16:55:13 +0800776
Adrian Hoban298c9262010-09-20 16:05:12 +0800777out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800778 ctx = crypto_aead_ctx(tfm);
779 refcnt = atomic_read(&ctx->refcnt);
780
Adrian Hoban298c9262010-09-20 16:05:12 +0800781 local_bh_disable();
Herbert Xuec9f2002015-07-06 19:11:03 +0800782 compl(&req->base, err);
Adrian Hoban298c9262010-09-20 16:05:12 +0800783 local_bh_enable();
Herbert Xu81760ea2016-06-21 16:55:13 +0800784
785 if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
786 crypto_free_aead(tfm);
Adrian Hoban298c9262010-09-20 16:05:12 +0800787}
788
789static void cryptd_aead_encrypt(struct crypto_async_request *areq, int err)
790{
791 struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
792 struct crypto_aead *child = ctx->child;
793 struct aead_request *req;
794
795 req = container_of(areq, struct aead_request, base);
Herbert Xuba3749a2015-08-13 17:29:02 +0800796 cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->encrypt);
Adrian Hoban298c9262010-09-20 16:05:12 +0800797}
798
799static void cryptd_aead_decrypt(struct crypto_async_request *areq, int err)
800{
801 struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
802 struct crypto_aead *child = ctx->child;
803 struct aead_request *req;
804
805 req = container_of(areq, struct aead_request, base);
Herbert Xuba3749a2015-08-13 17:29:02 +0800806 cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->decrypt);
Adrian Hoban298c9262010-09-20 16:05:12 +0800807}
808
809static int cryptd_aead_enqueue(struct aead_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700810 crypto_completion_t compl)
Adrian Hoban298c9262010-09-20 16:05:12 +0800811{
812 struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req);
813 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
814 struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm));
815
816 rctx->complete = req->base.complete;
Mark Rustad3e3dc252014-07-25 02:53:38 -0700817 req->base.complete = compl;
Adrian Hoban298c9262010-09-20 16:05:12 +0800818 return cryptd_enqueue_request(queue, &req->base);
819}
820
821static int cryptd_aead_encrypt_enqueue(struct aead_request *req)
822{
823 return cryptd_aead_enqueue(req, cryptd_aead_encrypt );
824}
825
826static int cryptd_aead_decrypt_enqueue(struct aead_request *req)
827{
828 return cryptd_aead_enqueue(req, cryptd_aead_decrypt );
829}
830
Herbert Xuf614e542015-05-28 22:08:04 +0800831static int cryptd_aead_init_tfm(struct crypto_aead *tfm)
Adrian Hoban298c9262010-09-20 16:05:12 +0800832{
Herbert Xuf614e542015-05-28 22:08:04 +0800833 struct aead_instance *inst = aead_alg_instance(tfm);
834 struct aead_instance_ctx *ictx = aead_instance_ctx(inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800835 struct crypto_aead_spawn *spawn = &ictx->aead_spawn;
Herbert Xuf614e542015-05-28 22:08:04 +0800836 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
Adrian Hoban298c9262010-09-20 16:05:12 +0800837 struct crypto_aead *cipher;
838
839 cipher = crypto_spawn_aead(spawn);
840 if (IS_ERR(cipher))
841 return PTR_ERR(cipher);
842
Adrian Hoban298c9262010-09-20 16:05:12 +0800843 ctx->child = cipher;
Herbert Xuec9f2002015-07-06 19:11:03 +0800844 crypto_aead_set_reqsize(
845 tfm, max((unsigned)sizeof(struct cryptd_aead_request_ctx),
846 crypto_aead_reqsize(cipher)));
Adrian Hoban298c9262010-09-20 16:05:12 +0800847 return 0;
848}
849
Herbert Xuf614e542015-05-28 22:08:04 +0800850static void cryptd_aead_exit_tfm(struct crypto_aead *tfm)
Adrian Hoban298c9262010-09-20 16:05:12 +0800851{
Herbert Xuf614e542015-05-28 22:08:04 +0800852 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
Adrian Hoban298c9262010-09-20 16:05:12 +0800853 crypto_free_aead(ctx->child);
854}
855
856static int cryptd_create_aead(struct crypto_template *tmpl,
857 struct rtattr **tb,
858 struct cryptd_queue *queue)
859{
860 struct aead_instance_ctx *ctx;
Herbert Xuf614e542015-05-28 22:08:04 +0800861 struct aead_instance *inst;
862 struct aead_alg *alg;
Herbert Xu9b8c4562015-05-21 15:10:57 +0800863 const char *name;
864 u32 type = 0;
Herbert Xuec9f2002015-07-06 19:11:03 +0800865 u32 mask = CRYPTO_ALG_ASYNC;
Adrian Hoban298c9262010-09-20 16:05:12 +0800866 int err;
867
Stephan Mueller466a7b92015-03-30 21:57:06 +0200868 cryptd_check_internal(tb, &type, &mask);
869
Herbert Xu9b8c4562015-05-21 15:10:57 +0800870 name = crypto_attr_alg_name(tb[1]);
871 if (IS_ERR(name))
872 return PTR_ERR(name);
Adrian Hoban298c9262010-09-20 16:05:12 +0800873
Herbert Xu9b8c4562015-05-21 15:10:57 +0800874 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
875 if (!inst)
876 return -ENOMEM;
Adrian Hoban298c9262010-09-20 16:05:12 +0800877
Herbert Xuf614e542015-05-28 22:08:04 +0800878 ctx = aead_instance_ctx(inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800879 ctx->queue = queue;
880
Herbert Xuf614e542015-05-28 22:08:04 +0800881 crypto_set_aead_spawn(&ctx->aead_spawn, aead_crypto_instance(inst));
Herbert Xu9b8c4562015-05-21 15:10:57 +0800882 err = crypto_grab_aead(&ctx->aead_spawn, name, type, mask);
Adrian Hoban298c9262010-09-20 16:05:12 +0800883 if (err)
884 goto out_free_inst;
885
Herbert Xuf614e542015-05-28 22:08:04 +0800886 alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
887 err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
Herbert Xu9b8c4562015-05-21 15:10:57 +0800888 if (err)
889 goto out_drop_aead;
890
Herbert Xuf614e542015-05-28 22:08:04 +0800891 inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC |
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800892 (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
Herbert Xuf614e542015-05-28 22:08:04 +0800893 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx);
Adrian Hoban298c9262010-09-20 16:05:12 +0800894
Herbert Xuf614e542015-05-28 22:08:04 +0800895 inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
896 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
897
898 inst->alg.init = cryptd_aead_init_tfm;
899 inst->alg.exit = cryptd_aead_exit_tfm;
900 inst->alg.setkey = cryptd_aead_setkey;
901 inst->alg.setauthsize = cryptd_aead_setauthsize;
902 inst->alg.encrypt = cryptd_aead_encrypt_enqueue;
903 inst->alg.decrypt = cryptd_aead_decrypt_enqueue;
904
905 err = aead_register_instance(tmpl, inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800906 if (err) {
Herbert Xu9b8c4562015-05-21 15:10:57 +0800907out_drop_aead:
908 crypto_drop_aead(&ctx->aead_spawn);
Adrian Hoban298c9262010-09-20 16:05:12 +0800909out_free_inst:
910 kfree(inst);
911 }
Adrian Hoban298c9262010-09-20 16:05:12 +0800912 return err;
913}
914
Huang Ying254eff72009-02-19 14:42:19 +0800915static struct cryptd_queue queue;
Herbert Xu124b53d2007-04-16 20:49:20 +1000916
Herbert Xu9cd899a2009-07-14 18:45:45 +0800917static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu124b53d2007-04-16 20:49:20 +1000918{
919 struct crypto_attr_type *algt;
920
921 algt = crypto_get_attr_type(tb);
922 if (IS_ERR(algt))
Herbert Xu9cd899a2009-07-14 18:45:45 +0800923 return PTR_ERR(algt);
Herbert Xu124b53d2007-04-16 20:49:20 +1000924
925 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
926 case CRYPTO_ALG_TYPE_BLKCIPHER:
Herbert Xu4e0958d2016-11-22 20:08:23 +0800927 return cryptd_create_skcipher(tmpl, tb, &queue);
Loc Hob8a28252008-05-14 21:23:00 +0800928 case CRYPTO_ALG_TYPE_DIGEST:
Herbert Xu9cd899a2009-07-14 18:45:45 +0800929 return cryptd_create_hash(tmpl, tb, &queue);
Adrian Hoban298c9262010-09-20 16:05:12 +0800930 case CRYPTO_ALG_TYPE_AEAD:
931 return cryptd_create_aead(tmpl, tb, &queue);
Herbert Xu124b53d2007-04-16 20:49:20 +1000932 }
933
Herbert Xu9cd899a2009-07-14 18:45:45 +0800934 return -EINVAL;
Herbert Xu124b53d2007-04-16 20:49:20 +1000935}
936
937static void cryptd_free(struct crypto_instance *inst)
938{
939 struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst);
Herbert Xu0b535ad2009-07-14 19:11:32 +0800940 struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800941 struct aead_instance_ctx *aead_ctx = crypto_instance_ctx(inst);
Herbert Xu0b535ad2009-07-14 19:11:32 +0800942
943 switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) {
944 case CRYPTO_ALG_TYPE_AHASH:
945 crypto_drop_shash(&hctx->spawn);
946 kfree(ahash_instance(inst));
947 return;
Adrian Hoban298c9262010-09-20 16:05:12 +0800948 case CRYPTO_ALG_TYPE_AEAD:
Herbert Xuf614e542015-05-28 22:08:04 +0800949 crypto_drop_aead(&aead_ctx->aead_spawn);
950 kfree(aead_instance(inst));
Adrian Hoban298c9262010-09-20 16:05:12 +0800951 return;
952 default:
953 crypto_drop_spawn(&ctx->spawn);
954 kfree(inst);
Herbert Xu0b535ad2009-07-14 19:11:32 +0800955 }
Herbert Xu124b53d2007-04-16 20:49:20 +1000956}
957
958static struct crypto_template cryptd_tmpl = {
959 .name = "cryptd",
Herbert Xu9cd899a2009-07-14 18:45:45 +0800960 .create = cryptd_create,
Herbert Xu124b53d2007-04-16 20:49:20 +1000961 .free = cryptd_free,
962 .module = THIS_MODULE,
963};
964
Herbert Xu4e0958d2016-11-22 20:08:23 +0800965struct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name,
966 u32 type, u32 mask)
967{
968 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
969 struct cryptd_skcipher_ctx *ctx;
970 struct crypto_skcipher *tfm;
971
972 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
973 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
974 return ERR_PTR(-EINVAL);
975
976 tfm = crypto_alloc_skcipher(cryptd_alg_name, type, mask);
977 if (IS_ERR(tfm))
978 return ERR_CAST(tfm);
979
980 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
981 crypto_free_skcipher(tfm);
982 return ERR_PTR(-EINVAL);
983 }
984
985 ctx = crypto_skcipher_ctx(tfm);
986 atomic_set(&ctx->refcnt, 1);
987
988 return container_of(tfm, struct cryptd_skcipher, base);
989}
990EXPORT_SYMBOL_GPL(cryptd_alloc_skcipher);
991
992struct crypto_skcipher *cryptd_skcipher_child(struct cryptd_skcipher *tfm)
993{
994 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
995
Kees Cook36b38752018-09-18 19:10:52 -0700996 return &ctx->child->base;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800997}
998EXPORT_SYMBOL_GPL(cryptd_skcipher_child);
999
1000bool cryptd_skcipher_queued(struct cryptd_skcipher *tfm)
1001{
1002 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
1003
1004 return atomic_read(&ctx->refcnt) - 1;
1005}
1006EXPORT_SYMBOL_GPL(cryptd_skcipher_queued);
1007
1008void cryptd_free_skcipher(struct cryptd_skcipher *tfm)
1009{
1010 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
1011
1012 if (atomic_dec_and_test(&ctx->refcnt))
1013 crypto_free_skcipher(&tfm->base);
1014}
1015EXPORT_SYMBOL_GPL(cryptd_free_skcipher);
1016
Huang Yingace13662009-08-06 15:35:20 +10001017struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name,
1018 u32 type, u32 mask)
1019{
1020 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu81760ea2016-06-21 16:55:13 +08001021 struct cryptd_hash_ctx *ctx;
Huang Yingace13662009-08-06 15:35:20 +10001022 struct crypto_ahash *tfm;
1023
1024 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
1025 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
1026 return ERR_PTR(-EINVAL);
1027 tfm = crypto_alloc_ahash(cryptd_alg_name, type, mask);
1028 if (IS_ERR(tfm))
1029 return ERR_CAST(tfm);
1030 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
1031 crypto_free_ahash(tfm);
1032 return ERR_PTR(-EINVAL);
1033 }
1034
Herbert Xu81760ea2016-06-21 16:55:13 +08001035 ctx = crypto_ahash_ctx(tfm);
1036 atomic_set(&ctx->refcnt, 1);
1037
Huang Yingace13662009-08-06 15:35:20 +10001038 return __cryptd_ahash_cast(tfm);
1039}
1040EXPORT_SYMBOL_GPL(cryptd_alloc_ahash);
1041
1042struct crypto_shash *cryptd_ahash_child(struct cryptd_ahash *tfm)
1043{
1044 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
1045
1046 return ctx->child;
1047}
1048EXPORT_SYMBOL_GPL(cryptd_ahash_child);
1049
Huang Ying0e1227d2009-10-19 11:53:06 +09001050struct shash_desc *cryptd_shash_desc(struct ahash_request *req)
1051{
1052 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
1053 return &rctx->desc;
1054}
1055EXPORT_SYMBOL_GPL(cryptd_shash_desc);
1056
Herbert Xu81760ea2016-06-21 16:55:13 +08001057bool cryptd_ahash_queued(struct cryptd_ahash *tfm)
1058{
1059 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
1060
1061 return atomic_read(&ctx->refcnt) - 1;
1062}
1063EXPORT_SYMBOL_GPL(cryptd_ahash_queued);
1064
Huang Yingace13662009-08-06 15:35:20 +10001065void cryptd_free_ahash(struct cryptd_ahash *tfm)
1066{
Herbert Xu81760ea2016-06-21 16:55:13 +08001067 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
1068
1069 if (atomic_dec_and_test(&ctx->refcnt))
1070 crypto_free_ahash(&tfm->base);
Huang Yingace13662009-08-06 15:35:20 +10001071}
1072EXPORT_SYMBOL_GPL(cryptd_free_ahash);
1073
Adrian Hoban298c9262010-09-20 16:05:12 +08001074struct cryptd_aead *cryptd_alloc_aead(const char *alg_name,
1075 u32 type, u32 mask)
1076{
1077 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu81760ea2016-06-21 16:55:13 +08001078 struct cryptd_aead_ctx *ctx;
Adrian Hoban298c9262010-09-20 16:05:12 +08001079 struct crypto_aead *tfm;
1080
1081 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
1082 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
1083 return ERR_PTR(-EINVAL);
1084 tfm = crypto_alloc_aead(cryptd_alg_name, type, mask);
1085 if (IS_ERR(tfm))
1086 return ERR_CAST(tfm);
1087 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
1088 crypto_free_aead(tfm);
1089 return ERR_PTR(-EINVAL);
1090 }
Herbert Xu81760ea2016-06-21 16:55:13 +08001091
1092 ctx = crypto_aead_ctx(tfm);
1093 atomic_set(&ctx->refcnt, 1);
1094
Adrian Hoban298c9262010-09-20 16:05:12 +08001095 return __cryptd_aead_cast(tfm);
1096}
1097EXPORT_SYMBOL_GPL(cryptd_alloc_aead);
1098
1099struct crypto_aead *cryptd_aead_child(struct cryptd_aead *tfm)
1100{
1101 struct cryptd_aead_ctx *ctx;
1102 ctx = crypto_aead_ctx(&tfm->base);
1103 return ctx->child;
1104}
1105EXPORT_SYMBOL_GPL(cryptd_aead_child);
1106
Herbert Xu81760ea2016-06-21 16:55:13 +08001107bool cryptd_aead_queued(struct cryptd_aead *tfm)
1108{
1109 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
1110
1111 return atomic_read(&ctx->refcnt) - 1;
1112}
1113EXPORT_SYMBOL_GPL(cryptd_aead_queued);
1114
Adrian Hoban298c9262010-09-20 16:05:12 +08001115void cryptd_free_aead(struct cryptd_aead *tfm)
1116{
Herbert Xu81760ea2016-06-21 16:55:13 +08001117 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
1118
1119 if (atomic_dec_and_test(&ctx->refcnt))
1120 crypto_free_aead(&tfm->base);
Adrian Hoban298c9262010-09-20 16:05:12 +08001121}
1122EXPORT_SYMBOL_GPL(cryptd_free_aead);
1123
Herbert Xu124b53d2007-04-16 20:49:20 +10001124static int __init cryptd_init(void)
1125{
1126 int err;
1127
Eric Biggers3e56e162019-05-20 09:53:58 -07001128 cryptd_wq = alloc_workqueue("cryptd", WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE,
1129 1);
1130 if (!cryptd_wq)
1131 return -ENOMEM;
1132
Jon Maxwellc3a53602017-11-22 16:08:17 +11001133 err = cryptd_init_queue(&queue, cryptd_max_cpu_qlen);
Herbert Xu124b53d2007-04-16 20:49:20 +10001134 if (err)
Eric Biggers3e56e162019-05-20 09:53:58 -07001135 goto err_destroy_wq;
Herbert Xu124b53d2007-04-16 20:49:20 +10001136
1137 err = crypto_register_template(&cryptd_tmpl);
1138 if (err)
Eric Biggers3e56e162019-05-20 09:53:58 -07001139 goto err_fini_queue;
Herbert Xu124b53d2007-04-16 20:49:20 +10001140
Eric Biggers3e56e162019-05-20 09:53:58 -07001141 return 0;
1142
1143err_fini_queue:
1144 cryptd_fini_queue(&queue);
1145err_destroy_wq:
1146 destroy_workqueue(cryptd_wq);
Herbert Xu124b53d2007-04-16 20:49:20 +10001147 return err;
1148}
1149
1150static void __exit cryptd_exit(void)
1151{
Eric Biggers3e56e162019-05-20 09:53:58 -07001152 destroy_workqueue(cryptd_wq);
Huang Ying254eff72009-02-19 14:42:19 +08001153 cryptd_fini_queue(&queue);
Herbert Xu124b53d2007-04-16 20:49:20 +10001154 crypto_unregister_template(&cryptd_tmpl);
1155}
1156
Herbert Xub2bac6a2011-08-19 16:11:23 +08001157subsys_initcall(cryptd_init);
Herbert Xu124b53d2007-04-16 20:49:20 +10001158module_exit(cryptd_exit);
1159
1160MODULE_LICENSE("GPL");
1161MODULE_DESCRIPTION("Software async crypto daemon");
Kees Cook4943ba12014-11-24 16:32:38 -08001162MODULE_ALIAS_CRYPTO("cryptd");