Commit 3ee0cb5f authored by Michal Marek's avatar Michal Marek Committed by Herbert Xu

lib/mpi: Endianness fix

The limbs are integers in the host endianness, so we can't simply
iterate over the individual bytes. The current code happens to work on
little-endian, because the order of the limbs in the MPI array is the
same as the order of the bytes in each limb, but it breaks on
big-endian.

Fixes: 0f74fbf7 ("MPI: Fix mpi_read_buffer")
Signed-off-by: default avatarMichal Marek <mmarek@suse.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent bfd927ff
...@@ -128,6 +128,23 @@ MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread) ...@@ -128,6 +128,23 @@ MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
} }
EXPORT_SYMBOL_GPL(mpi_read_from_buffer); EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
static int count_lzeros(MPI a)
{
mpi_limb_t alimb;
int i, lzeros = 0;
for (i = a->nlimbs - 1; i >= 0; i--) {
alimb = a->d[i];
if (alimb == 0) {
lzeros += sizeof(mpi_limb_t);
} else {
lzeros += count_leading_zeros(alimb) / 8;
break;
}
}
return lzeros;
}
/** /**
* mpi_read_buffer() - read MPI to a bufer provided by user (msb first) * mpi_read_buffer() - read MPI to a bufer provided by user (msb first)
* *
...@@ -148,7 +165,7 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, ...@@ -148,7 +165,7 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
uint8_t *p; uint8_t *p;
mpi_limb_t alimb; mpi_limb_t alimb;
unsigned int n = mpi_get_size(a); unsigned int n = mpi_get_size(a);
int i, lzeros = 0; int i, lzeros;
if (!buf || !nbytes) if (!buf || !nbytes)
return -EINVAL; return -EINVAL;
...@@ -156,14 +173,7 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, ...@@ -156,14 +173,7 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
if (sign) if (sign)
*sign = a->sign; *sign = a->sign;
p = (void *)&a->d[a->nlimbs] - 1; lzeros = count_lzeros(a);
for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) {
if (!*p)
lzeros++;
else
break;
}
if (buf_len < n - lzeros) { if (buf_len < n - lzeros) {
*nbytes = n - lzeros; *nbytes = n - lzeros;
...@@ -351,7 +361,7 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, ...@@ -351,7 +361,7 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
u8 *p, *p2; u8 *p, *p2;
mpi_limb_t alimb, alimb2; mpi_limb_t alimb, alimb2;
unsigned int n = mpi_get_size(a); unsigned int n = mpi_get_size(a);
int i, x, y = 0, lzeros = 0, buf_len; int i, x, y = 0, lzeros, buf_len;
if (!nbytes) if (!nbytes)
return -EINVAL; return -EINVAL;
...@@ -359,14 +369,7 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, ...@@ -359,14 +369,7 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
if (sign) if (sign)
*sign = a->sign; *sign = a->sign;
p = (void *)&a->d[a->nlimbs] - 1; lzeros = count_lzeros(a);
for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) {
if (!*p)
lzeros++;
else
break;
}
if (*nbytes < n - lzeros) { if (*nbytes < n - lzeros) {
*nbytes = n - lzeros; *nbytes = n - lzeros;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment