aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/mpi/mpicoder.c38
1 files changed, 25 insertions, 13 deletions
diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c
index bc0a1da8afba..95c52a95259e 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,
146 uint8_t *p; 146 uint8_t *p;
147 mpi_limb_t alimb; 147 mpi_limb_t alimb;
148 unsigned int n = mpi_get_size(a); 148 unsigned int n = mpi_get_size(a);
149 int i; 149 int i, lzeros = 0;
150 150
151 if (buf_len < n || !buf) 151 if (buf_len < n || !buf || !nbytes)
152 return -EINVAL; 152 return -EINVAL;
153 153
154 if (sign) 154 if (sign)
155 *sign = a->sign; 155 *sign = a->sign;
156 156
157 if (nbytes) 157 p = (void *)&a->d[a->nlimbs] - 1;
158 *nbytes = n; 158
159 for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) {
160 if (!*p)
161 lzeros++;
162 else
163 break;
164 }
159 165
160 p = buf; 166 p = buf;
167 *nbytes = n - lzeros;
161 168
162 for (i = a->nlimbs - 1; i >= 0; i--) { 169 for (i = a->nlimbs - 1; i >= 0; i--) {
163 alimb = a->d[i]; 170 alimb = a->d[i];
@@ -178,6 +185,19 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
178#else 185#else
179#error please implement for this limb size. 186#error please implement for this limb size.
180#endif 187#endif
188
189 if (lzeros > 0) {
190 if (lzeros >= sizeof(alimb)) {
191 p -= sizeof(alimb);
192 } else {
193 mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
194 mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
195 + lzeros;
196 *limb1 = *limb2;
197 p -= lzeros;
198 }
199 lzeros -= sizeof(alimb);
200 }
181 } 201 }
182 return 0; 202 return 0;
183} 203}
@@ -197,7 +217,7 @@ EXPORT_SYMBOL_GPL(mpi_read_buffer);
197 */ 217 */
198void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign) 218void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
199{ 219{
200 uint8_t *buf, *p; 220 uint8_t *buf;
201 unsigned int n; 221 unsigned int n;
202 int ret; 222 int ret;
203 223
@@ -220,14 +240,6 @@ void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
220 kfree(buf); 240 kfree(buf);
221 return NULL; 241 return NULL;
222 } 242 }
223
224 /* this is sub-optimal but we need to do the shift operation
225 * because the caller has to free the returned buffer */
226 for (p = buf; !*p && *nbytes; p++, --*nbytes)
227 ;
228 if (p != buf)
229 memmove(buf, p, *nbytes);
230
231 return buf; 243 return buf;
232} 244}
233EXPORT_SYMBOL_GPL(mpi_get_buffer); 245EXPORT_SYMBOL_GPL(mpi_get_buffer);