Skip to content

Commit 0b77abb

Browse files
Zoltan Sogorherbertx
authored andcommitted
[CRYPTO] lzo: Add LZO compression algorithm support
Add LZO compression algorithm support Signed-off-by: Zoltan Sogor <weth@inf.u-szeged.hu> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 91755a9 commit 0b77abb

5 files changed

Lines changed: 205 additions & 1 deletion

File tree

‎crypto/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,14 @@ config CRYPTO_AUTHENC
536536
Authenc: Combined mode wrapper for IPsec.
537537
This is required for IPSec.
538538

539+
config CRYPTO_LZO
540+
tristate "LZO compression algorithm"
541+
select CRYPTO_ALGAPI
542+
select LZO_COMPRESS
543+
select LZO_DECOMPRESS
544+
help
545+
This is the LZO algorithm.
546+
539547
source "drivers/crypto/Kconfig"
540548

541549
endif # if CRYPTO

‎crypto/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ obj-$(CONFIG_CRYPTO_DEFLATE) += deflate.o
5555
obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
5656
obj-$(CONFIG_CRYPTO_CRC32C) += crc32c.o
5757
obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o
58+
obj-$(CONFIG_CRYPTO_LZO) += lzo.o
5859

5960
obj-$(CONFIG_CRYPTO_TEST) += tcrypt.o
6061

‎crypto/lzo.c‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Cryptographic API.
3+
*
4+
* This program is free software; you can redistribute it and/or modify it
5+
* under the terms of the GNU General Public License version 2 as published by
6+
* the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful, but WITHOUT
9+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11+
* more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with
14+
* this program; if not, write to the Free Software Foundation, Inc., 51
15+
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
*
17+
*/
18+
19+
#include <linux/init.h>
20+
#include <linux/module.h>
21+
#include <linux/crypto.h>
22+
#include <linux/vmalloc.h>
23+
#include <linux/lzo.h>
24+
25+
struct lzo_ctx {
26+
void *lzo_comp_mem;
27+
};
28+
29+
static int lzo_init(struct crypto_tfm *tfm)
30+
{
31+
struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
32+
33+
ctx->lzo_comp_mem = vmalloc(LZO1X_MEM_COMPRESS);
34+
if (!ctx->lzo_comp_mem)
35+
return -ENOMEM;
36+
37+
return 0;
38+
}
39+
40+
static void lzo_exit(struct crypto_tfm *tfm)
41+
{
42+
struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
43+
44+
vfree(ctx->lzo_comp_mem);
45+
}
46+
47+
static int lzo_compress(struct crypto_tfm *tfm, const u8 *src,
48+
unsigned int slen, u8 *dst, unsigned int *dlen)
49+
{
50+
struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
51+
size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
52+
int err;
53+
54+
err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx->lzo_comp_mem);
55+
56+
if (err != LZO_E_OK)
57+
return -EINVAL;
58+
59+
*dlen = tmp_len;
60+
return 0;
61+
}
62+
63+
static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src,
64+
unsigned int slen, u8 *dst, unsigned int *dlen)
65+
{
66+
int err;
67+
size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
68+
69+
err = lzo1x_decompress_safe(src, slen, dst, &tmp_len);
70+
71+
if (err != LZO_E_OK)
72+
return -EINVAL;
73+
74+
*dlen = tmp_len;
75+
return 0;
76+
77+
}
78+
79+
static struct crypto_alg alg = {
80+
.cra_name = "lzo",
81+
.cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
82+
.cra_ctxsize = sizeof(struct lzo_ctx),
83+
.cra_module = THIS_MODULE,
84+
.cra_list = LIST_HEAD_INIT(alg.cra_list),
85+
.cra_init = lzo_init,
86+
.cra_exit = lzo_exit,
87+
.cra_u = { .compress = {
88+
.coa_compress = lzo_compress,
89+
.coa_decompress = lzo_decompress } }
90+
};
91+
92+
static int __init init(void)
93+
{
94+
return crypto_register_alg(&alg);
95+
}
96+
97+
static void __exit fini(void)
98+
{
99+
crypto_unregister_alg(&alg);
100+
}
101+
102+
module_init(init);
103+
module_exit(fini);
104+
105+
MODULE_LICENSE("GPL");
106+
MODULE_DESCRIPTION("LZO Compression Algorithm");

