1*0ea8530dSChanho Min /* 2*0ea8530dSChanho Min * Cryptographic API. 3*0ea8530dSChanho Min * 4*0ea8530dSChanho Min * Copyright (c) 2013 Chanho Min <chanho.min@lge.com> 5*0ea8530dSChanho Min * 6*0ea8530dSChanho Min * This program is free software; you can redistribute it and/or modify it 7*0ea8530dSChanho Min * under the terms of the GNU General Public License version 2 as published by 8*0ea8530dSChanho Min * the Free Software Foundation. 9*0ea8530dSChanho Min * 10*0ea8530dSChanho Min * This program is distributed in the hope that it will be useful, but WITHOUT 11*0ea8530dSChanho Min * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12*0ea8530dSChanho Min * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13*0ea8530dSChanho Min * more details. 14*0ea8530dSChanho Min * 15*0ea8530dSChanho Min * You should have received a copy of the GNU General Public License along with 16*0ea8530dSChanho Min * this program; if not, write to the Free Software Foundation, Inc., 51 17*0ea8530dSChanho Min * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18*0ea8530dSChanho Min * 19*0ea8530dSChanho Min */ 20*0ea8530dSChanho Min #include <linux/init.h> 21*0ea8530dSChanho Min #include <linux/module.h> 22*0ea8530dSChanho Min #include <linux/crypto.h> 23*0ea8530dSChanho Min #include <linux/vmalloc.h> 24*0ea8530dSChanho Min #include <linux/lz4.h> 25*0ea8530dSChanho Min 26*0ea8530dSChanho Min struct lz4hc_ctx { 27*0ea8530dSChanho Min void *lz4hc_comp_mem; 28*0ea8530dSChanho Min }; 29*0ea8530dSChanho Min 30*0ea8530dSChanho Min static int lz4hc_init(struct crypto_tfm *tfm) 31*0ea8530dSChanho Min { 32*0ea8530dSChanho Min struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); 33*0ea8530dSChanho Min 34*0ea8530dSChanho Min ctx->lz4hc_comp_mem = vmalloc(LZ4HC_MEM_COMPRESS); 35*0ea8530dSChanho Min if (!ctx->lz4hc_comp_mem) 36*0ea8530dSChanho Min return -ENOMEM; 37*0ea8530dSChanho Min 38*0ea8530dSChanho Min return 0; 39*0ea8530dSChanho Min } 40*0ea8530dSChanho Min 41*0ea8530dSChanho Min static void lz4hc_exit(struct crypto_tfm *tfm) 42*0ea8530dSChanho Min { 43*0ea8530dSChanho Min struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); 44*0ea8530dSChanho Min 45*0ea8530dSChanho Min vfree(ctx->lz4hc_comp_mem); 46*0ea8530dSChanho Min } 47*0ea8530dSChanho Min 48*0ea8530dSChanho Min static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src, 49*0ea8530dSChanho Min unsigned int slen, u8 *dst, unsigned int *dlen) 50*0ea8530dSChanho Min { 51*0ea8530dSChanho Min struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); 52*0ea8530dSChanho Min size_t tmp_len = *dlen; 53*0ea8530dSChanho Min int err; 54*0ea8530dSChanho Min 55*0ea8530dSChanho Min err = lz4hc_compress(src, slen, dst, &tmp_len, ctx->lz4hc_comp_mem); 56*0ea8530dSChanho Min 57*0ea8530dSChanho Min if (err < 0) 58*0ea8530dSChanho Min return -EINVAL; 59*0ea8530dSChanho Min 60*0ea8530dSChanho Min *dlen = tmp_len; 61*0ea8530dSChanho Min return 0; 62*0ea8530dSChanho Min } 63*0ea8530dSChanho Min 64*0ea8530dSChanho Min static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, 65*0ea8530dSChanho Min unsigned int slen, u8 *dst, unsigned int *dlen) 66*0ea8530dSChanho Min { 67*0ea8530dSChanho Min int err; 68*0ea8530dSChanho Min size_t tmp_len = *dlen; 69*0ea8530dSChanho Min size_t __slen = slen; 70*0ea8530dSChanho Min 71*0ea8530dSChanho Min err = lz4_decompress(src, &__slen, dst, tmp_len); 72*0ea8530dSChanho Min if (err < 0) 73*0ea8530dSChanho Min return -EINVAL; 74*0ea8530dSChanho Min 75*0ea8530dSChanho Min *dlen = tmp_len; 76*0ea8530dSChanho Min return err; 77*0ea8530dSChanho Min } 78*0ea8530dSChanho Min 79*0ea8530dSChanho Min static struct crypto_alg alg_lz4hc = { 80*0ea8530dSChanho Min .cra_name = "lz4hc", 81*0ea8530dSChanho Min .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, 82*0ea8530dSChanho Min .cra_ctxsize = sizeof(struct lz4hc_ctx), 83*0ea8530dSChanho Min .cra_module = THIS_MODULE, 84*0ea8530dSChanho Min .cra_list = LIST_HEAD_INIT(alg_lz4hc.cra_list), 85*0ea8530dSChanho Min .cra_init = lz4hc_init, 86*0ea8530dSChanho Min .cra_exit = lz4hc_exit, 87*0ea8530dSChanho Min .cra_u = { .compress = { 88*0ea8530dSChanho Min .coa_compress = lz4hc_compress_crypto, 89*0ea8530dSChanho Min .coa_decompress = lz4hc_decompress_crypto } } 90*0ea8530dSChanho Min }; 91*0ea8530dSChanho Min 92*0ea8530dSChanho Min static int __init lz4hc_mod_init(void) 93*0ea8530dSChanho Min { 94*0ea8530dSChanho Min return crypto_register_alg(&alg_lz4hc); 95*0ea8530dSChanho Min } 96*0ea8530dSChanho Min 97*0ea8530dSChanho Min static void __exit lz4hc_mod_fini(void) 98*0ea8530dSChanho Min { 99*0ea8530dSChanho Min crypto_unregister_alg(&alg_lz4hc); 100*0ea8530dSChanho Min } 101*0ea8530dSChanho Min 102*0ea8530dSChanho Min module_init(lz4hc_mod_init); 103*0ea8530dSChanho Min module_exit(lz4hc_mod_fini); 104*0ea8530dSChanho Min 105*0ea8530dSChanho Min MODULE_LICENSE("GPL"); 106*0ea8530dSChanho Min MODULE_DESCRIPTION("LZ4HC Compression Algorithm"); 107