lz4.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 lz4_ctx {
  14. void *lz4_comp_mem;
  15. };
  16. static void *lz4_alloc_ctx(struct crypto_scomp *tfm)
  17. {
  18. void *ctx;
  19. ctx = vmalloc(LZ4_MEM_COMPRESS);
  20. if (!ctx)
  21. return ERR_PTR(-ENOMEM);
  22. return ctx;
  23. }
  24. static int lz4_init(struct crypto_tfm *tfm)
  25. {
  26. struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
  27. ctx->lz4_comp_mem = lz4_alloc_ctx(NULL);
  28. if (IS_ERR(ctx->lz4_comp_mem))
  29. return -ENOMEM;
  30. return 0;
  31. }
  32. static void lz4_free_ctx(struct crypto_scomp *tfm, void *ctx)
  33. {
  34. vfree(ctx);
  35. }
  36. static void lz4_exit(struct crypto_tfm *tfm)
  37. {
  38. struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
  39. lz4_free_ctx(NULL, ctx->lz4_comp_mem);
  40. }
  41. static int __lz4_compress_crypto(const u8 *src, unsigned int slen,
  42. u8 *dst, unsigned int *dlen, void *ctx)
  43. {
  44. int out_len = LZ4_compress_default(src, dst,
  45. slen, *dlen, ctx);
  46. if (!out_len)
  47. return -EINVAL;
  48. *dlen = out_len;
  49. return 0;
  50. }
  51. static int lz4_scompress(struct crypto_scomp *tfm, const u8 *src,
  52. unsigned int slen, u8 *dst, unsigned int *dlen,
  53. void *ctx)
  54. {
  55. return __lz4_compress_crypto(src, slen, dst, dlen, ctx);
  56. }
  57. static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
  58. unsigned int slen, u8 *dst, unsigned int *dlen)
  59. {
  60. struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
  61. return __lz4_compress_crypto(src, slen, dst, dlen, ctx->lz4_comp_mem);
  62. }
  63. static int __lz4_decompress_crypto(const u8 *src, unsigned int slen,
  64. u8 *dst, unsigned int *dlen, void *ctx)
  65. {
  66. int out_len = LZ4_decompress_safe(src, dst, slen, *dlen);
  67. if (out_len < 0)
  68. return -EINVAL;
  69. *dlen = out_len;
  70. return 0;
  71. }
  72. static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  73. unsigned int slen, u8 *dst, unsigned int *dlen,
  74. void *ctx)
  75. {
  76. return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
  77. }
  78. static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
  79. unsigned int slen, u8 *dst,
  80. unsigned int *dlen)
  81. {
  82. return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
  83. }
  84. static struct crypto_alg alg_lz4 = {
  85. .cra_name = "lz4",
  86. .cra_driver_name = "lz4-generic",
  87. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  88. .cra_ctxsize = sizeof(struct lz4_ctx),
  89. .cra_module = THIS_MODULE,
  90. .cra_init = lz4_init,
  91. .cra_exit = lz4_exit,
  92. .cra_u = { .compress = {
  93. .coa_compress = lz4_compress_crypto,
  94. .coa_decompress = lz4_decompress_crypto } }
  95. };
  96. static struct scomp_alg scomp = {
  97. .alloc_ctx = lz4_alloc_ctx,
  98. .free_ctx = lz4_free_ctx,
  99. .compress = lz4_scompress,
  100. .decompress = lz4_sdecompress,
  101. .base = {
  102. .cra_name = "lz4",
  103. .cra_driver_name = "lz4-scomp",
  104. .cra_module = THIS_MODULE,
  105. }
  106. };
  107. static int __init lz4_mod_init(void)
  108. {
  109. int ret;
  110. ret = crypto_register_alg(&alg_lz4);
  111. if (ret)
  112. return ret;
  113. ret = crypto_register_scomp(&scomp);
  114. if (ret) {
  115. crypto_unregister_alg(&alg_lz4);
  116. return ret;
  117. }
  118. return ret;
  119. }
  120. static void __exit lz4_mod_fini(void)
  121. {
  122. crypto_unregister_alg(&alg_lz4);
  123. crypto_unregister_scomp(&scomp);
  124. }
  125. subsys_initcall(lz4_mod_init);
  126. module_exit(lz4_mod_fini);
  127. MODULE_LICENSE("GPL");
  128. MODULE_DESCRIPTION("LZ4 Compression Algorithm");
  129. MODULE_ALIAS_CRYPTO("lz4");