From: Kees Cook <keescook@chromium.org> To: Herbert Xu <herbert@gondor.apana.org.au> Cc: Kees Cook <keescook@chromium.org>, Eric Biggers <ebiggers@google.com>, Ard Biesheuvel <ard.biesheuvel@linaro.org>, Gilad Ben-Yossef <gilad@benyossef.com>, Alexander Stein <alexander.stein@systec-electronic.com>, Antoine Tenart <antoine.tenart@bootlin.com>, Boris Brezillon <boris.brezillon@bootlin.com>, Arnaud Ebalard <arno@natisbad.org>, Corentin Labbe <clabbe.montjoie@gmail.com>, Maxime Ripard <maxime.ripard@bootlin.com>, Chen-Yu Tsai <wens@csie.org>, Christian Lamparter <chunkeey@gmail.com>, Philippe Ombredanne <pombredanne@nexb.com>, Jonathan Cameron <Jonathan.Cameron@huawei.com>, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v2 1/4] crypto: skcipher - Consolidate encrypt/decrypt sanity check Date: Thu, 6 Sep 2018 15:58:51 -0700 [thread overview] Message-ID: <20180906225854.40989-2-keescook@chromium.org> (raw) In-Reply-To: <20180906225854.40989-1-keescook@chromium.org> In preparation to adding additional sanity checks before running an skcipher request, this consolidates the open-coded checks into a single function. Instead of passing both req and tfm into the new check this just returns the tfm on success and an ERR_PTR on failure, keeping things as clean as possible. Signed-off-by: Kees Cook <keescook@chromium.org> --- include/crypto/skcipher.h | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/include/crypto/skcipher.h b/include/crypto/skcipher.h index 2f327f090c3e..6e954d398e0f 100644 --- a/include/crypto/skcipher.h +++ b/include/crypto/skcipher.h @@ -422,6 +422,27 @@ static inline struct crypto_skcipher *crypto_skcipher_reqtfm( return __crypto_skcipher_cast(req->base.tfm); } +/** + * crypto_skcipher_reqtfm_check() - obtain and check cipher handle from request + * @req: skcipher_request out of which the cipher handle is to be obtained + * + * Return the crypto_skcipher handle when furnishing an skcipher_request + * data structure. Check for error conditions that would make it unusable + * for an encrypt/decrypt call. + * + * Return: crypto_skcipher handle, or ERR_PTR on error. + */ +static inline struct crypto_skcipher *crypto_skcipher_reqtfm_check( + struct skcipher_request *req) +{ + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); + + if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) + return ERR_PTR(-ENOKEY); + + return tfm; +} + /** * crypto_skcipher_encrypt() - encrypt plaintext * @req: reference to the skcipher_request handle that holds all information @@ -435,10 +456,10 @@ static inline struct crypto_skcipher *crypto_skcipher_reqtfm( */ static inline int crypto_skcipher_encrypt(struct skcipher_request *req) { - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm_check(req); - if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) - return -ENOKEY; + if (IS_ERR(tfm)) + return PTR_ERR(tfm); return tfm->encrypt(req); } @@ -456,10 +477,10 @@ static inline int crypto_skcipher_encrypt(struct skcipher_request *req) */ static inline int crypto_skcipher_decrypt(struct skcipher_request *req) { - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm_check(req); - if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) - return -ENOKEY; + if (IS_ERR(tfm)) + return PTR_ERR(tfm); return tfm->decrypt(req); } -- 2.17.1
WARNING: multiple messages have this Message-ID (diff)
From: keescook@chromium.org (Kees Cook) To: linux-arm-kernel@lists.infradead.org Subject: [PATCH v2 1/4] crypto: skcipher - Consolidate encrypt/decrypt sanity check Date: Thu, 6 Sep 2018 15:58:51 -0700 [thread overview] Message-ID: <20180906225854.40989-2-keescook@chromium.org> (raw) In-Reply-To: <20180906225854.40989-1-keescook@chromium.org> In preparation to adding additional sanity checks before running an skcipher request, this consolidates the open-coded checks into a single function. Instead of passing both req and tfm into the new check this just returns the tfm on success and an ERR_PTR on failure, keeping things as clean as possible. Signed-off-by: Kees Cook <keescook@chromium.org> --- include/crypto/skcipher.h | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/include/crypto/skcipher.h b/include/crypto/skcipher.h index 2f327f090c3e..6e954d398e0f 100644 --- a/include/crypto/skcipher.h +++ b/include/crypto/skcipher.h @@ -422,6 +422,27 @@ static inline struct crypto_skcipher *crypto_skcipher_reqtfm( return __crypto_skcipher_cast(req->base.tfm); } +/** + * crypto_skcipher_reqtfm_check() - obtain and check cipher handle from request + * @req: skcipher_request out of which the cipher handle is to be obtained + * + * Return the crypto_skcipher handle when furnishing an skcipher_request + * data structure. Check for error conditions that would make it unusable + * for an encrypt/decrypt call. + * + * Return: crypto_skcipher handle, or ERR_PTR on error. + */ +static inline struct crypto_skcipher *crypto_skcipher_reqtfm_check( + struct skcipher_request *req) +{ + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); + + if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) + return ERR_PTR(-ENOKEY); + + return tfm; +} + /** * crypto_skcipher_encrypt() - encrypt plaintext * @req: reference to the skcipher_request handle that holds all information @@ -435,10 +456,10 @@ static inline struct crypto_skcipher *crypto_skcipher_reqtfm( */ static inline int crypto_skcipher_encrypt(struct skcipher_request *req) { - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm_check(req); - if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) - return -ENOKEY; + if (IS_ERR(tfm)) + return PTR_ERR(tfm); return tfm->encrypt(req); } @@ -456,10 +477,10 @@ static inline int crypto_skcipher_encrypt(struct skcipher_request *req) */ static inline int crypto_skcipher_decrypt(struct skcipher_request *req) { - struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm_check(req); - if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) - return -ENOKEY; + if (IS_ERR(tfm)) + return PTR_ERR(tfm); return tfm->decrypt(req); } -- 2.17.1
next prev parent reply other threads:[~2018-09-06 22:58 UTC|newest] Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top 2018-09-06 22:58 [PATCH v2 0/4] crypto: skcipher - Remove VLA usage Kees Cook 2018-09-06 22:58 ` Kees Cook 2018-09-06 22:58 ` Kees Cook [this message] 2018-09-06 22:58 ` [PATCH v2 1/4] crypto: skcipher - Consolidate encrypt/decrypt sanity check Kees Cook 2018-09-06 22:58 ` [PATCH v2 2/4] crypto: skcipher - Enforce non-ASYNC for on-stack requests Kees Cook 2018-09-06 22:58 ` Kees Cook 2018-09-07 3:42 ` Herbert Xu 2018-09-07 3:42 ` Herbert Xu 2018-09-07 6:56 ` Ard Biesheuvel 2018-09-07 6:56 ` Ard Biesheuvel 2018-09-07 6:56 ` Ard Biesheuvel 2018-09-11 5:52 ` Herbert Xu 2018-09-11 5:52 ` Herbert Xu 2018-09-11 5:52 ` Herbert Xu 2018-09-13 16:46 ` Kees Cook 2018-09-13 16:46 ` Kees Cook 2018-09-13 16:46 ` Kees Cook 2018-09-13 17:40 ` Kees Cook 2018-09-13 17:40 ` Kees Cook 2018-09-13 17:40 ` Kees Cook 2018-09-07 16:02 ` Kees Cook 2018-09-07 16:02 ` Kees Cook 2018-09-11 5:53 ` Herbert Xu 2018-09-11 5:53 ` Herbert Xu 2018-09-06 22:58 ` [PATCH v2 3/4] crypto: skcipher - Remove VLA usage for SKCIPHER_REQUEST_ON_STACK Kees Cook 2018-09-06 22:58 ` Kees Cook 2018-09-06 22:58 ` [PATCH 4/4] crypto: skcipher - Remove unused argument to SKCIPHER_REQUEST_ON_STACK() Kees Cook 2018-09-06 22:58 ` Kees Cook
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20180906225854.40989-2-keescook@chromium.org \ --to=keescook@chromium.org \ --cc=Jonathan.Cameron@huawei.com \ --cc=alexander.stein@systec-electronic.com \ --cc=antoine.tenart@bootlin.com \ --cc=ard.biesheuvel@linaro.org \ --cc=arno@natisbad.org \ --cc=boris.brezillon@bootlin.com \ --cc=chunkeey@gmail.com \ --cc=clabbe.montjoie@gmail.com \ --cc=ebiggers@google.com \ --cc=gilad@benyossef.com \ --cc=herbert@gondor.apana.org.au \ --cc=linux-arm-kernel@lists.infradead.org \ --cc=linux-crypto@vger.kernel.org \ --cc=linux-kernel@vger.kernel.org \ --cc=maxime.ripard@bootlin.com \ --cc=pombredanne@nexb.com \ --cc=wens@csie.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.