lz4hc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Cryptographic API.
  4. *
  5. * Copyright (c) 2013 Chanho Min <chanho.min@lge.com>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/crypto.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/lz4.h>
  12. #include <crypto/internal/scompress.h>
  13. struct lz4hc_ctx {
  14. void *lz4hc_comp_mem;
  15. };
  16. static void *lz4hc_alloc_ctx(struct crypto_scomp *tfm)
  17. {
  18. void *ctx;
  19. ctx = vmalloc(LZ4HC_MEM_COMPRESS);
  20. if (!ctx)
  21. return ERR_PTR(-ENOMEM);
  22. return ctx;
  23. }
  24. static int lz4hc_init(struct crypto_tfm *tfm)
  25. {
  26. struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
  27. ctx->lz4hc_comp_mem = lz4hc_alloc_ctx(NULL);
  28. if (IS_ERR(ctx->lz4hc_comp_mem))
  29. return -ENOMEM;
  30. return 0;
  31. }
  32. static void lz4hc_free_ctx(struct crypto_scomp *tfm, void *ctx)
  33. {
  34. vfree(ctx);
  35. }
  36. static void lz4hc_exit(struct crypto_tfm *tfm)
  37. {
  38. struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
  39. lz4hc_free_ctx(NULL, ctx->lz4hc_comp_mem);
  40. }
  41. static int __lz4hc_compress_crypto(const u8 *src, unsigned int slen,
  42. u8 *dst, unsigned int *dlen, void *ctx)
  43. {
  44. int out_len = LZ4_compress_HC(src, dst, slen,
  45. *dlen, LZ4HC_DEFAULT_CLEVEL, ctx);
  46. if (!out_len)
  47. return -EINVAL;
  48. *dlen = out_len;
  49. return 0;
  50. }
  51. static int lz4hc_scompress(struct crypto_scomp *tfm, const u8 *src,
  52. unsigned int slen, u8 *dst, unsigned int *dlen,
  53. void *ctx)
  54. {
  55. return __lz4hc_compress_crypto(src, slen, dst, dlen, ctx);
  56. }
  57. static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
  58. unsigned int slen, u8 *dst,
  59. unsigned int *dlen)
  60. {
  61. struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
  62. return __lz4hc_compress_crypto(src, slen, dst, dlen,
  63. ctx->lz4hc_comp_mem);
  64. }
  65. static int __lz4hc_decompress_crypto(const u8 *src, unsigned int slen,
  66. u8 *dst, unsigned int *dlen, void *ctx)
  67. {
  68. int out_len = LZ4_decompress_safe(src, dst, slen, *dlen);
  69. if (out_len < 0)
  70. return -EINVAL;
  71. *dlen = out_len;
  72. return 0;
  73. }
  74. static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  75. unsigned int slen, u8 *dst, unsigned int *dlen,
  76. void *ctx)
  77. {
  78. return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
  79. }
  80. static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
  81. unsigned int slen, u8 *dst,
  82. unsigned int *dlen)
  83. {
  84. return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
  85. }
  86. static struct crypto_alg alg_lz4hc = {
  87. .cra_name = "lz4hc",
  88. .cra_driver_name = "lz4hc-generic",
  89. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  90. .cra_ctxsize = sizeof(struct lz4hc_ctx),
  91. .cra_module = THIS_MODULE,
  92. .cra_init = lz4hc_init,
  93. .cra_exit = lz4hc_exit,
  94. .cra_u = { .compress = {
  95. .coa_compress = lz4hc_compress_crypto,
  96. .coa_decompress = lz4hc_decompress_crypto } }
  97. };
  98. static struct scomp_alg scomp = {
  99. .alloc_ctx = lz4hc_alloc_ctx,
  100. .free_ctx = lz4hc_free_ctx,
  101. .compress = lz4hc_scompress,
  102. .decompress = lz4hc_sdecompress,
  103. .base = {
  104. .cra_name = "lz4hc",
  105. .cra_driver_name = "lz4hc-scomp",
  106. .cra_module = THIS_MODULE,
  107. }
  108. };
  109. static int __init lz4hc_mod_init(void)
  110. {
  111. int ret;
  112. ret = crypto_register_alg(&alg_lz4hc);
  113. if (ret)
  114. return ret;
  115. ret = crypto_register_scomp(&scomp);
  116. if (ret) {
  117. crypto_unregister_alg(&alg_lz4hc);
  118. return ret;
  119. }
  120. return ret;
  121. }
  122. static void __exit lz4hc_mod_fini(void)
  123. {
  124. crypto_unregister_alg(&alg_lz4hc);
  125. crypto_unregister_scomp(&scomp);
  126. }
  127. subsys_initcall(lz4hc_mod_init);
  128. module_exit(lz4hc_mod_fini);
  129. MODULE_LICENSE("GPL");
  130. MODULE_DESCRIPTION("LZ4HC Compression Algorithm");
  131. MODULE_ALIAS_CRYPTO("lz4hc");