18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Cryptographic API. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Deflate algorithm (RFC 1951), implemented here primarily for use 68c2ecf20Sopenharmony_ci * by IPCOMP (RFC 3173 & RFC 2394). 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Copyright (c) 2003 James Morris <jmorris@intercode.com.au> 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * FIXME: deflate transforms will require up to a total of about 436k of kernel 118c2ecf20Sopenharmony_ci * memory on i386 (390k for compression, the rest for decompression), as the 128c2ecf20Sopenharmony_ci * current zlib kernel code uses a worst case pre-allocation system by default. 138c2ecf20Sopenharmony_ci * This needs to be fixed so that the amount of memory required is properly 148c2ecf20Sopenharmony_ci * related to the winbits and memlevel parameters. 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * The default winbits of 11 should suit most packets, and it may be something 178c2ecf20Sopenharmony_ci * to configure on a per-tfm basis in the future. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * Currently, compression history is not maintained between tfm calls, as 208c2ecf20Sopenharmony_ci * it is not needed for IPCOMP and keeps the code simpler. It can be 218c2ecf20Sopenharmony_ci * implemented if someone wants it. 228c2ecf20Sopenharmony_ci */ 238c2ecf20Sopenharmony_ci#include <linux/init.h> 248c2ecf20Sopenharmony_ci#include <linux/module.h> 258c2ecf20Sopenharmony_ci#include <linux/crypto.h> 268c2ecf20Sopenharmony_ci#include <linux/zlib.h> 278c2ecf20Sopenharmony_ci#include <linux/vmalloc.h> 288c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 298c2ecf20Sopenharmony_ci#include <linux/mm.h> 308c2ecf20Sopenharmony_ci#include <linux/net.h> 318c2ecf20Sopenharmony_ci#include <crypto/internal/scompress.h> 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION 348c2ecf20Sopenharmony_ci#define DEFLATE_DEF_WINBITS 11 358c2ecf20Sopenharmony_ci#define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistruct deflate_ctx { 388c2ecf20Sopenharmony_ci struct z_stream_s comp_stream; 398c2ecf20Sopenharmony_ci struct z_stream_s decomp_stream; 408c2ecf20Sopenharmony_ci}; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic int deflate_comp_init(struct deflate_ctx *ctx, int format) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci int ret = 0; 458c2ecf20Sopenharmony_ci struct z_stream_s *stream = &ctx->comp_stream; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci stream->workspace = vzalloc(zlib_deflate_workspacesize( 488c2ecf20Sopenharmony_ci MAX_WBITS, MAX_MEM_LEVEL)); 498c2ecf20Sopenharmony_ci if (!stream->workspace) { 508c2ecf20Sopenharmony_ci ret = -ENOMEM; 518c2ecf20Sopenharmony_ci goto out; 528c2ecf20Sopenharmony_ci } 538c2ecf20Sopenharmony_ci if (format) 548c2ecf20Sopenharmony_ci ret = zlib_deflateInit(stream, 3); 558c2ecf20Sopenharmony_ci else 568c2ecf20Sopenharmony_ci ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED, 578c2ecf20Sopenharmony_ci -DEFLATE_DEF_WINBITS, 588c2ecf20Sopenharmony_ci DEFLATE_DEF_MEMLEVEL, 598c2ecf20Sopenharmony_ci Z_DEFAULT_STRATEGY); 608c2ecf20Sopenharmony_ci if (ret != Z_OK) { 618c2ecf20Sopenharmony_ci ret = -EINVAL; 628c2ecf20Sopenharmony_ci goto out_free; 638c2ecf20Sopenharmony_ci } 648c2ecf20Sopenharmony_ciout: 658c2ecf20Sopenharmony_ci return ret; 668c2ecf20Sopenharmony_ciout_free: 678c2ecf20Sopenharmony_ci vfree(stream->workspace); 688c2ecf20Sopenharmony_ci goto out; 698c2ecf20Sopenharmony_ci} 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_cistatic int deflate_decomp_init(struct deflate_ctx *ctx, int format) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci int ret = 0; 748c2ecf20Sopenharmony_ci struct z_stream_s *stream = &ctx->decomp_stream; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci stream->workspace = vzalloc(zlib_inflate_workspacesize()); 778c2ecf20Sopenharmony_ci if (!stream->workspace) { 788c2ecf20Sopenharmony_ci ret = -ENOMEM; 798c2ecf20Sopenharmony_ci goto out; 808c2ecf20Sopenharmony_ci } 818c2ecf20Sopenharmony_ci if (format) 828c2ecf20Sopenharmony_ci ret = zlib_inflateInit(stream); 838c2ecf20Sopenharmony_ci else 848c2ecf20Sopenharmony_ci ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS); 858c2ecf20Sopenharmony_ci if (ret != Z_OK) { 868c2ecf20Sopenharmony_ci ret = -EINVAL; 878c2ecf20Sopenharmony_ci goto out_free; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ciout: 908c2ecf20Sopenharmony_ci return ret; 918c2ecf20Sopenharmony_ciout_free: 928c2ecf20Sopenharmony_ci vfree(stream->workspace); 938c2ecf20Sopenharmony_ci goto out; 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistatic void deflate_comp_exit(struct deflate_ctx *ctx) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci zlib_deflateEnd(&ctx->comp_stream); 998c2ecf20Sopenharmony_ci vfree(ctx->comp_stream.workspace); 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cistatic void deflate_decomp_exit(struct deflate_ctx *ctx) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci zlib_inflateEnd(&ctx->decomp_stream); 1058c2ecf20Sopenharmony_ci vfree(ctx->decomp_stream.workspace); 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_cistatic int __deflate_init(void *ctx, int format) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci int ret; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci ret = deflate_comp_init(ctx, format); 1138c2ecf20Sopenharmony_ci if (ret) 1148c2ecf20Sopenharmony_ci goto out; 1158c2ecf20Sopenharmony_ci ret = deflate_decomp_init(ctx, format); 1168c2ecf20Sopenharmony_ci if (ret) 1178c2ecf20Sopenharmony_ci deflate_comp_exit(ctx); 1188c2ecf20Sopenharmony_ciout: 1198c2ecf20Sopenharmony_ci return ret; 1208c2ecf20Sopenharmony_ci} 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_cistatic void *gen_deflate_alloc_ctx(struct crypto_scomp *tfm, int format) 1238c2ecf20Sopenharmony_ci{ 1248c2ecf20Sopenharmony_ci struct deflate_ctx *ctx; 1258c2ecf20Sopenharmony_ci int ret; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 1288c2ecf20Sopenharmony_ci if (!ctx) 1298c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci ret = __deflate_init(ctx, format); 1328c2ecf20Sopenharmony_ci if (ret) { 1338c2ecf20Sopenharmony_ci kfree(ctx); 1348c2ecf20Sopenharmony_ci return ERR_PTR(ret); 1358c2ecf20Sopenharmony_ci } 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci return ctx; 1388c2ecf20Sopenharmony_ci} 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistatic void *deflate_alloc_ctx(struct crypto_scomp *tfm) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci return gen_deflate_alloc_ctx(tfm, 0); 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic void *zlib_deflate_alloc_ctx(struct crypto_scomp *tfm) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci return gen_deflate_alloc_ctx(tfm, 1); 1488c2ecf20Sopenharmony_ci} 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_cistatic int deflate_init(struct crypto_tfm *tfm) 1518c2ecf20Sopenharmony_ci{ 1528c2ecf20Sopenharmony_ci struct deflate_ctx *ctx = crypto_tfm_ctx(tfm); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci return __deflate_init(ctx, 0); 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic void __deflate_exit(void *ctx) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci deflate_comp_exit(ctx); 1608c2ecf20Sopenharmony_ci deflate_decomp_exit(ctx); 1618c2ecf20Sopenharmony_ci} 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_cistatic void deflate_free_ctx(struct crypto_scomp *tfm, void *ctx) 1648c2ecf20Sopenharmony_ci{ 1658c2ecf20Sopenharmony_ci __deflate_exit(ctx); 1668c2ecf20Sopenharmony_ci kfree_sensitive(ctx); 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_cistatic void deflate_exit(struct crypto_tfm *tfm) 1708c2ecf20Sopenharmony_ci{ 1718c2ecf20Sopenharmony_ci struct deflate_ctx *ctx = crypto_tfm_ctx(tfm); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci __deflate_exit(ctx); 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_cistatic int __deflate_compress(const u8 *src, unsigned int slen, 1778c2ecf20Sopenharmony_ci u8 *dst, unsigned int *dlen, void *ctx) 1788c2ecf20Sopenharmony_ci{ 1798c2ecf20Sopenharmony_ci int ret = 0; 1808c2ecf20Sopenharmony_ci struct deflate_ctx *dctx = ctx; 1818c2ecf20Sopenharmony_ci struct z_stream_s *stream = &dctx->comp_stream; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci ret = zlib_deflateReset(stream); 1848c2ecf20Sopenharmony_ci if (ret != Z_OK) { 1858c2ecf20Sopenharmony_ci ret = -EINVAL; 1868c2ecf20Sopenharmony_ci goto out; 1878c2ecf20Sopenharmony_ci } 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci stream->next_in = (u8 *)src; 1908c2ecf20Sopenharmony_ci stream->avail_in = slen; 1918c2ecf20Sopenharmony_ci stream->next_out = (u8 *)dst; 1928c2ecf20Sopenharmony_ci stream->avail_out = *dlen; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci ret = zlib_deflate(stream, Z_FINISH); 1958c2ecf20Sopenharmony_ci if (ret != Z_STREAM_END) { 1968c2ecf20Sopenharmony_ci ret = -EINVAL; 1978c2ecf20Sopenharmony_ci goto out; 1988c2ecf20Sopenharmony_ci } 1998c2ecf20Sopenharmony_ci ret = 0; 2008c2ecf20Sopenharmony_ci *dlen = stream->total_out; 2018c2ecf20Sopenharmony_ciout: 2028c2ecf20Sopenharmony_ci return ret; 2038c2ecf20Sopenharmony_ci} 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_cistatic int deflate_compress(struct crypto_tfm *tfm, const u8 *src, 2068c2ecf20Sopenharmony_ci unsigned int slen, u8 *dst, unsigned int *dlen) 2078c2ecf20Sopenharmony_ci{ 2088c2ecf20Sopenharmony_ci struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci return __deflate_compress(src, slen, dst, dlen, dctx); 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_cistatic int deflate_scompress(struct crypto_scomp *tfm, const u8 *src, 2148c2ecf20Sopenharmony_ci unsigned int slen, u8 *dst, unsigned int *dlen, 2158c2ecf20Sopenharmony_ci void *ctx) 2168c2ecf20Sopenharmony_ci{ 2178c2ecf20Sopenharmony_ci return __deflate_compress(src, slen, dst, dlen, ctx); 2188c2ecf20Sopenharmony_ci} 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_cistatic int __deflate_decompress(const u8 *src, unsigned int slen, 2218c2ecf20Sopenharmony_ci u8 *dst, unsigned int *dlen, void *ctx) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci int ret = 0; 2258c2ecf20Sopenharmony_ci struct deflate_ctx *dctx = ctx; 2268c2ecf20Sopenharmony_ci struct z_stream_s *stream = &dctx->decomp_stream; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci ret = zlib_inflateReset(stream); 2298c2ecf20Sopenharmony_ci if (ret != Z_OK) { 2308c2ecf20Sopenharmony_ci ret = -EINVAL; 2318c2ecf20Sopenharmony_ci goto out; 2328c2ecf20Sopenharmony_ci } 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci stream->next_in = (u8 *)src; 2358c2ecf20Sopenharmony_ci stream->avail_in = slen; 2368c2ecf20Sopenharmony_ci stream->next_out = (u8 *)dst; 2378c2ecf20Sopenharmony_ci stream->avail_out = *dlen; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci ret = zlib_inflate(stream, Z_SYNC_FLUSH); 2408c2ecf20Sopenharmony_ci /* 2418c2ecf20Sopenharmony_ci * Work around a bug in zlib, which sometimes wants to taste an extra 2428c2ecf20Sopenharmony_ci * byte when being used in the (undocumented) raw deflate mode. 2438c2ecf20Sopenharmony_ci * (From USAGI). 2448c2ecf20Sopenharmony_ci */ 2458c2ecf20Sopenharmony_ci if (ret == Z_OK && !stream->avail_in && stream->avail_out) { 2468c2ecf20Sopenharmony_ci u8 zerostuff = 0; 2478c2ecf20Sopenharmony_ci stream->next_in = &zerostuff; 2488c2ecf20Sopenharmony_ci stream->avail_in = 1; 2498c2ecf20Sopenharmony_ci ret = zlib_inflate(stream, Z_FINISH); 2508c2ecf20Sopenharmony_ci } 2518c2ecf20Sopenharmony_ci if (ret != Z_STREAM_END) { 2528c2ecf20Sopenharmony_ci ret = -EINVAL; 2538c2ecf20Sopenharmony_ci goto out; 2548c2ecf20Sopenharmony_ci } 2558c2ecf20Sopenharmony_ci ret = 0; 2568c2ecf20Sopenharmony_ci *dlen = stream->total_out; 2578c2ecf20Sopenharmony_ciout: 2588c2ecf20Sopenharmony_ci return ret; 2598c2ecf20Sopenharmony_ci} 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_cistatic int deflate_decompress(struct crypto_tfm *tfm, const u8 *src, 2628c2ecf20Sopenharmony_ci unsigned int slen, u8 *dst, unsigned int *dlen) 2638c2ecf20Sopenharmony_ci{ 2648c2ecf20Sopenharmony_ci struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci return __deflate_decompress(src, slen, dst, dlen, dctx); 2678c2ecf20Sopenharmony_ci} 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cistatic int deflate_sdecompress(struct crypto_scomp *tfm, const u8 *src, 2708c2ecf20Sopenharmony_ci unsigned int slen, u8 *dst, unsigned int *dlen, 2718c2ecf20Sopenharmony_ci void *ctx) 2728c2ecf20Sopenharmony_ci{ 2738c2ecf20Sopenharmony_ci return __deflate_decompress(src, slen, dst, dlen, ctx); 2748c2ecf20Sopenharmony_ci} 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_cistatic struct crypto_alg alg = { 2778c2ecf20Sopenharmony_ci .cra_name = "deflate", 2788c2ecf20Sopenharmony_ci .cra_driver_name = "deflate-generic", 2798c2ecf20Sopenharmony_ci .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, 2808c2ecf20Sopenharmony_ci .cra_ctxsize = sizeof(struct deflate_ctx), 2818c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 2828c2ecf20Sopenharmony_ci .cra_init = deflate_init, 2838c2ecf20Sopenharmony_ci .cra_exit = deflate_exit, 2848c2ecf20Sopenharmony_ci .cra_u = { .compress = { 2858c2ecf20Sopenharmony_ci .coa_compress = deflate_compress, 2868c2ecf20Sopenharmony_ci .coa_decompress = deflate_decompress } } 2878c2ecf20Sopenharmony_ci}; 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_cistatic struct scomp_alg scomp[] = { { 2908c2ecf20Sopenharmony_ci .alloc_ctx = deflate_alloc_ctx, 2918c2ecf20Sopenharmony_ci .free_ctx = deflate_free_ctx, 2928c2ecf20Sopenharmony_ci .compress = deflate_scompress, 2938c2ecf20Sopenharmony_ci .decompress = deflate_sdecompress, 2948c2ecf20Sopenharmony_ci .base = { 2958c2ecf20Sopenharmony_ci .cra_name = "deflate", 2968c2ecf20Sopenharmony_ci .cra_driver_name = "deflate-scomp", 2978c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 2988c2ecf20Sopenharmony_ci } 2998c2ecf20Sopenharmony_ci}, { 3008c2ecf20Sopenharmony_ci .alloc_ctx = zlib_deflate_alloc_ctx, 3018c2ecf20Sopenharmony_ci .free_ctx = deflate_free_ctx, 3028c2ecf20Sopenharmony_ci .compress = deflate_scompress, 3038c2ecf20Sopenharmony_ci .decompress = deflate_sdecompress, 3048c2ecf20Sopenharmony_ci .base = { 3058c2ecf20Sopenharmony_ci .cra_name = "zlib-deflate", 3068c2ecf20Sopenharmony_ci .cra_driver_name = "zlib-deflate-scomp", 3078c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 3088c2ecf20Sopenharmony_ci } 3098c2ecf20Sopenharmony_ci} }; 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_cistatic int __init deflate_mod_init(void) 3128c2ecf20Sopenharmony_ci{ 3138c2ecf20Sopenharmony_ci int ret; 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci ret = crypto_register_alg(&alg); 3168c2ecf20Sopenharmony_ci if (ret) 3178c2ecf20Sopenharmony_ci return ret; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci ret = crypto_register_scomps(scomp, ARRAY_SIZE(scomp)); 3208c2ecf20Sopenharmony_ci if (ret) { 3218c2ecf20Sopenharmony_ci crypto_unregister_alg(&alg); 3228c2ecf20Sopenharmony_ci return ret; 3238c2ecf20Sopenharmony_ci } 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci return ret; 3268c2ecf20Sopenharmony_ci} 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_cistatic void __exit deflate_mod_fini(void) 3298c2ecf20Sopenharmony_ci{ 3308c2ecf20Sopenharmony_ci crypto_unregister_alg(&alg); 3318c2ecf20Sopenharmony_ci crypto_unregister_scomps(scomp, ARRAY_SIZE(scomp)); 3328c2ecf20Sopenharmony_ci} 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_cisubsys_initcall(deflate_mod_init); 3358c2ecf20Sopenharmony_cimodule_exit(deflate_mod_fini); 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 3388c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Deflate Compression Algorithm for IPCOMP"); 3398c2ecf20Sopenharmony_ciMODULE_AUTHOR("James Morris <jmorris@intercode.com.au>"); 3408c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("deflate"); 341