blob: 668095eca0fafb52137e33ee70a97c2695b3180b [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 Siewior91e8bcd2022-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;
Kees Cook36b38752018-09-18 19:10:52 -070071 struct crypto_sync_skcipher *child;
Herbert Xu4e0958d2016-11-22 20:08:23 +080072};
73
74struct cryptd_skcipher_request_ctx {
75 crypto_completion_t complete;
76};
77
Loc Hob8a28252008-05-14 21:23:00 +080078struct cryptd_hash_ctx {
Chuhong Yuan43b970f2019-08-08 16:00:22 +080079 refcount_t refcnt;
Herbert Xu46309d82009-07-12 21:38:59 +080080 struct crypto_shash *child;
Loc Hob8a28252008-05-14 21:23:00 +080081};
82
83struct cryptd_hash_request_ctx {
84 crypto_completion_t complete;
Herbert Xu46309d82009-07-12 21:38:59 +080085 struct shash_desc desc;
Loc Hob8a28252008-05-14 21:23:00 +080086};
Herbert Xu124b53d2007-04-16 20:49:20 +100087
Adrian Hoban298c9262010-09-20 16:05:12 +080088struct cryptd_aead_ctx {
Chuhong Yuan43b970f2019-08-08 16:00:22 +080089 refcount_t refcnt;
Adrian Hoban298c9262010-09-20 16:05:12 +080090 struct crypto_aead *child;
91};
92
93struct cryptd_aead_request_ctx {
94 crypto_completion_t complete;
95};
96
Huang Ying254eff72009-02-19 14:42:19 +080097static void cryptd_queue_worker(struct work_struct *work);
98
99static int cryptd_init_queue(struct cryptd_queue *queue,
100 unsigned int max_cpu_qlen)
101{
102 int cpu;
103 struct cryptd_cpu_queue *cpu_queue;
104
105 queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue);
106 if (!queue->cpu_queue)
107 return -ENOMEM;
108 for_each_possible_cpu(cpu) {
109 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
110 crypto_init_queue(&cpu_queue->queue, max_cpu_qlen);
111 INIT_WORK(&cpu_queue->work, cryptd_queue_worker);
112 }
Jon Maxwellc3a53602017-11-22 16:08:17 +1100113 pr_info("cryptd: max_cpu_qlen set to %d\n", max_cpu_qlen);
Huang Ying254eff72009-02-19 14:42:19 +0800114 return 0;
115}
116
117static void cryptd_fini_queue(struct cryptd_queue *queue)
118{
119 int cpu;
120 struct cryptd_cpu_queue *cpu_queue;
121
122 for_each_possible_cpu(cpu) {
123 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu);
124 BUG_ON(cpu_queue->queue.qlen);
125 }
126 free_percpu(queue->cpu_queue);
127}
128
129static int cryptd_enqueue_request(struct cryptd_queue *queue,
130 struct crypto_async_request *request)
131{
Sebastian Andrzej Siewior91e8bcd2022-05-04 17:07:36 +0200132 int err;
Huang Ying254eff72009-02-19 14:42:19 +0800133 struct cryptd_cpu_queue *cpu_queue;
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800134 refcount_t *refcnt;
Huang Ying254eff72009-02-19 14:42:19 +0800135
Sebastian Andrzej Siewior91e8bcd2022-05-04 17:07:36 +0200136 local_bh_disable();
Christoph Lameter0b44f482009-10-03 19:48:23 +0900137 cpu_queue = this_cpu_ptr(queue->cpu_queue);
Huang Ying254eff72009-02-19 14:42:19 +0800138 err = crypto_enqueue_request(&cpu_queue->queue, request);
Herbert Xu81760ea2016-06-21 16:55:13 +0800139
140 refcnt = crypto_tfm_ctx(request->tfm);
Herbert Xu81760ea2016-06-21 16:55:13 +0800141
Gilad Ben-Yossef6b80ea32017-10-18 08:00:33 +0100142 if (err == -ENOSPC)
Sebastian Andrzej Siewior91e8bcd2022-05-04 17:07:36 +0200143 goto out;
Herbert Xu81760ea2016-06-21 16:55:13 +0800144
Sebastian Andrzej Siewior91e8bcd2022-05-04 17:07:36 +0200145 queue_work_on(smp_processor_id(), cryptd_wq, &cpu_queue->work);
Herbert Xu81760ea2016-06-21 16:55:13 +0800146
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800147 if (!refcount_read(refcnt))
Sebastian Andrzej Siewior91e8bcd2022-05-04 17:07:36 +0200148 goto out;
Herbert Xu81760ea2016-06-21 16:55:13 +0800149
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800150 refcount_inc(refcnt);
Herbert Xu81760ea2016-06-21 16:55:13 +0800151
Sebastian Andrzej Siewior91e8bcd2022-05-04 17:07:36 +0200152out:
153 local_bh_enable();
Huang Ying254eff72009-02-19 14:42:19 +0800154
155 return err;
156}
157
158/* Called in workqueue context, do one real cryption work (via
159 * req->complete) and reschedule itself if there are more work to
160 * do. */
161static void cryptd_queue_worker(struct work_struct *work)
162{
163 struct cryptd_cpu_queue *cpu_queue;
164 struct crypto_async_request *req, *backlog;
165
166 cpu_queue = container_of(work, struct cryptd_cpu_queue, work);
Jussi Kivilinna9efade12012-10-21 20:42:28 +0300167 /*
168 * Only handle one request at a time to avoid hogging crypto workqueue.
Jussi Kivilinna9efade12012-10-21 20:42:28 +0300169 */
170 local_bh_disable();
Huang Ying254eff72009-02-19 14:42:19 +0800171 backlog = crypto_get_backlog(&cpu_queue->queue);
172 req = crypto_dequeue_request(&cpu_queue->queue);
Jussi Kivilinna9efade12012-10-21 20:42:28 +0300173 local_bh_enable();
Huang Ying254eff72009-02-19 14:42:19 +0800174
175 if (!req)
176 return;
177
178 if (backlog)
179 backlog->complete(backlog, -EINPROGRESS);
180 req->complete(req, 0);
181
182 if (cpu_queue->queue.qlen)
Eric Biggers3e56e162019-05-20 09:53:58 -0700183 queue_work(cryptd_wq, &cpu_queue->work);
Huang Ying254eff72009-02-19 14:42:19 +0800184}
185
186static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm)
Herbert Xu124b53d2007-04-16 20:49:20 +1000187{
188 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
189 struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
Huang Ying254eff72009-02-19 14:42:19 +0800190 return ictx->queue;
Herbert Xu124b53d2007-04-16 20:49:20 +1000191}
192
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700193static void cryptd_type_and_mask(struct crypto_attr_type *algt,
194 u32 *type, u32 *mask)
Stephan Mueller466a7b92015-03-30 21:57:06 +0200195{
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700196 /*
197 * cryptd is allowed to wrap internal algorithms, but in that case the
198 * resulting cryptd instance will be marked as internal as well.
199 */
200 *type = algt->type & CRYPTO_ALG_INTERNAL;
201 *mask = algt->mask & CRYPTO_ALG_INTERNAL;
Stephan Mueller466a7b92015-03-30 21:57:06 +0200202
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700203 /* No point in cryptd wrapping an algorithm that's already async. */
204 *mask |= CRYPTO_ALG_ASYNC;
Herbert Xuf6da3202015-07-09 07:17:19 +0800205
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700206 *mask |= crypto_algt_inherited_mask(algt);
Stephan Mueller466a7b92015-03-30 21:57:06 +0200207}
208
Herbert Xu9b8c4562015-05-21 15:10:57 +0800209static int cryptd_init_instance(struct crypto_instance *inst,
210 struct crypto_alg *alg)
211{
212 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
213 "cryptd(%s)",
214 alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
215 return -ENAMETOOLONG;
216
217 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
218
219 inst->alg.cra_priority = alg->cra_priority + 50;
220 inst->alg.cra_blocksize = alg->cra_blocksize;
221 inst->alg.cra_alignmask = alg->cra_alignmask;
222
223 return 0;
224}
225
Herbert Xu4e0958d2016-11-22 20:08:23 +0800226static int cryptd_skcipher_setkey(struct crypto_skcipher *parent,
227 const u8 *key, unsigned int keylen)
228{
229 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(parent);
Kees Cook36b38752018-09-18 19:10:52 -0700230 struct crypto_sync_skcipher *child = ctx->child;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800231
Kees Cook36b38752018-09-18 19:10:52 -0700232 crypto_sync_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
233 crypto_sync_skcipher_set_flags(child,
234 crypto_skcipher_get_flags(parent) &
Herbert Xu4e0958d2016-11-22 20:08:23 +0800235 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600236 return crypto_sync_skcipher_setkey(child, key, keylen);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800237}
238
239static void cryptd_skcipher_complete(struct skcipher_request *req, int err)
240{
241 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
242 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
243 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800244 int refcnt = refcount_read(&ctx->refcnt);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800245
246 local_bh_disable();
247 rctx->complete(&req->base, err);
248 local_bh_enable();
249
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800250 if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
Herbert Xu4e0958d2016-11-22 20:08:23 +0800251 crypto_free_skcipher(tfm);
252}
253
254static void cryptd_skcipher_encrypt(struct crypto_async_request *base,
255 int err)
256{
257 struct skcipher_request *req = skcipher_request_cast(base);
258 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
259 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
260 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
Kees Cook36b38752018-09-18 19:10:52 -0700261 struct crypto_sync_skcipher *child = ctx->child;
262 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, child);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800263
264 if (unlikely(err == -EINPROGRESS))
265 goto out;
266
Kees Cook36b38752018-09-18 19:10:52 -0700267 skcipher_request_set_sync_tfm(subreq, child);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800268 skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
269 NULL, NULL);
270 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
271 req->iv);
272
273 err = crypto_skcipher_encrypt(subreq);
274 skcipher_request_zero(subreq);
275
276 req->base.complete = rctx->complete;
277
278out:
279 cryptd_skcipher_complete(req, err);
280}
281
282static void cryptd_skcipher_decrypt(struct crypto_async_request *base,
283 int err)
284{
285 struct skcipher_request *req = skcipher_request_cast(base);
286 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
287 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
288 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
Kees Cook36b38752018-09-18 19:10:52 -0700289 struct crypto_sync_skcipher *child = ctx->child;
290 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, child);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800291
292 if (unlikely(err == -EINPROGRESS))
293 goto out;
294
Kees Cook36b38752018-09-18 19:10:52 -0700295 skcipher_request_set_sync_tfm(subreq, child);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800296 skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP,
297 NULL, NULL);
298 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
299 req->iv);
300
301 err = crypto_skcipher_decrypt(subreq);
302 skcipher_request_zero(subreq);
303
304 req->base.complete = rctx->complete;
305
306out:
307 cryptd_skcipher_complete(req, err);
308}
309
310static int cryptd_skcipher_enqueue(struct skcipher_request *req,
311 crypto_completion_t compl)
312{
313 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
314 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
315 struct cryptd_queue *queue;
316
317 queue = cryptd_get_queue(crypto_skcipher_tfm(tfm));
318 rctx->complete = req->base.complete;
319 req->base.complete = compl;
320
321 return cryptd_enqueue_request(queue, &req->base);
322}
323
324static int cryptd_skcipher_encrypt_enqueue(struct skcipher_request *req)
325{
326 return cryptd_skcipher_enqueue(req, cryptd_skcipher_encrypt);
327}
328
329static int cryptd_skcipher_decrypt_enqueue(struct skcipher_request *req)
330{
331 return cryptd_skcipher_enqueue(req, cryptd_skcipher_decrypt);
332}
333
334static int cryptd_skcipher_init_tfm(struct crypto_skcipher *tfm)
335{
336 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
337 struct skcipherd_instance_ctx *ictx = skcipher_instance_ctx(inst);
338 struct crypto_skcipher_spawn *spawn = &ictx->spawn;
339 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
340 struct crypto_skcipher *cipher;
341
342 cipher = crypto_spawn_skcipher(spawn);
343 if (IS_ERR(cipher))
344 return PTR_ERR(cipher);
345
Kees Cook36b38752018-09-18 19:10:52 -0700346 ctx->child = (struct crypto_sync_skcipher *)cipher;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800347 crypto_skcipher_set_reqsize(
348 tfm, sizeof(struct cryptd_skcipher_request_ctx));
349 return 0;
350}
351
352static void cryptd_skcipher_exit_tfm(struct crypto_skcipher *tfm)
353{
354 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
355
Kees Cook36b38752018-09-18 19:10:52 -0700356 crypto_free_sync_skcipher(ctx->child);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800357}
358
359static void cryptd_skcipher_free(struct skcipher_instance *inst)
360{
361 struct skcipherd_instance_ctx *ctx = skcipher_instance_ctx(inst);
362
363 crypto_drop_skcipher(&ctx->spawn);
Vincent Whitchurch1a0fad62019-07-02 09:53:25 +0200364 kfree(inst);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800365}
366
367static int cryptd_create_skcipher(struct crypto_template *tmpl,
368 struct rtattr **tb,
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700369 struct crypto_attr_type *algt,
Herbert Xu4e0958d2016-11-22 20:08:23 +0800370 struct cryptd_queue *queue)
371{
372 struct skcipherd_instance_ctx *ctx;
373 struct skcipher_instance *inst;
374 struct skcipher_alg *alg;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800375 u32 type;
376 u32 mask;
377 int err;
378
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700379 cryptd_type_and_mask(algt, &type, &mask);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800380
Herbert Xu4e0958d2016-11-22 20:08:23 +0800381 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
382 if (!inst)
383 return -ENOMEM;
384
385 ctx = skcipher_instance_ctx(inst);
386 ctx->queue = queue;
387
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800388 err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst),
Eric Biggersb8c0d742020-02-25 20:59:15 -0800389 crypto_attr_alg_name(tb[1]), type, mask);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800390 if (err)
Eric Biggersb8c0d742020-02-25 20:59:15 -0800391 goto err_free_inst;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800392
393 alg = crypto_spawn_skcipher_alg(&ctx->spawn);
394 err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base);
395 if (err)
Eric Biggersb8c0d742020-02-25 20:59:15 -0800396 goto err_free_inst;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800397
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700398 inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC |
399 (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800400 inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg);
401 inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
402 inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
403 inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
404
405 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_skcipher_ctx);
406
407 inst->alg.init = cryptd_skcipher_init_tfm;
408 inst->alg.exit = cryptd_skcipher_exit_tfm;
409
410 inst->alg.setkey = cryptd_skcipher_setkey;
411 inst->alg.encrypt = cryptd_skcipher_encrypt_enqueue;
412 inst->alg.decrypt = cryptd_skcipher_decrypt_enqueue;
413
414 inst->free = cryptd_skcipher_free;
415
416 err = skcipher_register_instance(tmpl, inst);
417 if (err) {
Eric Biggersb8c0d742020-02-25 20:59:15 -0800418err_free_inst:
419 cryptd_skcipher_free(inst);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800420 }
421 return err;
422}
423
Loc Hob8a28252008-05-14 21:23:00 +0800424static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
425{
426 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
Herbert Xu46309d82009-07-12 21:38:59 +0800427 struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst);
428 struct crypto_shash_spawn *spawn = &ictx->spawn;
Loc Hob8a28252008-05-14 21:23:00 +0800429 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
Herbert Xu46309d82009-07-12 21:38:59 +0800430 struct crypto_shash *hash;
Loc Hob8a28252008-05-14 21:23:00 +0800431
Herbert Xu46309d82009-07-12 21:38:59 +0800432 hash = crypto_spawn_shash(spawn);
433 if (IS_ERR(hash))
434 return PTR_ERR(hash);
Loc Hob8a28252008-05-14 21:23:00 +0800435
Herbert Xu46309d82009-07-12 21:38:59 +0800436 ctx->child = hash;
Herbert Xu0d6669e2009-07-12 23:06:33 +0800437 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
438 sizeof(struct cryptd_hash_request_ctx) +
439 crypto_shash_descsize(hash));
Loc Hob8a28252008-05-14 21:23:00 +0800440 return 0;
441}
442
443static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
444{
445 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
Loc Hob8a28252008-05-14 21:23:00 +0800446
Herbert Xu46309d82009-07-12 21:38:59 +0800447 crypto_free_shash(ctx->child);
Loc Hob8a28252008-05-14 21:23:00 +0800448}
449
450static int cryptd_hash_setkey(struct crypto_ahash *parent,
451 const u8 *key, unsigned int keylen)
452{
453 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
Herbert Xu46309d82009-07-12 21:38:59 +0800454 struct crypto_shash *child = ctx->child;
Loc Hob8a28252008-05-14 21:23:00 +0800455
Herbert Xu46309d82009-07-12 21:38:59 +0800456 crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
457 crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) &
458 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600459 return crypto_shash_setkey(child, key, keylen);
Loc Hob8a28252008-05-14 21:23:00 +0800460}
461
462static int cryptd_hash_enqueue(struct ahash_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700463 crypto_completion_t compl)
Loc Hob8a28252008-05-14 21:23:00 +0800464{
465 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
466 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
Huang Ying254eff72009-02-19 14:42:19 +0800467 struct cryptd_queue *queue =
468 cryptd_get_queue(crypto_ahash_tfm(tfm));
Loc Hob8a28252008-05-14 21:23:00 +0800469
470 rctx->complete = req->base.complete;
Mark Rustad3e3dc252014-07-25 02:53:38 -0700471 req->base.complete = compl;
Loc Hob8a28252008-05-14 21:23:00 +0800472
Huang Ying254eff72009-02-19 14:42:19 +0800473 return cryptd_enqueue_request(queue, &req->base);
Loc Hob8a28252008-05-14 21:23:00 +0800474}
475
Herbert Xu81760ea2016-06-21 16:55:13 +0800476static void cryptd_hash_complete(struct ahash_request *req, int err)
477{
478 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
479 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
480 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800481 int refcnt = refcount_read(&ctx->refcnt);
Herbert Xu81760ea2016-06-21 16:55:13 +0800482
483 local_bh_disable();
484 rctx->complete(&req->base, err);
485 local_bh_enable();
486
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800487 if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
Herbert Xu81760ea2016-06-21 16:55:13 +0800488 crypto_free_ahash(tfm);
489}
490
Loc Hob8a28252008-05-14 21:23:00 +0800491static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
492{
Herbert Xu46309d82009-07-12 21:38:59 +0800493 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
494 struct crypto_shash *child = ctx->child;
495 struct ahash_request *req = ahash_request_cast(req_async);
496 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
497 struct shash_desc *desc = &rctx->desc;
Loc Hob8a28252008-05-14 21:23:00 +0800498
499 if (unlikely(err == -EINPROGRESS))
500 goto out;
501
Herbert Xu46309d82009-07-12 21:38:59 +0800502 desc->tfm = child;
Loc Hob8a28252008-05-14 21:23:00 +0800503
Herbert Xu46309d82009-07-12 21:38:59 +0800504 err = crypto_shash_init(desc);
Loc Hob8a28252008-05-14 21:23:00 +0800505
506 req->base.complete = rctx->complete;
507
508out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800509 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800510}
511
512static int cryptd_hash_init_enqueue(struct ahash_request *req)
513{
514 return cryptd_hash_enqueue(req, cryptd_hash_init);
515}
516
517static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
518{
Herbert Xu46309d82009-07-12 21:38:59 +0800519 struct ahash_request *req = ahash_request_cast(req_async);
Loc Hob8a28252008-05-14 21:23:00 +0800520 struct cryptd_hash_request_ctx *rctx;
Loc Hob8a28252008-05-14 21:23:00 +0800521
522 rctx = ahash_request_ctx(req);
523
524 if (unlikely(err == -EINPROGRESS))
525 goto out;
526
Herbert Xu46309d82009-07-12 21:38:59 +0800527 err = shash_ahash_update(req, &rctx->desc);
Loc Hob8a28252008-05-14 21:23:00 +0800528
529 req->base.complete = rctx->complete;
530
531out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800532 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800533}
534
535static int cryptd_hash_update_enqueue(struct ahash_request *req)
536{
537 return cryptd_hash_enqueue(req, cryptd_hash_update);
538}
539
540static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
541{
Herbert Xu46309d82009-07-12 21:38:59 +0800542 struct ahash_request *req = ahash_request_cast(req_async);
543 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
Loc Hob8a28252008-05-14 21:23:00 +0800544
545 if (unlikely(err == -EINPROGRESS))
546 goto out;
547
Herbert Xu46309d82009-07-12 21:38:59 +0800548 err = crypto_shash_final(&rctx->desc, req->result);
Loc Hob8a28252008-05-14 21:23:00 +0800549
550 req->base.complete = rctx->complete;
551
552out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800553 cryptd_hash_complete(req, err);
Loc Hob8a28252008-05-14 21:23:00 +0800554}
555
556static int cryptd_hash_final_enqueue(struct ahash_request *req)
557{
558 return cryptd_hash_enqueue(req, cryptd_hash_final);
559}
560
Herbert Xu6fba00d2009-07-22 11:10:22 +0800561static void cryptd_hash_finup(struct crypto_async_request *req_async, int err)
562{
563 struct ahash_request *req = ahash_request_cast(req_async);
564 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
565
566 if (unlikely(err == -EINPROGRESS))
567 goto out;
568
569 err = shash_ahash_finup(req, &rctx->desc);
570
571 req->base.complete = rctx->complete;
572
573out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800574 cryptd_hash_complete(req, err);
Herbert Xu6fba00d2009-07-22 11:10:22 +0800575}
576
577static int cryptd_hash_finup_enqueue(struct ahash_request *req)
578{
579 return cryptd_hash_enqueue(req, cryptd_hash_finup);
580}
581
Loc Hob8a28252008-05-14 21:23:00 +0800582static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
583{
Herbert Xu46309d82009-07-12 21:38:59 +0800584 struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
585 struct crypto_shash *child = ctx->child;
586 struct ahash_request *req = ahash_request_cast(req_async);
587 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
588 struct shash_desc *desc = &rctx->desc;
Loc Hob8a28252008-05-14 21:23:00 +0800589
590 if (unlikely(err == -EINPROGRESS))
591 goto out;
592
Herbert Xu46309d82009-07-12 21:38:59 +0800593 desc->tfm = child;
Loc Hob8a28252008-05-14 21:23:00 +0800594
Herbert Xu46309d82009-07-12 21:38:59 +0800595 err = shash_ahash_digest(req, desc);
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_digest_enqueue(struct ahash_request *req)
604{
605 return cryptd_hash_enqueue(req, cryptd_hash_digest);
606}
607
Herbert Xu6fba00d2009-07-22 11:10:22 +0800608static int cryptd_hash_export(struct ahash_request *req, void *out)
609{
610 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
611
612 return crypto_shash_export(&rctx->desc, out);
613}
614
615static int cryptd_hash_import(struct ahash_request *req, const void *in)
616{
Ard Biesheuvel0bd22232016-09-01 14:25:43 +0100617 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
618 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
619 struct shash_desc *desc = cryptd_shash_desc(req);
Herbert Xu6fba00d2009-07-22 11:10:22 +0800620
Ard Biesheuvel0bd22232016-09-01 14:25:43 +0100621 desc->tfm = ctx->child;
Ard Biesheuvel0bd22232016-09-01 14:25:43 +0100622
623 return crypto_shash_import(desc, in);
Herbert Xu6fba00d2009-07-22 11:10:22 +0800624}
625
Eric Biggers758ec5a2020-01-02 20:04:37 -0800626static void cryptd_hash_free(struct ahash_instance *inst)
627{
628 struct hashd_instance_ctx *ctx = ahash_instance_ctx(inst);
629
630 crypto_drop_shash(&ctx->spawn);
631 kfree(inst);
632}
633
Herbert Xu9cd899a2009-07-14 18:45:45 +0800634static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb,
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700635 struct crypto_attr_type *algt,
Herbert Xu9cd899a2009-07-14 18:45:45 +0800636 struct cryptd_queue *queue)
Loc Hob8a28252008-05-14 21:23:00 +0800637{
Herbert Xu46309d82009-07-12 21:38:59 +0800638 struct hashd_instance_ctx *ctx;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800639 struct ahash_instance *inst;
Eric Biggers218c5032020-01-02 19:58:53 -0800640 struct shash_alg *alg;
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700641 u32 type;
642 u32 mask;
Herbert Xu46309d82009-07-12 21:38:59 +0800643 int err;
Loc Hob8a28252008-05-14 21:23:00 +0800644
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700645 cryptd_type_and_mask(algt, &type, &mask);
Stephan Mueller466a7b92015-03-30 21:57:06 +0200646
Eric Biggers218c5032020-01-02 19:58:53 -0800647 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
648 if (!inst)
649 return -ENOMEM;
Loc Hob8a28252008-05-14 21:23:00 +0800650
Herbert Xu0b535ad2009-07-14 19:11:32 +0800651 ctx = ahash_instance_ctx(inst);
Herbert Xu46309d82009-07-12 21:38:59 +0800652 ctx->queue = queue;
653
Eric Biggers218c5032020-01-02 19:58:53 -0800654 err = crypto_grab_shash(&ctx->spawn, ahash_crypto_instance(inst),
655 crypto_attr_alg_name(tb[1]), type, mask);
Herbert Xu46309d82009-07-12 21:38:59 +0800656 if (err)
Eric Biggers218c5032020-01-02 19:58:53 -0800657 goto err_free_inst;
658 alg = crypto_spawn_shash_alg(&ctx->spawn);
659
660 err = cryptd_init_instance(ahash_crypto_instance(inst), &alg->base);
661 if (err)
662 goto err_free_inst;
Herbert Xu46309d82009-07-12 21:38:59 +0800663
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700664 inst->alg.halg.base.cra_flags |= CRYPTO_ALG_ASYNC |
665 (alg->base.cra_flags & (CRYPTO_ALG_INTERNAL|
Eric Biggers218c5032020-01-02 19:58:53 -0800666 CRYPTO_ALG_OPTIONAL_KEY));
Eric Biggers218c5032020-01-02 19:58:53 -0800667 inst->alg.halg.digestsize = alg->digestsize;
668 inst->alg.halg.statesize = alg->statesize;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800669 inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
Loc Hob8a28252008-05-14 21:23:00 +0800670
Herbert Xu0b535ad2009-07-14 19:11:32 +0800671 inst->alg.halg.base.cra_init = cryptd_hash_init_tfm;
672 inst->alg.halg.base.cra_exit = cryptd_hash_exit_tfm;
Loc Hob8a28252008-05-14 21:23:00 +0800673
Herbert Xu0b535ad2009-07-14 19:11:32 +0800674 inst->alg.init = cryptd_hash_init_enqueue;
675 inst->alg.update = cryptd_hash_update_enqueue;
676 inst->alg.final = cryptd_hash_final_enqueue;
Herbert Xu6fba00d2009-07-22 11:10:22 +0800677 inst->alg.finup = cryptd_hash_finup_enqueue;
678 inst->alg.export = cryptd_hash_export;
679 inst->alg.import = cryptd_hash_import;
Eric Biggers218c5032020-01-02 19:58:53 -0800680 if (crypto_shash_alg_has_setkey(alg))
Eric Biggers841a3ff2018-01-03 11:16:23 -0800681 inst->alg.setkey = cryptd_hash_setkey;
Herbert Xu0b535ad2009-07-14 19:11:32 +0800682 inst->alg.digest = cryptd_hash_digest_enqueue;
Loc Hob8a28252008-05-14 21:23:00 +0800683
Eric Biggers758ec5a2020-01-02 20:04:37 -0800684 inst->free = cryptd_hash_free;
685
Herbert Xu0b535ad2009-07-14 19:11:32 +0800686 err = ahash_register_instance(tmpl, inst);
Herbert Xu9cd899a2009-07-14 18:45:45 +0800687 if (err) {
Eric Biggers218c5032020-01-02 19:58:53 -0800688err_free_inst:
Eric Biggersb8c0d742020-02-25 20:59:15 -0800689 cryptd_hash_free(inst);
Herbert Xu9cd899a2009-07-14 18:45:45 +0800690 }
Herbert Xu9cd899a2009-07-14 18:45:45 +0800691 return err;
Loc Hob8a28252008-05-14 21:23:00 +0800692}
693
Herbert Xu92b98762015-05-28 22:08:01 +0800694static int cryptd_aead_setkey(struct crypto_aead *parent,
695 const u8 *key, unsigned int keylen)
696{
697 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
698 struct crypto_aead *child = ctx->child;
699
700 return crypto_aead_setkey(child, key, keylen);
701}
702
703static int cryptd_aead_setauthsize(struct crypto_aead *parent,
704 unsigned int authsize)
705{
706 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent);
707 struct crypto_aead *child = ctx->child;
708
709 return crypto_aead_setauthsize(child, authsize);
710}
711
Adrian Hoban298c9262010-09-20 16:05:12 +0800712static void cryptd_aead_crypt(struct aead_request *req,
713 struct crypto_aead *child,
714 int err,
715 int (*crypt)(struct aead_request *req))
716{
717 struct cryptd_aead_request_ctx *rctx;
Herbert Xu81760ea2016-06-21 16:55:13 +0800718 struct cryptd_aead_ctx *ctx;
Herbert Xuec9f2002015-07-06 19:11:03 +0800719 crypto_completion_t compl;
Herbert Xu81760ea2016-06-21 16:55:13 +0800720 struct crypto_aead *tfm;
721 int refcnt;
Herbert Xuec9f2002015-07-06 19:11:03 +0800722
Adrian Hoban298c9262010-09-20 16:05:12 +0800723 rctx = aead_request_ctx(req);
Herbert Xuec9f2002015-07-06 19:11:03 +0800724 compl = rctx->complete;
Adrian Hoban298c9262010-09-20 16:05:12 +0800725
Herbert Xu31bd44e2016-08-25 16:49:51 +0800726 tfm = crypto_aead_reqtfm(req);
727
Adrian Hoban298c9262010-09-20 16:05:12 +0800728 if (unlikely(err == -EINPROGRESS))
729 goto out;
730 aead_request_set_tfm(req, child);
731 err = crypt( req );
Herbert Xu81760ea2016-06-21 16:55:13 +0800732
Adrian Hoban298c9262010-09-20 16:05:12 +0800733out:
Herbert Xu81760ea2016-06-21 16:55:13 +0800734 ctx = crypto_aead_ctx(tfm);
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800735 refcnt = refcount_read(&ctx->refcnt);
Herbert Xu81760ea2016-06-21 16:55:13 +0800736
Adrian Hoban298c9262010-09-20 16:05:12 +0800737 local_bh_disable();
Herbert Xuec9f2002015-07-06 19:11:03 +0800738 compl(&req->base, err);
Adrian Hoban298c9262010-09-20 16:05:12 +0800739 local_bh_enable();
Herbert Xu81760ea2016-06-21 16:55:13 +0800740
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800741 if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
Herbert Xu81760ea2016-06-21 16:55:13 +0800742 crypto_free_aead(tfm);
Adrian Hoban298c9262010-09-20 16:05:12 +0800743}
744
745static void cryptd_aead_encrypt(struct crypto_async_request *areq, int err)
746{
747 struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
748 struct crypto_aead *child = ctx->child;
749 struct aead_request *req;
750
751 req = container_of(areq, struct aead_request, base);
Herbert Xuba3749a2015-08-13 17:29:02 +0800752 cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->encrypt);
Adrian Hoban298c9262010-09-20 16:05:12 +0800753}
754
755static void cryptd_aead_decrypt(struct crypto_async_request *areq, int err)
756{
757 struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm);
758 struct crypto_aead *child = ctx->child;
759 struct aead_request *req;
760
761 req = container_of(areq, struct aead_request, base);
Herbert Xuba3749a2015-08-13 17:29:02 +0800762 cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->decrypt);
Adrian Hoban298c9262010-09-20 16:05:12 +0800763}
764
765static int cryptd_aead_enqueue(struct aead_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700766 crypto_completion_t compl)
Adrian Hoban298c9262010-09-20 16:05:12 +0800767{
768 struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req);
769 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
770 struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm));
771
772 rctx->complete = req->base.complete;
Mark Rustad3e3dc252014-07-25 02:53:38 -0700773 req->base.complete = compl;
Adrian Hoban298c9262010-09-20 16:05:12 +0800774 return cryptd_enqueue_request(queue, &req->base);
775}
776
777static int cryptd_aead_encrypt_enqueue(struct aead_request *req)
778{
779 return cryptd_aead_enqueue(req, cryptd_aead_encrypt );
780}
781
782static int cryptd_aead_decrypt_enqueue(struct aead_request *req)
783{
784 return cryptd_aead_enqueue(req, cryptd_aead_decrypt );
785}
786
Herbert Xuf614e542015-05-28 22:08:04 +0800787static int cryptd_aead_init_tfm(struct crypto_aead *tfm)
Adrian Hoban298c9262010-09-20 16:05:12 +0800788{
Herbert Xuf614e542015-05-28 22:08:04 +0800789 struct aead_instance *inst = aead_alg_instance(tfm);
790 struct aead_instance_ctx *ictx = aead_instance_ctx(inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800791 struct crypto_aead_spawn *spawn = &ictx->aead_spawn;
Herbert Xuf614e542015-05-28 22:08:04 +0800792 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
Adrian Hoban298c9262010-09-20 16:05:12 +0800793 struct crypto_aead *cipher;
794
795 cipher = crypto_spawn_aead(spawn);
796 if (IS_ERR(cipher))
797 return PTR_ERR(cipher);
798
Adrian Hoban298c9262010-09-20 16:05:12 +0800799 ctx->child = cipher;
Herbert Xuec9f2002015-07-06 19:11:03 +0800800 crypto_aead_set_reqsize(
801 tfm, max((unsigned)sizeof(struct cryptd_aead_request_ctx),
802 crypto_aead_reqsize(cipher)));
Adrian Hoban298c9262010-09-20 16:05:12 +0800803 return 0;
804}
805
Herbert Xuf614e542015-05-28 22:08:04 +0800806static void cryptd_aead_exit_tfm(struct crypto_aead *tfm)
Adrian Hoban298c9262010-09-20 16:05:12 +0800807{
Herbert Xuf614e542015-05-28 22:08:04 +0800808 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm);
Adrian Hoban298c9262010-09-20 16:05:12 +0800809 crypto_free_aead(ctx->child);
810}
811
Eric Biggers758ec5a2020-01-02 20:04:37 -0800812static void cryptd_aead_free(struct aead_instance *inst)
813{
814 struct aead_instance_ctx *ctx = aead_instance_ctx(inst);
815
816 crypto_drop_aead(&ctx->aead_spawn);
817 kfree(inst);
818}
819
Adrian Hoban298c9262010-09-20 16:05:12 +0800820static int cryptd_create_aead(struct crypto_template *tmpl,
821 struct rtattr **tb,
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700822 struct crypto_attr_type *algt,
Adrian Hoban298c9262010-09-20 16:05:12 +0800823 struct cryptd_queue *queue)
824{
825 struct aead_instance_ctx *ctx;
Herbert Xuf614e542015-05-28 22:08:04 +0800826 struct aead_instance *inst;
827 struct aead_alg *alg;
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700828 u32 type;
829 u32 mask;
Adrian Hoban298c9262010-09-20 16:05:12 +0800830 int err;
831
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700832 cryptd_type_and_mask(algt, &type, &mask);
Stephan Mueller466a7b92015-03-30 21:57:06 +0200833
Herbert Xu9b8c4562015-05-21 15:10:57 +0800834 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
835 if (!inst)
836 return -ENOMEM;
Adrian Hoban298c9262010-09-20 16:05:12 +0800837
Herbert Xuf614e542015-05-28 22:08:04 +0800838 ctx = aead_instance_ctx(inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800839 ctx->queue = queue;
840
Eric Biggerscd900f02020-01-02 19:58:46 -0800841 err = crypto_grab_aead(&ctx->aead_spawn, aead_crypto_instance(inst),
Eric Biggersb8c0d742020-02-25 20:59:15 -0800842 crypto_attr_alg_name(tb[1]), type, mask);
Adrian Hoban298c9262010-09-20 16:05:12 +0800843 if (err)
Eric Biggersb8c0d742020-02-25 20:59:15 -0800844 goto err_free_inst;
Adrian Hoban298c9262010-09-20 16:05:12 +0800845
Herbert Xuf614e542015-05-28 22:08:04 +0800846 alg = crypto_spawn_aead_alg(&ctx->aead_spawn);
847 err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base);
Herbert Xu9b8c4562015-05-21 15:10:57 +0800848 if (err)
Eric Biggersb8c0d742020-02-25 20:59:15 -0800849 goto err_free_inst;
Herbert Xu9b8c4562015-05-21 15:10:57 +0800850
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700851 inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC |
852 (alg->base.cra_flags & CRYPTO_ALG_INTERNAL);
Herbert Xuf614e542015-05-28 22:08:04 +0800853 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx);
Adrian Hoban298c9262010-09-20 16:05:12 +0800854
Herbert Xuf614e542015-05-28 22:08:04 +0800855 inst->alg.ivsize = crypto_aead_alg_ivsize(alg);
856 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
857
858 inst->alg.init = cryptd_aead_init_tfm;
859 inst->alg.exit = cryptd_aead_exit_tfm;
860 inst->alg.setkey = cryptd_aead_setkey;
861 inst->alg.setauthsize = cryptd_aead_setauthsize;
862 inst->alg.encrypt = cryptd_aead_encrypt_enqueue;
863 inst->alg.decrypt = cryptd_aead_decrypt_enqueue;
864
Eric Biggers758ec5a2020-01-02 20:04:37 -0800865 inst->free = cryptd_aead_free;
866
Herbert Xuf614e542015-05-28 22:08:04 +0800867 err = aead_register_instance(tmpl, inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800868 if (err) {
Eric Biggersb8c0d742020-02-25 20:59:15 -0800869err_free_inst:
870 cryptd_aead_free(inst);
Adrian Hoban298c9262010-09-20 16:05:12 +0800871 }
Adrian Hoban298c9262010-09-20 16:05:12 +0800872 return err;
873}
874
Huang Ying254eff72009-02-19 14:42:19 +0800875static struct cryptd_queue queue;
Herbert Xu124b53d2007-04-16 20:49:20 +1000876
Herbert Xu9cd899a2009-07-14 18:45:45 +0800877static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu124b53d2007-04-16 20:49:20 +1000878{
879 struct crypto_attr_type *algt;
880
881 algt = crypto_get_attr_type(tb);
882 if (IS_ERR(algt))
Herbert Xu9cd899a2009-07-14 18:45:45 +0800883 return PTR_ERR(algt);
Herbert Xu124b53d2007-04-16 20:49:20 +1000884
885 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
Eric Biggersc65058b2019-10-25 12:41:12 -0700886 case CRYPTO_ALG_TYPE_SKCIPHER:
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700887 return cryptd_create_skcipher(tmpl, tb, algt, &queue);
Eric Biggers84ede582019-05-20 09:54:46 -0700888 case CRYPTO_ALG_TYPE_HASH:
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700889 return cryptd_create_hash(tmpl, tb, algt, &queue);
Adrian Hoban298c9262010-09-20 16:05:12 +0800890 case CRYPTO_ALG_TYPE_AEAD:
Eric Biggers7bcb2c92020-07-09 23:20:38 -0700891 return cryptd_create_aead(tmpl, tb, algt, &queue);
Herbert Xu124b53d2007-04-16 20:49:20 +1000892 }
893
Herbert Xu9cd899a2009-07-14 18:45:45 +0800894 return -EINVAL;
Herbert Xu124b53d2007-04-16 20:49:20 +1000895}
896
Herbert Xu124b53d2007-04-16 20:49:20 +1000897static struct crypto_template cryptd_tmpl = {
898 .name = "cryptd",
Herbert Xu9cd899a2009-07-14 18:45:45 +0800899 .create = cryptd_create,
Herbert Xu124b53d2007-04-16 20:49:20 +1000900 .module = THIS_MODULE,
901};
902
Herbert Xu4e0958d2016-11-22 20:08:23 +0800903struct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name,
904 u32 type, u32 mask)
905{
906 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
907 struct cryptd_skcipher_ctx *ctx;
908 struct crypto_skcipher *tfm;
909
910 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
911 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
912 return ERR_PTR(-EINVAL);
913
914 tfm = crypto_alloc_skcipher(cryptd_alg_name, type, mask);
915 if (IS_ERR(tfm))
916 return ERR_CAST(tfm);
917
918 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
919 crypto_free_skcipher(tfm);
920 return ERR_PTR(-EINVAL);
921 }
922
923 ctx = crypto_skcipher_ctx(tfm);
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800924 refcount_set(&ctx->refcnt, 1);
Herbert Xu4e0958d2016-11-22 20:08:23 +0800925
926 return container_of(tfm, struct cryptd_skcipher, base);
927}
928EXPORT_SYMBOL_GPL(cryptd_alloc_skcipher);
929
930struct crypto_skcipher *cryptd_skcipher_child(struct cryptd_skcipher *tfm)
931{
932 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
933
Kees Cook36b38752018-09-18 19:10:52 -0700934 return &ctx->child->base;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800935}
936EXPORT_SYMBOL_GPL(cryptd_skcipher_child);
937
938bool cryptd_skcipher_queued(struct cryptd_skcipher *tfm)
939{
940 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
941
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800942 return refcount_read(&ctx->refcnt) - 1;
Herbert Xu4e0958d2016-11-22 20:08:23 +0800943}
944EXPORT_SYMBOL_GPL(cryptd_skcipher_queued);
945
946void cryptd_free_skcipher(struct cryptd_skcipher *tfm)
947{
948 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
949
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800950 if (refcount_dec_and_test(&ctx->refcnt))
Herbert Xu4e0958d2016-11-22 20:08:23 +0800951 crypto_free_skcipher(&tfm->base);
952}
953EXPORT_SYMBOL_GPL(cryptd_free_skcipher);
954
Huang Yingace13662009-08-06 15:35:20 +1000955struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name,
956 u32 type, u32 mask)
957{
958 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu81760ea2016-06-21 16:55:13 +0800959 struct cryptd_hash_ctx *ctx;
Huang Yingace13662009-08-06 15:35:20 +1000960 struct crypto_ahash *tfm;
961
962 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
963 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
964 return ERR_PTR(-EINVAL);
965 tfm = crypto_alloc_ahash(cryptd_alg_name, type, mask);
966 if (IS_ERR(tfm))
967 return ERR_CAST(tfm);
968 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
969 crypto_free_ahash(tfm);
970 return ERR_PTR(-EINVAL);
971 }
972
Herbert Xu81760ea2016-06-21 16:55:13 +0800973 ctx = crypto_ahash_ctx(tfm);
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800974 refcount_set(&ctx->refcnt, 1);
Herbert Xu81760ea2016-06-21 16:55:13 +0800975
Huang Yingace13662009-08-06 15:35:20 +1000976 return __cryptd_ahash_cast(tfm);
977}
978EXPORT_SYMBOL_GPL(cryptd_alloc_ahash);
979
980struct crypto_shash *cryptd_ahash_child(struct cryptd_ahash *tfm)
981{
982 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
983
984 return ctx->child;
985}
986EXPORT_SYMBOL_GPL(cryptd_ahash_child);
987
Huang Ying0e1227d2009-10-19 11:53:06 +0900988struct shash_desc *cryptd_shash_desc(struct ahash_request *req)
989{
990 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
991 return &rctx->desc;
992}
993EXPORT_SYMBOL_GPL(cryptd_shash_desc);
994
Herbert Xu81760ea2016-06-21 16:55:13 +0800995bool cryptd_ahash_queued(struct cryptd_ahash *tfm)
996{
997 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
998
Chuhong Yuan43b970f2019-08-08 16:00:22 +0800999 return refcount_read(&ctx->refcnt) - 1;
Herbert Xu81760ea2016-06-21 16:55:13 +08001000}
1001EXPORT_SYMBOL_GPL(cryptd_ahash_queued);
1002
Huang Yingace13662009-08-06 15:35:20 +10001003void cryptd_free_ahash(struct cryptd_ahash *tfm)
1004{
Herbert Xu81760ea2016-06-21 16:55:13 +08001005 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
1006
Chuhong Yuan43b970f2019-08-08 16:00:22 +08001007 if (refcount_dec_and_test(&ctx->refcnt))
Herbert Xu81760ea2016-06-21 16:55:13 +08001008 crypto_free_ahash(&tfm->base);
Huang Yingace13662009-08-06 15:35:20 +10001009}
1010EXPORT_SYMBOL_GPL(cryptd_free_ahash);
1011
Adrian Hoban298c9262010-09-20 16:05:12 +08001012struct cryptd_aead *cryptd_alloc_aead(const char *alg_name,
1013 u32 type, u32 mask)
1014{
1015 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME];
Herbert Xu81760ea2016-06-21 16:55:13 +08001016 struct cryptd_aead_ctx *ctx;
Adrian Hoban298c9262010-09-20 16:05:12 +08001017 struct crypto_aead *tfm;
1018
1019 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME,
1020 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME)
1021 return ERR_PTR(-EINVAL);
1022 tfm = crypto_alloc_aead(cryptd_alg_name, type, mask);
1023 if (IS_ERR(tfm))
1024 return ERR_CAST(tfm);
1025 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) {
1026 crypto_free_aead(tfm);
1027 return ERR_PTR(-EINVAL);
1028 }
Herbert Xu81760ea2016-06-21 16:55:13 +08001029
1030 ctx = crypto_aead_ctx(tfm);
Chuhong Yuan43b970f2019-08-08 16:00:22 +08001031 refcount_set(&ctx->refcnt, 1);
Herbert Xu81760ea2016-06-21 16:55:13 +08001032
Adrian Hoban298c9262010-09-20 16:05:12 +08001033 return __cryptd_aead_cast(tfm);
1034}
1035EXPORT_SYMBOL_GPL(cryptd_alloc_aead);
1036
1037struct crypto_aead *cryptd_aead_child(struct cryptd_aead *tfm)
1038{
1039 struct cryptd_aead_ctx *ctx;
1040 ctx = crypto_aead_ctx(&tfm->base);
1041 return ctx->child;
1042}
1043EXPORT_SYMBOL_GPL(cryptd_aead_child);
1044
Herbert Xu81760ea2016-06-21 16:55:13 +08001045bool cryptd_aead_queued(struct cryptd_aead *tfm)
1046{
1047 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
1048
Chuhong Yuan43b970f2019-08-08 16:00:22 +08001049 return refcount_read(&ctx->refcnt) - 1;
Herbert Xu81760ea2016-06-21 16:55:13 +08001050}
1051EXPORT_SYMBOL_GPL(cryptd_aead_queued);
1052
Adrian Hoban298c9262010-09-20 16:05:12 +08001053void cryptd_free_aead(struct cryptd_aead *tfm)
1054{
Herbert Xu81760ea2016-06-21 16:55:13 +08001055 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
1056
Chuhong Yuan43b970f2019-08-08 16:00:22 +08001057 if (refcount_dec_and_test(&ctx->refcnt))
Herbert Xu81760ea2016-06-21 16:55:13 +08001058 crypto_free_aead(&tfm->base);
Adrian Hoban298c9262010-09-20 16:05:12 +08001059}
1060EXPORT_SYMBOL_GPL(cryptd_free_aead);
1061
Herbert Xu124b53d2007-04-16 20:49:20 +10001062static int __init cryptd_init(void)
1063{
1064 int err;
1065
Eric Biggers3e56e162019-05-20 09:53:58 -07001066 cryptd_wq = alloc_workqueue("cryptd", WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE,
1067 1);
1068 if (!cryptd_wq)
1069 return -ENOMEM;
1070
Jon Maxwellc3a53602017-11-22 16:08:17 +11001071 err = cryptd_init_queue(&queue, cryptd_max_cpu_qlen);
Herbert Xu124b53d2007-04-16 20:49:20 +10001072 if (err)
Eric Biggers3e56e162019-05-20 09:53:58 -07001073 goto err_destroy_wq;
Herbert Xu124b53d2007-04-16 20:49:20 +10001074
1075 err = crypto_register_template(&cryptd_tmpl);
1076 if (err)
Eric Biggers3e56e162019-05-20 09:53:58 -07001077 goto err_fini_queue;
Herbert Xu124b53d2007-04-16 20:49:20 +10001078
Eric Biggers3e56e162019-05-20 09:53:58 -07001079 return 0;
1080
1081err_fini_queue:
1082 cryptd_fini_queue(&queue);
1083err_destroy_wq:
1084 destroy_workqueue(cryptd_wq);
Herbert Xu124b53d2007-04-16 20:49:20 +10001085 return err;
1086}
1087
1088static void __exit cryptd_exit(void)
1089{
Eric Biggers3e56e162019-05-20 09:53:58 -07001090 destroy_workqueue(cryptd_wq);
Huang Ying254eff72009-02-19 14:42:19 +08001091 cryptd_fini_queue(&queue);
Herbert Xu124b53d2007-04-16 20:49:20 +10001092 crypto_unregister_template(&cryptd_tmpl);
1093}
1094
Herbert Xub2bac6ac2011-08-19 16:11:23 +08001095subsys_initcall(cryptd_init);
Herbert Xu124b53d2007-04-16 20:49:20 +10001096module_exit(cryptd_exit);
1097
1098MODULE_LICENSE("GPL");
1099MODULE_DESCRIPTION("Software async crypto daemon");
Kees Cook4943ba12014-11-24 16:32:38 -08001100MODULE_ALIAS_CRYPTO("cryptd");