Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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