Skip to contentSkip to conversation

crypto: atmel-sha: fix registration issue and other bugs

PATCH In review Cyrille Pitchen 3 participants 11 messages Jan 15, 2016 – Jan 25, 2016

Subsystems (2)
lore
Share & tools
lore

Cyrille Pitchen

author Applied
#0 lore raw

cover crypto: atmel-sha: fix registration issue and other bugs

Hi all,

This series of patches fixes many issues such as the algo registration failure or the broken support of context switches.

This series was applied to linux-next and tested on a sama5d2 xplained ultra board. We now pass the tcrypt tests in the following modes:
- 2: sha1
- 6: sha256
- 11: sha384
- 12: sha512
- 33: sha224

The context switch fix was tested with a userspace program using the cryptodev module. This single thread program computes the SHA256 hashes of many files by splitting then into fixed size chunks. The chunks of each file are processed by calling 'update' operations using a round robin algorithm.

However, the .import() / .export() implementation was NOT tested! Nonetheless the last patch is needed to fix the registration issue, otherwise atmel_sha_probe() would still fail.

Best regards,

Cyrille

Cyrille Pitchen (5):
crypto: atmel-sha: fix crash when computing digest on empty message
crypto: atmel-sha: fix a race between the 'done' tasklet and the
crypto client
crypto: atmel-sha: add support of sama5d2x SoCs
crypto: atmel-sha: fix context switches
crypto: atmel-sha: fix algorihtm registration

 drivers/crypto/atmel-sha-regs.h |   4 +
 drivers/crypto/atmel-sha.c      | 186 +++++++++++++++++++++++++++++++++-------
 2 files changed, 158 insertions(+), 32 deletions(-)
Signature · 1 line
1.8.2.2
#1 lore raw

PATCH 1/5 crypto: atmel-sha: fix crash when computing digest on empty message

This patch fixes a crash which occured during the computation of the digest of an empty message.

Indeed, when processing an empty message, the atmel_sha_handle_queue() function was never called, hence the dd->req pointer remained uninitialized.

Later, when the atmel_sha_final_req() function was called, it used to crash while using this uninitialized dd->req pointer.

Hence this patch adds missing initializations of dd->req before calls of the atmel_sha_final_req() function.

This bug prevented us from passing the tcrypt test suite on SHA algo.

---
 drivers/crypto/atmel-sha.c | 1 +
 1 file changed, 1 insertion(+)
drivers/crypto/atmel-sha.c +1 -0
index 20de861aa0ea..006b2aefba30 100644
@@ -939,6 +939,7 @@ static int atmel_sha_final(struct ahash_request *req)
 		if (err)
 			goto err1;
 
+		dd->req = req;
 		dd->flags |= SHA_FLAGS_BUSY;
 		err = atmel_sha_final_req(dd);
 	} else {
Signature · 1 line
1.8.2.2
#2 lore raw

PATCH 2/5 crypto: atmel-sha: fix a race between the 'done' tasklet and the crypto client

In reply to Cyrille Pitchen

The 'done' tasklet handler used to check the 'BUSY' flag to either finalize the processing of a crypto request which had just completed or manage the crypto queue to start the next crypto request.

On request R1 completion, the driver calls atmel_sha_finish_req(), which:
1 - clears the 'BUSY' flag since the hardware is no longer used and is
ready again to process new crypto requests.
2 - notifies the above layer (the client) about the completion of the
asynchronous crypto request R1 by calling its base.complete() callback.
3 - schedules the 'done' task to check the crypto queue and start to
process the next crypto request (the 'BUSY' flag is supposed to be cleared at that moment) if such a pending request exists.

However step 2 might wake the client up so it can now ask our driver to process a new crypto request R2. This request is enqueued by calling the atmel_sha_handle_queue() function, which sets the 'BUSY' flags then starts to process R2.

If the 'done' tasklet, scheduled by step 3, runs just after, it would see that the 'BUSY' flag is set then understand that R2 has just completed, which is wrong!

So the state of 'BUSY' flag is not a proper way to detect and handle crypto request completion.

This patch fixes this race condition by using two different tasklets, one to handle the crypto request completion events, the other to manage the crypto queue if needed.

---
 drivers/crypto/atmel-sha.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/crypto/atmel-sha.c +13 -6
index 006b2aefba30..75a8cbcf3121 100644
@@ -122,6 +122,7 @@ struct atmel_sha_dev {
 	spinlock_t		lock;
 	int			err;
 	struct tasklet_struct	done_task;
+	struct tasklet_struct	queue_task;
 
 	unsigned long		flags;
 	struct crypto_queue	queue;
@@ -788,7 +789,7 @@ static void atmel_sha_finish_req(struct ahash_request *req, int err)
 		req->base.complete(&req->base, err);
 
 	/* handle new request */
-	tasklet_schedule(&dd->done_task);
+	tasklet_schedule(&dd->queue_task);
 }
 
 static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
@@ -1101,16 +1102,18 @@ static struct ahash_alg sha_384_512_algs[] = {
 },
 };
 
