]>
Commit | Line | Data |
---|---|---|
cdec9cb5 DK |
1 | /* mpicoder.c - Coder for the external representation of MPIs |
2 | * Copyright (C) 1998, 1999 Free Software Foundation, Inc. | |
3 | * | |
4 | * This file is part of GnuPG. | |
5 | * | |
6 | * GnuPG is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * GnuPG is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | |
19 | */ | |
20 | ||
e1045992 | 21 | #include <linux/bitops.h> |
a1164a3a | 22 | #include <linux/count_zeros.h> |
d7552906 | 23 | #include <linux/byteorder/generic.h> |
cdec9cb5 DK |
24 | #include "mpi-internal.h" |
25 | ||
cdec9cb5 DK |
26 | #define MAX_EXTERN_MPI_BITS 16384 |
27 | ||
e1045992 DH |
28 | /** |
29 | * mpi_read_raw_data - Read a raw byte stream as a positive integer | |
30 | * @xbuffer: The data to read | |
31 | * @nbytes: The amount of data to read | |
32 | */ | |
33 | MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes) | |
34 | { | |
35 | const uint8_t *buffer = xbuffer; | |
36 | int i, j; | |
37 | unsigned nbits, nlimbs; | |
38 | mpi_limb_t a; | |
39 | MPI val = NULL; | |
40 | ||
5402b804 | 41 | while (nbytes > 0 && buffer[0] == 0) { |
e1045992 DH |
42 | buffer++; |
43 | nbytes--; | |
44 | } | |
45 | ||
46 | nbits = nbytes * 8; | |
47 | if (nbits > MAX_EXTERN_MPI_BITS) { | |
48 | pr_info("MPI: mpi too large (%u bits)\n", nbits); | |
49 | return NULL; | |
50 | } | |
51 | if (nbytes > 0) | |
52 | nbits -= count_leading_zeros(buffer[0]); | |
53 | else | |
54 | nbits = 0; | |
55 | ||
0d2a1b2d | 56 | nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB); |
e1045992 DH |
57 | val = mpi_alloc(nlimbs); |
58 | if (!val) | |
59 | return NULL; | |
60 | val->nbits = nbits; | |
61 | val->sign = 0; | |
62 | val->nlimbs = nlimbs; | |
63 | ||
64 | if (nbytes > 0) { | |
65 | i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; | |
66 | i %= BYTES_PER_MPI_LIMB; | |
67 | for (j = nlimbs; j > 0; j--) { | |
68 | a = 0; | |
69 | for (; i < BYTES_PER_MPI_LIMB; i++) { | |
70 | a <<= 8; | |
71 | a |= *buffer++; | |
72 | } | |
73 | i = 0; | |
74 | val->d[j - 1] = a; | |
75 | } | |
76 | } | |
77 | return val; | |
78 | } | |
79 | EXPORT_SYMBOL_GPL(mpi_read_raw_data); | |
80 | ||
cdec9cb5 DK |
81 | MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread) |
82 | { | |
83 | const uint8_t *buffer = xbuffer; | |
84 | int i, j; | |
85 | unsigned nbits, nbytes, nlimbs, nread = 0; | |
86 | mpi_limb_t a; | |
3cccd154 | 87 | MPI val = NULL; |
cdec9cb5 DK |
88 | |
89 | if (*ret_nread < 2) | |
90 | goto leave; | |
91 | nbits = buffer[0] << 8 | buffer[1]; | |
92 | ||
93 | if (nbits > MAX_EXTERN_MPI_BITS) { | |
94 | pr_info("MPI: mpi too large (%u bits)\n", nbits); | |
95 | goto leave; | |
96 | } | |
97 | buffer += 2; | |
98 | nread = 2; | |
99 | ||
0d2a1b2d AS |
100 | nbytes = DIV_ROUND_UP(nbits, 8); |
101 | nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB); | |
cdec9cb5 DK |
102 | val = mpi_alloc(nlimbs); |
103 | if (!val) | |
3cccd154 | 104 | return NULL; |
cdec9cb5 DK |
105 | i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; |
106 | i %= BYTES_PER_MPI_LIMB; | |
107 | val->nbits = nbits; | |
108 | j = val->nlimbs = nlimbs; | |
109 | val->sign = 0; | |
110 | for (; j > 0; j--) { | |
111 | a = 0; | |
112 | for (; i < BYTES_PER_MPI_LIMB; i++) { | |
113 | if (++nread > *ret_nread) { | |
114 | printk | |
115 | ("MPI: mpi larger than buffer nread=%d ret_nread=%d\n", | |
116 | nread, *ret_nread); | |
117 | goto leave; | |
118 | } | |
119 | a <<= 8; | |
120 | a |= *buffer++; | |
121 | } | |
122 | i = 0; | |
123 | val->d[j - 1] = a; | |
124 | } | |
125 | ||
126 | leave: | |
127 | *ret_nread = nread; | |
128 | return val; | |
129 | } | |
130 | EXPORT_SYMBOL_GPL(mpi_read_from_buffer); | |
131 | ||
3ee0cb5f MM |
132 | static int count_lzeros(MPI a) |
133 | { | |
134 | mpi_limb_t alimb; | |
135 | int i, lzeros = 0; | |
136 | ||
137 | for (i = a->nlimbs - 1; i >= 0; i--) { | |
138 | alimb = a->d[i]; | |
139 | if (alimb == 0) { | |
140 | lzeros += sizeof(mpi_limb_t); | |
141 | } else { | |
142 | lzeros += count_leading_zeros(alimb) / 8; | |
143 | break; | |
144 | } | |
145 | } | |
146 | return lzeros; | |
147 | } | |
148 | ||
d37e2969 TS |
149 | /** |
150 | * mpi_read_buffer() - read MPI to a bufer provided by user (msb first) | |
151 | * | |
152 | * @a: a multi precision integer | |
153 | * @buf: bufer to which the output will be written to. Needs to be at | |
154 | * leaset mpi_get_size(a) long. | |
155 | * @buf_len: size of the buf. | |
9cbe21d8 AZ |
156 | * @nbytes: receives the actual length of the data written on success and |
157 | * the data to-be-written on -EOVERFLOW in case buf_len was too | |
158 | * small. | |
d37e2969 TS |
159 | * @sign: if not NULL, it will be set to the sign of a. |
160 | * | |
161 | * Return: 0 on success or error code in case of error | |
cdec9cb5 | 162 | */ |
d37e2969 TS |
163 | int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, |
164 | int *sign) | |
cdec9cb5 | 165 | { |
d37e2969 | 166 | uint8_t *p; |
cdec9cb5 | 167 | mpi_limb_t alimb; |
d37e2969 | 168 | unsigned int n = mpi_get_size(a); |
3ee0cb5f | 169 | int i, lzeros; |
d37e2969 | 170 | |
9cbe21d8 | 171 | if (!buf || !nbytes) |
d37e2969 | 172 | return -EINVAL; |
cdec9cb5 DK |
173 | |
174 | if (sign) | |
175 | *sign = a->sign; | |
d37e2969 | 176 | |
3ee0cb5f | 177 | lzeros = count_lzeros(a); |
d37e2969 | 178 | |
9cbe21d8 AZ |
179 | if (buf_len < n - lzeros) { |
180 | *nbytes = n - lzeros; | |
181 | return -EOVERFLOW; | |
182 | } | |
183 | ||
d37e2969 | 184 | p = buf; |
0f74fbf7 | 185 | *nbytes = n - lzeros; |
cdec9cb5 | 186 | |
f00fa241 NS |
187 | for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB, |
188 | lzeros %= BYTES_PER_MPI_LIMB; | |
189 | i >= 0; i--) { | |
cdec9cb5 DK |
190 | alimb = a->d[i]; |
191 | #if BYTES_PER_MPI_LIMB == 4 | |
192 | *p++ = alimb >> 24; | |
193 | *p++ = alimb >> 16; | |
194 | *p++ = alimb >> 8; | |
195 | *p++ = alimb; | |
196 | #elif BYTES_PER_MPI_LIMB == 8 | |
197 | *p++ = alimb >> 56; | |
198 | *p++ = alimb >> 48; | |
199 | *p++ = alimb >> 40; | |
200 | *p++ = alimb >> 32; | |
201 | *p++ = alimb >> 24; | |
202 | *p++ = alimb >> 16; | |
203 | *p++ = alimb >> 8; | |
204 | *p++ = alimb; | |
205 | #else | |
206 | #error please implement for this limb size. | |
207 | #endif | |
0f74fbf7 TS |
208 | |
209 | if (lzeros > 0) { | |
f00fa241 NS |
210 | mpi_limb_t *limb1 = (void *)p - sizeof(alimb); |
211 | mpi_limb_t *limb2 = (void *)p - sizeof(alimb) | |
212 | + lzeros; | |
213 | *limb1 = *limb2; | |
214 | p -= lzeros; | |
0f74fbf7 TS |
215 | lzeros -= sizeof(alimb); |
216 | } | |
cdec9cb5 | 217 | } |
d37e2969 TS |
218 | return 0; |
219 | } | |
220 | EXPORT_SYMBOL_GPL(mpi_read_buffer); | |
221 | ||
222 | /* | |
223 | * mpi_get_buffer() - Returns an allocated buffer with the MPI (msb first). | |
224 | * Caller must free the return string. | |
225 | * This function does return a 0 byte buffer with nbytes set to zero if the | |
226 | * value of A is zero. | |
227 | * | |
228 | * @a: a multi precision integer. | |
229 | * @nbytes: receives the length of this buffer. | |
230 | * @sign: if not NULL, it will be set to the sign of the a. | |
231 | * | |
232 | * Return: Pointer to MPI buffer or NULL on error | |
233 | */ | |
234 | void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign) | |
235 | { | |
0f74fbf7 | 236 | uint8_t *buf; |
d37e2969 TS |
237 | unsigned int n; |
238 | int ret; | |
239 | ||
240 | if (!nbytes) | |
241 | return NULL; | |
242 | ||
243 | n = mpi_get_size(a); | |
244 | ||
245 | if (!n) | |
246 | n++; | |
247 | ||
248 | buf = kmalloc(n, GFP_KERNEL); | |
249 | ||
250 | if (!buf) | |
251 | return NULL; | |
252 | ||
253 | ret = mpi_read_buffer(a, buf, n, nbytes, sign); | |
254 | ||
255 | if (ret) { | |
256 | kfree(buf); | |
257 | return NULL; | |
258 | } | |
d37e2969 | 259 | return buf; |
cdec9cb5 DK |
260 | } |
261 | EXPORT_SYMBOL_GPL(mpi_get_buffer); | |
262 | ||
263 | /**************** | |
264 | * Use BUFFER to update MPI. | |
265 | */ | |
266 | int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign) | |
267 | { | |
268 | const uint8_t *buffer = xbuffer, *p; | |
269 | mpi_limb_t alimb; | |
270 | int nlimbs; | |
271 | int i; | |
272 | ||
0d2a1b2d | 273 | nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB); |
cdec9cb5 DK |
274 | if (RESIZE_IF_NEEDED(a, nlimbs) < 0) |
275 | return -ENOMEM; | |
276 | a->sign = sign; | |
277 | ||
278 | for (i = 0, p = buffer + nbytes - 1; p >= buffer + BYTES_PER_MPI_LIMB;) { | |
279 | #if BYTES_PER_MPI_LIMB == 4 | |
280 | alimb = (mpi_limb_t) *p--; | |
281 | alimb |= (mpi_limb_t) *p-- << 8; | |
282 | alimb |= (mpi_limb_t) *p-- << 16; | |
283 | alimb |= (mpi_limb_t) *p-- << 24; | |
284 | #elif BYTES_PER_MPI_LIMB == 8 | |
285 | alimb = (mpi_limb_t) *p--; | |
286 | alimb |= (mpi_limb_t) *p-- << 8; | |
287 | alimb |= (mpi_limb_t) *p-- << 16; | |
288 | alimb |= (mpi_limb_t) *p-- << 24; | |
289 | alimb |= (mpi_limb_t) *p-- << 32; | |
290 | alimb |= (mpi_limb_t) *p-- << 40; | |
291 | alimb |= (mpi_limb_t) *p-- << 48; | |
292 | alimb |= (mpi_limb_t) *p-- << 56; | |
293 | #else | |
294 | #error please implement for this limb size. | |
295 | #endif | |
296 | a->d[i++] = alimb; | |
297 | } | |
298 | if (p >= buffer) { | |
299 | #if BYTES_PER_MPI_LIMB == 4 | |
300 | alimb = *p--; | |
301 | if (p >= buffer) | |
302 | alimb |= (mpi_limb_t) *p-- << 8; | |
303 | if (p >= buffer) | |
304 | alimb |= (mpi_limb_t) *p-- << 16; | |
305 | if (p >= buffer) | |
306 | alimb |= (mpi_limb_t) *p-- << 24; | |
307 | #elif BYTES_PER_MPI_LIMB == 8 | |
308 | alimb = (mpi_limb_t) *p--; | |
309 | if (p >= buffer) | |
310 | alimb |= (mpi_limb_t) *p-- << 8; | |
311 | if (p >= buffer) | |
312 | alimb |= (mpi_limb_t) *p-- << 16; | |
313 | if (p >= buffer) | |
314 | alimb |= (mpi_limb_t) *p-- << 24; | |
315 | if (p >= buffer) | |
316 | alimb |= (mpi_limb_t) *p-- << 32; | |
317 | if (p >= buffer) | |
318 | alimb |= (mpi_limb_t) *p-- << 40; | |
319 | if (p >= buffer) | |
320 | alimb |= (mpi_limb_t) *p-- << 48; | |
321 | if (p >= buffer) | |
322 | alimb |= (mpi_limb_t) *p-- << 56; | |
323 | #else | |
324 | #error please implement for this limb size. | |
325 | #endif | |
326 | a->d[i++] = alimb; | |
327 | } | |
328 | a->nlimbs = i; | |
329 | ||
330 | if (i != nlimbs) { | |
331 | pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i, | |
332 | nlimbs); | |
333 | BUG(); | |
334 | } | |
335 | return 0; | |
336 | } | |
337 | EXPORT_SYMBOL_GPL(mpi_set_buffer); | |
2d4d1eea TS |
338 | |
339 | /** | |
340 | * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first) | |
341 | * | |
342 | * This function works in the same way as the mpi_read_buffer, but it | |
343 | * takes an sgl instead of u8 * buf. | |
344 | * | |
345 | * @a: a multi precision integer | |
346 | * @sgl: scatterlist to write to. Needs to be at least | |
347 | * mpi_get_size(a) long. | |
348 | * @nbytes: in/out param - it has the be set to the maximum number of | |
349 | * bytes that can be written to sgl. This has to be at least | |
350 | * the size of the integer a. On return it receives the actual | |
9cbe21d8 AZ |
351 | * length of the data written on success or the data that would |
352 | * be written if buffer was too small. | |
2d4d1eea TS |
353 | * @sign: if not NULL, it will be set to the sign of a. |
354 | * | |
355 | * Return: 0 on success or error code in case of error | |
356 | */ | |
357 | int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, | |
358 | int *sign) | |
359 | { | |
360 | u8 *p, *p2; | |
d7552906 NS |
361 | #if BYTES_PER_MPI_LIMB == 4 |
362 | __be32 alimb; | |
363 | #elif BYTES_PER_MPI_LIMB == 8 | |
364 | __be64 alimb; | |
365 | #else | |
366 | #error please implement for this limb size. | |
367 | #endif | |
2d4d1eea | 368 | unsigned int n = mpi_get_size(a); |
3ee0cb5f | 369 | int i, x, y = 0, lzeros, buf_len; |
2d4d1eea | 370 | |
9cbe21d8 | 371 | if (!nbytes) |
2d4d1eea TS |
372 | return -EINVAL; |
373 | ||
374 | if (sign) | |
375 | *sign = a->sign; | |
376 | ||
3ee0cb5f | 377 | lzeros = count_lzeros(a); |
2d4d1eea | 378 | |
9cbe21d8 AZ |
379 | if (*nbytes < n - lzeros) { |
380 | *nbytes = n - lzeros; | |
381 | return -EOVERFLOW; | |
382 | } | |
383 | ||
2d4d1eea TS |
384 | *nbytes = n - lzeros; |
385 | buf_len = sgl->length; | |
386 | p2 = sg_virt(sgl); | |
387 | ||
f2d1362f NS |
388 | for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB, |
389 | lzeros %= BYTES_PER_MPI_LIMB; | |
390 | i >= 0; i--) { | |
2d4d1eea | 391 | #if BYTES_PER_MPI_LIMB == 4 |
d7552906 | 392 | alimb = cpu_to_be32(a->d[i]); |
2d4d1eea | 393 | #elif BYTES_PER_MPI_LIMB == 8 |
d7552906 | 394 | alimb = cpu_to_be64(a->d[i]); |
2d4d1eea TS |
395 | #else |
396 | #error please implement for this limb size. | |
397 | #endif | |
654842ef | 398 | if (lzeros) { |
f2d1362f | 399 | y = lzeros; |
654842ef | 400 | lzeros = 0; |
2d4d1eea TS |
401 | } |
402 | ||
d7552906 | 403 | p = (u8 *)&alimb + y; |
2d4d1eea TS |
404 | |
405 | for (x = 0; x < sizeof(alimb) - y; x++) { | |
406 | if (!buf_len) { | |
407 | sgl = sg_next(sgl); | |
408 | if (!sgl) | |
409 | return -EINVAL; | |
410 | buf_len = sgl->length; | |
411 | p2 = sg_virt(sgl); | |
412 | } | |
413 | *p2++ = *p++; | |
414 | buf_len--; | |
415 | } | |
416 | y = 0; | |
417 | } | |
418 | return 0; | |
419 | } | |
420 | EXPORT_SYMBOL_GPL(mpi_write_to_sgl); | |
421 | ||
422 | /* | |
423 | * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with | |
424 | * data from the sgl | |
425 | * | |
426 | * This function works in the same way as the mpi_read_raw_data, but it | |
427 | * takes an sgl instead of void * buffer. i.e. it allocates | |
428 | * a new MPI and reads the content of the sgl to the MPI. | |
429 | * | |
430 | * @sgl: scatterlist to read from | |
431 | * @len: number of bytes to read | |
432 | * | |
433 | * Return: Pointer to a new MPI or NULL on error | |
434 | */ | |
435 | MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len) | |
436 | { | |
437 | struct scatterlist *sg; | |
438 | int x, i, j, z, lzeros, ents; | |
439 | unsigned int nbits, nlimbs, nbytes; | |
440 | mpi_limb_t a; | |
441 | MPI val = NULL; | |
442 | ||
443 | lzeros = 0; | |
444 | ents = sg_nents(sgl); | |
445 | ||
446 | for_each_sg(sgl, sg, ents, i) { | |
447 | const u8 *buff = sg_virt(sg); | |
448 | int len = sg->length; | |
449 | ||
63349d02 | 450 | while (len && !*buff) { |
2d4d1eea | 451 | lzeros++; |
63349d02 SM |
452 | len--; |
453 | buff++; | |
454 | } | |
2d4d1eea TS |
455 | |
456 | if (len && *buff) | |
457 | break; | |
458 | ||
459 | ents--; | |
460 | lzeros = 0; | |
461 | } | |
462 | ||
463 | sgl = sg; | |
464 | ||
465 | if (!ents) | |
466 | nbytes = 0; | |
467 | else | |
468 | nbytes = len - lzeros; | |
469 | ||
470 | nbits = nbytes * 8; | |
471 | if (nbits > MAX_EXTERN_MPI_BITS) { | |
472 | pr_info("MPI: mpi too large (%u bits)\n", nbits); | |
473 | return NULL; | |
474 | } | |
475 | ||
476 | if (nbytes > 0) | |
477 | nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros)); | |
478 | else | |
479 | nbits = 0; | |
480 | ||
481 | nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB); | |
482 | val = mpi_alloc(nlimbs); | |
483 | if (!val) | |
484 | return NULL; | |
485 | ||
486 | val->nbits = nbits; | |
487 | val->sign = 0; | |
488 | val->nlimbs = nlimbs; | |
489 | ||
490 | if (nbytes == 0) | |
491 | return val; | |
492 | ||
493 | j = nlimbs - 1; | |
494 | a = 0; | |
495 | z = 0; | |
496 | x = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; | |
497 | x %= BYTES_PER_MPI_LIMB; | |
498 | ||
499 | for_each_sg(sgl, sg, ents, i) { | |
500 | const u8 *buffer = sg_virt(sg) + lzeros; | |
501 | int len = sg->length - lzeros; | |
502 | int buf_shift = x; | |
503 | ||
504 | if (sg_is_last(sg) && (len % BYTES_PER_MPI_LIMB)) | |
505 | len += BYTES_PER_MPI_LIMB - (len % BYTES_PER_MPI_LIMB); | |
506 | ||
507 | for (; x < len + buf_shift; x++) { | |
508 | a <<= 8; | |
509 | a |= *buffer++; | |
510 | if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) { | |
511 | val->d[j--] = a; | |
512 | a = 0; | |
513 | } | |
514 | } | |
515 | z += x; | |
516 | x = 0; | |
517 | lzeros = 0; | |
518 | } | |
519 | return val; | |
520 | } | |
521 | EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl); |