Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Cryptographic API.
0004  */
0005 
0006 #include <linux/init.h>
0007 #include <linux/module.h>
0008 #include <linux/crypto.h>
0009 #include <linux/vmalloc.h>
0010 #include <linux/mm.h>
0011 #include <linux/lzo.h>
0012 #include <crypto/internal/scompress.h>
0013 
0014 struct lzorle_ctx {
0015     void *lzorle_comp_mem;
0016 };
0017 
0018 static void *lzorle_alloc_ctx(struct crypto_scomp *tfm)
0019 {
0020     void *ctx;
0021 
0022     ctx = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
0023     if (!ctx)
0024         return ERR_PTR(-ENOMEM);
0025 
0026     return ctx;
0027 }
0028 
0029 static int lzorle_init(struct crypto_tfm *tfm)
0030 {
0031     struct lzorle_ctx *ctx = crypto_tfm_ctx(tfm);
0032 
0033     ctx->lzorle_comp_mem = lzorle_alloc_ctx(NULL);
0034     if (IS_ERR(ctx->lzorle_comp_mem))
0035         return -ENOMEM;
0036 
0037     return 0;
0038 }
0039 
0040 static void lzorle_free_ctx(struct crypto_scomp *tfm, void *ctx)
0041 {
0042     kvfree(ctx);
0043 }
0044 
0045 static void lzorle_exit(struct crypto_tfm *tfm)
0046 {
0047     struct lzorle_ctx *ctx = crypto_tfm_ctx(tfm);
0048 
0049     lzorle_free_ctx(NULL, ctx->lzorle_comp_mem);
0050 }
0051 
0052 static int __lzorle_compress(const u8 *src, unsigned int slen,
0053               u8 *dst, unsigned int *dlen, void *ctx)
0054 {
0055     size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
0056     int err;
0057 
0058     err = lzorle1x_1_compress(src, slen, dst, &tmp_len, ctx);
0059 
0060     if (err != LZO_E_OK)
0061         return -EINVAL;
0062 
0063     *dlen = tmp_len;
0064     return 0;
0065 }
0066 
0067 static int lzorle_compress(struct crypto_tfm *tfm, const u8 *src,
0068             unsigned int slen, u8 *dst, unsigned int *dlen)
0069 {
0070     struct lzorle_ctx *ctx = crypto_tfm_ctx(tfm);
0071 
0072     return __lzorle_compress(src, slen, dst, dlen, ctx->lzorle_comp_mem);
0073 }
0074 
0075 static int lzorle_scompress(struct crypto_scomp *tfm, const u8 *src,
0076              unsigned int slen, u8 *dst, unsigned int *dlen,
0077              void *ctx)
0078 {
0079     return __lzorle_compress(src, slen, dst, dlen, ctx);
0080 }
0081 
0082 static int __lzorle_decompress(const u8 *src, unsigned int slen,
0083                 u8 *dst, unsigned int *dlen)
0084 {
0085     int err;
0086     size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
0087 
0088     err = lzo1x_decompress_safe(src, slen, dst, &tmp_len);
0089 
0090     if (err != LZO_E_OK)
0091         return -EINVAL;
0092 
0093     *dlen = tmp_len;
0094     return 0;
0095 }
0096 
0097 static int lzorle_decompress(struct crypto_tfm *tfm, const u8 *src,
0098               unsigned int slen, u8 *dst, unsigned int *dlen)
0099 {
0100     return __lzorle_decompress(src, slen, dst, dlen);
0101 }
0102 
0103 static int lzorle_sdecompress(struct crypto_scomp *tfm, const u8 *src,
0104                unsigned int slen, u8 *dst, unsigned int *dlen,
0105                void *ctx)
0106 {
0107     return __lzorle_decompress(src, slen, dst, dlen);
0108 }
0109 
0110 static struct crypto_alg alg = {
0111     .cra_name       = "lzo-rle",
0112     .cra_driver_name    = "lzo-rle-generic",
0113     .cra_flags      = CRYPTO_ALG_TYPE_COMPRESS,
0114     .cra_ctxsize        = sizeof(struct lzorle_ctx),
0115     .cra_module     = THIS_MODULE,
0116     .cra_init       = lzorle_init,
0117     .cra_exit       = lzorle_exit,
0118     .cra_u          = { .compress = {
0119     .coa_compress       = lzorle_compress,
0120     .coa_decompress     = lzorle_decompress } }
0121 };
0122 
0123 static struct scomp_alg scomp = {
0124     .alloc_ctx      = lzorle_alloc_ctx,
0125     .free_ctx       = lzorle_free_ctx,
0126     .compress       = lzorle_scompress,
0127     .decompress     = lzorle_sdecompress,
0128     .base           = {
0129         .cra_name   = "lzo-rle",
0130         .cra_driver_name = "lzo-rle-scomp",
0131         .cra_module  = THIS_MODULE,
0132     }
0133 };
0134 
0135 static int __init lzorle_mod_init(void)
0136 {
0137     int ret;
0138 
0139     ret = crypto_register_alg(&alg);
0140     if (ret)
0141         return ret;
0142 
0143     ret = crypto_register_scomp(&scomp);
0144     if (ret) {
0145         crypto_unregister_alg(&alg);
0146         return ret;
0147     }
0148 
0149     return ret;
0150 }
0151 
0152 static void __exit lzorle_mod_fini(void)
0153 {
0154     crypto_unregister_alg(&alg);
0155     crypto_unregister_scomp(&scomp);
0156 }
0157 
0158 subsys_initcall(lzorle_mod_init);
0159 module_exit(lzorle_mod_fini);
0160 
0161 MODULE_LICENSE("GPL");
0162 MODULE_DESCRIPTION("LZO-RLE Compression Algorithm");
0163 MODULE_ALIAS_CRYPTO("lzo-rle");