Skip to content

Commit a368f43

Browse files
gcabidduherbertx
authored andcommitted
crypto: scomp - add support for deflate rfc1950 (zlib)
Add scomp backend for zlib-deflate compression algorithm. This backend outputs data using the format defined in rfc1950 (raw deflate surrounded by zlib header and footer). Signed-off-by: Giovanni Cabiddu <giovanni.cabiddu@intel.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 3de4f5e commit a368f43

3 files changed

Lines changed: 129 additions & 17 deletions

File tree

‎crypto/deflate.c‎

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,24 @@ struct deflate_ctx {
4343
struct z_stream_s decomp_stream;
4444
};
4545

46-
static int deflate_comp_init(struct deflate_ctx *ctx)
46+
static int deflate_comp_init(struct deflate_ctx *ctx, int format)
4747
{
4848
int ret = 0;
4949
struct z_stream_s *stream = &ctx->comp_stream;
5050

5151
stream->workspace = vzalloc(zlib_deflate_workspacesize(
52-
-DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL));
52+
MAX_WBITS, MAX_MEM_LEVEL));
5353
if (!stream->workspace) {
5454
ret = -ENOMEM;
5555
goto out;
5656
}
57-
ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
58-
-DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
59-
Z_DEFAULT_STRATEGY);
57+
if (format)
58+
ret = zlib_deflateInit(stream, 3);
59+
else
60+
ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
61+
-DEFLATE_DEF_WINBITS,
62+
DEFLATE_DEF_MEMLEVEL,
63+
Z_DEFAULT_STRATEGY);
6064
if (ret != Z_OK) {
6165
ret = -EINVAL;
6266
goto out_free;
@@ -68,7 +72,7 @@ static int deflate_comp_init(struct deflate_ctx *ctx)
6872
goto out;
6973
}
7074

71-
static int deflate_decomp_init(struct deflate_ctx *ctx)
75+
static int deflate_decomp_init(struct deflate_ctx *ctx, int format)
7276
{
7377
int ret = 0;
7478
struct z_stream_s *stream = &ctx->decomp_stream;
@@ -78,7 +82,10 @@ static int deflate_decomp_init(struct deflate_ctx *ctx)
7882
ret = -ENOMEM;
7983
goto out;
8084
}
81-
ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
85+
if (format)
86+
ret = zlib_inflateInit(stream);
87+
else
88+
ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
8289
if (ret != Z_OK) {
8390
ret = -EINVAL;
8491
goto out_free;
@@ -102,21 +109,21 @@ static void deflate_decomp_exit(struct deflate_ctx *ctx)
102109
vfree(ctx->decomp_stream.workspace);
103110
}
104111

105-
static int __deflate_init(void *ctx)
112+
static int __deflate_init(void *ctx, int format)
106113
{
107114
int ret;
108115

109-
ret = deflate_comp_init(ctx);
116+
ret = deflate_comp_init(ctx, format);
110117
if (ret)
111118
goto out;
112-
ret = deflate_decomp_init(ctx);
119+
ret = deflate_decomp_init(ctx, format);
113120
if (ret)
114121
deflate_comp_exit(ctx);
115122
out:
116123
return ret;
117124
}
118125

119-
static void *deflate_alloc_ctx(struct crypto_scomp *tfm)
126+
static void *gen_deflate_alloc_ctx(struct crypto_scomp *tfm, int format)
120127
{
121128
struct deflate_ctx *ctx;
122129
int ret;
@@ -125,7 +132,7 @@ static void *deflate_alloc_ctx(struct crypto_scomp *tfm)
125132
if (!ctx)
126133
return ERR_PTR(-ENOMEM);
127134

128-
ret = __deflate_init(ctx);
135+
ret = __deflate_init(ctx, format);
129136
if (ret) {
130137
kfree(ctx);
131138
return ERR_PTR(ret);
@@ -134,11 +141,21 @@ static void *deflate_alloc_ctx(struct crypto_scomp *tfm)
134141
return ctx;
135142
}
136143

144+
static void *deflate_alloc_ctx(struct crypto_scomp *tfm)
145+
{
146+
return gen_deflate_alloc_ctx(tfm, 0);
147+
}
148+
149+
static void *zlib_deflate_alloc_ctx(struct crypto_scomp *tfm)
150+
{
151+
return gen_deflate_alloc_ctx(tfm, 1);
152+
}
153+
137154
static int deflate_init(struct crypto_tfm *tfm)
138155
{
139156
struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
140157

141-
return __deflate_init(ctx);
158+
return __deflate_init(ctx, 0);
142159
}
143160

144161
static void __deflate_exit(void *ctx)
@@ -272,7 +289,7 @@ static struct crypto_alg alg = {
272289
.coa_decompress = deflate_decompress } }
273290
};
274291

