acompress.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #ifndef _CRYPTO_ACOMP_H
  15. #define _CRYPTO_ACOMP_H
  16. #include <linux/crypto.h>
  17. #define CRYPTO_ACOMP_ALLOC_OUTPUT 0x00000001
  18. /**
  19. * struct acomp_req - asynchronous (de)compression request
  20. *
  21. * @base: Common attributes for asynchronous crypto requests
  22. * @src: Source Data
  23. * @dst: Destination data
  24. * @slen: Size of the input buffer
  25. * @dlen: Size of the output buffer and number of bytes produced
  26. * @flags: Internal flags
  27. * @__ctx: Start of private context data
  28. */
  29. struct acomp_req {
  30. struct crypto_async_request base;
  31. struct scatterlist *src;
  32. struct scatterlist *dst;
  33. unsigned int slen;
  34. unsigned int dlen;
  35. u32 flags;
  36. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  37. };
  38. /**
  39. * struct crypto_acomp - user-instantiated objects which encapsulate
  40. * algorithms and core processing logic
  41. *
  42. * @compress: Function performs a compress operation
  43. * @decompress: Function performs a de-compress operation
  44. * @dst_free: Frees destination buffer if allocated inside the
  45. * algorithm
  46. * @reqsize: Context size for (de)compression requests
  47. * @base: Common crypto API algorithm data structure
  48. */
  49. struct crypto_acomp {
  50. int (*compress)(struct acomp_req *req);
  51. int (*decompress)(struct acomp_req *req);
  52. void (*dst_free)(struct scatterlist *dst);
  53. unsigned int reqsize;
  54. struct crypto_tfm base;
  55. };
  56. /**
  57. * struct acomp_alg - asynchronous compression algorithm
  58. *
  59. * @compress: Function performs a compress operation
  60. * @decompress: Function performs a de-compress operation
  61. * @dst_free: Frees destination buffer if allocated inside the algorithm
  62. * @init: Initialize the cryptographic transformation object.
  63. * This function is used to initialize the cryptographic
  64. * transformation object. This function is called only once at
  65. * the instantiation time, right after the transformation context
  66. * was allocated. In case the cryptographic hardware has some
  67. * special requirements which need to be handled by software, this
  68. * function shall check for the precise requirement of the
  69. * transformation and put any software fallbacks in place.
  70. * @exit: Deinitialize the cryptographic transformation object. This is a
  71. * counterpart to @init, used to remove various changes set in
  72. * @init.
  73. *
  74. * @reqsize: Context size for (de)compression requests
  75. * @base: Common crypto API algorithm data structure
  76. */
  77. struct acomp_alg {
  78. int (*compress)(struct acomp_req *req);
  79. int (*decompress)(struct acomp_req *req);
  80. void (*dst_free)(struct scatterlist *dst);
  81. int (*init)(struct crypto_acomp *tfm);
  82. void (*exit)(struct crypto_acomp *tfm);
  83. unsigned int reqsize;
  84. struct crypto_alg base;
  85. };
  86. /**
  87. * DOC: Asynchronous Compression API
  88. *
  89. * The Asynchronous Compression API is used with the algorithms of type
  90. * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto)
  91. */
  92. /**
  93. * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle
  94. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  95. * compression algorithm e.g. "deflate"
  96. * @type: specifies the type of the algorithm
  97. * @mask: specifies the mask for the algorithm
  98. *
  99. * Allocate a handle for a compression algorithm. The returned struct
  100. * crypto_acomp is the handle that is required for any subsequent
  101. * API invocation for the compression operations.
  102. *
  103. * Return: allocated handle in case of success; IS_ERR() is true in case
  104. * of an error, PTR_ERR() returns the error code.
  105. */
  106. struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type,
  107. u32 mask);
  108. static inline struct crypto_tfm *crypto_acomp_tfm(struct crypto_acomp *tfm)
  109. {
  110. return &tfm->base;
  111. }
  112. static inline struct acomp_alg *__crypto_acomp_alg(struct crypto_alg *alg)
  113. {
  114. return container_of(alg, struct acomp_alg, base);
  115. }
  116. static inline struct crypto_acomp *__crypto_acomp_tfm(struct crypto_tfm *tfm)
  117. {
  118. return container_of(tfm, struct crypto_acomp, base);
  119. }
  120. static inline struct acomp_alg *crypto_acomp_alg(struct crypto_acomp *tfm)
  121. {
  122. return __crypto_acomp_alg(crypto_acomp_tfm(tfm)->__crt_alg);
  123. }
  124. static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp *tfm)
  125. {
  126. return tfm->reqsize;
  127. }
  128. static inline void acomp_request_set_tfm(struct acomp_req *req,
  129. struct crypto_acomp *tfm)
  130. {
  131. req->base.tfm = crypto_acomp_tfm(tfm);
  132. }
  133. static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req)
  134. {
  135. return __crypto_acomp_tfm(req->base.tfm);
  136. }
  137. /**
  138. * crypto_free_acomp() -- free ACOMPRESS tfm handle
  139. *
  140. * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
  141. */
  142. static inline void crypto_free_acomp(struct crypto_acomp *tfm)
  143. {
  144. crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm));
  145. }
  146. static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask)
  147. {
  148. type &= ~CRYPTO_ALG_TYPE_MASK;
  149. type |= CRYPTO_ALG_TYPE_ACOMPRESS;
  150. mask |= CRYPTO_ALG_TYPE_MASK;
  151. return crypto_has_alg(alg_name, type, mask);
  152. }
  153. /**
  154. * acomp_request_alloc() -- allocates asynchronous (de)compression request
  155. *
  156. * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
  157. *
  158. * Return: allocated handle in case of success or NULL in case of an error
  159. */
  160. struct acomp_req *acomp_request_alloc(struct crypto_acomp *tfm);
  161. /**
  162. * acomp_request_free() -- zeroize and free asynchronous (de)compression
  163. * request as well as the output buffer if allocated
  164. * inside the algorithm
  165. *
  166. * @req: request to free
  167. */
  168. void acomp_request_free(struct acomp_req *req);
  169. /**
  170. * acomp_request_set_callback() -- Sets an asynchronous callback
  171. *
  172. * Callback will be called when an asynchronous operation on a given
  173. * request is finished.
  174. *
  175. * @req: request that the callback will be set for
  176. * @flgs: specify for instance if the operation may backlog
  177. * @cmlp: callback which will be called
  178. * @data: private data used by the caller
  179. */
  180. static inline void acomp_request_set_callback(struct acomp_req *req,
  181. u32 flgs,
  182. crypto_completion_t cmpl,
  183. void *data)
  184. {
  185. req->base.complete = cmpl;
  186. req->base.data = data;
  187. req->base.flags = flgs;
  188. }
  189. /**
  190. * acomp_request_set_params() -- Sets request parameters
  191. *
  192. * Sets parameters required by an acomp operation
  193. *
  194. * @req: asynchronous compress request
  195. * @src: pointer to input buffer scatterlist
  196. * @dst: pointer to output buffer scatterlist. If this is NULL, the
  197. * acomp layer will allocate the output memory
  198. * @slen: size of the input buffer
  199. * @dlen: size of the output buffer. If dst is NULL, this can be used by
  200. * the user to specify the maximum amount of memory to allocate
  201. */
  202. static inline void acomp_request_set_params(struct acomp_req *req,
  203. struct scatterlist *src,
  204. struct scatterlist *dst,
  205. unsigned int slen,
  206. unsigned int dlen)
  207. {
  208. req->src = src;
  209. req->dst = dst;
  210. req->slen = slen;
  211. req->dlen = dlen;
  212. if (!req->dst)
  213. req->flags |= CRYPTO_ACOMP_ALLOC_OUTPUT;
  214. }
  215. /**
  216. * crypto_acomp_compress() -- Invoke asynchronous compress operation
  217. *
  218. * Function invokes the asynchronous compress operation
  219. *
  220. * @req: asynchronous compress request
  221. *
  222. * Return: zero on success; error code in case of error
  223. */
  224. static inline int crypto_acomp_compress(struct acomp_req *req)
  225. {
  226. struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
  227. return tfm->compress(req);
  228. }
  229. /**
  230. * crypto_acomp_decompress() -- Invoke asynchronous decompress operation
  231. *
  232. * Function invokes the asynchronous decompress operation
  233. *
  234. * @req: asynchronous compress request
  235. *
  236. * Return: zero on success; error code in case of error
  237. */
  238. static inline int crypto_acomp_decompress(struct acomp_req *req)
  239. {
  240. struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
  241. return tfm->decompress(req);
  242. }
  243. #endif