+static void atmel_sha_queue_task(unsigned long data)
+{
+	struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
+
+	atmel_sha_handle_queue(dd, NULL);
+}
+
 static void atmel_sha_done_task(unsigned long data)
 {
 	struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
 	int err = 0;
 
-	if (!(SHA_FLAGS_BUSY & dd->flags)) {
-		atmel_sha_handle_queue(dd, NULL);
-		return;
-	}
-
 	if (SHA_FLAGS_CPU & dd->flags) {
 		if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
 			dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
@@ -1367,6 +1370,8 @@ static int atmel_sha_probe(struct platform_device *pdev)
 
 	tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
 					(unsigned long)sha_dd);
+	tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
+					(unsigned long)sha_dd);
 
 	crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
 
@@ -1459,6 +1464,7 @@ err_algs:
 		atmel_sha_dma_cleanup(sha_dd);
 err_sha_dma:
 res_err:
+	tasklet_kill(&sha_dd->queue_task);
 	tasklet_kill(&sha_dd->done_task);
 sha_dd_err:
 	dev_err(dev, "initialization failed.\n");
@@ -1479,6 +1485,7 @@ static int atmel_sha_remove(struct platform_device *pdev)
 
 	atmel_sha_unregister_algs(sha_dd);
 
+	tasklet_kill(&sha_dd->queue_task);
 	tasklet_kill(&sha_dd->done_task);
 
 	if (sha_dd->caps.has_dma)
Signature · 1 line
1.8.2.2
#3 lore raw

PATCH 3/5 crypto: atmel-sha: add support of sama5d2x SoCs

In reply to Cyrille Pitchen

This patch adds support of hardware version 5.1.x embedded inside sama5d2x SoCs.

---
 drivers/crypto/atmel-sha.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/crypto/atmel-sha.c +6 -0
