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