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. #define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION
  36. #define DEFLATE_DEF_WINBITS 11
  37. #define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
  38. struct deflate_ctx {
  39. struct z_stream_s comp_stream;
  40. struct z_stream_s decomp_stream;
  41. };
  42. static int deflate_comp_init(struct deflate_ctx *ctx)
  43. {
  44. int ret = 0;
  45. struct z_stream_s *stream = &ctx->comp_stream;
  46. stream->workspace = vzalloc(zlib_deflate_workspacesize(
  47. -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL));
  48. if (!stream->workspace) {
  49. ret = -ENOMEM;
  50. goto out;
  51. }
  52. ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
  53. -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
  54. Z_DEFAULT_STRATEGY);
  55. if (ret != Z_OK) {
  56. ret = -EINVAL;
  57. goto out_free;
  58. }
  59. out:
  60. return ret;
  61. out_free:
  62. vfree(stream->workspace);
  63. goto out;
  64. }
  65. static int deflate_decomp_init(struct deflate_ctx *ctx)
  66. {
  67. int ret = 0;
  68. struct z_stream_s *stream = &ctx->decomp_stream;
  69. stream->workspace = vzalloc(zlib_inflate_workspacesize());
  70. if (!stream->workspace) {
  71. ret = -ENOMEM;
  72. goto out;
  73. }
  74. ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
  75. if (ret != Z_OK) {
  76. ret = -EINVAL;
  77. goto out_free;
  78. }
  79. out:
  80. return ret;
  81. out_free:
  82. vfree(stream->workspace);
  83. goto out;
  84. }
  85. static void deflate_comp_exit(struct deflate_ctx *ctx)
  86. {
  87. zlib_deflateEnd(&ctx->comp_stream);
  88. vfree(ctx->comp_stream.workspace);
  89. }
  90. static void deflate_decomp_exit(struct deflate_ctx *ctx)
  91. {
  92. zlib_inflateEnd(&ctx->decomp_stream);
  93. vfree(ctx->decomp_stream.workspace);
  94. }
  95. static int deflate_init(struct crypto_tfm *tfm)
  96. {
  97. struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
  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(struct crypto_tfm *tfm)
  109. {
  110. struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
  111. deflate_comp_exit(ctx);
  112. deflate_decomp_exit(ctx);
  113. }
  114. static int deflate_compress(struct crypto_tfm *tfm, const u8 *src,
  115. unsigned int slen, u8 *dst, unsigned int *dlen)
  116. {
  117. int ret = 0;
  118. struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
  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(struct crypto_tfm *tfm, const u8 *src,
  140. unsigned int slen, u8 *dst, unsigned int *dlen)
  141. {
  142. int ret = 0;
  143. struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
  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_init = deflate_init,
  181. .cra_exit = deflate_exit,
  182. .cra_u = { .compress = {
  183. .coa_compress = deflate_compress,
  184. .coa_decompress = deflate_decompress } }
  185. };
  186. static int __init deflate_mod_init(void)
  187. {
  188. return crypto_register_alg(&alg);
  189. }
  190. static void __exit deflate_mod_fini(void)
  191. {
  192. crypto_unregister_alg(&alg);
  193. }
  194. module_init(deflate_mod_init);
  195. module_exit(deflate_mod_fini);
  196. MODULE_LICENSE("GPL");
  197. MODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP");
  198. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
  199. MODULE_ALIAS_CRYPTO("deflate");