acompress.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. * If @tfm is a NULL or error pointer, this function does nothing.
  143. */
  144. static inline void crypto_free_acomp(struct crypto_acomp *tfm)
  145. {
  146. crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm));
  147. }
  148. static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask)
  149. {
  150. type &= ~CRYPTO_ALG_TYPE_MASK;
  151. type |= CRYPTO_ALG_TYPE_ACOMPRESS;
  152. mask |= CRYPTO_ALG_TYPE_MASK;
  153. return crypto_has_alg(alg_name, type, mask);
  154. }
  155. /**
  156. * acomp_request_alloc() -- allocates asynchronous (de)compression request
  157. *
  158. * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
  159. *
  160. * Return: allocated handle in case of success or NULL in case of an error
  161. */
  162. struct acomp_req *acomp_request_alloc(struct crypto_acomp *tfm);
  163. /**
  164. * acomp_request_free() -- zeroize and free asynchronous (de)compression
  165. * request as well as the output buffer if allocated
  166. * inside the algorithm
  167. *
  168. * @req: request to free
  169. */
  170. void acomp_request_free(struct acomp_req *req);
  171. /**
  172. * acomp_request_set_callback() -- Sets an asynchronous callback
  173. *
  174. * Callback will be called when an asynchronous operation on a given
  175. * request is finished.
  176. *
  177. * @req: request that the callback will be set for
  178. * @flgs: specify for instance if the operation may backlog
  179. * @cmlp: callback which will be called
  180. * @data: private data used by the caller
  181. */
  182. static inline void acomp_request_set_callback(struct acomp_req *req,
  183. u32 flgs,
  184. crypto_completion_t cmpl,
  185. void *data)
  186. {
  187. req->base.complete = cmpl;
  188. req->base.data = data;
  189. req->base.flags = flgs;
  190. }
  191. /**
  192. * acomp_request_set_params() -- Sets request parameters
  193. *
  194. * Sets parameters required by an acomp operation
  195. *
  196. * @req: asynchronous compress request
  197. * @src: pointer to input buffer scatterlist
  198. * @dst: pointer to output buffer scatterlist. If this is NULL, the
  199. * acomp layer will allocate the output memory
  200. * @slen: size of the input buffer
  201. * @dlen: size of the output buffer. If dst is NULL, this can be used by
  202. * the user to specify the maximum amount of memory to allocate
  203. */
  204. static inline void acomp_request_set_params(struct acomp_req *req,
  205. struct scatterlist *src,
  206. struct scatterlist *dst,
  207. unsigned int slen,
  208. unsigned int dlen)
  209. {
  210. req->src = src;
  211. req->dst = dst;
  212. req->slen = slen;
  213. req->dlen = dlen;
  214. if (!req->dst)
  215. req->flags |= CRYPTO_ACOMP_ALLOC_OUTPUT;
  216. }
  217. /**
  218. * crypto_acomp_compress() -- Invoke asynchronous compress operation
  219. *
  220. * Function invokes the asynchronous compress operation
  221. *
  222. * @req: asynchronous compress request
  223. *
  224. * Return: zero on success; error code in case of error
  225. */
  226. static inline int crypto_acomp_compress(struct acomp_req *req)
  227. {
  228. struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
  229. return tfm->compress(req);
  230. }
  231. /**
  232. * crypto_acomp_decompress() -- Invoke asynchronous decompress operation
  233. *
  234. * Function invokes the asynchronous decompress operation
  235. *
  236. * @req: asynchronous compress request
  237. *
  238. * Return: zero on success; error code in case of error
  239. */
  240. static inline int crypto_acomp_decompress(struct acomp_req *req)
  241. {
  242. struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
  243. return tfm->decompress(req);
  244. }
  245. #endif