]>
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> |
90f864e2 | 24 | #include <linux/string.h> |
cdec9cb5 DK |
25 | #include "mpi-internal.h" |
26 | ||
cdec9cb5 DK |
27 | #define MAX_EXTERN_MPI_BITS 16384 |
28 | ||
e1045992 DH |
29 | /** |
30 | * mpi_read_raw_data - Read a raw byte stream as a positive integer | |
31 | * @xbuffer: The data to read | |
32 | * @nbytes: The amount of data to read | |
33 | */ | |
34 | MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes) | |
35 | { | |
36 | const uint8_t *buffer = xbuffer; | |
37 | int i, j; | |
38 | unsigned nbits, nlimbs; | |
39 | mpi_limb_t a; | |
40 | MPI val = NULL; | |
41 | ||
5402b804 | 42 | while (nbytes > 0 && buffer[0] == 0) { |
e1045992 DH |
43 | buffer++; |
44 | nbytes--; | |
45 | } | |
46 | ||
47 | nbits = nbytes * 8; | |
48 | if (nbits > MAX_EXTERN_MPI_BITS) { | |
49 | pr_info("MPI: mpi too large (%u bits)\n", nbits); | |
50 | return NULL; | |
51 | } | |
52 | if (nbytes > 0) | |
53 | nbits -= count_leading_zeros(buffer[0]); | |
54 | else | |
55 | nbits = 0; | |
56 | ||
0d2a1b2d | 57 | nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB); |
e1045992 DH |
58 | val = mpi_alloc(nlimbs); |
59 | if (!val) | |
60 | return NULL; | |
61 | val->nbits = nbits; | |
62 | val->sign = 0; | |
63 | val->nlimbs = nlimbs; | |
64 | ||
65 | if (nbytes > 0) { | |
66 | i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; | |
67 | i %= BYTES_PER_MPI_LIMB; | |
68 | for (j = nlimbs; j > 0; j--) { | |
69 | a = 0; | |
70 | for (; i < BYTES_PER_MPI_LIMB; i++) { | |
71 | a <<= 8; | |
72 | a |= *buffer++; | |
73 | } | |
74 | i = 0; | |
75 | val->d[j - 1] = a; | |
76 | } | |
77 | } | |
78 | return val; | |
79 | } | |
80 | EXPORT_SYMBOL_GPL(mpi_read_raw_data); | |
81 | ||
cdec9cb5 DK |
82 | MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread) |
83 | { | |
84 | const uint8_t *buffer = xbuffer; | |
85 | int i, j; | |
86 | unsigned nbits, nbytes, nlimbs, nread = 0; | |
87 | mpi_limb_t a; | |
3cccd154 | 88 | MPI val = NULL; |
cdec9cb5 DK |
89 | |
90 | if (*ret_nread < 2) | |
91 | goto leave; | |
92 | nbits = buffer[0] << 8 | buffer[1]; | |
93 | ||
94 | if (nbits > MAX_EXTERN_MPI_BITS) { | |
95 | pr_info("MPI: mpi too large (%u bits)\n", nbits); | |
96 | goto leave; | |
97 | } | |
98 | buffer += 2; | |
99 | nread = 2; | |
100 | ||
0d2a1b2d AS |
101 | nbytes = DIV_ROUND_UP(nbits, 8); |
102 | nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB); | |
cdec9cb5 DK |
103 | val = mpi_alloc(nlimbs); |
104 | if (!val) | |
3cccd154 | 105 | return NULL; |
cdec9cb5 DK |
106 | i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; |
107 | i %= BYTES_PER_MPI_LIMB; | |
108 | val->nbits = nbits; | |
109 | j = val->nlimbs = nlimbs; | |
110 | val->sign = 0; | |
111 | for (; j > 0; j--) { | |
112 | a = 0; | |
113 | for (; i < BYTES_PER_MPI_LIMB; i++) { | |
114 | if (++nread > *ret_nread) { | |
115 | printk | |
116 | ("MPI: mpi larger than buffer nread=%d ret_nread=%d\n", | |
117 | nread, *ret_nread); | |
118 | goto leave; | |
119 | } | |
120 | a <<= 8; | |
121 | a |= *buffer++; | |
122 | } | |
123 | i = 0; | |
124 | val->d[j - 1] = a; | |
125 | } | |
126 | ||
127 | leave: | |
128 | *ret_nread = nread; | |
129 | return val; | |
130 | } | |
131 | EXPORT_SYMBOL_GPL(mpi_read_from_buffer); | |
132 | ||
3ee0cb5f MM |
133 | static int count_lzeros(MPI a) |
134 | { | |
135 | mpi_limb_t alimb; | |
136 | int i, lzeros = 0; | |
137 | ||
138 | for (i = a->nlimbs - 1; i >= 0; i--) { | |
139 | alimb = a->d[i]; | |
140 | if (alimb == 0) { | |
141 | lzeros += sizeof(mpi_limb_t); | |
142 | } else { | |
143 | lzeros += count_leading_zeros(alimb) / 8; | |
144 | break; | |
145 | } | |
146 | } | |
147 | return lzeros; | |
148 | } | |
149 | ||
d37e2969 TS |
150 | /** |
151 | * mpi_read_buffer() - read MPI to a bufer provided by user (msb first) | |
152 | * | |
153 | * @a: a multi precision integer | |
154 | * @buf: bufer to which the output will be written to. Needs to be at | |
155 | * leaset mpi_get_size(a) long. | |
156 | * @buf_len: size of the buf. | |
9cbe21d8 AZ |
157 | * @nbytes: receives the actual length of the data written on success and |
158 | * the data to-be-written on -EOVERFLOW in case buf_len was too | |
159 | * small. | |
d37e2969 TS |
160 | * @sign: if not NULL, it will be set to the sign of a. |
161 | * | |
162 | * Return: 0 on success or error code in case of error | |
cdec9cb5 | 163 | */ |
d37e2969 TS |
164 | int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, |
165 | int *sign) | |
cdec9cb5 | 166 | { |
d37e2969 | 167 | uint8_t *p; |
90f864e2 NS |
168 | #if BYTES_PER_MPI_LIMB == 4 |
169 | __be32 alimb; | |
170 | #elif BYTES_PER_MPI_LIMB == 8 | |
171 | __be64 alimb; | |
172 | #else | |
173 | #error please implement for this limb size. | |
174 | #endif | |
d37e2969 | 175 | unsigned int n = mpi_get_size(a); |
3ee0cb5f | 176 | int i, lzeros; |
d37e2969 | 177 | |
9cbe21d8 | 178 | if (!buf || !nbytes) |
d37e2969 | 179 | return -EINVAL; |
cdec9cb5 DK |
180 | |
181 | if (sign) | |
182 | *sign = a->sign; | |
d37e2969 | 183 | |
3ee0cb5f | 184 | lzeros = count_lzeros(a); |
d37e2969 | 185 | |
9cbe21d8 AZ |
186 | if (buf_len < n - lzeros) { |
187 | *nbytes = n - lzeros; | |
188 | return -EOVERFLOW; | |
189 | } | |
190 | ||
d37e2969 | 191 | p = buf; |
0f74fbf7 | 192 | *nbytes = n - lzeros; |
cdec9cb5 | 193 | |
f00fa241 NS |
194 | for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB, |
195 | lzeros %= BYTES_PER_MPI_LIMB; | |
196 | i >= 0; i--) { | |
cdec9cb5 | 197 | #if BYTES_PER_MPI_LIMB == 4 |
90f864e2 | 198 | alimb = cpu_to_be32(a->d[i]); |
cdec9cb5 | 199 | #elif BYTES_PER_MPI_LIMB == 8 |
90f864e2 | 200 | alimb = cpu_to_be64(a->d[i]); |
cdec9cb5 DK |
201 | #else |
202 | #error please implement for this limb size. | |
203 | #endif | |
462696fd NS |
204 | memcpy(p, (u8 *)&alimb + lzeros, BYTES_PER_MPI_LIMB - lzeros); |
205 | p += BYTES_PER_MPI_LIMB - lzeros; | |
206 | lzeros = 0; | |
cdec9cb5 | 207 | } |
d37e2969 TS |
208 | return 0; |
209 | } | |
210 | EXPORT_SYMBOL_GPL(mpi_read_buffer); | |
211 | ||
212 | /* | |
213 | * mpi_get_buffer() - Returns an allocated buffer with the MPI (msb first). | |
214 | * Caller must free the return string. | |
215 | * This function does return a 0 byte buffer with nbytes set to zero if the | |
216 | * value of A is zero. | |
217 | * | |
218 | * @a: a multi precision integer. | |
219 | * @nbytes: receives the length of this buffer. | |
220 | * @sign: if not NULL, it will be set to the sign of the a. | |
221 | * | |
222 | * Return: Pointer to MPI buffer or NULL on error | |
223 | */ | |
224 | void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign) | |
225 | { | |
0f74fbf7 | 226 | uint8_t *buf; |
d37e2969 TS |
227 | unsigned int n; |
228 | int ret; | |
229 | ||
230 | if (!nbytes) | |
231 | return NULL; | |
232 | ||
233 | n = mpi_get_size(a); | |
234 | ||
235 | if (!n) | |
236 | n++; | |
237 | ||
238 | buf = kmalloc(n, GFP_KERNEL); | |
239 | ||
240 | if (!buf) | |
241 | return NULL; | |
242 | ||
243 | ret = mpi_read_buffer(a, buf, n, nbytes, sign); | |
244 | ||
245 | if (ret) { | |
246 | kfree(buf); | |
247 | return NULL; | |
248 | } | |
d37e2969 | 249 | return buf; |
cdec9cb5 DK |
250 | } |
251 | EXPORT_SYMBOL_GPL(mpi_get_buffer); | |
252 | ||
2d4d1eea TS |
253 | /** |
254 | * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first) | |
255 | * | |
256 | * This function works in the same way as the mpi_read_buffer, but it | |
257 | * takes an sgl instead of u8 * buf. | |
258 | * | |
259 | * @a: a multi precision integer | |
260 | * @sgl: scatterlist to write to. Needs to be at least | |
261 | * mpi_get_size(a) long. | |
262 | * @nbytes: in/out param - it has the be set to the maximum number of | |
263 | * bytes that can be written to sgl. This has to be at least | |
264 | * the size of the integer a. On return it receives the actual | |
9cbe21d8 AZ |
265 | * length of the data written on success or the data that would |
266 | * be written if buffer was too small. | |
2d4d1eea TS |
267 | * @sign: if not NULL, it will be set to the sign of a. |
268 | * | |
269 | * Return: 0 on success or error code in case of error | |
270 | */ | |
271 | int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, | |
272 | int *sign) | |
273 | { | |
274 | u8 *p, *p2; | |
d7552906 NS |
275 | #if BYTES_PER_MPI_LIMB == 4 |
276 | __be32 alimb; | |
277 | #elif BYTES_PER_MPI_LIMB == 8 | |
278 | __be64 alimb; | |
279 | #else | |
280 | #error please implement for this limb size. | |
281 | #endif | |
2d4d1eea | 282 | unsigned int n = mpi_get_size(a); |
3ee0cb5f | 283 | int i, x, y = 0, lzeros, buf_len; |
2d4d1eea | 284 | |
9cbe21d8 | 285 | if (!nbytes) |
2d4d1eea TS |
286 | return -EINVAL; |
287 | ||
288 | if (sign) | |
289 | *sign = a->sign; | |
290 | ||
3ee0cb5f | 291 | lzeros = count_lzeros(a); |
2d4d1eea | 292 | |
9cbe21d8 AZ |
293 | if (*nbytes < n - lzeros) { |
294 | *nbytes = n - lzeros; | |
295 | return -EOVERFLOW; | |
296 | } | |
297 | ||
2d4d1eea TS |
298 | *nbytes = n - lzeros; |
299 | buf_len = sgl->length; | |
300 | p2 = sg_virt(sgl); | |
301 | ||
f2d1362f NS |
302 | for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB, |
303 | lzeros %= BYTES_PER_MPI_LIMB; | |
304 | i >= 0; i--) { | |
2d4d1eea | 305 | #if BYTES_PER_MPI_LIMB == 4 |
d7552906 | 306 | alimb = cpu_to_be32(a->d[i]); |
2d4d1eea | 307 | #elif BYTES_PER_MPI_LIMB == 8 |
d7552906 | 308 | alimb = cpu_to_be64(a->d[i]); |
2d4d1eea TS |
309 | #else |
310 | #error please implement for this limb size. | |
311 | #endif | |
654842ef | 312 | if (lzeros) { |
f2d1362f | 313 | y = lzeros; |
654842ef | 314 | lzeros = 0; |
2d4d1eea TS |
315 | } |
316 | ||
d7552906 | 317 | p = (u8 *)&alimb + y; |
2d4d1eea TS |
318 | |
319 | for (x = 0; x < sizeof(alimb) - y; x++) { | |
320 | if (!buf_len) { | |
321 | sgl = sg_next(sgl); | |
322 | if (!sgl) | |
323 | return -EINVAL; | |
324 | buf_len = sgl->length; | |
325 | p2 = sg_virt(sgl); | |
326 | } | |
327 | *p2++ = *p++; | |
328 | buf_len--; | |
329 | } | |
330 | y = 0; | |
331 | } | |
332 | return 0; | |
333 | } | |
334 | EXPORT_SYMBOL_GPL(mpi_write_to_sgl); | |
335 | ||
336 | /* | |
337 | * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with | |
338 | * data from the sgl | |
339 | * | |
340 | * This function works in the same way as the mpi_read_raw_data, but it | |
341 | * takes an sgl instead of void * buffer. i.e. it allocates | |
342 | * a new MPI and reads the content of the sgl to the MPI. | |
343 | * | |
344 | * @sgl: scatterlist to read from | |
b6985389 | 345 | * @nbytes: number of bytes to read |
2d4d1eea TS |
346 | * |
347 | * Return: Pointer to a new MPI or NULL on error | |
348 | */ | |
b6985389 | 349 | MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes) |
2d4d1eea TS |
350 | { |
351 | struct scatterlist *sg; | |
352 | int x, i, j, z, lzeros, ents; | |
b6985389 | 353 | unsigned int nbits, nlimbs; |
2d4d1eea TS |
354 | mpi_limb_t a; |
355 | MPI val = NULL; | |
356 | ||
357 | lzeros = 0; | |
358 | ents = sg_nents(sgl); | |
359 | ||
360 | for_each_sg(sgl, sg, ents, i) { | |
361 | const u8 *buff = sg_virt(sg); | |
362 | int len = sg->length; | |
363 | ||
63349d02 | 364 | while (len && !*buff) { |
2d4d1eea | 365 | lzeros++; |
63349d02 SM |
366 | len--; |
367 | buff++; | |
368 | } | |
2d4d1eea TS |
369 | |
370 | if (len && *buff) | |
371 | break; | |
372 | ||
373 | ents--; | |
ab1e912e | 374 | nbytes -= lzeros; |
2d4d1eea TS |
375 | lzeros = 0; |
376 | } | |
377 | ||
378 | sgl = sg; | |
ab1e912e | 379 | nbytes -= lzeros; |
2d4d1eea TS |
380 | nbits = nbytes * 8; |
381 | if (nbits > MAX_EXTERN_MPI_BITS) { | |
382 | pr_info("MPI: mpi too large (%u bits)\n", nbits); | |
383 | return NULL; | |
384 | } | |
385 | ||
386 | if (nbytes > 0) | |
64c09b0b NS |
387 | nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros)) - |
388 | (BITS_PER_LONG - 8); | |
2d4d1eea TS |
389 | |
390 | nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB); | |
391 | val = mpi_alloc(nlimbs); | |
392 | if (!val) | |
393 | return NULL; | |
394 | ||
395 | val->nbits = nbits; | |
396 | val->sign = 0; | |
397 | val->nlimbs = nlimbs; | |
398 | ||
399 | if (nbytes == 0) | |
400 | return val; | |
401 | ||
402 | j = nlimbs - 1; | |
403 | a = 0; | |
85d541a3 NS |
404 | z = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; |
405 | z %= BYTES_PER_MPI_LIMB; | |
2d4d1eea TS |
406 | |
407 | for_each_sg(sgl, sg, ents, i) { | |
408 | const u8 *buffer = sg_virt(sg) + lzeros; | |
409 | int len = sg->length - lzeros; | |
2d4d1eea | 410 | |
85d541a3 | 411 | for (x = 0; x < len; x++) { |
2d4d1eea TS |
412 | a <<= 8; |
413 | a |= *buffer++; | |
414 | if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) { | |
415 | val->d[j--] = a; | |
416 | a = 0; | |
417 | } | |
418 | } | |
419 | z += x; | |
2d4d1eea TS |
420 | lzeros = 0; |
421 | } | |
422 | return val; | |
423 | } | |
424 | EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl); |