‎crypto/tcrypt.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static char *check[] = {
8484
"cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
8585
"arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
8686
"khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
87-
"camellia", "seed", "salsa20", NULL
87+
"camellia", "seed", "salsa20", "lzo", NULL
8888
};
8989

9090
static void hexdump(unsigned char *buf, unsigned int len)
@@ -1292,6 +1292,8 @@ static void do_test(void)
12921292
test_comp("deflate", deflate_comp_tv_template,
12931293
deflate_decomp_tv_template, DEFLATE_COMP_TEST_VECTORS,
12941294
DEFLATE_DECOMP_TEST_VECTORS);
1295+
test_comp("lzo", lzo_comp_tv_template, lzo_decomp_tv_template,
1296+
LZO_COMP_TEST_VECTORS, LZO_DECOMP_TEST_VECTORS);
12951297
test_hash("crc32c", crc32c_tv_template, CRC32C_TEST_VECTORS);
12961298
test_hash("hmac(md5)", hmac_md5_tv_template,
12971299
HMAC_MD5_TEST_VECTORS);
@@ -1550,6 +1552,11 @@ static void do_test(void)
15501552
AES_GCM_DEC_TEST_VECTORS);
15511553
break;
15521554

1555+
case 36:
1556+
test_comp("lzo", lzo_comp_tv_template, lzo_decomp_tv_template,
1557+
LZO_COMP_TEST_VECTORS, LZO_DECOMP_TEST_VECTORS);
1558+
break;
1559+
15531560
case 100:
15541561
test_hash("hmac(md5)", hmac_md5_tv_template,
15551562
HMAC_MD5_TEST_VECTORS);

‎crypto/tcrypt.h‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7460,6 +7460,88 @@ static struct comp_testvec deflate_decomp_tv_template[] = {
74607460
},
74617461
};
74627462

7463+
/*
7464+
* LZO test vectors (null-terminated strings).
7465+
*/
7466+
#define LZO_COMP_TEST_VECTORS 2
7467+
#define LZO_DECOMP_TEST_VECTORS 2
7468+
7469+
static struct comp_testvec lzo_comp_tv_template[] = {
7470+
{
7471+
.inlen = 70,
7472+
.outlen = 46,
7473+
.input = "Join us now and share the software "
7474+
"Join us now and share the software ",
7475+
.output = { 0x00, 0x0d, 0x4a, 0x6f, 0x69, 0x6e, 0x20, 0x75,
7476+
0x73, 0x20, 0x6e, 0x6f, 0x77, 0x20, 0x61, 0x6e,
7477+
0x64, 0x20, 0x73, 0x68, 0x61, 0x72, 0x65, 0x20,
7478+
0x74, 0x68, 0x65, 0x20, 0x73, 0x6f, 0x66, 0x74,
7479+
0x77, 0x70, 0x01, 0x01, 0x4a, 0x6f, 0x69, 0x6e,
7480+
0x3d, 0x88, 0x00, 0x11, 0x00, 0x00 },
7481+
}, {
7482+
.inlen = 159,
7483+
.outlen = 133,
7484+
.input = "This document describes a compression method based on the LZO "
7485+
"compression algorithm. This document defines the application of "
7486+
"the LZO algorithm used in UBIFS.",
7487+
.output = { 0x00, 0x2b, 0x54, 0x68, 0x69, 0x73, 0x20, 0x64,
7488+
0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x20,
7489+
0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65,
7490+
0x73, 0x20, 0x61, 0x20, 0x63, 0x6f, 0x6d, 0x70,
7491+
0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x20,
7492+
0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x20, 0x62,
7493+
0x61, 0x73, 0x65, 0x64, 0x20, 0x6f, 0x6e, 0x20,
7494+
0x74, 0x68, 0x65, 0x20, 0x4c, 0x5a, 0x4f, 0x2b,
7495+
0x8c, 0x00, 0x0d, 0x61, 0x6c, 0x67, 0x6f, 0x72,
7496+
0x69, 0x74, 0x68, 0x6d, 0x2e, 0x20, 0x20, 0x54,
7497+
0x68, 0x69, 0x73, 0x2a, 0x54, 0x01, 0x02, 0x66,
7498+
0x69, 0x6e, 0x65, 0x73, 0x94, 0x06, 0x05, 0x61,
7499+
0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x76,
7500+
0x0a, 0x6f, 0x66, 0x88, 0x02, 0x60, 0x09, 0x27,
7501+
0xf0, 0x00, 0x0c, 0x20, 0x75, 0x73, 0x65, 0x64,
7502+
0x20, 0x69, 0x6e, 0x20, 0x55, 0x42, 0x49, 0x46,
7503+
0x53, 0x2e, 0x11, 0x00, 0x00 },
7504+
},
7505+
};
7506+
7507+
static struct comp_testvec lzo_decomp_tv_template[] = {
7508+
{
7509+
.inlen = 133,
7510+
.outlen = 159,
7511+
.input = { 0x00, 0x2b, 0x54, 0x68, 0x69, 0x73, 0x20, 0x64,
7512+
0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x20,
7513+
0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65,
7514+
0x73, 0x20, 0x61, 0x20, 0x63, 0x6f, 0x6d, 0x70,
7515+
0x72, 0x65, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x20,
7516+
0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x20, 0x62,
7517+
0x61, 0x73, 0x65, 0x64, 0x20, 0x6f, 0x6e, 0x20,
7518+
0x74, 0x68, 0x65, 0x20, 0x4c, 0x5a, 0x4f, 0x2b,
7519+
0x8c, 0x00, 0x0d, 0x61, 0x6c, 0x67, 0x6f, 0x72,
7520+
0x69, 0x74, 0x68, 0x6d, 0x2e, 0x20, 0x20, 0x54,
7521+
0x68, 0x69, 0x73, 0x2a, 0x54, 0x01, 0x02, 0x66,
7522+
0x69, 0x6e, 0x65, 0x73, 0x94, 0x06, 0x05, 0x61,
7523+
0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x76,
7524+
0x0a, 0x6f, 0x66, 0x88, 0x02, 0x60, 0x09, 0x27,
7525+
0xf0, 0x00, 0x0c, 0x20, 0x75, 0x73, 0x65, 0x64,
7526+
0x20, 0x69, 0x6e, 0x20, 0x55, 0x42, 0x49, 0x46,
7527+
0x53, 0x2e, 0x11, 0x00, 0x00 },
7528+
.output = "This document describes a compression method based on the LZO "
7529+
"compression algorithm. This document defines the application of "
7530+
"the LZO algorithm used in UBIFS.",
7531+
}, {
7532+
.inlen = 46,
7533+
.outlen = 70,
7534+
.input = { 0x00, 0x0d, 0x4a, 0x6f, 0x69, 0x6e, 0x20, 0x75,
7535+
0x73, 0x20, 0x6e, 0x6f, 0x77, 0x20, 0x61, 0x6e,
7536+
0x64, 0x20, 0x73, 0x68, 0x61, 0x72, 0x65, 0x20,
7537+
0x74, 0x68, 0x65, 0x20, 0x73, 0x6f, 0x66, 0x74,
7538+
0x77, 0x70, 0x01, 0x01, 0x4a, 0x6f, 0x69, 0x6e,
7539+
0x3d, 0x88, 0x00, 0x11, 0x00, 0x00 },
7540+
.output = "Join us now and share the software "
7541+
"Join us now and share the software ",
7542+
},
7543+
};
7544+
74637545
/*
74647546
* Michael MIC test vectors from IEEE 802.11i
74657547
*/

0 commit comments

Comments
 (0)