linux-crypto.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] MPI: Fix mpi_read_buffer
@ 2015-08-24 14:52 Tadeusz Struk
  2015-08-25 13:17 ` Herbert Xu
  2015-09-15  9:09 ` David Howells
  0 siblings, 2 replies; 6+ messages in thread
From: Tadeusz Struk @ 2015-08-24 14:52 UTC (permalink / raw)
  To: herbert; +Cc: dhowells, linux-crypto, tadeusz.struk

Change mpi_read_buffer to return a number without leading zeros
so that mpi_read_buffer and mpi_get_buffer return the same thing.

Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
---
 lib/mpi/mpicoder.c |   38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)

diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c
index bc0a1da..95c52a9 100644
--- a/lib/mpi/mpicoder.c
+++ b/lib/mpi/mpicoder.c
@@ -146,18 +146,25 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
 	uint8_t *p;
 	mpi_limb_t alimb;
 	unsigned int n = mpi_get_size(a);
-	int i;
+	int i, lzeros = 0;
 
-	if (buf_len < n || !buf)
+	if (buf_len < n || !buf || !nbytes)
 		return -EINVAL;
 
 	if (sign)
 		*sign = a->sign;
 
-	if (nbytes)
-		*nbytes = n;
+	p = (void *)&a->d[a->nlimbs] - 1;
+
+	for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) {
+		if (!*p)
+			lzeros++;
+		else
+			break;
+	}
 
 	p = buf;
+	*nbytes = n - lzeros;
 
 	for (i = a->nlimbs - 1; i >= 0; i--) {
 		alimb = a->d[i];
@@ -178,6 +185,19 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
 #else
 #error please implement for this limb size.
 #endif
+
+		if (lzeros > 0) {
+			if (lzeros >= sizeof(alimb)) {
+				p -= sizeof(alimb);
+			} else {
+				mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
+				mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
+							+ lzeros;
+				*limb1 = *limb2;
+				p -= lzeros;
+			}
+			lzeros -= sizeof(alimb);
+		}
 	}
 	return 0;
 }
@@ -197,7 +217,7 @@ EXPORT_SYMBOL_GPL(mpi_read_buffer);
  */
 void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
 {
-	uint8_t *buf, *p;
+	uint8_t *buf;
 	unsigned int n;
 	int ret;
 
@@ -220,14 +240,6 @@ void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
 		kfree(buf);
 		return NULL;
 	}
-
-	/* this is sub-optimal but we need to do the shift operation
-	 * because the caller has to free the returned buffer */
-	for (p = buf; !*p && *nbytes; p++, --*nbytes)
-		;
-	if (p != buf)
-		memmove(buf, p, *nbytes);
-
 	return buf;
 }
 EXPORT_SYMBOL_GPL(mpi_get_buffer);

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

* Re: [PATCH] MPI: Fix mpi_read_buffer
  2015-08-24 14:52 [PATCH] MPI: Fix mpi_read_buffer Tadeusz Struk
@ 2015-08-25 13:17 ` Herbert Xu
  2015-09-15  9:09 ` David Howells
  1 sibling, 0 replies; 6+ messages in thread
From: Herbert Xu @ 2015-08-25 13:17 UTC (permalink / raw)
  To: Tadeusz Struk; +Cc: dhowells, linux-crypto

On Mon, Aug 24, 2015 at 07:52:14AM -0700, Tadeusz Struk wrote:
> Change mpi_read_buffer to return a number without leading zeros
> so that mpi_read_buffer and mpi_get_buffer return the same thing.
> 
> Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>

Applied.
-- 
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] 6+ messages in thread

* Re: [PATCH] MPI: Fix mpi_read_buffer
  2015-08-24 14:52 [PATCH] MPI: Fix mpi_read_buffer Tadeusz Struk
  2015-08-25 13:17 ` Herbert Xu
@ 2015-09-15  9:09 ` David Howells
  2015-09-15 11:05   ` David Howells
  2015-09-15 12:24   ` Herbert Xu
  1 sibling, 2 replies; 6+ messages in thread
