1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2013 Chanho Min <chanho.min@lge.com>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/crypto.h>
11 #include <linux/vmalloc.h>
12 #include <linux/lz4.h>
13 #include <crypto/internal/scompress.h>
19 static void *lz4_alloc_ctx(struct crypto_scomp *tfm)
23 ctx = vmalloc(LZ4_MEM_COMPRESS);
25 return ERR_PTR(-ENOMEM);
30 static int lz4_init(struct crypto_tfm *tfm)
32 struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
34 ctx->lz4_comp_mem = lz4_alloc_ctx(NULL);
35 if (IS_ERR(ctx->lz4_comp_mem))
41 static void lz4_free_ctx(struct crypto_scomp *tfm, void *ctx)
46 static void lz4_exit(struct crypto_tfm *tfm)
48 struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
50 lz4_free_ctx(NULL, ctx->lz4_comp_mem);
53 static int __lz4_compress_crypto(const u8 *src, unsigned int slen,
54 u8 *dst, unsigned int *dlen, void *ctx)
56 int out_len = LZ4_compress_default(src, dst,
66 static int lz4_scompress(struct crypto_scomp *tfm, const u8 *src,
67 unsigned int slen, u8 *dst, unsigned int *dlen,
70 return __lz4_compress_crypto(src, slen, dst, dlen, ctx);
73 static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
74 unsigned int slen, u8 *dst, unsigned int *dlen)
76 struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
78 return __lz4_compress_crypto(src, slen, dst, dlen, ctx->lz4_comp_mem);
81 static int __lz4_decompress_crypto(const u8 *src, unsigned int slen,
82 u8 *dst, unsigned int *dlen, void *ctx)
84 int out_len = LZ4_decompress_safe(src, dst, slen, *dlen);
93 static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
94 unsigned int slen, u8 *dst, unsigned int *dlen,
97 return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
100 static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
101 unsigned int slen, u8 *dst,
104 return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
107 static struct crypto_alg alg_lz4 = {
109 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
110 .cra_ctxsize = sizeof(struct lz4_ctx),
111 .cra_module = THIS_MODULE,
112 .cra_init = lz4_init,
113 .cra_exit = lz4_exit,
114 .cra_u = { .compress = {
115 .coa_compress = lz4_compress_crypto,
116 .coa_decompress = lz4_decompress_crypto } }
119 static struct scomp_alg scomp = {
120 .alloc_ctx = lz4_alloc_ctx,
121 .free_ctx = lz4_free_ctx,
122 .compress = lz4_scompress,
123 .decompress = lz4_sdecompress,
126 .cra_driver_name = "lz4-scomp",
127 .cra_module = THIS_MODULE,
131 static int __init lz4_mod_init(void)
135 ret = crypto_register_alg(&alg_lz4);
139 ret = crypto_register_scomp(&scomp);
141 crypto_unregister_alg(&alg_lz4);
148 static void __exit lz4_mod_fini(void)
150 crypto_unregister_alg(&alg_lz4);
151 crypto_unregister_scomp(&scomp);
154 subsys_initcall(lz4_mod_init);
155 module_exit(lz4_mod_fini);
157 MODULE_LICENSE("GPL");
158 MODULE_DESCRIPTION("LZ4 Compression Algorithm");
159 MODULE_ALIAS_CRYPTO("lz4");