Commit aba973c6 authored by Gilad Ben-Yossef's avatar Gilad Ben-Yossef Committed by Herbert Xu

crypto: doc - adapt api sample to use async. op wait

The code sample is waiting for an async. crypto op completion.
Adapt sample to use the new generic infrastructure to do the same.

This also fixes a possible data coruption bug created by the
use of wait_for_completion_interruptible() without dealing
correctly with an interrupt aborting the wait prior to the
async op finishing.
Signed-off-by: default avatarGilad Ben-Yossef <gilad@benyossef.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 591b95af
...@@ -7,59 +7,27 @@ Code Example For Symmetric Key Cipher Operation ...@@ -7,59 +7,27 @@ Code Example For Symmetric Key Cipher Operation
:: ::
struct tcrypt_result {
struct completion completion;
int err;
};
/* tie all data structures together */ /* tie all data structures together */
struct skcipher_def { struct skcipher_def {
struct scatterlist sg; struct scatterlist sg;
struct crypto_skcipher *tfm; struct crypto_skcipher *tfm;
struct skcipher_request *req; struct skcipher_request *req;
struct tcrypt_result result; struct crypto_wait wait;
}; };
/* Callback function */
static void test_skcipher_cb(struct crypto_async_request *req, int error)
{
struct tcrypt_result *result = req->data;
if (error == -EINPROGRESS)
return;
result->err = error;
complete(&result->completion);
pr_info("Encryption finished successfully\n");
}
/* Perform cipher operation */ /* Perform cipher operation */
static unsigned int test_skcipher_encdec(struct skcipher_def *sk, static unsigned int test_skcipher_encdec(struct skcipher_def *sk,
int enc) int enc)
{ {
int rc = 0; int rc;
if (enc) if (enc)
rc = crypto_skcipher_encrypt(sk->req); rc = crypto_wait_req(crypto_skcipher_encrypt(sk->req), &sk->wait);
else else
rc = crypto_skcipher_decrypt(sk->req); rc = crypto_wait_req(crypto_skcipher_decrypt(sk->req), &sk->wait);
switch (rc) { if (rc)
case 0: pr_info("skcipher encrypt returned with result %d\n", rc);
break;
case -EINPROGRESS:
case -EBUSY:
rc = wait_for_completion_interruptible(
&sk->result.completion);
if (!rc && !sk->result.err) {
reinit_completion(&sk->result.completion);
break;
}
default:
pr_info("skcipher encrypt returned with %d result %d\n",
rc, sk->result.err);
break;
}
init_completion(&sk->result.completion);
return rc; return rc;
} }
...@@ -89,8 +57,8 @@ Code Example For Symmetric Key Cipher Operation ...@@ -89,8 +57,8 @@ Code Example For Symmetric Key Cipher Operation
} }
skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
test_skcipher_cb, crypto_req_done,
&sk.result); &sk.wait);
/* AES 256 with random key */ /* AES 256 with random key */
get_random_bytes(&key, 32); get_random_bytes(&key, 32);
...@@ -122,7 +90,7 @@ Code Example For Symmetric Key Cipher Operation ...@@ -122,7 +90,7 @@ Code Example For Symmetric Key Cipher Operation
/* We encrypt one block */ /* We encrypt one block */
sg_init_one(&sk.sg, scratchpad, 16); sg_init_one(&sk.sg, scratchpad, 16);
skcipher_request_set_crypt(req, &sk.sg, &sk.sg, 16, ivdata); skcipher_request_set_crypt(req, &sk.sg, &sk.sg, 16, ivdata);
init_completion(&sk.result.completion); crypto_init_wait(&sk.wait);
/* encrypt data */ /* encrypt data */
ret = test_skcipher_encdec(&sk, 1); ret = test_skcipher_encdec(&sk, 1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment