blob: ca3a40fc7da911816b956e9aa37d6534592c493d [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xu124b53d2007-04-16 20:49:20 +10002/*
3 * Software async crypto daemon.
4 *
5 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
6 *
Adrian Hoban298c9262010-09-20 16:05:12 +08007 * Added AEAD support to cryptd.
8 * Authors: Tadeusz Struk (tadeusz.struk@intel.com)
9 * Adrian Hoban <adrian.hoban@intel.com>
10 * Gabriele Paoloni <gabriele.paoloni@intel.com>
11 * Aidan O'Mahony (aidan.o.mahony@intel.com)
12 * Copyright (c) 2010, Intel Corporation.
Herbert Xu124b53d2007-04-16 20:49:20 +100013 */
14
Herbert Xu18e33e62008-07-10 16:01:22 +080015#include <crypto/internal/hash.h>
Adrian Hoban298c9262010-09-20 16:05:12 +080016#include <crypto/internal/aead.h>
Herbert Xu4e0958d2016-11-22 20:08:23 +080017#include <crypto/internal/skcipher.h>
Huang Ying1cac2cb2009-01-18 16:19:46 +110018#include <crypto/cryptd.h>
Chuhong Yuan43b970f2019-08-08 16:00:22 +080019#include <linux/refcount.h>
Herbert Xu124b53d2007-04-16 20:49:20 +100020#include <linux/err.h>
21#include <linux/init.h>
22#include <linux/kernel.h>
Herbert Xu124b53d2007-04-16 20:49:20 +100023#include <linux/list.h>
24#include <linux/module.h>
Herbert Xu124b53d2007-04-16 20:49:20 +100025#include <linux/scatterlist.h>
26#include <linux/sched.h>
27#include <linux/slab.h>
Eric Biggers3e56e162019-05-20 09:53:58 -070028#include <linux/workqueue.h>
Herbert Xu124b53d2007-04-16 20:49:20 +100029
Colin Ian Kingeaf356e2017-11-30 11:26:14 +000030static unsigned int cryptd_max_cpu_qlen = 1000;
Jon Maxwellc3a53602017-11-22 16:08:17 +110031module_param(cryptd_max_cpu_qlen, uint, 0);
32MODULE_PARM_DESC(cryptd_max_cpu_qlen, "Set cryptd Max queue depth");
Herbert Xu124b53d2007-04-16 20:49:20 +100033
Eric Biggers3e56e162019-05-20 09:53:58 -070034static struct workqueue_struct *cryptd_wq;
35
Huang Ying254eff72009-02-19 14:42:19 +080036struct cryptd_cpu_queue {
Herbert Xu124b53d2007-04-16 20:49:20 +100037 struct crypto_queue queue;
Huang Ying254eff72009-02-19 14:42:19 +080038 struct work_struct work;
39};
40
41struct cryptd_queue {
Sebastian Andrzej Siewior2a304462022-05-04 17:07:36 +020042 /*
43 * Protected by disabling BH to allow enqueueing from softinterrupt and
44 * dequeuing from kworker (cryptd_queue_worker()).
45 */
Tejun Heoa29d8b82010-02-02 14:39:15 +090046 struct cryptd_cpu_queue __percpu *cpu_queue;
Herbert Xu124b53d2007-04-16 20:49:20 +100047};
48
49struct cryptd_instance_ctx {
50 struct crypto_spawn spawn;
Huang Ying254eff72009-02-19 14:42:19 +080051 struct cryptd_queue *queue;
Herbert Xu124b53d2007-04-16 20:49:20 +100052};
53
Herbert Xu4e0958d2016-11-22 20:08:23 +080054struct skcipherd_instance_ctx {
55 struct crypto_skcipher_spawn spawn;
56 struct cryptd_queue *queue;
57};
58
Herbert Xu46309d82009-07-12 21:38:59 +080059struct hashd_instance_ctx {
60 struct crypto_shash_spawn spawn;
61 struct cryptd_queue *queue;
62};
63
Adrian Hoban298c9262010-09-20 16:05:12 +080064struct aead_instance_ctx {
65 struct crypto_aead_spawn aead_spawn;
66 struct cryptd_queue *queue;
67};
68
Herbert Xu4e0958d2016-11-22 20:08:23 +080069struct cryptd_skcipher_ctx {
Chuhong Yuan43b970f2019-08-08 16:00:22 +080070 refcount_t refcnt;
Herbert Xu442caec2022-11-11 17:59:17 +080071 struct crypto_skcipher *child;
Herbert Xu4e0958d2016-11-22 20:08:23 +080072};
73
74struct cryptd_skcipher_request_ctx {
75 crypto_completion_t complete;
Herbert Xu442caec2022-11-11 17:59:17 +080076 struct skcipher_request req;
Herbert Xu4e0958d2016-11-22 20:08:23 +080077};
78
Loc Hob8a28252008-05-14 21:23:00 +080079struct cryptd_hash_ctx {
Chuhong Yuan43b970f2019-08-08 16:00:22 +080080 refcount_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 {
Chuhong Yuan43b970f2019-08-08 16:00:22 +080090 refcount_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{
Sebastian Andrzej Siewior2a304462022-05-04 17:07:36 +0200133 int err;
Huang Ying254eff72009-02-19 14:42:19 +0800134 struct cryptd_cpu_queue *cpu_queue;
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800135 refcount_t *refcnt;
Huang Ying254eff72009-02-19 14:42:19 +0800136
Sebastian Andrzej Siewior2a304462022-05-04 17:07:36 +0200137 local_bh_disable();
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)
Sebastian Andrzej Siewior2a304462022-05-04 17:07:36 +0200144 goto out;
Herbert Xu81760ea2016-06-21 16:55:13 +0800145
Sebastian Andrzej Siewior2a304462022-05-04 17:07:36 +0200146 queue_work_on(smp_processor_id(), cryptd_wq, &cpu_queue->work);
Herbert Xu81760ea2016-06-21 16:55:13 +0800147
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800148 if (!refcount_read(refcnt))
Sebastian Andrzej Siewior2a304462022-05-04 17:07:36 +0200149 goto out;
Herbert Xu81760ea2016-06-21 16:55:13 +0800150
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800151 refcount_inc(refcnt);
Herbert Xu81760ea2016-06-21 16:55:13 +0800152
Sebastian Andrzej Siewior2a304462022-05-04 17:07:36 +0200153out:
154 local_bh_enable();
Huang Ying254eff72009-02-19 14:42:19 +0800155
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.
Jussi Kivilinna9efade12012-10-21 20:42:28 +0300170 */
171 local_bh_disable();
Huang Ying254eff72009-02-19 14:42:19 +0800172 backlog = crypto_get_backlog(&cpu_queue->queue);
173 req = crypto_dequeue_request(&cpu_queue->queue);
Jussi Kivilinna9efade12012-10-21 20:42:28 +0300174 local_bh_enable();
Huang Ying254eff72009-02-19 14:42:19 +0800175
176 if (!req)
177 return;
178
179 if (backlog)
180 backlog->complete(backlog, -EINPROGRESS);
181 req->complete(req, 0);
182
183 if (cpu_queue->queue.qlen)
Eric Biggers3e56e162019-05-20 09:53:58 -0700184 queue_work(cryptd_wq, &cpu_queue->work);
Huang Ying254eff72009-02-19 14:42:19 +0800185}
186
187static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
Herbert Xu124b53d2007-04-16 20:49:20 +1000188{
189 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
190 struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
Huang Ying254eff72009-02-19 14:42:19 +0800191 return ictx->queue;
Herbert Xu124b53d2007-04-16 20:49:20 +1000192}
193
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700194static void cryptd_type_and_mask(struct crypto_attr_type *algt,
195 u32 *type, u32 *mask)
Stephan Mueller466a7b92015-03-30 21:57:06 +0200196{
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700197 /*
198 * cryptd is allowed to wrap internal algorithms, but in that case the
199 * resulting cryptd instance will be marked as internal as well.
200 */
201 *type = algt->type & CRYPTO_ALG_INTERNAL;
202 *mask = algt->mask & CRYPTO_ALG_INTERNAL;
Stephan Mueller466a7b92015-03-30 21:57:06 +0200203
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700204 /* No point in cryptd wrapping an algorithm that's already async. */
205 *mask |= CRYPTO_ALG_ASYNC;
Herbert Xuf6da3202015-07-09 07:17:19 +0800206
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700207 *mask |= crypto_algt_inherited_mask(algt);
Stephan Mueller466a7b92015-03-30 21:57:06 +0200208}
209
Herbert Xu9b8c4562015-05-21 15:10:57 +0800210static int cryptd_init_instance(struct crypto_instance *inst,
211 struct crypto_alg *alg)
212{
213 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
214 "cryptd(%s)",
215 alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
216 return -ENAMETOOLONG;
217
218 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
219
220 inst->alg.cra_priority = alg->cra_priority + 50;
221 inst->alg.cra_blocksize = alg->cra_blocksize;
222 inst->alg.cra_alignmask = alg->cra_alignmask;
223
224 return 0;
225}
226
Herbert Xu4e0958d2016-11-22 20:08:23 +0800227static int cryptd_skcipher_setkey(struct crypto_skcipher *parent,
228 const u8 *key, unsigned int keylen)
229{
230 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(parent);
Herbert Xu442caec2022-11-11 17:59:17 +0800231 struct crypto_skcipher *child = ctx->child;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800232
Herbert Xu442caec2022-11-11 17:59:17 +0800233 crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
234 crypto_skcipher_set_flags(child,
235 crypto_skcipher_get_flags(parent) &
236 CRYPTO_TFM_REQ_MASK);
237 return crypto_skcipher_setkey(child, key, keylen);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800238}
239
240static void cryptd_skcipher_complete(struct skcipher_request *req, int err)
241{
242 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
243 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
244 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800245 int refcnt = refcount_read(&ctx->refcnt);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800246
247 local_bh_disable();
248 rctx->complete(&req->base, err);
249 local_bh_enable();
250
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800251 if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
Herbert Xu4e0958d2016-11-22 20:08:23 +0800252 crypto_free_skcipher(tfm);
253}
254
255static void cryptd_skcipher_encrypt(struct crypto_async_request *base,
256 int err)
257{
258 struct skcipher_request *req = skcipher_request_cast(base);
259 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
260 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
261 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
Herbert Xu442caec2022-11-11 17:59:17 +0800262 struct skcipher_request *subreq = &rctx->req;
263 struct crypto_skcipher *child = ctx->child;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800264
265 if (unlikely(err == -EINPROGRESS))
266 goto out;
267
Herbert Xu442caec2022-11-11 17:59:17 +0800268 skcipher_request_set_tfm(subreq, child);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800269 skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
270 NULL, NULL);
271 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
272 req->iv);
273
274 err = crypto_skcipher_encrypt(subreq);
275 skcipher_request_zero(subreq);
276
277 req->base.complete = rctx->complete;
278
279out:
280 cryptd_skcipher_complete(req, err);
281}
282
283static void cryptd_skcipher_decrypt(struct crypto_async_request *base,
284 int err)
285{
286 struct skcipher_request *req = skcipher_request_cast(base);
287 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
288 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
289 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
Herbert Xu442caec2022-11-11 17:59:17 +0800290 struct skcipher_request *subreq = &rctx->req;
291 struct crypto_skcipher *child = ctx->child;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800292
293 if (unlikely(err == -EINPROGRESS))
294 goto out;
295
Herbert Xu442caec2022-11-11 17:59:17 +0800296 skcipher_request_set_tfm(subreq, child);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800297 skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
298 NULL, NULL);
299 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
300 req->iv);
301
302 err = crypto_skcipher_decrypt(subreq);
303 skcipher_request_zero(subreq);
304
305 req->base.complete = rctx->complete;
306
307out:
308 cryptd_skcipher_complete(req, err);
309}
310
311static int cryptd_skcipher_enqueue(struct skcipher_request *req,
312 crypto_completion_t compl)
313{
314 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
315 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
316 struct cryptd_queue *queue;
317
318 queue = cryptd_get_queue(crypto_skcipher_tfm(tfm));
319 rctx->complete = req->base.complete;
320 req->base.complete = compl;
321
322 return cryptd_enqueue_request(queue, &req->base);
323}
324
325static int cryptd_skcipher_encrypt_enqueue(struct skcipher_request *req)
326{
327 return cryptd_skcipher_enqueue(req, cryptd_skcipher_encrypt);
328}
329
330static int cryptd_skcipher_decrypt_enqueue(struct skcipher_request *req)
331{
332 return cryptd_skcipher_enqueue(req, cryptd_skcipher_decrypt);
333}
334
335static int cryptd_skcipher_init_tfm(struct crypto_skcipher *tfm)
336{
337 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
338 struct skcipherd_instance_ctx *ictx = skcipher_instance_ctx(inst);
339 struct crypto_skcipher_spawn *spawn = &ictx->spawn;
340 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
341 struct crypto_skcipher *cipher;
342
343 cipher = crypto_spawn_skcipher(spawn);
344 if (IS_ERR(cipher))
345 return PTR_ERR(cipher);
346
Herbert Xu442caec2022-11-11 17:59:17 +0800347 ctx->child = cipher;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800348 crypto_skcipher_set_reqsize(
Herbert Xu442caec2022-11-11 17:59:17 +0800349 tfm, sizeof(struct cryptd_skcipher_request_ctx) +
350 crypto_skcipher_reqsize(cipher));
Herbert Xu4e0958d2016-11-22 20:08:23 +0800351 return 0;
352}
353
354static void cryptd_skcipher_exit_tfm(struct crypto_skcipher *tfm)
355{
356 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
357
Herbert Xu442caec2022-11-11 17:59:17 +0800358 crypto_free_skcipher(ctx->child);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800359}
360
361static void cryptd_skcipher_free(struct skcipher_instance *inst)
362{
363 struct skcipherd_instance_ctx *ctx = skcipher_instance_ctx(inst);
364
365 crypto_drop_skcipher(&ctx->spawn);
Vincent Whitchurch1a0fad62019-07-02 09:53:25 +0200366 kfree(inst);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800367}
368
369static int cryptd_create_skcipher(struct crypto_template *tmpl,
370 struct rtattr **tb,
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700371 struct crypto_attr_type *algt,
Herbert Xu4e0958d2016-11-22 20:08:23 +0800372 struct cryptd_queue *queue)
373{
374 struct skcipherd_instance_ctx *ctx;
375 struct skcipher_instance *inst;
376 struct skcipher_alg *alg;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800377 u32 type;
378 u32 mask;
379 int err;
380
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700381 cryptd_type_and_mask(algt, &type, &mask);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800382
Herbert Xu4e0958d2016-11-22 20:08:23 +0800383 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
384 if (!inst)
385 return -ENOMEM;
386
387 ctx = skcipher_instance_ctx(inst);
388 ctx->queue = queue;
389
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800390 err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst),
Eric Biggersb8c0d742020-02-25 20:59:15 -0800391 crypto_attr_alg_name(tb[1]), type, mask);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800392 if (err)
Eric Biggersb8c0d742020-02-25 20:59:15 -0800393 goto err_free_inst;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800394
395 alg = crypto_spawn_skcipher_alg(&ctx->spawn);
396 err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base);
397 if (err)
Eric Biggersb8c0d742020-02-25 20:59:15 -0800398 goto err_free_inst;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800399
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700400 inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC |
401 (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800402 inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg);
403 inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
404 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
405 inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
406
407 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_skcipher_ctx);
408
409 inst->alg.init = cryptd_skcipher_init_tfm;
410 inst->alg.exit = cryptd_skcipher_exit_tfm;
411
412 inst->alg.setkey = cryptd_skcipher_setkey;
413 inst->alg.encrypt = cryptd_skcipher_encrypt_enqueue;
414 inst->alg.decrypt = cryptd_skcipher_decrypt_enqueue;
415
416 inst->free = cryptd_skcipher_free;
417
418 err = skcipher_register_instance(tmpl, inst);
419 if (err) {
Eric Biggersb8c0d742020-02-25 20:59:15 -0800420err_free_inst:
421 cryptd_skcipher_free(inst);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800422 }
423 return err;
424}
425
Loc Hob8a28252008-05-14 21:23:00 +0800426static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
427{
428 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
Herbert Xu46309d82009-07-12 21:38:59 +0800429 struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
430 struct crypto_shash_spawn *spawn = &ictx->spawn;
Loc Hob8a28252008-05-14 21:23:00 +0800431 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu46309d82009-07-12 21:38:59 +0800432 struct crypto_shash *hash;
Loc Hob8a28252008-05-14 21:23:00 +0800433
Herbert Xu46309d82009-07-12 21:38:59 +0800434 hash = crypto_spawn_shash(spawn);
435 if (IS_ERR(hash))
436 return PTR_ERR(hash);
Loc Hob8a28252008-05-14 21:23:00 +0800437
Herbert Xu46309d82009-07-12 21:38:59 +0800438 ctx->child = hash;
Herbert Xu0d6669e2009-07-12 23:06:33 +0800439 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
440 sizeof(struct cryptd_hash_request_ctx) +
441 crypto_shash_descsize(hash));
Loc Hob8a28252008-05-14 21:23:00 +0800442 return 0;
443}
444
445static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
446{
447 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
Loc Hob8a28252008-05-14 21:23:00 +0800448
Herbert Xu46309d82009-07-12 21:38:59 +0800449 crypto_free_shash(ctx->child);
Loc Hob8a28252008-05-14 21:23:00 +0800450}
451
452static int cryptd_hash_setkey(struct crypto_ahash *parent,
453 const u8 *key, unsigned int keylen)
454{
455 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
Herbert Xu46309d82009-07-12 21:38:59 +0800456 struct crypto_shash *child = ctx->child;
Loc Hob8a28252008-05-14 21:23:00 +0800457
Herbert Xu46309d82009-07-12 21:38:59 +0800458 crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
459 crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
460 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600461 return crypto_shash_setkey(child, key, keylen);
Loc Hob8a28252008-05-14 21:23:00 +0800462}
463
464static int cryptd_hash_enqueue(struct ahash_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700465 crypto_completion_t compl)
Loc Hob8a28252008-05-14 21:23:00 +0800466{
467 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
468 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
Huang Ying254eff72009-02-19 14:42:19 +0800469 struct cryptd_queue *queue =
470 cryptd_get_queue(crypto_ahash_tfm(tfm));
Loc Hob8a28252008-05-14 21:23:00 +0800471
472 rctx->complete = req->base.complete;
Mark Rustad3e3dc252014-07-25 02:53:38 -0700473 req->base.complete = compl;
Loc Hob8a28252008-05-14 21:23:00 +0800474
Huang Ying254eff72009-02-19 14:42:19 +0800475 return cryptd_enqueue_request(queue, &req->base);
Loc Hob8a28252008-05-14 21:23:00 +0800476}
477
Herbert Xu81760ea2016-06-21 16:55:13 +0800478static void cryptd_hash_complete(struct ahash_request *req, int err)
479{
480 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
481 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
482 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800483 int refcnt = refcount_read(&ctx->refcnt);
Herbert Xu81760ea2016-06-21 16:55:13 +0800484
485 local_bh_disable();
486 rctx->complete(&req->base, err);
487 local_bh_enable();
488
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800489 if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
Herbert Xu81760ea2016-06-21 16:55:13 +0800490 crypto_free_ahash(tfm);
491}
492
Loc Hob8a28252008-05-14 21:23:00 +0800493static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
494{
Herbert Xu46309d82009-07-12 21:38:59 +0800495 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
496 struct crypto_shash *child = ctx->child;
497 struct ahash_request *req = ahash_request_cast(req_async);
498 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
499 struct shash_desc *desc = &rctx->desc;
Loc Hob8a28252008-05-14 21:23:00 +0800500
501 if (unlikely(err == -EINPROGRESS))
502 goto out;
503
Herbert Xu46309d82009-07-12 21:38:59 +0800504 desc->tfm = child;
Loc Hob8a28252008-05-14 21:23:00 +0800505
Herbert Xu46309d82009-07-12 21:38:59 +0800506 err = crypto_shash_init(desc);
Loc Hob8a28252008-05-14 21:23:00 +0800507
508 req->base.complete = rctx->complete;
509
510out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800511 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800512}
513
514static int cryptd_hash_init_enqueue(struct ahash_request *req)
515{
516 return cryptd_hash_enqueue(req, cryptd_hash_init);
517}
518
519static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
520{
Herbert Xu46309d82009-07-12 21:38:59 +0800521 struct ahash_request *req = ahash_request_cast(req_async);
Loc Hob8a28252008-05-14 21:23:00 +0800522 struct cryptd_hash_request_ctx *rctx;
Loc Hob8a28252008-05-14 21:23:00 +0800523
524 rctx = ahash_request_ctx(req);
525
526 if (unlikely(err == -EINPROGRESS))
527 goto out;
528
Herbert Xu46309d82009-07-12 21:38:59 +0800529 err = shash_ahash_update(req, &rctx->desc);
Loc Hob8a28252008-05-14 21:23:00 +0800530
531 req->base.complete = rctx->complete;
532
533out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800534 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800535}
536
537static int cryptd_hash_update_enqueue(struct ahash_request *req)
538{
539 return cryptd_hash_enqueue(req, cryptd_hash_update);
540}
541
542static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
543{
Herbert Xu46309d82009-07-12 21:38:59 +0800544 struct ahash_request *req = ahash_request_cast(req_async);
545 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
Loc Hob8a28252008-05-14 21:23:00 +0800546
547 if (unlikely(err == -EINPROGRESS))
548 goto out;
549
Herbert Xu46309d82009-07-12 21:38:59 +0800550 err = crypto_shash_final(&rctx->desc, req->result);
Loc Hob8a28252008-05-14 21:23:00 +0800551
552 req->base.complete = rctx->complete;
553
554out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800555 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800556}
557
558static int cryptd_hash_final_enqueue(struct ahash_request *req)
559{
560 return cryptd_hash_enqueue(req, cryptd_hash_final);
561}
562
Herbert Xu6fba00d2009-07-22 11:10:22 +0800563static void cryptd_hash_finup(struct crypto_async_request *req_async, int err)
564{
565 struct ahash_request *req = ahash_request_cast(req_async);
566 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
567
568 if (unlikely(err == -EINPROGRESS))
569 goto out;
570
571 err = shash_ahash_finup(req, &rctx->desc);
572
573 req->base.complete = rctx->complete;
574
575out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800576 cryptd_hash_complete(req, err);
Herbert Xu6fba00d2009-07-22 11:10:22 +0800577}
578
579static int cryptd_hash_finup_enqueue(struct ahash_request *req)
580{
581 return cryptd_hash_enqueue(req, cryptd_hash_finup);
582}
583
Loc Hob8a28252008-05-14 21:23:00 +0800584static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
585{
Herbert Xu46309d82009-07-12 21:38:59 +0800586 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
587 struct crypto_shash *child = ctx->child;
588 struct ahash_request *req = ahash_request_cast(req_async);
589 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
590 struct shash_desc *desc = &rctx->desc;
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 desc->tfm = child;
Loc Hob8a28252008-05-14 21:23:00 +0800596
Herbert Xu46309d82009-07-12 21:38:59 +0800597 err = shash_ahash_digest(req, desc);
Loc Hob8a28252008-05-14 21:23:00 +0800598
599 req->base.complete = rctx->complete;
600
601out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800602 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800603}
604
605static int cryptd_hash_digest_enqueue(struct ahash_request *req)
606{
607 return cryptd_hash_enqueue(req, cryptd_hash_digest);
608}
609
Herbert Xu6fba00d2009-07-22 11:10:22 +0800610static int cryptd_hash_export(struct ahash_request *req, void *out)
611{
612 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
613
614 return crypto_shash_export(&rctx->desc, out);
615}
616
617static int cryptd_hash_import(struct ahash_request *req, const void *in)
618{
Ard Biesheuvel0bd22232016-09-01 14:25:43 +0100619 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
620 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
621 struct shash_desc *desc = cryptd_shash_desc(req);
Herbert Xu6fba00d2009-07-22 11:10:22 +0800622
Ard Biesheuvel0bd22232016-09-01 14:25:43 +0100623 desc->tfm = ctx->child;
Ard Biesheuvel0bd22232016-09-01 14:25:43 +0100624
625 return crypto_shash_import(desc, in);
Herbert Xu6fba00d2009-07-22 11:10:22 +0800626}
627
Eric Biggers758ec5a2020-01-02 20:04:37 -0800628static void cryptd_hash_free(struct ahash_instance *inst)
629{
630 struct hashd_instance_ctx *ctx = ahash_instance_ctx(inst);
631
632 crypto_drop_shash(&ctx->spawn);
633 kfree(inst);
634}
635
Herbert Xu9cd899a2009-07-14 18:45:45 +0800636static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700637 struct crypto_attr_type *algt,
Herbert Xu9cd899a2009-07-14 18:45:45 +0800638 struct cryptd_queue *queue)
Loc Hob8a28252008-05-14 21:23:00 +0800639{
Herbert Xu46309d82009-07-12 21:38:59 +0800640 struct hashd_instance_ctx *ctx;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800641 struct ahash_instance *inst;
Eric Biggers218c5032020-01-02 19:58:53 -0800642 struct shash_alg *alg;
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700643 u32 type;
644 u32 mask;
Herbert Xu46309d82009-07-12 21:38:59 +0800645 int err;
Loc Hob8a28252008-05-14 21:23:00 +0800646
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700647 cryptd_type_and_mask(algt, &type, &mask);
Stephan Mueller466a7b92015-03-30 21:57:06 +0200648
Eric Biggers218c5032020-01-02 19:58:53 -0800649 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
650 if (!inst)
651 return -ENOMEM;
Loc Hob8a28252008-05-14 21:23:00 +0800652
Herbert Xu0b535ad2009-07-14 19:11:32 +0800653 ctx = ahash_instance_ctx(inst);
Herbert Xu46309d82009-07-12 21:38:59 +0800654 ctx->queue = queue;
655
Eric Biggers218c5032020-01-02 19:58:53 -0800656 err = crypto_grab_shash(&ctx->spawn, ahash_crypto_instance(inst),
657 crypto_attr_alg_name(tb[1]), type, mask);
Herbert Xu46309d82009-07-12 21:38:59 +0800658 if (err)
Eric Biggers218c5032020-01-02 19:58:53 -0800659 goto err_free_inst;
660 alg = crypto_spawn_shash_alg(&ctx->spawn);
661
662 err = cryptd_init_instance(ahash_crypto_instance(inst), &alg->base);
663 if (err)
664 goto err_free_inst;
Herbert Xu46309d82009-07-12 21:38:59 +0800665
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700666 inst->alg.halg.base.cra_flags |= CRYPTO_ALG_ASYNC |
667 (alg->base.cra_flags & (CRYPTO_ALG_INTERNAL|
Eric Biggers218c5032020-01-02 19:58:53 -0800668 CRYPTO_ALG_OPTIONAL_KEY));
Eric Biggers218c5032020-01-02 19:58:53 -0800669 inst->alg.halg.digestsize = alg->digestsize;
670 inst->alg.halg.statesize = alg->statesize;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800671 inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
Loc Hob8a28252008-05-14 21:23:00 +0800672
Herbert Xu0b535ad2009-07-14 19:11:32 +0800673 inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
674 inst->alg.halg.base.cra_exit = cryptd_hash_exit_tfm;
Loc Hob8a28252008-05-14 21:23:00 +0800675
Herbert Xu0b535ad2009-07-14 19:11:32 +0800676 inst->alg.init = cryptd_hash_init_enqueue;
677 inst->alg.update = cryptd_hash_update_enqueue;
678 inst->alg.final = cryptd_hash_final_enqueue;
Herbert Xu6fba00d2009-07-22 11:10:22 +0800679 inst->alg.finup = cryptd_hash_finup_enqueue;
680 inst->alg.export = cryptd_hash_export;
681 inst->alg.import = cryptd_hash_import;
Eric Biggers218c5032020-01-02 19:58:53 -0800682 if (crypto_shash_alg_has_setkey(alg))
Eric Biggers841a3ff2018-01-03 11:16:23 -0800683 inst->alg.setkey = cryptd_hash_setkey;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800684 inst->alg.digest = cryptd_hash_digest_enqueue;
Loc Hob8a28252008-05-14 21:23:00 +0800685
Eric Biggers758ec5a2020-01-02 20:04:37 -0800686 inst->free = cryptd_hash_free;
687
Herbert Xu0b535ad2009-07-14 19:11:32 +0800688 err = ahash_register_instance(tmpl, inst);
Herbert Xu9cd899a2009-07-14 18:45:45 +0800689 if (err) {
Eric Biggers218c5032020-01-02 19:58:53 -0800690err_free_inst:
Eric Biggersb8c0d742020-02-25 20:59:15 -0800691 cryptd_hash_free(inst);
Herbert Xu9cd899a2009-07-14 18:45:45 +0800692 }
Herbert Xu9cd899a2009-07-14 18:45:45 +0800693 return err;
Loc Hob8a28252008-05-14 21:23:00 +0800694}
695
Herbert Xu92b98762015-05-28 22:08:01 +0800696static int cryptd_aead_setkey(struct crypto_aead *parent,
697 const u8 *key, unsigned int keylen)
698{
699 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
700 struct crypto_aead *child = ctx->child;
701
702 return crypto_aead_setkey(child, key, keylen);
703}
704
705static int cryptd_aead_setauthsize(struct crypto_aead *parent,
706 unsigned int authsize)
707{
708 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
709 struct crypto_aead *child = ctx->child;
710
711 return crypto_aead_setauthsize(child, authsize);
712}
713
Adrian Hoban298c9262010-09-20 16:05:12 +0800714static void cryptd_aead_crypt(struct aead_request *req,
715 struct crypto_aead *child,
716 int err,
717 int (*crypt)(struct aead_request *req))
718{
719 struct cryptd_aead_request_ctx *rctx;
Herbert Xu81760ea2016-06-21 16:55:13 +0800720 struct cryptd_aead_ctx *ctx;
Herbert Xuec9f2002015-07-06 19:11:03 +0800721 crypto_completion_t compl;
Herbert Xu81760ea2016-06-21 16:55:13 +0800722 struct crypto_aead *tfm;
723 int refcnt;
Herbert Xuec9f2002015-07-06 19:11:03 +0800724
Adrian Hoban298c9262010-09-20 16:05:12 +0800725 rctx = aead_request_ctx(req);
Herbert Xuec9f2002015-07-06 19:11:03 +0800726 compl = rctx->complete;
Adrian Hoban298c9262010-09-20 16:05:12 +0800727
Herbert Xu31bd44e2016-08-25 16:49:51 +0800728 tfm = crypto_aead_reqtfm(req);
729
Adrian Hoban298c9262010-09-20 16:05:12 +0800730 if (unlikely(err == -EINPROGRESS))
731 goto out;
732 aead_request_set_tfm(req, child);
733 err = crypt( req );
Herbert Xu81760ea2016-06-21 16:55:13 +0800734
Adrian Hoban298c9262010-09-20 16:05:12 +0800735out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800736 ctx = crypto_aead_ctx(tfm);
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800737 refcnt = refcount_read(&ctx->refcnt);
Herbert Xu81760ea2016-06-21 16:55:13 +0800738
Adrian Hoban298c9262010-09-20 16:05:12 +0800739 local_bh_disable();
Herbert Xuec9f2002015-07-06 19:11:03 +0800740 compl(&req->base, err);
Adrian Hoban298c9262010-09-20 16:05:12 +0800741 local_bh_enable();
Herbert Xu81760ea2016-06-21 16:55:13 +0800742
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800743 if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
Herbert Xu81760ea2016-06-21 16:55:13 +0800744 crypto_free_aead(tfm);
Adrian Hoban298c9262010-09-20 16:05:12 +0800745}
746
747static void cryptd_aead_encrypt(struct crypto_async_request *areq, int err)
748{
749 struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
750 struct crypto_aead *child = ctx->child;
751 struct aead_request *req;
752
753 req = container_of(areq, struct aead_request, base);
Herbert Xuba3749a2015-08-13 17:29:02 +0800754 cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->encrypt);
Adrian Hoban298c9262010-09-20 16:05:12 +0800755}
756
757static void cryptd_aead_decrypt(struct crypto_async_request *areq, int err)
758{
759 struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
760 struct crypto_aead *child = ctx->child;
761 struct aead_request *req;
762
763 req = container_of(areq, struct aead_request, base);
Herbert Xuba3749a2015-08-13 17:29:02 +0800764 cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->decrypt);
Adrian Hoban298c9262010-09-20 16:05:12 +0800765}
766
767static int cryptd_aead_enqueue(struct aead_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700768 crypto_completion_t compl)
Adrian Hoban298c9262010-09-20 16:05:12 +0800769{
770 struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req);
771 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
772 struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm));
773
774 rctx->complete = req->base.complete;
Mark Rustad3e3dc252014-07-25 02:53:38 -0700775 req->base.complete = compl;
Adrian Hoban298c9262010-09-20 16:05:12 +0800776 return cryptd_enqueue_request(queue, &req->base);
777}
778
779static int cryptd_aead_encrypt_enqueue(struct aead_request *req)
780{
781 return cryptd_aead_enqueue(req, cryptd_aead_encrypt );
782}
783
784static int cryptd_aead_decrypt_enqueue(struct aead_request *req)
785{
786 return cryptd_aead_enqueue(req, cryptd_aead_decrypt );
787}
788
Herbert Xuf614e542015-05-28 22:08:04 +0800789static int cryptd_aead_init_tfm(struct crypto_aead *tfm)
Adrian Hoban298c9262010-09-20 16:05:12 +0800790{
Herbert Xuf614e542015-05-28 22:08:04 +0800791 struct aead_instance *inst = aead_alg_instance(tfm);
792 struct aead_instance_ctx *ictx = aead_instance_ctx(inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800793 struct crypto_aead_spawn *spawn = &ictx->aead_spawn;
Herbert Xuf614e542015-05-28 22:08:04 +0800794 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
Adrian Hoban298c9262010-09-20 16:05:12 +0800795 struct crypto_aead *cipher;
796
797 cipher = crypto_spawn_aead(spawn);
798 if (IS_ERR(cipher))
799 return PTR_ERR(cipher);
800
Adrian Hoban298c9262010-09-20 16:05:12 +0800801 ctx->child = cipher;
Herbert Xuec9f2002015-07-06 19:11:03 +0800802 crypto_aead_set_reqsize(
803 tfm, max((unsigned)sizeof(struct cryptd_aead_request_ctx),
804 crypto_aead_reqsize(cipher)));
Adrian Hoban298c9262010-09-20 16:05:12 +0800805 return 0;
806}
807
Herbert Xuf614e542015-05-28 22:08:04 +0800808static void cryptd_aead_exit_tfm(struct crypto_aead *tfm)
Adrian Hoban298c9262010-09-20 16:05:12 +0800809{
Herbert Xuf614e542015-05-28 22:08:04 +0800810 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
Adrian Hoban298c9262010-09-20 16:05:12 +0800811 crypto_free_aead(ctx->child);
812}
813
Eric Biggers758ec5a2020-01-02 20:04:37 -0800814static void cryptd_aead_free(struct aead_instance *inst)
815{
816 struct aead_instance_ctx *ctx = aead_instance_ctx(inst);
817
818 crypto_drop_aead(&ctx->aead_spawn);
819 kfree(inst);
820}
821
Adrian Hoban298c9262010-09-20 16:05:12 +0800822static int cryptd_create_aead(struct crypto_template *tmpl,
823 struct rtattr **tb,
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700824 struct crypto_attr_type *algt,
Adrian Hoban298c9262010-09-20 16:05:12 +0800825 struct cryptd_queue *queue)
826{
827 struct aead_instance_ctx *ctx;
Herbert Xuf614e542015-05-28 22:08:04 +0800828 struct aead_instance *inst;
829 struct aead_alg *alg;
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700830 u32 type;
831 u32 mask;
Adrian Hoban298c9262010-09-20 16:05:12 +0800832 int err;
833
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700834 cryptd_type_and_mask(algt, &type, &mask);
Stephan Mueller466a7b92015-03-30 21:57:06 +0200835
Herbert Xu9b8c4562015-05-21 15:10:57 +0800836 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
837 if (!inst)
838 return -ENOMEM;
Adrian Hoban298c9262010-09-20 16:05:12 +0800839
Herbert Xuf614e542015-05-28 22:08:04 +0800840 ctx = aead_instance_ctx(inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800841 ctx->queue = queue;
842
Eric Biggerscd900f02020-01-02 19:58:46 -0800843 err = crypto_grab_aead(&ctx->aead_spawn, aead_crypto_instance(inst),
Eric Biggersb8c0d742020-02-25 20:59:15 -0800844 crypto_attr_alg_name(tb[1]), type, mask);
Adrian Hoban298c9262010-09-20 16:05:12 +0800845 if (err)
Eric Biggersb8c0d742020-02-25 20:59:15 -0800846 goto err_free_inst;
Adrian Hoban298c9262010-09-20 16:05:12 +0800847
Herbert Xuf614e542015-05-28 22:08:04 +0800848 alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
849 err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
Herbert Xu9b8c4562015-05-21 15:10:57 +0800850 if (err)
Eric Biggersb8c0d742020-02-25 20:59:15 -0800851 goto err_free_inst;
Herbert Xu9b8c4562015-05-21 15:10:57 +0800852
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700853 inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC |
854 (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
Herbert Xuf614e542015-05-28 22:08:04 +0800855 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx);
Adrian Hoban298c9262010-09-20 16:05:12 +0800856
Herbert Xuf614e542015-05-28 22:08:04 +0800857 inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
858 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
859
860 inst->alg.init = cryptd_aead_init_tfm;
861 inst->alg.exit = cryptd_aead_exit_tfm;
862 inst->alg.setkey = cryptd_aead_setkey;
863 inst->alg.setauthsize = cryptd_aead_setauthsize;
864 inst->alg.encrypt = cryptd_aead_encrypt_enqueue;
865 inst->alg.decrypt = cryptd_aead_decrypt_enqueue;
866
Eric Biggers758ec5a2020-01-02 20:04:37 -0800867 inst->free = cryptd_aead_free;
868
Herbert Xuf614e542015-05-28 22:08:04 +0800869 err = aead_register_instance(tmpl, inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800870 if (err) {
Eric Biggersb8c0d742020-02-25 20:59:15 -0800871err_free_inst:
872 cryptd_aead_free(inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800873 }
Adrian Hoban298c9262010-09-20 16:05:12 +0800874 return err;
875}
876
Huang Ying254eff72009-02-19 14:42:19 +0800877static struct cryptd_queue queue;
Herbert Xu124b53d2007-04-16 20:49:20 +1000878
Herbert Xu9cd899a2009-07-14 18:45:45 +0800879static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu124b53d2007-04-16 20:49:20 +1000880{
881 struct crypto_attr_type *algt;
882
883 algt = crypto_get_attr_type(tb);
884 if (IS_ERR(algt))
Herbert Xu9cd899a2009-07-14 18:45:45 +0800885 return PTR_ERR(algt);
Herbert Xu124b53d2007-04-16 20:49:20 +1000886
887 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
Eric Biggersc65058b2019-10-25 12:41:12 -0700888 case CRYPTO_ALG_TYPE_SKCIPHER:
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700889 return cryptd_create_skcipher(tmpl, tb, algt, &queue);
Eric Biggers84ede582019-05-20 09:54:46 -0700890 case CRYPTO_ALG_TYPE_HASH:
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700891 return cryptd_create_hash(tmpl, tb, algt, &queue);
Adrian Hoban298c9262010-09-20 16:05:12 +0800892 case CRYPTO_ALG_TYPE_AEAD:
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700893 return cryptd_create_aead(tmpl, tb, algt, &queue);
Herbert Xu124b53d2007-04-16 20:49:20 +1000894 }
895
Herbert Xu9cd899a2009-07-14 18:45:45 +0800896 return -EINVAL;
Herbert Xu124b53d2007-04-16 20:49:20 +1000897}
898
Herbert Xu124b53d2007-04-16 20:49:20 +1000899static struct crypto_template cryptd_tmpl = {
900 .name = "cryptd",
Herbert Xu9cd899a2009-07-14 18:45:45 +0800901 .create = cryptd_create,
Herbert Xu124b53d2007-04-16 20:49:20 +1000902 .module = THIS_MODULE,
903};
904
Herbert Xu4e0958d2016-11-22 20:08:23 +0800905struct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name,
906 u32 type, u32 mask)
907{
908 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
909 struct cryptd_skcipher_ctx *ctx;
910 struct crypto_skcipher *tfm;
911
912 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
913 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
914 return ERR_PTR(-EINVAL);
915
916 tfm = crypto_alloc_skcipher(cryptd_alg_name, type, mask);
917 if (IS_ERR(tfm))
918 return ERR_CAST(tfm);
919
920 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
921 crypto_free_skcipher(tfm);
922 return ERR_PTR(-EINVAL);
923 }
924
925 ctx = crypto_skcipher_ctx(tfm);
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800926 refcount_set(&ctx->refcnt, 1);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800927
928 return container_of(tfm, struct cryptd_skcipher, base);
929}
930EXPORT_SYMBOL_GPL(cryptd_alloc_skcipher);
931
932struct crypto_skcipher *cryptd_skcipher_child(struct cryptd_skcipher *tfm)
933{
934 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
935
Herbert Xu442caec2022-11-11 17:59:17 +0800936 return ctx->child;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800937}
938EXPORT_SYMBOL_GPL(cryptd_skcipher_child);
939
940bool cryptd_skcipher_queued(struct cryptd_skcipher *tfm)
941{
942 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
943
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800944 return refcount_read(&ctx->refcnt) - 1;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800945}
946EXPORT_SYMBOL_GPL(cryptd_skcipher_queued);
947
948void cryptd_free_skcipher(struct cryptd_skcipher *tfm)
949{
950 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
951
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800952 if (refcount_dec_and_test(&ctx->refcnt))
Herbert Xu4e0958d2016-11-22 20:08:23 +0800953 crypto_free_skcipher(&tfm->base);
954}
955EXPORT_SYMBOL_GPL(cryptd_free_skcipher);
956
Huang Yingace13662009-08-06 15:35:20 +1000957struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name,
958 u32 type, u32 mask)
959{
960 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu81760ea2016-06-21 16:55:13 +0800961 struct cryptd_hash_ctx *ctx;
Huang Yingace13662009-08-06 15:35:20 +1000962 struct crypto_ahash *tfm;
963
964 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
965 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
966 return ERR_PTR(-EINVAL);
967 tfm = crypto_alloc_ahash(cryptd_alg_name, type, mask);
968 if (IS_ERR(tfm))
969 return ERR_CAST(tfm);
970 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
971 crypto_free_ahash(tfm);
972 return ERR_PTR(-EINVAL);
973 }
974
Herbert Xu81760ea2016-06-21 16:55:13 +0800975 ctx = crypto_ahash_ctx(tfm);
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800976 refcount_set(&ctx->refcnt, 1);
Herbert Xu81760ea2016-06-21 16:55:13 +0800977
Huang Yingace13662009-08-06 15:35:20 +1000978 return __cryptd_ahash_cast(tfm);
979}
980EXPORT_SYMBOL_GPL(cryptd_alloc_ahash);
981
982struct crypto_shash *cryptd_ahash_child(struct cryptd_ahash *tfm)
983{
984 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
985
986 return ctx->child;
987}
988EXPORT_SYMBOL_GPL(cryptd_ahash_child);
989
Huang Ying0e1227d2009-10-19 11:53:06 +0900990struct shash_desc *cryptd_shash_desc(struct ahash_request *req)
991{
992 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
993 return &rctx->desc;
994}
995EXPORT_SYMBOL_GPL(cryptd_shash_desc);
996
Herbert Xu81760ea2016-06-21 16:55:13 +0800997bool cryptd_ahash_queued(struct cryptd_ahash *tfm)
998{
999 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
1000
Chuhong Yuan43b970f2019-08-08 16:00:22 +08001001 return refcount_read(&ctx->refcnt) - 1;
Herbert Xu81760ea2016-06-21 16:55:13 +08001002}
1003EXPORT_SYMBOL_GPL(cryptd_ahash_queued);
1004
Huang Yingace13662009-08-06 15:35:20 +10001005void cryptd_free_ahash(struct cryptd_ahash *tfm)
1006{
Herbert Xu81760ea2016-06-21 16:55:13 +08001007 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
1008
Chuhong Yuan43b970f2019-08-08 16:00:22 +08001009 if (refcount_dec_and_test(&ctx->refcnt))
Herbert Xu81760ea2016-06-21 16:55:13 +08001010 crypto_free_ahash(&tfm->base);
Huang Yingace13662009-08-06 15:35:20 +10001011}
1012EXPORT_SYMBOL_GPL(cryptd_free_ahash);
1013
Adrian Hoban298c9262010-09-20 16:05:12 +08001014struct cryptd_aead *cryptd_alloc_aead(const char *alg_name,
1015 u32 type, u32 mask)
1016{
1017 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu81760ea2016-06-21 16:55:13 +08001018 struct cryptd_aead_ctx *ctx;
Adrian Hoban298c9262010-09-20 16:05:12 +08001019 struct crypto_aead *tfm;
1020
1021 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
1022 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
1023 return ERR_PTR(-EINVAL);
1024 tfm = crypto_alloc_aead(cryptd_alg_name, type, mask);
1025 if (IS_ERR(tfm))
1026 return ERR_CAST(tfm);
1027 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
1028 crypto_free_aead(tfm);
1029 return ERR_PTR(-EINVAL);
1030 }
Herbert Xu81760ea2016-06-21 16:55:13 +08001031
1032 ctx = crypto_aead_ctx(tfm);
Chuhong Yuan43b970f2019-08-08 16:00:22 +08001033 refcount_set(&ctx->refcnt, 1);
Herbert Xu81760ea2016-06-21 16:55:13 +08001034
Adrian Hoban298c9262010-09-20 16:05:12 +08001035 return __cryptd_aead_cast(tfm);
1036}
1037EXPORT_SYMBOL_GPL(cryptd_alloc_aead);
1038
1039struct crypto_aead *cryptd_aead_child(struct cryptd_aead *tfm)
1040{
1041 struct cryptd_aead_ctx *ctx;
1042 ctx = crypto_aead_ctx(&tfm->base);
1043 return ctx->child;
1044}
1045EXPORT_SYMBOL_GPL(cryptd_aead_child);
1046
Herbert Xu81760ea2016-06-21 16:55:13 +08001047bool cryptd_aead_queued(struct cryptd_aead *tfm)
1048{
1049 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
1050
Chuhong Yuan43b970f2019-08-08 16:00:22 +08001051 return refcount_read(&ctx->refcnt) - 1;
Herbert Xu81760ea2016-06-21 16:55:13 +08001052}
1053EXPORT_SYMBOL_GPL(cryptd_aead_queued);
1054
Adrian Hoban298c9262010-09-20 16:05:12 +08001055void cryptd_free_aead(struct cryptd_aead *tfm)
1056{
Herbert Xu81760ea2016-06-21 16:55:13 +08001057 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
1058
Chuhong Yuan43b970f2019-08-08 16:00:22 +08001059 if (refcount_dec_and_test(&ctx->refcnt))
Herbert Xu81760ea2016-06-21 16:55:13 +08001060 crypto_free_aead(&tfm->base);
Adrian Hoban298c9262010-09-20 16:05:12 +08001061}
1062EXPORT_SYMBOL_GPL(cryptd_free_aead);
1063
Herbert Xu124b53d2007-04-16 20:49:20 +10001064static int __init cryptd_init(void)
1065{
1066 int err;
1067
Eric Biggers3e56e162019-05-20 09:53:58 -07001068 cryptd_wq = alloc_workqueue("cryptd", WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE,
1069 1);
1070 if (!cryptd_wq)
1071 return -ENOMEM;
1072
Jon Maxwellc3a53602017-11-22 16:08:17 +11001073 err = cryptd_init_queue(&queue, cryptd_max_cpu_qlen);
Herbert Xu124b53d2007-04-16 20:49:20 +10001074 if (err)
Eric Biggers3e56e162019-05-20 09:53:58 -07001075 goto err_destroy_wq;
Herbert Xu124b53d2007-04-16 20:49:20 +10001076
1077 err = crypto_register_template(&cryptd_tmpl);
1078 if (err)
Eric Biggers3e56e162019-05-20 09:53:58 -07001079 goto err_fini_queue;
Herbert Xu124b53d2007-04-16 20:49:20 +10001080
Eric Biggers3e56e162019-05-20 09:53:58 -07001081 return 0;
1082
1083err_fini_queue:
1084 cryptd_fini_queue(&queue);
1085err_destroy_wq:
1086 destroy_workqueue(cryptd_wq);
Herbert Xu124b53d2007-04-16 20:49:20 +10001087 return err;
1088}
1089
1090static void __exit cryptd_exit(void)
1091{
Eric Biggers3e56e162019-05-20 09:53:58 -07001092 destroy_workqueue(cryptd_wq);
Huang Ying254eff72009-02-19 14:42:19 +08001093 cryptd_fini_queue(&queue);
Herbert Xu124b53d2007-04-16 20:49:20 +10001094 crypto_unregister_template(&cryptd_tmpl);
1095}
1096
Herbert Xub2bac6a2011-08-19 16:11:23 +08001097subsys_initcall(cryptd_init);
Herbert Xu124b53d2007-04-16 20:49:20 +10001098module_exit(cryptd_exit);
1099
1100MODULE_LICENSE("GPL");
1101MODULE_DESCRIPTION("Software async crypto daemon");
Kees Cook4943ba12014-11-24 16:32:38 -08001102MODULE_ALIAS_CRYPTO("cryptd");