842.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API for the 842 software compression algorithm.
  4. *
  5. * Copyright (C) IBM Corporation, 2011-2015
  6. *
  7. * Original Authors: Robert Jennings <rcj@linux.vnet.ibm.com>
  8. * Seth Jennings <sjenning@linux.vnet.ibm.com>
  9. *
  10. * Rewrite: Dan Streetman <ddstreet@ieee.org>
  11. *
  12. * This is the software implementation of compression and decompression using
  13. * the 842 format. This uses the software 842 library at lib/842/ which is
  14. * only a reference implementation, and is very, very slow as compared to other
  15. * software compressors. You probably do not want to use this software
  16. * compression. If you have access to the PowerPC 842 compression hardware, you
  17. * want to use the 842 hardware compression interface, which is at:
  18. * drivers/crypto/nx/nx-842-crypto.c
  19. */
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/crypto.h>
  23. #include <linux/sw842.h>
  24. #include <crypto/internal/scompress.h>
  25. struct crypto842_ctx {
  26. void *wmem; /* working memory for compress */
  27. };
  28. static void *crypto842_alloc_ctx(struct crypto_scomp *tfm)
  29. {
  30. void *ctx;
  31. ctx = kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL);
  32. if (!ctx)
  33. return ERR_PTR(-ENOMEM);
  34. return ctx;
  35. }
  36. static int crypto842_init(struct crypto_tfm *tfm)
  37. {
  38. struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
  39. ctx->wmem = crypto842_alloc_ctx(NULL);
  40. if (IS_ERR(ctx->wmem))
  41. return -ENOMEM;
  42. return 0;
  43. }
  44. static void crypto842_free_ctx(struct crypto_scomp *tfm, void *ctx)
  45. {
  46. kfree(ctx);
  47. }
  48. static void crypto842_exit(struct crypto_tfm *tfm)
  49. {
  50. struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
  51. crypto842_free_ctx(NULL, ctx->wmem);
  52. }
  53. static int crypto842_compress(struct crypto_tfm *tfm,
  54. const u8 *src, unsigned int slen,
  55. u8 *dst, unsigned int *dlen)
  56. {
  57. struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
  58. return sw842_compress(src, slen, dst, dlen, ctx->wmem);
  59. }
  60. static int crypto842_scompress(struct crypto_scomp *tfm,
  61. const u8 *src, unsigned int slen,
  62. u8 *dst, unsigned int *dlen, void *ctx)
  63. {
  64. return sw842_compress(src, slen, dst, dlen, ctx);
  65. }
  66. static int crypto842_decompress(struct crypto_tfm *tfm,
  67. const u8 *src, unsigned int slen,
  68. u8 *dst, unsigned int *dlen)
  69. {
  70. return sw842_decompress(src, slen, dst, dlen);
  71. }
  72. static int crypto842_sdecompress(struct crypto_scomp *tfm,
  73. const u8 *src, unsigned int slen,
  74. u8 *dst, unsigned int *dlen, void *ctx)
  75. {
  76. return sw842_decompress(src, slen, dst, dlen);
  77. }
  78. static struct crypto_alg alg = {
  79. .cra_name = "842",
  80. .cra_driver_name = "842-generic",
  81. .cra_priority = 100,
  82. .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  83. .cra_ctxsize = sizeof(struct crypto842_ctx),
  84. .cra_module = THIS_MODULE,
  85. .cra_init = crypto842_init,
  86. .cra_exit = crypto842_exit,
  87. .cra_u = { .compress = {
  88. .coa_compress = crypto842_compress,
  89. .coa_decompress = crypto842_decompress } }
  90. };
  91. static struct scomp_alg scomp = {
  92. .alloc_ctx = crypto842_alloc_ctx,
  93. .free_ctx = crypto842_free_ctx,
  94. .compress = crypto842_scompress,
  95. .decompress = crypto842_sdecompress,
  96. .base = {
  97. .cra_name = "842",
  98. .cra_driver_name = "842-scomp",
  99. .cra_priority = 100,
  100. .cra_module = THIS_MODULE,
  101. }
  102. };
  103. static int __init crypto842_mod_init(void)
  104. {
  105. int ret;
  106. ret = crypto_register_alg(&alg);
  107. if (ret)
  108. return ret;
  109. ret = crypto_register_scomp(&scomp);
  110. if (ret) {
  111. crypto_unregister_alg(&alg);
  112. return ret;
  113. }
  114. return ret;
  115. }
  116. subsys_initcall(crypto842_mod_init);
  117. static void __exit crypto842_mod_exit(void)
  118. {
  119. crypto_unregister_alg(&alg);
  120. crypto_unregister_scomp(&scomp);
  121. }
  122. module_exit(crypto842_mod_exit);
  123. MODULE_LICENSE("GPL");
  124. MODULE_DESCRIPTION("842 Software Compression Algorithm");
  125. MODULE_ALIAS_CRYPTO("842");
  126. MODULE_ALIAS_CRYPTO("842-generic");
  127. MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");