10ea8530dSChanho Min /* 20ea8530dSChanho Min * Cryptographic API. 30ea8530dSChanho Min * 40ea8530dSChanho Min * Copyright (c) 2013 Chanho Min <chanho.min@lge.com> 50ea8530dSChanho Min * 60ea8530dSChanho Min * This program is free software; you can redistribute it and/or modify it 70ea8530dSChanho Min * under the terms of the GNU General Public License version 2 as published by 80ea8530dSChanho Min * the Free Software Foundation. 90ea8530dSChanho Min * 100ea8530dSChanho Min * This program is distributed in the hope that it will be useful, but WITHOUT 110ea8530dSChanho Min * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 120ea8530dSChanho Min * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 130ea8530dSChanho Min * more details. 140ea8530dSChanho Min * 150ea8530dSChanho Min * You should have received a copy of the GNU General Public License along with 160ea8530dSChanho Min * this program; if not, write to the Free Software Foundation, Inc., 51 170ea8530dSChanho Min * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 180ea8530dSChanho Min * 190ea8530dSChanho Min */ 200ea8530dSChanho Min #include <linux/init.h> 210ea8530dSChanho Min #include <linux/module.h> 220ea8530dSChanho Min #include <linux/crypto.h> 230ea8530dSChanho Min #include <linux/vmalloc.h> 240ea8530dSChanho Min #include <linux/lz4.h> 250ea8530dSChanho Min 260ea8530dSChanho Min struct lz4hc_ctx { 270ea8530dSChanho Min void *lz4hc_comp_mem; 280ea8530dSChanho Min }; 290ea8530dSChanho Min 300ea8530dSChanho Min static int lz4hc_init(struct crypto_tfm *tfm) 310ea8530dSChanho Min { 320ea8530dSChanho Min struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); 330ea8530dSChanho Min 340ea8530dSChanho Min ctx->lz4hc_comp_mem = vmalloc(LZ4HC_MEM_COMPRESS); 350ea8530dSChanho Min if (!ctx->lz4hc_comp_mem) 360ea8530dSChanho Min return -ENOMEM; 370ea8530dSChanho Min 380ea8530dSChanho Min return 0; 390ea8530dSChanho Min } 400ea8530dSChanho Min 410ea8530dSChanho Min static void lz4hc_exit(struct crypto_tfm *tfm) 420ea8530dSChanho Min { 430ea8530dSChanho Min struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); 440ea8530dSChanho Min 450ea8530dSChanho Min vfree(ctx->lz4hc_comp_mem); 460ea8530dSChanho Min } 470ea8530dSChanho Min 480ea8530dSChanho Min static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src, 490ea8530dSChanho Min unsigned int slen, u8 *dst, unsigned int *dlen) 500ea8530dSChanho Min { 510ea8530dSChanho Min struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); 520ea8530dSChanho Min size_t tmp_len = *dlen; 530ea8530dSChanho Min int err; 540ea8530dSChanho Min 550ea8530dSChanho Min err = lz4hc_compress(src, slen, dst, &tmp_len, ctx->lz4hc_comp_mem); 560ea8530dSChanho Min 570ea8530dSChanho Min if (err < 0) 580ea8530dSChanho Min return -EINVAL; 590ea8530dSChanho Min 600ea8530dSChanho Min *dlen = tmp_len; 610ea8530dSChanho Min return 0; 620ea8530dSChanho Min } 630ea8530dSChanho Min 640ea8530dSChanho Min static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, 650ea8530dSChanho Min unsigned int slen, u8 *dst, unsigned int *dlen) 660ea8530dSChanho Min { 670ea8530dSChanho Min int err; 680ea8530dSChanho Min size_t tmp_len = *dlen; 690ea8530dSChanho Min size_t __slen = slen; 700ea8530dSChanho Min 71*d801ab20SKOVACS Krisztian err = lz4_decompress_unknownoutputsize(src, __slen, dst, &tmp_len); 720ea8530dSChanho Min if (err < 0) 730ea8530dSChanho Min return -EINVAL; 740ea8530dSChanho Min 750ea8530dSChanho Min *dlen = tmp_len; 760ea8530dSChanho Min return err; 770ea8530dSChanho Min } 780ea8530dSChanho Min 790ea8530dSChanho Min static struct crypto_alg alg_lz4hc = { 800ea8530dSChanho Min .cra_name = "lz4hc", 810ea8530dSChanho Min .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, 820ea8530dSChanho Min .cra_ctxsize = sizeof(struct lz4hc_ctx), 830ea8530dSChanho Min .cra_module = THIS_MODULE, 840ea8530dSChanho Min .cra_list = LIST_HEAD_INIT(alg_lz4hc.cra_list), 850ea8530dSChanho Min .cra_init = lz4hc_init, 860ea8530dSChanho Min .cra_exit = lz4hc_exit, 870ea8530dSChanho Min .cra_u = { .compress = { 880ea8530dSChanho Min .coa_compress = lz4hc_compress_crypto, 890ea8530dSChanho Min .coa_decompress = lz4hc_decompress_crypto } } 900ea8530dSChanho Min }; 910ea8530dSChanho Min 920ea8530dSChanho Min static int __init lz4hc_mod_init(void) 930ea8530dSChanho Min { 940ea8530dSChanho Min return crypto_register_alg(&alg_lz4hc); 950ea8530dSChanho Min } 960ea8530dSChanho Min 970ea8530dSChanho Min static void __exit lz4hc_mod_fini(void) 980ea8530dSChanho Min { 990ea8530dSChanho Min crypto_unregister_alg(&alg_lz4hc); 1000ea8530dSChanho Min } 1010ea8530dSChanho Min 1020ea8530dSChanho Min module_init(lz4hc_mod_init); 1030ea8530dSChanho Min module_exit(lz4hc_mod_fini); 1040ea8530dSChanho Min 1050ea8530dSChanho Min MODULE_LICENSE("GPL"); 1060ea8530dSChanho Min MODULE_DESCRIPTION("LZ4HC Compression Algorithm"); 107