lz4hc.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <crypto/internal/scompress.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/lz4.h>
  12. static void *lz4hc_alloc_ctx(void)
  13. {
  14. void *ctx;
  15. ctx = vmalloc(LZ4HC_MEM_COMPRESS);
  16. if (!ctx)
  17. return ERR_PTR(-ENOMEM);
  18. return ctx;
  19. }
  20. static void lz4hc_free_ctx(void *ctx)
  21. {
  22. vfree(ctx);
  23. }
  24. static int __lz4hc_compress_crypto(const u8 *src, unsigned int slen,
  25. u8 *dst, unsigned int *dlen, void *ctx)
  26. {
  27. int out_len = LZ4_compress_HC(src, dst, slen,
  28. *dlen, LZ4HC_DEFAULT_CLEVEL, ctx);
  29. if (!out_len)
  30. return -EINVAL;
  31. *dlen = out_len;
  32. return 0;
  33. }
  34. static int lz4hc_scompress(struct crypto_scomp *tfm, const u8 *src,
  35. unsigned int slen, u8 *dst, unsigned int *dlen,
  36. void *ctx)
  37. {
  38. return __lz4hc_compress_crypto(src, slen, dst, dlen, ctx);
  39. }
  40. static int __lz4hc_decompress_crypto(const u8 *src, unsigned int slen,
  41. u8 *dst, unsigned int *dlen, void *ctx)
  42. {
  43. int out_len = LZ4_decompress_safe(src, dst, slen, *dlen);
  44. if (out_len < 0)
  45. return -EINVAL;
  46. *dlen = out_len;
  47. return 0;
  48. }
  49. static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  50. unsigned int slen, u8 *dst, unsigned int *dlen,
  51. void *ctx)
  52. {
  53. return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
  54. }
  55. static struct scomp_alg scomp = {
  56. .streams = {
  57. .alloc_ctx = lz4hc_alloc_ctx,
  58. .free_ctx = lz4hc_free_ctx,
  59. },
  60. .compress = lz4hc_scompress,
  61. .decompress = lz4hc_sdecompress,
  62. .base = {
  63. .cra_name = "lz4hc",
  64. .cra_driver_name = "lz4hc-scomp",
  65. .cra_module = THIS_MODULE,
  66. }
  67. };
  68. static int __init lz4hc_mod_init(void)
  69. {
  70. return crypto_register_scomp(&scomp);
  71. }
  72. static void __exit lz4hc_mod_fini(void)
  73. {
  74. crypto_unregister_scomp(&scomp);
  75. }
  76. module_init(lz4hc_mod_init);
  77. module_exit(lz4hc_mod_fini);
  78. MODULE_LICENSE("GPL");
  79. MODULE_DESCRIPTION("LZ4HC Compression Algorithm");
  80. MODULE_ALIAS_CRYPTO("lz4hc");