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