18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Software async crypto daemon. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Added AEAD support to cryptd. 88c2ecf20Sopenharmony_ci * Authors: Tadeusz Struk (tadeusz.struk@intel.com) 98c2ecf20Sopenharmony_ci * Adrian Hoban <adrian.hoban@intel.com> 108c2ecf20Sopenharmony_ci * Gabriele Paoloni <gabriele.paoloni@intel.com> 118c2ecf20Sopenharmony_ci * Aidan O'Mahony (aidan.o.mahony@intel.com) 128c2ecf20Sopenharmony_ci * Copyright (c) 2010, Intel Corporation. 138c2ecf20Sopenharmony_ci */ 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h> 168c2ecf20Sopenharmony_ci#include <crypto/internal/aead.h> 178c2ecf20Sopenharmony_ci#include <crypto/internal/skcipher.h> 188c2ecf20Sopenharmony_ci#include <crypto/cryptd.h> 198c2ecf20Sopenharmony_ci#include <linux/refcount.h> 208c2ecf20Sopenharmony_ci#include <linux/err.h> 218c2ecf20Sopenharmony_ci#include <linux/init.h> 228c2ecf20Sopenharmony_ci#include <linux/kernel.h> 238c2ecf20Sopenharmony_ci#include <linux/list.h> 248c2ecf20Sopenharmony_ci#include <linux/module.h> 258c2ecf20Sopenharmony_ci#include <linux/scatterlist.h> 268c2ecf20Sopenharmony_ci#include <linux/sched.h> 278c2ecf20Sopenharmony_ci#include <linux/slab.h> 288c2ecf20Sopenharmony_ci#include <linux/workqueue.h> 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistatic unsigned int cryptd_max_cpu_qlen = 1000; 318c2ecf20Sopenharmony_cimodule_param(cryptd_max_cpu_qlen, uint, 0); 328c2ecf20Sopenharmony_ciMODULE_PARM_DESC(cryptd_max_cpu_qlen, "Set cryptd Max queue depth"); 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic struct workqueue_struct *cryptd_wq; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistruct cryptd_cpu_queue { 378c2ecf20Sopenharmony_ci struct crypto_queue queue; 388c2ecf20Sopenharmony_ci struct work_struct work; 398c2ecf20Sopenharmony_ci}; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistruct cryptd_queue { 428c2ecf20Sopenharmony_ci /* 438c2ecf20Sopenharmony_ci * Protected by disabling BH to allow enqueueing from softinterrupt and 448c2ecf20Sopenharmony_ci * dequeuing from kworker (cryptd_queue_worker()). 458c2ecf20Sopenharmony_ci */ 468c2ecf20Sopenharmony_ci struct cryptd_cpu_queue __percpu *cpu_queue; 478c2ecf20Sopenharmony_ci}; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistruct cryptd_instance_ctx { 508c2ecf20Sopenharmony_ci struct crypto_spawn spawn; 518c2ecf20Sopenharmony_ci struct cryptd_queue *queue; 528c2ecf20Sopenharmony_ci}; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_cistruct skcipherd_instance_ctx { 558c2ecf20Sopenharmony_ci struct crypto_skcipher_spawn spawn; 568c2ecf20Sopenharmony_ci struct cryptd_queue *queue; 578c2ecf20Sopenharmony_ci}; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_cistruct hashd_instance_ctx { 608c2ecf20Sopenharmony_ci struct crypto_shash_spawn spawn; 618c2ecf20Sopenharmony_ci struct cryptd_queue *queue; 628c2ecf20Sopenharmony_ci}; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistruct aead_instance_ctx { 658c2ecf20Sopenharmony_ci struct crypto_aead_spawn aead_spawn; 668c2ecf20Sopenharmony_ci struct cryptd_queue *queue; 678c2ecf20Sopenharmony_ci}; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_cistruct cryptd_skcipher_ctx { 708c2ecf20Sopenharmony_ci refcount_t refcnt; 718c2ecf20Sopenharmony_ci struct crypto_skcipher *child; 728c2ecf20Sopenharmony_ci}; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistruct cryptd_skcipher_request_ctx { 758c2ecf20Sopenharmony_ci crypto_completion_t complete; 768c2ecf20Sopenharmony_ci struct skcipher_request req; 778c2ecf20Sopenharmony_ci}; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_cistruct cryptd_hash_ctx { 808c2ecf20Sopenharmony_ci refcount_t refcnt; 818c2ecf20Sopenharmony_ci struct crypto_shash *child; 828c2ecf20Sopenharmony_ci}; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_cistruct cryptd_hash_request_ctx { 858c2ecf20Sopenharmony_ci crypto_completion_t complete; 868c2ecf20Sopenharmony_ci struct shash_desc desc; 878c2ecf20Sopenharmony_ci}; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cistruct cryptd_aead_ctx { 908c2ecf20Sopenharmony_ci refcount_t refcnt; 918c2ecf20Sopenharmony_ci struct crypto_aead *child; 928c2ecf20Sopenharmony_ci}; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_cistruct cryptd_aead_request_ctx { 958c2ecf20Sopenharmony_ci crypto_completion_t complete; 968c2ecf20Sopenharmony_ci}; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic void cryptd_queue_worker(struct work_struct *work); 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic int cryptd_init_queue(struct cryptd_queue *queue, 1018c2ecf20Sopenharmony_ci unsigned int max_cpu_qlen) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci int cpu; 1048c2ecf20Sopenharmony_ci struct cryptd_cpu_queue *cpu_queue; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci queue->cpu_queue = alloc_percpu(struct cryptd_cpu_queue); 1078c2ecf20Sopenharmony_ci if (!queue->cpu_queue) 1088c2ecf20Sopenharmony_ci return -ENOMEM; 1098c2ecf20Sopenharmony_ci for_each_possible_cpu(cpu) { 1108c2ecf20Sopenharmony_ci cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu); 1118c2ecf20Sopenharmony_ci crypto_init_queue(&cpu_queue->queue, max_cpu_qlen); 1128c2ecf20Sopenharmony_ci INIT_WORK(&cpu_queue->work, cryptd_queue_worker); 1138c2ecf20Sopenharmony_ci } 1148c2ecf20Sopenharmony_ci pr_info("cryptd: max_cpu_qlen set to %d\n", max_cpu_qlen); 1158c2ecf20Sopenharmony_ci return 0; 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic void cryptd_fini_queue(struct cryptd_queue *queue) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci int cpu; 1218c2ecf20Sopenharmony_ci struct cryptd_cpu_queue *cpu_queue; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci for_each_possible_cpu(cpu) { 1248c2ecf20Sopenharmony_ci cpu_queue = per_cpu_ptr(queue->cpu_queue, cpu); 1258c2ecf20Sopenharmony_ci BUG_ON(cpu_queue->queue.qlen); 1268c2ecf20Sopenharmony_ci } 1278c2ecf20Sopenharmony_ci free_percpu(queue->cpu_queue); 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic int cryptd_enqueue_request(struct cryptd_queue *queue, 1318c2ecf20Sopenharmony_ci struct crypto_async_request *request) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci int err; 1348c2ecf20Sopenharmony_ci struct cryptd_cpu_queue *cpu_queue; 1358c2ecf20Sopenharmony_ci refcount_t *refcnt; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci local_bh_disable(); 1388c2ecf20Sopenharmony_ci cpu_queue = this_cpu_ptr(queue->cpu_queue); 1398c2ecf20Sopenharmony_ci err = crypto_enqueue_request(&cpu_queue->queue, request); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci refcnt = crypto_tfm_ctx(request->tfm); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci if (err == -ENOSPC) 1448c2ecf20Sopenharmony_ci goto out; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci queue_work_on(smp_processor_id(), cryptd_wq, &cpu_queue->work); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci if (!refcount_read(refcnt)) 1498c2ecf20Sopenharmony_ci goto out; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci refcount_inc(refcnt); 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ciout: 1548c2ecf20Sopenharmony_ci local_bh_enable(); 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci return err; 1578c2ecf20Sopenharmony_ci} 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci/* Called in workqueue context, do one real cryption work (via 1608c2ecf20Sopenharmony_ci * req->complete) and reschedule itself if there are more work to 1618c2ecf20Sopenharmony_ci * do. */ 1628c2ecf20Sopenharmony_cistatic void cryptd_queue_worker(struct work_struct *work) 1638c2ecf20Sopenharmony_ci{ 1648c2ecf20Sopenharmony_ci struct cryptd_cpu_queue *cpu_queue; 1658c2ecf20Sopenharmony_ci struct crypto_async_request *req, *backlog; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci cpu_queue = container_of(work, struct cryptd_cpu_queue, work); 1688c2ecf20Sopenharmony_ci /* 1698c2ecf20Sopenharmony_ci * Only handle one request at a time to avoid hogging crypto workqueue. 1708c2ecf20Sopenharmony_ci */ 1718c2ecf20Sopenharmony_ci local_bh_disable(); 1728c2ecf20Sopenharmony_ci backlog = crypto_get_backlog(&cpu_queue->queue); 1738c2ecf20Sopenharmony_ci req = crypto_dequeue_request(&cpu_queue->queue); 1748c2ecf20Sopenharmony_ci local_bh_enable(); 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci if (!req) 1778c2ecf20Sopenharmony_ci return; 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci if (backlog) 1808c2ecf20Sopenharmony_ci backlog->complete(backlog, -EINPROGRESS); 1818c2ecf20Sopenharmony_ci req->complete(req, 0); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci if (cpu_queue->queue.qlen) 1848c2ecf20Sopenharmony_ci queue_work(cryptd_wq, &cpu_queue->work); 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_cistatic inline struct cryptd_queue *cryptd_get_queue(struct crypto_tfm *tfm) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci struct crypto_instance *inst = crypto_tfm_alg_instance(tfm); 1908c2ecf20Sopenharmony_ci struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst); 1918c2ecf20Sopenharmony_ci return ictx->queue; 1928c2ecf20Sopenharmony_ci} 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_cistatic void cryptd_type_and_mask(struct crypto_attr_type *algt, 1958c2ecf20Sopenharmony_ci u32 *type, u32 *mask) 1968c2ecf20Sopenharmony_ci{ 1978c2ecf20Sopenharmony_ci /* 1988c2ecf20Sopenharmony_ci * cryptd is allowed to wrap internal algorithms, but in that case the 1998c2ecf20Sopenharmony_ci * resulting cryptd instance will be marked as internal as well. 2008c2ecf20Sopenharmony_ci */ 2018c2ecf20Sopenharmony_ci *type = algt->type & CRYPTO_ALG_INTERNAL; 2028c2ecf20Sopenharmony_ci *mask = algt->mask & CRYPTO_ALG_INTERNAL; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci /* No point in cryptd wrapping an algorithm that's already async. */ 2058c2ecf20Sopenharmony_ci *mask |= CRYPTO_ALG_ASYNC; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci *mask |= crypto_algt_inherited_mask(algt); 2088c2ecf20Sopenharmony_ci} 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_cistatic int cryptd_init_instance(struct crypto_instance *inst, 2118c2ecf20Sopenharmony_ci struct crypto_alg *alg) 2128c2ecf20Sopenharmony_ci{ 2138c2ecf20Sopenharmony_ci if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, 2148c2ecf20Sopenharmony_ci "cryptd(%s)", 2158c2ecf20Sopenharmony_ci alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) 2168c2ecf20Sopenharmony_ci return -ENAMETOOLONG; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci inst->alg.cra_priority = alg->cra_priority + 50; 2218c2ecf20Sopenharmony_ci inst->alg.cra_blocksize = alg->cra_blocksize; 2228c2ecf20Sopenharmony_ci inst->alg.cra_alignmask = alg->cra_alignmask; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci return 0; 2258c2ecf20Sopenharmony_ci} 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_cistatic int cryptd_skcipher_setkey(struct crypto_skcipher *parent, 2288c2ecf20Sopenharmony_ci const u8 *key, unsigned int keylen) 2298c2ecf20Sopenharmony_ci{ 2308c2ecf20Sopenharmony_ci struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(parent); 2318c2ecf20Sopenharmony_ci struct crypto_skcipher *child = ctx->child; 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); 2348c2ecf20Sopenharmony_ci crypto_skcipher_set_flags(child, 2358c2ecf20Sopenharmony_ci crypto_skcipher_get_flags(parent) & 2368c2ecf20Sopenharmony_ci CRYPTO_TFM_REQ_MASK); 2378c2ecf20Sopenharmony_ci return crypto_skcipher_setkey(child, key, keylen); 2388c2ecf20Sopenharmony_ci} 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_cistatic void cryptd_skcipher_complete(struct skcipher_request *req, int err) 2418c2ecf20Sopenharmony_ci{ 2428c2ecf20Sopenharmony_ci struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 2438c2ecf20Sopenharmony_ci struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 2448c2ecf20Sopenharmony_ci struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req); 2458c2ecf20Sopenharmony_ci int refcnt = refcount_read(&ctx->refcnt); 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci local_bh_disable(); 2488c2ecf20Sopenharmony_ci rctx->complete(&req->base, err); 2498c2ecf20Sopenharmony_ci local_bh_enable(); 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt)) 2528c2ecf20Sopenharmony_ci crypto_free_skcipher(tfm); 2538c2ecf20Sopenharmony_ci} 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_cistatic void cryptd_skcipher_encrypt(struct crypto_async_request *base, 2568c2ecf20Sopenharmony_ci int err) 2578c2ecf20Sopenharmony_ci{ 2588c2ecf20Sopenharmony_ci struct skcipher_request *req = skcipher_request_cast(base); 2598c2ecf20Sopenharmony_ci struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req); 2608c2ecf20Sopenharmony_ci struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 2618c2ecf20Sopenharmony_ci struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 2628c2ecf20Sopenharmony_ci struct skcipher_request *subreq = &rctx->req; 2638c2ecf20Sopenharmony_ci struct crypto_skcipher *child = ctx->child; 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci if (unlikely(err == -EINPROGRESS)) 2668c2ecf20Sopenharmony_ci goto out; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci skcipher_request_set_tfm(subreq, child); 2698c2ecf20Sopenharmony_ci skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP, 2708c2ecf20Sopenharmony_ci NULL, NULL); 2718c2ecf20Sopenharmony_ci skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, 2728c2ecf20Sopenharmony_ci req->iv); 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci err = crypto_skcipher_encrypt(subreq); 2758c2ecf20Sopenharmony_ci skcipher_request_zero(subreq); 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci req->base.complete = rctx->complete; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ciout: 2808c2ecf20Sopenharmony_ci cryptd_skcipher_complete(req, err); 2818c2ecf20Sopenharmony_ci} 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_cistatic void cryptd_skcipher_decrypt(struct crypto_async_request *base, 2848c2ecf20Sopenharmony_ci int err) 2858c2ecf20Sopenharmony_ci{ 2868c2ecf20Sopenharmony_ci struct skcipher_request *req = skcipher_request_cast(base); 2878c2ecf20Sopenharmony_ci struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req); 2888c2ecf20Sopenharmony_ci struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 2898c2ecf20Sopenharmony_ci struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 2908c2ecf20Sopenharmony_ci struct skcipher_request *subreq = &rctx->req; 2918c2ecf20Sopenharmony_ci struct crypto_skcipher *child = ctx->child; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci if (unlikely(err == -EINPROGRESS)) 2948c2ecf20Sopenharmony_ci goto out; 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci skcipher_request_set_tfm(subreq, child); 2978c2ecf20Sopenharmony_ci skcipher_request_set_callback(subreq, CRYPTO_TFM_REQ_MAY_SLEEP, 2988c2ecf20Sopenharmony_ci NULL, NULL); 2998c2ecf20Sopenharmony_ci skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, 3008c2ecf20Sopenharmony_ci req->iv); 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci err = crypto_skcipher_decrypt(subreq); 3038c2ecf20Sopenharmony_ci skcipher_request_zero(subreq); 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci req->base.complete = rctx->complete; 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ciout: 3088c2ecf20Sopenharmony_ci cryptd_skcipher_complete(req, err); 3098c2ecf20Sopenharmony_ci} 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_cistatic int cryptd_skcipher_enqueue(struct skcipher_request *req, 3128c2ecf20Sopenharmony_ci crypto_completion_t compl) 3138c2ecf20Sopenharmony_ci{ 3148c2ecf20Sopenharmony_ci struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req); 3158c2ecf20Sopenharmony_ci struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 3168c2ecf20Sopenharmony_ci struct cryptd_queue *queue; 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_ci queue = cryptd_get_queue(crypto_skcipher_tfm(tfm)); 3198c2ecf20Sopenharmony_ci rctx->complete = req->base.complete; 3208c2ecf20Sopenharmony_ci req->base.complete = compl; 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci return cryptd_enqueue_request(queue, &req->base); 3238c2ecf20Sopenharmony_ci} 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_cistatic int cryptd_skcipher_encrypt_enqueue(struct skcipher_request *req) 3268c2ecf20Sopenharmony_ci{ 3278c2ecf20Sopenharmony_ci return cryptd_skcipher_enqueue(req, cryptd_skcipher_encrypt); 3288c2ecf20Sopenharmony_ci} 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_cistatic int cryptd_skcipher_decrypt_enqueue(struct skcipher_request *req) 3318c2ecf20Sopenharmony_ci{ 3328c2ecf20Sopenharmony_ci return cryptd_skcipher_enqueue(req, cryptd_skcipher_decrypt); 3338c2ecf20Sopenharmony_ci} 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_cistatic int cryptd_skcipher_init_tfm(struct crypto_skcipher *tfm) 3368c2ecf20Sopenharmony_ci{ 3378c2ecf20Sopenharmony_ci struct skcipher_instance *inst = skcipher_alg_instance(tfm); 3388c2ecf20Sopenharmony_ci struct skcipherd_instance_ctx *ictx = skcipher_instance_ctx(inst); 3398c2ecf20Sopenharmony_ci struct crypto_skcipher_spawn *spawn = &ictx->spawn; 3408c2ecf20Sopenharmony_ci struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 3418c2ecf20Sopenharmony_ci struct crypto_skcipher *cipher; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci cipher = crypto_spawn_skcipher(spawn); 3448c2ecf20Sopenharmony_ci if (IS_ERR(cipher)) 3458c2ecf20Sopenharmony_ci return PTR_ERR(cipher); 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci ctx->child = cipher; 3488c2ecf20Sopenharmony_ci crypto_skcipher_set_reqsize( 3498c2ecf20Sopenharmony_ci tfm, sizeof(struct cryptd_skcipher_request_ctx) + 3508c2ecf20Sopenharmony_ci crypto_skcipher_reqsize(cipher)); 3518c2ecf20Sopenharmony_ci return 0; 3528c2ecf20Sopenharmony_ci} 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_cistatic void cryptd_skcipher_exit_tfm(struct crypto_skcipher *tfm) 3558c2ecf20Sopenharmony_ci{ 3568c2ecf20Sopenharmony_ci struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci crypto_free_skcipher(ctx->child); 3598c2ecf20Sopenharmony_ci} 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_cistatic void cryptd_skcipher_free(struct skcipher_instance *inst) 3628c2ecf20Sopenharmony_ci{ 3638c2ecf20Sopenharmony_ci struct skcipherd_instance_ctx *ctx = skcipher_instance_ctx(inst); 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci crypto_drop_skcipher(&ctx->spawn); 3668c2ecf20Sopenharmony_ci kfree(inst); 3678c2ecf20Sopenharmony_ci} 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_cistatic int cryptd_create_skcipher(struct crypto_template *tmpl, 3708c2ecf20Sopenharmony_ci struct rtattr **tb, 3718c2ecf20Sopenharmony_ci struct crypto_attr_type *algt, 3728c2ecf20Sopenharmony_ci struct cryptd_queue *queue) 3738c2ecf20Sopenharmony_ci{ 3748c2ecf20Sopenharmony_ci struct skcipherd_instance_ctx *ctx; 3758c2ecf20Sopenharmony_ci struct skcipher_instance *inst; 3768c2ecf20Sopenharmony_ci struct skcipher_alg *alg; 3778c2ecf20Sopenharmony_ci u32 type; 3788c2ecf20Sopenharmony_ci u32 mask; 3798c2ecf20Sopenharmony_ci int err; 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci cryptd_type_and_mask(algt, &type, &mask); 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); 3848c2ecf20Sopenharmony_ci if (!inst) 3858c2ecf20Sopenharmony_ci return -ENOMEM; 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci ctx = skcipher_instance_ctx(inst); 3888c2ecf20Sopenharmony_ci ctx->queue = queue; 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci err = crypto_grab_skcipher(&ctx->spawn, skcipher_crypto_instance(inst), 3918c2ecf20Sopenharmony_ci crypto_attr_alg_name(tb[1]), type, mask); 3928c2ecf20Sopenharmony_ci if (err) 3938c2ecf20Sopenharmony_ci goto err_free_inst; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci alg = crypto_spawn_skcipher_alg(&ctx->spawn); 3968c2ecf20Sopenharmony_ci err = cryptd_init_instance(skcipher_crypto_instance(inst), &alg->base); 3978c2ecf20Sopenharmony_ci if (err) 3988c2ecf20Sopenharmony_ci goto err_free_inst; 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC | 4018c2ecf20Sopenharmony_ci (alg->base.cra_flags & CRYPTO_ALG_INTERNAL); 4028c2ecf20Sopenharmony_ci inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg); 4038c2ecf20Sopenharmony_ci inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg); 4048c2ecf20Sopenharmony_ci inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg); 4058c2ecf20Sopenharmony_ci inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg); 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci inst->alg.base.cra_ctxsize = sizeof(struct cryptd_skcipher_ctx); 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci inst->alg.init = cryptd_skcipher_init_tfm; 4108c2ecf20Sopenharmony_ci inst->alg.exit = cryptd_skcipher_exit_tfm; 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci inst->alg.setkey = cryptd_skcipher_setkey; 4138c2ecf20Sopenharmony_ci inst->alg.encrypt = cryptd_skcipher_encrypt_enqueue; 4148c2ecf20Sopenharmony_ci inst->alg.decrypt = cryptd_skcipher_decrypt_enqueue; 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci inst->free = cryptd_skcipher_free; 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci err = skcipher_register_instance(tmpl, inst); 4198c2ecf20Sopenharmony_ci if (err) { 4208c2ecf20Sopenharmony_cierr_free_inst: 4218c2ecf20Sopenharmony_ci cryptd_skcipher_free(inst); 4228c2ecf20Sopenharmony_ci } 4238c2ecf20Sopenharmony_ci return err; 4248c2ecf20Sopenharmony_ci} 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_cistatic int cryptd_hash_init_tfm(struct crypto_tfm *tfm) 4278c2ecf20Sopenharmony_ci{ 4288c2ecf20Sopenharmony_ci struct crypto_instance *inst = crypto_tfm_alg_instance(tfm); 4298c2ecf20Sopenharmony_ci struct hashd_instance_ctx *ictx = crypto_instance_ctx(inst); 4308c2ecf20Sopenharmony_ci struct crypto_shash_spawn *spawn = &ictx->spawn; 4318c2ecf20Sopenharmony_ci struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm); 4328c2ecf20Sopenharmony_ci struct crypto_shash *hash; 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci hash = crypto_spawn_shash(spawn); 4358c2ecf20Sopenharmony_ci if (IS_ERR(hash)) 4368c2ecf20Sopenharmony_ci return PTR_ERR(hash); 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci ctx->child = hash; 4398c2ecf20Sopenharmony_ci crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), 4408c2ecf20Sopenharmony_ci sizeof(struct cryptd_hash_request_ctx) + 4418c2ecf20Sopenharmony_ci crypto_shash_descsize(hash)); 4428c2ecf20Sopenharmony_ci return 0; 4438c2ecf20Sopenharmony_ci} 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_cistatic void cryptd_hash_exit_tfm(struct crypto_tfm *tfm) 4468c2ecf20Sopenharmony_ci{ 4478c2ecf20Sopenharmony_ci struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm); 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci crypto_free_shash(ctx->child); 4508c2ecf20Sopenharmony_ci} 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_cistatic int cryptd_hash_setkey(struct crypto_ahash *parent, 4538c2ecf20Sopenharmony_ci const u8 *key, unsigned int keylen) 4548c2ecf20Sopenharmony_ci{ 4558c2ecf20Sopenharmony_ci struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent); 4568c2ecf20Sopenharmony_ci struct crypto_shash *child = ctx->child; 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ci crypto_shash_clear_flags(child, CRYPTO_TFM_REQ_MASK); 4598c2ecf20Sopenharmony_ci crypto_shash_set_flags(child, crypto_ahash_get_flags(parent) & 4608c2ecf20Sopenharmony_ci CRYPTO_TFM_REQ_MASK); 4618c2ecf20Sopenharmony_ci return crypto_shash_setkey(child, key, keylen); 4628c2ecf20Sopenharmony_ci} 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_cistatic int cryptd_hash_enqueue(struct ahash_request *req, 4658c2ecf20Sopenharmony_ci crypto_completion_t compl) 4668c2ecf20Sopenharmony_ci{ 4678c2ecf20Sopenharmony_ci struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); 4688c2ecf20Sopenharmony_ci struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 4698c2ecf20Sopenharmony_ci struct cryptd_queue *queue = 4708c2ecf20Sopenharmony_ci cryptd_get_queue(crypto_ahash_tfm(tfm)); 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci rctx->complete = req->base.complete; 4738c2ecf20Sopenharmony_ci req->base.complete = compl; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci return cryptd_enqueue_request(queue, &req->base); 4768c2ecf20Sopenharmony_ci} 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_cistatic void cryptd_hash_complete(struct ahash_request *req, int err) 4798c2ecf20Sopenharmony_ci{ 4808c2ecf20Sopenharmony_ci struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 4818c2ecf20Sopenharmony_ci struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm); 4828c2ecf20Sopenharmony_ci struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); 4838c2ecf20Sopenharmony_ci int refcnt = refcount_read(&ctx->refcnt); 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci local_bh_disable(); 4868c2ecf20Sopenharmony_ci rctx->complete(&req->base, err); 4878c2ecf20Sopenharmony_ci local_bh_enable(); 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt)) 4908c2ecf20Sopenharmony_ci crypto_free_ahash(tfm); 4918c2ecf20Sopenharmony_ci} 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_cistatic void cryptd_hash_init(struct crypto_async_request *req_async, int err) 4948c2ecf20Sopenharmony_ci{ 4958c2ecf20Sopenharmony_ci struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm); 4968c2ecf20Sopenharmony_ci struct crypto_shash *child = ctx->child; 4978c2ecf20Sopenharmony_ci struct ahash_request *req = ahash_request_cast(req_async); 4988c2ecf20Sopenharmony_ci struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); 4998c2ecf20Sopenharmony_ci struct shash_desc *desc = &rctx->desc; 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci if (unlikely(err == -EINPROGRESS)) 5028c2ecf20Sopenharmony_ci goto out; 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci desc->tfm = child; 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci err = crypto_shash_init(desc); 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci req->base.complete = rctx->complete; 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ciout: 5118c2ecf20Sopenharmony_ci cryptd_hash_complete(req, err); 5128c2ecf20Sopenharmony_ci} 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_cistatic int cryptd_hash_init_enqueue(struct ahash_request *req) 5158c2ecf20Sopenharmony_ci{ 5168c2ecf20Sopenharmony_ci return cryptd_hash_enqueue(req, cryptd_hash_init); 5178c2ecf20Sopenharmony_ci} 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_cistatic void cryptd_hash_update(struct crypto_async_request *req_async, int err) 5208c2ecf20Sopenharmony_ci{ 5218c2ecf20Sopenharmony_ci struct ahash_request *req = ahash_request_cast(req_async); 5228c2ecf20Sopenharmony_ci struct cryptd_hash_request_ctx *rctx; 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci rctx = ahash_request_ctx(req); 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci if (unlikely(err == -EINPROGRESS)) 5278c2ecf20Sopenharmony_ci goto out; 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci err = shash_ahash_update(req, &rctx->desc); 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_ci req->base.complete = rctx->complete; 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ciout: 5348c2ecf20Sopenharmony_ci cryptd_hash_complete(req, err); 5358c2ecf20Sopenharmony_ci} 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_cistatic int cryptd_hash_update_enqueue(struct ahash_request *req) 5388c2ecf20Sopenharmony_ci{ 5398c2ecf20Sopenharmony_ci return cryptd_hash_enqueue(req, cryptd_hash_update); 5408c2ecf20Sopenharmony_ci} 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_cistatic void cryptd_hash_final(struct crypto_async_request *req_async, int err) 5438c2ecf20Sopenharmony_ci{ 5448c2ecf20Sopenharmony_ci struct ahash_request *req = ahash_request_cast(req_async); 5458c2ecf20Sopenharmony_ci struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci if (unlikely(err == -EINPROGRESS)) 5488c2ecf20Sopenharmony_ci goto out; 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci err = crypto_shash_final(&rctx->desc, req->result); 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ci req->base.complete = rctx->complete; 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_ciout: 5558c2ecf20Sopenharmony_ci cryptd_hash_complete(req, err); 5568c2ecf20Sopenharmony_ci} 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_cistatic int cryptd_hash_final_enqueue(struct ahash_request *req) 5598c2ecf20Sopenharmony_ci{ 5608c2ecf20Sopenharmony_ci return cryptd_hash_enqueue(req, cryptd_hash_final); 5618c2ecf20Sopenharmony_ci} 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_cistatic void cryptd_hash_finup(struct crypto_async_request *req_async, int err) 5648c2ecf20Sopenharmony_ci{ 5658c2ecf20Sopenharmony_ci struct ahash_request *req = ahash_request_cast(req_async); 5668c2ecf20Sopenharmony_ci struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_ci if (unlikely(err == -EINPROGRESS)) 5698c2ecf20Sopenharmony_ci goto out; 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci err = shash_ahash_finup(req, &rctx->desc); 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci req->base.complete = rctx->complete; 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ciout: 5768c2ecf20Sopenharmony_ci cryptd_hash_complete(req, err); 5778c2ecf20Sopenharmony_ci} 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_cistatic int cryptd_hash_finup_enqueue(struct ahash_request *req) 5808c2ecf20Sopenharmony_ci{ 5818c2ecf20Sopenharmony_ci return cryptd_hash_enqueue(req, cryptd_hash_finup); 5828c2ecf20Sopenharmony_ci} 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_cistatic void cryptd_hash_digest(struct crypto_async_request *req_async, int err) 5858c2ecf20Sopenharmony_ci{ 5868c2ecf20Sopenharmony_ci struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm); 5878c2ecf20Sopenharmony_ci struct crypto_shash *child = ctx->child; 5888c2ecf20Sopenharmony_ci struct ahash_request *req = ahash_request_cast(req_async); 5898c2ecf20Sopenharmony_ci struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); 5908c2ecf20Sopenharmony_ci struct shash_desc *desc = &rctx->desc; 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci if (unlikely(err == -EINPROGRESS)) 5938c2ecf20Sopenharmony_ci goto out; 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci desc->tfm = child; 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci err = shash_ahash_digest(req, desc); 5988c2ecf20Sopenharmony_ci 5998c2ecf20Sopenharmony_ci req->base.complete = rctx->complete; 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_ciout: 6028c2ecf20Sopenharmony_ci cryptd_hash_complete(req, err); 6038c2ecf20Sopenharmony_ci} 6048c2ecf20Sopenharmony_ci 6058c2ecf20Sopenharmony_cistatic int cryptd_hash_digest_enqueue(struct ahash_request *req) 6068c2ecf20Sopenharmony_ci{ 6078c2ecf20Sopenharmony_ci return cryptd_hash_enqueue(req, cryptd_hash_digest); 6088c2ecf20Sopenharmony_ci} 6098c2ecf20Sopenharmony_ci 6108c2ecf20Sopenharmony_cistatic int cryptd_hash_export(struct ahash_request *req, void *out) 6118c2ecf20Sopenharmony_ci{ 6128c2ecf20Sopenharmony_ci struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); 6138c2ecf20Sopenharmony_ci 6148c2ecf20Sopenharmony_ci return crypto_shash_export(&rctx->desc, out); 6158c2ecf20Sopenharmony_ci} 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_cistatic int cryptd_hash_import(struct ahash_request *req, const void *in) 6188c2ecf20Sopenharmony_ci{ 6198c2ecf20Sopenharmony_ci struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 6208c2ecf20Sopenharmony_ci struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm); 6218c2ecf20Sopenharmony_ci struct shash_desc *desc = cryptd_shash_desc(req); 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci desc->tfm = ctx->child; 6248c2ecf20Sopenharmony_ci 6258c2ecf20Sopenharmony_ci return crypto_shash_import(desc, in); 6268c2ecf20Sopenharmony_ci} 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_cistatic void cryptd_hash_free(struct ahash_instance *inst) 6298c2ecf20Sopenharmony_ci{ 6308c2ecf20Sopenharmony_ci struct hashd_instance_ctx *ctx = ahash_instance_ctx(inst); 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_ci crypto_drop_shash(&ctx->spawn); 6338c2ecf20Sopenharmony_ci kfree(inst); 6348c2ecf20Sopenharmony_ci} 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_cistatic int cryptd_create_hash(struct crypto_template *tmpl, struct rtattr **tb, 6378c2ecf20Sopenharmony_ci struct crypto_attr_type *algt, 6388c2ecf20Sopenharmony_ci struct cryptd_queue *queue) 6398c2ecf20Sopenharmony_ci{ 6408c2ecf20Sopenharmony_ci struct hashd_instance_ctx *ctx; 6418c2ecf20Sopenharmony_ci struct ahash_instance *inst; 6428c2ecf20Sopenharmony_ci struct shash_alg *alg; 6438c2ecf20Sopenharmony_ci u32 type; 6448c2ecf20Sopenharmony_ci u32 mask; 6458c2ecf20Sopenharmony_ci int err; 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci cryptd_type_and_mask(algt, &type, &mask); 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); 6508c2ecf20Sopenharmony_ci if (!inst) 6518c2ecf20Sopenharmony_ci return -ENOMEM; 6528c2ecf20Sopenharmony_ci 6538c2ecf20Sopenharmony_ci ctx = ahash_instance_ctx(inst); 6548c2ecf20Sopenharmony_ci ctx->queue = queue; 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_ci err = crypto_grab_shash(&ctx->spawn, ahash_crypto_instance(inst), 6578c2ecf20Sopenharmony_ci crypto_attr_alg_name(tb[1]), type, mask); 6588c2ecf20Sopenharmony_ci if (err) 6598c2ecf20Sopenharmony_ci goto err_free_inst; 6608c2ecf20Sopenharmony_ci alg = crypto_spawn_shash_alg(&ctx->spawn); 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_ci err = cryptd_init_instance(ahash_crypto_instance(inst), &alg->base); 6638c2ecf20Sopenharmony_ci if (err) 6648c2ecf20Sopenharmony_ci goto err_free_inst; 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci inst->alg.halg.base.cra_flags |= CRYPTO_ALG_ASYNC | 6678c2ecf20Sopenharmony_ci (alg->base.cra_flags & (CRYPTO_ALG_INTERNAL| 6688c2ecf20Sopenharmony_ci CRYPTO_ALG_OPTIONAL_KEY)); 6698c2ecf20Sopenharmony_ci inst->alg.halg.digestsize = alg->digestsize; 6708c2ecf20Sopenharmony_ci inst->alg.halg.statesize = alg->statesize; 6718c2ecf20Sopenharmony_ci inst->alg.halg.base.cra_ctxsize = sizeof(struct cryptd_hash_ctx); 6728c2ecf20Sopenharmony_ci 6738c2ecf20Sopenharmony_ci inst->alg.halg.base.cra_init = cryptd_hash_init_tfm; 6748c2ecf20Sopenharmony_ci inst->alg.halg.base.cra_exit = cryptd_hash_exit_tfm; 6758c2ecf20Sopenharmony_ci 6768c2ecf20Sopenharmony_ci inst->alg.init = cryptd_hash_init_enqueue; 6778c2ecf20Sopenharmony_ci inst->alg.update = cryptd_hash_update_enqueue; 6788c2ecf20Sopenharmony_ci inst->alg.final = cryptd_hash_final_enqueue; 6798c2ecf20Sopenharmony_ci inst->alg.finup = cryptd_hash_finup_enqueue; 6808c2ecf20Sopenharmony_ci inst->alg.export = cryptd_hash_export; 6818c2ecf20Sopenharmony_ci inst->alg.import = cryptd_hash_import; 6828c2ecf20Sopenharmony_ci if (crypto_shash_alg_has_setkey(alg)) 6838c2ecf20Sopenharmony_ci inst->alg.setkey = cryptd_hash_setkey; 6848c2ecf20Sopenharmony_ci inst->alg.digest = cryptd_hash_digest_enqueue; 6858c2ecf20Sopenharmony_ci 6868c2ecf20Sopenharmony_ci inst->free = cryptd_hash_free; 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_ci err = ahash_register_instance(tmpl, inst); 6898c2ecf20Sopenharmony_ci if (err) { 6908c2ecf20Sopenharmony_cierr_free_inst: 6918c2ecf20Sopenharmony_ci cryptd_hash_free(inst); 6928c2ecf20Sopenharmony_ci } 6938c2ecf20Sopenharmony_ci return err; 6948c2ecf20Sopenharmony_ci} 6958c2ecf20Sopenharmony_ci 6968c2ecf20Sopenharmony_cistatic int cryptd_aead_setkey(struct crypto_aead *parent, 6978c2ecf20Sopenharmony_ci const u8 *key, unsigned int keylen) 6988c2ecf20Sopenharmony_ci{ 6998c2ecf20Sopenharmony_ci struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent); 7008c2ecf20Sopenharmony_ci struct crypto_aead *child = ctx->child; 7018c2ecf20Sopenharmony_ci 7028c2ecf20Sopenharmony_ci return crypto_aead_setkey(child, key, keylen); 7038c2ecf20Sopenharmony_ci} 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_cistatic int cryptd_aead_setauthsize(struct crypto_aead *parent, 7068c2ecf20Sopenharmony_ci unsigned int authsize) 7078c2ecf20Sopenharmony_ci{ 7088c2ecf20Sopenharmony_ci struct cryptd_aead_ctx *ctx = crypto_aead_ctx(parent); 7098c2ecf20Sopenharmony_ci struct crypto_aead *child = ctx->child; 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_ci return crypto_aead_setauthsize(child, authsize); 7128c2ecf20Sopenharmony_ci} 7138c2ecf20Sopenharmony_ci 7148c2ecf20Sopenharmony_cistatic void cryptd_aead_crypt(struct aead_request *req, 7158c2ecf20Sopenharmony_ci struct crypto_aead *child, 7168c2ecf20Sopenharmony_ci int err, 7178c2ecf20Sopenharmony_ci int (*crypt)(struct aead_request *req)) 7188c2ecf20Sopenharmony_ci{ 7198c2ecf20Sopenharmony_ci struct cryptd_aead_request_ctx *rctx; 7208c2ecf20Sopenharmony_ci struct cryptd_aead_ctx *ctx; 7218c2ecf20Sopenharmony_ci crypto_completion_t compl; 7228c2ecf20Sopenharmony_ci struct crypto_aead *tfm; 7238c2ecf20Sopenharmony_ci int refcnt; 7248c2ecf20Sopenharmony_ci 7258c2ecf20Sopenharmony_ci rctx = aead_request_ctx(req); 7268c2ecf20Sopenharmony_ci compl = rctx->complete; 7278c2ecf20Sopenharmony_ci 7288c2ecf20Sopenharmony_ci tfm = crypto_aead_reqtfm(req); 7298c2ecf20Sopenharmony_ci 7308c2ecf20Sopenharmony_ci if (unlikely(err == -EINPROGRESS)) 7318c2ecf20Sopenharmony_ci goto out; 7328c2ecf20Sopenharmony_ci aead_request_set_tfm(req, child); 7338c2ecf20Sopenharmony_ci err = crypt( req ); 7348c2ecf20Sopenharmony_ci 7358c2ecf20Sopenharmony_ciout: 7368c2ecf20Sopenharmony_ci ctx = crypto_aead_ctx(tfm); 7378c2ecf20Sopenharmony_ci refcnt = refcount_read(&ctx->refcnt); 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_ci local_bh_disable(); 7408c2ecf20Sopenharmony_ci compl(&req->base, err); 7418c2ecf20Sopenharmony_ci local_bh_enable(); 7428c2ecf20Sopenharmony_ci 7438c2ecf20Sopenharmony_ci if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt)) 7448c2ecf20Sopenharmony_ci crypto_free_aead(tfm); 7458c2ecf20Sopenharmony_ci} 7468c2ecf20Sopenharmony_ci 7478c2ecf20Sopenharmony_cistatic void cryptd_aead_encrypt(struct crypto_async_request *areq, int err) 7488c2ecf20Sopenharmony_ci{ 7498c2ecf20Sopenharmony_ci struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm); 7508c2ecf20Sopenharmony_ci struct crypto_aead *child = ctx->child; 7518c2ecf20Sopenharmony_ci struct aead_request *req; 7528c2ecf20Sopenharmony_ci 7538c2ecf20Sopenharmony_ci req = container_of(areq, struct aead_request, base); 7548c2ecf20Sopenharmony_ci cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->encrypt); 7558c2ecf20Sopenharmony_ci} 7568c2ecf20Sopenharmony_ci 7578c2ecf20Sopenharmony_cistatic void cryptd_aead_decrypt(struct crypto_async_request *areq, int err) 7588c2ecf20Sopenharmony_ci{ 7598c2ecf20Sopenharmony_ci struct cryptd_aead_ctx *ctx = crypto_tfm_ctx(areq->tfm); 7608c2ecf20Sopenharmony_ci struct crypto_aead *child = ctx->child; 7618c2ecf20Sopenharmony_ci struct aead_request *req; 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_ci req = container_of(areq, struct aead_request, base); 7648c2ecf20Sopenharmony_ci cryptd_aead_crypt(req, child, err, crypto_aead_alg(child)->decrypt); 7658c2ecf20Sopenharmony_ci} 7668c2ecf20Sopenharmony_ci 7678c2ecf20Sopenharmony_cistatic int cryptd_aead_enqueue(struct aead_request *req, 7688c2ecf20Sopenharmony_ci crypto_completion_t compl) 7698c2ecf20Sopenharmony_ci{ 7708c2ecf20Sopenharmony_ci struct cryptd_aead_request_ctx *rctx = aead_request_ctx(req); 7718c2ecf20Sopenharmony_ci struct crypto_aead *tfm = crypto_aead_reqtfm(req); 7728c2ecf20Sopenharmony_ci struct cryptd_queue *queue = cryptd_get_queue(crypto_aead_tfm(tfm)); 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_ci rctx->complete = req->base.complete; 7758c2ecf20Sopenharmony_ci req->base.complete = compl; 7768c2ecf20Sopenharmony_ci return cryptd_enqueue_request(queue, &req->base); 7778c2ecf20Sopenharmony_ci} 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_cistatic int cryptd_aead_encrypt_enqueue(struct aead_request *req) 7808c2ecf20Sopenharmony_ci{ 7818c2ecf20Sopenharmony_ci return cryptd_aead_enqueue(req, cryptd_aead_encrypt ); 7828c2ecf20Sopenharmony_ci} 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_cistatic int cryptd_aead_decrypt_enqueue(struct aead_request *req) 7858c2ecf20Sopenharmony_ci{ 7868c2ecf20Sopenharmony_ci return cryptd_aead_enqueue(req, cryptd_aead_decrypt ); 7878c2ecf20Sopenharmony_ci} 7888c2ecf20Sopenharmony_ci 7898c2ecf20Sopenharmony_cistatic int cryptd_aead_init_tfm(struct crypto_aead *tfm) 7908c2ecf20Sopenharmony_ci{ 7918c2ecf20Sopenharmony_ci struct aead_instance *inst = aead_alg_instance(tfm); 7928c2ecf20Sopenharmony_ci struct aead_instance_ctx *ictx = aead_instance_ctx(inst); 7938c2ecf20Sopenharmony_ci struct crypto_aead_spawn *spawn = &ictx->aead_spawn; 7948c2ecf20Sopenharmony_ci struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm); 7958c2ecf20Sopenharmony_ci struct crypto_aead *cipher; 7968c2ecf20Sopenharmony_ci 7978c2ecf20Sopenharmony_ci cipher = crypto_spawn_aead(spawn); 7988c2ecf20Sopenharmony_ci if (IS_ERR(cipher)) 7998c2ecf20Sopenharmony_ci return PTR_ERR(cipher); 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_ci ctx->child = cipher; 8028c2ecf20Sopenharmony_ci crypto_aead_set_reqsize( 8038c2ecf20Sopenharmony_ci tfm, max((unsigned)sizeof(struct cryptd_aead_request_ctx), 8048c2ecf20Sopenharmony_ci crypto_aead_reqsize(cipher))); 8058c2ecf20Sopenharmony_ci return 0; 8068c2ecf20Sopenharmony_ci} 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_cistatic void cryptd_aead_exit_tfm(struct crypto_aead *tfm) 8098c2ecf20Sopenharmony_ci{ 8108c2ecf20Sopenharmony_ci struct cryptd_aead_ctx *ctx = crypto_aead_ctx(tfm); 8118c2ecf20Sopenharmony_ci crypto_free_aead(ctx->child); 8128c2ecf20Sopenharmony_ci} 8138c2ecf20Sopenharmony_ci 8148c2ecf20Sopenharmony_cistatic void cryptd_aead_free(struct aead_instance *inst) 8158c2ecf20Sopenharmony_ci{ 8168c2ecf20Sopenharmony_ci struct aead_instance_ctx *ctx = aead_instance_ctx(inst); 8178c2ecf20Sopenharmony_ci 8188c2ecf20Sopenharmony_ci crypto_drop_aead(&ctx->aead_spawn); 8198c2ecf20Sopenharmony_ci kfree(inst); 8208c2ecf20Sopenharmony_ci} 8218c2ecf20Sopenharmony_ci 8228c2ecf20Sopenharmony_cistatic int cryptd_create_aead(struct crypto_template *tmpl, 8238c2ecf20Sopenharmony_ci struct rtattr **tb, 8248c2ecf20Sopenharmony_ci struct crypto_attr_type *algt, 8258c2ecf20Sopenharmony_ci struct cryptd_queue *queue) 8268c2ecf20Sopenharmony_ci{ 8278c2ecf20Sopenharmony_ci struct aead_instance_ctx *ctx; 8288c2ecf20Sopenharmony_ci struct aead_instance *inst; 8298c2ecf20Sopenharmony_ci struct aead_alg *alg; 8308c2ecf20Sopenharmony_ci u32 type; 8318c2ecf20Sopenharmony_ci u32 mask; 8328c2ecf20Sopenharmony_ci int err; 8338c2ecf20Sopenharmony_ci 8348c2ecf20Sopenharmony_ci cryptd_type_and_mask(algt, &type, &mask); 8358c2ecf20Sopenharmony_ci 8368c2ecf20Sopenharmony_ci inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); 8378c2ecf20Sopenharmony_ci if (!inst) 8388c2ecf20Sopenharmony_ci return -ENOMEM; 8398c2ecf20Sopenharmony_ci 8408c2ecf20Sopenharmony_ci ctx = aead_instance_ctx(inst); 8418c2ecf20Sopenharmony_ci ctx->queue = queue; 8428c2ecf20Sopenharmony_ci 8438c2ecf20Sopenharmony_ci err = crypto_grab_aead(&ctx->aead_spawn, aead_crypto_instance(inst), 8448c2ecf20Sopenharmony_ci crypto_attr_alg_name(tb[1]), type, mask); 8458c2ecf20Sopenharmony_ci if (err) 8468c2ecf20Sopenharmony_ci goto err_free_inst; 8478c2ecf20Sopenharmony_ci 8488c2ecf20Sopenharmony_ci alg = crypto_spawn_aead_alg(&ctx->aead_spawn); 8498c2ecf20Sopenharmony_ci err = cryptd_init_instance(aead_crypto_instance(inst), &alg->base); 8508c2ecf20Sopenharmony_ci if (err) 8518c2ecf20Sopenharmony_ci goto err_free_inst; 8528c2ecf20Sopenharmony_ci 8538c2ecf20Sopenharmony_ci inst->alg.base.cra_flags |= CRYPTO_ALG_ASYNC | 8548c2ecf20Sopenharmony_ci (alg->base.cra_flags & CRYPTO_ALG_INTERNAL); 8558c2ecf20Sopenharmony_ci inst->alg.base.cra_ctxsize = sizeof(struct cryptd_aead_ctx); 8568c2ecf20Sopenharmony_ci 8578c2ecf20Sopenharmony_ci inst->alg.ivsize = crypto_aead_alg_ivsize(alg); 8588c2ecf20Sopenharmony_ci inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg); 8598c2ecf20Sopenharmony_ci 8608c2ecf20Sopenharmony_ci inst->alg.init = cryptd_aead_init_tfm; 8618c2ecf20Sopenharmony_ci inst->alg.exit = cryptd_aead_exit_tfm; 8628c2ecf20Sopenharmony_ci inst->alg.setkey = cryptd_aead_setkey; 8638c2ecf20Sopenharmony_ci inst->alg.setauthsize = cryptd_aead_setauthsize; 8648c2ecf20Sopenharmony_ci inst->alg.encrypt = cryptd_aead_encrypt_enqueue; 8658c2ecf20Sopenharmony_ci inst->alg.decrypt = cryptd_aead_decrypt_enqueue; 8668c2ecf20Sopenharmony_ci 8678c2ecf20Sopenharmony_ci inst->free = cryptd_aead_free; 8688c2ecf20Sopenharmony_ci 8698c2ecf20Sopenharmony_ci err = aead_register_instance(tmpl, inst); 8708c2ecf20Sopenharmony_ci if (err) { 8718c2ecf20Sopenharmony_cierr_free_inst: 8728c2ecf20Sopenharmony_ci cryptd_aead_free(inst); 8738c2ecf20Sopenharmony_ci } 8748c2ecf20Sopenharmony_ci return err; 8758c2ecf20Sopenharmony_ci} 8768c2ecf20Sopenharmony_ci 8778c2ecf20Sopenharmony_cistatic struct cryptd_queue queue; 8788c2ecf20Sopenharmony_ci 8798c2ecf20Sopenharmony_cistatic int cryptd_create(struct crypto_template *tmpl, struct rtattr **tb) 8808c2ecf20Sopenharmony_ci{ 8818c2ecf20Sopenharmony_ci struct crypto_attr_type *algt; 8828c2ecf20Sopenharmony_ci 8838c2ecf20Sopenharmony_ci algt = crypto_get_attr_type(tb); 8848c2ecf20Sopenharmony_ci if (IS_ERR(algt)) 8858c2ecf20Sopenharmony_ci return PTR_ERR(algt); 8868c2ecf20Sopenharmony_ci 8878c2ecf20Sopenharmony_ci switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) { 8888c2ecf20Sopenharmony_ci case CRYPTO_ALG_TYPE_SKCIPHER: 8898c2ecf20Sopenharmony_ci return cryptd_create_skcipher(tmpl, tb, algt, &queue); 8908c2ecf20Sopenharmony_ci case CRYPTO_ALG_TYPE_HASH: 8918c2ecf20Sopenharmony_ci return cryptd_create_hash(tmpl, tb, algt, &queue); 8928c2ecf20Sopenharmony_ci case CRYPTO_ALG_TYPE_AEAD: 8938c2ecf20Sopenharmony_ci return cryptd_create_aead(tmpl, tb, algt, &queue); 8948c2ecf20Sopenharmony_ci } 8958c2ecf20Sopenharmony_ci 8968c2ecf20Sopenharmony_ci return -EINVAL; 8978c2ecf20Sopenharmony_ci} 8988c2ecf20Sopenharmony_ci 8998c2ecf20Sopenharmony_cistatic struct crypto_template cryptd_tmpl = { 9008c2ecf20Sopenharmony_ci .name = "cryptd", 9018c2ecf20Sopenharmony_ci .create = cryptd_create, 9028c2ecf20Sopenharmony_ci .module = THIS_MODULE, 9038c2ecf20Sopenharmony_ci}; 9048c2ecf20Sopenharmony_ci 9058c2ecf20Sopenharmony_cistruct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name, 9068c2ecf20Sopenharmony_ci u32 type, u32 mask) 9078c2ecf20Sopenharmony_ci{ 9088c2ecf20Sopenharmony_ci char cryptd_alg_name[CRYPTO_MAX_ALG_NAME]; 9098c2ecf20Sopenharmony_ci struct cryptd_skcipher_ctx *ctx; 9108c2ecf20Sopenharmony_ci struct crypto_skcipher *tfm; 9118c2ecf20Sopenharmony_ci 9128c2ecf20Sopenharmony_ci if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME, 9138c2ecf20Sopenharmony_ci "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME) 9148c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 9158c2ecf20Sopenharmony_ci 9168c2ecf20Sopenharmony_ci tfm = crypto_alloc_skcipher(cryptd_alg_name, type, mask); 9178c2ecf20Sopenharmony_ci if (IS_ERR(tfm)) 9188c2ecf20Sopenharmony_ci return ERR_CAST(tfm); 9198c2ecf20Sopenharmony_ci 9208c2ecf20Sopenharmony_ci if (tfm->base.__crt_alg->cra_module != THIS_MODULE) { 9218c2ecf20Sopenharmony_ci crypto_free_skcipher(tfm); 9228c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 9238c2ecf20Sopenharmony_ci } 9248c2ecf20Sopenharmony_ci 9258c2ecf20Sopenharmony_ci ctx = crypto_skcipher_ctx(tfm); 9268c2ecf20Sopenharmony_ci refcount_set(&ctx->refcnt, 1); 9278c2ecf20Sopenharmony_ci 9288c2ecf20Sopenharmony_ci return container_of(tfm, struct cryptd_skcipher, base); 9298c2ecf20Sopenharmony_ci} 9308c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cryptd_alloc_skcipher); 9318c2ecf20Sopenharmony_ci 9328c2ecf20Sopenharmony_cistruct crypto_skcipher *cryptd_skcipher_child(struct cryptd_skcipher *tfm) 9338c2ecf20Sopenharmony_ci{ 9348c2ecf20Sopenharmony_ci struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base); 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci return ctx->child; 9378c2ecf20Sopenharmony_ci} 9388c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cryptd_skcipher_child); 9398c2ecf20Sopenharmony_ci 9408c2ecf20Sopenharmony_cibool cryptd_skcipher_queued(struct cryptd_skcipher *tfm) 9418c2ecf20Sopenharmony_ci{ 9428c2ecf20Sopenharmony_ci struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base); 9438c2ecf20Sopenharmony_ci 9448c2ecf20Sopenharmony_ci return refcount_read(&ctx->refcnt) - 1; 9458c2ecf20Sopenharmony_ci} 9468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cryptd_skcipher_queued); 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_civoid cryptd_free_skcipher(struct cryptd_skcipher *tfm) 9498c2ecf20Sopenharmony_ci{ 9508c2ecf20Sopenharmony_ci struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base); 9518c2ecf20Sopenharmony_ci 9528c2ecf20Sopenharmony_ci if (refcount_dec_and_test(&ctx->refcnt)) 9538c2ecf20Sopenharmony_ci crypto_free_skcipher(&tfm->base); 9548c2ecf20Sopenharmony_ci} 9558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cryptd_free_skcipher); 9568c2ecf20Sopenharmony_ci 9578c2ecf20Sopenharmony_cistruct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name, 9588c2ecf20Sopenharmony_ci u32 type, u32 mask) 9598c2ecf20Sopenharmony_ci{ 9608c2ecf20Sopenharmony_ci char cryptd_alg_name[CRYPTO_MAX_ALG_NAME]; 9618c2ecf20Sopenharmony_ci struct cryptd_hash_ctx *ctx; 9628c2ecf20Sopenharmony_ci struct crypto_ahash *tfm; 9638c2ecf20Sopenharmony_ci 9648c2ecf20Sopenharmony_ci if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME, 9658c2ecf20Sopenharmony_ci "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME) 9668c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 9678c2ecf20Sopenharmony_ci tfm = crypto_alloc_ahash(cryptd_alg_name, type, mask); 9688c2ecf20Sopenharmony_ci if (IS_ERR(tfm)) 9698c2ecf20Sopenharmony_ci return ERR_CAST(tfm); 9708c2ecf20Sopenharmony_ci if (tfm->base.__crt_alg->cra_module != THIS_MODULE) { 9718c2ecf20Sopenharmony_ci crypto_free_ahash(tfm); 9728c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 9738c2ecf20Sopenharmony_ci } 9748c2ecf20Sopenharmony_ci 9758c2ecf20Sopenharmony_ci ctx = crypto_ahash_ctx(tfm); 9768c2ecf20Sopenharmony_ci refcount_set(&ctx->refcnt, 1); 9778c2ecf20Sopenharmony_ci 9788c2ecf20Sopenharmony_ci return __cryptd_ahash_cast(tfm); 9798c2ecf20Sopenharmony_ci} 9808c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cryptd_alloc_ahash); 9818c2ecf20Sopenharmony_ci 9828c2ecf20Sopenharmony_cistruct crypto_shash *cryptd_ahash_child(struct cryptd_ahash *tfm) 9838c2ecf20Sopenharmony_ci{ 9848c2ecf20Sopenharmony_ci struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base); 9858c2ecf20Sopenharmony_ci 9868c2ecf20Sopenharmony_ci return ctx->child; 9878c2ecf20Sopenharmony_ci} 9888c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cryptd_ahash_child); 9898c2ecf20Sopenharmony_ci 9908c2ecf20Sopenharmony_cistruct shash_desc *cryptd_shash_desc(struct ahash_request *req) 9918c2ecf20Sopenharmony_ci{ 9928c2ecf20Sopenharmony_ci struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req); 9938c2ecf20Sopenharmony_ci return &rctx->desc; 9948c2ecf20Sopenharmony_ci} 9958c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cryptd_shash_desc); 9968c2ecf20Sopenharmony_ci 9978c2ecf20Sopenharmony_cibool cryptd_ahash_queued(struct cryptd_ahash *tfm) 9988c2ecf20Sopenharmony_ci{ 9998c2ecf20Sopenharmony_ci struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base); 10008c2ecf20Sopenharmony_ci 10018c2ecf20Sopenharmony_ci return refcount_read(&ctx->refcnt) - 1; 10028c2ecf20Sopenharmony_ci} 10038c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cryptd_ahash_queued); 10048c2ecf20Sopenharmony_ci 10058c2ecf20Sopenharmony_civoid cryptd_free_ahash(struct cryptd_ahash *tfm) 10068c2ecf20Sopenharmony_ci{ 10078c2ecf20Sopenharmony_ci struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base); 10088c2ecf20Sopenharmony_ci 10098c2ecf20Sopenharmony_ci if (refcount_dec_and_test(&ctx->refcnt)) 10108c2ecf20Sopenharmony_ci crypto_free_ahash(&tfm->base); 10118c2ecf20Sopenharmony_ci} 10128c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cryptd_free_ahash); 10138c2ecf20Sopenharmony_ci 10148c2ecf20Sopenharmony_cistruct cryptd_aead *cryptd_alloc_aead(const char *alg_name, 10158c2ecf20Sopenharmony_ci u32 type, u32 mask) 10168c2ecf20Sopenharmony_ci{ 10178c2ecf20Sopenharmony_ci char cryptd_alg_name[CRYPTO_MAX_ALG_NAME]; 10188c2ecf20Sopenharmony_ci struct cryptd_aead_ctx *ctx; 10198c2ecf20Sopenharmony_ci struct crypto_aead *tfm; 10208c2ecf20Sopenharmony_ci 10218c2ecf20Sopenharmony_ci if (snprintf(cryptd_alg_name, CRYPTO_MAX_ALG_NAME, 10228c2ecf20Sopenharmony_ci "cryptd(%s)", alg_name) >= CRYPTO_MAX_ALG_NAME) 10238c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 10248c2ecf20Sopenharmony_ci tfm = crypto_alloc_aead(cryptd_alg_name, type, mask); 10258c2ecf20Sopenharmony_ci if (IS_ERR(tfm)) 10268c2ecf20Sopenharmony_ci return ERR_CAST(tfm); 10278c2ecf20Sopenharmony_ci if (tfm->base.__crt_alg->cra_module != THIS_MODULE) { 10288c2ecf20Sopenharmony_ci crypto_free_aead(tfm); 10298c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 10308c2ecf20Sopenharmony_ci } 10318c2ecf20Sopenharmony_ci 10328c2ecf20Sopenharmony_ci ctx = crypto_aead_ctx(tfm); 10338c2ecf20Sopenharmony_ci refcount_set(&ctx->refcnt, 1); 10348c2ecf20Sopenharmony_ci 10358c2ecf20Sopenharmony_ci return __cryptd_aead_cast(tfm); 10368c2ecf20Sopenharmony_ci} 10378c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cryptd_alloc_aead); 10388c2ecf20Sopenharmony_ci 10398c2ecf20Sopenharmony_cistruct crypto_aead *cryptd_aead_child(struct cryptd_aead *tfm) 10408c2ecf20Sopenharmony_ci{ 10418c2ecf20Sopenharmony_ci struct cryptd_aead_ctx *ctx; 10428c2ecf20Sopenharmony_ci ctx = crypto_aead_ctx(&tfm->base); 10438c2ecf20Sopenharmony_ci return ctx->child; 10448c2ecf20Sopenharmony_ci} 10458c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cryptd_aead_child); 10468c2ecf20Sopenharmony_ci 10478c2ecf20Sopenharmony_cibool cryptd_aead_queued(struct cryptd_aead *tfm) 10488c2ecf20Sopenharmony_ci{ 10498c2ecf20Sopenharmony_ci struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base); 10508c2ecf20Sopenharmony_ci 10518c2ecf20Sopenharmony_ci return refcount_read(&ctx->refcnt) - 1; 10528c2ecf20Sopenharmony_ci} 10538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cryptd_aead_queued); 10548c2ecf20Sopenharmony_ci 10558c2ecf20Sopenharmony_civoid cryptd_free_aead(struct cryptd_aead *tfm) 10568c2ecf20Sopenharmony_ci{ 10578c2ecf20Sopenharmony_ci struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base); 10588c2ecf20Sopenharmony_ci 10598c2ecf20Sopenharmony_ci if (refcount_dec_and_test(&ctx->refcnt)) 10608c2ecf20Sopenharmony_ci crypto_free_aead(&tfm->base); 10618c2ecf20Sopenharmony_ci} 10628c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cryptd_free_aead); 10638c2ecf20Sopenharmony_ci 10648c2ecf20Sopenharmony_cistatic int __init cryptd_init(void) 10658c2ecf20Sopenharmony_ci{ 10668c2ecf20Sopenharmony_ci int err; 10678c2ecf20Sopenharmony_ci 10688c2ecf20Sopenharmony_ci cryptd_wq = alloc_workqueue("cryptd", WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE, 10698c2ecf20Sopenharmony_ci 1); 10708c2ecf20Sopenharmony_ci if (!cryptd_wq) 10718c2ecf20Sopenharmony_ci return -ENOMEM; 10728c2ecf20Sopenharmony_ci 10738c2ecf20Sopenharmony_ci err = cryptd_init_queue(&queue, cryptd_max_cpu_qlen); 10748c2ecf20Sopenharmony_ci if (err) 10758c2ecf20Sopenharmony_ci goto err_destroy_wq; 10768c2ecf20Sopenharmony_ci 10778c2ecf20Sopenharmony_ci err = crypto_register_template(&cryptd_tmpl); 10788c2ecf20Sopenharmony_ci if (err) 10798c2ecf20Sopenharmony_ci goto err_fini_queue; 10808c2ecf20Sopenharmony_ci 10818c2ecf20Sopenharmony_ci return 0; 10828c2ecf20Sopenharmony_ci 10838c2ecf20Sopenharmony_cierr_fini_queue: 10848c2ecf20Sopenharmony_ci cryptd_fini_queue(&queue); 10858c2ecf20Sopenharmony_cierr_destroy_wq: 10868c2ecf20Sopenharmony_ci destroy_workqueue(cryptd_wq); 10878c2ecf20Sopenharmony_ci return err; 10888c2ecf20Sopenharmony_ci} 10898c2ecf20Sopenharmony_ci 10908c2ecf20Sopenharmony_cistatic void __exit cryptd_exit(void) 10918c2ecf20Sopenharmony_ci{ 10928c2ecf20Sopenharmony_ci destroy_workqueue(cryptd_wq); 10938c2ecf20Sopenharmony_ci cryptd_fini_queue(&queue); 10948c2ecf20Sopenharmony_ci crypto_unregister_template(&cryptd_tmpl); 10958c2ecf20Sopenharmony_ci} 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_cisubsys_initcall(cryptd_init); 10988c2ecf20Sopenharmony_cimodule_exit(cryptd_exit); 10998c2ecf20Sopenharmony_ci 11008c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 11018c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Software async crypto daemon"); 11028c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("cryptd"); 1103