deflate.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  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. #define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION
  37. #define DEFLATE_DEF_WINBITS 11
  38. #define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
  39. struct deflate_ctx {
  40. struct z_stream_s comp_stream;
  41. struct z_stream_s decomp_stream;
  42. };
  43. static int deflate_comp_init(struct deflate_ctx *ctx)
  44. {
  45. int ret = 0;
  46. struct z_stream_s *stream = &ctx->comp_stream;
  47. stream->workspace = vmalloc(zlib_deflate_workspacesize());
  48. if (!stream->workspace ) {
  49. ret = -ENOMEM;
  50. goto out;
  51. }
  52. memset(stream->workspace, 0, zlib_deflate_workspacesize());
  53. ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
  54. -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
  55. Z_DEFAULT_STRATEGY);
  56. if (ret != Z_OK) {
  57. ret = -EINVAL;
  58. goto out_free;
  59. }
  60. out:
  61. return ret;
  62. out_free:
  63. vfree(stream->workspace);
  64. goto out;
  65. }
  66. static int deflate_decomp_init(struct deflate_ctx *ctx)
  67. {
  68. int ret = 0;
  69. struct z_stream_s *stream = &ctx->decomp_stream;
  70. stream->workspace = kzalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
  71. if (!stream->workspace ) {
  72. ret = -ENOMEM;
  73. goto out;
  74. }
  75. ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
  76. if (ret != Z_OK) {
  77. ret = -EINVAL;
  78. goto out_free;
  79. }
  80. out:
  81. return ret;
  82. out_free:
  83. kfree(stream->workspace);
  84. goto out;
  85. }
  86. static void deflate_comp_exit(struct deflate_ctx *ctx)
  87. {
  88. zlib_deflateEnd(&ctx->comp_stream);
  89. vfree(ctx->comp_stream.workspace);
  90. }
  91. static void deflate_decomp_exit(struct deflate_ctx *ctx)
  92. {
  93. zlib_inflateEnd(&ctx->decomp_stream);
  94. kfree(ctx->decomp_stream.workspace);
  95. }
  96. static int deflate_init(void *ctx)
  97. {
  98. int ret;
  99. ret = deflate_comp_init(ctx);
  100. if (ret)
  101. goto out;
  102. ret = deflate_decomp_init(ctx);
  103. if (ret)
  104. deflate_comp_exit(ctx);
  105. out:
  106. return ret;
  107. }
  108. static void deflate_exit(void *ctx)
  109. {
  110. deflate_comp_exit(ctx);
  111. deflate_decomp_exit(ctx);
  112. }
  113. static int deflate_compress(void *ctx, const u8 *src, unsigned int slen,
  114. u8 *dst, unsigned int *dlen)
  115. {
  116. int ret = 0;
  117. struct deflate_ctx *dctx = ctx;
  118. struct z_stream_s *stream = &dctx->comp_stream;
  119. ret = zlib_deflateReset(stream);
  120. if (ret != Z_OK) {
  121. ret = -EINVAL;
  122. goto out;
  123. }
  124. stream->next_in = (u8 *)src;
  125. stream->avail_in = slen;
  126. stream->next_out = (u8 *)dst;
  127. stream->avail_out = *dlen;
  128. ret = zlib_deflate(stream, Z_FINISH);
  129. if (ret != Z_STREAM_END) {
  130. ret = -EINVAL;
  131. goto out;
  132. }
  133. ret = 0;
  134. *dlen = stream->total_out;
  135. out:
  136. return ret;
  137. }
  138. static int deflate_decompress(void *ctx, const u8 *src, unsigned int slen,
  139. u8 *dst, unsigned int *dlen)
  140. {
  141. int ret = 0;
  142. struct deflate_ctx *dctx = ctx;
  143. struct z_stream_s *stream = &dctx->decomp_stream;
  144. ret = zlib_inflateReset(stream);
  145. if (ret != Z_OK) {
  146. ret = -EINVAL;
  147. goto out;
  148. }
  149. stream->next_in = (u8 *)src;
  150. stream->avail_in = slen;
  151. stream->next_out = (u8 *)dst;
  152. stream->avail_out = *dlen;
  153. ret = zlib_inflate(stream, Z_SYNC_FLUSH);
  154. /*
  155. * Work around a bug in zlib, which sometimes wants to taste an extra
  156. * byte when being used in the (undocumented) raw deflate mode.
  157. * (From USAGI).
  158. */
  159. if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
  160. u8 zerostuff = 0;
  161. stream->next_in = &zerostuff;
  162. stream->avail_in = 1;
  163. ret = zlib_inflate(stream, Z_FINISH);
  164. }
  165. if (ret != Z_STREAM_END) {
  166. ret = -EINVAL;
  167. goto out;
  168. }
  169. ret = 0;
  170. *dlen = stream->total_out;
  171. out:
  172. return ret;
  173. }
  174. static struct crypto_alg alg = {
  175. .cra_name = "deflate",
  176. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  177. .cra_ctxsize = sizeof(struct deflate_ctx),
  178. .cra_module = THIS_MODULE,
  179. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  180. .cra_u = { .compress = {
  181. .coa_init = deflate_init,
  182. .coa_exit = deflate_exit,
  183. .coa_compress = deflate_compress,
  184. .coa_decompress = deflate_decompress } }
  185. };
  186. static int __init init(void)
  187. {
  188. return crypto_register_alg(&alg);
  189. }
  190. static void __exit fini(void)
  191. {
  192. crypto_unregister_alg(&alg);
  193. }
  194. module_init(init);
  195. module_exit(fini);
  196. MODULE_LICENSE("GPL");
  197. MODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP");
  198. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");