lzo.c 3.9 KB

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