linux-crypto.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] crypto: atmel-tdes - Set the IV after {en,de}crypt
@ 2019-11-15 13:49 Tudor.Ambarus
  2019-11-15 13:49 ` [PATCH 2/2] crypto: atmel-aes - Change data type for "lastc" buffer Tudor.Ambarus
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Tudor.Ambarus @ 2019-11-15 13:49 UTC (permalink / raw)
  To: herbert
  Cc: Nicolas.Ferre, alexandre.belloni, Ludovic.Desroches,
	linux-crypto, linux-arm-kernel, linux-kernel, Tudor.Ambarus

From: Tudor Ambarus <tudor.ambarus@microchip.com>

The req->iv of the skcipher_request is expected to contain the
last ciphertext block when the {en,de}crypt operation is done.
In case of in-place decryption, copy the ciphertext in an
intermediate buffer before decryption.

This fixes the following tcrypt tests:
alg: skcipher: atmel-cbc-des encryption test failed (wrong output IV) on test vector 0, cfg="in-place"
00000000: fe dc ba 98 76 54 32 10
alg: skcipher: atmel-cbc-tdes encryption test failed (wrong output IV) on test vector 0, cfg="in-place"
00000000: 7d 33 88 93 0f 93 b2 42

Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com>
---
 drivers/crypto/atmel-tdes.c | 40 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

diff --git a/drivers/crypto/atmel-tdes.c b/drivers/crypto/atmel-tdes.c
index bb7c0a387c04..0c1f79b30fc1 100644
--- a/drivers/crypto/atmel-tdes.c
+++ b/drivers/crypto/atmel-tdes.c
@@ -81,6 +81,7 @@ struct atmel_tdes_ctx {
 
 struct atmel_tdes_reqctx {
 	unsigned long mode;
+	u8 lastc[DES_BLOCK_SIZE];
 };
 
 struct atmel_tdes_dma {
@@ -572,6 +573,30 @@ static int atmel_tdes_crypt_start(struct atmel_tdes_dev *dd)
 	return err;
 }
 
+static void
+atmel_tdes_set_iv_as_last_ciphertext_block(struct atmel_tdes_dev *dd)
+{
+	struct skcipher_request *req = dd->req;
+	struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(req);
+	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
+	unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
+
+	if (req->cryptlen < ivsize)
+		return;
+
+	if (rctx->mode & TDES_FLAGS_ENCRYPT) {
+		scatterwalk_map_and_copy(req->iv, req->dst,
+					 req->cryptlen - ivsize, ivsize, 0);
+	} else {
+		if (req->src == req->dst)
+			memcpy(req->iv, rctx->lastc, ivsize);
+		else
+			scatterwalk_map_and_copy(req->iv, req->src,
+						 req->cryptlen - ivsize,
+						 ivsize, 0);
+	}
+}
+
 static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err)
 {
 	struct skcipher_request *req = dd->req;
@@ -580,6 +605,8 @@ static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err)
 
 	dd->flags &= ~TDES_FLAGS_BUSY;
 
+	atmel_tdes_set_iv_as_last_ciphertext_block(dd);
+
 	req->base.complete(&req->base, err);
 }
 
@@ -668,8 +695,8 @@ static int atmel_tdes_crypt_dma_stop(struct atmel_tdes_dev *dd)
 
 static int atmel_tdes_crypt(struct skcipher_request *req, unsigned long mode)
 {
-	struct atmel_tdes_ctx *ctx = crypto_skcipher_ctx(
-			crypto_skcipher_reqtfm(req));
+	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
+	struct atmel_tdes_ctx *ctx = crypto_skcipher_ctx(skcipher);
 	struct atmel_tdes_reqctx *rctx = skcipher_request_ctx(req);
 
 	if (mode & TDES_FLAGS_CFB8) {
@@ -700,6 +727,15 @@ static int atmel_tdes_crypt(struct skcipher_request *req, unsigned long mode)
 
 	rctx->mode = mode;
 
+	if (!(mode & TDES_FLAGS_ENCRYPT) && req->src == req->dst) {
+		unsigned int ivsize = crypto_skcipher_ivsize(skcipher);
+
+		if (req->cryptlen >= ivsize)
+			scatterwalk_map_and_copy(rctx->lastc, req->src,
+						 req->cryptlen - ivsize,
+						 ivsize, 0);
+	}
+
 	return atmel_tdes_handle_queue(ctx->dd, req);
 }
 
-- 
2.9.5


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

* [PATCH 2/2] crypto: atmel-aes - Change data type for "lastc" buffer
  2019-11-15 13:49 [PATCH 1/2] crypto: atmel-tdes - Set the IV after {en,de}crypt Tudor.Ambarus
@ 2019-11-15 13:49 ` Tudor.Ambarus
  2019-11-22 11:09 ` [PATCH 1/2] crypto: atmel-tdes - Set the IV after {en,de}crypt Herbert Xu
  2019-11-27 16:56 ` Tudor.Ambarus
  2 siblings, 0 replies; 5+ messages in thread
