zstd.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #define ZSTD_DEF_LEVEL 3
  24. struct zstd_ctx {
  25. ZSTD_CCtx *cctx;
  26. ZSTD_DCtx *dctx;
  27. void *cwksp;
  28. void *dwksp;
  29. };
  30. static ZSTD_parameters zstd_params(void)
  31. {
  32. return ZSTD_getParams(ZSTD_DEF_LEVEL, 0, 0);
  33. }
  34. static int zstd_comp_init(struct zstd_ctx *ctx)
  35. {
  36. int ret = 0;
  37. const ZSTD_parameters params = zstd_params();
  38. const size_t wksp_size = ZSTD_CCtxWorkspaceBound(params.cParams);
  39. ctx->cwksp = vzalloc(wksp_size);
  40. if (!ctx->cwksp) {
  41. ret = -ENOMEM;
  42. goto out;
  43. }
  44. ctx->cctx = ZSTD_initCCtx(ctx->cwksp, wksp_size);
  45. if (!ctx->cctx) {
  46. ret = -EINVAL;
  47. goto out_free;
  48. }
  49. out:
  50. return ret;
  51. out_free:
  52. vfree(ctx->cwksp);
  53. goto out;
  54. }
  55. static int zstd_decomp_init(struct zstd_ctx *ctx)
  56. {
  57. int ret = 0;
  58. const size_t wksp_size = ZSTD_DCtxWorkspaceBound();
  59. ctx->dwksp = vzalloc(wksp_size);
  60. if (!ctx->dwksp) {
  61. ret = -ENOMEM;
  62. goto out;
  63. }
  64. ctx->dctx = ZSTD_initDCtx(ctx->dwksp, wksp_size);
  65. if (!ctx->dctx) {
  66. ret = -EINVAL;
  67. goto out_free;
  68. }
  69. out:
  70. return ret;
  71. out_free:
  72. vfree(ctx->dwksp);
  73. goto out;
  74. }
  75. static void zstd_comp_exit(struct zstd_ctx *ctx)
  76. {
  77. vfree(ctx->cwksp);
  78. ctx->cwksp = NULL;
  79. ctx->cctx = NULL;
  80. }
  81. static void zstd_decomp_exit(struct zstd_ctx *ctx)
  82. {
  83. vfree(ctx->dwksp);
  84. ctx->dwksp = NULL;
  85. ctx->dctx = NULL;
  86. }
  87. static int __zstd_init(void *ctx)
  88. {
  89. int ret;
  90. ret = zstd_comp_init(ctx);
  91. if (ret)
  92. return ret;
  93. ret = zstd_decomp_init(ctx);
  94. if (ret)
  95. zstd_comp_exit(ctx);
  96. return ret;
  97. }
  98. static int zstd_init(struct crypto_tfm *tfm)
  99. {
  100. struct zstd_ctx *ctx = crypto_tfm_ctx(tfm);
  101. return __zstd_init(ctx);
  102. }
  103. static void __zstd_exit(void *ctx)
  104. {
  105. zstd_comp_exit(ctx);
  106. zstd_decomp_exit(ctx);
  107. }
  108. static void zstd_exit(struct crypto_tfm *tfm)
  109. {
  110. struct zstd_ctx *ctx = crypto_tfm_ctx(tfm);
  111. __zstd_exit(ctx);
  112. }
  113. static int __zstd_compress(const u8 *src, unsigned int slen,
  114. u8 *dst, unsigned int *dlen, void *ctx)
  115. {
  116. size_t out_len;
  117. struct zstd_ctx *zctx = ctx;
  118. const ZSTD_parameters params = zstd_params();
  119. out_len = ZSTD_compressCCtx(zctx->cctx, dst, *dlen, src, slen, params);
  120. if (ZSTD_isError(out_len))
  121. return -EINVAL;
  122. *dlen = out_len;
  123. return 0;
  124. }
  125. static int zstd_compress(struct crypto_tfm *tfm, const u8 *src,
  126. unsigned int slen, u8 *dst, unsigned int *dlen)
  127. {
  128. struct zstd_ctx *ctx = crypto_tfm_ctx(tfm);
  129. return __zstd_compress(src, slen, dst, dlen, ctx);
  130. }
  131. static int __zstd_decompress(const u8 *src, unsigned int slen,
  132. u8 *dst, unsigned int *dlen, void *ctx)
  133. {
  134. size_t out_len;
  135. struct zstd_ctx *zctx = ctx;
  136. out_len = ZSTD_decompressDCtx(zctx->dctx, dst, *dlen, src, slen);
  137. if (ZSTD_isError(out_len))
  138. return -EINVAL;
  139. *dlen = out_len;
  140. return 0;
  141. }
  142. static int zstd_decompress(struct crypto_tfm *tfm, const u8 *src,
  143. unsigned int slen, u8 *dst, unsigned int *dlen)
  144. {
  145. struct zstd_ctx *ctx = crypto_tfm_ctx(tfm);
  146. return __zstd_decompress(src, slen, dst, dlen, ctx);
  147. }
  148. static struct crypto_alg alg = {
  149. .cra_name = "zstd",
  150. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  151. .cra_ctxsize = sizeof(struct zstd_ctx),
  152. .cra_module = THIS_MODULE,
  153. .cra_init = zstd_init,
  154. .cra_exit = zstd_exit,
  155. .cra_u = { .compress = {
  156. .coa_compress = zstd_compress,
  157. .coa_decompress = zstd_decompress } }
  158. };
  159. static int __init zstd_mod_init(void)
  160. {
  161. int ret;
  162. ret = crypto_register_alg(&alg);
  163. if (ret)
  164. return ret;
  165. return ret;
  166. }
  167. static void __exit zstd_mod_fini(void)
  168. {
  169. crypto_unregister_alg(&alg);
  170. }
  171. module_init(zstd_mod_init);
  172. module_exit(zstd_mod_fini);
  173. MODULE_LICENSE("GPL");
  174. MODULE_DESCRIPTION("Zstd Compression Algorithm");
  175. MODULE_ALIAS_CRYPTO("zstd");