Skip to contentSkip to conversation

crypto: sahara - avoid needlessly saving and restoring sahara_ctx

PATCH In review Fabio Estevam 2 participants 9 messages Jan 13, 2016 – Feb 3, 2016

Subsystems (2)
lore
Share & tools
lore

Fabio Estevam

author reviewer
#0 lore raw

PATCH v2 1/3 crypto: sahara - avoid needlessly saving and restoring sahara_ctx

From: Fabio Estevam <[email protected]>

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.

---
Changes since v1:
- Newly introduced in this series

 drivers/crypto/sahara.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
drivers/crypto/sahara.c +2 -10
index f68c24a..53c7a9a 100644
@@ -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;
 }
Signature · 1 line
1.9.1
author reviewer
#1 lore raw

PATCH v2 2/3 crypto: sahara - fill the statesize field

From: Fabio Estevam <[email protected]>

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.

---
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(+)
drivers/crypto/sahara.c +2 -0
index 53c7a9a..239f31a 100644
@@ -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",
Signature · 1 line
1.9.1
author reviewer
#2 lore raw

PATCH v2 3/3 crypto: mxs-dcp - provide statesize and import/export()

In reply to Fabio Estevam

From: Fabio Estevam <[email protected]>

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.

---
Changes since v2:
- Newly introduced in this series

 drivers/crypto/mxs-dcp.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
drivers/crypto/mxs-dcp.c +24 -0
index 59ed54e..1e2017f 100644
@@ -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",
Signature · 1 line
1.9.1
maintainer
#4 lore raw
In reply to Fabio Estevam
Quoted 5 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

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

Quoted 8 lines

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.

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,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#3 lore raw

Re: [PATCH v2 3/3] crypto: mxs-dcp - provide statesize and import/export()

In reply to Fabio Estevam
Quoted 10 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

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.

Quoted 6 lines

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

Based on the implementation of the sahara and caam drivers.

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,,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author reviewer
#5 lore raw
In reply to Herbert Xu

Hi Herbert,

Quoted 6 lines · Herbert Xu

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

maintainer
#6 lore raw
Quoted 7 lines · Fabio Estevam

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,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author reviewer
#7 lore raw
Quoted 2 lines · Herbert Xu

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?

drivers/crypto/sahara.c +0 -5
@@ -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

maintainer
#8 lore raw
Quoted 6 lines · Fabio Estevam
Quoted 2 lines · Herbert Xu

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,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt