deflate.c 5.4 KB

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