0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include <linux/init.h>
0024 #include <linux/module.h>
0025 #include <linux/crypto.h>
0026 #include <linux/zlib.h>
0027 #include <linux/vmalloc.h>
0028 #include <linux/interrupt.h>
0029 #include <linux/mm.h>
0030 #include <linux/net.h>
0031 #include <crypto/internal/scompress.h>
0032
0033 #define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION
0034 #define DEFLATE_DEF_WINBITS 11
0035 #define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
0036
0037 struct deflate_ctx {
0038 struct z_stream_s comp_stream;
0039 struct z_stream_s decomp_stream;
0040 };
0041
0042 static int deflate_comp_init(struct deflate_ctx *ctx, int format)
0043 {
0044 int ret = 0;
0045 struct z_stream_s *stream = &ctx->comp_stream;
0046
0047 stream->workspace = vzalloc(zlib_deflate_workspacesize(
0048 MAX_WBITS, MAX_MEM_LEVEL));
0049 if (!stream->workspace) {
0050 ret = -ENOMEM;
0051 goto out;
0052 }
0053 if (format)
0054 ret = zlib_deflateInit(stream, 3);
0055 else
0056 ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
0057 -DEFLATE_DEF_WINBITS,
0058 DEFLATE_DEF_MEMLEVEL,
0059 Z_DEFAULT_STRATEGY);
0060 if (ret != Z_OK) {
0061 ret = -EINVAL;
0062 goto out_free;
0063 }
0064 out:
0065 return ret;
0066 out_free:
0067 vfree(stream->workspace);
0068 goto out;
0069 }
0070
0071 static int deflate_decomp_init(struct deflate_ctx *ctx, int format)
0072 {
0073 int ret = 0;
0074 struct z_stream_s *stream = &ctx->decomp_stream;
0075
0076 stream->workspace = vzalloc(zlib_inflate_workspacesize());
0077 if (!stream->workspace) {
0078 ret = -ENOMEM;
0079 goto out;
0080 }
0081 if (format)
0082 ret = zlib_inflateInit(stream);
0083 else
0084 ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
0085 if (ret != Z_OK) {
0086 ret = -EINVAL;
0087 goto out_free;
0088 }
0089 out:
0090 return ret;
0091 out_free:
0092 vfree(stream->workspace);
0093 goto out;
0094 }
0095
0096 static void deflate_comp_exit(struct deflate_ctx *ctx)
0097 {
0098 zlib_deflateEnd(&ctx->comp_stream);
0099 vfree(ctx->comp_stream.workspace);
0100 }
0101
0102 static void deflate_decomp_exit(struct deflate_ctx *ctx)
0103 {
0104 zlib_inflateEnd(&ctx->decomp_stream);
0105 vfree(ctx->decomp_stream.workspace);
0106 }
0107
0108 static int __deflate_init(void *ctx, int format)
0109 {
0110 int ret;
0111
0112 ret = deflate_comp_init(ctx, format);
0113 if (ret)
0114 goto out;
0115 ret = deflate_decomp_init(ctx, format);
0116 if (ret)
0117 deflate_comp_exit(ctx);
0118 out:
0119 return ret;
0120 }
0121
0122 static void *gen_deflate_alloc_ctx(struct crypto_scomp *tfm, int format)
0123 {
0124 struct deflate_ctx *ctx;
0125 int ret;
0126
0127 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
0128 if (!ctx)
0129 return ERR_PTR(-ENOMEM);
0130
0131 ret = __deflate_init(ctx, format);
0132 if (ret) {
0133 kfree(ctx);
0134 return ERR_PTR(ret);
0135 }
0136
0137 return ctx;
0138 }
0139
0140 static void *deflate_alloc_ctx(struct crypto_scomp *tfm)
0141 {
0142 return gen_deflate_alloc_ctx(tfm, 0);
0143 }
0144
0145 static void *zlib_deflate_alloc_ctx(struct crypto_scomp *tfm)
0146 {
0147 return gen_deflate_alloc_ctx(tfm, 1);
0148 }
0149
0150 static int deflate_init(struct crypto_tfm *tfm)
0151 {
0152 struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
0153
0154 return __deflate_init(ctx, 0);
0155 }
0156
0157 static void __deflate_exit(void *ctx)
0158 {
0159 deflate_comp_exit(ctx);
0160 deflate_decomp_exit(ctx);
0161 }
0162
0163 static void deflate_free_ctx(struct crypto_scomp *tfm, void *ctx)
0164 {
0165 __deflate_exit(ctx);
0166 kfree_sensitive(ctx);
0167 }
0168
0169 static void deflate_exit(struct crypto_tfm *tfm)
0170 {
0171 struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
0172
0173 __deflate_exit(ctx);
0174 }
0175
0176 static int __deflate_compress(const u8 *src, unsigned int slen,
0177 u8 *dst, unsigned int *dlen, void *ctx)
0178 {
0179 int ret = 0;
0180 struct deflate_ctx *dctx = ctx;
0181 struct z_stream_s *stream = &dctx->comp_stream;
0182
0183 ret = zlib_deflateReset(stream);
0184 if (ret != Z_OK) {
0185 ret = -EINVAL;
0186 goto out;
0187 }
0188
0189 stream->next_in = (u8 *)src;
0190 stream->avail_in = slen;
0191 stream->next_out = (u8 *)dst;
0192 stream->avail_out = *dlen;
0193
0194 ret = zlib_deflate(stream, Z_FINISH);
0195 if (ret != Z_STREAM_END) {
0196 ret = -EINVAL;
0197 goto out;
0198 }
0199 ret = 0;
0200 *dlen = stream->total_out;
0201 out:
0202 return ret;
0203 }
0204
0205 static int deflate_compress(struct crypto_tfm *tfm, const u8 *src,
0206 unsigned int slen, u8 *dst, unsigned int *dlen)
0207 {
0208 struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
0209
0210 return __deflate_compress(src, slen, dst, dlen, dctx);
0211 }
0212
0213 static int deflate_scompress(struct crypto_scomp *tfm, const u8 *src,
0214 unsigned int slen, u8 *dst, unsigned int *dlen,
0215 void *ctx)
0216 {
0217 return __deflate_compress(src, slen, dst, dlen, ctx);
0218 }
0219
0220 static int __deflate_decompress(const u8 *src, unsigned int slen,
0221 u8 *dst, unsigned int *dlen, void *ctx)
0222 {
0223
0224 int ret = 0;
0225 struct deflate_ctx *dctx = ctx;
0226 struct z_stream_s *stream = &dctx->decomp_stream;
0227
0228 ret = zlib_inflateReset(stream);
0229 if (ret != Z_OK) {
0230 ret = -EINVAL;
0231 goto out;
0232 }
0233
0234 stream->next_in = (u8 *)src;
0235 stream->avail_in = slen;
0236 stream->next_out = (u8 *)dst;
0237 stream->avail_out = *dlen;
0238
0239 ret = zlib_inflate(stream, Z_SYNC_FLUSH);
0240
0241
0242
0243
0244
0245 if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
0246 u8 zerostuff = 0;
0247 stream->next_in = &zerostuff;
0248 stream->avail_in = 1;
0249 ret = zlib_inflate(stream, Z_FINISH);
0250 }
0251 if (ret != Z_STREAM_END) {
0252 ret = -EINVAL;
0253 goto out;
0254 }
0255 ret = 0;
0256 *dlen = stream->total_out;
0257 out:
0258 return ret;
0259 }
0260
0261 static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src,
0262 unsigned int slen, u8 *dst, unsigned int *dlen)
0263 {
0264 struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
0265
0266 return __deflate_decompress(src, slen, dst, dlen, dctx);
0267 }
0268
0269 static int deflate_sdecompress(struct crypto_scomp *tfm, const u8 *src,
0270 unsigned int slen, u8 *dst, unsigned int *dlen,
0271 void *ctx)
0272 {
0273 return __deflate_decompress(src, slen, dst, dlen, ctx);
0274 }
0275
0276 static struct crypto_alg alg = {
0277 .cra_name = "deflate",
0278 .cra_driver_name = "deflate-generic",
0279 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
0280 .cra_ctxsize = sizeof(struct deflate_ctx),
0281 .cra_module = THIS_MODULE,
0282 .cra_init = deflate_init,
0283 .cra_exit = deflate_exit,
0284 .cra_u = { .compress = {
0285 .coa_compress = deflate_compress,
0286 .coa_decompress = deflate_decompress } }
0287 };
0288
0289 static struct scomp_alg scomp[] = { {
0290 .alloc_ctx = deflate_alloc_ctx,
0291 .free_ctx = deflate_free_ctx,
0292 .compress = deflate_scompress,
0293 .decompress = deflate_sdecompress,
0294 .base = {
0295 .cra_name = "deflate",
0296 .cra_driver_name = "deflate-scomp",
0297 .cra_module = THIS_MODULE,
0298 }
0299 }, {
0300 .alloc_ctx = zlib_deflate_alloc_ctx,
0301 .free_ctx = deflate_free_ctx,
0302 .compress = deflate_scompress,
0303 .decompress = deflate_sdecompress,
0304 .base = {
0305 .cra_name = "zlib-deflate",
0306 .cra_driver_name = "zlib-deflate-scomp",
0307 .cra_module = THIS_MODULE,
0308 }
0309 } };
0310
0311 static int __init deflate_mod_init(void)
0312 {
0313 int ret;
0314
0315 ret = crypto_register_alg(&alg);
0316 if (ret)
0317 return ret;
0318
0319 ret = crypto_register_scomps(scomp, ARRAY_SIZE(scomp));
0320 if (ret) {
0321 crypto_unregister_alg(&alg);
0322 return ret;
0323 }
0324
0325 return ret;
0326 }
0327
0328 static void __exit deflate_mod_fini(void)
0329 {
0330 crypto_unregister_alg(&alg);
0331 crypto_unregister_scomps(scomp, ARRAY_SIZE(scomp));
0332 }
0333
0334 subsys_initcall(deflate_mod_init);
0335 module_exit(deflate_mod_fini);
0336
0337 MODULE_LICENSE("GPL");
0338 MODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP");
0339 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
0340 MODULE_ALIAS_CRYPTO("deflate");