Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: acomp - add support for deflate via scomp

Add scomp backend for deflate compression algorithm.

Signed-off-by: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Giovanni Cabiddu and committed by
Herbert Xu
f6ded09d 6a8de3ae

+102 -10
+1
crypto/Kconfig
··· 1578 1578 config CRYPTO_DEFLATE 1579 1579 tristate "Deflate compression algorithm" 1580 1580 select CRYPTO_ALGAPI 1581 + select CRYPTO_ACOMP2 1581 1582 select ZLIB_INFLATE 1582 1583 select ZLIB_DEFLATE 1583 1584 help
+101 -10
crypto/deflate.c
··· 32 32 #include <linux/interrupt.h> 33 33 #include <linux/mm.h> 34 34 #include <linux/net.h> 35 + #include <crypto/internal/scompress.h> 35 36 36 37 #define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION 37 38 #define DEFLATE_DEF_WINBITS 11 ··· 102 101 vfree(ctx->decomp_stream.workspace); 103 102 } 104 103 105 - static int deflate_init(struct crypto_tfm *tfm) 104 + static int __deflate_init(void *ctx) 106 105 { 107 - struct deflate_ctx *ctx = crypto_tfm_ctx(tfm); 108 106 int ret; 109 107 110 108 ret = deflate_comp_init(ctx); ··· 116 116 return ret; 117 117 } 118 118 119 - static void deflate_exit(struct crypto_tfm *tfm) 119 + static void *deflate_alloc_ctx(struct crypto_scomp *tfm) 120 + { 121 + struct deflate_ctx *ctx; 122 + int ret; 123 + 124 + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 125 + if (!ctx) 126 + return ERR_PTR(-ENOMEM); 127 + 128 + ret = __deflate_init(ctx); 129 + if (ret) { 130 + kfree(ctx); 131 + return ERR_PTR(ret); 132 + } 133 + 134 + return ctx; 135 + } 136 + 137 + static int deflate_init(struct crypto_tfm *tfm) 120 138 { 121 139 struct deflate_ctx *ctx = crypto_tfm_ctx(tfm); 122 140 141 + return __deflate_init(ctx); 142 + } 143 + 144 + static void __deflate_exit(void *ctx) 145 + { 123 146 deflate_comp_exit(ctx); 124 147 deflate_decomp_exit(ctx); 125 148 } 126 149 127 - static int deflate_compress(struct crypto_tfm *tfm, const u8 *src, 128 - unsigned int slen, u8 *dst, unsigned int *dlen) 150 + static void deflate_free_ctx(struct crypto_scomp *tfm, void *ctx) 151 + { 152 + __deflate_exit(ctx); 153 + kzfree(ctx); 154 + } 155 + 156 + static void deflate_exit(struct crypto_tfm *tfm) 157 + { 158 + struct deflate_ctx *ctx = crypto_tfm_ctx(tfm); 159 + 160 + __deflate_exit(ctx); 161 + } 162 + 163 + static int __deflate_compress(const u8 *src, unsigned int slen, 164 + u8 *dst, unsigned int *dlen, void *ctx) 129 165 { 130 166 int ret = 0; 131 - struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); 167 + struct deflate_ctx *dctx = ctx; 132 168 struct z_stream_s *stream = &dctx->comp_stream; 133 169 134 170 ret = zlib_deflateReset(stream); ··· 189 153 return ret; 190 154 } 191 155 192 - static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src, 193 - unsigned int slen, u8 *dst, unsigned int *dlen) 156 + static int deflate_compress(struct crypto_tfm *tfm, const u8 *src, 157 + unsigned int slen, u8 *dst, unsigned int *dlen) 158 + { 159 + struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); 160 + 161 + return __deflate_compress(src, slen, dst, dlen, dctx); 162 + } 163 + 164 + static int deflate_scompress(struct crypto_scomp *tfm, const u8 *src, 165 + unsigned int slen, u8 *dst, unsigned int *dlen, 166 + void *ctx) 167 + { 168 + return __deflate_compress(src, slen, dst, dlen, ctx); 169 + } 170 + 171 + static int __deflate_decompress(const u8 *src, unsigned int slen, 172 + u8 *dst, unsigned int *dlen, void *ctx) 194 173 { 195 174 196 175 int ret = 0; 197 - struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); 176 + struct deflate_ctx *dctx = ctx; 198 177 struct z_stream_s *stream = &dctx->decomp_stream; 199 178 200 179 ret = zlib_inflateReset(stream); ··· 245 194 return ret; 246 195 } 247 196 197 + static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src, 198 + unsigned int slen, u8 *dst, unsigned int *dlen) 199 + { 200 + struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); 201 + 202 + return __deflate_decompress(src, slen, dst, dlen, dctx); 203 + } 204 + 205 + static int deflate_sdecompress(struct crypto_scomp *tfm, const u8 *src, 206 + unsigned int slen, u8 *dst, unsigned int *dlen, 207 + void *ctx) 208 + { 209 + return __deflate_decompress(src, slen, dst, dlen, ctx); 210 + } 211 + 248 212 static struct crypto_alg alg = { 249 213 .cra_name = "deflate", 250 214 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, ··· 272 206 .coa_decompress = deflate_decompress } } 273 207 }; 274 208 209 + static struct scomp_alg scomp = { 210 + .alloc_ctx = deflate_alloc_ctx, 211 + .free_ctx = deflate_free_ctx, 212 + .compress = deflate_scompress, 213 + .decompress = deflate_sdecompress, 214 + .base = { 215 + .cra_name = "deflate", 216 + .cra_driver_name = "deflate-scomp", 217 + .cra_module = THIS_MODULE, 218 + } 219 + }; 220 + 275 221 static int __init deflate_mod_init(void) 276 222 { 277 - return crypto_register_alg(&alg); 223 + int ret; 224 + 225 + ret = crypto_register_alg(&alg); 226 + if (ret) 227 + return ret; 228 + 229 + ret = crypto_register_scomp(&scomp); 230 + if (ret) { 231 + crypto_unregister_alg(&alg); 232 + return ret; 233 + } 234 + 235 + return ret; 278 236 } 279 237 280 238 static void __exit deflate_mod_fini(void) 281 239 { 282 240 crypto_unregister_alg(&alg); 241 + crypto_unregister_scomp(&scomp); 283 242 } 284 243 285 244 module_init(deflate_mod_init);