0001
0002
0003
0004
0005
0006
0007 #include <linux/init.h>
0008 #include <linux/module.h>
0009 #include <linux/crypto.h>
0010 #include <linux/vmalloc.h>
0011 #include <linux/lz4.h>
0012 #include <crypto/internal/scompress.h>
0013
0014 struct lz4hc_ctx {
0015 void *lz4hc_comp_mem;
0016 };
0017
0018 static void *lz4hc_alloc_ctx(struct crypto_scomp *tfm)
0019 {
0020 void *ctx;
0021
0022 ctx = vmalloc(LZ4HC_MEM_COMPRESS);
0023 if (!ctx)
0024 return ERR_PTR(-ENOMEM);
0025
0026 return ctx;
0027 }
0028
0029 static int lz4hc_init(struct crypto_tfm *tfm)
0030 {
0031 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
0032
0033 ctx->lz4hc_comp_mem = lz4hc_alloc_ctx(NULL);
0034 if (IS_ERR(ctx->lz4hc_comp_mem))
0035 return -ENOMEM;
0036
0037 return 0;
0038 }
0039
0040 static void lz4hc_free_ctx(struct crypto_scomp *tfm, void *ctx)
0041 {
0042 vfree(ctx);
0043 }
0044
0045 static void lz4hc_exit(struct crypto_tfm *tfm)
0046 {
0047 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
0048
0049 lz4hc_free_ctx(NULL, ctx->lz4hc_comp_mem);
0050 }
0051
0052 static int __lz4hc_compress_crypto(const u8 *src, unsigned int slen,
0053 u8 *dst, unsigned int *dlen, void *ctx)
0054 {
0055 int out_len = LZ4_compress_HC(src, dst, slen,
0056 *dlen, LZ4HC_DEFAULT_CLEVEL, ctx);
0057
0058 if (!out_len)
0059 return -EINVAL;
0060
0061 *dlen = out_len;
0062 return 0;
0063 }
0064
0065 static int lz4hc_scompress(struct crypto_scomp *tfm, const u8 *src,
0066 unsigned int slen, u8 *dst, unsigned int *dlen,
0067 void *ctx)
0068 {
0069 return __lz4hc_compress_crypto(src, slen, dst, dlen, ctx);
0070 }
0071
0072 static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
0073 unsigned int slen, u8 *dst,
0074 unsigned int *dlen)
0075 {
0076 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
0077
0078 return __lz4hc_compress_crypto(src, slen, dst, dlen,
0079 ctx->lz4hc_comp_mem);
0080 }
0081
0082 static int __lz4hc_decompress_crypto(const u8 *src, unsigned int slen,
0083 u8 *dst, unsigned int *dlen, void *ctx)
0084 {
0085 int out_len = LZ4_decompress_safe(src, dst, slen, *dlen);
0086
0087 if (out_len < 0)
0088 return -EINVAL;
0089
0090 *dlen = out_len;
0091 return 0;
0092 }
0093
0094 static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
0095 unsigned int slen, u8 *dst, unsigned int *dlen,
0096 void *ctx)
0097 {
0098 return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
0099 }
0100
0101 static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
0102 unsigned int slen, u8 *dst,
0103 unsigned int *dlen)
0104 {
0105 return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
0106 }
0107
0108 static struct crypto_alg alg_lz4hc = {
0109 .cra_name = "lz4hc",
0110 .cra_driver_name = "lz4hc-generic",
0111 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
0112 .cra_ctxsize = sizeof(struct lz4hc_ctx),
0113 .cra_module = THIS_MODULE,
0114 .cra_init = lz4hc_init,
0115 .cra_exit = lz4hc_exit,
0116 .cra_u = { .compress = {
0117 .coa_compress = lz4hc_compress_crypto,
0118 .coa_decompress = lz4hc_decompress_crypto } }
0119 };
0120
0121 static struct scomp_alg scomp = {
0122 .alloc_ctx = lz4hc_alloc_ctx,
0123 .free_ctx = lz4hc_free_ctx,
0124 .compress = lz4hc_scompress,
0125 .decompress = lz4hc_sdecompress,
0126 .base = {
0127 .cra_name = "lz4hc",
0128 .cra_driver_name = "lz4hc-scomp",
0129 .cra_module = THIS_MODULE,
0130 }
0131 };
0132
0133 static int __init lz4hc_mod_init(void)
0134 {
0135 int ret;
0136
0137 ret = crypto_register_alg(&alg_lz4hc);
0138 if (ret)
0139 return ret;
0140
0141 ret = crypto_register_scomp(&scomp);
0142 if (ret) {
0143 crypto_unregister_alg(&alg_lz4hc);
0144 return ret;
0145 }
0146
0147 return ret;
0148 }
0149
0150 static void __exit lz4hc_mod_fini(void)
0151 {
0152 crypto_unregister_alg(&alg_lz4hc);
0153 crypto_unregister_scomp(&scomp);
0154 }
0155
0156 subsys_initcall(lz4hc_mod_init);
0157 module_exit(lz4hc_mod_fini);
0158
0159 MODULE_LICENSE("GPL");
0160 MODULE_DESCRIPTION("LZ4HC Compression Algorithm");
0161 MODULE_ALIAS_CRYPTO("lz4hc");