Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Patrick McHardy <kaber@trash.net>
To: Herbert Xu <herbert@gondor.apana.org.au>
Cc: linux-crypto@vger.kernel.org, Evgeniy Polyakov <johnpol@2ka.mipt.ru>
Subject: Re: [HIFN 05/n]: Fix data alignment checks
Date: Wed, 07 May 2008 14:51:52 +0200	[thread overview]
Message-ID: <4821A5E8.2050102@trash.net> (raw)
In-Reply-To: <20080507124828.GA26672@gondor.apana.org.au>

[-- Attachment #1: Type: text/plain, Size: 338 bytes --]

Herbert Xu wrote:
> On Wed, May 07, 2008 at 02:26:30PM +0200, Patrick McHardy wrote:
>   
>> I'm not entirely sure about the alignmask change at the end of
>> this patch, is an alignmask of 1 correct if no source buffer
>>     
>
> If no alignment is required you want 0, 1 means 2-byte aligned.
>   

Thanks, fixed in the patch below.



[-- Attachment #2: 05.diff --]
[-- Type: text/x-diff, Size: 4576 bytes --]

commit de0a3b3bdf5a4d91ff77547dfad96dcbfd5a989f
Author: Patrick McHardy <kaber@trash.net>
Date:   Wed May 7 13:14:50 2008 +0200

    [HIFN]: Fix data alignment checks
    
    The check for misalignment of the scatterlist data has two bugs:
    
    - the source buffer doesn't need to be aligned at all
    - the destination buffer and its size needs to be aligned to a multiple
      of 4, not to the crypto alg blocksize
    
    Introduce symbolic constant for destination buffer alignment requirements,
    use it instead of the crypto alg blocksize and remove the unnecessary
    checks for source buffer alignment and change cra_alignmask to zero.
    
    Signed-off-by: Patrick McHardy <kaber@trash.net>

diff --git a/drivers/crypto/hifn_795x.c b/drivers/crypto/hifn_795x.c
index 4e89cd8..4428e8e 100644
--- a/drivers/crypto/hifn_795x.c
+++ b/drivers/crypto/hifn_795x.c
@@ -369,6 +369,8 @@ static atomic_t hifn_dev_number;
 #define	HIFN_D_DST_RSIZE		80*4
 #define	HIFN_D_RES_RSIZE		24*4
 
+#define HIFN_D_DST_DALIGN		4
+
 #define HIFN_QUEUE_LENGTH		HIFN_D_CMD_RSIZE-5
 
 #define AES_MIN_KEY_SIZE		16
@@ -1458,10 +1460,6 @@ static int ablkcipher_add(void *daddr, unsigned int *drestp, struct scatterlist
 static int ablkcipher_walk(struct ablkcipher_request *req,
 		struct ablkcipher_walk *w)
 {
-	unsigned blocksize =
-		crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(req));
-	unsigned alignmask =
-		crypto_ablkcipher_alignmask(crypto_ablkcipher_reqtfm(req));
 	struct scatterlist *src, *dst, *t;
 	void *daddr;
 	unsigned int nbytes = req->nbytes, offset, copy, diff;
@@ -1477,15 +1475,13 @@ static int ablkcipher_walk(struct ablkcipher_request *req,
 		dst = &req->dst[idx];
 
 		dprintk("\n%s: slen: %u, dlen: %u, soff: %u, doff: %u, offset: %u, "
-				"blocksize: %u, nbytes: %u.\n",
+				"nbytes: %u.\n",
 				__func__, src->length, dst->length, src->offset,
-				dst->offset, offset, blocksize, nbytes);
+				dst->offset, offset, nbytes);
 
-		if (src->length & (blocksize - 1) ||
-				src->offset & (alignmask - 1) ||
-				dst->length & (blocksize - 1) ||
-				dst->offset & (alignmask - 1) ||
-				offset) {
+		if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
+		    !IS_ALIGNED(dst->length, HIFN_D_DST_DALIGN) ||
+		    offset) {
 			unsigned slen = src->length - offset;
 			unsigned dlen = PAGE_SIZE;
 
@@ -1498,8 +1494,8 @@ static int ablkcipher_walk(struct ablkcipher_request *req,
 
 			idx += err;
 
-			copy = slen & ~(blocksize - 1);
-			diff = slen & (blocksize - 1);
+			copy = slen & ~(HIFN_D_DST_DALIGN - 1);
+			diff = slen & (HIFN_D_DST_DALIGN - 1);
 
 			if (dlen < nbytes) {
 				/*
@@ -1507,7 +1503,7 @@ static int ablkcipher_walk(struct ablkcipher_request *req,
 				 * to put there additional blocksized chunk,
 				 * so we mark that page as containing only
 				 * blocksize aligned chunks:
-				 * 	t->length = (slen & ~(blocksize - 1));
+				 * 	t->length = (slen & ~(HIFN_D_DST_DALIGN - 1));
 				 * and increase number of bytes to be processed
 				 * in next chunk:
 				 * 	nbytes += diff;
@@ -1567,10 +1563,6 @@ static int hifn_setup_session(struct ablkcipher_request *req)
 	unsigned int nbytes = req->nbytes, idx = 0, len;
 	int err = -EINVAL, sg_num;
 	struct scatterlist *src, *dst, *t;
-	unsigned blocksize =
-		crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(req));
-	unsigned alignmask =
-		crypto_ablkcipher_alignmask(crypto_ablkcipher_reqtfm(req));
 
 	if (ctx->iv && !ctx->ivsize && ctx->mode != ACRYPTO_MODE_ECB)
 		goto err_out_exit;
@@ -1578,17 +1570,13 @@ static int hifn_setup_session(struct ablkcipher_request *req)
 	ctx->walk.flags = 0;
 
 	while (nbytes) {
-		src = &req->src[idx];
 		dst = &req->dst[idx];
 
-		if (src->length & (blocksize - 1) ||
-				src->offset & (alignmask - 1) ||
-				dst->length & (blocksize - 1) ||
-				dst->offset & (alignmask - 1)) {
+		if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
+		    !IS_ALIGNED(dst->length, HIFN_D_DST_DALIGN))
 			ctx->walk.flags |= ASYNC_FLAGS_MISALIGNED;
-		}
 
-		nbytes -= src->length;
+		nbytes -= dst->length;
 		idx++;
 	}
 
@@ -2523,9 +2511,7 @@ static int hifn_alg_alloc(struct hifn_device *dev, struct hifn_alg_template *t)
 	alg->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
 	alg->alg.cra_blocksize = t->bsize;
 	alg->alg.cra_ctxsize = sizeof(struct hifn_context);
-	alg->alg.cra_alignmask = 15;
-	if (t->bsize == 8)
-		alg->alg.cra_alignmask = 3;
+	alg->alg.cra_alignmask = 0;
 	alg->alg.cra_type = &crypto_ablkcipher_type;
 	alg->alg.cra_module = THIS_MODULE;
 	alg->alg.cra_u.ablkcipher = t->ablkcipher;

  reply	other threads:[~2008-05-07 12:52 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-07 12:14 [HIFN 01/n]: Endianess fixes Patrick McHardy
2008-05-07 12:15 ` [HIFN 02/n]: Remove printk_ratelimit() for debugging printk Patrick McHardy
2008-05-07 12:23   ` Evgeniy Polyakov
2008-05-07 12:19 ` [HIFN 03/n]: Indicate asynchronous processing to crypto API Patrick McHardy
2008-05-07 12:23   ` Evgeniy Polyakov
2008-05-07 12:29     ` Patrick McHardy
2008-05-07 12:20 ` [HIFN 04/n]: Handle ablkcipher_walk errors Patrick McHardy
2008-05-07 12:44   ` Evgeniy Polyakov
2008-05-07 12:26 ` [HIFN 05/n]: Fix data alignment checks Patrick McHardy
2008-05-07 12:42   ` Evgeniy Polyakov
2008-05-07 12:45     ` Patrick McHardy
2008-05-07 13:04       ` Evgeniy Polyakov
2008-05-07 13:05         ` Patrick McHardy
2008-05-07 13:22           ` Evgeniy Polyakov
2008-05-07 12:48   ` Herbert Xu
2008-05-07 12:51     ` Patrick McHardy [this message]
2008-05-07 12:30 ` [HIFN 06/n]: Properly handle requests for less than the full scatterlist Patrick McHardy
2008-05-07 13:01   ` Evgeniy Polyakov
2008-05-07 12:32 ` [HIFN 07/n]: Use unique driver names for different algos Patrick McHardy
2008-05-07 13:07   ` Evgeniy Polyakov
2008-05-07 12:36 ` [HIFN 08/n]: Properly initialize ivsize for CBC modes Patrick McHardy
2008-05-07 12:38 ` [HIFN 09/n]: Fix max queue length value Patrick McHardy
2008-05-07 12:46   ` Evgeniy Polyakov
2008-05-07 12:50 ` [HIFN 10/n]: Move command descriptor setup to seperate function Patrick McHardy
2008-05-07 13:11   ` Evgeniy Polyakov
2008-05-07 12:53 ` [HIFN 11/n]: Have HW invalidate src and dest descriptors after processing Patrick McHardy
2008-05-07 13:14   ` Evgeniy Polyakov
2008-05-07 13:18     ` Patrick McHardy
2008-05-07 13:00 ` [HIFN 01/n]: Endianess fixes Evgeniy Polyakov
2008-05-07 13:01   ` Patrick McHardy
2008-05-07 13:23     ` Evgeniy Polyakov
2008-05-07 13:46       ` Patrick McHardy
2008-05-07 14:04         ` Evgeniy Polyakov
2008-05-07 14:38 ` Herbert Xu

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=4821A5E8.2050102@trash.net \
    --to=kaber@trash.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=johnpol@2ka.mipt.ru \
    --cc=linux-crypto@vger.kernel.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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox