1 // SPDX-License-Identifier: GPL-2.0-only
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/crypto.h>
9 #include <linux/vmalloc.h>
11 #include <linux/lzo.h>
12 #include <crypto/internal/scompress.h>
18 static void *lzo_alloc_ctx(struct crypto_scomp
*tfm
)
22 ctx
= kvmalloc(LZO1X_MEM_COMPRESS
, GFP_KERNEL
);
24 return ERR_PTR(-ENOMEM
);
29 static int lzo_init(struct crypto_tfm
*tfm
)
31 struct lzo_ctx
*ctx
= crypto_tfm_ctx(tfm
);
33 ctx
->lzo_comp_mem
= lzo_alloc_ctx(NULL
);
34 if (IS_ERR(ctx
->lzo_comp_mem
))
40 static void lzo_free_ctx(struct crypto_scomp
*tfm
, void *ctx
)
45 static void lzo_exit(struct crypto_tfm
*tfm
)
47 struct lzo_ctx
*ctx
= crypto_tfm_ctx(tfm
);
49 lzo_free_ctx(NULL
, ctx
->lzo_comp_mem
);
52 static int __lzo_compress(const u8
*src
, unsigned int slen
,
53 u8
*dst
, unsigned int *dlen
, void *ctx
)
55 size_t tmp_len
= *dlen
; /* size_t(ulong) <-> uint on 64 bit */
58 err
= lzo1x_1_compress(src
, slen
, dst
, &tmp_len
, ctx
);
67 static int lzo_compress(struct crypto_tfm
*tfm
, const u8
*src
,
68 unsigned int slen
, u8
*dst
, unsigned int *dlen
)
70 struct lzo_ctx
*ctx
= crypto_tfm_ctx(tfm
);
72 return __lzo_compress(src
, slen
, dst
, dlen
, ctx
->lzo_comp_mem
);
75 static int lzo_scompress(struct crypto_scomp
*tfm
, const u8
*src
,
76 unsigned int slen
, u8
*dst
, unsigned int *dlen
,
79 return __lzo_compress(src
, slen
, dst
, dlen
, ctx
);
82 static int __lzo_decompress(const u8
*src
, unsigned int slen
,
83 u8
*dst
, unsigned int *dlen
)
86 size_t tmp_len
= *dlen
; /* size_t(ulong) <-> uint on 64 bit */
88 err
= lzo1x_decompress_safe(src
, slen
, dst
, &tmp_len
);
97 static int lzo_decompress(struct crypto_tfm
*tfm
, const u8
*src
,
98 unsigned int slen
, u8
*dst
, unsigned int *dlen
)
100 return __lzo_decompress(src
, slen
, dst
, dlen
);
103 static int lzo_sdecompress(struct crypto_scomp
*tfm
, const u8
*src
,
104 unsigned int slen
, u8
*dst
, unsigned int *dlen
,
107 return __lzo_decompress(src
, slen
, dst
, dlen
);
110 static struct crypto_alg alg
= {
112 .cra_driver_name
= "lzo-generic",
113 .cra_flags
= CRYPTO_ALG_TYPE_COMPRESS
,
114 .cra_ctxsize
= sizeof(struct lzo_ctx
),
115 .cra_module
= THIS_MODULE
,
116 .cra_init
= lzo_init
,
117 .cra_exit
= lzo_exit
,
118 .cra_u
= { .compress
= {
119 .coa_compress
= lzo_compress
,
120 .coa_decompress
= lzo_decompress
} }
123 static struct scomp_alg scomp
= {
124 .alloc_ctx
= lzo_alloc_ctx
,
125 .free_ctx
= lzo_free_ctx
,
126 .compress
= lzo_scompress
,
127 .decompress
= lzo_sdecompress
,
130 .cra_driver_name
= "lzo-scomp",
131 .cra_module
= THIS_MODULE
,
135 static int __init
lzo_mod_init(void)
139 ret
= crypto_register_alg(&alg
);
143 ret
= crypto_register_scomp(&scomp
);
145 crypto_unregister_alg(&alg
);
152 static void __exit
lzo_mod_fini(void)
154 crypto_unregister_alg(&alg
);
155 crypto_unregister_scomp(&scomp
);
158 subsys_initcall(lzo_mod_init
);
159 module_exit(lzo_mod_fini
);
161 MODULE_LICENSE("GPL");
162 MODULE_DESCRIPTION("LZO Compression Algorithm");
163 MODULE_ALIAS_CRYPTO("lzo");