Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Software async crypto daemon. |
| 3 | * |
| 4 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
| 5 | * |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 6 | * Added AEAD support to cryptd. |
| 7 | * Authors: Tadeusz Struk (tadeusz.struk@intel.com) |
| 8 | * Adrian Hoban <adrian.hoban@intel.com> |
| 9 | * Gabriele Paoloni <gabriele.paoloni@intel.com> |
| 10 | * Aidan O'Mahony (aidan.o.mahony@intel.com) |
| 11 | * Copyright (c) 2010, Intel Corporation. |
| 12 | * |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify it |
| 14 | * under the terms of the GNU General Public License as published by the Free |
| 15 | * Software Foundation; either version 2 of the License, or (at your option) |
| 16 | * any later version. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <crypto/algapi.h> |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 21 | #include <crypto/internal/hash.h> |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 22 | #include <crypto/internal/aead.h> |
Huang Ying | 1cac2cb | 2009-01-18 16:19:46 +1100 | [diff] [blame] | 23 | #include <crypto/cryptd.h> |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 24 | #include <crypto/crypto_wq.h> |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 25 | #include <linux/atomic.h> |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 26 | #include <linux/err.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/kernel.h> |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 29 | #include <linux/list.h> |
| 30 | #include <linux/module.h> |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 31 | #include <linux/scatterlist.h> |
| 32 | #include <linux/sched.h> |
| 33 | #include <linux/slab.h> |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 34 | |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 35 | #define CRYPTD_MAX_CPU_QLEN 1000 |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 36 | |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 37 | struct cryptd_cpu_queue { |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 38 | struct crypto_queue queue; |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 39 | struct work_struct work; |
| 40 | }; |
| 41 | |
| 42 | struct cryptd_queue { |
Tejun Heo | a29d8b8 | 2010-02-02 14:39:15 +0900 | [diff] [blame] | 43 | struct cryptd_cpu_queue __percpu *cpu_queue; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | struct cryptd_instance_ctx { |
| 47 | struct crypto_spawn spawn; |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 48 | struct cryptd_queue *queue; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 49 | }; |
| 50 | |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 51 | struct hashd_instance_ctx { |
| 52 | struct crypto_shash_spawn spawn; |
| 53 | struct cryptd_queue *queue; |
| 54 | }; |
| 55 | |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 56 | struct aead_instance_ctx { |
| 57 | struct crypto_aead_spawn aead_spawn; |
| 58 | struct cryptd_queue *queue; |
| 59 | }; |
| 60 | |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 61 | struct cryptd_blkcipher_ctx { |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 62 | atomic_t refcnt; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 63 | struct crypto_blkcipher *child; |
| 64 | }; |
| 65 | |
| 66 | struct cryptd_blkcipher_request_ctx { |
| 67 | crypto_completion_t complete; |
| 68 | }; |
| 69 | |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 70 | struct cryptd_hash_ctx { |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 71 | atomic_t refcnt; |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 72 | struct crypto_shash *child; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | struct cryptd_hash_request_ctx { |
| 76 | crypto_completion_t complete; |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 77 | struct shash_desc desc; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 78 | }; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 79 | |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 80 | struct cryptd_aead_ctx { |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 81 | atomic_t refcnt; |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 82 | struct crypto_aead *child; |
| 83 | }; |
| 84 | |
| 85 | struct cryptd_aead_request_ctx { |
| 86 | crypto_completion_t complete; |
| 87 | }; |
| 88 | |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 89 | static void cryptd_queue_worker(struct work_struct *work); |
| 90 | |
| 91 | static int cryptd_init_queue(struct cryptd_queue *queue, |
| 92 | unsigned int max_cpu_qlen) |
| 93 | { |
| 94 | int cpu; |
| 95 | struct cryptd_cpu_queue *cpu_queue; |
| 96 | |
| 97 | queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue); |
| 98 | if (!queue->cpu_queue) |
| 99 | return -ENOMEM; |
| 100 | for_each_possible_cpu(cpu) { |
| 101 | cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu); |
| 102 | crypto_init_queue(&cpu_queue->queue, max_cpu_qlen); |
| 103 | INIT_WORK(&cpu_queue->work, cryptd_queue_worker); |
| 104 | } |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static void cryptd_fini_queue(struct cryptd_queue *queue) |
| 109 | { |
| 110 | int cpu; |
| 111 | struct cryptd_cpu_queue *cpu_queue; |
| 112 | |
| 113 | for_each_possible_cpu(cpu) { |
| 114 | cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu); |
| 115 | BUG_ON(cpu_queue->queue.qlen); |
| 116 | } |
| 117 | free_percpu(queue->cpu_queue); |
| 118 | } |
| 119 | |
| 120 | static int cryptd_enqueue_request(struct cryptd_queue *queue, |
| 121 | struct crypto_async_request *request) |
| 122 | { |
| 123 | int cpu, err; |
| 124 | struct cryptd_cpu_queue *cpu_queue; |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 125 | atomic_t *refcnt; |
| 126 | bool may_backlog; |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 127 | |
| 128 | cpu = get_cpu(); |
Christoph Lameter | 0b44f48 | 2009-10-03 19:48:23 +0900 | [diff] [blame] | 129 | cpu_queue = this_cpu_ptr(queue->cpu_queue); |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 130 | err = crypto_enqueue_request(&cpu_queue->queue, request); |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 131 | |
| 132 | refcnt = crypto_tfm_ctx(request->tfm); |
| 133 | may_backlog = request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG; |
| 134 | |
| 135 | if (err == -EBUSY && !may_backlog) |
| 136 | goto out_put_cpu; |
| 137 | |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 138 | queue_work_on(cpu, kcrypto_wq, &cpu_queue->work); |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 139 | |
| 140 | if (!atomic_read(refcnt)) |
| 141 | goto out_put_cpu; |
| 142 | |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 143 | atomic_inc(refcnt); |
| 144 | |
| 145 | out_put_cpu: |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 146 | put_cpu(); |
| 147 | |
| 148 | return err; |
| 149 | } |
| 150 | |
| 151 | /* Called in workqueue context, do one real cryption work (via |
| 152 | * req->complete) and reschedule itself if there are more work to |
| 153 | * do. */ |
| 154 | static void cryptd_queue_worker(struct work_struct *work) |
| 155 | { |
| 156 | struct cryptd_cpu_queue *cpu_queue; |
| 157 | struct crypto_async_request *req, *backlog; |
| 158 | |
| 159 | cpu_queue = container_of(work, struct cryptd_cpu_queue, work); |
Jussi Kivilinna | 9efade1 | 2012-10-21 20:42:28 +0300 | [diff] [blame] | 160 | /* |
| 161 | * Only handle one request at a time to avoid hogging crypto workqueue. |
| 162 | * preempt_disable/enable is used to prevent being preempted by |
| 163 | * cryptd_enqueue_request(). local_bh_disable/enable is used to prevent |
| 164 | * cryptd_enqueue_request() being accessed from software interrupts. |
| 165 | */ |
| 166 | local_bh_disable(); |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 167 | preempt_disable(); |
| 168 | backlog = crypto_get_backlog(&cpu_queue->queue); |
| 169 | req = crypto_dequeue_request(&cpu_queue->queue); |
| 170 | preempt_enable(); |
Jussi Kivilinna | 9efade1 | 2012-10-21 20:42:28 +0300 | [diff] [blame] | 171 | local_bh_enable(); |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 172 | |
| 173 | if (!req) |
| 174 | return; |
| 175 | |
| 176 | if (backlog) |
| 177 | backlog->complete(backlog, -EINPROGRESS); |
| 178 | req->complete(req, 0); |
| 179 | |
| 180 | if (cpu_queue->queue.qlen) |
| 181 | queue_work(kcrypto_wq, &cpu_queue->work); |
| 182 | } |
| 183 | |
| 184 | static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm) |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 185 | { |
| 186 | struct crypto_instance *inst = crypto_tfm_alg_instance(tfm); |
| 187 | struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst); |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 188 | return ictx->queue; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 189 | } |
| 190 | |
Stephan Mueller | 466a7b9 | 2015-03-30 21:57:06 +0200 | [diff] [blame] | 191 | static inline void cryptd_check_internal(struct rtattr **tb, u32 *type, |
| 192 | u32 *mask) |
| 193 | { |
| 194 | struct crypto_attr_type *algt; |
| 195 | |
| 196 | algt = crypto_get_attr_type(tb); |
| 197 | if (IS_ERR(algt)) |
| 198 | return; |
Herbert Xu | f6da320 | 2015-07-09 07:17:19 +0800 | [diff] [blame] | 199 | |
Herbert Xu | 5e4b8c1 | 2015-08-13 17:29:06 +0800 | [diff] [blame] | 200 | *type |= algt->type & CRYPTO_ALG_INTERNAL; |
| 201 | *mask |= algt->mask & CRYPTO_ALG_INTERNAL; |
Stephan Mueller | 466a7b9 | 2015-03-30 21:57:06 +0200 | [diff] [blame] | 202 | } |
| 203 | |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 204 | static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent, |
| 205 | const u8 *key, unsigned int keylen) |
| 206 | { |
| 207 | struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent); |
| 208 | struct crypto_blkcipher *child = ctx->child; |
| 209 | int err; |
| 210 | |
| 211 | crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 212 | crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) & |
| 213 | CRYPTO_TFM_REQ_MASK); |
| 214 | err = crypto_blkcipher_setkey(child, key, keylen); |
| 215 | crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) & |
| 216 | CRYPTO_TFM_RES_MASK); |
| 217 | return err; |
| 218 | } |
| 219 | |
| 220 | static void cryptd_blkcipher_crypt(struct ablkcipher_request *req, |
| 221 | struct crypto_blkcipher *child, |
| 222 | int err, |
| 223 | int (*crypt)(struct blkcipher_desc *desc, |
| 224 | struct scatterlist *dst, |
| 225 | struct scatterlist *src, |
| 226 | unsigned int len)) |
| 227 | { |
| 228 | struct cryptd_blkcipher_request_ctx *rctx; |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 229 | struct cryptd_blkcipher_ctx *ctx; |
| 230 | struct crypto_ablkcipher *tfm; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 231 | struct blkcipher_desc desc; |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 232 | int refcnt; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 233 | |
| 234 | rctx = ablkcipher_request_ctx(req); |
| 235 | |
Herbert Xu | 93aa7f8 | 2008-05-07 21:10:13 +0800 | [diff] [blame] | 236 | if (unlikely(err == -EINPROGRESS)) |
| 237 | goto out; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 238 | |
| 239 | desc.tfm = child; |
| 240 | desc.info = req->info; |
| 241 | desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 242 | |
| 243 | err = crypt(&desc, req->dst, req->src, req->nbytes); |
| 244 | |
| 245 | req->base.complete = rctx->complete; |
| 246 | |
Herbert Xu | 93aa7f8 | 2008-05-07 21:10:13 +0800 | [diff] [blame] | 247 | out: |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 248 | tfm = crypto_ablkcipher_reqtfm(req); |
| 249 | ctx = crypto_ablkcipher_ctx(tfm); |
| 250 | refcnt = atomic_read(&ctx->refcnt); |
| 251 | |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 252 | local_bh_disable(); |
Herbert Xu | 93aa7f8 | 2008-05-07 21:10:13 +0800 | [diff] [blame] | 253 | rctx->complete(&req->base, err); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 254 | local_bh_enable(); |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 255 | |
| 256 | if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt)) |
| 257 | crypto_free_ablkcipher(tfm); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err) |
| 261 | { |
| 262 | struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm); |
| 263 | struct crypto_blkcipher *child = ctx->child; |
| 264 | |
| 265 | cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err, |
| 266 | crypto_blkcipher_crt(child)->encrypt); |
| 267 | } |
| 268 | |
| 269 | static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err) |
| 270 | { |
| 271 | struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm); |
| 272 | struct crypto_blkcipher *child = ctx->child; |
| 273 | |
| 274 | cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err, |
| 275 | crypto_blkcipher_crt(child)->decrypt); |
| 276 | } |
| 277 | |
| 278 | static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req, |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 279 | crypto_completion_t compl) |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 280 | { |
| 281 | struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req); |
| 282 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 283 | struct cryptd_queue *queue; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 284 | |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 285 | queue = cryptd_get_queue(crypto_ablkcipher_tfm(tfm)); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 286 | rctx->complete = req->base.complete; |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 287 | req->base.complete = compl; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 288 | |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 289 | return cryptd_enqueue_request(queue, &req->base); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req) |
| 293 | { |
| 294 | return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt); |
| 295 | } |
| 296 | |
| 297 | static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req) |
| 298 | { |
| 299 | return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt); |
| 300 | } |
| 301 | |
| 302 | static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm) |
| 303 | { |
| 304 | struct crypto_instance *inst = crypto_tfm_alg_instance(tfm); |
| 305 | struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst); |
| 306 | struct crypto_spawn *spawn = &ictx->spawn; |
| 307 | struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm); |
| 308 | struct crypto_blkcipher *cipher; |
| 309 | |
| 310 | cipher = crypto_spawn_blkcipher(spawn); |
| 311 | if (IS_ERR(cipher)) |
| 312 | return PTR_ERR(cipher); |
| 313 | |
| 314 | ctx->child = cipher; |
| 315 | tfm->crt_ablkcipher.reqsize = |
| 316 | sizeof(struct cryptd_blkcipher_request_ctx); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm) |
| 321 | { |
| 322 | struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 323 | |
| 324 | crypto_free_blkcipher(ctx->child); |
| 325 | } |
| 326 | |
Herbert Xu | 9b8c456 | 2015-05-21 15:10:57 +0800 | [diff] [blame] | 327 | static int cryptd_init_instance(struct crypto_instance *inst, |
| 328 | struct crypto_alg *alg) |
| 329 | { |
| 330 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 331 | "cryptd(%s)", |
| 332 | alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
| 333 | return -ENAMETOOLONG; |
| 334 | |
| 335 | memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); |
| 336 | |
| 337 | inst->alg.cra_priority = alg->cra_priority + 50; |
| 338 | inst->alg.cra_blocksize = alg->cra_blocksize; |
| 339 | inst->alg.cra_alignmask = alg->cra_alignmask; |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 344 | static void *cryptd_alloc_instance(struct crypto_alg *alg, unsigned int head, |
| 345 | unsigned int tail) |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 346 | { |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 347 | char *p; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 348 | struct crypto_instance *inst; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 349 | int err; |
| 350 | |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 351 | p = kzalloc(head + sizeof(*inst) + tail, GFP_KERNEL); |
| 352 | if (!p) |
| 353 | return ERR_PTR(-ENOMEM); |
| 354 | |
| 355 | inst = (void *)(p + head); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 356 | |
Herbert Xu | 9b8c456 | 2015-05-21 15:10:57 +0800 | [diff] [blame] | 357 | err = cryptd_init_instance(inst, alg); |
| 358 | if (err) |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 359 | goto out_free_inst; |
| 360 | |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 361 | out: |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 362 | return p; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 363 | |
| 364 | out_free_inst: |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 365 | kfree(p); |
| 366 | p = ERR_PTR(err); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 367 | goto out; |
| 368 | } |
| 369 | |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 370 | static int cryptd_create_blkcipher(struct crypto_template *tmpl, |
| 371 | struct rtattr **tb, |
| 372 | struct cryptd_queue *queue) |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 373 | { |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 374 | struct cryptd_instance_ctx *ctx; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 375 | struct crypto_instance *inst; |
| 376 | struct crypto_alg *alg; |
Stephan Mueller | 466a7b9 | 2015-03-30 21:57:06 +0200 | [diff] [blame] | 377 | u32 type = CRYPTO_ALG_TYPE_BLKCIPHER; |
| 378 | u32 mask = CRYPTO_ALG_TYPE_MASK; |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 379 | int err; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 380 | |
Stephan Mueller | 466a7b9 | 2015-03-30 21:57:06 +0200 | [diff] [blame] | 381 | cryptd_check_internal(tb, &type, &mask); |
| 382 | |
| 383 | alg = crypto_get_attr_alg(tb, type, mask); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 384 | if (IS_ERR(alg)) |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 385 | return PTR_ERR(alg); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 386 | |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 387 | inst = cryptd_alloc_instance(alg, 0, sizeof(*ctx)); |
Steffen Klassert | 05ed875 | 2009-07-15 16:51:04 +0800 | [diff] [blame] | 388 | err = PTR_ERR(inst); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 389 | if (IS_ERR(inst)) |
| 390 | goto out_put_alg; |
| 391 | |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 392 | ctx = crypto_instance_ctx(inst); |
| 393 | ctx->queue = queue; |
| 394 | |
| 395 | err = crypto_init_spawn(&ctx->spawn, alg, inst, |
| 396 | CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); |
| 397 | if (err) |
| 398 | goto out_free_inst; |
| 399 | |
Stephan Mueller | 466a7b9 | 2015-03-30 21:57:06 +0200 | [diff] [blame] | 400 | type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC; |
| 401 | if (alg->cra_flags & CRYPTO_ALG_INTERNAL) |
| 402 | type |= CRYPTO_ALG_INTERNAL; |
| 403 | inst->alg.cra_flags = type; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 404 | inst->alg.cra_type = &crypto_ablkcipher_type; |
| 405 | |
| 406 | inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize; |
| 407 | inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize; |
| 408 | inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize; |
| 409 | |
Herbert Xu | 927eead | 2007-11-27 21:15:31 +0800 | [diff] [blame] | 410 | inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv; |
| 411 | |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 412 | inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx); |
| 413 | |
| 414 | inst->alg.cra_init = cryptd_blkcipher_init_tfm; |
| 415 | inst->alg.cra_exit = cryptd_blkcipher_exit_tfm; |
| 416 | |
| 417 | inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey; |
| 418 | inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue; |
| 419 | inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue; |
| 420 | |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 421 | err = crypto_register_instance(tmpl, inst); |
| 422 | if (err) { |
| 423 | crypto_drop_spawn(&ctx->spawn); |
| 424 | out_free_inst: |
| 425 | kfree(inst); |
| 426 | } |
| 427 | |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 428 | out_put_alg: |
| 429 | crypto_mod_put(alg); |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 430 | return err; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 431 | } |
| 432 | |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 433 | static int cryptd_hash_init_tfm(struct crypto_tfm *tfm) |
| 434 | { |
| 435 | struct crypto_instance *inst = crypto_tfm_alg_instance(tfm); |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 436 | struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst); |
| 437 | struct crypto_shash_spawn *spawn = &ictx->spawn; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 438 | struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm); |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 439 | struct crypto_shash *hash; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 440 | |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 441 | hash = crypto_spawn_shash(spawn); |
| 442 | if (IS_ERR(hash)) |
| 443 | return PTR_ERR(hash); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 444 | |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 445 | ctx->child = hash; |
Herbert Xu | 0d6669e | 2009-07-12 23:06:33 +0800 | [diff] [blame] | 446 | crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), |
| 447 | sizeof(struct cryptd_hash_request_ctx) + |
| 448 | crypto_shash_descsize(hash)); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm) |
| 453 | { |
| 454 | struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 455 | |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 456 | crypto_free_shash(ctx->child); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | static int cryptd_hash_setkey(struct crypto_ahash *parent, |
| 460 | const u8 *key, unsigned int keylen) |
| 461 | { |
| 462 | struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent); |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 463 | struct crypto_shash *child = ctx->child; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 464 | int err; |
| 465 | |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 466 | crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 467 | crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) & |
| 468 | CRYPTO_TFM_REQ_MASK); |
| 469 | err = crypto_shash_setkey(child, key, keylen); |
| 470 | crypto_ahash_set_flags(parent, crypto_shash_get_flags(child) & |
| 471 | CRYPTO_TFM_RES_MASK); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 472 | return err; |
| 473 | } |
| 474 | |
| 475 | static int cryptd_hash_enqueue(struct ahash_request *req, |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 476 | crypto_completion_t compl) |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 477 | { |
| 478 | struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); |
| 479 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 480 | struct cryptd_queue *queue = |
| 481 | cryptd_get_queue(crypto_ahash_tfm(tfm)); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 482 | |
| 483 | rctx->complete = req->base.complete; |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 484 | req->base.complete = compl; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 485 | |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 486 | return cryptd_enqueue_request(queue, &req->base); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 487 | } |
| 488 | |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 489 | static void cryptd_hash_complete(struct ahash_request *req, int err) |
| 490 | { |
| 491 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 492 | struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm); |
| 493 | struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); |
| 494 | int refcnt = atomic_read(&ctx->refcnt); |
| 495 | |
| 496 | local_bh_disable(); |
| 497 | rctx->complete(&req->base, err); |
| 498 | local_bh_enable(); |
| 499 | |
| 500 | if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt)) |
| 501 | crypto_free_ahash(tfm); |
| 502 | } |
| 503 | |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 504 | static void cryptd_hash_init(struct crypto_async_request *req_async, int err) |
| 505 | { |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 506 | struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm); |
| 507 | struct crypto_shash *child = ctx->child; |
| 508 | struct ahash_request *req = ahash_request_cast(req_async); |
| 509 | struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); |
| 510 | struct shash_desc *desc = &rctx->desc; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 511 | |
| 512 | if (unlikely(err == -EINPROGRESS)) |
| 513 | goto out; |
| 514 | |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 515 | desc->tfm = child; |
| 516 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 517 | |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 518 | err = crypto_shash_init(desc); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 519 | |
| 520 | req->base.complete = rctx->complete; |
| 521 | |
| 522 | out: |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 523 | cryptd_hash_complete(req, err); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static int cryptd_hash_init_enqueue(struct ahash_request *req) |
| 527 | { |
| 528 | return cryptd_hash_enqueue(req, cryptd_hash_init); |
| 529 | } |
| 530 | |
| 531 | static void cryptd_hash_update(struct crypto_async_request *req_async, int err) |
| 532 | { |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 533 | struct ahash_request *req = ahash_request_cast(req_async); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 534 | struct cryptd_hash_request_ctx *rctx; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 535 | |
| 536 | rctx = ahash_request_ctx(req); |
| 537 | |
| 538 | if (unlikely(err == -EINPROGRESS)) |
| 539 | goto out; |
| 540 | |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 541 | err = shash_ahash_update(req, &rctx->desc); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 542 | |
| 543 | req->base.complete = rctx->complete; |
| 544 | |
| 545 | out: |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 546 | cryptd_hash_complete(req, err); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | static int cryptd_hash_update_enqueue(struct ahash_request *req) |
| 550 | { |
| 551 | return cryptd_hash_enqueue(req, cryptd_hash_update); |
| 552 | } |
| 553 | |
| 554 | static void cryptd_hash_final(struct crypto_async_request *req_async, int err) |
| 555 | { |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 556 | struct ahash_request *req = ahash_request_cast(req_async); |
| 557 | struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 558 | |
| 559 | if (unlikely(err == -EINPROGRESS)) |
| 560 | goto out; |
| 561 | |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 562 | err = crypto_shash_final(&rctx->desc, req->result); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 563 | |
| 564 | req->base.complete = rctx->complete; |
| 565 | |
| 566 | out: |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 567 | cryptd_hash_complete(req, err); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static int cryptd_hash_final_enqueue(struct ahash_request *req) |
| 571 | { |
| 572 | return cryptd_hash_enqueue(req, cryptd_hash_final); |
| 573 | } |
| 574 | |
Herbert Xu | 6fba00d | 2009-07-22 11:10:22 +0800 | [diff] [blame] | 575 | static void cryptd_hash_finup(struct crypto_async_request *req_async, int err) |
| 576 | { |
| 577 | struct ahash_request *req = ahash_request_cast(req_async); |
| 578 | struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); |
| 579 | |
| 580 | if (unlikely(err == -EINPROGRESS)) |
| 581 | goto out; |
| 582 | |
| 583 | err = shash_ahash_finup(req, &rctx->desc); |
| 584 | |
| 585 | req->base.complete = rctx->complete; |
| 586 | |
| 587 | out: |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 588 | cryptd_hash_complete(req, err); |
Herbert Xu | 6fba00d | 2009-07-22 11:10:22 +0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | static int cryptd_hash_finup_enqueue(struct ahash_request *req) |
| 592 | { |
| 593 | return cryptd_hash_enqueue(req, cryptd_hash_finup); |
| 594 | } |
| 595 | |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 596 | static void cryptd_hash_digest(struct crypto_async_request *req_async, int err) |
| 597 | { |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 598 | struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm); |
| 599 | struct crypto_shash *child = ctx->child; |
| 600 | struct ahash_request *req = ahash_request_cast(req_async); |
| 601 | struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); |
| 602 | struct shash_desc *desc = &rctx->desc; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 603 | |
| 604 | if (unlikely(err == -EINPROGRESS)) |
| 605 | goto out; |
| 606 | |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 607 | desc->tfm = child; |
| 608 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 609 | |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 610 | err = shash_ahash_digest(req, desc); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 611 | |
| 612 | req->base.complete = rctx->complete; |
| 613 | |
| 614 | out: |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 615 | cryptd_hash_complete(req, err); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static int cryptd_hash_digest_enqueue(struct ahash_request *req) |
| 619 | { |
| 620 | return cryptd_hash_enqueue(req, cryptd_hash_digest); |
| 621 | } |
| 622 | |
Herbert Xu | 6fba00d | 2009-07-22 11:10:22 +0800 | [diff] [blame] | 623 | static int cryptd_hash_export(struct ahash_request *req, void *out) |
| 624 | { |
| 625 | struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); |
| 626 | |
| 627 | return crypto_shash_export(&rctx->desc, out); |
| 628 | } |
| 629 | |
| 630 | static int cryptd_hash_import(struct ahash_request *req, const void *in) |
| 631 | { |
Ard Biesheuvel | 0bd2223 | 2016-09-01 14:25:43 +0100 | [diff] [blame] | 632 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 633 | struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm); |
| 634 | struct shash_desc *desc = cryptd_shash_desc(req); |
Herbert Xu | 6fba00d | 2009-07-22 11:10:22 +0800 | [diff] [blame] | 635 | |
Ard Biesheuvel | 0bd2223 | 2016-09-01 14:25:43 +0100 | [diff] [blame] | 636 | desc->tfm = ctx->child; |
| 637 | desc->flags = req->base.flags; |
| 638 | |
| 639 | return crypto_shash_import(desc, in); |
Herbert Xu | 6fba00d | 2009-07-22 11:10:22 +0800 | [diff] [blame] | 640 | } |
| 641 | |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 642 | static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb, |
| 643 | struct cryptd_queue *queue) |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 644 | { |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 645 | struct hashd_instance_ctx *ctx; |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 646 | struct ahash_instance *inst; |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 647 | struct shash_alg *salg; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 648 | struct crypto_alg *alg; |
Stephan Mueller | 466a7b9 | 2015-03-30 21:57:06 +0200 | [diff] [blame] | 649 | u32 type = 0; |
| 650 | u32 mask = 0; |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 651 | int err; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 652 | |
Stephan Mueller | 466a7b9 | 2015-03-30 21:57:06 +0200 | [diff] [blame] | 653 | cryptd_check_internal(tb, &type, &mask); |
| 654 | |
| 655 | salg = shash_attr_alg(tb[1], type, mask); |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 656 | if (IS_ERR(salg)) |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 657 | return PTR_ERR(salg); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 658 | |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 659 | alg = &salg->base; |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 660 | inst = cryptd_alloc_instance(alg, ahash_instance_headroom(), |
| 661 | sizeof(*ctx)); |
Steffen Klassert | 05ed875 | 2009-07-15 16:51:04 +0800 | [diff] [blame] | 662 | err = PTR_ERR(inst); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 663 | if (IS_ERR(inst)) |
| 664 | goto out_put_alg; |
| 665 | |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 666 | ctx = ahash_instance_ctx(inst); |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 667 | ctx->queue = queue; |
| 668 | |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 669 | err = crypto_init_shash_spawn(&ctx->spawn, salg, |
| 670 | ahash_crypto_instance(inst)); |
Herbert Xu | 46309d8 | 2009-07-12 21:38:59 +0800 | [diff] [blame] | 671 | if (err) |
| 672 | goto out_free_inst; |
| 673 | |
Stephan Mueller | 466a7b9 | 2015-03-30 21:57:06 +0200 | [diff] [blame] | 674 | type = CRYPTO_ALG_ASYNC; |
| 675 | if (alg->cra_flags & CRYPTO_ALG_INTERNAL) |
| 676 | type |= CRYPTO_ALG_INTERNAL; |
| 677 | inst->alg.halg.base.cra_flags = type; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 678 | |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 679 | inst->alg.halg.digestsize = salg->digestsize; |
Wang, Rui Y | 1a07834 | 2015-11-29 22:45:34 +0800 | [diff] [blame] | 680 | inst->alg.halg.statesize = salg->statesize; |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 681 | inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 682 | |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 683 | inst->alg.halg.base.cra_init = cryptd_hash_init_tfm; |
| 684 | inst->alg.halg.base.cra_exit = cryptd_hash_exit_tfm; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 685 | |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 686 | inst->alg.init = cryptd_hash_init_enqueue; |
| 687 | inst->alg.update = cryptd_hash_update_enqueue; |
| 688 | inst->alg.final = cryptd_hash_final_enqueue; |
Herbert Xu | 6fba00d | 2009-07-22 11:10:22 +0800 | [diff] [blame] | 689 | inst->alg.finup = cryptd_hash_finup_enqueue; |
| 690 | inst->alg.export = cryptd_hash_export; |
| 691 | inst->alg.import = cryptd_hash_import; |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 692 | inst->alg.setkey = cryptd_hash_setkey; |
| 693 | inst->alg.digest = cryptd_hash_digest_enqueue; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 694 | |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 695 | err = ahash_register_instance(tmpl, inst); |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 696 | if (err) { |
| 697 | crypto_drop_shash(&ctx->spawn); |
| 698 | out_free_inst: |
| 699 | kfree(inst); |
| 700 | } |
| 701 | |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 702 | out_put_alg: |
| 703 | crypto_mod_put(alg); |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 704 | return err; |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 705 | } |
| 706 | |
Herbert Xu | 92b9876 | 2015-05-28 22:08:01 +0800 | [diff] [blame] | 707 | static int cryptd_aead_setkey(struct crypto_aead *parent, |
| 708 | const u8 *key, unsigned int keylen) |
| 709 | { |
| 710 | struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent); |
| 711 | struct crypto_aead *child = ctx->child; |
| 712 | |
| 713 | return crypto_aead_setkey(child, key, keylen); |
| 714 | } |
| 715 | |
| 716 | static int cryptd_aead_setauthsize(struct crypto_aead *parent, |
| 717 | unsigned int authsize) |
| 718 | { |
| 719 | struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent); |
| 720 | struct crypto_aead *child = ctx->child; |
| 721 | |
| 722 | return crypto_aead_setauthsize(child, authsize); |
| 723 | } |
| 724 | |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 725 | static void cryptd_aead_crypt(struct aead_request *req, |
| 726 | struct crypto_aead *child, |
| 727 | int err, |
| 728 | int (*crypt)(struct aead_request *req)) |
| 729 | { |
| 730 | struct cryptd_aead_request_ctx *rctx; |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 731 | struct cryptd_aead_ctx *ctx; |
Herbert Xu | ec9f200 | 2015-07-06 19:11:03 +0800 | [diff] [blame] | 732 | crypto_completion_t compl; |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 733 | struct crypto_aead *tfm; |
| 734 | int refcnt; |
Herbert Xu | ec9f200 | 2015-07-06 19:11:03 +0800 | [diff] [blame] | 735 | |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 736 | rctx = aead_request_ctx(req); |
Herbert Xu | ec9f200 | 2015-07-06 19:11:03 +0800 | [diff] [blame] | 737 | compl = rctx->complete; |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 738 | |
Herbert Xu | 31bd44e | 2016-08-25 16:49:51 +0800 | [diff] [blame] | 739 | tfm = crypto_aead_reqtfm(req); |
| 740 | |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 741 | if (unlikely(err == -EINPROGRESS)) |
| 742 | goto out; |
| 743 | aead_request_set_tfm(req, child); |
| 744 | err = crypt( req ); |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 745 | |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 746 | out: |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 747 | ctx = crypto_aead_ctx(tfm); |
| 748 | refcnt = atomic_read(&ctx->refcnt); |
| 749 | |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 750 | local_bh_disable(); |
Herbert Xu | ec9f200 | 2015-07-06 19:11:03 +0800 | [diff] [blame] | 751 | compl(&req->base, err); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 752 | local_bh_enable(); |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 753 | |
| 754 | if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt)) |
| 755 | crypto_free_aead(tfm); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | static void cryptd_aead_encrypt(struct crypto_async_request *areq, int err) |
| 759 | { |
| 760 | struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm); |
| 761 | struct crypto_aead *child = ctx->child; |
| 762 | struct aead_request *req; |
| 763 | |
| 764 | req = container_of(areq, struct aead_request, base); |
Herbert Xu | ba3749a | 2015-08-13 17:29:02 +0800 | [diff] [blame] | 765 | cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->encrypt); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | static void cryptd_aead_decrypt(struct crypto_async_request *areq, int err) |
| 769 | { |
| 770 | struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm); |
| 771 | struct crypto_aead *child = ctx->child; |
| 772 | struct aead_request *req; |
| 773 | |
| 774 | req = container_of(areq, struct aead_request, base); |
Herbert Xu | ba3749a | 2015-08-13 17:29:02 +0800 | [diff] [blame] | 775 | cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->decrypt); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static int cryptd_aead_enqueue(struct aead_request *req, |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 779 | crypto_completion_t compl) |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 780 | { |
| 781 | struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req); |
| 782 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); |
| 783 | struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm)); |
| 784 | |
| 785 | rctx->complete = req->base.complete; |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 786 | req->base.complete = compl; |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 787 | return cryptd_enqueue_request(queue, &req->base); |
| 788 | } |
| 789 | |
| 790 | static int cryptd_aead_encrypt_enqueue(struct aead_request *req) |
| 791 | { |
| 792 | return cryptd_aead_enqueue(req, cryptd_aead_encrypt ); |
| 793 | } |
| 794 | |
| 795 | static int cryptd_aead_decrypt_enqueue(struct aead_request *req) |
| 796 | { |
| 797 | return cryptd_aead_enqueue(req, cryptd_aead_decrypt ); |
| 798 | } |
| 799 | |
Herbert Xu | f614e54 | 2015-05-28 22:08:04 +0800 | [diff] [blame] | 800 | static int cryptd_aead_init_tfm(struct crypto_aead *tfm) |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 801 | { |
Herbert Xu | f614e54 | 2015-05-28 22:08:04 +0800 | [diff] [blame] | 802 | struct aead_instance *inst = aead_alg_instance(tfm); |
| 803 | struct aead_instance_ctx *ictx = aead_instance_ctx(inst); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 804 | struct crypto_aead_spawn *spawn = &ictx->aead_spawn; |
Herbert Xu | f614e54 | 2015-05-28 22:08:04 +0800 | [diff] [blame] | 805 | struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 806 | struct crypto_aead *cipher; |
| 807 | |
| 808 | cipher = crypto_spawn_aead(spawn); |
| 809 | if (IS_ERR(cipher)) |
| 810 | return PTR_ERR(cipher); |
| 811 | |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 812 | ctx->child = cipher; |
Herbert Xu | ec9f200 | 2015-07-06 19:11:03 +0800 | [diff] [blame] | 813 | crypto_aead_set_reqsize( |
| 814 | tfm, max((unsigned)sizeof(struct cryptd_aead_request_ctx), |
| 815 | crypto_aead_reqsize(cipher))); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 816 | return 0; |
| 817 | } |
| 818 | |
Herbert Xu | f614e54 | 2015-05-28 22:08:04 +0800 | [diff] [blame] | 819 | static void cryptd_aead_exit_tfm(struct crypto_aead *tfm) |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 820 | { |
Herbert Xu | f614e54 | 2015-05-28 22:08:04 +0800 | [diff] [blame] | 821 | struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 822 | crypto_free_aead(ctx->child); |
| 823 | } |
| 824 | |
| 825 | static int cryptd_create_aead(struct crypto_template *tmpl, |
| 826 | struct rtattr **tb, |
| 827 | struct cryptd_queue *queue) |
| 828 | { |
| 829 | struct aead_instance_ctx *ctx; |
Herbert Xu | f614e54 | 2015-05-28 22:08:04 +0800 | [diff] [blame] | 830 | struct aead_instance *inst; |
| 831 | struct aead_alg *alg; |
Herbert Xu | 9b8c456 | 2015-05-21 15:10:57 +0800 | [diff] [blame] | 832 | const char *name; |
| 833 | u32 type = 0; |
Herbert Xu | ec9f200 | 2015-07-06 19:11:03 +0800 | [diff] [blame] | 834 | u32 mask = CRYPTO_ALG_ASYNC; |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 835 | int err; |
| 836 | |
Stephan Mueller | 466a7b9 | 2015-03-30 21:57:06 +0200 | [diff] [blame] | 837 | cryptd_check_internal(tb, &type, &mask); |
| 838 | |
Herbert Xu | 9b8c456 | 2015-05-21 15:10:57 +0800 | [diff] [blame] | 839 | name = crypto_attr_alg_name(tb[1]); |
| 840 | if (IS_ERR(name)) |
| 841 | return PTR_ERR(name); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 842 | |
Herbert Xu | 9b8c456 | 2015-05-21 15:10:57 +0800 | [diff] [blame] | 843 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
| 844 | if (!inst) |
| 845 | return -ENOMEM; |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 846 | |
Herbert Xu | f614e54 | 2015-05-28 22:08:04 +0800 | [diff] [blame] | 847 | ctx = aead_instance_ctx(inst); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 848 | ctx->queue = queue; |
| 849 | |
Herbert Xu | f614e54 | 2015-05-28 22:08:04 +0800 | [diff] [blame] | 850 | crypto_set_aead_spawn(&ctx->aead_spawn, aead_crypto_instance(inst)); |
Herbert Xu | 9b8c456 | 2015-05-21 15:10:57 +0800 | [diff] [blame] | 851 | err = crypto_grab_aead(&ctx->aead_spawn, name, type, mask); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 852 | if (err) |
| 853 | goto out_free_inst; |
| 854 | |
Herbert Xu | f614e54 | 2015-05-28 22:08:04 +0800 | [diff] [blame] | 855 | alg = crypto_spawn_aead_alg(&ctx->aead_spawn); |
| 856 | err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base); |
Herbert Xu | 9b8c456 | 2015-05-21 15:10:57 +0800 | [diff] [blame] | 857 | if (err) |
| 858 | goto out_drop_aead; |
| 859 | |
Herbert Xu | f614e54 | 2015-05-28 22:08:04 +0800 | [diff] [blame] | 860 | inst->alg.base.cra_flags = CRYPTO_ALG_ASYNC | |
Herbert Xu | 5e4b8c1 | 2015-08-13 17:29:06 +0800 | [diff] [blame] | 861 | (alg->base.cra_flags & CRYPTO_ALG_INTERNAL); |
Herbert Xu | f614e54 | 2015-05-28 22:08:04 +0800 | [diff] [blame] | 862 | inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 863 | |
Herbert Xu | f614e54 | 2015-05-28 22:08:04 +0800 | [diff] [blame] | 864 | inst->alg.ivsize = crypto_aead_alg_ivsize(alg); |
| 865 | inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg); |
| 866 | |
| 867 | inst->alg.init = cryptd_aead_init_tfm; |
| 868 | inst->alg.exit = cryptd_aead_exit_tfm; |
| 869 | inst->alg.setkey = cryptd_aead_setkey; |
| 870 | inst->alg.setauthsize = cryptd_aead_setauthsize; |
| 871 | inst->alg.encrypt = cryptd_aead_encrypt_enqueue; |
| 872 | inst->alg.decrypt = cryptd_aead_decrypt_enqueue; |
| 873 | |
| 874 | err = aead_register_instance(tmpl, inst); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 875 | if (err) { |
Herbert Xu | 9b8c456 | 2015-05-21 15:10:57 +0800 | [diff] [blame] | 876 | out_drop_aead: |
| 877 | crypto_drop_aead(&ctx->aead_spawn); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 878 | out_free_inst: |
| 879 | kfree(inst); |
| 880 | } |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 881 | return err; |
| 882 | } |
| 883 | |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 884 | static struct cryptd_queue queue; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 885 | |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 886 | static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb) |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 887 | { |
| 888 | struct crypto_attr_type *algt; |
| 889 | |
| 890 | algt = crypto_get_attr_type(tb); |
| 891 | if (IS_ERR(algt)) |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 892 | return PTR_ERR(algt); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 893 | |
| 894 | switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) { |
| 895 | case CRYPTO_ALG_TYPE_BLKCIPHER: |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 896 | return cryptd_create_blkcipher(tmpl, tb, &queue); |
Loc Ho | b8a2825 | 2008-05-14 21:23:00 +0800 | [diff] [blame] | 897 | case CRYPTO_ALG_TYPE_DIGEST: |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 898 | return cryptd_create_hash(tmpl, tb, &queue); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 899 | case CRYPTO_ALG_TYPE_AEAD: |
| 900 | return cryptd_create_aead(tmpl, tb, &queue); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 901 | } |
| 902 | |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 903 | return -EINVAL; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static void cryptd_free(struct crypto_instance *inst) |
| 907 | { |
| 908 | struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst); |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 909 | struct hashd_instance_ctx *hctx = crypto_instance_ctx(inst); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 910 | struct aead_instance_ctx *aead_ctx = crypto_instance_ctx(inst); |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 911 | |
| 912 | switch (inst->alg.cra_flags & CRYPTO_ALG_TYPE_MASK) { |
| 913 | case CRYPTO_ALG_TYPE_AHASH: |
| 914 | crypto_drop_shash(&hctx->spawn); |
| 915 | kfree(ahash_instance(inst)); |
| 916 | return; |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 917 | case CRYPTO_ALG_TYPE_AEAD: |
Herbert Xu | f614e54 | 2015-05-28 22:08:04 +0800 | [diff] [blame] | 918 | crypto_drop_aead(&aead_ctx->aead_spawn); |
| 919 | kfree(aead_instance(inst)); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 920 | return; |
| 921 | default: |
| 922 | crypto_drop_spawn(&ctx->spawn); |
| 923 | kfree(inst); |
Herbert Xu | 0b535ad | 2009-07-14 19:11:32 +0800 | [diff] [blame] | 924 | } |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | static struct crypto_template cryptd_tmpl = { |
| 928 | .name = "cryptd", |
Herbert Xu | 9cd899a | 2009-07-14 18:45:45 +0800 | [diff] [blame] | 929 | .create = cryptd_create, |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 930 | .free = cryptd_free, |
| 931 | .module = THIS_MODULE, |
| 932 | }; |
| 933 | |
Huang Ying | 1cac2cb | 2009-01-18 16:19:46 +1100 | [diff] [blame] | 934 | struct cryptd_ablkcipher *cryptd_alloc_ablkcipher(const char *alg_name, |
| 935 | u32 type, u32 mask) |
| 936 | { |
| 937 | char cryptd_alg_name[CRYPTO_MAX_ALG_NAME]; |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 938 | struct cryptd_blkcipher_ctx *ctx; |
Huang Ying | 505fd21 | 2009-03-29 15:33:53 +0800 | [diff] [blame] | 939 | struct crypto_tfm *tfm; |
Huang Ying | 1cac2cb | 2009-01-18 16:19:46 +1100 | [diff] [blame] | 940 | |
| 941 | if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME, |
| 942 | "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME) |
| 943 | return ERR_PTR(-EINVAL); |
Alexander Kuleshov | c012a79 | 2015-11-25 23:48:28 +0600 | [diff] [blame] | 944 | type = crypto_skcipher_type(type); |
Huang Ying | 505fd21 | 2009-03-29 15:33:53 +0800 | [diff] [blame] | 945 | mask &= ~CRYPTO_ALG_TYPE_MASK; |
| 946 | mask |= (CRYPTO_ALG_GENIV | CRYPTO_ALG_TYPE_BLKCIPHER_MASK); |
| 947 | tfm = crypto_alloc_base(cryptd_alg_name, type, mask); |
Huang Ying | 1cac2cb | 2009-01-18 16:19:46 +1100 | [diff] [blame] | 948 | if (IS_ERR(tfm)) |
| 949 | return ERR_CAST(tfm); |
Huang Ying | 505fd21 | 2009-03-29 15:33:53 +0800 | [diff] [blame] | 950 | if (tfm->__crt_alg->cra_module != THIS_MODULE) { |
| 951 | crypto_free_tfm(tfm); |
Huang Ying | 1cac2cb | 2009-01-18 16:19:46 +1100 | [diff] [blame] | 952 | return ERR_PTR(-EINVAL); |
| 953 | } |
| 954 | |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 955 | ctx = crypto_tfm_ctx(tfm); |
| 956 | atomic_set(&ctx->refcnt, 1); |
| 957 | |
Huang Ying | 505fd21 | 2009-03-29 15:33:53 +0800 | [diff] [blame] | 958 | return __cryptd_ablkcipher_cast(__crypto_ablkcipher_cast(tfm)); |
Huang Ying | 1cac2cb | 2009-01-18 16:19:46 +1100 | [diff] [blame] | 959 | } |
| 960 | EXPORT_SYMBOL_GPL(cryptd_alloc_ablkcipher); |
| 961 | |
| 962 | struct crypto_blkcipher *cryptd_ablkcipher_child(struct cryptd_ablkcipher *tfm) |
| 963 | { |
| 964 | struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base); |
| 965 | return ctx->child; |
| 966 | } |
| 967 | EXPORT_SYMBOL_GPL(cryptd_ablkcipher_child); |
| 968 | |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 969 | bool cryptd_ablkcipher_queued(struct cryptd_ablkcipher *tfm) |
| 970 | { |
| 971 | struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base); |
| 972 | |
| 973 | return atomic_read(&ctx->refcnt) - 1; |
| 974 | } |
| 975 | EXPORT_SYMBOL_GPL(cryptd_ablkcipher_queued); |
| 976 | |
Huang Ying | 1cac2cb | 2009-01-18 16:19:46 +1100 | [diff] [blame] | 977 | void cryptd_free_ablkcipher(struct cryptd_ablkcipher *tfm) |
| 978 | { |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 979 | struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(&tfm->base); |
| 980 | |
| 981 | if (atomic_dec_and_test(&ctx->refcnt)) |
| 982 | crypto_free_ablkcipher(&tfm->base); |
Huang Ying | 1cac2cb | 2009-01-18 16:19:46 +1100 | [diff] [blame] | 983 | } |
| 984 | EXPORT_SYMBOL_GPL(cryptd_free_ablkcipher); |
| 985 | |
Huang Ying | ace1366 | 2009-08-06 15:35:20 +1000 | [diff] [blame] | 986 | struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name, |
| 987 | u32 type, u32 mask) |
| 988 | { |
| 989 | char cryptd_alg_name[CRYPTO_MAX_ALG_NAME]; |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 990 | struct cryptd_hash_ctx *ctx; |
Huang Ying | ace1366 | 2009-08-06 15:35:20 +1000 | [diff] [blame] | 991 | struct crypto_ahash *tfm; |
| 992 | |
| 993 | if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME, |
| 994 | "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME) |
| 995 | return ERR_PTR(-EINVAL); |
| 996 | tfm = crypto_alloc_ahash(cryptd_alg_name, type, mask); |
| 997 | if (IS_ERR(tfm)) |
| 998 | return ERR_CAST(tfm); |
| 999 | if (tfm->base.__crt_alg->cra_module != THIS_MODULE) { |
| 1000 | crypto_free_ahash(tfm); |
| 1001 | return ERR_PTR(-EINVAL); |
| 1002 | } |
| 1003 | |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 1004 | ctx = crypto_ahash_ctx(tfm); |
| 1005 | atomic_set(&ctx->refcnt, 1); |
| 1006 | |
Huang Ying | ace1366 | 2009-08-06 15:35:20 +1000 | [diff] [blame] | 1007 | return __cryptd_ahash_cast(tfm); |
| 1008 | } |
| 1009 | EXPORT_SYMBOL_GPL(cryptd_alloc_ahash); |
| 1010 | |
| 1011 | struct crypto_shash *cryptd_ahash_child(struct cryptd_ahash *tfm) |
| 1012 | { |
| 1013 | struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base); |
| 1014 | |
| 1015 | return ctx->child; |
| 1016 | } |
| 1017 | EXPORT_SYMBOL_GPL(cryptd_ahash_child); |
| 1018 | |
Huang Ying | 0e1227d | 2009-10-19 11:53:06 +0900 | [diff] [blame] | 1019 | struct shash_desc *cryptd_shash_desc(struct ahash_request *req) |
| 1020 | { |
| 1021 | struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); |
| 1022 | return &rctx->desc; |
| 1023 | } |
| 1024 | EXPORT_SYMBOL_GPL(cryptd_shash_desc); |
| 1025 | |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 1026 | bool cryptd_ahash_queued(struct cryptd_ahash *tfm) |
| 1027 | { |
| 1028 | struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base); |
| 1029 | |
| 1030 | return atomic_read(&ctx->refcnt) - 1; |
| 1031 | } |
| 1032 | EXPORT_SYMBOL_GPL(cryptd_ahash_queued); |
| 1033 | |
Huang Ying | ace1366 | 2009-08-06 15:35:20 +1000 | [diff] [blame] | 1034 | void cryptd_free_ahash(struct cryptd_ahash *tfm) |
| 1035 | { |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 1036 | struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base); |
| 1037 | |
| 1038 | if (atomic_dec_and_test(&ctx->refcnt)) |
| 1039 | crypto_free_ahash(&tfm->base); |
Huang Ying | ace1366 | 2009-08-06 15:35:20 +1000 | [diff] [blame] | 1040 | } |
| 1041 | EXPORT_SYMBOL_GPL(cryptd_free_ahash); |
| 1042 | |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 1043 | struct cryptd_aead *cryptd_alloc_aead(const char *alg_name, |
| 1044 | u32 type, u32 mask) |
| 1045 | { |
| 1046 | char cryptd_alg_name[CRYPTO_MAX_ALG_NAME]; |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 1047 | struct cryptd_aead_ctx *ctx; |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 1048 | struct crypto_aead *tfm; |
| 1049 | |
| 1050 | if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME, |
| 1051 | "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME) |
| 1052 | return ERR_PTR(-EINVAL); |
| 1053 | tfm = crypto_alloc_aead(cryptd_alg_name, type, mask); |
| 1054 | if (IS_ERR(tfm)) |
| 1055 | return ERR_CAST(tfm); |
| 1056 | if (tfm->base.__crt_alg->cra_module != THIS_MODULE) { |
| 1057 | crypto_free_aead(tfm); |
| 1058 | return ERR_PTR(-EINVAL); |
| 1059 | } |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 1060 | |
| 1061 | ctx = crypto_aead_ctx(tfm); |
| 1062 | atomic_set(&ctx->refcnt, 1); |
| 1063 | |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 1064 | return __cryptd_aead_cast(tfm); |
| 1065 | } |
| 1066 | EXPORT_SYMBOL_GPL(cryptd_alloc_aead); |
| 1067 | |
| 1068 | struct crypto_aead *cryptd_aead_child(struct cryptd_aead *tfm) |
| 1069 | { |
| 1070 | struct cryptd_aead_ctx *ctx; |
| 1071 | ctx = crypto_aead_ctx(&tfm->base); |
| 1072 | return ctx->child; |
| 1073 | } |
| 1074 | EXPORT_SYMBOL_GPL(cryptd_aead_child); |
| 1075 | |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 1076 | bool cryptd_aead_queued(struct cryptd_aead *tfm) |
| 1077 | { |
| 1078 | struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base); |
| 1079 | |
| 1080 | return atomic_read(&ctx->refcnt) - 1; |
| 1081 | } |
| 1082 | EXPORT_SYMBOL_GPL(cryptd_aead_queued); |
| 1083 | |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 1084 | void cryptd_free_aead(struct cryptd_aead *tfm) |
| 1085 | { |
Herbert Xu | 81760ea | 2016-06-21 16:55:13 +0800 | [diff] [blame] | 1086 | struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base); |
| 1087 | |
| 1088 | if (atomic_dec_and_test(&ctx->refcnt)) |
| 1089 | crypto_free_aead(&tfm->base); |
Adrian Hoban | 298c926 | 2010-09-20 16:05:12 +0800 | [diff] [blame] | 1090 | } |
| 1091 | EXPORT_SYMBOL_GPL(cryptd_free_aead); |
| 1092 | |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 1093 | static int __init cryptd_init(void) |
| 1094 | { |
| 1095 | int err; |
| 1096 | |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 1097 | err = cryptd_init_queue(&queue, CRYPTD_MAX_CPU_QLEN); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 1098 | if (err) |
| 1099 | return err; |
| 1100 | |
| 1101 | err = crypto_register_template(&cryptd_tmpl); |
| 1102 | if (err) |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 1103 | cryptd_fini_queue(&queue); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 1104 | |
| 1105 | return err; |
| 1106 | } |
| 1107 | |
| 1108 | static void __exit cryptd_exit(void) |
| 1109 | { |
Huang Ying | 254eff7 | 2009-02-19 14:42:19 +0800 | [diff] [blame] | 1110 | cryptd_fini_queue(&queue); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 1111 | crypto_unregister_template(&cryptd_tmpl); |
| 1112 | } |
| 1113 | |
Herbert Xu | b2bac6a | 2011-08-19 16:11:23 +0800 | [diff] [blame] | 1114 | subsys_initcall(cryptd_init); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 1115 | module_exit(cryptd_exit); |
| 1116 | |
| 1117 | MODULE_LICENSE("GPL"); |
| 1118 | MODULE_DESCRIPTION("Software async crypto daemon"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 1119 | MODULE_ALIAS_CRYPTO("cryptd"); |