blob: f209368d62aefab5c1c1998f2af20159d5323d6b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
4 * Deflate algorithm (RFC 1951), implemented here primarily for use
5 * by IPCOMP (RFC 3173 & RFC 2394).
6 *
7 * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 * FIXME: deflate transforms will require up to a total of about 436k of kernel
15 * memory on i386 (390k for compression, the rest for decompression), as the
16 * current zlib kernel code uses a worst case pre-allocation system by default.
17 * This needs to be fixed so that the amount of memory required is properly
18 * related to the winbits and memlevel parameters.
19 *
20 * The default winbits of 11 should suit most packets, and it may be something
21 * to configure on a per-tfm basis in the future.
22 *
23 * Currently, compression history is not maintained between tfm calls, as
24 * it is not needed for IPCOMP and keeps the code simpler. It can be
25 * implemented if someone wants it.
26 */
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/crypto.h>
30#include <linux/zlib.h>
31#include <linux/vmalloc.h>
32#include <linux/interrupt.h>
33#include <linux/mm.h>
34#include <linux/net.h>
35#include <linux/slab.h>
36
37#define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION
38#define DEFLATE_DEF_WINBITS 11
39#define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
40
41struct deflate_ctx {
42 struct z_stream_s comp_stream;
43 struct z_stream_s decomp_stream;
44};
45
46static int deflate_comp_init(struct deflate_ctx *ctx)
47{
48 int ret = 0;
49 struct z_stream_s *stream = &ctx->comp_stream;
50
51 stream->workspace = vmalloc(zlib_deflate_workspacesize());
52 if (!stream->workspace ) {
53 ret = -ENOMEM;
54 goto out;
55 }
56 memset(stream->workspace, 0, zlib_deflate_workspacesize());
57 ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
58 -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
59 Z_DEFAULT_STRATEGY);
60 if (ret != Z_OK) {
61 ret = -EINVAL;
62 goto out_free;
63 }
64out:
65 return ret;
66out_free:
67 vfree(stream->workspace);
68 goto out;
69}
70
71static int deflate_decomp_init(struct deflate_ctx *ctx)
72{
73 int ret = 0;
74 struct z_stream_s *stream = &ctx->decomp_stream;
75
Eric Sesterhennbbeb5632006-03-06 21:42:07 +110076 stream->workspace = kzalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (!stream->workspace ) {
78 ret = -ENOMEM;
79 goto out;
80 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
82 if (ret != Z_OK) {
83 ret = -EINVAL;
84 goto out_free;
85 }
86out:
87 return ret;
88out_free:
89 kfree(stream->workspace);
90 goto out;
91}
92
93static void deflate_comp_exit(struct deflate_ctx *ctx)
94{
Artem B. Bityuckiy9ffb7142005-04-16 15:23:58 -070095 zlib_deflateEnd(&ctx->comp_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 vfree(ctx->comp_stream.workspace);
97}
98
99static void deflate_decomp_exit(struct deflate_ctx *ctx)
100{
Artem B. Bityuckiy9ffb7142005-04-16 15:23:58 -0700101 zlib_inflateEnd(&ctx->decomp_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 kfree(ctx->decomp_stream.workspace);
103}
104
105static int deflate_init(void *ctx)
106{
107 int ret;
108
109 ret = deflate_comp_init(ctx);
110 if (ret)
111 goto out;
112 ret = deflate_decomp_init(ctx);
113 if (ret)
114 deflate_comp_exit(ctx);
115out:
116 return ret;
117}
118
119static void deflate_exit(void *ctx)
120{
121 deflate_comp_exit(ctx);
122 deflate_decomp_exit(ctx);
123}
124
125static int deflate_compress(void *ctx, const u8 *src, unsigned int slen,
126 u8 *dst, unsigned int *dlen)
127{
128 int ret = 0;
129 struct deflate_ctx *dctx = ctx;
130 struct z_stream_s *stream = &dctx->comp_stream;
131
132 ret = zlib_deflateReset(stream);
133 if (ret != Z_OK) {
134 ret = -EINVAL;
135 goto out;
136 }
137
138 stream->next_in = (u8 *)src;
139 stream->avail_in = slen;
140 stream->next_out = (u8 *)dst;
141 stream->avail_out = *dlen;
142
143 ret = zlib_deflate(stream, Z_FINISH);
144 if (ret != Z_STREAM_END) {
145 ret = -EINVAL;
146 goto out;
147 }
148 ret = 0;
149 *dlen = stream->total_out;
150out:
151 return ret;
152}
153
154static int deflate_decompress(void *ctx, const u8 *src, unsigned int slen,
155 u8 *dst, unsigned int *dlen)
156{
157
158 int ret = 0;
159 struct deflate_ctx *dctx = ctx;
160 struct z_stream_s *stream = &dctx->decomp_stream;
161
162 ret = zlib_inflateReset(stream);
163 if (ret != Z_OK) {
164 ret = -EINVAL;
165 goto out;
166 }
167
168 stream->next_in = (u8 *)src;
169 stream->avail_in = slen;
170 stream->next_out = (u8 *)dst;
171 stream->avail_out = *dlen;
172
173 ret = zlib_inflate(stream, Z_SYNC_FLUSH);
174 /*
175 * Work around a bug in zlib, which sometimes wants to taste an extra
176 * byte when being used in the (undocumented) raw deflate mode.
177 * (From USAGI).
178 */
179 if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
180 u8 zerostuff = 0;
181 stream->next_in = &zerostuff;
182 stream->avail_in = 1;
183 ret = zlib_inflate(stream, Z_FINISH);
184 }
185 if (ret != Z_STREAM_END) {
186 ret = -EINVAL;
187 goto out;
188 }
189 ret = 0;
190 *dlen = stream->total_out;
191out:
192 return ret;
193}
194
195static struct crypto_alg alg = {
196 .cra_name = "deflate",
197 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
198 .cra_ctxsize = sizeof(struct deflate_ctx),
199 .cra_module = THIS_MODULE,
200 .cra_list = LIST_HEAD_INIT(alg.cra_list),
201 .cra_u = { .compress = {
202 .coa_init = deflate_init,
203 .coa_exit = deflate_exit,
204 .coa_compress = deflate_compress,
205 .coa_decompress = deflate_decompress } }
206};
207
208static int __init init(void)
209{
210 return crypto_register_alg(&alg);
211}
212
213static void __exit fini(void)
214{
215 crypto_unregister_alg(&alg);
216}
217
218module_init(init);
219module_exit(fini);
220
221MODULE_LICENSE("GPL");
222MODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP");
223MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
224