deflate.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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(struct crypto_tfm *tfm)
  97. {
  98. struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
  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(struct crypto_tfm *tfm)
  110. {
  111. struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
  112. deflate_comp_exit(ctx);
  113. deflate_decomp_exit(ctx);
  114. }
  115. static int deflate_compress(struct crypto_tfm *tfm, const u8 *src,
  116. unsigned int slen, u8 *dst, unsigned int *dlen)
  117. {
  118. int ret = 0;
  119. struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
  120. struct z_stream_s *stream = &dctx->comp_stream;
  121. ret = zlib_deflateReset(stream);
  122. if (ret != Z_OK) {
  123. ret = -EINVAL;
  124. goto out;
  125. }
  126. stream->next_in = (u8 *)src;
  127. stream->avail_in = slen;
  128. stream->next_out = (u8 *)dst;
  129. stream->avail_out = *dlen;
  130. ret = zlib_deflate(stream, Z_FINISH);
  131. if (ret != Z_STREAM_END) {
  132. ret = -EINVAL;
  133. goto out;
  134. }
  135. ret = 0;
  136. *dlen = stream->total_out;
  137. out:
  138. return ret;
  139. }
  140. static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src,
  141. unsigned int slen, u8 *dst, unsigned int *dlen)
  142. {
  143. int ret = 0;
  144. struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
  145. struct z_stream_s *stream = &dctx->decomp_stream;
  146. ret = zlib_inflateReset(stream);
  147. if (ret != Z_OK) {
  148. ret = -EINVAL;
  149. goto out;
  150. }
  151. stream->next_in = (u8 *)src;
  152. stream->avail_in = slen;
  153. stream->next_out = (u8 *)dst;
  154. stream->avail_out = *dlen;
  155. ret = zlib_inflate(stream, Z_SYNC_FLUSH);
  156. /*
  157. * Work around a bug in zlib, which sometimes wants to taste an extra
  158. * byte when being used in the (undocumented) raw deflate mode.
  159. * (From USAGI).
  160. */
  161. if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
  162. u8 zerostuff = 0;
  163. stream->next_in = &zerostuff;
  164. stream->avail_in = 1;
  165. ret = zlib_inflate(stream, Z_FINISH);
  166. }
  167. if (ret != Z_STREAM_END) {
  168. ret = -EINVAL;
  169. goto out;
  170. }
  171. ret = 0;
  172. *dlen = stream->total_out;
  173. out:
  174. return ret;
  175. }
  176. static struct crypto_alg alg = {
  177. .cra_name = "deflate",
  178. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  179. .cra_ctxsize = sizeof(struct deflate_ctx),
  180. .cra_module = THIS_MODULE,
  181. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  182. .cra_init = deflate_init,
  183. .cra_exit = deflate_exit,
  184. .cra_u = { .compress = {
  185. .coa_compress = deflate_compress,
  186. .coa_decompress = deflate_decompress } }
  187. };
  188. static int __init deflate_mod_init(void)
  189. {
  190. return crypto_register_alg(&alg);
  191. }
  192. static void __exit deflate_mod_fini(void)
  193. {
  194. crypto_unregister_alg(&alg);
  195. }
  196. module_init(deflate_mod_init);
  197. module_exit(deflate_mod_fini);
  198. MODULE_LICENSE("GPL");
  199. MODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP");
  200. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");