index 75a8cbcf3121..7327e11e0ccb 100644
@@ -1279,6 +1279,12 @@ static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
 
 	/* keep only major version number */
 	switch (dd->hw_version & 0xff0) {
+	case 0x510:
+		dd->caps.has_dma = 1;
+		dd->caps.has_dualbuff = 1;
+		dd->caps.has_sha224 = 1;
+		dd->caps.has_sha_384_512 = 1;
+		break;
 	case 0x420:
 		dd->caps.has_dma = 1;
 		dd->caps.has_dualbuff = 1;
Signature · 1 line
1.8.2.2
#4 lore raw

PATCH 4/5 crypto: atmel-sha: fix context switches

In reply to Cyrille Pitchen

This patch saves the value of the internal hash register at the end of an 'update' operation then restores this value before starting the next 'update'. This way the driver can now properly handle context switches.

WARNING: only hardware versions from sama5d4x and later provide the needed interface to update the internal hash value. Especially, sama5d3x cannot implement this feature so context switches are still broken.

---
 drivers/crypto/atmel-sha-regs.h |   4 ++
 drivers/crypto/atmel-sha.c      | 105 ++++++++++++++++++++++++++++++----------
 2 files changed, 84 insertions(+), 25 deletions(-)
drivers/crypto/atmel-sha-regs.h +4 -0
index 83b2d7425666..e08897109cab 100644
@@ -8,6 +8,8 @@
 #define SHA_CR_START			(1 << 0)
 #define SHA_CR_FIRST			(1 << 4)
 #define SHA_CR_SWRST			(1 << 8)
+#define SHA_CR_WUIHV			(1 << 12)
+#define SHA_CR_WUIEHV			(1 << 13)
 
 #define SHA_MR				0x04
 #define SHA_MR_MODE_MASK		(0x3 << 0)
@@ -15,6 +17,8 @@
 #define SHA_MR_MODE_AUTO		0x1
 #define SHA_MR_MODE_PDC			0x2
 #define SHA_MR_PROCDLY			(1 << 4)
+#define SHA_MR_UIHV			(1 << 5)
+#define SHA_MR_UIEHV			(1 << 6)
 #define SHA_MR_ALGO_SHA1		(0 << 8)
 #define SHA_MR_ALGO_SHA256		(1 << 8)
 #define SHA_MR_ALGO_SHA384		(2 << 8)
drivers/crypto/atmel-sha.c +80 -25

168 lines not on this page · raw message

Signature · 1 line
1.8.2.2
#5 lore raw

PATCH 5/5 crypto: atmel-sha: fix algorihtm registration

In reply to Cyrille Pitchen

This patch implements the missing .import() and .export() mandatory hooks for asynchronous hash algorithms. It also sets the relevant, non zero, value for the .statesize field when declaring the supported SHA algorithms. Indeed a zero value of .statesize prevents the algorithm from being registered.

---
 drivers/crypto/atmel-sha.c | 55 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 54 insertions(+), 1 deletion(-)
drivers/crypto/atmel-sha.c +54 -1
index da4c3055784f..ad6a0df47ccd 100644
@@ -66,7 +66,7 @@
 #define SHA_OP_UPDATE	1
 #define SHA_OP_FINAL	2
 
-#define SHA_BUFFER_LEN		PAGE_SIZE
+#define SHA_BUFFER_LEN		(PAGE_SIZE / 16)
 
 #define ATMEL_SHA_DMA_THRESHOLD		56
 
@@ -80,6 +80,17 @@ struct atmel_sha_caps {
 
 struct atmel_sha_dev;
 
+/*
+ * .statesize = sizeof(struct atmel_sha_state) must be <= PAGE_SIZE / 8 as
+ * tested by the ahash_prepare_alg() function.
+ */
+struct atmel_sha_state {
+	u8	digest[SHA512_DIGEST_SIZE];
+	u8	buffer[SHA_BUFFER_LEN];
+	u64	digcnt[2];
+	size_t	bufcnt;
+};
+
 struct atmel_sha_reqctx {
 	struct atmel_sha_dev	*dd;
 	unsigned long	flags;
@@ -1033,6 +1044,33 @@ static int atmel_sha_digest(struct ahash_request *req)
 	return atmel_sha_init(req) ?: atmel_sha_finup(req);
 }
 
+
+static int atmel_sha_export(struct ahash_request *req, void *out)
+{
+	const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
+	struct atmel_sha_state *state = out;
+
+	memcpy(state->digest, ctx->digest, SHA512_DIGEST_SIZE);
+	memcpy(state->buffer, ctx->buffer, ctx->bufcnt);
+	state->bufcnt = ctx->bufcnt;
+	state->digcnt[0] = ctx->digcnt[0];
+	state->digcnt[1] = ctx->digcnt[1];
+	return 0;
+}
+
+static int atmel_sha_import(struct ahash_request *req, const void *in)
+{
+	struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
+	const struct atmel_sha_state *state = in;
+
+	memcpy(ctx->digest, state->digest, SHA512_DIGEST_SIZE);
+	memcpy(ctx->buffer, state->buffer, state->bufcnt);
+	ctx->bufcnt = state->bufcnt;
+	ctx->digcnt[0] = state->digcnt[0];
+	ctx->digcnt[1] = state->digcnt[1];
+	return 0;
+}
+
 static int atmel_sha_cra_init(struct crypto_tfm *tfm)
 {
 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
@@ -1049,8 +1087,11 @@ static struct ahash_alg sha_1_256_algs[] = {
 	.final		= atmel_sha_final,
 	.finup		= atmel_sha_finup,
 	.digest		= atmel_sha_digest,
+	.export		= atmel_sha_export,
+	.import		= atmel_sha_import,
 	.halg = {
 		.digestsize	= SHA1_DIGEST_SIZE,
+		.statesize	= sizeof(struct atmel_sha_state),
 		.base	= {
 			.cra_name		= "sha1",
 			.cra_driver_name	= "atmel-sha1",
@@ -1070,8 +1111,11 @@ static struct ahash_alg sha_1_256_algs[] = {
 	.final		= atmel_sha_final,
 	.finup		= atmel_sha_finup,
 	.digest		= atmel_sha_digest,
+	.export		= atmel_sha_export,
+	.import		= atmel_sha_import,
 	.halg = {
 		.digestsize	= SHA256_DIGEST_SIZE,
+		.statesize	= sizeof(struct atmel_sha_state),
 		.base	= {
 			.cra_name		= "sha256",
 			.cra_driver_name	= "atmel-sha256",
@@ -1093,8 +1137,11 @@ static struct ahash_alg sha_224_alg = {
 	.final		= atmel_sha_final,
 	.finup		= atmel_sha_finup,
 	.digest		= atmel_sha_digest,
+	.export		= atmel_sha_export,
+	.import		= atmel_sha_import,
 	.halg = {
 		.digestsize	= SHA224_DIGEST_SIZE,
+		.statesize	= sizeof(struct atmel_sha_state),
 		.base	= {
 			.cra_name		= "sha224",
 			.cra_driver_name	= "atmel-sha224",
@@ -1116,8 +1163,11 @@ static struct ahash_alg sha_384_512_algs[] = {
 	.final		= atmel_sha_final,
 	.finup		= atmel_sha_finup,
 	.digest		= atmel_sha_digest,
+	.export		= atmel_sha_export,
+	.import		= atmel_sha_import,
 	.halg = {
 		.digestsize	= SHA384_DIGEST_SIZE,
+		.statesize	= sizeof(struct atmel_sha_state),
 		.base	= {
 			.cra_name		= "sha384",
 			.cra_driver_name	= "atmel-sha384",
@@ -1137,8 +1187,11 @@ static struct ahash_alg sha_384_512_algs[] = {
 	.final		= atmel_sha_final,
 	.finup		= atmel_sha_finup,
 	.digest		= atmel_sha_digest,
+	.export		= atmel_sha_export,
+	.import		= atmel_sha_import,
 	.halg = {
 		.digestsize	= SHA512_DIGEST_SIZE,
+		.statesize	= sizeof(struct atmel_sha_state),
 		.base	= {
 			.cra_name		= "sha512",
 			.cra_driver_name	= "atmel-sha512",
Signature · 1 line
1.8.2.2
Acked-by on the v1 series
#7 lore raw
In reply to Cyrille Pitchen

Le 15/01/2016 15:49, Cyrille Pitchen a ?crit :

Quoted 4 lines

Hi all,

This series of patches fixes many issues such as the algo registration failure or the broken support of context switches.

I think it's eligible as a "fixes" series for 4.5... Herbert, is the whole series can be queued as fixes, in your opinion?

And, on the whole series:

Quoted 34 lines

This series was applied to linux-next and tested on a sama5d2 xplained ultra board. We now pass the tcrypt tests in the following modes:

(quoted diff) +0 -5
-  2: sha1
-  6: sha256
- 11: sha384
- 12: sha512
- 33: sha224

The context switch fix was tested with a userspace program using the cryptodev module. This single thread program computes the SHA256 hashes of many files by splitting then into fixed size chunks. The chunks of each file are processed by calling 'update' operations using a round robin algorithm.

However, the .import() / .export() implementation was NOT tested! Nonetheless the last patch is needed to fix the registration issue, otherwise atmel_sha_probe() would still fail.

Best regards,

Cyrille

Cyrille Pitchen (5):
crypto: atmel-sha: fix crash when computing digest on empty message
crypto: atmel-sha: fix a race between the 'done' tasklet and the
crypto client
crypto: atmel-sha: add support of sama5d2x SoCs
crypto: atmel-sha: fix context switches
crypto: atmel-sha: fix algorihtm registration

 drivers/crypto/atmel-sha-regs.h |   4 +
 drivers/crypto/atmel-sha.c      | 186 +++++++++++++++++++++++++++++++++-------
 2 files changed, 158 insertions(+), 32 deletions(-)
Signature · 1 line
Nicolas Ferre
maintainer
#6 lore raw

Re: [PATCH 5/5] crypto: atmel-sha: fix algorihtm registration

In reply to Cyrille Pitchen

drivers/crypto/atmel-sha.c static int atmel_sha_digest(struct ahash_request *req) · line 1059

Quoted 13 lines · Cyrille Pitchen
drivers/crypto/atmel-sha.c +12 -0
+static int atmel_sha_export(struct ahash_request *req, void *out)
+{
+	const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
+	struct atmel_sha_state *state = out;
+
+	memcpy(state->digest, ctx->digest, SHA512_DIGEST_SIZE);
+	memcpy(state->buffer, ctx->buffer, ctx->bufcnt);
+	state->bufcnt = ctx->bufcnt;
+	state->digcnt[0] = ctx->digcnt[0];
+	state->digcnt[1] = ctx->digcnt[1];
+	return 0;
+}

Hmm, you're assuming that out is aligned but that is not necessarily the case. Ditto for import.

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
maintainer
#8 lore raw
In reply to Nicolas Ferre
Quoted 8 lines · Nicolas Ferre

Le 15/01/2016 15:49, Cyrille Pitchen a ?crit :

Quoted 4 lines

Hi all,

This series of patches fixes many issues such as the algo registration failure or the broken support of context switches.

I think it's eligible as a "fixes" series for 4.5... Herbert, is the whole series can be queued as fixes, in your opinion?

Sorry, but I'd prefer to postpone this til the next cycle as it came in too close to the cut-off mark. This code is directly accessible by unprivileged users so I'd like it to cook for a bit longer.

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
#9 lore raw
In reply to Cyrille Pitchen
Quoted 13 lines · Cyrille Pitchen

Hi all,

This series of patches fixes many issues such as the algo registration failure or the broken support of context switches.

This series was applied to linux-next and tested on a sama5d2 xplained ultra board. We now pass the tcrypt tests in the following modes:

(quoted diff) +0 -5
-  2: sha1
-  6: sha256
- 11: sha384
- 12: sha512
- 33: sha224
Quoted 8 lines

The context switch fix was tested with a userspace program using the cryptodev module. This single thread program computes the SHA256 hashes of many files by splitting then into fixed size chunks. The chunks of each file are processed by calling 'update' operations using a round robin algorithm.

However, the .import() / .export() implementation was NOT tested! Nonetheless the last patch is needed to fix the registration issue, otherwise atmel_sha_probe() would still fail.

Patch 1-4 applied. Please fix the alignment issue with patch 5.

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
#10 lore raw

Hi Herbert,

Le 25/01/2016 15:48, Herbert Xu a ?crit :

Quoted 19 lines · Cyrille Pitchen
Quoted 18 lines

Hi all,

This series of patches fixes many issues such as the algo registration failure or the broken support of context switches.

This series was applied to linux-next and tested on a sama5d2 xplained ultra board. We now pass the tcrypt tests in the following modes:

(quoted diff) +0 -5
-  2: sha1
-  6: sha256
- 11: sha384
- 12: sha512
- 33: sha224

The context switch fix was tested with a userspace program using the cryptodev module. This single thread program computes the SHA256 hashes of many files by splitting then into fixed size chunks. The chunks of each file are processed by calling 'update' operations using a round robin algorithm.

Quoted 6 lines
Quoted 3 lines

However, the .import() / .export() implementation was NOT tested! Nonetheless the last patch is needed to fix the registration issue, otherwise atmel_sha_probe() would still fail.

Patch 1-4 applied. Please fix the alignment issue with patch 5.

OK, I will fix it soon!

Best regards,

Cyrille