842.c 4.2 KB

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