4 * Copyright (c) 2017-present, Facebook, Inc.
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.
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
15 #include <linux/crypto.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.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>
26 #define ZSTD_DEF_LEVEL 3
35 static ZSTD_parameters zstd_params(void)
37 return ZSTD_getParams(ZSTD_DEF_LEVEL, 0, 0);
40 static int zstd_comp_init(struct zstd_ctx *ctx)
43 const ZSTD_parameters params = zstd_params();
44 const size_t wksp_size = ZSTD_CCtxWorkspaceBound(params.cParams);
46 ctx->cwksp = vzalloc(wksp_size);
52 ctx->cctx = ZSTD_initCCtx(ctx->cwksp, wksp_size);
64 static int zstd_decomp_init(struct zstd_ctx *ctx)
67 const size_t wksp_size = ZSTD_DCtxWorkspaceBound();
69 ctx->dwksp = vzalloc(wksp_size);
75 ctx->dctx = ZSTD_initDCtx(ctx->dwksp, wksp_size);
87 static void zstd_comp_exit(struct zstd_ctx *ctx)
94 static void zstd_decomp_exit(struct zstd_ctx *ctx)
101 static int __zstd_init(void *ctx)
105 ret = zstd_comp_init(ctx);
108 ret = zstd_decomp_init(ctx);
114 static void *zstd_alloc_ctx(struct crypto_scomp *tfm)
117 struct zstd_ctx *ctx;
119 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
121 return ERR_PTR(-ENOMEM);
123 ret = __zstd_init(ctx);
132 static int zstd_init(struct crypto_tfm *tfm)
134 struct zstd_ctx *ctx = crypto_tfm_ctx(tfm);
136 return __zstd_init(ctx);
139 static void __zstd_exit(void *ctx)
142 zstd_decomp_exit(ctx);
145 static void zstd_free_ctx(struct crypto_scomp *tfm, void *ctx)
151 static void zstd_exit(struct crypto_tfm *tfm)
153 struct zstd_ctx *ctx = crypto_tfm_ctx(tfm);
158 static int __zstd_compress(const u8 *src, unsigned int slen,
159 u8 *dst, unsigned int *dlen, void *ctx)
162 struct zstd_ctx *zctx = ctx;
163 const ZSTD_parameters params = zstd_params();
165 out_len = ZSTD_compressCCtx(zctx->cctx, dst, *dlen, src, slen, params);
166 if (ZSTD_isError(out_len))
172 static int zstd_compress(struct crypto_tfm *tfm, const u8 *src,
173 unsigned int slen, u8 *dst, unsigned int *dlen)
175 struct zstd_ctx *ctx = crypto_tfm_ctx(tfm);
177 return __zstd_compress(src, slen, dst, dlen, ctx);
180 static int zstd_scompress(struct crypto_scomp *tfm, const u8 *src,
181 unsigned int slen, u8 *dst, unsigned int *dlen,
184 return __zstd_compress(src, slen, dst, dlen, ctx);
187 static int __zstd_decompress(const u8 *src, unsigned int slen,
188 u8 *dst, unsigned int *dlen, void *ctx)
191 struct zstd_ctx *zctx = ctx;
193 out_len = ZSTD_decompressDCtx(zctx->dctx, dst, *dlen, src, slen);
194 if (ZSTD_isError(out_len))
200 static int zstd_decompress(struct crypto_tfm *tfm, const u8 *src,
201 unsigned int slen, u8 *dst, unsigned int *dlen)
203 struct zstd_ctx *ctx = crypto_tfm_ctx(tfm);
205 return __zstd_decompress(src, slen, dst, dlen, ctx);
208 static int zstd_sdecompress(struct crypto_scomp *tfm, const u8 *src,
209 unsigned int slen, u8 *dst, unsigned int *dlen,
212 return __zstd_decompress(src, slen, dst, dlen, ctx);
215 static struct crypto_alg alg = {
217 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
218 .cra_ctxsize = sizeof(struct zstd_ctx),
219 .cra_module = THIS_MODULE,
220 .cra_init = zstd_init,
221 .cra_exit = zstd_exit,
222 .cra_u = { .compress = {
223 .coa_compress = zstd_compress,
224 .coa_decompress = zstd_decompress } }
227 static struct scomp_alg scomp = {
228 .alloc_ctx = zstd_alloc_ctx,
229 .free_ctx = zstd_free_ctx,
230 .compress = zstd_scompress,
231 .decompress = zstd_sdecompress,
234 .cra_driver_name = "zstd-scomp",
235 .cra_module = THIS_MODULE,
239 static int __init zstd_mod_init(void)
243 ret = crypto_register_alg(&alg);
247 ret = crypto_register_scomp(&scomp);
249 crypto_unregister_alg(&alg);
254 static void __exit zstd_mod_fini(void)
256 crypto_unregister_alg(&alg);
257 crypto_unregister_scomp(&scomp);
260 module_init(zstd_mod_init);
261 module_exit(zstd_mod_fini);
263 MODULE_LICENSE("GPL");
264 MODULE_DESCRIPTION("Zstd Compression Algorithm");
265 MODULE_ALIAS_CRYPTO("zstd");