acompress.c 5.0 KB

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