Merge tag 'imx-fixes-4.1' of git://git.kernel.org/pub/scm/linux/kernel/git/shawnguo...
[linux/fpc-iii.git] / crypto / lz4hc.c
bloba1d3b5bd3d85118c681098382fb2dd00adc1dac8
1 /*
2 * Cryptographic API.
4 * Copyright (c) 2013 Chanho Min <chanho.min@lge.com>
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.
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.
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
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>
26 struct lz4hc_ctx {
27 void *lz4hc_comp_mem;
30 static int lz4hc_init(struct crypto_tfm *tfm)
32 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
34 ctx->lz4hc_comp_mem = vmalloc(LZ4HC_MEM_COMPRESS);
35 if (!ctx->lz4hc_comp_mem)
36 return -ENOMEM;
38 return 0;
41 static void lz4hc_exit(struct crypto_tfm *tfm)
43 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
45 vfree(ctx->lz4hc_comp_mem);
48 static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
49 unsigned int slen, u8 *dst, unsigned int *dlen)
51 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
52 size_t tmp_len = *dlen;
53 int err;
55 err = lz4hc_compress(src, slen, dst, &tmp_len, ctx->lz4hc_comp_mem);
57 if (err < 0)
58 return -EINVAL;
60 *dlen = tmp_len;
61 return 0;
64 static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
65 unsigned int slen, u8 *dst, unsigned int *dlen)
67 int err;
68 size_t tmp_len = *dlen;
69 size_t __slen = slen;
71 err = lz4_decompress_unknownoutputsize(src, __slen, dst, &tmp_len);
72 if (err < 0)
73 return -EINVAL;
75 *dlen = tmp_len;
76 return err;
79 static 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 } }
92 static int __init lz4hc_mod_init(void)
94 return crypto_register_alg(&alg_lz4hc);
97 static void __exit lz4hc_mod_fini(void)
99 crypto_unregister_alg(&alg_lz4hc);
102 module_init(lz4hc_mod_init);
103 module_exit(lz4hc_mod_fini);
105 MODULE_LICENSE("GPL");
106 MODULE_DESCRIPTION("LZ4HC Compression Algorithm");
107 MODULE_ALIAS_CRYPTO("lz4hc");