1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2019 Samsung Electronics Co., Ltd.
6 #include <linux/kernel.h>
7 #include <linux/string.h>
9 #include <linux/slab.h>
10 #include <linux/wait.h>
11 #include <linux/sched.h>
14 #include "crypto_ctx.h"
16 struct crypto_ctx_list {
19 struct list_head idle_ctx;
20 wait_queue_head_t ctx_wait;
23 static struct crypto_ctx_list ctx_list;
25 static inline void free_aead(struct crypto_aead *aead)
28 crypto_free_aead(aead);
31 static void free_shash(struct shash_desc *shash)
34 crypto_free_shash(shash->tfm);
39 static struct crypto_aead *alloc_aead(int id)
41 struct crypto_aead *tfm = NULL;
44 case CRYPTO_AEAD_AES_GCM:
45 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
47 case CRYPTO_AEAD_AES_CCM:
48 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
51 pr_err("Does not support encrypt ahead(id : %d)\n", id);
56 pr_err("Failed to alloc encrypt aead : %ld\n", PTR_ERR(tfm));
63 static struct shash_desc *alloc_shash_desc(int id)
65 struct crypto_shash *tfm = NULL;
66 struct shash_desc *shash;
69 case CRYPTO_SHASH_HMACMD5:
70 tfm = crypto_alloc_shash("hmac(md5)", 0, 0);
72 case CRYPTO_SHASH_HMACSHA256:
73 tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
75 case CRYPTO_SHASH_CMACAES:
76 tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
78 case CRYPTO_SHASH_SHA256:
79 tfm = crypto_alloc_shash("sha256", 0, 0);
81 case CRYPTO_SHASH_SHA512:
82 tfm = crypto_alloc_shash("sha512", 0, 0);
91 shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
94 crypto_free_shash(tfm);
100 static void ctx_free(struct ksmbd_crypto_ctx *ctx)
104 for (i = 0; i < CRYPTO_SHASH_MAX; i++)
105 free_shash(ctx->desc[i]);
106 for (i = 0; i < CRYPTO_AEAD_MAX; i++)
107 free_aead(ctx->ccmaes[i]);
111 static struct ksmbd_crypto_ctx *ksmbd_find_crypto_ctx(void)
113 struct ksmbd_crypto_ctx *ctx;
116 spin_lock(&ctx_list.ctx_lock);
117 if (!list_empty(&ctx_list.idle_ctx)) {
118 ctx = list_entry(ctx_list.idle_ctx.next,
119 struct ksmbd_crypto_ctx,
121 list_del(&ctx->list);
122 spin_unlock(&ctx_list.ctx_lock);
126 if (ctx_list.avail_ctx > num_online_cpus()) {
127 spin_unlock(&ctx_list.ctx_lock);
128 wait_event(ctx_list.ctx_wait,
129 !list_empty(&ctx_list.idle_ctx));
133 ctx_list.avail_ctx++;
134 spin_unlock(&ctx_list.ctx_lock);
136 ctx = kzalloc(sizeof(struct ksmbd_crypto_ctx), GFP_KERNEL);
138 spin_lock(&ctx_list.ctx_lock);
139 ctx_list.avail_ctx--;
140 spin_unlock(&ctx_list.ctx_lock);
141 wait_event(ctx_list.ctx_wait,
142 !list_empty(&ctx_list.idle_ctx));
150 void ksmbd_release_crypto_ctx(struct ksmbd_crypto_ctx *ctx)
155 spin_lock(&ctx_list.ctx_lock);
156 if (ctx_list.avail_ctx <= num_online_cpus()) {
157 list_add(&ctx->list, &ctx_list.idle_ctx);
158 spin_unlock(&ctx_list.ctx_lock);
159 wake_up(&ctx_list.ctx_wait);
163 ctx_list.avail_ctx--;
164 spin_unlock(&ctx_list.ctx_lock);
168 static struct ksmbd_crypto_ctx *____crypto_shash_ctx_find(int id)
170 struct ksmbd_crypto_ctx *ctx;
172 if (id >= CRYPTO_SHASH_MAX)
175 ctx = ksmbd_find_crypto_ctx();
179 ctx->desc[id] = alloc_shash_desc(id);
182 ksmbd_release_crypto_ctx(ctx);
186 struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_hmacmd5(void)
188 return ____crypto_shash_ctx_find(CRYPTO_SHASH_HMACMD5);
191 struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_hmacsha256(void)
193 return ____crypto_shash_ctx_find(CRYPTO_SHASH_HMACSHA256);
196 struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_cmacaes(void)
198 return ____crypto_shash_ctx_find(CRYPTO_SHASH_CMACAES);
201 struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_sha256(void)
203 return ____crypto_shash_ctx_find(CRYPTO_SHASH_SHA256);
206 struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_sha512(void)
208 return ____crypto_shash_ctx_find(CRYPTO_SHASH_SHA512);
211 static struct ksmbd_crypto_ctx *____crypto_aead_ctx_find(int id)
213 struct ksmbd_crypto_ctx *ctx;
215 if (id >= CRYPTO_AEAD_MAX)
218 ctx = ksmbd_find_crypto_ctx();
222 ctx->ccmaes[id] = alloc_aead(id);
225 ksmbd_release_crypto_ctx(ctx);
229 struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_gcm(void)
231 return ____crypto_aead_ctx_find(CRYPTO_AEAD_AES_GCM);
234 struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_ccm(void)
236 return ____crypto_aead_ctx_find(CRYPTO_AEAD_AES_CCM);
239 void ksmbd_crypto_destroy(void)
241 struct ksmbd_crypto_ctx *ctx;
243 while (!list_empty(&ctx_list.idle_ctx)) {
244 ctx = list_entry(ctx_list.idle_ctx.next,
245 struct ksmbd_crypto_ctx,
247 list_del(&ctx->list);
252 int ksmbd_crypto_create(void)
254 struct ksmbd_crypto_ctx *ctx;
256 spin_lock_init(&ctx_list.ctx_lock);
257 INIT_LIST_HEAD(&ctx_list.idle_ctx);
258 init_waitqueue_head(&ctx_list.ctx_wait);
259 ctx_list.avail_ctx = 1;
261 ctx = kzalloc(sizeof(struct ksmbd_crypto_ctx), GFP_KERNEL);
264 list_add(&ctx->list, &ctx_list.idle_ctx);