^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Cryptographic API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2013 Chanho Min <chanho.min@lge.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/lz4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <crypto/internal/scompress.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct lz4_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) void *lz4_comp_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static void *lz4_alloc_ctx(struct crypto_scomp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) void *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) ctx = vmalloc(LZ4_MEM_COMPRESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static int lz4_init(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) ctx->lz4_comp_mem = lz4_alloc_ctx(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (IS_ERR(ctx->lz4_comp_mem))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static void lz4_free_ctx(struct crypto_scomp *tfm, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) vfree(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static void lz4_exit(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) lz4_free_ctx(NULL, ctx->lz4_comp_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static int __lz4_compress_crypto(const u8 *src, unsigned int slen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) u8 *dst, unsigned int *dlen, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int out_len = LZ4_compress_default(src, dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) slen, *dlen, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (!out_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *dlen = out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static int lz4_scompress(struct crypto_scomp *tfm, const u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned int slen, u8 *dst, unsigned int *dlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return __lz4_compress_crypto(src, slen, dst, dlen, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned int slen, u8 *dst, unsigned int *dlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return __lz4_compress_crypto(src, slen, dst, dlen, ctx->lz4_comp_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int __lz4_decompress_crypto(const u8 *src, unsigned int slen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) u8 *dst, unsigned int *dlen, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int out_len = LZ4_decompress_safe(src, dst, slen, *dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (out_len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) *dlen = out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) unsigned int slen, u8 *dst, unsigned int *dlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) unsigned int slen, u8 *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) unsigned int *dlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static struct crypto_alg alg_lz4 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .cra_name = "lz4",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .cra_driver_name = "lz4-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .cra_ctxsize = sizeof(struct lz4_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .cra_init = lz4_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .cra_exit = lz4_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .cra_u = { .compress = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) .coa_compress = lz4_compress_crypto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .coa_decompress = lz4_decompress_crypto } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static struct scomp_alg scomp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .alloc_ctx = lz4_alloc_ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .free_ctx = lz4_free_ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .compress = lz4_scompress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .decompress = lz4_sdecompress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) .cra_name = "lz4",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) .cra_driver_name = "lz4-scomp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int __init lz4_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ret = crypto_register_alg(&alg_lz4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) ret = crypto_register_scomp(&scomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) crypto_unregister_alg(&alg_lz4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static void __exit lz4_mod_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) crypto_unregister_alg(&alg_lz4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) crypto_unregister_scomp(&scomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) subsys_initcall(lz4_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) module_exit(lz4_mod_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) MODULE_DESCRIPTION("LZ4 Compression Algorithm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) MODULE_ALIAS_CRYPTO("lz4");