From: Tudor.Ambarus @ 2019-11-15 13:49 UTC (permalink / raw)
  To: herbert
  Cc: Nicolas.Ferre, alexandre.belloni, Ludovic.Desroches,
	linux-crypto, linux-arm-kernel, linux-kernel, Tudor.Ambarus

From: Tudor Ambarus <tudor.ambarus@microchip.com>

In case of in-place decryption, the "lastc" buffer is used to copy
the last ciphertext block before the decryption of the message. It
is later used to update the req->iv of the skcipher_request.

"lastc" variable is not used to interact with the hardware, there
is no restriction to be of type "u32". Change the type of "lastc"
to "u8".

Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com>
---
 drivers/crypto/atmel-aes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/crypto/atmel-aes.c b/drivers/crypto/atmel-aes.c
index 3c88c164c3dc..91092504bc96 100644
--- a/drivers/crypto/atmel-aes.c
+++ b/drivers/crypto/atmel-aes.c
@@ -155,7 +155,7 @@ struct atmel_aes_authenc_ctx {
 
 struct atmel_aes_reqctx {
 	unsigned long		mode;
-	u32			lastc[AES_BLOCK_SIZE / sizeof(u32)];
+	u8			lastc[AES_BLOCK_SIZE];
 };
 
 #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
-- 
2.9.5


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

* Re: [PATCH 1/2] crypto: atmel-tdes - Set the IV after {en,de}crypt
  2019-11-15 13:49 [PATCH 1/2] crypto: atmel-tdes - Set the IV after {en,de}crypt Tudor.Ambarus
  2019-11-15 13:49 ` [PATCH 2/2] crypto: atmel-aes - Change data type for "lastc" buffer Tudor.Ambarus
@ 2019-11-22 11:09 ` Herbert Xu
  2019-11-27 16:56 ` Tudor.Ambarus
  2 siblings, 0 replies; 5+ messages in thread
From: Herbert Xu @ 2019-11-22 11:09 UTC (permalink / raw)
  To: Tudor.Ambarus
  Cc: Nicolas.Ferre, alexandre.belloni, Ludovic.Desroches,
	linux-crypto, linux-arm-kernel, linux-kernel

On Fri, Nov 15, 2019 at 01:49:06PM +0000, Tudor.Ambarus@microchip.com wrote:
> From: Tudor Ambarus <tudor.ambarus@microchip.com>
> 
> The req->iv of the skcipher_request is expected to contain the
> last ciphertext block when the {en,de}crypt operation is done.
> In case of in-place decryption, copy the ciphertext in an
> intermediate buffer before decryption.
> 
> This fixes the following tcrypt tests:
> alg: skcipher: atmel-cbc-des encryption test failed (wrong output IV) on test vector 0, cfg="in-place"
> 00000000: fe dc ba 98 76 54 32 10
> alg: skcipher: atmel-cbc-tdes encryption test failed (wrong output IV) on test vector 0, cfg="in-place"
> 00000000: 7d 33 88 93 0f 93 b2 42
> 
> Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com>
> ---
>  drivers/crypto/atmel-tdes.c | 40 ++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 38 insertions(+), 2 deletions(-)

All applied.  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] 5+ messages in thread

* Re: [PATCH 1/2] crypto: atmel-tdes - Set the IV after {en,de}crypt
  2019-11-15 13:49 [PATCH 1/2] crypto: atmel-tdes - Set the IV after {en,de}crypt Tudor.Ambarus
  2019-11-15 13:49 ` [PATCH 2/2] crypto: atmel-aes - Change data type for "lastc" buffer Tudor.Ambarus
  2019-11-22 11:09 ` [PATCH 1/2] crypto: atmel-tdes - Set the IV after {en,de}crypt Herbert Xu
@ 2019-11-27 16:56 ` Tudor.Ambarus
  2019-11-28  2:33   ` Herbert Xu
  2 siblings, 1 reply; 5+ messages in thread
From: Tudor.Ambarus @ 2019-11-27 16:56 UTC (permalink / raw)
  To: herbert
  Cc: Nicolas.Ferre, alexandre.belloni, Ludovic.Desroches,
	linux-crypto, linux-arm-kernel, linux-kernel



On 11/15/19 3:49 PM, Tudor Ambarus - M18064 wrote:
>  static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err)
>  {
>  	struct skcipher_request *req = dd->req;
> @@ -580,6 +605,8 @@ static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err)
>  
>  	dd->flags &= ~TDES_FLAGS_BUSY;
>  
> +	atmel_tdes_set_iv_as_last_ciphertext_block(dd);

ECB mode does not use an IV, I should probably exclude the update of IV for the
ECB mode. v2 will follow.

> +
>  	req->base.complete(&req->base, err);
>  }

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

* Re: [PATCH 1/2] crypto: atmel-tdes - Set the IV after {en,de}crypt
  2019-11-27 16:56 ` Tudor.Ambarus
@ 2019-11-28  2:33   ` Herbert Xu
  0 siblings, 0 replies; 5+ messages in thread
From: Herbert Xu @ 2019-11-28  2:33 UTC (permalink / raw)
  To: Tudor.Ambarus
  Cc: Nicolas.Ferre, alexandre.belloni, Ludovic.Desroches,
	linux-crypto, linux-arm-kernel, linux-kernel

On Wed, Nov 27, 2019 at 04:56:37PM +0000, Tudor.Ambarus@microchip.com wrote:
> 
> 
> On 11/15/19 3:49 PM, Tudor Ambarus - M18064 wrote:
> >  static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err)
> >  {
> >  	struct skcipher_request *req = dd->req;
> > @@ -580,6 +605,8 @@ static void atmel_tdes_finish_req(struct atmel_tdes_dev *dd, int err)
> >  
> >  	dd->flags &= ~TDES_FLAGS_BUSY;
> >  
> > +	atmel_tdes_set_iv_as_last_ciphertext_block(dd);
> 
> ECB mode does not use an IV, I should probably exclude the update of IV for the
> ECB mode. v2 will follow.

Please send an incremental patch as this one has already been
applied.

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] 5+ messages in thread

end of thread, other threads:[~2019-11-28  2:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-15 13:49 [PATCH 1/2] crypto: atmel-tdes - Set the IV after {en,de}crypt Tudor.Ambarus
2019-11-15 13:49 ` [PATCH 2/2] crypto: atmel-aes - Change data type for "lastc" buffer Tudor.Ambarus
2019-11-22 11:09 ` [PATCH 1/2] crypto: atmel-tdes - Set the IV after {en,de}crypt Herbert Xu
2019-11-27 16:56 ` Tudor.Ambarus
2019-11-28  2:33   ` Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).