deflate.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API.
  4. *
  5. * Deflate algorithm (RFC 1951), implemented here primarily for use
  6. * by IPCOMP (RFC 3173 & RFC 2394).
  7. *
  8. * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
  9. *
  10. * FIXME: deflate transforms will require up to a total of about 436k of kernel
  11. * memory on i386 (390k for compression, the rest for decompression), as the
  12. * current zlib kernel code uses a worst case pre-allocation system by default.
  13. * This needs to be fixed so that the amount of memory required is properly
  14. * related to the winbits and memlevel parameters.
  15. *
  16. * The default winbits of 11 should suit most packets, and it may be something
  17. * to configure on a per-tfm basis in the future.
  18. *
  19. * Currently, compression history is not maintained between tfm calls, as
  20. * it is not needed for IPCOMP and keeps the code simpler. It can be
  21. * implemented if someone wants it.
  22. */
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/crypto.h>
  26. #include <linux/zlib.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/mm.h>
  30. #include <linux/net.h>
  31. #include <crypto/internal/scompress.h>
  32. #define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION
  33. #define DEFLATE_DEF_WINBITS 11
  34. #define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
  35. struct deflate_ctx {
  36. struct z_stream_s comp_stream;
  37. struct z_stream_s decomp_stream;
  38. };
  39. static int deflate_comp_init(struct deflate_ctx *ctx)
  40. {
  41. int ret = 0;
  42. struct z_stream_s *stream = &ctx->comp_stream;
  43. stream->workspace = vzalloc(zlib_deflate_workspacesize(
  44. -DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL));
  45. if (!stream->workspace) {
  46. ret = -ENOMEM;
  47. goto out;
  48. }
  49. ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
  50. -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
  51. Z_DEFAULT_STRATEGY);
  52. if (ret != Z_OK) {
  53. ret = -EINVAL;
  54. goto out_free;
  55. }
  56. out:
  57. return ret;
  58. out_free:
  59. vfree(stream->workspace);
  60. goto out;
  61. }
  62. static int deflate_decomp_init(struct deflate_ctx *ctx)
  63. {
  64. int ret = 0;
  65. struct z_stream_s *stream = &ctx->decomp_stream;
  66. stream->workspace = vzalloc(zlib_inflate_workspacesize());
  67. if (!stream->workspace) {
  68. ret = -ENOMEM;
  69. goto out;
  70. }
  71. ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
  72. if (ret != Z_OK) {
  73. ret = -EINVAL;
  74. goto out_free;
  75. }
  76. out:
  77. return ret;
  78. out_free:
  79. vfree(stream->workspace);
  80. goto out;
  81. }
  82. static void deflate_comp_exit(struct deflate_ctx *ctx)
  83. {
  84. zlib_deflateEnd(&ctx->comp_stream);
  85. vfree(ctx->comp_stream.workspace);
  86. }
  87. static void deflate_decomp_exit(struct deflate_ctx *ctx)
  88. {
  89. zlib_inflateEnd(&ctx->decomp_stream);
  90. vfree(ctx->decomp_stream.workspace);
  91. }
  92. static int __deflate_init(void *ctx)
  93. {
  94. int ret;
  95. ret = deflate_comp_init(ctx);
  96. if (ret)
  97. goto out;
  98. ret = deflate_decomp_init(ctx);
  99. if (ret)
  100. deflate_comp_exit(ctx);
  101. out:
  102. return ret;
  103. }
  104. static void *deflate_alloc_ctx(struct crypto_scomp *tfm)
  105. {
  106. struct deflate_ctx *ctx;
  107. int ret;
  108. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  109. if (!ctx)
  110. return ERR_PTR(-ENOMEM);
  111. ret = __deflate_init(ctx);
  112. if (ret) {
  113. kfree(ctx);
  114. return ERR_PTR(ret);
  115. }
  116. return ctx;
  117. }
  118. static int deflate_init(struct crypto_tfm *tfm)
  119. {
  120. struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
  121. return __deflate_init(ctx);
  122. }
  123. static void __deflate_exit(void *ctx)
  124. {
  125. deflate_comp_exit(ctx);
  126. deflate_decomp_exit(ctx);
  127. }
  128. static void deflate_free_ctx(struct crypto_scomp *tfm, void *ctx)
  129. {
  130. __deflate_exit(ctx);
  131. kfree_sensitive(ctx);
  132. }
  133. static void deflate_exit(struct crypto_tfm *tfm)
  134. {
  135. struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
  136. __deflate_exit(ctx);
  137. }
  138. static int __deflate_compress(const u8 *src, unsigned int slen,
  139. u8 *dst, unsigned int *dlen, void *ctx)
  140. {
  141. int ret = 0;
  142. struct deflate_ctx *dctx = ctx;
  143. struct z_stream_s *stream = &dctx->comp_stream;
  144. ret = zlib_deflateReset(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_deflate(stream, Z_FINISH);
  154. if (ret != Z_STREAM_END) {
  155. ret = -EINVAL;
  156. goto out;
  157. }
  158. ret = 0;
  159. *dlen = stream->total_out;
  160. out:
  161. return ret;
  162. }
  163. static int deflate_compress(struct crypto_tfm *tfm, const u8 *src,
  164. unsigned int slen, u8 *dst, unsigned int *dlen)
  165. {
  166. struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
  167. return __deflate_compress(src, slen, dst, dlen, dctx);
  168. }
  169. static int deflate_scompress(struct crypto_scomp *tfm, const u8 *src,
  170. unsigned int slen, u8 *dst, unsigned int *dlen,
  171. void *ctx)
  172. {
  173. return __deflate_compress(src, slen, dst, dlen, ctx);
  174. }
  175. static int __deflate_decompress(const u8 *src, unsigned int slen,
  176. u8 *dst, unsigned int *dlen, void *ctx)
  177. {
  178. int ret = 0;
  179. struct deflate_ctx *dctx = ctx;
  180. struct z_stream_s *stream = &dctx->decomp_stream;
  181. ret = zlib_inflateReset(stream);
  182. if (ret != Z_OK) {
  183. ret = -EINVAL;
  184. goto out;
  185. }
  186. stream->next_in = (u8 *)src;
  187. stream->avail_in = slen;
  188. stream->next_out = (u8 *)dst;
  189. stream->avail_out = *dlen;
  190. ret = zlib_inflate(stream, Z_SYNC_FLUSH);
  191. /*
  192. * Work around a bug in zlib, which sometimes wants to taste an extra
  193. * byte when being used in the (undocumented) raw deflate mode.
  194. * (From USAGI).
  195. */
  196. if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
  197. u8 zerostuff = 0;
  198. stream->next_in = &zerostuff;
  199. stream->avail_in = 1;
  200. ret = zlib_inflate(stream, Z_FINISH);
  201. }
  202. if (ret != Z_STREAM_END) {
  203. ret = -EINVAL;
  204. goto out;
  205. }
  206. ret = 0;
  207. *dlen = stream->total_out;
  208. out:
  209. return ret;
  210. }
  211. static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src,
  212. unsigned int slen, u8 *dst, unsigned int *dlen)
  213. {
  214. struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
  215. return __deflate_decompress(src, slen, dst, dlen, dctx);
  216. }
  217. static int deflate_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  218. unsigned int slen, u8 *dst, unsigned int *dlen,
  219. void *ctx)
  220. {
  221. return __deflate_decompress(src, slen, dst, dlen, ctx);
  222. }
  223. static struct crypto_alg alg = {
  224. .cra_name = "deflate",
  225. .cra_driver_name = "deflate-generic",
  226. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  227. .cra_ctxsize = sizeof(struct deflate_ctx),
  228. .cra_module = THIS_MODULE,
  229. .cra_init = deflate_init,
  230. .cra_exit = deflate_exit,
  231. .cra_u = { .compress = {
  232. .coa_compress = deflate_compress,
  233. .coa_decompress = deflate_decompress } }
  234. };
  235. static struct scomp_alg scomp = {
  236. .alloc_ctx = deflate_alloc_ctx,
  237. .free_ctx = deflate_free_ctx,
  238. .compress = deflate_scompress,
  239. .decompress = deflate_sdecompress,
  240. .base = {
  241. .cra_name = "deflate",
  242. .cra_driver_name = "deflate-scomp",
  243. .cra_module = THIS_MODULE,
  244. }
  245. };
  246. static int __init deflate_mod_init(void)
  247. {
  248. int ret;
  249. ret = crypto_register_alg(&alg);
  250. if (ret)
  251. return ret;
  252. ret = crypto_register_scomp(&scomp);
  253. if (ret) {
  254. crypto_unregister_alg(&alg);
  255. return ret;
  256. }
  257. return ret;
  258. }
  259. static void __exit deflate_mod_fini(void)
  260. {
  261. crypto_unregister_alg(&alg);
  262. crypto_unregister_scomp(&scomp);
  263. }
  264. subsys_initcall(deflate_mod_init);
  265. module_exit(deflate_mod_fini);
  266. MODULE_LICENSE("GPL");
  267. MODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP");
  268. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
  269. MODULE_ALIAS_CRYPTO("deflate");
  270. MODULE_ALIAS_CRYPTO("deflate-generic");