1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Cryptographic API. 4 */ 5 6 #include <crypto/internal/scompress.h> 7 #include <linux/init.h> 8 #include <linux/lzo.h> 9 #include <linux/module.h> 10 #include <linux/slab.h> 11 12 static void *lzo_alloc_ctx(void) 13 { 14 void *ctx; 15 16 ctx = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); 17 if (!ctx) 18 return ERR_PTR(-ENOMEM); 19 20 return ctx; 21 } 22 23 static void lzo_free_ctx(void *ctx) 24 { 25 kvfree(ctx); 26 } 27 28 static int __lzo_compress(const u8 *src, unsigned int slen, 29 u8 *dst, unsigned int *dlen, void *ctx) 30 { 31 size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ 32 int err; 33 34 err = lzo1x_1_compress_safe(src, slen, dst, &tmp_len, ctx); 35 36 if (err != LZO_E_OK) 37 return -EINVAL; 38 39 *dlen = tmp_len; 40 return 0; 41 } 42 43 static int lzo_scompress(struct crypto_scomp *tfm, const u8 *src, 44 unsigned int slen, u8 *dst, unsigned int *dlen, 45 void *ctx) 46 { 47 return __lzo_compress(src, slen, dst, dlen, ctx); 48 } 49 50 static int __lzo_decompress(const u8 *src, unsigned int slen, 51 u8 *dst, unsigned int *dlen) 52 { 53 int err; 54 size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ 55 56 err = lzo1x_decompress_safe(src, slen, dst, &tmp_len); 57 58 if (err != LZO_E_OK) 59 return -EINVAL; 60 61 *dlen = tmp_len; 62 return 0; 63 } 64 65 static int lzo_sdecompress(struct crypto_scomp *tfm, const u8 *src, 66 unsigned int slen, u8 *dst, unsigned int *dlen, 67 void *ctx) 68 { 69 return __lzo_decompress(src, slen, dst, dlen); 70 } 71 72 static struct scomp_alg scomp = { 73 .streams = { 74 .alloc_ctx = lzo_alloc_ctx, 75 .free_ctx = lzo_free_ctx, 76 }, 77 .compress = lzo_scompress, 78 .decompress = lzo_sdecompress, 79 .base = { 80 .cra_name = "lzo", 81 .cra_driver_name = "lzo-scomp", 82 .cra_module = THIS_MODULE, 83 } 84 }; 85 86 static int __init lzo_mod_init(void) 87 { 88 return crypto_register_scomp(&scomp); 89 } 90 91 static void __exit lzo_mod_fini(void) 92 { 93 crypto_unregister_scomp(&scomp); 94 } 95 96 module_init(lzo_mod_init); 97 module_exit(lzo_mod_fini); 98 99 MODULE_LICENSE("GPL"); 100 MODULE_DESCRIPTION("LZO Compression Algorithm"); 101 MODULE_ALIAS_CRYPTO("lzo"); 102