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