lz4.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Copyright (c) 2013 Chanho Min <[email protected]>
  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 lz4_ctx {
  27. void *lz4_comp_mem;
  28. };
  29. static void *lz4_alloc_ctx(struct crypto_scomp *tfm)
  30. {
  31. void *ctx;
  32. ctx = vmalloc(LZ4_MEM_COMPRESS);
  33. if (!ctx)
  34. return ERR_PTR(-ENOMEM);
  35. return ctx;
  36. }
  37. static int lz4_init(struct crypto_tfm *tfm)
  38. {
  39. struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
  40. ctx->lz4_comp_mem = lz4_alloc_ctx(NULL);
  41. if (IS_ERR(ctx->lz4_comp_mem))
  42. return -ENOMEM;
  43. return 0;
  44. }
  45. static void lz4_free_ctx(struct crypto_scomp *tfm, void *ctx)
  46. {
  47. vfree(ctx);
  48. }
  49. static void lz4_exit(struct crypto_tfm *tfm)
  50. {
  51. struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
  52. lz4_free_ctx(NULL, ctx->lz4_comp_mem);
  53. }
  54. static int __lz4_compress_crypto(const u8 *src, unsigned int slen,
  55. u8 *dst, unsigned int *dlen, void *ctx)
  56. {
  57. size_t tmp_len = *dlen;
  58. int err;
  59. err = lz4_compress(src, slen, dst, &tmp_len, ctx);
  60. if (err < 0)
  61. return -EINVAL;
  62. *dlen = tmp_len;
  63. return 0;
  64. }
  65. static int lz4_scompress(struct crypto_scomp *tfm, const u8 *src,
  66. unsigned int slen, u8 *dst, unsigned int *dlen,
  67. void *ctx)
  68. {
  69. return __lz4_compress_crypto(src, slen, dst, dlen, ctx);
  70. }
  71. static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
  72. unsigned int slen, u8 *dst, unsigned int *dlen)
  73. {
  74. struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
  75. return __lz4_compress_crypto(src, slen, dst, dlen, ctx->lz4_comp_mem);
  76. }
  77. static int __lz4_decompress_crypto(const u8 *src, unsigned int slen,
  78. u8 *dst, unsigned int *dlen, void *ctx)
  79. {
  80. int err;
  81. size_t tmp_len = *dlen;
  82. size_t __slen = slen;
  83. err = lz4_decompress_unknownoutputsize(src, __slen, dst, &tmp_len);
  84. if (err < 0)
  85. return -EINVAL;
  86. *dlen = tmp_len;
  87. return err;
  88. }
  89. static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  90. unsigned int slen, u8 *dst, unsigned int *dlen,
  91. void *ctx)
  92. {
  93. return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
  94. }
  95. static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
  96. unsigned int slen, u8 *dst,
  97. unsigned int *dlen)
  98. {
  99. return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
  100. }
  101. static struct crypto_alg alg_lz4 = {
  102. .cra_name = "lz4",
  103. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  104. .cra_ctxsize = sizeof(struct lz4_ctx),
  105. .cra_module = THIS_MODULE,
  106. .cra_list = LIST_HEAD_INIT(alg_lz4.cra_list),
  107. .cra_init = lz4_init,
  108. .cra_exit = lz4_exit,
  109. .cra_u = { .compress = {
  110. .coa_compress = lz4_compress_crypto,
  111. .coa_decompress = lz4_decompress_crypto } }
  112. };
  113. static struct scomp_alg scomp = {
  114. .alloc_ctx = lz4_alloc_ctx,
  115. .free_ctx = lz4_free_ctx,
  116. .compress = lz4_scompress,
  117. .decompress = lz4_sdecompress,
  118. .base = {
  119. .cra_name = "lz4",
  120. .cra_driver_name = "lz4-scomp",
  121. .cra_module = THIS_MODULE,
  122. }
  123. };
  124. static int __init lz4_mod_init(void)
  125. {
  126. int ret;
  127. ret = crypto_register_alg(&alg_lz4);
  128. if (ret)
  129. return ret;
  130. ret = crypto_register_scomp(&scomp);
  131. if (ret) {
  132. crypto_unregister_alg(&alg_lz4);
  133. return ret;
  134. }
  135. return ret;
  136. }
  137. static void __exit lz4_mod_fini(void)
  138. {
  139. crypto_unregister_alg(&alg_lz4);
  140. crypto_unregister_scomp(&scomp);
  141. }
  142. module_init(lz4_mod_init);
  143. module_exit(lz4_mod_fini);
  144. MODULE_LICENSE("GPL");
  145. MODULE_DESCRIPTION("LZ4 Compression Algorithm");
  146. MODULE_ALIAS_CRYPTO("lz4");