lz4.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. size_t tmp_len = *dlen;
  46. int err;
  47. err = lz4_compress(src, slen, dst, &tmp_len, ctx->lz4_comp_mem);
  48. if (err < 0)
  49. return -EINVAL;
  50. *dlen = tmp_len;
  51. return 0;
  52. }
  53. static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
  54. unsigned int slen, u8 *dst, unsigned int *dlen)
  55. {
  56. int err;
  57. size_t tmp_len = *dlen;
  58. size_t __slen = slen;
  59. err = lz4_decompress_unknownoutputsize(src, __slen, dst, &tmp_len);
  60. if (err < 0)
  61. return -EINVAL;
  62. *dlen = tmp_len;
  63. return err;
  64. }
  65. static struct crypto_alg alg_lz4 = {
  66. .cra_name = "lz4",
  67. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  68. .cra_ctxsize = sizeof(struct lz4_ctx),
  69. .cra_module = THIS_MODULE,
  70. .cra_list = LIST_HEAD_INIT(alg_lz4.cra_list),
  71. .cra_init = lz4_init,
  72. .cra_exit = lz4_exit,
  73. .cra_u = { .compress = {
  74. .coa_compress = lz4_compress_crypto,
  75. .coa_decompress = lz4_decompress_crypto } }
  76. };
  77. static int __init lz4_mod_init(void)
  78. {
  79. return crypto_register_alg(&alg_lz4);
  80. }
  81. static void __exit lz4_mod_fini(void)
  82. {
  83. crypto_unregister_alg(&alg_lz4);
  84. }
  85. module_init(lz4_mod_init);
  86. module_exit(lz4_mod_fini);
  87. MODULE_LICENSE("GPL");
  88. MODULE_DESCRIPTION("LZ4 Compression Algorithm");
  89. MODULE_ALIAS_CRYPTO("lz4");