lib/mpi: mpi_read_buffer(): optimize skipping of leading zero limbs
[linux-2.6/btrfs-unstable.git] / lib / mpi / mpicoder.c
blob2fd8d418526ca9c03f26a15c28c7416d5da6b3ae
1 /* mpicoder.c - Coder for the external representation of MPIs
2 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
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.
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.
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
21 #include <linux/bitops.h>
22 #include <linux/count_zeros.h>
23 #include <linux/byteorder/generic.h>
24 #include "mpi-internal.h"
26 #define MAX_EXTERN_MPI_BITS 16384
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
33 MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes)
35 const uint8_t *buffer = xbuffer;
36 int i, j;
37 unsigned nbits, nlimbs;
38 mpi_limb_t a;
39 MPI val = NULL;
41 while (nbytes > 0 && buffer[0] == 0) {
42 buffer++;
43 nbytes--;
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;
51 if (nbytes > 0)
52 nbits -= count_leading_zeros(buffer[0]);
53 else
54 nbits = 0;
56 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
57 val = mpi_alloc(nlimbs);
58 if (!val)
59 return NULL;
60 val->nbits = nbits;
61 val->sign = 0;
62 val->nlimbs = nlimbs;
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++;
73 i = 0;
74 val->d[j - 1] = a;
77 return val;
79 EXPORT_SYMBOL_GPL(mpi_read_raw_data);
81 MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
83 const uint8_t *buffer = xbuffer;
84 int i, j;
85 unsigned nbits, nbytes, nlimbs, nread = 0;
86 mpi_limb_t a;
87 MPI val = NULL;
89 if (*ret_nread < 2)
90 goto leave;
91 nbits = buffer[0] << 8 | buffer[1];
93 if (nbits > MAX_EXTERN_MPI_BITS) {
94 pr_info("MPI: mpi too large (%u bits)\n", nbits);
95 goto leave;
97 buffer += 2;
98 nread = 2;
100 nbytes = DIV_ROUND_UP(nbits, 8);
101 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
102 val = mpi_alloc(nlimbs);
103 if (!val)
104 return NULL;
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;
119 a <<= 8;
120 a |= *buffer++;
122 i = 0;
123 val->d[j - 1] = a;
126 leave:
127 *ret_nread = nread;
128 return val;
130 EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
132 static int count_lzeros(MPI a)
134 mpi_limb_t alimb;
135 int i, lzeros = 0;
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;
146 return lzeros;
150 * mpi_read_buffer() - read MPI to a bufer provided by user (msb first)
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.
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.
159 * @sign: if not NULL, it will be set to the sign of a.
161 * Return: 0 on success or error code in case of error
163 int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
164 int *sign)
166 uint8_t *p;
167 mpi_limb_t alimb;
168 unsigned int n = mpi_get_size(a);
169 int i, lzeros;
171 if (!buf || !nbytes)
172 return -EINVAL;
174 if (sign)
175 *sign = a->sign;
177 lzeros = count_lzeros(a);
179 if (buf_len < n - lzeros) {
180 *nbytes = n - lzeros;
181 return -EOVERFLOW;
184 p = buf;
185 *nbytes = n - lzeros;
187 for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
188 lzeros %= BYTES_PER_MPI_LIMB;
189 i >= 0; i--) {
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
209 if (lzeros > 0) {
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;
215 lzeros -= sizeof(alimb);
218 return 0;
220 EXPORT_SYMBOL_GPL(mpi_read_buffer);
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.
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.
232 * Return: Pointer to MPI buffer or NULL on error
234 void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
236 uint8_t *buf;
237 unsigned int n;
238 int ret;
240 if (!nbytes)
241 return NULL;
243 n = mpi_get_size(a);
245 if (!n)
246 n++;
248 buf = kmalloc(n, GFP_KERNEL);
250 if (!buf)
251 return NULL;
253 ret = mpi_read_buffer(a, buf, n, nbytes, sign);
255 if (ret) {
256 kfree(buf);
257 return NULL;
259 return buf;
261 EXPORT_SYMBOL_GPL(mpi_get_buffer);
263 /****************
264 * Use BUFFER to update MPI.
266 int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign)
268 const uint8_t *buffer = xbuffer, *p;
269 mpi_limb_t alimb;
270 int nlimbs;
271 int i;
273 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
274 if (RESIZE_IF_NEEDED(a, nlimbs) < 0)
275 return -ENOMEM;
276 a->sign = sign;
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;
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;
328 a->nlimbs = i;
330 if (i != nlimbs) {
331 pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i,
332 nlimbs);
333 BUG();
335 return 0;
337 EXPORT_SYMBOL_GPL(mpi_set_buffer);
340 * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first)
342 * This function works in the same way as the mpi_read_buffer, but it
343 * takes an sgl instead of u8 * buf.
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
351 * length of the data written on success or the data that would
352 * be written if buffer was too small.
353 * @sign: if not NULL, it will be set to the sign of a.
355 * Return: 0 on success or error code in case of error
357 int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
358 int *sign)
360 u8 *p, *p2;
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
368 unsigned int n = mpi_get_size(a);
369 int i, x, y = 0, lzeros, buf_len;
371 if (!nbytes)
372 return -EINVAL;
374 if (sign)
375 *sign = a->sign;
377 lzeros = count_lzeros(a);
379 if (*nbytes < n - lzeros) {
380 *nbytes = n - lzeros;
381 return -EOVERFLOW;
384 *nbytes = n - lzeros;
385 buf_len = sgl->length;
386 p2 = sg_virt(sgl);
388 for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
389 lzeros %= BYTES_PER_MPI_LIMB;
390 i >= 0; i--) {
391 #if BYTES_PER_MPI_LIMB == 4
392 alimb = cpu_to_be32(a->d[i]);
393 #elif BYTES_PER_MPI_LIMB == 8
394 alimb = cpu_to_be64(a->d[i]);
395 #else
396 #error please implement for this limb size.
397 #endif
398 if (lzeros) {
399 y = lzeros;
400 lzeros = 0;
403 p = (u8 *)&alimb + y;
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);
413 *p2++ = *p++;
414 buf_len--;
416 y = 0;
418 return 0;
420 EXPORT_SYMBOL_GPL(mpi_write_to_sgl);
423 * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with
424 * data from the sgl
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.
430 * @sgl: scatterlist to read from
431 * @len: number of bytes to read
433 * Return: Pointer to a new MPI or NULL on error
435 MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len)
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;
443 lzeros = 0;
444 ents = sg_nents(sgl);
446 for_each_sg(sgl, sg, ents, i) {
447 const u8 *buff = sg_virt(sg);
448 int len = sg->length;
450 while (len && !*buff) {
451 lzeros++;
452 len--;
453 buff++;
456 if (len && *buff)
457 break;
459 ents--;
460 lzeros = 0;
463 sgl = sg;
465 if (!ents)
466 nbytes = 0;
467 else
468 nbytes = len - lzeros;
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;
476 if (nbytes > 0)
477 nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros));
478 else
479 nbits = 0;
481 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
482 val = mpi_alloc(nlimbs);
483 if (!val)
484 return NULL;
486 val->nbits = nbits;
487 val->sign = 0;
488 val->nlimbs = nlimbs;
490 if (nbytes == 0)
491 return val;
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;
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;
504 if (sg_is_last(sg) && (len % BYTES_PER_MPI_LIMB))
505 len += BYTES_PER_MPI_LIMB - (len % BYTES_PER_MPI_LIMB);
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;
515 z += x;
516 x = 0;
517 lzeros = 0;
519 return val;
521 EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl);