acompress.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Asynchronous Compression operations
  3. *
  4. * Copyright (c) 2016, Intel Corporation
  5. * Authors: Weigang Li <[email protected]>
  6. * Giovanni Cabiddu <[email protected]>
  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 <net/netlink.h>
  24. #include <crypto/internal/acompress.h>
  25. #include <crypto/internal/scompress.h>
  26. #include "internal.h"
  27. static const struct crypto_type crypto_acomp_type;
  28. #ifdef CONFIG_NET
  29. static int crypto_acomp_report(struct sk_buff *skb, struct crypto_alg *alg)
  30. {
  31. struct crypto_report_acomp racomp;
  32. strncpy(racomp.type, "acomp", sizeof(racomp.type));
  33. if (nla_put(skb, CRYPTOCFGA_REPORT_ACOMP,
  34. sizeof(struct crypto_report_acomp), &racomp))
  35. goto nla_put_failure;
  36. return 0;
  37. nla_put_failure:
  38. return -EMSGSIZE;
  39. }
  40. #else
  41. static int crypto_acomp_report(struct sk_buff *skb, struct crypto_alg *alg)
  42. {
  43. return -ENOSYS;
  44. }
  45. #endif
  46. static void crypto_acomp_show(struct seq_file *m, struct crypto_alg *alg)
  47. __attribute__ ((unused));
  48. static void crypto_acomp_show(struct seq_file *m, struct crypto_alg *alg)
  49. {
  50. seq_puts(m, "type : acomp\n");
  51. }
  52. static void crypto_acomp_exit_tfm(struct crypto_tfm *tfm)
  53. {
  54. struct crypto_acomp *acomp = __crypto_acomp_tfm(tfm);
  55. struct acomp_alg *alg = crypto_acomp_alg(acomp);
  56. alg->exit(acomp);
  57. }
  58. static int crypto_acomp_init_tfm(struct crypto_tfm *tfm)
  59. {
  60. struct crypto_acomp *acomp = __crypto_acomp_tfm(tfm);
  61. struct acomp_alg *alg = crypto_acomp_alg(acomp);
  62. if (tfm->__crt_alg->cra_type != &crypto_acomp_type)
  63. return crypto_init_scomp_ops_async(tfm);
  64. acomp->compress = alg->compress;
  65. acomp->decompress = alg->decompress;
  66. acomp->dst_free = alg->dst_free;
  67. acomp->reqsize = alg->reqsize;
  68. if (alg->exit)
  69. acomp->base.exit = crypto_acomp_exit_tfm;
  70. if (alg->init)
  71. return alg->init(acomp);
  72. return 0;
  73. }
  74. static unsigned int crypto_acomp_extsize(struct crypto_alg *alg)
  75. {
  76. int extsize = crypto_alg_extsize(alg);
  77. if (alg->cra_type != &crypto_acomp_type)
  78. extsize += sizeof(struct crypto_scomp *);
  79. return extsize;
  80. }
  81. static const struct crypto_type crypto_acomp_type = {
  82. .extsize = crypto_acomp_extsize,
  83. .init_tfm = crypto_acomp_init_tfm,
  84. #ifdef CONFIG_PROC_FS
  85. .show = crypto_acomp_show,
  86. #endif
  87. .report = crypto_acomp_report,
  88. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  89. .maskset = CRYPTO_ALG_TYPE_ACOMPRESS_MASK,
  90. .type = CRYPTO_ALG_TYPE_ACOMPRESS,
  91. .tfmsize = offsetof(struct crypto_acomp, base),
  92. };
  93. struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type,
  94. u32 mask)
  95. {
  96. return crypto_alloc_tfm(alg_name, &crypto_acomp_type, type, mask);
  97. }
  98. EXPORT_SYMBOL_GPL(crypto_alloc_acomp);
  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. int crypto_register_acomp(struct acomp_alg *alg)
  123. {
  124. struct crypto_alg *base = &alg->base;
  125. base->cra_type = &crypto_acomp_type;
  126. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  127. base->cra_flags |= CRYPTO_ALG_TYPE_ACOMPRESS;
  128. return crypto_register_alg(base);
  129. }
  130. EXPORT_SYMBOL_GPL(crypto_register_acomp);
  131. int crypto_unregister_acomp(struct acomp_alg *alg)
  132. {
  133. return crypto_unregister_alg(&alg->base);
  134. }
  135. EXPORT_SYMBOL_GPL(crypto_unregister_acomp);
  136. MODULE_LICENSE("GPL");
  137. MODULE_DESCRIPTION("Asynchronous compression type");