crypto_ctx.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2019 Samsung Electronics Co., Ltd.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/string.h>
  7. #include <linux/err.h>
  8. #include <linux/slab.h>
  9. #include <linux/wait.h>
  10. #include <linux/sched.h>
  11. #include "glob.h"
  12. #include "crypto_ctx.h"
  13. struct crypto_ctx_list {
  14. spinlock_t ctx_lock;
  15. int avail_ctx;
  16. struct list_head idle_ctx;
  17. wait_queue_head_t ctx_wait;
  18. };
  19. static struct crypto_ctx_list ctx_list;
  20. static inline void free_aead(struct crypto_aead *aead)
  21. {
  22. if (aead)
  23. crypto_free_aead(aead);
  24. }
  25. static void free_shash(struct shash_desc *shash)
  26. {
  27. if (shash) {
  28. crypto_free_shash(shash->tfm);
  29. kfree(shash);
  30. }
  31. }
  32. static struct crypto_aead *alloc_aead(int id)
  33. {
  34. struct crypto_aead *tfm = NULL;
  35. switch (id) {
  36. case CRYPTO_AEAD_AES_GCM:
  37. tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
  38. break;
  39. case CRYPTO_AEAD_AES_CCM:
  40. tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
  41. break;
  42. default:
  43. pr_err("Does not support encrypt ahead(id : %d)\n", id);
  44. return NULL;
  45. }
  46. if (IS_ERR(tfm)) {
  47. pr_err("Failed to alloc encrypt aead : %ld\n", PTR_ERR(tfm));
  48. return NULL;
  49. }
  50. return tfm;
  51. }
  52. static struct shash_desc *alloc_shash_desc(int id)
  53. {
  54. struct crypto_shash *tfm = NULL;
  55. struct shash_desc *shash;
  56. switch (id) {
  57. case CRYPTO_SHASH_HMACMD5:
  58. tfm = crypto_alloc_shash("hmac(md5)", 0, 0);
  59. break;
  60. case CRYPTO_SHASH_HMACSHA256:
  61. tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
  62. break;
  63. case CRYPTO_SHASH_CMACAES:
  64. tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
  65. break;
  66. case CRYPTO_SHASH_SHA256:
  67. tfm = crypto_alloc_shash("sha256", 0, 0);
  68. break;
  69. case CRYPTO_SHASH_SHA512:
  70. tfm = crypto_alloc_shash("sha512", 0, 0);
  71. break;
  72. default:
  73. return NULL;
  74. }
  75. if (IS_ERR(tfm))
  76. return NULL;
  77. shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
  78. KSMBD_DEFAULT_GFP);
  79. if (!shash)
  80. crypto_free_shash(tfm);
  81. else
  82. shash->tfm = tfm;
  83. return shash;
  84. }
  85. static void ctx_free(struct ksmbd_crypto_ctx *ctx)
  86. {
  87. int i;
  88. for (i = 0; i < CRYPTO_SHASH_MAX; i++)
  89. free_shash(ctx->desc[i]);
  90. for (i = 0; i < CRYPTO_AEAD_MAX; i++)
  91. free_aead(ctx->ccmaes[i]);
  92. kfree(ctx);
  93. }
  94. static struct ksmbd_crypto_ctx *ksmbd_find_crypto_ctx(void)
  95. {
  96. struct ksmbd_crypto_ctx *ctx;
  97. while (1) {
  98. spin_lock(&ctx_list.ctx_lock);
  99. if (!list_empty(&ctx_list.idle_ctx)) {
  100. ctx = list_entry(ctx_list.idle_ctx.next,
  101. struct ksmbd_crypto_ctx,
  102. list);
  103. list_del(&ctx->list);
  104. spin_unlock(&ctx_list.ctx_lock);
  105. return ctx;
  106. }
  107. if (ctx_list.avail_ctx > num_online_cpus()) {
  108. spin_unlock(&ctx_list.ctx_lock);
  109. wait_event(ctx_list.ctx_wait,
  110. !list_empty(&ctx_list.idle_ctx));
  111. continue;
  112. }
  113. ctx_list.avail_ctx++;
  114. spin_unlock(&ctx_list.ctx_lock);
  115. ctx = kzalloc(sizeof(struct ksmbd_crypto_ctx), KSMBD_DEFAULT_GFP);
  116. if (!ctx) {
  117. spin_lock(&ctx_list.ctx_lock);
  118. ctx_list.avail_ctx--;
  119. spin_unlock(&ctx_list.ctx_lock);
  120. wait_event(ctx_list.ctx_wait,
  121. !list_empty(&ctx_list.idle_ctx));
  122. continue;
  123. }
  124. break;
  125. }
  126. return ctx;
  127. }
  128. void ksmbd_release_crypto_ctx(struct ksmbd_crypto_ctx *ctx)
  129. {
  130. if (!ctx)
  131. return;
  132. spin_lock(&ctx_list.ctx_lock);
  133. if (ctx_list.avail_ctx <= num_online_cpus()) {
  134. list_add(&ctx->list, &ctx_list.idle_ctx);
  135. spin_unlock(&ctx_list.ctx_lock);
  136. wake_up(&ctx_list.ctx_wait);
  137. return;
  138. }
  139. ctx_list.avail_ctx--;
  140. spin_unlock(&ctx_list.ctx_lock);
  141. ctx_free(ctx);
  142. }
  143. static struct ksmbd_crypto_ctx *____crypto_shash_ctx_find(int id)
  144. {
  145. struct ksmbd_crypto_ctx *ctx;
  146. if (id >= CRYPTO_SHASH_MAX)
  147. return NULL;
  148. ctx = ksmbd_find_crypto_ctx();
  149. if (ctx->desc[id])
  150. return ctx;
  151. ctx->desc[id] = alloc_shash_desc(id);
  152. if (ctx->desc[id])
  153. return ctx;
  154. ksmbd_release_crypto_ctx(ctx);
  155. return NULL;
  156. }
  157. struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_hmacmd5(void)
  158. {
  159. return ____crypto_shash_ctx_find(CRYPTO_SHASH_HMACMD5);
  160. }
  161. struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_hmacsha256(void)
  162. {
  163. return ____crypto_shash_ctx_find(CRYPTO_SHASH_HMACSHA256);
  164. }
  165. struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_cmacaes(void)
  166. {
  167. return ____crypto_shash_ctx_find(CRYPTO_SHASH_CMACAES);
  168. }
  169. struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_sha256(void)
  170. {
  171. return ____crypto_shash_ctx_find(CRYPTO_SHASH_SHA256);
  172. }
  173. struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_sha512(void)
  174. {
  175. return ____crypto_shash_ctx_find(CRYPTO_SHASH_SHA512);
  176. }
  177. static struct ksmbd_crypto_ctx *____crypto_aead_ctx_find(int id)
  178. {
  179. struct ksmbd_crypto_ctx *ctx;
  180. if (id >= CRYPTO_AEAD_MAX)
  181. return NULL;
  182. ctx = ksmbd_find_crypto_ctx();
  183. if (ctx->ccmaes[id])
  184. return ctx;
  185. ctx->ccmaes[id] = alloc_aead(id);
  186. if (ctx->ccmaes[id])
  187. return ctx;
  188. ksmbd_release_crypto_ctx(ctx);
  189. return NULL;
  190. }
  191. struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_gcm(void)
  192. {
  193. return ____crypto_aead_ctx_find(CRYPTO_AEAD_AES_GCM);
  194. }
  195. struct ksmbd_crypto_ctx *ksmbd_crypto_ctx_find_ccm(void)
  196. {
  197. return ____crypto_aead_ctx_find(CRYPTO_AEAD_AES_CCM);
  198. }
  199. void ksmbd_crypto_destroy(void)
  200. {
  201. struct ksmbd_crypto_ctx *ctx;
  202. while (!list_empty(&ctx_list.idle_ctx)) {
  203. ctx = list_entry(ctx_list.idle_ctx.next,
  204. struct ksmbd_crypto_ctx,
  205. list);
  206. list_del(&ctx->list);
  207. ctx_free(ctx);
  208. }
  209. }
  210. int ksmbd_crypto_create(void)
  211. {
  212. struct ksmbd_crypto_ctx *ctx;
  213. spin_lock_init(&ctx_list.ctx_lock);
  214. INIT_LIST_HEAD(&ctx_list.idle_ctx);
  215. init_waitqueue_head(&ctx_list.ctx_wait);
  216. ctx_list.avail_ctx = 1;
  217. ctx = kzalloc(sizeof(struct ksmbd_crypto_ctx), KSMBD_DEFAULT_GFP);
  218. if (!ctx)
  219. return -ENOMEM;
  220. list_add(&ctx->list, &ctx_list.idle_ctx);
  221. return 0;
  222. }