Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

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

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
Loading
Loading
Loading
Loading
+10 −42
Original line number Diff line number Diff line
@@ -7,59 +7,27 @@ Code Example For Symmetric Key Cipher Operation
::


    struct tcrypt_result {
        struct completion completion;
        int err;
    };

    /* tie all data structures together */
    struct skcipher_def {
        struct scatterlist sg;
        struct crypto_skcipher *tfm;
        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 */
    static unsigned int test_skcipher_encdec(struct skcipher_def *sk,
                         int enc)
    {
        int rc = 0;
        int rc;

        if (enc)
            rc = crypto_skcipher_encrypt(sk->req);
            rc = crypto_wait_req(crypto_skcipher_encrypt(sk->req), &sk->wait);
        else
            rc = crypto_skcipher_decrypt(sk->req);

        switch (rc) {
        case 0:
            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);
            rc = crypto_wait_req(crypto_skcipher_decrypt(sk->req), &sk->wait);

	if (rc)
		pr_info("skcipher encrypt returned with result %d\n", rc);

        return rc;
    }
@@ -89,8 +57,8 @@ Code Example For Symmetric Key Cipher Operation
        }

        skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
                          test_skcipher_cb,
                          &sk.result);
                          crypto_req_done,
                          &sk.wait);

        /* AES 256 with random key */
        get_random_bytes(&key, 32);
@@ -122,7 +90,7 @@ Code Example For Symmetric Key Cipher Operation
        /* We encrypt one block */
        sg_init_one(&sk.sg, scratchpad, 16);
        skcipher_request_set_crypt(req, &sk.sg, &sk.sg, 16, ivdata);
        init_completion(&sk.result.completion);
        crypto_init_wait(&sk.wait);

        /* encrypt data */
        ret = test_skcipher_encdec(&sk, 1);