acompress.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Asynchronous Compression operations
  4. *
  5. * Copyright (c) 2016, Intel Corporation
  6. * Authors: Weigang Li <weigang.li@intel.com>
  7. * Giovanni Cabiddu <giovanni.cabiddu@intel.com>
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/slab.h>
  14. #include <linux/string.h>
  15. #include <linux/crypto.h>
  16. #include <crypto/algapi.h>
  17. #include <linux/cryptouser.h>
  18. #include <linux/compiler.h>
  19. #include <net/netlink.h>
  20. #include <crypto/internal/acompress.h>
  21. #include <crypto/internal/scompress.h>
  22. #include "internal.h"
  23. static const struct crypto_type crypto_acomp_type;
  24. #ifdef CONFIG_NET
  25. static int crypto_acomp_report(struct sk_buff *skb, struct crypto_alg *alg)
  26. {
  27. struct crypto_report_acomp racomp;
  28. memset(&racomp, 0, sizeof(racomp));
  29. strscpy(racomp.type, "acomp", sizeof(racomp.type));
  30. return nla_put(skb, CRYPTOCFGA_REPORT_ACOMP, sizeof(racomp), &racomp);
  31. }
  32. #else
  33. static int crypto_acomp_report(struct sk_buff *skb, struct crypto_alg *alg)
  34. {
  35. return -ENOSYS;
  36. }
  37. #endif
  38. static void crypto_acomp_show(struct seq_file *m, struct crypto_alg *alg)
  39. __maybe_unused;
  40. static void crypto_acomp_show(struct seq_file *m, struct crypto_alg *alg)
  41. {
  42. seq_puts(m, "type : acomp\n");
  43. }
  44. static void crypto_acomp_exit_tfm(struct crypto_tfm *tfm)
  45. {
  46. struct crypto_acomp *acomp = __crypto_acomp_tfm(tfm);
  47. struct acomp_alg *alg = crypto_acomp_alg(acomp);
  48. alg->exit(acomp);
  49. }
  50. static int crypto_acomp_init_tfm(struct crypto_tfm *tfm)
  51. {
  52. struct crypto_acomp *acomp = __crypto_acomp_tfm(tfm);
  53. struct acomp_alg *alg = crypto_acomp_alg(acomp);
  54. if (tfm->__crt_alg->cra_type != &crypto_acomp_type)
  55. return crypto_init_scomp_ops_async(tfm);
  56. acomp->compress = alg->compress;
  57. acomp->decompress = alg->decompress;
  58. acomp->dst_free = alg->dst_free;
  59. acomp->reqsize = alg->reqsize;
  60. if (alg->exit)
  61. acomp->base.exit = crypto_acomp_exit_tfm;
  62. if (alg->init)
  63. return alg->init(acomp);
  64. return 0;
  65. }
  66. static unsigned int crypto_acomp_extsize(struct crypto_alg *alg)
  67. {
  68. int extsize = crypto_alg_extsize(alg);
  69. if (alg->cra_type != &crypto_acomp_type)
  70. extsize += sizeof(struct crypto_scomp *);
  71. return extsize;
  72. }
  73. static const struct crypto_type crypto_acomp_type = {
  74. .extsize = crypto_acomp_extsize,
  75. .init_tfm = crypto_acomp_init_tfm,
  76. #ifdef CONFIG_PROC_FS
  77. .show = crypto_acomp_show,
  78. #endif
  79. .report = crypto_acomp_report,
  80. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  81. .maskset = CRYPTO_ALG_TYPE_ACOMPRESS_MASK,
  82. .type = CRYPTO_ALG_TYPE_ACOMPRESS,
  83. .tfmsize = offsetof(struct crypto_acomp, base),
  84. };
  85. struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type,
  86. u32 mask)
  87. {
  88. return crypto_alloc_tfm(alg_name, &crypto_acomp_type, type, mask);
  89. }
  90. EXPORT_SYMBOL_GPL(crypto_alloc_acomp);
  91. struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type,
  92. u32 mask, int node)
  93. {
  94. return crypto_alloc_tfm_node(alg_name, &crypto_acomp_type, type, mask,
  95. node);
  96. }
  97. EXPORT_SYMBOL_GPL(crypto_alloc_acomp_node);
  98. struct acomp_req *acomp_request_alloc(struct crypto_acomp *acomp)
  99. {
  100. struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
  101. struct acomp_req *req;
  102. req = __acomp_request_alloc(acomp);
  103. if (req && (tfm->__crt_alg->cra_type != &crypto_acomp_type))
  104. return crypto_acomp_scomp_alloc_ctx(req);
  105. return req;
  106. }
  107. EXPORT_SYMBOL_GPL(acomp_request_alloc);
  108. void acomp_request_free(struct acomp_req *req)
  109. {
  110. struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
  111. struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
  112. if (tfm->__crt_alg->cra_type != &crypto_acomp_type)
  113. crypto_acomp_scomp_free_ctx(req);
  114. if (req->flags & CRYPTO_ACOMP_ALLOC_OUTPUT) {
  115. acomp->dst_free(req->dst);
  116. req->dst = NULL;
  117. }
  118. __acomp_request_free(req);
  119. }
  120. EXPORT_SYMBOL_GPL(acomp_request_free);
  121. int crypto_register_acomp(struct acomp_alg *alg)
  122. {
  123. struct crypto_alg *base = &alg->base;
  124. base->cra_type = &crypto_acomp_type;
  125. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  126. base->cra_flags |= CRYPTO_ALG_TYPE_ACOMPRESS;
  127. return crypto_register_alg(base);
  128. }
  129. EXPORT_SYMBOL_GPL(crypto_register_acomp);
  130. void crypto_unregister_acomp(struct acomp_alg *alg)
  131. {
  132. crypto_unregister_alg(&alg->base);
  133. }
  134. EXPORT_SYMBOL_GPL(crypto_unregister_acomp);
  135. int crypto_register_acomps(struct acomp_alg *algs, int count)
  136. {
  137. int i, ret;
  138. for (i = 0; i < count; i++) {
  139. ret = crypto_register_acomp(&algs[i]);
  140. if (ret)
  141. goto err;
  142. }
  143. return 0;
  144. err:
  145. for (--i; i >= 0; --i)
  146. crypto_unregister_acomp(&algs[i]);
  147. return ret;
  148. }
  149. EXPORT_SYMBOL_GPL(crypto_register_acomps);
  150. void crypto_unregister_acomps(struct acomp_alg *algs, int count)
  151. {
  152. int i;
  153. for (i = count - 1; i >= 0; --i)
  154. crypto_unregister_acomp(&algs[i]);
  155. }
  156. EXPORT_SYMBOL_GPL(crypto_unregister_acomps);
  157. MODULE_LICENSE("GPL");
  158. MODULE_DESCRIPTION("Asynchronous compression type");