acompress.c 4.8 KB

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