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 *lzorle_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 lzorle_free_ctx(void *ctx) 24 { 25 kvfree(ctx); 26 } 27 28 static int __lzorle_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 = lzorle1x_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 lzorle_scompress(struct crypto_scomp *tfm, const u8 *src, 44 unsigned int slen, u8 *dst, unsigned int *dlen, 45 void *ctx) 46 { 47 return __lzorle_compress(src, slen, dst, dlen, ctx); 48 } 49 50 static int __lzorle_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 lzorle_sdecompress(struct crypto_scomp *tfm, const u8 *src, 66 unsigned int slen, u8 *dst, unsigned int *dlen, 67 void *ctx) 68 { 69 return __lzorle_decompress(src, slen, dst, dlen); 70 } 71 72 static struct scomp_alg scomp = { 73 .alloc_ctx = lzorle_alloc_ctx, 74 .free_ctx = lzorle_free_ctx, 75 .compress = lzorle_scompress, 76 .decompress = lzorle_sdecompress, 77 .base = { 78 .cra_name = "lzo-rle", 79 .cra_driver_name = "lzo-rle-scomp", 80 .cra_module = THIS_MODULE, 81 } 82 }; 83 84 static int __init lzorle_mod_init(void) 85 { 86 return crypto_register_scomp(&scomp); 87 } 88 89 static void __exit lzorle_mod_fini(void) 90 { 91 crypto_unregister_scomp(&scomp); 92 } 93 94 module_init(lzorle_mod_init); 95 module_exit(lzorle_mod_fini); 96 97 MODULE_LICENSE("GPL"); 98 MODULE_DESCRIPTION("LZO-RLE Compression Algorithm"); 99 MODULE_ALIAS_CRYPTO("lzo-rle"); 100