From: Ard Biesheuvel <ardb@kernel.org> To: linux-crypto@vger.kernel.org Cc: "Eric Biggers" <ebiggers@google.com>, "Corentin Labbe" <clabbe.montjoie@gmail.com>, "Ayush Sawal" <ayush.sawal@chelsio.com>, "Jamie Iles" <jamie@jamieiles.com>, "Fabio Estevam" <festevam@gmail.com>, "Ard Biesheuvel" <ardb@kernel.org>, "Herbert Xu" <herbert@gondor.apana.org.au>, "Horia Geantă" <horia.geanta@nxp.com>, "Rohit Maheshwari" <rohitm@chelsio.com>, "Chen-Yu Tsai" <wens@csie.org>, "Corentin Labbe" <clabbe@baylibre.com>, "NXP Linux Team" <linux-imx@nxp.com>, "Tom Lendacky" <thomas.lendacky@amd.com>, "Sascha Hauer" <s.hauer@pengutronix.de>, "Vinay Kumar Yadav" <vinay.yadav@chelsio.com>, "Maxime Ripard" <mripard@kernel.org>, "Matthias Brugger" <matthias.bgg@gmail.com>, linux-amlogic@lists.infradead.org, linux-omap@vger.kernel.org, linux-arm-kernel@lists.infradead.org, "John Allen" <john.allen@amd.com>, "Tero Kristo" <t-kristo@ti.com>, "Pengutronix Kernel Team" <kernel@pengutronix.de>, "Shawn Guo" <shawnguo@kernel.org>, "David S. Miller" <davem@davemloft.net> Subject: [PATCH v4 05/13] crypto: sun8i-ce - permit asynchronous skcipher as fallback Date: Tue, 7 Jul 2020 09:31:55 +0300 Message-ID: <20200707063203.5018-6-ardb@kernel.org> (raw) In-Reply-To: <20200707063203.5018-1-ardb@kernel.org> Even though the sun8i-ce driver implements asynchronous versions of ecb(aes) and cbc(aes), the fallbacks it allocates are required to be synchronous. Given that SIMD based software implementations are usually asynchronous as well, even though they rarely complete asynchronously (this typically only happens in cases where the request was made from softirq context, while SIMD was already in use in the task context that it interrupted), these implementations are disregarded, and either the generic C version or another table based version implemented in assembler is selected instead. Since falling back to synchronous AES is not only a performance issue, but potentially a security issue as well (due to the fact that table based AES is not time invariant), let's fix this, by allocating an ordinary skcipher as the fallback, and invoke it with the completion routine that was given to the outer request. Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Acked-by: Corentin Labbe <clabbe.montjoie@gmail.com> --- drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c | 41 ++++++++++---------- drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h | 8 ++-- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c index 3665a0a2038f..1e4f9a58bb24 100644 --- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c +++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c @@ -58,23 +58,20 @@ static int sun8i_ce_cipher_fallback(struct skcipher_request *areq) #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG struct skcipher_alg *alg = crypto_skcipher_alg(tfm); struct sun8i_ce_alg_template *algt; -#endif - SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, op->fallback_tfm); -#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher); algt->stat_fb++; #endif - skcipher_request_set_sync_tfm(subreq, op->fallback_tfm); - skcipher_request_set_callback(subreq, areq->base.flags, NULL, NULL); - skcipher_request_set_crypt(subreq, areq->src, areq->dst, + skcipher_request_set_tfm(&rctx->fallback_req, op->fallback_tfm); + skcipher_request_set_callback(&rctx->fallback_req, areq->base.flags, + areq->base.complete, areq->base.data); + skcipher_request_set_crypt(&rctx->fallback_req, areq->src, areq->dst, areq->cryptlen, areq->iv); if (rctx->op_dir & CE_DECRYPTION) - err = crypto_skcipher_decrypt(subreq); + err = crypto_skcipher_decrypt(&rctx->fallback_req); else - err = crypto_skcipher_encrypt(subreq); - skcipher_request_zero(subreq); + err = crypto_skcipher_encrypt(&rctx->fallback_req); return err; } @@ -335,18 +332,20 @@ int sun8i_ce_cipher_init(struct crypto_tfm *tfm) algt = container_of(alg, struct sun8i_ce_alg_template, alg.skcipher); op->ce = algt->ce; - sktfm->reqsize = sizeof(struct sun8i_cipher_req_ctx); - - op->fallback_tfm = crypto_alloc_sync_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK); + op->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK); if (IS_ERR(op->fallback_tfm)) { dev_err(op->ce->dev, "ERROR: Cannot allocate fallback for %s %ld\n", name, PTR_ERR(op->fallback_tfm)); return PTR_ERR(op->fallback_tfm); } + sktfm->reqsize = sizeof(struct sun8i_cipher_req_ctx) + + crypto_skcipher_reqsize(op->fallback_tfm); + + dev_info(op->ce->dev, "Fallback for %s is %s\n", crypto_tfm_alg_driver_name(&sktfm->base), - crypto_tfm_alg_driver_name(crypto_skcipher_tfm(&op->fallback_tfm->base))); + crypto_tfm_alg_driver_name(crypto_skcipher_tfm(op->fallback_tfm))); op->enginectx.op.do_one_request = sun8i_ce_handle_cipher_request; op->enginectx.op.prepare_request = NULL; @@ -359,7 +358,7 @@ int sun8i_ce_cipher_init(struct crypto_tfm *tfm) return 0; error_pm: pm_runtime_put_noidle(op->ce->dev); - crypto_free_sync_skcipher(op->fallback_tfm); + crypto_free_skcipher(op->fallback_tfm); return err; } @@ -371,7 +370,7 @@ void sun8i_ce_cipher_exit(struct crypto_tfm *tfm) memzero_explicit(op->key, op->keylen); kfree(op->key); } - crypto_free_sync_skcipher(op->fallback_tfm); + crypto_free_skcipher(op->fallback_tfm); pm_runtime_put_sync_suspend(op->ce->dev); } @@ -401,10 +400,10 @@ int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, if (!op->key) return -ENOMEM; - crypto_sync_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK); - crypto_sync_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK); + crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK); + crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK); - return crypto_sync_skcipher_setkey(op->fallback_tfm, key, keylen); + return crypto_skcipher_setkey(op->fallback_tfm, key, keylen); } int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key, @@ -426,8 +425,8 @@ int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key, if (!op->key) return -ENOMEM; - crypto_sync_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK); - crypto_sync_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK); + crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK); + crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK); - return crypto_sync_skcipher_setkey(op->fallback_tfm, key, keylen); + return crypto_skcipher_setkey(op->fallback_tfm, key, keylen); } diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h index 0e9eac397e1b..963645fe4adb 100644 --- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h +++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h @@ -181,12 +181,14 @@ struct sun8i_ce_dev { /* * struct sun8i_cipher_req_ctx - context for a skcipher request - * @op_dir: direction (encrypt vs decrypt) for this request - * @flow: the flow to use for this request + * @op_dir: direction (encrypt vs decrypt) for this request + * @flow: the flow to use for this request + * @fallback_req: request struct for invoking the fallback skcipher TFM */ struct sun8i_cipher_req_ctx { u32 op_dir; int flow; + struct skcipher_request fallback_req; // keep at the end }; /* @@ -202,7 +204,7 @@ struct sun8i_cipher_tfm_ctx { u32 *key; u32 keylen; struct sun8i_ce_dev *ce; - struct crypto_sync_skcipher *fallback_tfm; + struct crypto_skcipher *fallback_tfm; }; /* -- 2.17.1 _______________________________________________ linux-amlogic mailing list linux-amlogic@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-amlogic
next prev parent reply index Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-07-07 6:31 [PATCH v4 00/13] crypto: permit asynchronous skciphers as driver fallbacks Ard Biesheuvel 2020-07-07 6:31 ` [PATCH v4 01/13] crypto: amlogic-gxl - default to build as module Ard Biesheuvel 2020-07-07 6:31 ` [PATCH v4 02/13] crypto: amlogic-gxl - permit async skcipher as fallback Ard Biesheuvel 2020-07-07 6:31 ` [PATCH v4 03/13] crypto: omap-aes - permit asynchronous " Ard Biesheuvel 2020-07-07 6:31 ` [PATCH v4 04/13] crypto: sun4i " Ard Biesheuvel 2020-07-07 6:31 ` Ard Biesheuvel [this message] 2020-07-08 7:53 ` [PATCH v4 05/13] crypto: sun8i-ce " Corentin Labbe 2020-07-07 6:31 ` [PATCH v4 06/13] crypto: sun8i-ss " Ard Biesheuvel 2020-07-07 6:31 ` [PATCH v4 07/13] crypto: ccp " Ard Biesheuvel 2020-07-07 20:04 ` John Allen 2020-07-07 6:31 ` [PATCH v4 08/13] crypto: chelsio " Ard Biesheuvel 2020-07-07 6:31 ` [PATCH v4 09/13] crypto: mxs-dcp " Ard Biesheuvel 2020-07-07 6:32 ` [PATCH v4 10/13] crypto: picoxcell " Ard Biesheuvel 2020-07-07 6:32 ` [PATCH v4 11/13] crypto: qce " Ard Biesheuvel 2020-07-07 6:32 ` [PATCH v4 12/13] crypto: sahara " Ard Biesheuvel 2020-07-07 6:32 ` [PATCH v4 13/13] crypto: mediatek - use AES library for GCM key derivation Ard Biesheuvel 2020-07-16 11:53 ` [PATCH v4 00/13] crypto: permit asynchronous skciphers as driver fallbacks Herbert Xu 2020-08-17 17:48 ` patchwork-bot+linux-amlogic
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=20200707063203.5018-6-ardb@kernel.org \ --to=ardb@kernel.org \ --cc=ayush.sawal@chelsio.com \ --cc=clabbe.montjoie@gmail.com \ --cc=clabbe@baylibre.com \ --cc=davem@davemloft.net \ --cc=ebiggers@google.com \ --cc=festevam@gmail.com \ --cc=herbert@gondor.apana.org.au \ --cc=horia.geanta@nxp.com \ --cc=jamie@jamieiles.com \ --cc=john.allen@amd.com \ --cc=kernel@pengutronix.de \ --cc=linux-amlogic@lists.infradead.org \ --cc=linux-arm-kernel@lists.infradead.org \ --cc=linux-crypto@vger.kernel.org \ --cc=linux-imx@nxp.com \ --cc=linux-omap@vger.kernel.org \ --cc=matthias.bgg@gmail.com \ --cc=mripard@kernel.org \ --cc=rohitm@chelsio.com \ --cc=s.hauer@pengutronix.de \ --cc=shawnguo@kernel.org \ --cc=t-kristo@ti.com \ --cc=thomas.lendacky@amd.com \ --cc=vinay.yadav@chelsio.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: link
Linux-Amlogic Archive on lore.kernel.org Archives are clonable: git clone --mirror https://lore.kernel.org/linux-amlogic/0 linux-amlogic/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 linux-amlogic linux-amlogic/ https://lore.kernel.org/linux-amlogic \ linux-amlogic@lists.infradead.org public-inbox-index linux-amlogic Example config snippet for mirrors Newsgroup available over NNTP: nntp://nntp.lore.kernel.org/org.infradead.lists.linux-amlogic AGPL code for this site: git clone https://public-inbox.org/public-inbox.git