Linux Audio

Check our new training course

Loading...
v3.15
  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#include <linux/init.h>
 21#include <linux/module.h>
 22#include <linux/crypto.h>
 23#include <linux/vmalloc.h>
 24#include <linux/lz4.h>
 
 25
 26struct lz4hc_ctx {
 27	void *lz4hc_comp_mem;
 28};
 29
 
 
 
 
 
 
 
 
 
 
 
 30static int lz4hc_init(struct crypto_tfm *tfm)
 31{
 32	struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
 33
 34	ctx->lz4hc_comp_mem = vmalloc(LZ4HC_MEM_COMPRESS);
 35	if (!ctx->lz4hc_comp_mem)
 36		return -ENOMEM;
 37
 38	return 0;
 39}
 40
 
 
 
 
 
 41static void lz4hc_exit(struct crypto_tfm *tfm)
 42{
 43	struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
 44
 45	vfree(ctx->lz4hc_comp_mem);
 46}
 47
 48static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
 49			    unsigned int slen, u8 *dst, unsigned int *dlen)
 50{
 51	struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
 52	size_t tmp_len = *dlen;
 53	int err;
 54
 55	err = lz4hc_compress(src, slen, dst, &tmp_len, ctx->lz4hc_comp_mem);
 56
 57	if (err < 0)
 58		return -EINVAL;
 59
 60	*dlen = tmp_len;
 61	return 0;
 62}
 63
 64static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
 65			      unsigned int slen, u8 *dst, unsigned int *dlen)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 66{
 67	int err;
 68	size_t tmp_len = *dlen;
 69	size_t __slen = slen;
 70
 71	err = lz4_decompress(src, &__slen, dst, tmp_len);
 72	if (err < 0)
 73		return -EINVAL;
 74
 75	*dlen = tmp_len;
 76	return err;
 77}
 78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 79static struct crypto_alg alg_lz4hc = {
 80	.cra_name		= "lz4hc",
 81	.cra_flags		= CRYPTO_ALG_TYPE_COMPRESS,
 82	.cra_ctxsize		= sizeof(struct lz4hc_ctx),
 83	.cra_module		= THIS_MODULE,
 84	.cra_list		= LIST_HEAD_INIT(alg_lz4hc.cra_list),
 85	.cra_init		= lz4hc_init,
 86	.cra_exit		= lz4hc_exit,
 87	.cra_u			= { .compress = {
 88	.coa_compress		= lz4hc_compress_crypto,
 89	.coa_decompress		= lz4hc_decompress_crypto } }
 90};
 91
 
 
 
 
 
 
 
 
 
 
 
 
 92static int __init lz4hc_mod_init(void)
 93{
 94	return crypto_register_alg(&alg_lz4hc);
 
 
 
 
 
 
 
 
 
 
 
 
 95}
 96
 97static void __exit lz4hc_mod_fini(void)
 98{
 99	crypto_unregister_alg(&alg_lz4hc);
 
100}
101
102module_init(lz4hc_mod_init);
103module_exit(lz4hc_mod_fini);
104
105MODULE_LICENSE("GPL");
106MODULE_DESCRIPTION("LZ4HC Compression Algorithm");
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#include <linux/init.h>
 21#include <linux/module.h>
 22#include <linux/crypto.h>
 23#include <linux/vmalloc.h>
 24#include <linux/lz4.h>
 25#include <crypto/internal/scompress.h>
 26
 27struct lz4hc_ctx {
 28	void *lz4hc_comp_mem;
 29};
 30
 31static void *lz4hc_alloc_ctx(struct crypto_scomp *tfm)
 32{
 33	void *ctx;
 34
 35	ctx = vmalloc(LZ4HC_MEM_COMPRESS);
 36	if (!ctx)
 37		return ERR_PTR(-ENOMEM);
 38
 39	return ctx;
 40}
 41
 42static int lz4hc_init(struct crypto_tfm *tfm)
 43{
 44	struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
 45
 46	ctx->lz4hc_comp_mem = lz4hc_alloc_ctx(NULL);
 47	if (IS_ERR(ctx->lz4hc_comp_mem))
 48		return -ENOMEM;
 49
 50	return 0;
 51}
 52
 53static void lz4hc_free_ctx(struct crypto_scomp *tfm, void *ctx)
 54{
 55	vfree(ctx);
 56}
 57
 58static void lz4hc_exit(struct crypto_tfm *tfm)
 59{
 60	struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
 61
 62	lz4hc_free_ctx(NULL, ctx->lz4hc_comp_mem);
 63}
 64
 65static int __lz4hc_compress_crypto(const u8 *src, unsigned int slen,
 66				   u8 *dst, unsigned int *dlen, void *ctx)
 67{
 
 68	size_t tmp_len = *dlen;
 69	int err;
 70
 71	err = lz4hc_compress(src, slen, dst, &tmp_len, ctx);
 72
 73	if (err < 0)
 74		return -EINVAL;
 75
 76	*dlen = tmp_len;
 77	return 0;
 78}
 79
 80static int lz4hc_scompress(struct crypto_scomp *tfm, const u8 *src,
 81			   unsigned int slen, u8 *dst, unsigned int *dlen,
 82			   void *ctx)
 83{
 84	return __lz4hc_compress_crypto(src, slen, dst, dlen, ctx);
 85}
 86
 87static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
 88				 unsigned int slen, u8 *dst,
 89				 unsigned int *dlen)
 90{
 91	struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
 92
 93	return __lz4hc_compress_crypto(src, slen, dst, dlen,
 94					ctx->lz4hc_comp_mem);
 95}
 96
 97static int __lz4hc_decompress_crypto(const u8 *src, unsigned int slen,
 98				     u8 *dst, unsigned int *dlen, void *ctx)
 99{
100	int err;
101	size_t tmp_len = *dlen;
102	size_t __slen = slen;
103
104	err = lz4_decompress_unknownoutputsize(src, __slen, dst, &tmp_len);
105	if (err < 0)
106		return -EINVAL;
107
108	*dlen = tmp_len;
109	return err;
110}
111
112static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
113			     unsigned int slen, u8 *dst, unsigned int *dlen,
114			     void *ctx)
115{
116	return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
117}
118
119static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
120				   unsigned int slen, u8 *dst,
121				   unsigned int *dlen)
122{
123	return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
124}
125
126static struct crypto_alg alg_lz4hc = {
127	.cra_name		= "lz4hc",
128	.cra_flags		= CRYPTO_ALG_TYPE_COMPRESS,
129	.cra_ctxsize		= sizeof(struct lz4hc_ctx),
130	.cra_module		= THIS_MODULE,
131	.cra_list		= LIST_HEAD_INIT(alg_lz4hc.cra_list),
132	.cra_init		= lz4hc_init,
133	.cra_exit		= lz4hc_exit,
134	.cra_u			= { .compress = {
135	.coa_compress		= lz4hc_compress_crypto,
136	.coa_decompress		= lz4hc_decompress_crypto } }
137};
138
139static struct scomp_alg scomp = {
140	.alloc_ctx		= lz4hc_alloc_ctx,
141	.free_ctx		= lz4hc_free_ctx,
142	.compress		= lz4hc_scompress,
143	.decompress		= lz4hc_sdecompress,
144	.base			= {
145		.cra_name	= "lz4hc",
146		.cra_driver_name = "lz4hc-scomp",
147		.cra_module	 = THIS_MODULE,
148	}
149};
150
151static int __init lz4hc_mod_init(void)
152{
153	int ret;
154
155	ret = crypto_register_alg(&alg_lz4hc);
156	if (ret)
157		return ret;
158
159	ret = crypto_register_scomp(&scomp);
160	if (ret) {
161		crypto_unregister_alg(&alg_lz4hc);
162		return ret;
163	}
164
165	return ret;
166}
167
168static void __exit lz4hc_mod_fini(void)
169{
170	crypto_unregister_alg(&alg_lz4hc);
171	crypto_unregister_scomp(&scomp);
172}
173
174module_init(lz4hc_mod_init);
175module_exit(lz4hc_mod_fini);
176
177MODULE_LICENSE("GPL");
178MODULE_DESCRIPTION("LZ4HC Compression Algorithm");
179MODULE_ALIAS_CRYPTO("lz4hc");