Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Cryptographic API.
  4 *
  5 * Copyright (c) 2013 Chanho Min <chanho.min@lge.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/init.h>
  9#include <linux/module.h>
 10#include <linux/crypto.h>
 11#include <linux/vmalloc.h>
 12#include <linux/lz4.h>
 13#include <crypto/internal/scompress.h>
 14
 15struct lz4_ctx {
 16	void *lz4_comp_mem;
 17};
 18
 19static void *lz4_alloc_ctx(struct crypto_scomp *tfm)
 20{
 21	void *ctx;
 22
 23	ctx = vmalloc(LZ4_MEM_COMPRESS);
 24	if (!ctx)
 25		return ERR_PTR(-ENOMEM);
 26
 27	return ctx;
 28}
 29
 30static int lz4_init(struct crypto_tfm *tfm)
 31{
 32	struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
 33
 34	ctx->lz4_comp_mem = lz4_alloc_ctx(NULL);
 35	if (IS_ERR(ctx->lz4_comp_mem))
 36		return -ENOMEM;
 37
 38	return 0;
 39}
 40
 41static void lz4_free_ctx(struct crypto_scomp *tfm, void *ctx)
 42{
 43	vfree(ctx);
 44}
 45
 46static void lz4_exit(struct crypto_tfm *tfm)
 47{
 48	struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
 49
 50	lz4_free_ctx(NULL, ctx->lz4_comp_mem);
 51}
 52
 53static int __lz4_compress_crypto(const u8 *src, unsigned int slen,
 54				 u8 *dst, unsigned int *dlen, void *ctx)
 55{
 56	int out_len = LZ4_compress_default(src, dst,
 57		slen, *dlen, ctx);
 
 
 58
 59	if (!out_len)
 60		return -EINVAL;
 61
 62	*dlen = out_len;
 63	return 0;
 64}
 65
 66static int lz4_scompress(struct crypto_scomp *tfm, const u8 *src,
 67			 unsigned int slen, u8 *dst, unsigned int *dlen,
 68			 void *ctx)
 69{
 70	return __lz4_compress_crypto(src, slen, dst, dlen, ctx);
 71}
 72
 73static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
 74			       unsigned int slen, u8 *dst, unsigned int *dlen)
 75{
 76	struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
 77
 78	return __lz4_compress_crypto(src, slen, dst, dlen, ctx->lz4_comp_mem);
 79}
 80
 81static int __lz4_decompress_crypto(const u8 *src, unsigned int slen,
 82				   u8 *dst, unsigned int *dlen, void *ctx)
 83{
 84	int out_len = LZ4_decompress_safe(src, dst, slen, *dlen);
 
 
 85
 86	if (out_len < 0)
 
 87		return -EINVAL;
 88
 89	*dlen = out_len;
 90	return 0;
 91}
 92
 93static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
 94			   unsigned int slen, u8 *dst, unsigned int *dlen,
 95			   void *ctx)
 96{
 97	return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
 98}
 99
