acompress.h 7.9 KB

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