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