lz4hc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Copyright (c) 2013 Chanho Min <chanho.min@lge.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/crypto.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/lz4.h>
  25. #include <crypto/internal/scompress.h>
  26. struct lz4hc_ctx {
  27. void *lz4hc_comp_mem;
  28. };
  29. static void *lz4hc_alloc_ctx(struct crypto_scomp *tfm)
  30. {
  31. void *ctx;
  32. ctx = vmalloc(LZ4HC_MEM_COMPRESS);
  33. if (!ctx)
  34. return ERR_PTR(-ENOMEM);
  35. return ctx;
  36. }
  37. static int lz4hc_init(struct crypto_tfm *tfm)
  38. {
  39. struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
  40. ctx->lz4hc_comp_mem = lz4hc_alloc_ctx(NULL);
  41. if (IS_ERR(ctx->lz4hc_comp_mem))
  42. return -ENOMEM;
  43. return 0;
  44. }
  45. static void lz4hc_free_ctx(struct crypto_scomp *tfm, void *ctx)
  46. {
  47. vfree(ctx);
  48. }
  49. static void lz4hc_exit(struct crypto_tfm *tfm)
  50. {
  51. struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
  52. lz4hc_free_ctx(NULL, ctx->lz4hc_comp_mem);
  53. }
  54. static int __lz4hc_compress_crypto(const u8 *src, unsigned int slen,
  55. u8 *dst, unsigned int *dlen, void *ctx)
  56. {
  57. int out_len = LZ4_compress_HC(src, dst, slen,
  58. *dlen, LZ4HC_DEFAULT_CLEVEL, ctx);
  59. if (!out_len)
  60. return -EINVAL;
  61. *dlen = out_len;
  62. return 0;
  63. }
  64. static int lz4hc_scompress(struct crypto_scomp *tfm, const u8 *src,
  65. unsigned int slen, u8 *dst, unsigned int *dlen,
  66. void *ctx)
  67. {
  68. return __lz4hc_compress_crypto(src, slen, dst, dlen, ctx);
  69. }
  70. static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
  71. unsigned int slen, u8 *dst,
  72. unsigned int *dlen)
  73. {
  74. struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
  75. return __lz4hc_compress_crypto(src, slen, dst, dlen,
  76. ctx->lz4hc_comp_mem);
  77. }
  78. static int __lz4hc_decompress_crypto(const u8 *src, unsigned int slen,
  79. u8 *dst, unsigned int *dlen, void *ctx)
  80. {
  81. int out_len = LZ4_decompress_safe(src, dst, slen, *dlen);
  82. if (out_len < 0)
  83. return -EINVAL;
  84. *dlen = out_len;
  85. return 0;
  86. }
  87. static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  88. unsigned int slen, u8 *dst, unsigned int *dlen,
  89. void *ctx)
  90. {
  91. return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
  92. }
  93. static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
  94. unsigned int slen, u8 *dst,
  95. unsigned int *dlen)
  96. {
  97. return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
  98. }
  99. static struct crypto_alg alg_lz4hc = {
  100. .cra_name = "lz4hc",
  101. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  102. .cra_ctxsize = sizeof(struct lz4hc_ctx),
  103. .cra_module = THIS_MODULE,
  104. .cra_list = LIST_HEAD_INIT(alg_lz4hc.cra_list),
  105. .cra_init = lz4hc_init,
  106. .cra_exit = lz4hc_exit,
  107. .cra_u = { .compress = {
  108. .coa_compress = lz4hc_compress_crypto,
  109. .coa_decompress = lz4hc_decompress_crypto } }
  110. };
  111. static struct scomp_alg scomp = {
  112. .alloc_ctx = lz4hc_alloc_ctx,
  113. .free_ctx = lz4hc_free_ctx,
  114. .compress = lz4hc_scompress,
  115. .decompress = lz4hc_sdecompress,
  116. .base = {
  117. .cra_name = "lz4hc",
  118. .cra_driver_name = "lz4hc-scomp",
  119. .cra_module = THIS_MODULE,
  120. }
  121. };
  122. static int __init lz4hc_mod_init(void)
  123. {
  124. int ret;
  125. ret = crypto_register_alg(&alg_lz4hc);
  126. if (ret)
  127. return ret;
  128. ret = crypto_register_scomp(&scomp);
  129. if (ret) {
  130. crypto_unregister_alg(&alg_lz4hc);
  131. return ret;
  132. }
  133. return ret;
  134. }
  135. static void __exit lz4hc_mod_fini(void)
  136. {
  137. crypto_unregister_alg(&alg_lz4hc);
  138. crypto_unregister_scomp(&scomp);
  139. }
  140. module_init(lz4hc_mod_init);
  141. module_exit(lz4hc_mod_fini);
  142. MODULE_LICENSE("GPL");
  143. MODULE_DESCRIPTION("LZ4HC Compression Algorithm");
  144. MODULE_ALIAS_CRYPTO("lz4hc");