xref: /linux/crypto/lz4hc.c (revision 0898782247ae533d1f4e47a06bc5d4870931b284)
12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20ea8530dSChanho Min /*
30ea8530dSChanho Min  * Cryptographic API.
40ea8530dSChanho Min  *
50ea8530dSChanho Min  * Copyright (c) 2013 Chanho Min <chanho.min@lge.com>
60ea8530dSChanho Min  */
70ea8530dSChanho Min #include <linux/init.h>
80ea8530dSChanho Min #include <linux/module.h>
90ea8530dSChanho Min #include <linux/crypto.h>
100ea8530dSChanho Min #include <linux/vmalloc.h>
110ea8530dSChanho Min #include <linux/lz4.h>
1291d53d96SGiovanni Cabiddu #include <crypto/internal/scompress.h>
130ea8530dSChanho Min 
140ea8530dSChanho Min struct lz4hc_ctx {
150ea8530dSChanho Min 	void *lz4hc_comp_mem;
160ea8530dSChanho Min };
170ea8530dSChanho Min 
lz4hc_alloc_ctx(struct crypto_scomp * tfm)1891d53d96SGiovanni Cabiddu static void *lz4hc_alloc_ctx(struct crypto_scomp *tfm)
1991d53d96SGiovanni Cabiddu {
2091d53d96SGiovanni Cabiddu 	void *ctx;
2191d53d96SGiovanni Cabiddu 
2291d53d96SGiovanni Cabiddu 	ctx = vmalloc(LZ4HC_MEM_COMPRESS);
2391d53d96SGiovanni Cabiddu 	if (!ctx)
2491d53d96SGiovanni Cabiddu 		return ERR_PTR(-ENOMEM);
2591d53d96SGiovanni Cabiddu 
2691d53d96SGiovanni Cabiddu 	return ctx;
2791d53d96SGiovanni Cabiddu }
2891d53d96SGiovanni Cabiddu 
lz4hc_init(struct crypto_tfm * tfm)290ea8530dSChanho Min static int lz4hc_init(struct crypto_tfm *tfm)
300ea8530dSChanho Min {
310ea8530dSChanho Min 	struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
320ea8530dSChanho Min 
3391d53d96SGiovanni Cabiddu 	ctx->lz4hc_comp_mem = lz4hc_alloc_ctx(NULL);
3491d53d96SGiovanni Cabiddu 	if (IS_ERR(ctx->lz4hc_comp_mem))
350ea8530dSChanho Min 		return -ENOMEM;
360ea8530dSChanho Min 
370ea8530dSChanho Min 	return 0;
380ea8530dSChanho Min }
390ea8530dSChanho Min 
lz4hc_free_ctx(struct crypto_scomp * tfm,void * ctx)4091d53d96SGiovanni Cabiddu static void lz4hc_free_ctx(struct crypto_scomp *tfm, void *ctx)
4191d53d96SGiovanni Cabiddu {
4291d53d96SGiovanni Cabiddu 	vfree(ctx);
4391d53d96SGiovanni Cabiddu }
4491d53d96SGiovanni Cabiddu 
lz4hc_exit(struct crypto_tfm * tfm)450ea8530dSChanho Min static void lz4hc_exit(struct crypto_tfm *tfm)
460ea8530dSChanho Min {
470ea8530dSChanho Min 	struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
480ea8530dSChanho Min 
4991d53d96SGiovanni Cabiddu 	lz4hc_free_ctx(NULL, ctx->lz4hc_comp_mem);
500ea8530dSChanho Min }
510ea8530dSChanho Min 
__lz4hc_compress_crypto(const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen,void * ctx)5291d53d96SGiovanni Cabiddu static int __lz4hc_compress_crypto(const u8 *src, unsigned int slen,
5391d53d96SGiovanni Cabiddu 				   u8 *dst, unsigned int *dlen, void *ctx)
540ea8530dSChanho Min {
5573a15ac6SSven Schmidt 	int out_len = LZ4_compress_HC(src, dst, slen,
5673a15ac6SSven Schmidt 		*dlen, LZ4HC_DEFAULT_CLEVEL, ctx);
570ea8530dSChanho Min 
5873a15ac6SSven Schmidt 	if (!out_len)
590ea8530dSChanho Min 		return -EINVAL;
600ea8530dSChanho Min 
6173a15ac6SSven Schmidt 	*dlen = out_len;
620ea8530dSChanho Min 	return 0;
630ea8530dSChanho Min }
640ea8530dSChanho Min 
lz4hc_scompress(struct crypto_scomp * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen,void * ctx)6591d53d96SGiovanni Cabiddu static int lz4hc_scompress(struct crypto_scomp *tfm, const u8 *src,
6691d53d96SGiovanni Cabiddu 			   unsigned int slen, u8 *dst, unsigned int *dlen,
6791d53d96SGiovanni Cabiddu 			   void *ctx)
6891d53d96SGiovanni Cabiddu {
6991d53d96SGiovanni Cabiddu 	return __lz4hc_compress_crypto(src, slen, dst, dlen, ctx);
7091d53d96SGiovanni Cabiddu }
7191d53d96SGiovanni Cabiddu 
lz4hc_compress_crypto(struct crypto_tfm * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen)7291d53d96SGiovanni Cabiddu static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
7391d53d96SGiovanni Cabiddu 				 unsigned int slen, u8 *dst,
7491d53d96SGiovanni Cabiddu 				 unsigned int *dlen)
7591d53d96SGiovanni Cabiddu {
7691d53d96SGiovanni Cabiddu 	struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
7791d53d96SGiovanni Cabiddu 
7891d53d96SGiovanni Cabiddu 	return __lz4hc_compress_crypto(src, slen, dst, dlen,
7991d53d96SGiovanni Cabiddu 					ctx->lz4hc_comp_mem);
8091d53d96SGiovanni Cabiddu }
8191d53d96SGiovanni Cabiddu 
__lz4hc_decompress_crypto(const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen,void * ctx)8291d53d96SGiovanni Cabiddu static int __lz4hc_decompress_crypto(const u8 *src, unsigned int slen,
8391d53d96SGiovanni Cabiddu 				     u8 *dst, unsigned int *dlen, void *ctx)
840ea8530dSChanho Min {
8573a15ac6SSven Schmidt 	int out_len = LZ4_decompress_safe(src, dst, slen, *dlen);
860ea8530dSChanho Min 
8773a15ac6SSven Schmidt 	if (out_len < 0)
88cd15f102SMyungho Jung 		return -EINVAL;
890ea8530dSChanho Min 
9073a15ac6SSven Schmidt 	*dlen = out_len;
9173a15ac6SSven Schmidt 	return 0;
920ea8530dSChanho Min }
930ea8530dSChanho Min 
lz4hc_sdecompress(struct crypto_scomp * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen,void * ctx)9491d53d96SGiovanni Cabiddu static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
9591d53d96SGiovanni Cabiddu 			     unsigned int slen, u8 *dst, unsigned int *dlen,
9691d53d96SGiovanni Cabiddu 			     void *ctx)
9791d53d96SGiovanni Cabiddu {
9891d53d96SGiovanni Cabiddu 	return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
9991d53d96SGiovanni Cabiddu }
10091d53d96SGiovanni Cabiddu 
lz4hc_decompress_crypto(struct crypto_tfm * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen)10191d53d96SGiovanni Cabiddu static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
10291d53d96SGiovanni Cabiddu 				   unsigned int slen, u8 *dst,
10391d53d96SGiovanni Cabiddu 				   unsigned int *dlen)
10491d53d96SGiovanni Cabiddu {
10591d53d96SGiovanni Cabiddu 	return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
10691d53d96SGiovanni Cabiddu }
10791d53d96SGiovanni Cabiddu 
1080ea8530dSChanho Min static struct crypto_alg alg_lz4hc = {
1090ea8530dSChanho Min 	.cra_name		= "lz4hc",
110*d6ebf528SEric Biggers 	.cra_driver_name	= "lz4hc-generic",
1110ea8530dSChanho Min 	.cra_flags		= CRYPTO_ALG_TYPE_COMPRESS,
1120ea8530dSChanho Min 	.cra_ctxsize		= sizeof(struct lz4hc_ctx),
1130ea8530dSChanho Min 	.cra_module		= THIS_MODULE,
1140ea8530dSChanho Min 	.cra_init		= lz4hc_init,
1150ea8530dSChanho Min 	.cra_exit		= lz4hc_exit,
1160ea8530dSChanho Min 	.cra_u			= { .compress = {
1170ea8530dSChanho Min 	.coa_compress		= lz4hc_compress_crypto,
1180ea8530dSChanho Min 	.coa_decompress		= lz4hc_decompress_crypto } }
1190ea8530dSChanho Min };
1200ea8530dSChanho Min 
12191d53d96SGiovanni Cabiddu static struct scomp_alg scomp = {
12291d53d96SGiovanni Cabiddu 	.alloc_ctx		= lz4hc_alloc_ctx,
12391d53d96SGiovanni Cabiddu 	.free_ctx		= lz4hc_free_ctx,
12491d53d96SGiovanni Cabiddu 	.compress		= lz4hc_scompress,
12591d53d96SGiovanni Cabiddu 	.decompress		= lz4hc_sdecompress,
12691d53d96SGiovanni Cabiddu 	.base			= {
12791d53d96SGiovanni Cabiddu 		.cra_name	= "lz4hc",
12891d53d96SGiovanni Cabiddu 		.cra_driver_name = "lz4hc-scomp",
12991d53d96SGiovanni Cabiddu 		.cra_module	 = THIS_MODULE,
13091d53d96SGiovanni Cabiddu 	}
13191d53d96SGiovanni Cabiddu };
13291d53d96SGiovanni Cabiddu 
lz4hc_mod_init(void)1330ea8530dSChanho Min static int __init lz4hc_mod_init(void)
1340ea8530dSChanho Min {
13591d53d96SGiovanni Cabiddu 	int ret;
13691d53d96SGiovanni Cabiddu 
13791d53d96SGiovanni Cabiddu 	ret = crypto_register_alg(&alg_lz4hc);
13891d53d96SGiovanni Cabiddu 	if (ret)
13991d53d96SGiovanni Cabiddu 		return ret;
14091d53d96SGiovanni Cabiddu 
14191d53d96SGiovanni Cabiddu 	ret = crypto_register_scomp(&scomp);
14291d53d96SGiovanni Cabiddu 	if (ret) {
14391d53d96SGiovanni Cabiddu 		crypto_unregister_alg(&alg_lz4hc);
14491d53d96SGiovanni Cabiddu 		return ret;
14591d53d96SGiovanni Cabiddu 	}
14691d53d96SGiovanni Cabiddu 
14791d53d96SGiovanni Cabiddu 	return ret;
1480ea8530dSChanho Min }
1490ea8530dSChanho Min 
lz4hc_mod_fini(void)1500ea8530dSChanho Min static void __exit lz4hc_mod_fini(void)
1510ea8530dSChanho Min {
1520ea8530dSChanho Min 	crypto_unregister_alg(&alg_lz4hc);
15391d53d96SGiovanni Cabiddu 	crypto_unregister_scomp(&scomp);
1540ea8530dSChanho Min }
1550ea8530dSChanho Min 
156c4741b23SEric Biggers subsys_initcall(lz4hc_mod_init);
1570ea8530dSChanho Min module_exit(lz4hc_mod_fini);
1580ea8530dSChanho Min 
1590ea8530dSChanho Min MODULE_LICENSE("GPL");
1600ea8530dSChanho Min MODULE_DESCRIPTION("LZ4HC Compression Algorithm");
1615d26a105SKees Cook MODULE_ALIAS_CRYPTO("lz4hc");
162