lzo.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL | __GFP_NOWARN);
  32. if (!ctx)
  33. ctx = vmalloc(LZO1X_MEM_COMPRESS);
  34. if (!ctx)
  35. return ERR_PTR(-ENOMEM);
  36. return ctx;
  37. }
  38. static int lzo_init(struct crypto_tfm *tfm)
  39. {
  40. struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
  41. ctx->lzo_comp_mem = lzo_alloc_ctx(NULL);
  42. if (IS_ERR(ctx->lzo_comp_mem))
  43. return -ENOMEM;
  44. return 0;
  45. }
  46. static void lzo_free_ctx(struct crypto_scomp *tfm, void *ctx)
  47. {
  48. kvfree(ctx);
  49. }
  50. static void lzo_exit(struct crypto_tfm *tfm)
  51. {
  52. struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
  53. lzo_free_ctx(NULL, ctx->lzo_comp_mem);
  54. }
  55. static int __lzo_compress(const u8 *src, unsigned int slen,
  56. u8 *dst, unsigned int *dlen, void *ctx)
  57. {
  58. size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
  59. int err;
  60. err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx);
  61. if (err != LZO_E_OK)
  62. return -EINVAL;
  63. *dlen = tmp_len;
  64. return 0;
  65. }
  66. static int lzo_compress(struct crypto_tfm *tfm, const u8 *src,
  67. unsigned int slen, u8 *dst, unsigned int *dlen)
  68. {
  69. struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
  70. return __lzo_compress(src, slen, dst, dlen, ctx->lzo_comp_mem);
  71. }
  72. static int lzo_scompress(struct crypto_scomp *tfm, const u8 *src,
  73. unsigned int slen, u8 *dst, unsigned int *dlen,
  74. void *ctx)
  75. {
  76. return __lzo_compress(src, slen, dst, dlen, ctx);
  77. }
  78. static int __lzo_decompress(const u8 *src, unsigned int slen,
  79. u8 *dst, unsigned int *dlen)
  80. {
  81. int err;
  82. size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
  83. err = lzo1x_decompress_safe(src, slen, dst, &tmp_len);
  84. if (err != LZO_E_OK)
  85. return -EINVAL;
  86. *dlen = tmp_len;
  87. return 0;
  88. }
  89. static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src,
  90. unsigned int slen, u8 *dst, unsigned int *dlen)
  91. {
  92. return __lzo_decompress(src, slen, dst, dlen);
  93. }
  94. static int lzo_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  95. unsigned int slen, u8 *dst, unsigned int *dlen,
  96. void *ctx)
  97. {
  98. return __lzo_decompress(src, slen, dst, dlen);
  99. }
  100. static struct crypto_alg alg = {
  101. .cra_name = "lzo",
  102. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  103. .cra_ctxsize = sizeof(struct lzo_ctx),
  104. .cra_module = THIS_MODULE,
  105. .cra_init = lzo_init,
  106. .cra_exit = lzo_exit,
  107. .cra_u = { .compress = {
  108. .coa_compress = lzo_compress,
  109. .coa_decompress = lzo_decompress } }
  110. };
  111. static struct scomp_alg scomp = {
  112. .alloc_ctx = lzo_alloc_ctx,
  113. .free_ctx = lzo_free_ctx,
  114. .compress = lzo_scompress,
  115. .decompress = lzo_sdecompress,
  116. .base = {
  117. .cra_name = "lzo",
  118. .cra_driver_name = "lzo-scomp",
  119. .cra_module = THIS_MODULE,
  120. }
  121. };
  122. static int __init lzo_mod_init(void)
  123. {
  124. int ret;
  125. ret = crypto_register_alg(&alg);
  126. if (ret)
  127. return ret;
  128. ret = crypto_register_scomp(&scomp);
  129. if (ret) {
  130. crypto_unregister_alg(&alg);
  131. return ret;
  132. }
  133. return ret;
  134. }
  135. static void __exit lzo_mod_fini(void)
  136. {
  137. crypto_unregister_alg(&alg);
  138. crypto_unregister_scomp(&scomp);
  139. }
  140. module_init(lzo_mod_init);
  141. module_exit(lzo_mod_fini);
  142. MODULE_LICENSE("GPL");
  143. MODULE_DESCRIPTION("LZO Compression Algorithm");
  144. MODULE_ALIAS_CRYPTO("lzo");