All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx
@ 2016-01-13 17:52 Fabio Estevam
  2016-01-13 17:52 ` [PATCH v2 2/3] crypto: sahara - fill the statesize field Fabio Estevam
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Fabio Estevam @ 2016-01-13 17:52 UTC (permalink / raw)
  To: herbert
  Cc: marex, s.trumtrar, thomas.lendacky, linux, linux-crypto, Fabio Estevam

From: Fabio Estevam <fabio.estevam@nxp.com>

Based on commit 434b421241f2d0 ("crypto: caam - avoid needlessly saving and
restoring caam_hash_ctx") from Russell King.

When exporting and importing the hash state, we will only export and
import into hashes which share the same struct crypto_ahash pointer.
(See hash_accept->af_alg_accept->hash_accept_parent.)
    
This means that saving the sahara_ctx structure on export, and
restoring it on import is a waste of resources.  So, remove this code.

Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>
---
Changes since v1:
- Newly introduced in this series

 drivers/crypto/sahara.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/drivers/crypto/sahara.c b/drivers/crypto/sahara.c
index f68c24a..53c7a9a 100644
--- a/drivers/crypto/sahara.c
+++ b/drivers/crypto/sahara.c
@@ -1155,26 +1155,18 @@ static int sahara_sha_digest(struct ahash_request *req)
 
 static int sahara_sha_export(struct ahash_request *req, void *out)
 {
-	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
-	struct sahara_ctx *ctx = crypto_ahash_ctx(ahash);
 	struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
 
-	memcpy(out, ctx, sizeof(struct sahara_ctx));
-	memcpy(out + sizeof(struct sahara_sha_reqctx), rctx,
-	       sizeof(struct sahara_sha_reqctx));
+	memcpy(out, rctx, sizeof(struct sahara_sha_reqctx));
 
 	return 0;
 }
 
 static int sahara_sha_import(struct ahash_request *req, const void *in)
 {
-	struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
-	struct sahara_ctx *ctx = crypto_ahash_ctx(ahash);
 	struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
 
-	memcpy(ctx, in, sizeof(struct sahara_ctx));
-	memcpy(rctx, in + sizeof(struct sahara_sha_reqctx),
-	       sizeof(struct sahara_sha_reqctx));
+	memcpy(rctx, in, sizeof(struct sahara_sha_reqctx));
 
 	return 0;
 }
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 2/3] crypto: sahara - fill the statesize field
  2016-01-13 17:52 [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx Fabio Estevam
@ 2016-01-13 17:52 ` Fabio Estevam
  2016-01-13 17:52 ` [PATCH v2 3/3] crypto: mxs-dcp - provide statesize and import/export() Fabio Estevam
  2016-01-25 14:07 ` [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx Herbert Xu
  2 siblings, 0 replies; 9+ messages in thread
From: Fabio Estevam @ 2016-01-13 17:52 UTC (permalink / raw)
  To: herbert
  Cc: marex, s.trumtrar, thomas.lendacky, linux, linux-crypto, Fabio Estevam

From: Fabio Estevam <fabio.estevam@nxp.com>

Currently the sahara driver fails to probe:

sahara: probe of 63ff8000.crypto failed with error -22

This happens since commit 8996eafdcbad ("crypto: ahash - ensure statesize
is non-zero"), which requires statesize to be filled.

Pass the statesize members for sha1 and sha256, so we can probe
the driver successfully again.

Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>
---
Changes since v1:
- Fix the statesize field to match the size used in import/export (Tom Lendacky)

 drivers/crypto/sahara.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/crypto/sahara.c b/drivers/crypto/sahara.c
index 53c7a9a..239f31a 100644
--- a/drivers/crypto/sahara.c
+++ b/drivers/crypto/sahara.c
@@ -1252,6 +1252,7 @@ static struct ahash_alg sha_v3_algs[] = {
 	.export		= sahara_sha_export,
 	.import		= sahara_sha_import,
 	.halg.digestsize	= SHA1_DIGEST_SIZE,
+	.halg.statesize         = sizeof(struct sahara_sha_reqctx),
 	.halg.base	= {
 		.cra_name		= "sha1",
 		.cra_driver_name	= "sahara-sha1",
@@ -1279,6 +1280,7 @@ static struct ahash_alg sha_v4_algs[] = {
 	.export		= sahara_sha_export,
 	.import		= sahara_sha_import,
 	.halg.digestsize	= SHA256_DIGEST_SIZE,
+	.halg.statesize         = sizeof(struct sahara_sha_reqctx),
 	.halg.base	= {
 		.cra_name		= "sha256",
 		.cra_driver_name	= "sahara-sha256",
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 3/3] crypto: mxs-dcp - provide statesize and import/export()
  2016-01-13 17:52 [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx Fabio Estevam
  2016-01-13 17:52 ` [PATCH v2 2/3] crypto: sahara - fill the statesize field Fabio Estevam
@ 2016-01-13 17:52 ` Fabio Estevam
  2016-01-25 14:10   ` Herbert Xu
  2016-01-25 14:07 ` [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx Herbert Xu
  2 siblings, 1 reply; 9+ messages in thread
From: Fabio Estevam @ 2016-01-13 17:52 UTC (permalink / raw)
  To: herbert
  Cc: marex, s.trumtrar, thomas.lendacky, linux, linux-crypto, Fabio Estevam

From: Fabio Estevam <fabio.estevam@nxp.com>

Currently the mxs-dcp driver fails to probe:

mxs-dcp 80028000.dcp: Failed to register sha1 hash!
mxs-dcp: probe of 80028000.dcp failed with error -22

This happens since commit 8996eafdcbad ("crypto: ahash - ensure statesize
is non-zero"), which requires statesize to be filled.

Other than filling statesize, we also need to provide the import/export
functions.

Based on the implementation of the sahara and caam drivers.

Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>
---
Changes since v2:
- Newly introduced in this series

 drivers/crypto/mxs-dcp.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/drivers/crypto/mxs-dcp.c b/drivers/crypto/mxs-dcp.c
index 59ed54e..1e2017f 100644
--- a/drivers/crypto/mxs-dcp.c
+++ b/drivers/crypto/mxs-dcp.c
@@ -775,6 +775,24 @@ static void dcp_sha_cra_exit(struct crypto_tfm *tfm)
 {
 }
 
+static int dcp_sha_export(struct ahash_request *req, void *out)
+{
+	struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
+
+	memcpy(out, rctx, sizeof(struct dcp_sha_req_ctx));
+
+	return 0;
+}
+
+static int dcp_sha_import(struct ahash_request *req, const void *in)
+{
+	struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
+
+	memcpy(rctx, in, sizeof(struct dcp_sha_req_ctx));
+
+	return 0;
+}
+
 /* AES 128 ECB and AES 128 CBC */
 static struct crypto_alg dcp_aes_algs[] = {
 	{
@@ -834,8 +852,11 @@ static struct ahash_alg dcp_sha1_alg = {
 	.final	= dcp_sha_final,
 	.finup	= dcp_sha_finup,
 	.digest	= dcp_sha_digest,
+	.import = dcp_sha_import,
+	.export = dcp_sha_export,
 	.halg	= {
 		.digestsize	= SHA1_DIGEST_SIZE,
+		.statesize	= sizeof(struct dcp_sha_req_ctx),
 		.base		= {
 			.cra_name		= "sha1",
 			.cra_driver_name	= "sha1-dcp",
@@ -858,8 +879,11 @@ static struct ahash_alg dcp_sha256_alg = {
 	.final	= dcp_sha_final,
 	.finup	= dcp_sha_finup,
 	.digest	= dcp_sha_digest,
+	.import = dcp_sha_import,
+	.export = dcp_sha_export,
 	.halg	= {
 		.digestsize	= SHA256_DIGEST_SIZE,
+		.statesize	= sizeof(struct dcp_sha_req_ctx),
 		.base		= {
 			.cra_name		= "sha256",
 			.cra_driver_name	= "sha256-dcp",
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx
  2016-01-13 17:52 [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx Fabio Estevam
  2016-01-13 17:52 ` [PATCH v2 2/3] crypto: sahara - fill the statesize field Fabio Estevam
  2016-01-13 17:52 ` [PATCH v2 3/3] crypto: mxs-dcp - provide statesize and import/export() Fabio Estevam
@ 2016-01-25 14:07 ` Herbert Xu
  2016-02-02 13:41   ` Fabio Estevam
  2 siblings, 1 reply; 9+ messages in thread
From: Herbert Xu @ 2016-01-25 14:07 UTC (permalink / raw)
  To: Fabio Estevam
  Cc: marex, s.trumtrar, thomas.lendacky, linux, linux-crypto, Fabio Estevam

On Wed, Jan 13, 2016 at 03:52:02PM -0200, Fabio Estevam wrote:
> From: Fabio Estevam <fabio.estevam@nxp.com>
> 
> Based on commit 434b421241f2d0 ("crypto: caam - avoid needlessly saving and
> restoring caam_hash_ctx") from Russell King.
> 
> When exporting and importing the hash state, we will only export and
> import into hashes which share the same struct crypto_ahash pointer.
> (See hash_accept->af_alg_accept->hash_accept_parent.)
>     
> This means that saving the sahara_ctx structure on export, and
> restoring it on import is a waste of resources.  So, remove this code.
> 
> Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>

Very good.  Not only is it a waste, it's a gaping security hole
because modifying the tfm from import will corrupt it.

But this is not enough, you're still copying things like the mutex
which should not be copied but instead should be reinitialised in
import.

Thanks,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 3/3] crypto: mxs-dcp - provide statesize and import/export()
  2016-01-13 17:52 ` [PATCH v2 3/3] crypto: mxs-dcp - provide statesize and import/export() Fabio Estevam
@ 2016-01-25 14:10   ` Herbert Xu
  0 siblings, 0 replies; 9+ messages in thread
From: Herbert Xu @ 2016-01-25 14:10 UTC (permalink / raw)
  To: Fabio Estevam
  Cc: marex, s.trumtrar, thomas.lendacky, linux, linux-crypto, Fabio Estevam

On Wed, Jan 13, 2016 at 03:52:04PM -0200, Fabio Estevam wrote:
> From: Fabio Estevam <fabio.estevam@nxp.com>
> 
> Currently the mxs-dcp driver fails to probe:
> 
> mxs-dcp 80028000.dcp: Failed to register sha1 hash!
> mxs-dcp: probe of 80028000.dcp failed with error -22
> 
> This happens since commit 8996eafdcbad ("crypto: ahash - ensure statesize
> is non-zero"), which requires statesize to be filled.
> 
> Other than filling statesize, we also need to provide the import/export
> functions.
> 
> Based on the implementation of the sahara and caam drivers.
> 
> Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>

This driver is hopelessly broken as its request context doesn't
contain the hash state at all.  Unless someone can fix that we
should probably just remove the hash implementations altogether.

Cheers,,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx
  2016-01-25 14:07 ` [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx Herbert Xu
@ 2016-02-02 13:41   ` Fabio Estevam
  2016-02-02 14:43     ` Herbert Xu
  0 siblings, 1 reply; 9+ messages in thread
From: Fabio Estevam @ 2016-02-02 13:41 UTC (permalink / raw)
  To: Herbert Xu
  Cc: Marek Vašut, Steffen Trumtrar, Tom Lendacky, Russell King,
	linux-crypto, Fabio Estevam

Hi Herbert,

On Mon, Jan 25, 2016 at 12:07 PM, Herbert Xu
<herbert@gondor.apana.org.au> wrote:

> Very good.  Not only is it a waste, it's a gaping security hole
> because modifying the tfm from import will corrupt it.
>
> But this is not enough, you're still copying things like the mutex
> which should not be copied but instead should be reinitialised in
> import.

So import() will look like this?

static int sahara_sha_import(struct ahash_request *req, const void *in)
{
    struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);

    mutex_init(&rctx->mutex);
    memcpy(rctx, in, sizeof(struct sahara_sha_reqctx));

    return 0;
}

Thanks

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx
  2016-02-02 13:41   ` Fabio Estevam
@ 2016-02-02 14:43     ` Herbert Xu
  2016-02-02 17:18       ` Fabio Estevam
  0 siblings, 1 reply; 9+ messages in thread
From: Herbert Xu @ 2016-02-02 14:43 UTC (permalink / raw)
  To: Fabio Estevam
  Cc: Marek Vašut, Steffen Trumtrar, Tom Lendacky, Russell King,
	linux-crypto, Fabio Estevam

On Tue, Feb 02, 2016 at 11:41:56AM -0200, Fabio Estevam wrote:
>
> static int sahara_sha_import(struct ahash_request *req, const void *in)
> {
>     struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
> 
>     mutex_init(&rctx->mutex);
>     memcpy(rctx, in, sizeof(struct sahara_sha_reqctx));

Preferably you shouldn't include the mutex in the exported state
at all.

Cheers,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx
  2016-02-02 14:43     ` Herbert Xu
@ 2016-02-02 17:18       ` Fabio Estevam
  2016-02-03 11:22         ` Herbert Xu
  0 siblings, 1 reply; 9+ messages in thread
From: Fabio Estevam @ 2016-02-02 17:18 UTC (permalink / raw)
  To: Herbert Xu
  Cc: Marek Vašut, Steffen Trumtrar, Tom Lendacky, Russell King,
	linux-crypto, Fabio Estevam

On Tue, Feb 2, 2016 at 12:43 PM, Herbert Xu <herbert@gondor.apana.org.au> wrote:

> Preferably you shouldn't include the mutex in the exported state
> at all.

Ok, so would it be safe to completely remove the mutex like this?

--- a/drivers/crypto/sahara.c
+++ b/drivers/crypto/sahara.c
@@ -182,7 +182,6 @@ struct sahara_sha_reqctx {
        u8                      buf[SAHARA_MAX_SHA_BLOCK_SIZE];
        u8                      rembuf[SAHARA_MAX_SHA_BLOCK_SIZE];
        u8                      context[SHA256_DIGEST_SIZE + 4];
-       struct mutex            mutex;
        unsigned int            mode;
        unsigned int            digest_size;
        unsigned int            context_size;
@@ -1096,7 +1095,6 @@ static int sahara_sha_enqueue(struct ahash_request *req, i
        if (!req->nbytes && !last)
                return 0;

-       mutex_lock(&rctx->mutex);
        rctx->last = last;

        if (!rctx->active) {
@@ -1109,7 +1107,6 @@ static int sahara_sha_enqueue(struct ahash_request *req, i
        mutex_unlock(&dev->queue_mutex);

        wake_up_process(dev->kthread);
-       mutex_unlock(&rctx->mutex);

        return ret;
 }
@@ -1137,8 +1134,6 @@ static int sahara_sha_init(struct ahash_request *req)
        rctx->context_size = rctx->digest_size + 4;
        rctx->active = 0;

-       mutex_init(&rctx->mutex);
-
        return 0;
 }

Thanks

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx
  2016-02-02 17:18       ` Fabio Estevam
@ 2016-02-03 11:22         ` Herbert Xu
  0 siblings, 0 replies; 9+ messages in thread
From: Herbert Xu @ 2016-02-03 11:22 UTC (permalink / raw)
  To: Fabio Estevam
  Cc: Marek Vašut, Steffen Trumtrar, Tom Lendacky, Russell King,
	linux-crypto, Fabio Estevam

On Tue, Feb 02, 2016 at 03:18:47PM -0200, Fabio Estevam wrote:
> On Tue, Feb 2, 2016 at 12:43 PM, Herbert Xu <herbert@gondor.apana.org.au> wrote:
> 
> > Preferably you shouldn't include the mutex in the exported state
> > at all.
> 
> Ok, so would it be safe to completely remove the mutex like this?

Hmm, indeed the mutex seems to be redundant so let's just kill
it.

Thanks,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2016-02-03 11:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-13 17:52 [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx Fabio Estevam
2016-01-13 17:52 ` [PATCH v2 2/3] crypto: sahara - fill the statesize field Fabio Estevam
2016-01-13 17:52 ` [PATCH v2 3/3] crypto: mxs-dcp - provide statesize and import/export() Fabio Estevam
2016-01-25 14:10   ` Herbert Xu
2016-01-25 14:07 ` [PATCH v2 1/3] crypto: sahara - avoid needlessly saving and restoring sahara_ctx Herbert Xu
2016-02-02 13:41   ` Fabio Estevam
2016-02-02 14:43     ` Herbert Xu
2016-02-02 17:18       ` Fabio Estevam
2016-02-03 11:22         ` Herbert Xu

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.