deflate.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <crypto/internal/scompress.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 = vzalloc(zlib_deflate_workspacesize(
  48. -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL));
  49. if (!stream->workspace) {
  50. ret = -ENOMEM;
  51. goto out;
  52. }
  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 = vzalloc(zlib_inflate_workspacesize());
  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. vfree(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. vfree(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_alloc_ctx(struct crypto_scomp *tfm)
  109. {
  110. struct deflate_ctx *ctx;
  111. int ret;
  112. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  113. if (!ctx)
  114. return ERR_PTR(-ENOMEM);
  115. ret = __deflate_init(ctx);
  116. if (ret) {
  117. kfree(ctx);
  118. return ERR_PTR(ret);
  119. }
  120. return ctx;
  121. }
  122. static int deflate_init(struct crypto_tfm *tfm)
  123. {
  124. struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
  125. return __deflate_init(ctx);
  126. }
  127. static void __deflate_exit(void *ctx)
  128. {
  129. deflate_comp_exit(ctx);
  130. deflate_decomp_exit(ctx);
  131. }
  132. static void deflate_free_ctx(struct crypto_scomp *tfm, void *ctx)
  133. {
  134. __deflate_exit(ctx);
  135. kzfree(ctx);
  136. }
  137. static void deflate_exit(struct crypto_tfm *tfm)
  138. {
  139. struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
  140. __deflate_exit(ctx);
  141. }
  142. static int __deflate_compress(const u8 *src, unsigned int slen,
  143. u8 *dst, unsigned int *dlen, void *ctx)
  144. {
  145. int ret = 0;
  146. struct deflate_ctx *dctx = ctx;
  147. struct z_stream_s *stream = &dctx->comp_stream;
  148. ret = zlib_deflateReset(stream);
  149. if (ret != Z_OK) {
  150. ret = -EINVAL;
  151. goto out;
  152. }
  153. stream->next_in = (u8 *)src;
  154. stream->avail_in = slen;
  155. stream->next_out = (u8 *)dst;
  156. stream->avail_out = *dlen;
  157. ret = zlib_deflate(stream, Z_FINISH);
  158. if (ret != Z_STREAM_END) {
  159. ret = -EINVAL;
  160. goto out;
  161. }
  162. ret = 0;
  163. *dlen = stream->total_out;
  164. out:
  165. return ret;
  166. }
  167. static int deflate_compress(struct crypto_tfm *tfm, const u8 *src,
  168. unsigned int slen, u8 *dst, unsigned int *dlen)
  169. {
  170. struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
  171. return __deflate_compress(src, slen, dst, dlen, dctx);
  172. }
  173. static int deflate_scompress(struct crypto_scomp *tfm, const u8 *src,
  174. unsigned int slen, u8 *dst, unsigned int *dlen,
  175. void *ctx)
  176. {
  177. return __deflate_compress(src, slen, dst, dlen, ctx);
  178. }
  179. static int __deflate_decompress(const u8 *src, unsigned int slen,
  180. u8 *dst, unsigned int *dlen, void *ctx)
  181. {
  182. int ret = 0;
  183. struct deflate_ctx *dctx = ctx;
  184. struct z_stream_s *stream = &dctx->decomp_stream;
  185. ret = zlib_inflateReset(stream);
  186. if (ret != Z_OK) {
  187. ret = -EINVAL;
  188. goto out;
  189. }
  190. stream->next_in = (u8 *)src;
  191. stream->avail_in = slen;
  192. stream->next_out = (u8 *)dst;
  193. stream->avail_out = *dlen;
  194. ret = zlib_inflate(stream, Z_SYNC_FLUSH);
  195. /*
  196. * Work around a bug in zlib, which sometimes wants to taste an extra
  197. * byte when being used in the (undocumented) raw deflate mode.
  198. * (From USAGI).
  199. */
  200. if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
  201. u8 zerostuff = 0;
  202. stream->next_in = &zerostuff;
  203. stream->avail_in = 1;
  204. ret = zlib_inflate(stream, Z_FINISH);
  205. }
  206. if (ret != Z_STREAM_END) {
  207. ret = -EINVAL;
  208. goto out;
  209. }
  210. ret = 0;
  211. *dlen = stream->total_out;
  212. out:
  213. return ret;
  214. }
  215. static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src,
  216. unsigned int slen, u8 *dst, unsigned int *dlen)
  217. {
  218. struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
  219. return __deflate_decompress(src, slen, dst, dlen, dctx);
  220. }
  221. static int deflate_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  222. unsigned int slen, u8 *dst, unsigned int *dlen,
  223. void *ctx)
  224. {
  225. return __deflate_decompress(src, slen, dst, dlen, ctx);
  226. }
  227. static struct crypto_alg alg = {
  228. .cra_name = "deflate",
  229. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  230. .cra_ctxsize = sizeof(struct deflate_ctx),
  231. .cra_module = THIS_MODULE,
  232. .cra_init = deflate_init,
  233. .cra_exit = deflate_exit,
  234. .cra_u = { .compress = {
  235. .coa_compress = deflate_compress,
  236. .coa_decompress = deflate_decompress } }
  237. };
  238. static struct scomp_alg scomp = {
  239. .alloc_ctx = deflate_alloc_ctx,
  240. .free_ctx = deflate_free_ctx,
  241. .compress = deflate_scompress,
  242. .decompress = deflate_sdecompress,
  243. .base = {
  244. .cra_name = "deflate",
  245. .cra_driver_name = "deflate-scomp",
  246. .cra_module = THIS_MODULE,
  247. }
  248. };
  249. static int __init deflate_mod_init(void)
  250. {
  251. int ret;
  252. ret = crypto_register_alg(&alg);
  253. if (ret)
  254. return ret;
  255. ret = crypto_register_scomp(&scomp);
  256. if (ret) {
  257. crypto_unregister_alg(&alg);
  258. return ret;
  259. }
  260. return ret;
  261. }
  262. static void __exit deflate_mod_fini(void)
  263. {
  264. crypto_unregister_alg(&alg);
  265. crypto_unregister_scomp(&scomp);
  266. }
  267. module_init(deflate_mod_init);
  268. module_exit(deflate_mod_fini);
  269. MODULE_LICENSE("GPL");
  270. MODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP");
  271. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
  272. MODULE_ALIAS_CRYPTO("deflate");