aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/lz4.c
diff options
context:
space:
mode:
authorChanho Min <chanho.min@lge.com>2013-07-08 19:01:51 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2013-07-09 13:33:30 -0400
commit0ea8530dcf762526459b29ac713a623b51fd691f (patch)
tree65c8e2ff2ec2502ea7200a8ac338419e668a73bb /crypto/lz4.c
parentc72ac7a1a926dbffb59daf0f275450e5eecce16f (diff)
crypto: add lz4 Cryptographic API
Add support for lz4 and lz4hc compression algorithm using the lib/lz4/* codebase. [akpm@linux-foundation.org: fix warnings] Signed-off-by: Chanho Min <chanho.min@lge.com> Cc: "Darrick J. Wong" <djwong@us.ibm.com> Cc: Bob Pearson <rpearson@systemfabricworks.com> Cc: Richard Weinberger <richard@nod.at> Cc: Herbert Xu <herbert@gondor.hengli.com.au> Cc: Yann Collet <yann.collet.73@gmail.com> Cc: Kyungsik Lee <kyungsik.lee@lge.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'crypto/lz4.c')
-rw-r--r--crypto/lz4.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/crypto/lz4.c b/crypto/lz4.c
new file mode 100644
index 000000000000..4586dd15b0d8
--- /dev/null
+++ b/crypto/lz4.c
@@ -0,0 +1,106 @@
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
27struct lz4_ctx {
28 void *lz4_comp_mem;
29};
30
31static int lz4_init(struct crypto_tfm *tfm)
32{
33 struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
34
35 ctx->lz4_comp_mem = vmalloc(LZ4_MEM_COMPRESS);
36 if (!ctx->lz4_comp_mem)
37 return -ENOMEM;
38
39 return 0;
40}
41
42static void lz4_exit(struct crypto_tfm *tfm)
43{
44 struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
45 vfree(ctx->lz4_comp_mem);
46}
47
48static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
49 unsigned int slen, u8 *dst, unsigned int *dlen)
50{
51 struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
52 size_t tmp_len = *dlen;
53 int err;
54
55 err = lz4_compress(src, slen, dst, &tmp_len, ctx->lz4_comp_mem);
56
57 if (err < 0)
58 return -EINVAL;
59
60 *dlen = tmp_len;
61 return 0;
62}
63
64static int lz4_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_lz4 = {
80 .cra_name = "lz4",
81 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
82 .cra_ctxsize = sizeof(struct lz4_ctx),
83 .cra_module = THIS_MODULE,
84 .cra_list = LIST_HEAD_INIT(alg_lz4.cra_list),
85 .cra_init = lz4_init,
86 .cra_exit = lz4_exit,
87 .cra_u = { .compress = {
88 .coa_compress = lz4_compress_crypto,
89 .coa_decompress = lz4_decompress_crypto } }
90};
91
92static int __init lz4_mod_init(void)
93{
94 return crypto_register_alg(&alg_lz4);
95}
96
97static void __exit lz4_mod_fini(void)
98{
99 crypto_unregister_alg(&alg_lz4);
100}
101
102module_init(lz4_mod_init);
103module_exit(lz4_mod_fini);
104
105MODULE_LICENSE("GPL");
106MODULE_DESCRIPTION("LZ4 Compression Algorithm");