zstd.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Copyright (c) 2017-present, Facebook, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/crypto.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/mm.h>
  19. #include <linux/module.h>
  20. #include <linux/net.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/zstd.h>
  23. #include <crypto/internal/scompress.h>
  24. #define ZSTD_DEF_LEVEL 3
  25. struct zstd_ctx {
  26. ZSTD_CCtx *cctx;
  27. ZSTD_DCtx *dctx;
  28. void *cwksp;
  29. void *dwksp;
  30. };
  31. static ZSTD_parameters zstd_params(void)
  32. {
  33. return ZSTD_getParams(ZSTD_DEF_LEVEL, 0, 0);
  34. }
  35. static int zstd_comp_init(struct zstd_ctx *ctx)
  36. {
  37. int ret = 0;
  38. const ZSTD_parameters params = zstd_params();
  39. const size_t wksp_size = ZSTD_CCtxWorkspaceBound(params.cParams);
  40. ctx->cwksp = vzalloc(wksp_size);
  41. if (!ctx->cwksp) {
  42. ret = -ENOMEM;
  43. goto out;
  44. }
  45. ctx->cctx = ZSTD_initCCtx(ctx->cwksp, wksp_size);
  46. if (!ctx->cctx) {
  47. ret = -EINVAL;
  48. goto out_free;
  49. }
  50. out:
  51. return ret;
  52. out_free:
  53. vfree(ctx->cwksp);
  54. goto out;
  55. }
  56. static int zstd_decomp_init(struct zstd_ctx *ctx)
  57. {
  58. int ret = 0;
  59. const size_t wksp_size = ZSTD_DCtxWorkspaceBound();
  60. ctx->dwksp = vzalloc(wksp_size);
  61. if (!ctx->dwksp) {
  62. ret = -ENOMEM;
  63. goto out;
  64. }
  65. ctx->dctx = ZSTD_initDCtx(ctx->dwksp, wksp_size);
  66. if (!ctx->dctx) {
  67. ret = -EINVAL;
  68. goto out_free;
  69. }
  70. out:
  71. return ret;
  72. out_free:
  73. vfree(ctx->dwksp);
  74. goto out;
  75. }
  76. static void zstd_comp_exit(struct zstd_ctx *ctx)
  77. {
  78. vfree(ctx->cwksp);
  79. ctx->cwksp = NULL;
  80. ctx->cctx = NULL;
  81. }
  82. static void zstd_decomp_exit(struct zstd_ctx *ctx)
  83. {
  84. vfree(ctx->dwksp);
  85. ctx->dwksp = NULL;
  86. ctx->dctx = NULL;
  87. }
  88. static int __zstd_init(void *ctx)
  89. {
  90. int ret;
  91. ret = zstd_comp_init(ctx);
  92. if (ret)
  93. return ret;
  94. ret = zstd_decomp_init(ctx);
  95. if (ret)
  96. zstd_comp_exit(ctx);
  97. return ret;
  98. }
  99. static void *zstd_alloc_ctx(struct crypto_scomp *tfm)
  100. {
  101. int ret;
  102. struct zstd_ctx *ctx;
  103. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  104. if (!ctx)
  105. return ERR_PTR(-ENOMEM);
  106. ret = __zstd_init(ctx);
  107. if (ret) {
  108. kfree(ctx);
  109. return ERR_PTR(ret);
  110. }
  111. return ctx;
  112. }
  113. static int zstd_init(struct crypto_tfm *tfm)
  114. {
  115. struct zstd_ctx *ctx = crypto_tfm_ctx(tfm);
  116. return __zstd_init(ctx);
  117. }
  118. static void __zstd_exit(void *ctx)
  119. {
  120. zstd_comp_exit(ctx);
  121. zstd_decomp_exit(ctx);
  122. }
  123. static void zstd_free_ctx(struct crypto_scomp *tfm, void *ctx)
  124. {
  125. __zstd_exit(ctx);
  126. kzfree(ctx);
  127. }
  128. static void zstd_exit(struct crypto_tfm *tfm)
  129. {
  130. struct zstd_ctx *ctx = crypto_tfm_ctx(tfm);
  131. __zstd_exit(ctx);
  132. }
  133. static int __zstd_compress(const u8 *src, unsigned int slen,
  134. u8 *dst, unsigned int *dlen, void *ctx)
  135. {
  136. size_t out_len;
  137. struct zstd_ctx *zctx = ctx;
  138. const ZSTD_parameters params = zstd_params();
  139. out_len = ZSTD_compressCCtx(zctx->cctx, dst, *dlen, src, slen, params);
  140. if (ZSTD_isError(out_len))
  141. return -EINVAL;
  142. *dlen = out_len;
  143. return 0;
  144. }
  145. static int zstd_compress(struct crypto_tfm *tfm, const u8 *src,
  146. unsigned int slen, u8 *dst, unsigned int *dlen)
  147. {
  148. struct zstd_ctx *ctx = crypto_tfm_ctx(tfm);
  149. return __zstd_compress(src, slen, dst, dlen, ctx);
  150. }
  151. static int zstd_scompress(struct crypto_scomp *tfm, const u8 *src,
  152. unsigned int slen, u8 *dst, unsigned int *dlen,
  153. void *ctx)
  154. {
  155. return __zstd_compress(src, slen, dst, dlen, ctx);
  156. }
  157. static int __zstd_decompress(const u8 *src, unsigned int slen,
  158. u8 *dst, unsigned int *dlen, void *ctx)
  159. {
  160. size_t out_len;
  161. struct zstd_ctx *zctx = ctx;
  162. out_len = ZSTD_decompressDCtx(zctx->dctx, dst, *dlen, src, slen);
  163. if (ZSTD_isError(out_len))
  164. return -EINVAL;
  165. *dlen = out_len;
  166. return 0;
  167. }
  168. static int zstd_decompress(struct crypto_tfm *tfm, const u8 *src,
  169. unsigned int slen, u8 *dst, unsigned int *dlen)
  170. {
  171. struct zstd_ctx *ctx = crypto_tfm_ctx(tfm);
  172. return __zstd_decompress(src, slen, dst, dlen, ctx);
  173. }
  174. static int zstd_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  175. unsigned int slen, u8 *dst, unsigned int *dlen,
  176. void *ctx)
  177. {
  178. return __zstd_decompress(src, slen, dst, dlen, ctx);
  179. }
  180. static struct crypto_alg alg = {
  181. .cra_name = "zstd",
  182. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  183. .cra_ctxsize = sizeof(struct zstd_ctx),
  184. .cra_module = THIS_MODULE,
  185. .cra_init = zstd_init,
  186. .cra_exit = zstd_exit,
  187. .cra_u = { .compress = {
  188. .coa_compress = zstd_compress,
  189. .coa_decompress = zstd_decompress } }
  190. };
  191. static struct scomp_alg scomp = {
  192. .alloc_ctx = zstd_alloc_ctx,
  193. .free_ctx = zstd_free_ctx,
  194. .compress = zstd_scompress,
  195. .decompress = zstd_sdecompress,
  196. .base = {
  197. .cra_name = "zstd",
  198. .cra_driver_name = "zstd-scomp",
  199. .cra_module = THIS_MODULE,
  200. }
  201. };
  202. static int __init zstd_mod_init(void)
  203. {
  204. int ret;
  205. ret = crypto_register_alg(&alg);
  206. if (ret)
  207. return ret;
  208. ret = crypto_register_scomp(&scomp);
  209. if (ret)
  210. crypto_unregister_alg(&alg);
  211. return ret;
  212. }
  213. static void __exit zstd_mod_fini(void)
  214. {
  215. crypto_unregister_alg(&alg);
  216. crypto_unregister_scomp(&scomp);
  217. }
  218. module_init(zstd_mod_init);
  219. module_exit(zstd_mod_fini);
  220. MODULE_LICENSE("GPL");
  221. MODULE_DESCRIPTION("Zstd Compression Algorithm");
  222. MODULE_ALIAS_CRYPTO("zstd");