blob: 168df784da8491e2daccf5b79316fb2191b46e6d [file] [log] [blame]
Zoltan Sogor0b77abb2007-12-07 16:53:23 +08001/*
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>
Eric Dumazet42614b02014-05-27 10:28:55 -070023#include <linux/mm.h>
Zoltan Sogor0b77abb2007-12-07 16:53:23 +080024#include <linux/lzo.h>
Giovanni Cabidduac9d2c42016-10-21 13:19:49 +010025#include <crypto/internal/scompress.h>
Zoltan Sogor0b77abb2007-12-07 16:53:23 +080026
27struct lzo_ctx {
28 void *lzo_comp_mem;
29};
30
Giovanni Cabidduac9d2c42016-10-21 13:19:49 +010031static void *lzo_alloc_ctx(struct crypto_scomp *tfm)
32{
33 void *ctx;
34
35 ctx = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL | __GFP_NOWARN);
36 if (!ctx)
37 ctx = vmalloc(LZO1X_MEM_COMPRESS);
38 if (!ctx)
39 return ERR_PTR(-ENOMEM);
40
41 return ctx;
42}
43
Zoltan Sogor0b77abb2007-12-07 16:53:23 +080044static int lzo_init(struct crypto_tfm *tfm)
45{
46 struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
47
Giovanni Cabidduac9d2c42016-10-21 13:19:49 +010048 ctx->lzo_comp_mem = lzo_alloc_ctx(NULL);
49 if (IS_ERR(ctx->lzo_comp_mem))
Zoltan Sogor0b77abb2007-12-07 16:53:23 +080050 return -ENOMEM;
51
52 return 0;
53}
54
Giovanni Cabidduac9d2c42016-10-21 13:19:49 +010055static void lzo_free_ctx(struct crypto_scomp *tfm, void *ctx)
56{
57 kvfree(ctx);
58}
59
Zoltan Sogor0b77abb2007-12-07 16:53:23 +080060static void lzo_exit(struct crypto_tfm *tfm)
61{
62 struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
63
Giovanni Cabidduac9d2c42016-10-21 13:19:49 +010064 lzo_free_ctx(NULL, ctx->lzo_comp_mem);
Zoltan Sogor0b77abb2007-12-07 16:53:23 +080065}
66
Giovanni Cabidduac9d2c42016-10-21 13:19:49 +010067static int __lzo_compress(const u8 *src, unsigned int slen,
68 u8 *dst, unsigned int *dlen, void *ctx)
Zoltan Sogor0b77abb2007-12-07 16:53:23 +080069{
Zoltan Sogor0b77abb2007-12-07 16:53:23 +080070 size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
71 int err;
72
Giovanni Cabidduac9d2c42016-10-21 13:19:49 +010073 err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx);
Zoltan Sogor0b77abb2007-12-07 16:53:23 +080074
75 if (err != LZO_E_OK)
76 return -EINVAL;
77
78 *dlen = tmp_len;
79 return 0;
80}
81
Giovanni Cabidduac9d2c42016-10-21 13:19:49 +010082static int lzo_compress(struct crypto_tfm *tfm, const u8 *src,
83 unsigned int slen, u8 *dst, unsigned int *dlen)
84{
85 struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
86
87 return __lzo_compress(src, slen, dst, dlen, ctx->lzo_comp_mem);
88}
89
90static int lzo_scompress(struct crypto_scomp *tfm, const u8 *src,
91 unsigned int slen, u8 *dst, unsigned int *dlen,
92 void *ctx)
93{
94 return __lzo_compress(src, slen, dst, dlen, ctx);
95}
96
97static int __lzo_decompress(const u8 *src, unsigned int slen,
98 u8 *dst, unsigned int *dlen)
Zoltan Sogor0b77abb2007-12-07 16:53:23 +080099{
100 int err;
101 size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
102
103 err = lzo1x_decompress_safe(src, slen, dst, &tmp_len);
104
105 if (err != LZO_E_OK)
106 return -EINVAL;
107
108 *dlen = tmp_len;
109 return 0;
Giovanni Cabidduac9d2c42016-10-21 13:19:49 +0100110}
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800111
Giovanni Cabidduac9d2c42016-10-21 13:19:49 +0100112static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src,
113 unsigned int slen, u8 *dst, unsigned int *dlen)
114{
115 return __lzo_decompress(src, slen, dst, dlen);
116}
117
118static int lzo_sdecompress(struct crypto_scomp *tfm, const u8 *src,
119 unsigned int slen, u8 *dst, unsigned int *dlen,
120 void *ctx)
121{
122 return __lzo_decompress(src, slen, dst, dlen);
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800123}
124
125static struct crypto_alg alg = {
126 .cra_name = "lzo",
127 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
128 .cra_ctxsize = sizeof(struct lzo_ctx),
129 .cra_module = THIS_MODULE,
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800130 .cra_init = lzo_init,
131 .cra_exit = lzo_exit,
132 .cra_u = { .compress = {
Giovanni Cabidduac9d2c42016-10-21 13:19:49 +0100133 .coa_compress = lzo_compress,
134 .coa_decompress = lzo_decompress } }
135};
136
137static struct scomp_alg scomp = {
138 .alloc_ctx = lzo_alloc_ctx,
139 .free_ctx = lzo_free_ctx,
140 .compress = lzo_scompress,
141 .decompress = lzo_sdecompress,
142 .base = {
143 .cra_name = "lzo",
144 .cra_driver_name = "lzo-scomp",
145 .cra_module = THIS_MODULE,
146 }
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800147};
148
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800149static int __init lzo_mod_init(void)
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800150{
Giovanni Cabidduac9d2c42016-10-21 13:19:49 +0100151 int ret;
152
153 ret = crypto_register_alg(&alg);
154 if (ret)
155 return ret;
156
157 ret = crypto_register_scomp(&scomp);
158 if (ret) {
159 crypto_unregister_alg(&alg);
160 return ret;
161 }
162
163 return ret;
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800164}
165
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800166static void __exit lzo_mod_fini(void)
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800167{
168 crypto_unregister_alg(&alg);
Giovanni Cabidduac9d2c42016-10-21 13:19:49 +0100169 crypto_unregister_scomp(&scomp);
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800170}
171
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800172module_init(lzo_mod_init);
173module_exit(lzo_mod_fini);
Zoltan Sogor0b77abb2007-12-07 16:53:23 +0800174
175MODULE_LICENSE("GPL");
176MODULE_DESCRIPTION("LZO Compression Algorithm");
Kees Cook5d26a102014-11-20 17:05:53 -0800177MODULE_ALIAS_CRYPTO("lzo");