deflate.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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, int format)
  40. {
  41. int ret = 0;
  42. struct z_stream_s *stream = &ctx->comp_stream;
  43. stream->workspace = vzalloc(zlib_deflate_workspacesize(
  44. MAX_WBITS, MAX_MEM_LEVEL));
  45. if (!stream->workspace) {
  46. ret = -ENOMEM;
  47. goto out;
  48. }
  49. if (format)
  50. ret = zlib_deflateInit(stream, 3);
  51. else
  52. ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
  53. -DEFLATE_DEF_WINBITS,
  54. 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, int format)
  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. if (format)
  76. ret = zlib_inflateInit(stream);
  77. else
  78. ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
  79. if (ret != Z_OK) {
  80. ret = -EINVAL;
  81. goto out_free;
  82. }
  83. out:
  84. return ret;
  85. out_free:
  86. vfree(stream->workspace);
  87. goto out;
  88. }
  89. static void deflate_comp_exit(struct deflate_ctx *ctx)
  90. {
  91. zlib_deflateEnd(&ctx->comp_stream);
  92. vfree(ctx->comp_stream.workspace);
  93. }
  94. static void deflate_decomp_exit(struct deflate_ctx *ctx)
  95. {
  96. zlib_inflateEnd(&ctx->decomp_stream);
  97. vfree(ctx->decomp_stream.workspace);
  98. }
  99. static int __deflate_init(void *ctx, int format)
  100. {
  101. int ret;
  102. ret = deflate_comp_init(ctx, format);
  103. if (ret)
  104. goto out;
  105. ret = deflate_decomp_init(ctx, format);
  106. if (ret)
  107. deflate_comp_exit(ctx);
  108. out:
  109. return ret;
  110. }
  111. static void *gen_deflate_alloc_ctx(struct crypto_scomp *tfm, int format)
  112. {
  113. struct deflate_ctx *ctx;
  114. int ret;
  115. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  116. if (!ctx)
  117. return ERR_PTR(-ENOMEM);
  118. ret = __deflate_init(ctx, format);
  119. if (ret) {
  120. kfree(ctx);
  121. return ERR_PTR(ret);
  122. }
  123. return ctx;
  124. }
  125. static void *deflate_alloc_ctx(struct crypto_scomp *tfm)
  126. {
  127. return gen_deflate_alloc_ctx(tfm, 0);
  128. }
  129. static void *zlib_deflate_alloc_ctx(struct crypto_scomp *tfm)
  130. {
  131. return gen_deflate_alloc_ctx(tfm, 1);
  132. }
  133. static int deflate_init(struct crypto_tfm *tfm)
  134. {
  135. struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
  136. return __deflate_init(ctx, 0);
  137. }
  138. static void __deflate_exit(void *ctx)
  139. {
  140. deflate_comp_exit(ctx);
  141. deflate_decomp_exit(ctx);
  142. }
  143. static void deflate_free_ctx(struct crypto_scomp *tfm, void *ctx)
  144. {
  145. __deflate_exit(ctx);
  146. kfree_sensitive(ctx);
  147. }
  148. static void deflate_exit(struct crypto_tfm *tfm)
  149. {
  150. struct deflate_ctx *ctx = crypto_tfm_ctx(tfm);
  151. __deflate_exit(ctx);
  152. }
  153. static int __deflate_compress(const u8 *src, unsigned int slen,
  154. u8 *dst, unsigned int *dlen, void *ctx)
  155. {
  156. int ret = 0;
  157. struct deflate_ctx *dctx = ctx;
  158. struct z_stream_s *stream = &dctx->comp_stream;
  159. ret = zlib_deflateReset(stream);
  160. if (ret != Z_OK) {
  161. ret = -EINVAL;
  162. goto out;
  163. }
  164. stream->next_in = (u8 *)src;
  165. stream->avail_in = slen;
  166. stream->next_out = (u8 *)dst;
  167. stream->avail_out = *dlen;
  168. ret = zlib_deflate(stream, Z_FINISH);
  169. if (ret != Z_STREAM_END) {
  170. ret = -EINVAL;
  171. goto out;
  172. }
  173. ret = 0;
  174. *dlen = stream->total_out;
  175. out:
  176. return ret;
  177. }
  178. static int deflate_compress(struct crypto_tfm *tfm, const u8 *src,
  179. unsigned int slen, u8 *dst, unsigned int *dlen)
  180. {
  181. struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
  182. return __deflate_compress(src, slen, dst, dlen, dctx);
  183. }
  184. static int deflate_scompress(struct crypto_scomp *tfm, const u8 *src,
  185. unsigned int slen, u8 *dst, unsigned int *dlen,
  186. void *ctx)
  187. {
  188. return __deflate_compress(src, slen, dst, dlen, ctx);
  189. }
  190. static int __deflate_decompress(const u8 *src, unsigned int slen,
  191. u8 *dst, unsigned int *dlen, void *ctx)
  192. {
  193. int ret = 0;
  194. struct deflate_ctx *dctx = ctx;
  195. struct z_stream_s *stream = &dctx->decomp_stream;
  196. ret = zlib_inflateReset(stream);
  197. if (ret != Z_OK) {
  198. ret = -EINVAL;
  199. goto out;
  200. }
  201. stream->next_in = (u8 *)src;
  202. stream->avail_in = slen;
  203. stream->next_out = (u8 *)dst;
  204. stream->avail_out = *dlen;
  205. ret = zlib_inflate(stream, Z_SYNC_FLUSH);
  206. /*
  207. * Work around a bug in zlib, which sometimes wants to taste an extra
  208. * byte when being used in the (undocumented) raw deflate mode.
  209. * (From USAGI).
  210. */
  211. if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
  212. u8 zerostuff = 0;
  213. stream->next_in = &zerostuff;
  214. stream->avail_in = 1;
  215. ret = zlib_inflate(stream, Z_FINISH);
  216. }
  217. if (ret != Z_STREAM_END) {
  218. ret = -EINVAL;
  219. goto out;
  220. }
  221. ret = 0;
  222. *dlen = stream->total_out;
  223. out:
  224. return ret;
  225. }
  226. static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src,
  227. unsigned int slen, u8 *dst, unsigned int *dlen)
  228. {
  229. struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
  230. return __deflate_decompress(src, slen, dst, dlen, dctx);
  231. }
  232. static int deflate_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  233. unsigned int slen, u8 *dst, unsigned int *dlen,
  234. void *ctx)
  235. {
  236. return __deflate_decompress(src, slen, dst, dlen, ctx);
  237. }
  238. static struct crypto_alg alg = {
  239. .cra_name = "deflate",
  240. .cra_driver_name = "deflate-generic",
  241. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  242. .cra_ctxsize = sizeof(struct deflate_ctx),
  243. .cra_module = THIS_MODULE,
  244. .cra_init = deflate_init,
  245. .cra_exit = deflate_exit,
  246. .cra_u = { .compress = {
  247. .coa_compress = deflate_compress,
  248. .coa_decompress = deflate_decompress } }
  249. };
  250. static struct scomp_alg scomp[] = { {
  251. .alloc_ctx = deflate_alloc_ctx,
  252. .free_ctx = deflate_free_ctx,
  253. .compress = deflate_scompress,
  254. .decompress = deflate_sdecompress,
  255. .base = {
  256. .cra_name = "deflate",
  257. .cra_driver_name = "deflate-scomp",
  258. .cra_module = THIS_MODULE,
  259. }
  260. }, {
  261. .alloc_ctx = zlib_deflate_alloc_ctx,
  262. .free_ctx = deflate_free_ctx,
  263. .compress = deflate_scompress,
  264. .decompress = deflate_sdecompress,
  265. .base = {
  266. .cra_name = "zlib-deflate",
  267. .cra_driver_name = "zlib-deflate-scomp",
  268. .cra_module = THIS_MODULE,
  269. }
  270. } };
  271. static int __init deflate_mod_init(void)
  272. {
  273. int ret;
  274. ret = crypto_register_alg(&alg);
  275. if (ret)
  276. return ret;
  277. ret = crypto_register_scomps(scomp, ARRAY_SIZE(scomp));
  278. if (ret) {
  279. crypto_unregister_alg(&alg);
  280. return ret;
  281. }
  282. return ret;
  283. }
  284. static void __exit deflate_mod_fini(void)
  285. {
  286. crypto_unregister_alg(&alg);
  287. crypto_unregister_scomps(scomp, ARRAY_SIZE(scomp));
  288. }
  289. subsys_initcall(deflate_mod_init);
  290. module_exit(deflate_mod_fini);
  291. MODULE_LICENSE("GPL");
  292. MODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP");
  293. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
  294. MODULE_ALIAS_CRYPTO("deflate");