Skip to contentSkip to conversation

MPI: Fix mpi_read_buffer

PATCH Applied Tadeusz Struk 3 participants 6 messages Aug 24, 2015 – Sep 15, 2015 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 MPI: Fix mpi_read_buffer

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

---
 lib/mpi/mpicoder.c |   38 +++++++++++++++++++++++++-------------
 1 file changed, 25 insertions(+), 13 deletions(-)
lib/mpi/mpicoder.c +25 -13
index bc0a1da..95c52a9 100644
@@ -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);
maintainer Applied
#1 lore raw
Quoted 4 lines · 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.

Applied.

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
#2 lore raw
In reply to Tadeusz Struk
Quoted 2 lines · 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.

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

#3 lore raw
Quoted 5 lines · David Howells
Quoted 2 lines

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

maintainer
#4 lore raw
In reply to David Howells
Quoted 7 lines · David Howells
Quoted 2 lines · 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.

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,

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
#5 lore raw
Quoted 6 lines · Herbert Xu
Quoted 5 lines
Quoted 5 lines

Change mpi_read_buffer to return a number without leading zeros

Quoted 1 line
Quoted 1 line

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.