All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: Nathan Huckleberry <nhuck@google.com>
Cc: linux-crypto@vger.kernel.org,
	Herbert Xu <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>,
	linux-arm-kernel@lists.infradead.org,
	Paul Crowley <paulcrowley@google.com>,
	Sami Tolvanen <samitolvanen@google.com>,
	Ard Biesheuvel <ardb@kernel.org>
Subject: Re: [PATCH v4 2/8] crypto: polyval - Add POLYVAL support
Date: Mon, 18 Apr 2022 12:25:13 -0700	[thread overview]
Message-ID: <Yl27GSvaCP6CDUmy@sol.localdomain> (raw)
In-Reply-To: <20220412172816.917723-3-nhuck@google.com>

A couple more nits:

On Tue, Apr 12, 2022 at 05:28:10PM +0000, Nathan Huckleberry wrote:

> +static int polyval_init(struct shash_desc *desc)
[...]
> +static int polyval_setkey(struct crypto_shash *tfm,

As I mentioned on the x86 patch, setkey() is the first step, before init().
People sometimes mix this up, e.g. see https://git.kernel.org/linus/f3aefb6a7066e24b.
Putting the definitions in their natural order might be helpful:

	1. polyval_setkey()
	2. polyval_init()
	3. polyval_update()
	4. polyval_final()

> +static void reverse_block(u8 block[POLYVAL_BLOCK_SIZE])
> +{
> +	u64 *p1 = (u64 *)block;
> +	u64 *p2 = (u64 *)&block[8];
> +	u64 a = get_unaligned(p1);
> +	u64 b = get_unaligned(p2);
> +
> +	put_unaligned(swab64(a), p2);
> +	put_unaligned(swab64(b), p1);
> +}

This is always paired with a memcpy() of the block, so consider making this
helper function handle the copy too.  E.g.

diff --git a/crypto/polyval-generic.c b/crypto/polyval-generic.c
index 1399af125b937..b50db5dd51fd1 100644
--- a/crypto/polyval-generic.c
+++ b/crypto/polyval-generic.c
@@ -75,15 +75,14 @@ static int polyval_init(struct shash_desc *desc)
 	return 0;
 }
 