100static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
101				 unsigned int slen, u8 *dst,
102				 unsigned int *dlen)
103{
104	return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
105}
106
107static struct crypto_alg alg_lz4 = {
108	.cra_name		= "lz4",
109	.cra_driver_name	= "lz4-generic",
110	.cra_flags		= CRYPTO_ALG_TYPE_COMPRESS,
111	.cra_ctxsize		= sizeof(struct lz4_ctx),
112	.cra_module		= THIS_MODULE,
 
113	.cra_init		= lz4_init,
114	.cra_exit		= lz4_exit,
115	.cra_u			= { .compress = {
116	.coa_compress		= lz4_compress_crypto,
117	.coa_decompress		= lz4_decompress_crypto } }
118};
119
120static struct scomp_alg scomp = {
121	.alloc_ctx		= lz4_alloc_ctx,
122	.free_ctx		= lz4_free_ctx,
123	.compress		= lz4_scompress,
124	.decompress		= lz4_sdecompress,
125	.base			= {
126		.cra_name	= "lz4",
127		.cra_driver_name = "lz4-scomp",
128		.cra_module	 = THIS_MODULE,
129	}
130};
131
132static int __init lz4_mod_init(void)
133{
134	int ret;
135
136	ret = crypto_register_alg(&alg_lz4);
137	if (ret)
138		return ret;
139
140	ret = crypto_register_scomp(&scomp);
141	if (ret) {
142		crypto_unregister_alg(&alg_lz4);
143		return ret;
144	}
145
146	return ret;
147}
148
149static void __exit lz4_mod_fini(void)
150{
151	crypto_unregister_alg(&alg_lz4);
152	crypto_unregister_scomp(&scomp);
153}
154
155subsys_initcall(lz4_mod_init);
156module_exit(lz4_mod_fini);
157
158MODULE_LICENSE("GPL");
159MODULE_DESCRIPTION("LZ4 Compression Algorithm");
160MODULE_ALIAS_CRYPTO("lz4");
v4.10.11
 
  1/*
  2 * Cryptographic API.
  3 *
  4 * Copyright (c) 2013 Chanho Min <chanho.min@lge.com>
  5 *
  6 * This program is free software; you can redistribute it and/or modify it
  7 * under the terms of the GNU General Public License version 2 as published by
  8 * the Free Software Foundation.
  9 *
 10 * This program is distributed in the hope that it will be useful, but WITHOUT
 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 13 * more details.
 14 *
 15 * You should have received a copy of the GNU General Public License along with
 16 * this program; if not, write to the Free Software Foundation, Inc., 51
 17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 18 *
 19 */
 20
 21#include <linux/init.h>
 22#include <linux/module.h>
 23#include <linux/crypto.h>
 24#include <linux/vmalloc.h>
 25#include <linux/lz4.h>
 26#include <crypto/internal/scompress.h>
 27
 28struct lz4_ctx {
 29	void *lz4_comp_mem;
 30};
 31
 32static void *lz4_alloc_ctx(struct crypto_scomp *tfm)
 33{
 34	void *ctx;
 35
 36	ctx = vmalloc(LZ4_MEM_COMPRESS);
 37	if (!ctx)
 38		return ERR_PTR(-ENOMEM);
 39
 40	return ctx;
 41}
 42
 43static int lz4_init(struct crypto_tfm *tfm)
 44{
 45	struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
 46
 47	ctx->lz4_comp_mem = lz4_alloc_ctx(NULL);
 48	if (IS_ERR(ctx->lz4_comp_mem))
 49		return -ENOMEM;
 50
 51	return 0;
 52}
 53
 54static void lz4_free_ctx(struct crypto_scomp *tfm, void *ctx)
 55{
 56	vfree(ctx);
 57}
 58
 59static void lz4_exit(struct crypto_tfm *tfm)
 60{
 61	struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
 62
 63	lz4_free_ctx(NULL, ctx->lz4_comp_mem);
 64}
 65
 66static int __lz4_compress_crypto(const u8 *src, unsigned int slen,
 67				 u8 *dst, unsigned int *dlen, void *ctx)
 68{
 69	size_t tmp_len = *dlen;
 70	int err;
 71
 72	err = lz4_compress(src, slen, dst, &tmp_len, ctx);
 73
 74	if (err < 0)
 75		return -EINVAL;
 76
 77	*dlen = tmp_len;
 78	return 0;
 79}
 80
 81static int lz4_scompress(struct crypto_scomp *tfm, const u8 *src,
 82			 unsigned int slen, u8 *dst, unsigned int *dlen,
 83			 void *ctx)
 84{
 85	return __lz4_compress_crypto(src, slen, dst, dlen, ctx);
 86}
 87
 88static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
 89			       unsigned int slen, u8 *dst, unsigned int *dlen)
 90{
 91	struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
 92
 93	return __lz4_compress_crypto(src, slen, dst, dlen, ctx->lz4_comp_mem);
 94}
 95
 96static int __lz4_decompress_crypto(const u8 *src, unsigned int slen,
 97				   u8 *dst, unsigned int *dlen, void *ctx)
 98{
 99	int err;
100	size_t tmp_len = *dlen;
101	size_t __slen = slen;
102
103	err = lz4_decompress_unknownoutputsize(src, __slen, dst, &tmp_len);
104	if (err < 0)
105		return -EINVAL;
106
107	*dlen = tmp_len;
108	return err;
109}
110
111static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
112			   unsigned int slen, u8 *dst, unsigned int *dlen,
113			   void *ctx)
114{
115	return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
116}
117
118static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
119				 unsigned int slen, u8 *dst,
120				 unsigned int *dlen)
121{
122	return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
123}
124
125static struct crypto_alg alg_lz4 = {
126	.cra_name		= "lz4",
 
127	.cra_flags		= CRYPTO_ALG_TYPE_COMPRESS,
128	.cra_ctxsize		= sizeof(struct lz4_ctx),
129	.cra_module		= THIS_MODULE,
130	.cra_list		= LIST_HEAD_INIT(alg_lz4.cra_list),
131	.cra_init		= lz4_init,
132	.cra_exit		= lz4_exit,
133	.cra_u			= { .compress = {
134	.coa_compress		= lz4_compress_crypto,
135	.coa_decompress		= lz4_decompress_crypto } }
136};
137
138static struct scomp_alg scomp = {
139	.alloc_ctx		= lz4_alloc_ctx,
140	.free_ctx		= lz4_free_ctx,
141	.compress		= lz4_scompress,
142	.decompress		= lz4_sdecompress,
143	.base			= {
144		.cra_name	= "lz4",
145		.cra_driver_name = "lz4-scomp",
146		.cra_module	 = THIS_MODULE,
147	}
148};
149
150static int __init lz4_mod_init(void)
151{
152	int ret;
153
154	ret = crypto_register_alg(&alg_lz4);
155	if (ret)
156		return ret;
157
158	ret = crypto_register_scomp(&scomp);
159	if (ret) {
160		crypto_unregister_alg(&alg_lz4);
161		return ret;
162	}
163
164	return ret;
165}
166
167static void __exit lz4_mod_fini(void)
168{
169	crypto_unregister_alg(&alg_lz4);
170	crypto_unregister_scomp(&scomp);
171}
172
173module_init(lz4_mod_init);
174module_exit(lz4_mod_fini);
175
176MODULE_LICENSE("GPL");
177MODULE_DESCRIPTION("LZ4 Compression Algorithm");
178MODULE_ALIAS_CRYPTO("lz4");