From: David Howells @ 2015-09-15  9:09 UTC (permalink / raw)
  To: Tadeusz Struk; +Cc: dhowells, herbert, linux-crypto

Tadeusz Struk <tadeusz.struk@intel.com> wrote:

> Change mpi_read_buffer to return a number without leading zeros
> so that mpi_read_buffer and mpi_get_buffer return the same thing.

Hmmm...  This would appear to have the undesirable side effect of causing 1
out of 256 module signing keys to be unusable (ie. those that begin with 00).

David

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

* Re: [PATCH] MPI: Fix mpi_read_buffer
  2015-09-15  9:09 ` David Howells
@ 2015-09-15 11:05   ` David Howells
  2015-09-15 12:24   ` Herbert Xu
  1 sibling, 0 replies; 6+ messages in thread
From: David Howells @ 2015-09-15 11:05 UTC (permalink / raw)
  Cc: dhowells, Tadeusz Struk, herbert, linux-crypto

David Howells <dhowells@redhat.com> wrote:

> > Change mpi_read_buffer to return a number without leading zeros
> > so that mpi_read_buffer and mpi_get_buffer return the same thing.
> 
> Hmmm...  This would appear to have the undesirable side effect of causing 1
> out of 256 module signing keys to be unusable (ie. those that begin with 00).

At least, I think it's this, but it's not entirely clear since it's not been
reproduced yet.

David

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

* Re: [PATCH] MPI: Fix mpi_read_buffer
  2015-09-15  9:09 ` David Howells
  2015-09-15 11:05   ` David Howells
@ 2015-09-15 12:24   ` Herbert Xu
  2015-09-15 15:56     ` Tadeusz Struk
  1 sibling, 1 reply; 6+ messages in thread
From: Herbert Xu @ 2015-09-15 12:24 UTC (permalink / raw)
  To: David Howells; +Cc: Tadeusz Struk, linux-crypto

On Tue, Sep 15, 2015 at 10:09:56AM +0100, David Howells wrote:
> Tadeusz Struk <tadeusz.struk@intel.com> wrote:
> 
> > Change mpi_read_buffer to return a number without leading zeros
> > so that mpi_read_buffer and mpi_get_buffer return the same thing.
> 
> Hmmm...  This would appear to have the undesirable side effect of causing 1
> out of 256 module signing keys to be unusable (ie. those that begin with 00).

I thought Tadeusz was just restoring the original behaviour, no?

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

* Re: [PATCH] MPI: Fix mpi_read_buffer
  2015-09-15 12:24   ` Herbert Xu
@ 2015-09-15 15:56     ` Tadeusz Struk
  0 siblings, 0 replies; 6+ messages in thread
From: Tadeusz Struk @ 2015-09-15 15:56 UTC (permalink / raw)
  To: Herbert Xu, David Howells; +Cc: linux-crypto

On 09/15/2015 05:24 AM, Herbert Xu wrote:
>>> Change mpi_read_buffer to return a number without leading zeros
>>> > > so that mpi_read_buffer and mpi_get_buffer return the same thing.
>> > 
>> > Hmmm...  This would appear to have the undesirable side effect of causing 1
>> > out of 256 module signing keys to be unusable (ie. those that begin with 00).
> I thought Tadeusz was just restoring the original behaviour, no?

That was the intention.
David, I can ran some more test if you have the key that was failing and
be willing to share it.

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

end of thread, other threads:[~2015-09-15 15:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-24 14:52 [PATCH] MPI: Fix mpi_read_buffer Tadeusz Struk
2015-08-25 13:17 ` Herbert Xu
2015-09-15  9:09 ` David Howells
2015-09-15 11:05   ` David Howells
2015-09-15 12:24   ` Herbert Xu
2015-09-15 15:56     ` Tadeusz Struk

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).