-static void reverse_block(u8 block[POLYVAL_BLOCK_SIZE])
+static void copy_and_reverse(u8 dst[POLYVAL_BLOCK_SIZE],
+			     const u8 src[POLYVAL_BLOCK_SIZE])
 {
-	u64 *p1 = (u64 *)block;
-	u64 *p2 = (u64 *)&block[8];
-	u64 a = get_unaligned(p1);
-	u64 b = get_unaligned(p2);
+	u64 a = get_unaligned((const u64 *)&src[0]);
+	u64 b = get_unaligned((const u64 *)&src[8]);
 
-	put_unaligned(swab64(a), p2);
-	put_unaligned(swab64(b), p1);
+	put_unaligned(swab64(a), (u64 *)&dst[8]);
+	put_unaligned(swab64(b), (u64 *)&dst[0]);
 }
 
 static int polyval_setkey(struct crypto_shash *tfm,
@@ -98,10 +97,7 @@ static int polyval_setkey(struct crypto_shash *tfm,
 	gf128mul_free_4k(ctx->gf128);
 
 	BUILD_BUG_ON(sizeof(k) != POLYVAL_BLOCK_SIZE);
-	// avoid violating alignment rules
-	memcpy(&k, key, POLYVAL_BLOCK_SIZE);
-
-	reverse_block((u8 *)&k);
+	copy_and_reverse((u8 *)&k, key);
 	gf128mul_x_lle(&k, &k);
 
 	ctx->gf128 = gf128mul_init_4k_lle(&k);
@@ -137,8 +133,7 @@ static int polyval_update(struct shash_desc *desc,
 	}
 
 	while (srclen >= POLYVAL_BLOCK_SIZE) {
-		memcpy(tmp, src, POLYVAL_BLOCK_SIZE);
-		reverse_block(tmp);
+		copy_and_reverse(tmp, src);
 		crypto_xor(dctx->buffer, tmp, POLYVAL_BLOCK_SIZE);
 		gf128mul_4k_lle(&dctx->buffer128, ctx->gf128);
 		src += POLYVAL_BLOCK_SIZE;
@@ -162,11 +157,7 @@ static int polyval_final(struct shash_desc *desc, u8 *dst)
 
 	if (dctx->bytes)
 		gf128mul_4k_lle(&dctx->buffer128, ctx->gf128);
-	dctx->bytes = 0;
-
-	reverse_block(dctx->buffer);
-	memcpy(dst, dctx->buffer, POLYVAL_BLOCK_SIZE);
-
+	copy_and_reverse(dst, dctx->buffer);
 	return 0;
 }

WARNING: multiple messages have this Message-ID (diff)
From: Eric Biggers <ebiggers@kernel.org>
To: Nathan Huckleberry <nhuck@google.com>
Cc: linux-crypto@vger.kernel.org,
	Herbert Xu <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>,
	linux-arm-kernel@lists.infradead.org,
	Paul Crowley <paulcrowley@google.com>,
	Sami Tolvanen <samitolvanen@google.com>,
	Ard Biesheuvel <ardb@kernel.org>
Subject: Re: [PATCH v4 2/8] crypto: polyval - Add POLYVAL support
Date: Mon, 18 Apr 2022 12:25:13 -0700	[thread overview]
Message-ID: <Yl27GSvaCP6CDUmy@sol.localdomain> (raw)
In-Reply-To: <20220412172816.917723-3-nhuck@google.com>

A couple more nits:

On Tue, Apr 12, 2022 at 05:28:10PM +0000, Nathan Huckleberry wrote:

> +static int polyval_init(struct shash_desc *desc)
[...]
> +static int polyval_setkey(struct crypto_shash *tfm,

As I mentioned on the x86 patch, setkey() is the first step, before init().
People sometimes mix this up, e.g. see https://git.kernel.org/linus/f3aefb6a7066e24b.
Putting the definitions in their natural order might be helpful:

	1. polyval_setkey()
	2. polyval_init()
	3. polyval_update()
	4. polyval_final()

> +static void reverse_block(u8 block[POLYVAL_BLOCK_SIZE])
> +{
> +	u64 *p1 = (u64 *)block;
> +	u64 *p2 = (u64 *)&block[8];
> +	u64 a = get_unaligned(p1);
> +	u64 b = get_unaligned(p2);
> +
> +	put_unaligned(swab64(a), p2);
> +	put_unaligned(swab64(b), p1);
> +}

This is always paired with a memcpy() of the block, so consider making this
helper function handle the copy too.  E.g.

diff --git a/crypto/polyval-generic.c b/crypto/polyval-generic.c
index 1399af125b937..b50db5dd51fd1 100644
--- a/crypto/polyval-generic.c
+++ b/crypto/polyval-generic.c
@@ -75,15 +75,14 @@ static int polyval_init(struct shash_desc *desc)
 	return 0;
 }
 
-static void reverse_block(u8 block[POLYVAL_BLOCK_SIZE])
+static void copy_and_reverse(u8 dst[POLYVAL_BLOCK_SIZE],
+			     const u8 src[POLYVAL_BLOCK_SIZE])
 {
-	u64 *p1 = (u64 *)block;
-	u64 *p2 = (u64 *)&block[8];
-	u64 a = get_unaligned(p1);
-	u64 b = get_unaligned(p2);
+	u64 a = get_unaligned((const u64 *)&src[0]);
+	u64 b = get_unaligned((const u64 *)&src[8]);
 
-	put_unaligned(swab64(a), p2);
-	put_unaligned(swab64(b), p1);
+	put_unaligned(swab64(a), (u64 *)&dst[8]);
+	put_unaligned(swab64(b), (u64 *)&dst[0]);
 }
 
 static int polyval_setkey(struct crypto_shash *tfm,
@@ -98,10 +97,7 @@ static int polyval_setkey(struct crypto_shash *tfm,
 	gf128mul_free_4k(ctx->gf128);
 
 	BUILD_BUG_ON(sizeof(k) != POLYVAL_BLOCK_SIZE);
-	// avoid violating alignment rules
-	memcpy(&k, key, POLYVAL_BLOCK_SIZE);
-
-	reverse_block((u8 *)&k);
+	copy_and_reverse((u8 *)&k, key);
 	gf128mul_x_lle(&k, &k);
 
 	ctx->gf128 = gf128mul_init_4k_lle(&k);
@@ -137,8 +133,7 @@ static int polyval_update(struct shash_desc *desc,
 	}
 
 	while (srclen >= POLYVAL_BLOCK_SIZE) {
-		memcpy(tmp, src, POLYVAL_BLOCK_SIZE);
-		reverse_block(tmp);
+		copy_and_reverse(tmp, src);
 		crypto_xor(dctx->buffer, tmp, POLYVAL_BLOCK_SIZE);
 		gf128mul_4k_lle(&dctx->buffer128, ctx->gf128);
 		src += POLYVAL_BLOCK_SIZE;
@@ -162,11 +157,7 @@ static int polyval_final(struct shash_desc *desc, u8 *dst)
 
 	if (dctx->bytes)
 		gf128mul_4k_lle(&dctx->buffer128, ctx->gf128);
-	dctx->bytes = 0;
-
-	reverse_block(dctx->buffer);
-	memcpy(dst, dctx->buffer, POLYVAL_BLOCK_SIZE);
-
+	copy_and_reverse(dst, dctx->buffer);
 	return 0;
 }

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  reply	other threads:[~2022-04-18 19:25 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-12 17:28 [PATCH v4 0/8] crypto: HCTR2 support Nathan Huckleberry
2022-04-12 17:28 ` Nathan Huckleberry
2022-04-12 17:28 ` [PATCH v4 1/8] crypto: xctr - Add XCTR support Nathan Huckleberry
2022-04-12 17:28   ` Nathan Huckleberry
2022-04-18 19:03   ` Eric Biggers
2022-04-18 19:03     ` Eric Biggers
2022-04-12 17:28 ` [PATCH v4 2/8] crypto: polyval - Add POLYVAL support Nathan Huckleberry
2022-04-12 17:28   ` Nathan Huckleberry
2022-04-18 19:25   ` Eric Biggers [this message]
2022-04-18 19:25     ` Eric Biggers
2022-04-12 17:28 ` [PATCH v4 3/8] crypto: hctr2 - Add HCTR2 support Nathan Huckleberry
2022-04-12 17:28   ` Nathan Huckleberry
2022-04-13  4:20   ` Eric Biggers
2022-04-13  4:20     ` Eric Biggers
2022-04-18 20:46   ` Eric Biggers
2022-04-18 20:46     ` Eric Biggers
2022-04-12 17:28 ` [PATCH v4 4/8] crypto: x86/aesni-xctr: Add accelerated implementation of XCTR Nathan Huckleberry
2022-04-12 17:28   ` Nathan Huckleberry
2022-04-14  7:00   ` Eric Biggers
2022-04-14  7:00     ` Eric Biggers
2022-04-18 23:44   ` Eric Biggers
2022-04-18 23:44     ` Eric Biggers
2022-04-19  0:13   ` Eric Biggers
2022-04-19  0:13     ` Eric Biggers
2022-04-21 21:59     ` Nathan Huckleberry
2022-04-21 21:59       ` Nathan Huckleberry
2022-04-21 22:29       ` Eric Biggers
2022-04-21 22:29         ` Eric Biggers
2022-04-12 17:28 ` [PATCH v4 5/8] crypto: arm64/aes-xctr: " Nathan Huckleberry
2022-04-12 17:28   ` Nathan Huckleberry
2022-04-19  4:33   ` Eric Biggers
2022-04-19  4:33     ` Eric Biggers
2022-04-12 17:28 ` [PATCH v4 6/8] crypto: x86/polyval: Add PCLMULQDQ accelerated implementation of POLYVAL Nathan Huckleberry
2022-04-12 17:28   ` Nathan Huckleberry
2022-04-13  5:18   ` Eric Biggers
2022-04-13  5:18     ` Eric Biggers
2022-04-18 21:36   ` Eric Biggers
2022-04-18 21:36     ` Eric Biggers
2022-04-12 17:28 ` [PATCH v4 7/8] crypto: arm64/polyval: Add PMULL " Nathan Huckleberry
2022-04-12 17:28   ` Nathan Huckleberry
2022-04-13  5:53   ` Eric Biggers
2022-04-13  5:53     ` Eric Biggers
2022-04-18 21:06   ` kernel test robot
2022-04-12 17:28 ` [PATCH v4 8/8] fscrypt: Add HCTR2 support for filename encryption Nathan Huckleberry
2022-04-12 17:28   ` Nathan Huckleberry
2022-04-13  6:10   ` Eric Biggers
2022-04-13  6:10     ` Eric Biggers
2022-04-13  6:16     ` Ard Biesheuvel
2022-04-13  6:16       ` Ard Biesheuvel
2022-04-14  7:12       ` Eric Biggers
2022-04-14  7:12         ` Eric Biggers
2022-04-14  7:15         ` Ard Biesheuvel
2022-04-14  7:15           ` Ard Biesheuvel
2022-04-18 18:05   ` Eric Biggers
2022-04-18 18:05     ` Eric Biggers
2022-04-14 14:18 ` [PATCH v4 0/8] crypto: HCTR2 support Ard Biesheuvel
2022-04-14 14:18   ` Ard Biesheuvel

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=Yl27GSvaCP6CDUmy@sol.localdomain \
    --to=ebiggers@kernel.org \
    --cc=ardb@kernel.org \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=nhuck@google.com \
    --cc=paulcrowley@google.com \
    --cc=samitolvanen@google.com \
    /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 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.