lz4.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. struct lz4_ctx {
  26. void *lz4_comp_mem;
  27. };
  28. static int lz4_init(struct crypto_tfm *tfm)
  29. {
  30. struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
  31. ctx->lz4_comp_mem = vmalloc(LZ4_MEM_COMPRESS);
  32. if (!ctx->lz4_comp_mem)
  33. return -ENOMEM;
  34. return 0;
  35. }
  36. static void lz4_exit(struct crypto_tfm *tfm)
  37. {
  38. struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
  39. vfree(ctx->lz4_comp_mem);
  40. }
  41. static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
  42. unsigned int slen, u8 *dst, unsigned int *dlen)
  43. {
  44. struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
  45. int out_len = LZ4_compress_default(src, dst,
  46. slen, *dlen, ctx->lz4_comp_mem);
  47. if (!out_len)
  48. return -EINVAL;
  49. *dlen = out_len;
  50. return 0;
  51. }
  52. static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
  53. unsigned int slen, u8 *dst, unsigned int *dlen)
  54. {
  55. int out_len = LZ4_decompress_safe(src, dst, slen, *dlen);
  56. if (out_len < 0)
  57. return out_len;
  58. *dlen = out_len;
  59. return 0;
  60. }
  61. static struct crypto_alg alg_lz4 = {
  62. .cra_name = "lz4",
  63. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  64. .cra_ctxsize = sizeof(struct lz4_ctx),
  65. .cra_module = THIS_MODULE,
  66. .cra_list = LIST_HEAD_INIT(alg_lz4.cra_list),
  67. .cra_init = lz4_init,
  68. .cra_exit = lz4_exit,
  69. .cra_u = { .compress = {
  70. .coa_compress = lz4_compress_crypto,
  71. .coa_decompress = lz4_decompress_crypto } }
  72. };
  73. static int __init lz4_mod_init(void)
  74. {
  75. return crypto_register_alg(&alg_lz4);
  76. }
  77. static void __exit lz4_mod_fini(void)
  78. {
  79. crypto_unregister_alg(&alg_lz4);
  80. }
  81. module_init(lz4_mod_init);
  82. module_exit(lz4_mod_fini);
  83. MODULE_LICENSE("GPL");
  84. MODULE_DESCRIPTION("LZ4 Compression Algorithm");