275-
static struct scomp_alg scomp = {
292+
static struct scomp_alg scomp[] = { {
276293
.alloc_ctx = deflate_alloc_ctx,
277294
.free_ctx = deflate_free_ctx,
278295
.compress = deflate_scompress,
@@ -282,7 +299,17 @@ static struct scomp_alg scomp = {
282299
.cra_driver_name = "deflate-scomp",
283300
.cra_module = THIS_MODULE,
284301
}
285-
};
302+
}, {
303+
.alloc_ctx = zlib_deflate_alloc_ctx,
304+
.free_ctx = deflate_free_ctx,
305+
.compress = deflate_scompress,
306+
.decompress = deflate_sdecompress,
307+
.base = {
308+
.cra_name = "zlib-deflate",
309+
.cra_driver_name = "zlib-deflate-scomp",
310+
.cra_module = THIS_MODULE,
311+
}
312+
} };
286313

287314
static int __init deflate_mod_init(void)
288315
{
@@ -292,7 +319,7 @@ static int __init deflate_mod_init(void)
292319
if (ret)
293320
return ret;
294321

295-
ret = crypto_register_scomp(&scomp);
322+
ret = crypto_register_scomps(scomp, ARRAY_SIZE(scomp));
296323
if (ret) {
297324
crypto_unregister_alg(&alg);
298325
return ret;
@@ -304,7 +331,7 @@ static int __init deflate_mod_init(void)
304331
static void __exit deflate_mod_fini(void)
305332
{
306333
crypto_unregister_alg(&alg);
307-
crypto_unregister_scomp(&scomp);
334+
crypto_unregister_scomps(scomp, ARRAY_SIZE(scomp));
308335
}
309336

310337
module_init(deflate_mod_init);

‎crypto/testmgr.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3513,6 +3513,16 @@ static const struct alg_test_desc alg_test_descs[] = {
35133513
.dec = __VECS(tf_xts_dec_tv_template)
35143514
}
35153515
}
3516+
}, {
3517+
.alg = "zlib-deflate",
3518+
.test = alg_test_comp,
3519+
.fips_allowed = 1,
3520+
.suite = {
3521+
.comp = {
3522+
.comp = __VECS(zlib_deflate_comp_tv_template),
3523+
.decomp = __VECS(zlib_deflate_decomp_tv_template)
3524+
}
3525+
}
35163526
}
35173527
};
35183528

‎crypto/testmgr.h‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33204,6 +33204,81 @@ static const struct comp_testvec deflate_decomp_tv_template[] = {
3320433204
},
3320533205
};
3320633206

