Skip to content

Commit 8cd9330

Browse files
gcabidduherbertx
authored andcommitted
crypto: acomp - add support for lz4 via scomp
Add scomp backend for lz4 compression algorithm. Signed-off-by: Giovanni Cabiddu <giovanni.cabiddu@intel.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent ac9d2c4 commit 8cd9330

2 files changed

Lines changed: 82 additions & 10 deletions

File tree

‎crypto/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,7 @@ config CRYPTO_842
16061606
config CRYPTO_LZ4
16071607
tristate "LZ4 compression algorithm"
16081608
select CRYPTO_ALGAPI
1609+
select CRYPTO_ACOMP2
16091610
select LZ4_COMPRESS
16101611
select LZ4_DECOMPRESS
16111612
help

‎crypto/lz4.c‎

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,53 @@
2323
#include <linux/crypto.h>
2424
#include <linux/vmalloc.h>
2525
#include <linux/lz4.h>
26+
#include <crypto/internal/scompress.h>
2627

2728
struct lz4_ctx {
2829
void *lz4_comp_mem;
2930
};
3031

32+
static void *lz4_alloc_ctx(struct crypto_scomp *tfm)
33+
{
34+
void *ctx;
35+
36+
ctx = vmalloc(LZ4_MEM_COMPRESS);
37+
if (!ctx)
38+
return ERR_PTR(-ENOMEM);
39+
40+
return ctx;
41+
}
42+
3143
static int lz4_init(struct crypto_tfm *tfm)
3244
{
3345
struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
3446

35-
ctx->lz4_comp_mem = vmalloc(LZ4_MEM_COMPRESS);
36-
if (!ctx->lz4_comp_mem)
47+
ctx->lz4_comp_mem = lz4_alloc_ctx(NULL);
48+
if (IS_ERR(ctx->lz4_comp_mem))
3749
return -ENOMEM;
3850

3951
return 0;
4052
}
4153

54+
static void lz4_free_ctx(struct crypto_scomp *tfm, void *ctx)
55+
{
56+
vfree(ctx);
57+
}
58+
4259
static void lz4_exit(struct crypto_tfm *tfm)
4360
{
4461
struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
45-
vfree(ctx->lz4_comp_mem);
62+
63+
lz4_free_ctx(NULL, ctx->lz4_comp_mem);
4664
}
4765

48-
static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
49-
unsigned int slen, u8 *dst, unsigned int *dlen)
66+
static int __lz4_compress_crypto(const u8 *src, unsigned int slen,
67+
u8 *dst, unsigned int *dlen, void *ctx)
5068
{
51-
struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
5269
size_t tmp_len = *dlen;
5370
int err;
5471

55-
err = lz4_compress(src, slen, dst, &tmp_len, ctx->lz4_comp_mem);
72+
err = lz4_compress(src, slen, dst, &tmp_len, ctx);
5673

5774
if (err < 0)
5875
return -EINVAL;
@@ -61,8 +78,23 @@ static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
6178
return 0;
6279
}
6380

64-
static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
65-
unsigned int slen, u8 *dst, unsigned int *dlen)
81+
static int lz4_scompress(struct crypto_scomp *tfm, const u8 *src,
82+
unsigned int slen, u8 *dst, unsigned int *dlen,
83+
void *ctx)
84+
{
85+
return __lz4_compress_crypto(src, slen, dst, dlen, ctx);
86+
}
87+
88+
static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
89+
unsigned int slen, u8 *dst, unsigned int *dlen)
90+
{
91+
struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
92+
93+
return __lz4_compress_crypto(src, slen, dst, dlen, ctx->lz4_comp_mem);
94+
}
95+
96+
static int __lz4_decompress_crypto(const u8 *src, unsigned int slen,
97+
u8 *dst, unsigned int *dlen, void *ctx)
6698
{
6799
int err;
68100
size_t tmp_len = *dlen;
@@ -76,6 +108,20 @@ static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
76108
return err;
77109
}
78110

111+
static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
112+
unsigned int slen, u8 *dst, unsigned int *dlen,
113+
void *ctx)
114+
{
115+
return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
116+
}
117+
118+
static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
119+
unsigned int slen, u8 *dst,
120+
unsigned int *dlen)
121+
{
122+
return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
123+
}
124+
79125
static struct crypto_alg alg_lz4 = {
80126
.cra_name = "lz4",
81127
.cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
@@ -89,14 +135,39 @@ static struct crypto_alg alg_lz4 = {
89135
.coa_decompress = lz4_decompress_crypto } }
90136
};
91137

138+
static struct scomp_alg scomp = {
139+
.alloc_ctx = lz4_alloc_ctx,
140+
.free_ctx = lz4_free_ctx,
141+
.compress = lz4_scompress,
142+
.decompress = lz4_sdecompress,
143+
.base = {
144+
.cra_name = "lz4",
145+
.cra_driver_name = "lz4-scomp",
146+
.cra_module = THIS_MODULE,
147+
}
148+
};
149+
92150
static int __init lz4_mod_init(void)
93151
{
94-
return crypto_register_alg(&alg_lz4);
152+
int ret;
153+
154+
ret = crypto_register_alg(&alg_lz4);
155+
if (ret)
156+
return ret;
157+
158+
ret = crypto_register_scomp(&scomp);
159+
if (ret) {
160+
crypto_unregister_alg(&alg_lz4);
161+
return ret;
162+
}
163+
164+
return ret;
95165
}
96166

97167
static void __exit lz4_mod_fini(void)
98168
{
99169
crypto_unregister_alg(&alg_lz4);
170+
crypto_unregister_scomp(&scomp);
100171
}
101172

102173
module_init(lz4_mod_init);

0 commit comments

Comments
 (0)