Skip to content

Commit 1ffc9fb

Browse files
Marek Vasutherbertx
Marek Vasut
authored andcommitted
crypto: hash - Pull out the functions to save/restore request
The functions to save original request within a newly adjusted request and it's counterpart to restore the original request can be re-used by more code in the crypto/ahash.c file. Pull these functions out from the code so they're available. Signed-off-by: Marek Vasut <[email protected]> Cc: David S. Miller <[email protected]> Cc: Fabio Estevam <[email protected]> Cc: Herbert Xu <[email protected]> Cc: Shawn Guo <[email protected]> Cc: Tom Lendacky <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent ab6bf4e commit 1ffc9fb

File tree

1 file changed

+62
-45
lines changed

1 file changed

+62
-45
lines changed

crypto/ahash.c

+62-45
Original file line numberDiff line numberDiff line change
@@ -190,55 +190,12 @@ static inline unsigned int ahash_align_buffer_size(unsigned len,
190190
return len + (mask & ~(crypto_tfm_ctx_alignment() - 1));
191191
}
192192

193-
static void ahash_op_unaligned_finish(struct ahash_request *req, int err)
194-
{
195-
struct ahash_request_priv *priv = req->priv;
196-
197-
if (err == -EINPROGRESS)
198-
return;
199-
200-
if (!err)
201-
memcpy(priv->result, req->result,
202-
crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
203-
204-
/* Restore the original crypto request. */
205-
req->result = priv->result;
206-
req->base.complete = priv->complete;
207-
req->base.data = priv->data;
208-
req->priv = NULL;
209-
210-
/* Free the req->priv.priv from the ADJUSTED request. */
211-
kzfree(priv);
212-
}
213-
214-
static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
215-
{
216-
struct ahash_request *areq = req->data;
217-
218-
/*
219-
* Restore the original request, see ahash_op_unaligned() for what
220-
* goes where.
221-
*
222-
* The "struct ahash_request *req" here is in fact the "req.base"
223-
* from the ADJUSTED request from ahash_op_unaligned(), thus as it
224-
* is a pointer to self, it is also the ADJUSTED "req" .
225-
*/
226-
227-
/* First copy areq->result into areq->priv.result */
228-
ahash_op_unaligned_finish(areq, err);
229-
230-
/* Complete the ORIGINAL request. */
231-
areq->base.complete(&areq->base, err);
232-
}
233-
234-
static int ahash_op_unaligned(struct ahash_request *req,
235-
int (*op)(struct ahash_request *))
193+
static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt)
236194
{
237195
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
238196
unsigned long alignmask = crypto_ahash_alignmask(tfm);
239197
unsigned int ds = crypto_ahash_digestsize(tfm);
240198
struct ahash_request_priv *priv;
241-
int err;
242199

243200
priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
244201
(req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
@@ -281,10 +238,70 @@ static int ahash_op_unaligned(struct ahash_request *req,
281238
*/
282239

283240
req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
284-
req->base.complete = ahash_op_unaligned_done;
241+
req->base.complete = cplt;
285242
req->base.data = req;
286243
req->priv = priv;
287244

245+
return 0;
246+
}
247+
248+
static void ahash_restore_req(struct ahash_request *req)
249+
{
250+
struct ahash_request_priv *priv = req->priv;
251+
252+
/* Restore the original crypto request. */
253+
req->result = priv->result;
254+
req->base.complete = priv->complete;
255+
req->base.data = priv->data;
256+
req->priv = NULL;
257+
258+
/* Free the req->priv.priv from the ADJUSTED request. */
259+
kzfree(priv);
260+
}
261+
262+
static void ahash_op_unaligned_finish(struct ahash_request *req, int err)
263+
{
264+
struct ahash_request_priv *priv = req->priv;
265+
266+
if (err == -EINPROGRESS)
267+
return;
268+
269+
if (!err)
270+
memcpy(priv->result, req->result,
271+
crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
272+
273+
ahash_restore_req(req);
274+
}
275+
276+
static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
277+
{
278+
struct ahash_request *areq = req->data;
279+
280+
/*
281+
* Restore the original request, see ahash_op_unaligned() for what
282+
* goes where.
283+
*
284+
* The "struct ahash_request *req" here is in fact the "req.base"
285+
* from the ADJUSTED request from ahash_op_unaligned(), thus as it
286+
* is a pointer to self, it is also the ADJUSTED "req" .
287+
*/
288+
289+
/* First copy req->result into req->priv.result */
290+
ahash_op_unaligned_finish(areq, err);
291+
292+
/* Complete the ORIGINAL request. */
293+
areq->base.complete(&areq->base, err);
294+
}
295+
296+
static int ahash_op_unaligned(struct ahash_request *req,
297+
int (*op)(struct ahash_request *))
298+
{
299+
int err;
300+
301+
err = ahash_save_req(req, ahash_op_unaligned_done);
302+
if (err)
303+
return err;
304+
288305
err = op(req);
289306
ahash_op_unaligned_finish(req, err);
290307

0 commit comments

Comments
 (0)