lz4hc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 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. size_t tmp_len = *dlen;
  58. int err;
  59. err = lz4hc_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 lz4hc_scompress(struct crypto_scomp *tfm, const u8 *src,
  66. unsigned int slen, u8 *dst, unsigned int *dlen,
  67. void *ctx)
  68. {
  69. return __lz4hc_compress_crypto(src, slen, dst, dlen, ctx);
  70. }
  71. static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
  72. unsigned int slen, u8 *dst,
  73. unsigned int *dlen)
  74. {
  75. struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
  76. return __lz4hc_compress_crypto(src, slen, dst, dlen,
  77. ctx->lz4hc_comp_mem);
  78. }
  79. static int __lz4hc_decompress_crypto(const u8 *src, unsigned int slen,
  80. u8 *dst, unsigned int *dlen, void *ctx)
  81. {
  82. int err;
  83. size_t tmp_len = *dlen;
  84. size_t __slen = slen;
  85. err = lz4_decompress_unknownoutputsize(src, __slen, dst, &tmp_len);
  86. if (err < 0)
  87. return -EINVAL;
  88. *dlen = tmp_len;
  89. return err;
  90. }
  91. static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  92. unsigned int slen, u8 *dst, unsigned int *dlen,
  93. void *ctx)
  94. {
  95. return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
  96. }
  97. static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
  98. unsigned int slen, u8 *dst,
  99. unsigned int *dlen)
  100. {
  101. return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
  102. }
  103. static struct crypto_alg alg_lz4hc = {
  104. .cra_name = "lz4hc",
  105. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  106. .cra_ctxsize = sizeof(struct lz4hc_ctx),
  107. .cra_module = THIS_MODULE,
  108. .cra_list = LIST_HEAD_INIT(alg_lz4hc.cra_list),
  109. .cra_init = lz4hc_init,
  110. .cra_exit = lz4hc_exit,
  111. .cra_u = { .compress = {
  112. .coa_compress = lz4hc_compress_crypto,
  113. .coa_decompress = lz4hc_decompress_crypto } }
  114. };
  115. static struct scomp_alg scomp = {
  116. .alloc_ctx = lz4hc_alloc_ctx,
  117. .free_ctx = lz4hc_free_ctx,
  118. .compress = lz4hc_scompress,
  119. .decompress = lz4hc_sdecompress,
  120. .base = {
  121. .cra_name = "lz4hc",
  122. .cra_driver_name = "lz4hc-scomp",
  123. .cra_module = THIS_MODULE,
  124. }
  125. };
  126. static int __init lz4hc_mod_init(void)
  127. {
  128. int ret;
  129. ret = crypto_register_alg(&alg_lz4hc);
  130. if (ret)
  131. return ret;
  132. ret = crypto_register_scomp(&scomp);
  133. if (ret) {
  134. crypto_unregister_alg(&alg_lz4hc);
  135. return ret;
  136. }
  137. return ret;
  138. }
  139. static void __exit lz4hc_mod_fini(void)
  140. {
  141. crypto_unregister_alg(&alg_lz4hc);
  142. crypto_unregister_scomp(&scomp);
  143. }
  144. module_init(lz4hc_mod_init);
  145. module_exit(lz4hc_mod_fini);
  146. MODULE_LICENSE("GPL");
  147. MODULE_DESCRIPTION("LZ4HC Compression Algorithm");
  148. MODULE_ALIAS_CRYPTO("lz4hc");