diff options
author | Michal Marek <mmarek@suse.com> | 2016-02-17 08:46:59 -0500 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2016-02-27 14:26:30 -0500 |
commit | 3ee0cb5fb5eea2110db1b5cb7f67029b7be8a376 (patch) | |
tree | 1e4d55e95009a958d84441ef97025c28f060ba72 /lib/mpi/mpicoder.c | |
parent | bfd927ffa219ac81082b2dcc61a1c4037869befc (diff) |
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: 0f74fbf77d45 ("MPI: Fix mpi_read_buffer")
Signed-off-by: Michal Marek <mmarek@suse.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'lib/mpi/mpicoder.c')
-rw-r--r-- | lib/mpi/mpicoder.c | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index ec533a6c77b5..eb15e7dc7b65 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c | |||
@@ -128,6 +128,23 @@ leave: | |||
128 | } | 128 | } |
129 | EXPORT_SYMBOL_GPL(mpi_read_from_buffer); | 129 | EXPORT_SYMBOL_GPL(mpi_read_from_buffer); |
130 | 130 | ||
131 | static int count_lzeros(MPI a) | ||
132 | { | ||
133 | mpi_limb_t alimb; | ||
134 | int i, lzeros = 0; | ||
135 | |||
136 | for (i = a->nlimbs - 1; i >= 0; i--) { | ||
137 | alimb = a->d[i]; | ||
138 | if (alimb == 0) { | ||
139 | lzeros += sizeof(mpi_limb_t); | ||
140 | } else { | ||
141 | lzeros += count_leading_zeros(alimb) / 8; | ||
142 | break; | ||
143 | } | ||
144 | } | ||
145 | return lzeros; | ||
146 | } | ||
147 | |||
131 | /** | 148 | /** |
132 | * mpi_read_buffer() - read MPI to a bufer provided by user (msb first) | 149 | * mpi_read_buffer() - read MPI to a bufer provided by user (msb first) |
133 | * | 150 | * |
@@ -148,7 +165,7 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, | |||
148 | uint8_t *p; | 165 | uint8_t *p; |
149 | mpi_limb_t alimb; | 166 | mpi_limb_t alimb; |
150 | unsigned int n = mpi_get_size(a); | 167 | unsigned int n = mpi_get_size(a); |
151 | int i, lzeros = 0; | 168 | int i, lzeros; |
152 | 169 | ||
153 | if (!buf || !nbytes) | 170 | if (!buf || !nbytes) |
154 | return -EINVAL; | 171 | return -EINVAL; |
@@ -156,14 +173,7 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, | |||
156 | if (sign) | 173 | if (sign) |
157 | *sign = a->sign; | 174 | *sign = a->sign; |
158 | 175 | ||
159 | p = (void *)&a->d[a->nlimbs] - 1; | 176 | lzeros = count_lzeros(a); |
160 | |||
161 | for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) { | ||
162 | if (!*p) | ||
163 | lzeros++; | ||
164 | else | ||
165 | break; | ||
166 | } | ||
167 | 177 | ||
168 | if (buf_len < n - lzeros) { | 178 | if (buf_len < n - lzeros) { |
169 | *nbytes = n - lzeros; | 179 | *nbytes = n - lzeros; |
@@ -351,7 +361,7 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, | |||
351 | u8 *p, *p2; | 361 | u8 *p, *p2; |
352 | mpi_limb_t alimb, alimb2; | 362 | mpi_limb_t alimb, alimb2; |
353 | unsigned int n = mpi_get_size(a); | 363 | unsigned int n = mpi_get_size(a); |
354 | int i, x, y = 0, lzeros = 0, buf_len; | 364 | int i, x, y = 0, lzeros, buf_len; |
355 | 365 | ||
356 | if (!nbytes) | 366 | if (!nbytes) |
357 | return -EINVAL; | 367 | return -EINVAL; |
@@ -359,14 +369,7 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, | |||
359 | if (sign) | 369 | if (sign) |
360 | *sign = a->sign; | 370 | *sign = a->sign; |
361 | 371 | ||
362 | p = (void *)&a->d[a->nlimbs] - 1; | 372 | lzeros = count_lzeros(a); |
363 | |||
364 | for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) { | ||
365 | if (!*p) | ||
366 | lzeros++; | ||
367 | else | ||
368 | break; | ||
369 | } | ||
370 | 373 | ||
371 | if (*nbytes < n - lzeros) { | 374 | if (*nbytes < n - lzeros) { |
372 | *nbytes = n - lzeros; | 375 | *nbytes = n - lzeros; |