lzo-rle.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Cryptographic API.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/crypto.h>
  8. #include <linux/vmalloc.h>
  9. #include <linux/mm.h>
  10. #include <linux/lzo.h>
  11. #include <crypto/internal/scompress.h>
  12. struct lzorle_ctx {
  13. void *lzorle_comp_mem;
  14. };
  15. static void *lzorle_alloc_ctx(struct crypto_scomp *tfm)
  16. {
  17. void *ctx;
  18. ctx = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
  19. if (!ctx)
  20. return ERR_PTR(-ENOMEM);
  21. return ctx;
  22. }
  23. static int lzorle_init(struct crypto_tfm *tfm)
  24. {
  25. struct lzorle_ctx *ctx = crypto_tfm_ctx(tfm);
  26. ctx->lzorle_comp_mem = lzorle_alloc_ctx(NULL);
  27. if (IS_ERR(ctx->lzorle_comp_mem))
  28. return -ENOMEM;
  29. return 0;
  30. }
  31. static void lzorle_free_ctx(struct crypto_scomp *tfm, void *ctx)
  32. {
  33. kvfree(ctx);
  34. }
  35. static void lzorle_exit(struct crypto_tfm *tfm)
  36. {
  37. struct lzorle_ctx *ctx = crypto_tfm_ctx(tfm);
  38. lzorle_free_ctx(NULL, ctx->lzorle_comp_mem);
  39. }
  40. static int __lzorle_compress(const u8 *src, unsigned int slen,
  41. u8 *dst, unsigned int *dlen, void *ctx)
  42. {
  43. size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
  44. int err;
  45. err = lzorle1x_1_compress(src, slen, dst, &tmp_len, ctx);
  46. if (err != LZO_E_OK)
  47. return -EINVAL;
  48. *dlen = tmp_len;
  49. return 0;
  50. }
  51. static int lzorle_compress(struct crypto_tfm *tfm, const u8 *src,
  52. unsigned int slen, u8 *dst, unsigned int *dlen)
  53. {
  54. struct lzorle_ctx *ctx = crypto_tfm_ctx(tfm);
  55. return __lzorle_compress(src, slen, dst, dlen, ctx->lzorle_comp_mem);
  56. }
  57. static int lzorle_scompress(struct crypto_scomp *tfm, const u8 *src,
  58. unsigned int slen, u8 *dst, unsigned int *dlen,
  59. void *ctx)
  60. {
  61. return __lzorle_compress(src, slen, dst, dlen, ctx);
  62. }
  63. static int __lzorle_decompress(const u8 *src, unsigned int slen,
  64. u8 *dst, unsigned int *dlen)
  65. {
  66. int err;
  67. size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
  68. err = lzo1x_decompress_safe(src, slen, dst, &tmp_len);
  69. if (err != LZO_E_OK)
  70. return -EINVAL;
  71. *dlen = tmp_len;
  72. return 0;
  73. }
  74. static int lzorle_decompress(struct crypto_tfm *tfm, const u8 *src,
  75. unsigned int slen, u8 *dst, unsigned int *dlen)
  76. {
  77. return __lzorle_decompress(src, slen, dst, dlen);
  78. }
  79. static int lzorle_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  80. unsigned int slen, u8 *dst, unsigned int *dlen,
  81. void *ctx)
  82. {
  83. return __lzorle_decompress(src, slen, dst, dlen);
  84. }
  85. static struct crypto_alg alg = {
  86. .cra_name = "lzo-rle",
  87. .cra_driver_name = "lzo-rle-generic",
  88. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  89. .cra_ctxsize = sizeof(struct lzorle_ctx),
  90. .cra_module = THIS_MODULE,
  91. .cra_init = lzorle_init,
  92. .cra_exit = lzorle_exit,
  93. .cra_u = { .compress = {
  94. .coa_compress = lzorle_compress,
  95. .coa_decompress = lzorle_decompress } }
  96. };
  97. static struct scomp_alg scomp = {
  98. .alloc_ctx = lzorle_alloc_ctx,
  99. .free_ctx = lzorle_free_ctx,
  100. .compress = lzorle_scompress,
  101. .decompress = lzorle_sdecompress,
  102. .base = {
  103. .cra_name = "lzo-rle",
  104. .cra_driver_name = "lzo-rle-scomp",
  105. .cra_module = THIS_MODULE,
  106. }
  107. };
  108. static int __init lzorle_mod_init(void)
  109. {
  110. int ret;
  111. ret = crypto_register_alg(&alg);
  112. if (ret)
  113. return ret;
  114. ret = crypto_register_scomp(&scomp);
  115. if (ret) {
  116. crypto_unregister_alg(&alg);
  117. return ret;
  118. }
  119. return ret;
  120. }
  121. static void __exit lzorle_mod_fini(void)
  122. {
  123. crypto_unregister_alg(&alg);
  124. crypto_unregister_scomp(&scomp);
  125. }
  126. subsys_initcall(lzorle_mod_init);
  127. module_exit(lzorle_mod_fini);
  128. MODULE_LICENSE("GPL");
  129. MODULE_DESCRIPTION("LZO-RLE Compression Algorithm");
  130. MODULE_ALIAS_CRYPTO("lzo-rle");