33207+
static const struct comp_testvec zlib_deflate_comp_tv_template[] = {
33208+
{
33209+
.inlen = 70,
33210+
.outlen = 44,
33211+
.input = "Join us now and share the software "
33212+
"Join us now and share the software ",
33213+
.output = "\x78\x5e\xf3\xca\xcf\xcc\x53\x28"
33214+
"\x2d\x56\xc8\xcb\x2f\x57\x48\xcc"
33215+
"\x4b\x51\x28\xce\x48\x2c\x4a\x55"
33216+
"\x28\xc9\x48\x55\x28\xce\x4f\x2b"
33217+
"\x29\x07\x71\xbc\x08\x2b\x01\x00"
33218+
"\x7c\x65\x19\x3d",
33219+
}, {
33220+
.inlen = 191,
33221+
.outlen = 129,
33222+
.input = "This document describes a compression method based on the DEFLATE"
33223+
"compression algorithm. This document defines the application of "
33224+
"the DEFLATE algorithm to the IP Payload Compression Protocol.",
33225+
.output = "\x78\x5e\x5d\xce\x41\x0a\xc3\x30"
33226+
"\x0c\x04\xc0\xaf\xec\x0b\xf2\x87"
33227+
"\xd2\xa6\x50\xe8\xc1\x07\x7f\x40"
33228+
"\xb1\x95\x5a\x60\x5b\xc6\x56\x0f"
33229+
"\xfd\x7d\x93\x1e\x42\xe8\x51\xec"
33230+
"\xee\x20\x9f\x64\x20\x6a\x78\x17"
33231+
"\xae\x86\xc8\x23\x74\x59\x78\x80"
33232+
"\x10\xb4\xb4\xce\x63\x88\x56\x14"
33233+
"\xb6\xa4\x11\x0b\x0d\x8e\xd8\x6e"
33234+
"\x4b\x8c\xdb\x7c\x7f\x5e\xfc\x7c"
33235+
"\xae\x51\x7e\x69\x17\x4b\x65\x02"
33236+
"\xfc\x1f\xbc\x4a\xdd\xd8\x7d\x48"
33237+
"\xad\x65\x09\x64\x3b\xac\xeb\xd9"
33238+
"\xc2\x01\xc0\xf4\x17\x3c\x1c\x1c"
33239+
"\x7d\xb2\x52\xc4\xf5\xf4\x8f\xeb"
33240+
"\x6a\x1a\x34\x4f\x5f\x2e\x32\x45"
33241+
"\x4e",
33242+
},
33243+
};
33244+
33245+
static const struct comp_testvec zlib_deflate_decomp_tv_template[] = {
33246+
{
33247+
.inlen = 128,
33248+
.outlen = 191,
33249+
.input = "\x78\x9c\x5d\x8d\x31\x0e\xc2\x30"
33250+
"\x10\x04\xbf\xb2\x2f\xc8\x1f\x10"
33251+
"\x04\x09\x89\xc2\x85\x3f\x70\xb1"
33252+
"\x2f\xf8\x24\xdb\x67\xd9\x47\xc1"
33253+
"\xef\x49\x68\x12\x51\xae\x76\x67"
33254+
"\xd6\x27\x19\x88\x1a\xde\x85\xab"
33255+
"\x21\xf2\x08\x5d\x16\x1e\x20\x04"
33256+
"\x2d\xad\xf3\x18\xa2\x15\x85\x2d"
33257+
"\x69\xc4\x42\x83\x23\xb6\x6c\x89"
33258+
"\x71\x9b\xef\xcf\x8b\x9f\xcf\x33"
33259+
"\xca\x2f\xed\x62\xa9\x4c\x80\xff"
33260+
"\x13\xaf\x52\x37\xed\x0e\x52\x6b"
33261+
"\x59\x02\xd9\x4e\xe8\x7a\x76\x1d"
33262+
"\x02\x98\xfe\x8a\x87\x83\xa3\x4f"
33263+
"\x56\x8a\xb8\x9e\x8e\x5c\x57\xd3"
33264+
"\xa0\x79\xfa\x02\x2e\x32\x45\x4e",
33265+
.output = "This document describes a compression method based on the DEFLATE"
33266+
"compression algorithm. This document defines the application of "
33267+
"the DEFLATE algorithm to the IP Payload Compression Protocol.",
33268+
}, {
33269+
.inlen = 44,
33270+
.outlen = 70,
33271+
.input = "\x78\x9c\xf3\xca\xcf\xcc\x53\x28"
33272+
"\x2d\x56\xc8\xcb\x2f\x57\x48\xcc"
33273+
"\x4b\x51\x28\xce\x48\x2c\x4a\x55"
33274+
"\x28\xc9\x48\x55\x28\xce\x4f\x2b"
33275+
"\x29\x07\x71\xbc\x08\x2b\x01\x00"
33276+
"\x7c\x65\x19\x3d",
33277+
.output = "Join us now and share the software "
33278+
"Join us now and share the software ",
33279+
},
33280+
};
33281+
3320733282
/*
3320833283
* LZO test vectors (null-terminated strings).
3320933284
*/

0 commit comments

Comments
 (0)