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 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <crypto/algapi.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/kthread.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/scatterlist.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | |
| 26 | #define CRYPTD_MAX_QLEN 100 |
| 27 | |
| 28 | struct cryptd_state { |
| 29 | spinlock_t lock; |
| 30 | struct mutex mutex; |
| 31 | struct crypto_queue queue; |
| 32 | struct task_struct *task; |
| 33 | }; |
| 34 | |
| 35 | struct cryptd_instance_ctx { |
| 36 | struct crypto_spawn spawn; |
| 37 | struct cryptd_state *state; |
| 38 | }; |
| 39 | |
| 40 | struct cryptd_blkcipher_ctx { |
| 41 | struct crypto_blkcipher *child; |
| 42 | }; |
| 43 | |
| 44 | struct cryptd_blkcipher_request_ctx { |
| 45 | crypto_completion_t complete; |
| 46 | }; |
| 47 | |
| 48 | |
| 49 | static inline struct cryptd_state *cryptd_get_state(struct crypto_tfm *tfm) |
| 50 | { |
| 51 | struct crypto_instance *inst = crypto_tfm_alg_instance(tfm); |
| 52 | struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst); |
| 53 | return ictx->state; |
| 54 | } |
| 55 | |
| 56 | static int cryptd_blkcipher_setkey(struct crypto_ablkcipher *parent, |
| 57 | const u8 *key, unsigned int keylen) |
| 58 | { |
| 59 | struct cryptd_blkcipher_ctx *ctx = crypto_ablkcipher_ctx(parent); |
| 60 | struct crypto_blkcipher *child = ctx->child; |
| 61 | int err; |
| 62 | |
| 63 | crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); |
| 64 | crypto_blkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) & |
| 65 | CRYPTO_TFM_REQ_MASK); |
| 66 | err = crypto_blkcipher_setkey(child, key, keylen); |
| 67 | crypto_ablkcipher_set_flags(parent, crypto_blkcipher_get_flags(child) & |
| 68 | CRYPTO_TFM_RES_MASK); |
| 69 | return err; |
| 70 | } |
| 71 | |
| 72 | static void cryptd_blkcipher_crypt(struct ablkcipher_request *req, |
| 73 | struct crypto_blkcipher *child, |
| 74 | int err, |
| 75 | int (*crypt)(struct blkcipher_desc *desc, |
| 76 | struct scatterlist *dst, |
| 77 | struct scatterlist *src, |
| 78 | unsigned int len)) |
| 79 | { |
| 80 | struct cryptd_blkcipher_request_ctx *rctx; |
| 81 | struct blkcipher_desc desc; |
| 82 | |
| 83 | rctx = ablkcipher_request_ctx(req); |
| 84 | |
| 85 | if (unlikely(err == -EINPROGRESS)) { |
| 86 | rctx->complete(&req->base, err); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | desc.tfm = child; |
| 91 | desc.info = req->info; |
| 92 | desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 93 | |
| 94 | err = crypt(&desc, req->dst, req->src, req->nbytes); |
| 95 | |
| 96 | req->base.complete = rctx->complete; |
| 97 | |
| 98 | local_bh_disable(); |
| 99 | req->base.complete(&req->base, err); |
| 100 | local_bh_enable(); |
| 101 | } |
| 102 | |
| 103 | static void cryptd_blkcipher_encrypt(struct crypto_async_request *req, int err) |
| 104 | { |
| 105 | struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm); |
| 106 | struct crypto_blkcipher *child = ctx->child; |
| 107 | |
| 108 | cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err, |
| 109 | crypto_blkcipher_crt(child)->encrypt); |
| 110 | } |
| 111 | |
| 112 | static void cryptd_blkcipher_decrypt(struct crypto_async_request *req, int err) |
| 113 | { |
| 114 | struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(req->tfm); |
| 115 | struct crypto_blkcipher *child = ctx->child; |
| 116 | |
| 117 | cryptd_blkcipher_crypt(ablkcipher_request_cast(req), child, err, |
| 118 | crypto_blkcipher_crt(child)->decrypt); |
| 119 | } |
| 120 | |
| 121 | static int cryptd_blkcipher_enqueue(struct ablkcipher_request *req, |
| 122 | crypto_completion_t complete) |
| 123 | { |
| 124 | struct cryptd_blkcipher_request_ctx *rctx = ablkcipher_request_ctx(req); |
| 125 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); |
| 126 | struct cryptd_state *state = |
| 127 | cryptd_get_state(crypto_ablkcipher_tfm(tfm)); |
| 128 | int err; |
| 129 | |
| 130 | rctx->complete = req->base.complete; |
| 131 | req->base.complete = complete; |
| 132 | |
| 133 | spin_lock_bh(&state->lock); |
Herbert Xu | 2de98e7 | 2007-08-26 19:12:47 +0800 | [diff] [blame] | 134 | err = ablkcipher_enqueue_request(&state->queue, req); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 135 | spin_unlock_bh(&state->lock); |
| 136 | |
| 137 | wake_up_process(state->task); |
| 138 | return err; |
| 139 | } |
| 140 | |
| 141 | static int cryptd_blkcipher_encrypt_enqueue(struct ablkcipher_request *req) |
| 142 | { |
| 143 | return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_encrypt); |
| 144 | } |
| 145 | |
| 146 | static int cryptd_blkcipher_decrypt_enqueue(struct ablkcipher_request *req) |
| 147 | { |
| 148 | return cryptd_blkcipher_enqueue(req, cryptd_blkcipher_decrypt); |
| 149 | } |
| 150 | |
| 151 | static int cryptd_blkcipher_init_tfm(struct crypto_tfm *tfm) |
| 152 | { |
| 153 | struct crypto_instance *inst = crypto_tfm_alg_instance(tfm); |
| 154 | struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst); |
| 155 | struct crypto_spawn *spawn = &ictx->spawn; |
| 156 | struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm); |
| 157 | struct crypto_blkcipher *cipher; |
| 158 | |
| 159 | cipher = crypto_spawn_blkcipher(spawn); |
| 160 | if (IS_ERR(cipher)) |
| 161 | return PTR_ERR(cipher); |
| 162 | |
| 163 | ctx->child = cipher; |
| 164 | tfm->crt_ablkcipher.reqsize = |
| 165 | sizeof(struct cryptd_blkcipher_request_ctx); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static void cryptd_blkcipher_exit_tfm(struct crypto_tfm *tfm) |
| 170 | { |
| 171 | struct cryptd_blkcipher_ctx *ctx = crypto_tfm_ctx(tfm); |
| 172 | struct cryptd_state *state = cryptd_get_state(tfm); |
| 173 | int active; |
| 174 | |
| 175 | mutex_lock(&state->mutex); |
Herbert Xu | 2de98e7 | 2007-08-26 19:12:47 +0800 | [diff] [blame] | 176 | active = ablkcipher_tfm_in_queue(&state->queue, |
| 177 | __crypto_ablkcipher_cast(tfm)); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 178 | mutex_unlock(&state->mutex); |
| 179 | |
| 180 | BUG_ON(active); |
| 181 | |
| 182 | crypto_free_blkcipher(ctx->child); |
| 183 | } |
| 184 | |
| 185 | static struct crypto_instance *cryptd_alloc_instance(struct crypto_alg *alg, |
| 186 | struct cryptd_state *state) |
| 187 | { |
| 188 | struct crypto_instance *inst; |
| 189 | struct cryptd_instance_ctx *ctx; |
| 190 | int err; |
| 191 | |
| 192 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); |
Julia Lawall | b1145ce | 2008-04-30 00:27:14 +0800 | [diff] [blame] | 193 | if (!inst) { |
| 194 | inst = ERR_PTR(-ENOMEM); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 195 | goto out; |
Julia Lawall | b1145ce | 2008-04-30 00:27:14 +0800 | [diff] [blame] | 196 | } |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 197 | |
| 198 | err = -ENAMETOOLONG; |
| 199 | if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 200 | "cryptd(%s)", alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
| 201 | goto out_free_inst; |
| 202 | |
| 203 | ctx = crypto_instance_ctx(inst); |
| 204 | err = crypto_init_spawn(&ctx->spawn, alg, inst, |
| 205 | CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); |
| 206 | if (err) |
| 207 | goto out_free_inst; |
| 208 | |
| 209 | ctx->state = state; |
| 210 | |
| 211 | memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); |
| 212 | |
| 213 | inst->alg.cra_priority = alg->cra_priority + 50; |
| 214 | inst->alg.cra_blocksize = alg->cra_blocksize; |
| 215 | inst->alg.cra_alignmask = alg->cra_alignmask; |
| 216 | |
| 217 | out: |
| 218 | return inst; |
| 219 | |
| 220 | out_free_inst: |
| 221 | kfree(inst); |
| 222 | inst = ERR_PTR(err); |
| 223 | goto out; |
| 224 | } |
| 225 | |
| 226 | static struct crypto_instance *cryptd_alloc_blkcipher( |
| 227 | struct rtattr **tb, struct cryptd_state *state) |
| 228 | { |
| 229 | struct crypto_instance *inst; |
| 230 | struct crypto_alg *alg; |
| 231 | |
| 232 | alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_BLKCIPHER, |
Herbert Xu | 332f8840 | 2007-11-15 22:36:07 +0800 | [diff] [blame] | 233 | CRYPTO_ALG_TYPE_MASK); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 234 | if (IS_ERR(alg)) |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 235 | return ERR_CAST(alg); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 236 | |
| 237 | inst = cryptd_alloc_instance(alg, state); |
| 238 | if (IS_ERR(inst)) |
| 239 | goto out_put_alg; |
| 240 | |
Herbert Xu | 332f8840 | 2007-11-15 22:36:07 +0800 | [diff] [blame] | 241 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC; |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 242 | inst->alg.cra_type = &crypto_ablkcipher_type; |
| 243 | |
| 244 | inst->alg.cra_ablkcipher.ivsize = alg->cra_blkcipher.ivsize; |
| 245 | inst->alg.cra_ablkcipher.min_keysize = alg->cra_blkcipher.min_keysize; |
| 246 | inst->alg.cra_ablkcipher.max_keysize = alg->cra_blkcipher.max_keysize; |
| 247 | |
Herbert Xu | 927eead | 2007-11-27 21:15:31 +0800 | [diff] [blame] | 248 | inst->alg.cra_ablkcipher.geniv = alg->cra_blkcipher.geniv; |
| 249 | |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 250 | inst->alg.cra_ctxsize = sizeof(struct cryptd_blkcipher_ctx); |
| 251 | |
| 252 | inst->alg.cra_init = cryptd_blkcipher_init_tfm; |
| 253 | inst->alg.cra_exit = cryptd_blkcipher_exit_tfm; |
| 254 | |
| 255 | inst->alg.cra_ablkcipher.setkey = cryptd_blkcipher_setkey; |
| 256 | inst->alg.cra_ablkcipher.encrypt = cryptd_blkcipher_encrypt_enqueue; |
| 257 | inst->alg.cra_ablkcipher.decrypt = cryptd_blkcipher_decrypt_enqueue; |
| 258 | |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 259 | out_put_alg: |
| 260 | crypto_mod_put(alg); |
| 261 | return inst; |
| 262 | } |
| 263 | |
| 264 | static struct cryptd_state state; |
| 265 | |
| 266 | static struct crypto_instance *cryptd_alloc(struct rtattr **tb) |
| 267 | { |
| 268 | struct crypto_attr_type *algt; |
| 269 | |
| 270 | algt = crypto_get_attr_type(tb); |
| 271 | if (IS_ERR(algt)) |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 272 | return ERR_CAST(algt); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 273 | |
| 274 | switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) { |
| 275 | case CRYPTO_ALG_TYPE_BLKCIPHER: |
| 276 | return cryptd_alloc_blkcipher(tb, &state); |
| 277 | } |
| 278 | |
| 279 | return ERR_PTR(-EINVAL); |
| 280 | } |
| 281 | |
| 282 | static void cryptd_free(struct crypto_instance *inst) |
| 283 | { |
| 284 | struct cryptd_instance_ctx *ctx = crypto_instance_ctx(inst); |
| 285 | |
| 286 | crypto_drop_spawn(&ctx->spawn); |
| 287 | kfree(inst); |
| 288 | } |
| 289 | |
| 290 | static struct crypto_template cryptd_tmpl = { |
| 291 | .name = "cryptd", |
| 292 | .alloc = cryptd_alloc, |
| 293 | .free = cryptd_free, |
| 294 | .module = THIS_MODULE, |
| 295 | }; |
| 296 | |
| 297 | static inline int cryptd_create_thread(struct cryptd_state *state, |
| 298 | int (*fn)(void *data), const char *name) |
| 299 | { |
| 300 | spin_lock_init(&state->lock); |
| 301 | mutex_init(&state->mutex); |
| 302 | crypto_init_queue(&state->queue, CRYPTD_MAX_QLEN); |
| 303 | |
Rafael J. Wysocki | 189fe31 | 2007-05-31 18:10:22 +1000 | [diff] [blame] | 304 | state->task = kthread_run(fn, state, name); |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 305 | if (IS_ERR(state->task)) |
| 306 | return PTR_ERR(state->task); |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static inline void cryptd_stop_thread(struct cryptd_state *state) |
| 312 | { |
| 313 | BUG_ON(state->queue.qlen); |
| 314 | kthread_stop(state->task); |
| 315 | } |
| 316 | |
| 317 | static int cryptd_thread(void *data) |
| 318 | { |
| 319 | struct cryptd_state *state = data; |
| 320 | int stop; |
| 321 | |
Rafael J. Wysocki | 189fe31 | 2007-05-31 18:10:22 +1000 | [diff] [blame] | 322 | current->flags |= PF_NOFREEZE; |
| 323 | |
Herbert Xu | 124b53d | 2007-04-16 20:49:20 +1000 | [diff] [blame] | 324 | do { |
| 325 | struct crypto_async_request *req, *backlog; |
| 326 | |
| 327 | mutex_lock(&state->mutex); |
| 328 | __set_current_state(TASK_INTERRUPTIBLE); |
| 329 | |
| 330 | spin_lock_bh(&state->lock); |
| 331 | backlog = crypto_get_backlog(&state->queue); |
| 332 | req = crypto_dequeue_request(&state->queue); |
| 333 | spin_unlock_bh(&state->lock); |
| 334 | |
| 335 | stop = kthread_should_stop(); |
| 336 | |
| 337 | if (stop || req) { |
| 338 | __set_current_state(TASK_RUNNING); |
| 339 | if (req) { |
| 340 | if (backlog) |
| 341 | backlog->complete(backlog, |
| 342 | -EINPROGRESS); |
| 343 | req->complete(req, 0); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | mutex_unlock(&state->mutex); |
| 348 | |
| 349 | schedule(); |
| 350 | } while (!stop); |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static int __init cryptd_init(void) |
| 356 | { |
| 357 | int err; |
| 358 | |
| 359 | err = cryptd_create_thread(&state, cryptd_thread, "cryptd"); |
| 360 | if (err) |
| 361 | return err; |
| 362 | |
| 363 | err = crypto_register_template(&cryptd_tmpl); |
| 364 | if (err) |
| 365 | kthread_stop(state.task); |
| 366 | |
| 367 | return err; |
| 368 | } |
| 369 | |
| 370 | static void __exit cryptd_exit(void) |
| 371 | { |
| 372 | cryptd_stop_thread(&state); |
| 373 | crypto_unregister_template(&cryptd_tmpl); |
| 374 | } |
| 375 | |
| 376 | module_init(cryptd_init); |
| 377 | module_exit(cryptd_exit); |
| 378 | |
| 379 | MODULE_LICENSE("GPL"); |
| 380 | MODULE_DESCRIPTION("Software async crypto daemon"); |