1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Software async crypto daemon. 4 * 5 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> 6 * 7 * 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. 13 */ 14 15 #include <crypto/internal/hash.h> 16 #include <crypto/internal/aead.h> 17 #include <crypto/internal/skcipher.h> 18 #include <crypto/cryptd.h> 19 #include <linux/refcount.h> 20 #include <linux/err.h> 21 #include <linux/init.h> 22 #include <linux/kernel.h> 23 #include <linux/list.h> 24 #include <linux/module.h> 25 #include <linux/scatterlist.h> 26 #include <linux/sched.h> 27 #include <linux/slab.h> 28 #include <linux/workqueue.h> 29 30 static unsigned int cryptd_max_cpu_qlen = 1000; 31 module_param(cryptd_max_cpu_qlen, uint, 0); 32 MODULE_PARM_DESC(cryptd_max_cpu_qlen, "Set cryptd Max queue depth"); 33 34 static struct workqueue_struct *cryptd_wq; 35 36 struct cryptd_cpu_queue { 37 local_lock_t bh_lock; 38 struct crypto_queue queue; 39 struct work_struct work; 40 }; 41 42 struct cryptd_queue { 43 /* 44 * Protected by disabling BH to allow enqueueing from softinterrupt and 45 * dequeuing from kworker (cryptd_queue_worker()). 46 */ 47 struct cryptd_cpu_queue __percpu *cpu_queue; 48 }; 49 50 struct cryptd_instance_ctx { 51 struct crypto_spawn spawn; 52 struct cryptd_queue *queue; 53 }; 54 55 struct skcipherd_instance_ctx { 56 struct crypto_skcipher_spawn spawn; 57 struct cryptd_queue *queue; 58 }; 59 60 struct hashd_instance_ctx { 61 struct crypto_shash_spawn spawn; 62 struct cryptd_queue *queue; 63 }; 64 65 struct aead_instance_ctx { 66 struct crypto_aead_spawn aead_spawn; 67 struct cryptd_queue *queue; 68 }; 69 70 struct cryptd_skcipher_ctx { 71 refcount_t refcnt; 72 struct crypto_skcipher *child; 73 }; 74 75 struct cryptd_skcipher_request_ctx { 76 struct skcipher_request req; 77 }; 78 79 struct cryptd_hash_ctx { 80 refcount_t refcnt; 81 struct crypto_shash *child; 82 }; 83 84 struct cryptd_hash_request_ctx { 85 crypto_completion_t complete; 86 void *data; 87 struct shash_desc desc; 88 }; 89 90 struct cryptd_aead_ctx { 91 refcount_t refcnt; 92 struct crypto_aead *child; 93 }; 94 95 struct cryptd_aead_request_ctx { 96 struct aead_request req; 97 }; 98 99 static void cryptd_queue_worker(struct work_struct *work); 100 101 static int cryptd_init_queue(struct cryptd_queue *queue, 102 unsigned int max_cpu_qlen) 103 { 104 int cpu; 105 struct cryptd_cpu_queue *cpu_queue; 106 107 queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue); 108 if (!queue->cpu_queue) 109 return -ENOMEM; 110 for_each_possible_cpu(cpu) { 111 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu); 112 crypto_init_queue(&cpu_queue->queue, max_cpu_qlen); 113 INIT_WORK(&cpu_queue->work, cryptd_queue_worker); 114 local_lock_init(&cpu_queue->bh_lock); 115 } 116 pr_info("cryptd: max_cpu_qlen set to %d\n", max_cpu_qlen); 117 return 0; 118 } 119 120 static void cryptd_fini_queue(struct cryptd_queue *queue) 121 { 122 int cpu; 123 struct cryptd_cpu_queue *cpu_queue; 124 125 for_each_possible_cpu(cpu) { 126 cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu); 127 BUG_ON(cpu_queue->queue.qlen); 128 } 129 free_percpu(queue->cpu_queue); 130 } 131 132 static int cryptd_enqueue_request(struct cryptd_queue *queue, 133 struct crypto_async_request *request) 134 { 135 int err; 136 struct cryptd_cpu_queue *cpu_queue; 137 refcount_t *refcnt; 138 139 local_bh_disable(); 140 local_lock_nested_bh(&queue->cpu_queue->bh_lock); 141 cpu_queue = this_cpu_ptr(queue->cpu_queue); 142 err = crypto_enqueue_request(&cpu_queue->queue, request); 143 144 refcnt = crypto_tfm_ctx(request->tfm); 145 146 if (err == -ENOSPC) 147 goto out; 148 149 queue_work_on(smp_processor_id(), cryptd_wq, &cpu_queue->work); 150 151 if (!refcount_read(refcnt)) 152 goto out; 153 154 refcount_inc(refcnt); 155 156 out: 157 local_unlock_nested_bh(&queue->cpu_queue->bh_lock); 158 local_bh_enable(); 159 160 return err; 161 } 162 163 /* Called in workqueue context, do one real cryption work (via 164 * req->complete) and reschedule itself if there are more work to 165 * do. */ 166 static void cryptd_queue_worker(struct work_struct *work) 167 { 168 struct cryptd_cpu_queue *cpu_queue; 169 struct crypto_async_request *req, *backlog; 170 171 cpu_queue = container_of(work, struct cryptd_cpu_queue, work); 172 /* 173 * Only handle one request at a time to avoid hogging crypto workqueue. 174 */ 175 local_bh_disable(); 176 __local_lock_nested_bh(&cpu_queue->bh_lock); 177 backlog = crypto_get_backlog(&cpu_queue->queue); 178 req = crypto_dequeue_request(&cpu_queue->queue); 179 __local_unlock_nested_bh(&cpu_queue->bh_lock); 180 local_bh_enable(); 181 182 if (!req) 183 return; 184 185 if (backlog) 186 crypto_request_complete(backlog, -EINPROGRESS); 187 crypto_request_complete(req, 0); 188 189 if (cpu_queue->queue.qlen) 190 queue_work(cryptd_wq, &cpu_queue->work); 191 } 192 193 static inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm) 194 { 195 struct crypto_instance *inst = crypto_tfm_alg_instance(tfm); 196 struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst); 197 return ictx->queue; 198 } 199 200 static void cryptd_type_and_mask(struct crypto_attr_type *algt, 201 u32 *type, u32 *mask) 202 { 203 /* 204 * cryptd is allowed to wrap internal algorithms, but in that case the 205 * resulting cryptd instance will be marked as internal as well. 206 */ 207 *type = algt->type & CRYPTO_ALG_INTERNAL; 208 *mask = algt->mask & CRYPTO_ALG_INTERNAL; 209 210 /* No point in cryptd wrapping an algorithm that's already async. */ 211 *mask |= CRYPTO_ALG_ASYNC; 212 213 *mask |= crypto_algt_inherited_mask(algt); 214 } 215 216 static int cryptd_init_instance(struct crypto_instance *inst, 217 struct crypto_alg *alg) 218 { 219 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, 220 "cryptd(%s)", 221 alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) 222 return -ENAMETOOLONG; 223 224 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); 225 226 inst->alg.cra_priority = alg->cra_priority + 50; 227 inst->alg.cra_blocksize = alg->cra_blocksize; 228 inst->alg.cra_alignmask = alg->cra_alignmask; 229 230 return 0; 231 } 232 233 static int cryptd_skcipher_setkey(struct crypto_skcipher *parent, 234 const u8 *key, unsigned int keylen) 235 { 236 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(parent); 237 struct crypto_skcipher *child = ctx->child; 238 239 crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); 240 crypto_skcipher_set_flags(child, 241 crypto_skcipher_get_flags(parent) & 242 CRYPTO_TFM_REQ_MASK); 243 return crypto_skcipher_setkey(child, key, keylen); 244 } 245 246 static struct skcipher_request *cryptd_skcipher_prepare( 247 struct skcipher_request *req, int err) 248 { 249 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req); 250 struct skcipher_request *subreq = &rctx->req; 251 struct cryptd_skcipher_ctx *ctx; 252 struct crypto_skcipher *child; 253 254 req->base.complete = subreq->base.complete; 255 req->base.data = subreq->base.data; 256 257 if (unlikely(err == -EINPROGRESS)) 258 return NULL; 259 260 ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req)); 261 child = ctx->child; 262 263 skcipher_request_set_tfm(subreq, child); 264 skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP, 265 NULL, NULL); 266 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, 267 req->iv); 268 269 return subreq; 270 } 271 272 static void cryptd_skcipher_complete(struct skcipher_request *req, int err, 273 crypto_completion_t complete) 274 { 275 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req); 276 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 277 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 278 struct skcipher_request *subreq = &rctx->req; 279 int refcnt = refcount_read(&ctx->refcnt); 280 281 local_bh_disable(); 282 skcipher_request_complete(req, err); 283 local_bh_enable(); 284 285 if (unlikely(err == -EINPROGRESS)) { 286 subreq->base.complete = req->base.complete; 287 subreq->base.data = req->base.data; 288 req->base.complete = complete; 289 req->base.data = req; 290 } else if (refcnt && refcount_dec_and_test(&ctx->refcnt)) 291 crypto_free_skcipher(tfm); 292 } 293 294 static void cryptd_skcipher_encrypt(void *data, int err) 295 { 296 struct skcipher_request *req = data; 297 struct skcipher_request *subreq; 298 299 subreq = cryptd_skcipher_prepare(req, err); 300 if (likely(subreq)) 301 err = crypto_skcipher_encrypt(subreq); 302 303 cryptd_skcipher_complete(req, err, cryptd_skcipher_encrypt); 304 } 305 306 static void cryptd_skcipher_decrypt(void *data, int err) 307 { 308 struct skcipher_request *req = data; 309 struct skcipher_request *subreq; 310 311 subreq = cryptd_skcipher_prepare(req, err); 312 if (likely(subreq)) 313 err = crypto_skcipher_decrypt(subreq); 314 315 cryptd_skcipher_complete(req, err, cryptd_skcipher_decrypt); 316 } 317 318 static int cryptd_skcipher_enqueue(struct skcipher_request *req, 319 crypto_completion_t compl) 320 { 321 struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req); 322 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 323 struct skcipher_request *subreq = &rctx->req; 324 struct cryptd_queue *queue; 325 326 queue = cryptd_get_queue(crypto_skcipher_tfm(tfm)); 327 subreq->base.complete = req->base.complete; 328 subreq->base.data = req->base.data; 329 req->base.complete = compl; 330 req->base.data = req; 331 332 return cryptd_enqueue_request(queue, &req->base); 333 } 334 335 static int cryptd_skcipher_encrypt_enqueue(struct skcipher_request *req) 336 { 337 return cryptd_skcipher_enqueue(req, cryptd_skcipher_encrypt); 338 } 339 340 static int cryptd_skcipher_decrypt_enqueue(struct skcipher_request *req) 341 { 342 return cryptd_skcipher_enqueue(req, cryptd_skcipher_decrypt); 343 } 344 345 static int cryptd_skcipher_init_tfm(struct crypto_skcipher *tfm) 346 { 347 struct skcipher_instance *inst = skcipher_alg_instance(tfm); 348 struct skcipherd_instance_ctx *ictx = skcipher_instance_ctx(inst); 349 struct crypto_skcipher_spawn *spawn = &ictx->spawn; 350 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 351 struct crypto_skcipher *cipher; 352 353 cipher = crypto_spawn_skcipher(spawn); 354 if (IS_ERR(cipher)) 355 return PTR_ERR(cipher); 356 357 ctx->child = cipher; 358 crypto_skcipher_set_reqsize( 359 tfm, sizeof(struct cryptd_skcipher_request_ctx) + 360 crypto_skcipher_reqsize(cipher)); 361 return 0; 362 } 363 364 static void cryptd_skcipher_exit_tfm(struct crypto_skcipher *tfm) 365 { 366 struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 367 368 crypto_free_skcipher(ctx->child); 369 } 370 371 static void cryptd_skcipher_free(struct skcipher_instance *inst) 372 { 373 struct skcipherd_instance_ctx *ctx = skcipher_instance_ctx(inst); 374 375 crypto_drop_skcipher(&ctx->spawn); 376 kfree(inst); 377 } 378 379 static int cryptd_create_skcipher(struct crypto_template *tmpl, 380 struct rtattr **tb, 381 struct crypto_attr_type *algt, 382 struct cryptd_queue *queue) 383 { 384 struct skcipherd_instance_ctx *ctx; 385 struct skcipher_instance *inst; 386 struct skcipher_alg_common *alg; 387 u32 type; 388 u32 mask; 389 int err; 390 391 cryptd_type_and_mask(algt, &type, &mask); 392 393 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); 394 if (!inst) 395 return -ENOMEM; 396 397 ctx = skcipher_instance_ctx(inst); 398 ctx->queue = queue; 399 400 err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst), 401 crypto_attr_alg_name(tb[1]), type, mask); 402 if (err) 403 goto err_free_inst; 404 405 alg = crypto_spawn_skcipher_alg_common(&ctx->spawn); 406 err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base); 407 if (err) 408 goto err_free_inst; 409 410 inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC | 411 (alg->base.cra_flags & CRYPTO_ALG_INTERNAL); 412 inst->alg.ivsize = alg->ivsize; 413 inst->alg.chunksize = alg->chunksize; 414 inst->alg.min_keysize = alg->min_keysize; 415 inst->alg.max_keysize = alg->max_keysize; 416 417 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_skcipher_ctx); 418 419 inst->alg.init = cryptd_skcipher_init_tfm; 420 inst->alg.exit = cryptd_skcipher_exit_tfm; 421 422 inst->alg.setkey = cryptd_skcipher_setkey; 423 inst->alg.encrypt = cryptd_skcipher_encrypt_enqueue; 424 inst->alg.decrypt = cryptd_skcipher_decrypt_enqueue; 425 426 inst->free = cryptd_skcipher_free; 427 428 err = skcipher_register_instance(tmpl, inst); 429 if (err) { 430 err_free_inst: 431 cryptd_skcipher_free(inst); 432 } 433 return err; 434 } 435 436 static int cryptd_hash_init_tfm(struct crypto_ahash *tfm) 437 { 438 struct ahash_instance *inst = ahash_alg_instance(tfm); 439 struct hashd_instance_ctx *ictx = ahash_instance_ctx(inst); 440 struct crypto_shash_spawn *spawn = &ictx->spawn; 441 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm); 442 struct crypto_shash *hash; 443 444 hash = crypto_spawn_shash(spawn); 445 if (IS_ERR(hash)) 446 return PTR_ERR(hash); 447 448 ctx->child = hash; 449 crypto_ahash_set_reqsize(tfm, 450 sizeof(struct cryptd_hash_request_ctx) + 451 crypto_shash_descsize(hash)); 452 return 0; 453 } 454 455 static void cryptd_hash_exit_tfm(struct crypto_ahash *tfm) 456 { 457 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm); 458 459 crypto_free_shash(ctx->child); 460 } 461 462 static int cryptd_hash_setkey(struct crypto_ahash *parent, 463 const u8 *key, unsigned int keylen) 464 { 465 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent); 466 struct crypto_shash *child = ctx->child; 467 468 crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK); 469 crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) & 470 CRYPTO_TFM_REQ_MASK); 471 return crypto_shash_setkey(child, key, keylen); 472 } 473 474 static int cryptd_hash_enqueue(struct ahash_request *req, 475 crypto_completion_t compl) 476 { 477 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); 478 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 479 struct cryptd_queue *queue = 480 cryptd_get_queue(crypto_ahash_tfm(tfm)); 481 482 rctx->complete = req->base.complete; 483 rctx->data = req->base.data; 484 req->base.complete = compl; 485 req->base.data = req; 486 487 return cryptd_enqueue_request(queue, &req->base); 488 } 489 490 static struct shash_desc *cryptd_hash_prepare(struct ahash_request *req, 491 int err) 492 { 493 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); 494 495 req->base.complete = rctx->complete; 496 req->base.data = rctx->data; 497 498 if (unlikely(err == -EINPROGRESS)) 499 return NULL; 500 501 return &rctx->desc; 502 } 503 504 static void cryptd_hash_complete(struct ahash_request *req, int err, 505 crypto_completion_t complete) 506 { 507 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 508 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm); 509 int refcnt = refcount_read(&ctx->refcnt); 510 511 local_bh_disable(); 512 ahash_request_complete(req, err); 513 local_bh_enable(); 514 515 if (err == -EINPROGRESS) { 516 req->base.complete = complete; 517 req->base.data = req; 518 } else if (refcnt && refcount_dec_and_test(&ctx->refcnt)) 519 crypto_free_ahash(tfm); 520 } 521 522 static void cryptd_hash_init(void *data, int err) 523 { 524 struct ahash_request *req = data; 525 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 526 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm); 527 struct crypto_shash *child = ctx->child; 528 struct shash_desc *desc; 529 530 desc = cryptd_hash_prepare(req, err); 531 if (unlikely(!desc)) 532 goto out; 533 534 desc->tfm = child; 535 536 err = crypto_shash_init(desc); 537 538 out: 539 cryptd_hash_complete(req, err, cryptd_hash_init); 540 } 541 542 static int cryptd_hash_init_enqueue(struct ahash_request *req) 543 { 544 return cryptd_hash_enqueue(req, cryptd_hash_init); 545 } 546 547 static void cryptd_hash_update(void *data, int err) 548 { 549 struct ahash_request *req = data; 550 struct shash_desc *desc; 551 552 desc = cryptd_hash_prepare(req, err); 553 if (likely(desc)) 554 err = shash_ahash_update(req, desc); 555 556 cryptd_hash_complete(req, err, cryptd_hash_update); 557 } 558 559 static int cryptd_hash_update_enqueue(struct ahash_request *req) 560 { 561 return cryptd_hash_enqueue(req, cryptd_hash_update); 562 } 563 564 static void cryptd_hash_final(void *data, int err) 565 { 566 struct ahash_request *req = data; 567 struct shash_desc *desc; 568 569 desc = cryptd_hash_prepare(req, err); 570 if (likely(desc)) 571 err = crypto_shash_final(desc, req->result); 572 573 cryptd_hash_complete(req, err, cryptd_hash_final); 574 } 575 576 static int cryptd_hash_final_enqueue(struct ahash_request *req) 577 { 578 return cryptd_hash_enqueue(req, cryptd_hash_final); 579 } 580 581 static void cryptd_hash_finup(void *data, int err) 582 { 583 struct ahash_request *req = data; 584 struct shash_desc *desc; 585 586 desc = cryptd_hash_prepare(req, err); 587 if (likely(desc)) 588 err = shash_ahash_finup(req, desc); 589 590 cryptd_hash_complete(req, err, cryptd_hash_finup); 591 } 592 593 static int cryptd_hash_finup_enqueue(struct ahash_request *req) 594 { 595 return cryptd_hash_enqueue(req, cryptd_hash_finup); 596 } 597 598 static void cryptd_hash_digest(void *data, int err) 599 { 600 struct ahash_request *req = data; 601 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 602 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm); 603 struct crypto_shash *child = ctx->child; 604 struct shash_desc *desc; 605 606 desc = cryptd_hash_prepare(req, err); 607 if (unlikely(!desc)) 608 goto out; 609 610 desc->tfm = child; 611 612 err = shash_ahash_digest(req, desc); 613 614 out: 615 cryptd_hash_complete(req, err, cryptd_hash_digest); 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 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 { 632 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 633 struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm); 634 struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); 635 struct shash_desc *desc = &rctx->desc; 636 637 desc->tfm = ctx->child; 638 639 return crypto_shash_import(desc, in); 640 } 641 642 static void cryptd_hash_free(struct ahash_instance *inst) 643 { 644 struct hashd_instance_ctx *ctx = ahash_instance_ctx(inst); 645 646 crypto_drop_shash(&ctx->spawn); 647 kfree(inst); 648 } 649 650 static int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb, 651 struct crypto_attr_type *algt, 652 struct cryptd_queue *queue) 653 { 654 struct hashd_instance_ctx *ctx; 655 struct ahash_instance *inst; 656 struct shash_alg *alg; 657 u32 type; 658 u32 mask; 659 int err; 660 661 cryptd_type_and_mask(algt, &type, &mask); 662 663 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); 664 if (!inst) 665 return -ENOMEM; 666 667 ctx = ahash_instance_ctx(inst); 668 ctx->queue = queue; 669 670 err = crypto_grab_shash(&ctx->spawn, ahash_crypto_instance(inst), 671 crypto_attr_alg_name(tb[1]), type, mask); 672 if (err) 673 goto err_free_inst; 674 alg = crypto_spawn_shash_alg(&ctx->spawn); 675 676 err = cryptd_init_instance(ahash_crypto_instance(inst), &alg->base); 677 if (err) 678 goto err_free_inst; 679 680 inst->alg.halg.base.cra_flags |= CRYPTO_ALG_ASYNC | 681 (alg->base.cra_flags & (CRYPTO_ALG_INTERNAL| 682 CRYPTO_ALG_OPTIONAL_KEY)); 683 inst->alg.halg.digestsize = alg->digestsize; 684 inst->alg.halg.statesize = alg->statesize; 685 inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx); 686 687 inst->alg.init_tfm = cryptd_hash_init_tfm; 688 inst->alg.exit_tfm = cryptd_hash_exit_tfm; 689 690 inst->alg.init = cryptd_hash_init_enqueue; 691 inst->alg.update = cryptd_hash_update_enqueue; 692 inst->alg.final = cryptd_hash_final_enqueue; 693 inst->alg.finup = cryptd_hash_finup_enqueue; 694 inst->alg.export = cryptd_hash_export; 695 inst->alg.import = cryptd_hash_import; 696 if (crypto_shash_alg_has_setkey(alg)) 697 inst->alg.setkey = cryptd_hash_setkey; 698 inst->alg.digest = cryptd_hash_digest_enqueue; 699 700 inst->free = cryptd_hash_free; 701 702 err = ahash_register_instance(tmpl, inst); 703 if (err) { 704 err_free_inst: 705 cryptd_hash_free(inst); 706 } 707 return err; 708 } 709 710 static int cryptd_aead_setkey(struct crypto_aead *parent, 711 const u8 *key, unsigned int keylen) 712 { 713 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent); 714 struct crypto_aead *child = ctx->child; 715 716 return crypto_aead_setkey(child, key, keylen); 717 } 718 719 static int cryptd_aead_setauthsize(struct crypto_aead *parent, 720 unsigned int authsize) 721 { 722 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent); 723 struct crypto_aead *child = ctx->child; 724 725 return crypto_aead_setauthsize(child, authsize); 726 } 727 728 static void cryptd_aead_crypt(struct aead_request *req, 729 struct crypto_aead *child, int err, 730 int (*crypt)(struct aead_request *req), 731 crypto_completion_t compl) 732 { 733 struct cryptd_aead_request_ctx *rctx; 734 struct aead_request *subreq; 735 struct cryptd_aead_ctx *ctx; 736 struct crypto_aead *tfm; 737 int refcnt; 738 739 rctx = aead_request_ctx(req); 740 subreq = &rctx->req; 741 req->base.complete = subreq->base.complete; 742 req->base.data = subreq->base.data; 743 744 tfm = crypto_aead_reqtfm(req); 745 746 if (unlikely(err == -EINPROGRESS)) 747 goto out; 748 749 aead_request_set_tfm(subreq, child); 750 aead_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP, 751 NULL, NULL); 752 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, 753 req->iv); 754 aead_request_set_ad(subreq, req->assoclen); 755 756 err = crypt(subreq); 757 758 out: 759 ctx = crypto_aead_ctx(tfm); 760 refcnt = refcount_read(&ctx->refcnt); 761 762 local_bh_disable(); 763 aead_request_complete(req, err); 764 local_bh_enable(); 765 766 if (err == -EINPROGRESS) { 767 subreq->base.complete = req->base.complete; 768 subreq->base.data = req->base.data; 769 req->base.complete = compl; 770 req->base.data = req; 771 } else if (refcnt && refcount_dec_and_test(&ctx->refcnt)) 772 crypto_free_aead(tfm); 773 } 774 775 static void cryptd_aead_encrypt(void *data, int err) 776 { 777 struct aead_request *req = data; 778 struct cryptd_aead_ctx *ctx; 779 struct crypto_aead *child; 780 781 ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); 782 child = ctx->child; 783 cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->encrypt, 784 cryptd_aead_encrypt); 785 } 786 787 static void cryptd_aead_decrypt(void *data, int err) 788 { 789 struct aead_request *req = data; 790 struct cryptd_aead_ctx *ctx; 791 struct crypto_aead *child; 792 793 ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); 794 child = ctx->child; 795 cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->decrypt, 796 cryptd_aead_decrypt); 797 } 798 799 static int cryptd_aead_enqueue(struct aead_request *req, 800 crypto_completion_t compl) 801 { 802 struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req); 803 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 804 struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm)); 805 struct aead_request *subreq = &rctx->req; 806 807 subreq->base.complete = req->base.complete; 808 subreq->base.data = req->base.data; 809 req->base.complete = compl; 810 req->base.data = req; 811 return cryptd_enqueue_request(queue, &req->base); 812 } 813 814 static int cryptd_aead_encrypt_enqueue(struct aead_request *req) 815 { 816 return cryptd_aead_enqueue(req, cryptd_aead_encrypt ); 817 } 818 819 static int cryptd_aead_decrypt_enqueue(struct aead_request *req) 820 { 821 return cryptd_aead_enqueue(req, cryptd_aead_decrypt ); 822 } 823 824 static int cryptd_aead_init_tfm(struct crypto_aead *tfm) 825 { 826 struct aead_instance *inst = aead_alg_instance(tfm); 827 struct aead_instance_ctx *ictx = aead_instance_ctx(inst); 828 struct crypto_aead_spawn *spawn = &ictx->aead_spawn; 829 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm); 830 struct crypto_aead *cipher; 831 832 cipher = crypto_spawn_aead(spawn); 833 if (IS_ERR(cipher)) 834 return PTR_ERR(cipher); 835 836 ctx->child = cipher; 837 crypto_aead_set_reqsize( 838 tfm, sizeof(struct cryptd_aead_request_ctx) + 839 crypto_aead_reqsize(cipher)); 840 return 0; 841 } 842 843 static void cryptd_aead_exit_tfm(struct crypto_aead *tfm) 844 { 845 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm); 846 crypto_free_aead(ctx->child); 847 } 848 849 static void cryptd_aead_free(struct aead_instance *inst) 850 { 851 struct aead_instance_ctx *ctx = aead_instance_ctx(inst); 852 853 crypto_drop_aead(&ctx->aead_spawn); 854 kfree(inst); 855 } 856 857 static int cryptd_create_aead(struct crypto_template *tmpl, 858 struct rtattr **tb, 859 struct crypto_attr_type *algt, 860 struct cryptd_queue *queue) 861 { 862 struct aead_instance_ctx *ctx; 863 struct aead_instance *inst; 864 struct aead_alg *alg; 865 u32 type; 866 u32 mask; 867 int err; 868 869 cryptd_type_and_mask(algt, &type, &mask); 870 871 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); 872 if (!inst) 873 return -ENOMEM; 874 875 ctx = aead_instance_ctx(inst); 876 ctx->queue = queue; 877 878 err = crypto_grab_aead(&ctx->aead_spawn, aead_crypto_instance(inst), 879 crypto_attr_alg_name(tb[1]), type, mask); 880 if (err) 881 goto err_free_inst; 882 883 alg = crypto_spawn_aead_alg(&ctx->aead_spawn); 884 err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base); 885 if (err) 886 goto err_free_inst; 887 888 inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC | 889 (alg->base.cra_flags & CRYPTO_ALG_INTERNAL); 890 inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx); 891 892 inst->alg.ivsize = crypto_aead_alg_ivsize(alg); 893 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg); 894 895 inst->alg.init = cryptd_aead_init_tfm; 896 inst->alg.exit = cryptd_aead_exit_tfm; 897 inst->alg.setkey = cryptd_aead_setkey; 898 inst->alg.setauthsize = cryptd_aead_setauthsize; 899 inst->alg.encrypt = cryptd_aead_encrypt_enqueue; 900 inst->alg.decrypt = cryptd_aead_decrypt_enqueue; 901 902 inst->free = cryptd_aead_free; 903 904 err = aead_register_instance(tmpl, inst); 905 if (err) { 906 err_free_inst: 907 cryptd_aead_free(inst); 908 } 909 return err; 910 } 911 912 static struct cryptd_queue queue; 913 914 static int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb) 915 { 916 struct crypto_attr_type *algt; 917 918 algt = crypto_get_attr_type(tb); 919 if (IS_ERR(algt)) 920 return PTR_ERR(algt); 921 922 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) { 923 case CRYPTO_ALG_TYPE_LSKCIPHER: 924 return cryptd_create_skcipher(tmpl, tb, algt, &queue); 925 case CRYPTO_ALG_TYPE_HASH: 926 return cryptd_create_hash(tmpl, tb, algt, &queue); 927 case CRYPTO_ALG_TYPE_AEAD: 928 return cryptd_create_aead(tmpl, tb, algt, &queue); 929 } 930 931 return -EINVAL; 932 } 933 934 static struct crypto_template cryptd_tmpl = { 935 .name = "cryptd", 936 .create = cryptd_create, 937 .module = THIS_MODULE, 938 }; 939 940 struct cryptd_aead *cryptd_alloc_aead(const char *alg_name, 941 u32 type, u32 mask) 942 { 943 char cryptd_alg_name[CRYPTO_MAX_ALG_NAME]; 944 struct cryptd_aead_ctx *ctx; 945 struct crypto_aead *tfm; 946 947 if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME, 948 "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME) 949 return ERR_PTR(-EINVAL); 950 tfm = crypto_alloc_aead(cryptd_alg_name, type, mask); 951 if (IS_ERR(tfm)) 952 return ERR_CAST(tfm); 953 if (tfm->base.__crt_alg->cra_module != THIS_MODULE) { 954 crypto_free_aead(tfm); 955 return ERR_PTR(-EINVAL); 956 } 957 958 ctx = crypto_aead_ctx(tfm); 959 refcount_set(&ctx->refcnt, 1); 960 961 return __cryptd_aead_cast(tfm); 962 } 963 EXPORT_SYMBOL_GPL(cryptd_alloc_aead); 964 965 struct crypto_aead *cryptd_aead_child(struct cryptd_aead *tfm) 966 { 967 struct cryptd_aead_ctx *ctx; 968 ctx = crypto_aead_ctx(&tfm->base); 969 return ctx->child; 970 } 971 EXPORT_SYMBOL_GPL(cryptd_aead_child); 972 973 bool cryptd_aead_queued(struct cryptd_aead *tfm) 974 { 975 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base); 976 977 return refcount_read(&ctx->refcnt) - 1; 978 } 979 EXPORT_SYMBOL_GPL(cryptd_aead_queued); 980 981 void cryptd_free_aead(struct cryptd_aead *tfm) 982 { 983 struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base); 984 985 if (refcount_dec_and_test(&ctx->refcnt)) 986 crypto_free_aead(&tfm->base); 987 } 988 EXPORT_SYMBOL_GPL(cryptd_free_aead); 989 990 static int __init cryptd_init(void) 991 { 992 int err; 993 994 cryptd_wq = alloc_workqueue("cryptd", 995 WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE | WQ_PERCPU, 996 1); 997 if (!cryptd_wq) 998 return -ENOMEM; 999 1000 err = cryptd_init_queue(&queue, cryptd_max_cpu_qlen); 1001 if (err) 1002 goto err_destroy_wq; 1003 1004 err = crypto_register_template(&cryptd_tmpl); 1005 if (err) 1006 goto err_fini_queue; 1007 1008 return 0; 1009 1010 err_fini_queue: 1011 cryptd_fini_queue(&queue); 1012 err_destroy_wq: 1013 destroy_workqueue(cryptd_wq); 1014 return err; 1015 } 1016 1017 static void __exit cryptd_exit(void) 1018 { 1019 destroy_workqueue(cryptd_wq); 1020 cryptd_fini_queue(&queue); 1021 crypto_unregister_template(&cryptd_tmpl); 1022 } 1023 1024 module_init(cryptd_init); 1025 module_exit(cryptd_exit); 1026 1027 MODULE_LICENSE("GPL"); 1028 MODULE_DESCRIPTION("Software async crypto daemon"); 1029 MODULE_ALIAS_CRYPTO("cryptd"); 1030