1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2013 Chanho Min <chanho.min@lge.com>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/crypto.h>
10 #include <linux/vmalloc.h>
11 #include <linux/lz4.h>
12 #include <crypto/internal/scompress.h>
18 static void *lz4hc_alloc_ctx(struct crypto_scomp *tfm)
22 ctx = vmalloc(LZ4HC_MEM_COMPRESS);
24 return ERR_PTR(-ENOMEM);
29 static int lz4hc_init(struct crypto_tfm *tfm)
31 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
33 ctx->lz4hc_comp_mem = lz4hc_alloc_ctx(NULL);
34 if (IS_ERR(ctx->lz4hc_comp_mem))
40 static void lz4hc_free_ctx(struct crypto_scomp *tfm, void *ctx)
45 static void lz4hc_exit(struct crypto_tfm *tfm)
47 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
49 lz4hc_free_ctx(NULL, ctx->lz4hc_comp_mem);
52 static int __lz4hc_compress_crypto(const u8 *src, unsigned int slen,
53 u8 *dst, unsigned int *dlen, void *ctx)
55 int out_len = LZ4_compress_HC(src, dst, slen,
56 *dlen, LZ4HC_DEFAULT_CLEVEL, ctx);
65 static int lz4hc_scompress(struct crypto_scomp *tfm, const u8 *src,
66 unsigned int slen, u8 *dst, unsigned int *dlen,
69 return __lz4hc_compress_crypto(src, slen, dst, dlen, ctx);
72 static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
73 unsigned int slen, u8 *dst,
76 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
78 return __lz4hc_compress_crypto(src, slen, dst, dlen,
82 static int __lz4hc_decompress_crypto(const u8 *src, unsigned int slen,
83 u8 *dst, unsigned int *dlen, void *ctx)
85 int out_len = LZ4_decompress_safe(src, dst, slen, *dlen);
94 static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
95 unsigned int slen, u8 *dst, unsigned int *dlen,
98 return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
101 static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
102 unsigned int slen, u8 *dst,
105 return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
108 static struct crypto_alg alg_lz4hc = {
110 .cra_driver_name = "lz4hc-generic",
111 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
112 .cra_ctxsize = sizeof(struct lz4hc_ctx),
113 .cra_module = THIS_MODULE,
114 .cra_init = lz4hc_init,
115 .cra_exit = lz4hc_exit,
116 .cra_u = { .compress = {
117 .coa_compress = lz4hc_compress_crypto,
118 .coa_decompress = lz4hc_decompress_crypto } }
121 static struct scomp_alg scomp = {
122 .alloc_ctx = lz4hc_alloc_ctx,
123 .free_ctx = lz4hc_free_ctx,
124 .compress = lz4hc_scompress,
125 .decompress = lz4hc_sdecompress,
128 .cra_driver_name = "lz4hc-scomp",
129 .cra_module = THIS_MODULE,
133 static int __init lz4hc_mod_init(void)
137 ret = crypto_register_alg(&alg_lz4hc);
141 ret = crypto_register_scomp(&scomp);
143 crypto_unregister_alg(&alg_lz4hc);
150 static void __exit lz4hc_mod_fini(void)
152 crypto_unregister_alg(&alg_lz4hc);
153 crypto_unregister_scomp(&scomp);
156 subsys_initcall(lz4hc_mod_init);
157 module_exit(lz4hc_mod_fini);
159 MODULE_LICENSE("GPL");
160 MODULE_DESCRIPTION("LZ4HC Compression Algorithm");
161 MODULE_ALIAS_CRYPTO("lz4hc");