blob: 623439e4bad5a8014698451b44bcf29fb6885a35 [file] [log] [blame]
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +03001/* 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
David Howellse1045992012-09-24 17:11:27 +010021#include <linux/bitops.h>
Christoph Hellwiga1164a32015-08-28 09:27:15 +020022#include <linux/count_zeros.h>
Nicolai Stanged7552902016-03-22 13:12:39 +010023#include <linux/byteorder/generic.h>
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030024#include "mpi-internal.h"
25
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030026#define MAX_EXTERN_MPI_BITS 16384
27
David Howellse1045992012-09-24 17:11:27 +010028/**
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 */
33MPI 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
Chen Gang5402b802013-06-12 14:04:40 -070041 while (nbytes > 0 && buffer[0] == 0) {
David Howellse1045992012-09-24 17:11:27 +010042 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
Andy Shevchenko0d2a1b22013-01-30 11:30:06 +020056 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
David Howellse1045992012-09-24 17:11:27 +010057 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}
79EXPORT_SYMBOL_GPL(mpi_read_raw_data);
80
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030081MPI 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;
Dmitry Kasatkin3cccd152012-01-26 19:13:16 +020087 MPI val = NULL;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030088
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
Andy Shevchenko0d2a1b22013-01-30 11:30:06 +0200100 nbytes = DIV_ROUND_UP(nbits, 8);
101 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300102 val = mpi_alloc(nlimbs);
103 if (!val)
Dmitry Kasatkin3cccd152012-01-26 19:13:16 +0200104 return NULL;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300105 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
126leave:
127 *ret_nread = nread;
128 return val;
129}
130EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
131
Michal Marek3ee0cb52016-02-17 14:46:59 +0100132static 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
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700149/**
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.
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100156 * @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.
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700159 * @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
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300162 */
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700163int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
164 int *sign)
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300165{
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700166 uint8_t *p;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300167 mpi_limb_t alimb;
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700168 unsigned int n = mpi_get_size(a);
Michal Marek3ee0cb52016-02-17 14:46:59 +0100169 int i, lzeros;
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700170
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100171 if (!buf || !nbytes)
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700172 return -EINVAL;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300173
174 if (sign)
175 *sign = a->sign;
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700176
Michal Marek3ee0cb52016-02-17 14:46:59 +0100177 lzeros = count_lzeros(a);
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700178
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100179 if (buf_len < n - lzeros) {
180 *nbytes = n - lzeros;
181 return -EOVERFLOW;
182 }
183
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700184 p = buf;
Tadeusz Struk0f74fbf2015-08-24 07:52:14 -0700185 *nbytes = n - lzeros;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300186
187 for (i = a->nlimbs - 1; i >= 0; i--) {
188 alimb = a->d[i];
189#if BYTES_PER_MPI_LIMB == 4
190 *p++ = alimb >> 24;
191 *p++ = alimb >> 16;
192 *p++ = alimb >> 8;
193 *p++ = alimb;
194#elif BYTES_PER_MPI_LIMB == 8
195 *p++ = alimb >> 56;
196 *p++ = alimb >> 48;
197 *p++ = alimb >> 40;
198 *p++ = alimb >> 32;
199 *p++ = alimb >> 24;
200 *p++ = alimb >> 16;
201 *p++ = alimb >> 8;
202 *p++ = alimb;
203#else
204#error please implement for this limb size.
205#endif
Tadeusz Struk0f74fbf2015-08-24 07:52:14 -0700206
207 if (lzeros > 0) {
208 if (lzeros >= sizeof(alimb)) {
209 p -= sizeof(alimb);
210 } else {
211 mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
212 mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
213 + lzeros;
214 *limb1 = *limb2;
215 p -= lzeros;
216 }
217 lzeros -= sizeof(alimb);
218 }
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300219 }
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700220 return 0;
221}
222EXPORT_SYMBOL_GPL(mpi_read_buffer);
223
224/*
225 * mpi_get_buffer() - Returns an allocated buffer with the MPI (msb first).
226 * Caller must free the return string.
227 * This function does return a 0 byte buffer with nbytes set to zero if the
228 * value of A is zero.
229 *
230 * @a: a multi precision integer.
231 * @nbytes: receives the length of this buffer.
232 * @sign: if not NULL, it will be set to the sign of the a.
233 *
234 * Return: Pointer to MPI buffer or NULL on error
235 */
236void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
237{
Tadeusz Struk0f74fbf2015-08-24 07:52:14 -0700238 uint8_t *buf;
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700239 unsigned int n;
240 int ret;
241
242 if (!nbytes)
243 return NULL;
244
245 n = mpi_get_size(a);
246
247 if (!n)
248 n++;
249
250 buf = kmalloc(n, GFP_KERNEL);
251
252 if (!buf)
253 return NULL;
254
255 ret = mpi_read_buffer(a, buf, n, nbytes, sign);
256
257 if (ret) {
258 kfree(buf);
259 return NULL;
260 }
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700261 return buf;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300262}
263EXPORT_SYMBOL_GPL(mpi_get_buffer);
264
265/****************
266 * Use BUFFER to update MPI.
267 */
268int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign)
269{
270 const uint8_t *buffer = xbuffer, *p;
271 mpi_limb_t alimb;
272 int nlimbs;
273 int i;
274
Andy Shevchenko0d2a1b22013-01-30 11:30:06 +0200275 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300276 if (RESIZE_IF_NEEDED(a, nlimbs) < 0)
277 return -ENOMEM;
278 a->sign = sign;
279
280 for (i = 0, p = buffer + nbytes - 1; p >= buffer + BYTES_PER_MPI_LIMB;) {
281#if BYTES_PER_MPI_LIMB == 4
282 alimb = (mpi_limb_t) *p--;
283 alimb |= (mpi_limb_t) *p-- << 8;
284 alimb |= (mpi_limb_t) *p-- << 16;
285 alimb |= (mpi_limb_t) *p-- << 24;
286#elif BYTES_PER_MPI_LIMB == 8
287 alimb = (mpi_limb_t) *p--;
288 alimb |= (mpi_limb_t) *p-- << 8;
289 alimb |= (mpi_limb_t) *p-- << 16;
290 alimb |= (mpi_limb_t) *p-- << 24;
291 alimb |= (mpi_limb_t) *p-- << 32;
292 alimb |= (mpi_limb_t) *p-- << 40;
293 alimb |= (mpi_limb_t) *p-- << 48;
294 alimb |= (mpi_limb_t) *p-- << 56;
295#else
296#error please implement for this limb size.
297#endif
298 a->d[i++] = alimb;
299 }
300 if (p >= buffer) {
301#if BYTES_PER_MPI_LIMB == 4
302 alimb = *p--;
303 if (p >= buffer)
304 alimb |= (mpi_limb_t) *p-- << 8;
305 if (p >= buffer)
306 alimb |= (mpi_limb_t) *p-- << 16;
307 if (p >= buffer)
308 alimb |= (mpi_limb_t) *p-- << 24;
309#elif BYTES_PER_MPI_LIMB == 8
310 alimb = (mpi_limb_t) *p--;
311 if (p >= buffer)
312 alimb |= (mpi_limb_t) *p-- << 8;
313 if (p >= buffer)
314 alimb |= (mpi_limb_t) *p-- << 16;
315 if (p >= buffer)
316 alimb |= (mpi_limb_t) *p-- << 24;
317 if (p >= buffer)
318 alimb |= (mpi_limb_t) *p-- << 32;
319 if (p >= buffer)
320 alimb |= (mpi_limb_t) *p-- << 40;
321 if (p >= buffer)
322 alimb |= (mpi_limb_t) *p-- << 48;
323 if (p >= buffer)
324 alimb |= (mpi_limb_t) *p-- << 56;
325#else
326#error please implement for this limb size.
327#endif
328 a->d[i++] = alimb;
329 }
330 a->nlimbs = i;
331
332 if (i != nlimbs) {
333 pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i,
334 nlimbs);
335 BUG();
336 }
337 return 0;
338}
339EXPORT_SYMBOL_GPL(mpi_set_buffer);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700340
341/**
342 * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first)
343 *
344 * This function works in the same way as the mpi_read_buffer, but it
345 * takes an sgl instead of u8 * buf.
346 *
347 * @a: a multi precision integer
348 * @sgl: scatterlist to write to. Needs to be at least
349 * mpi_get_size(a) long.
350 * @nbytes: in/out param - it has the be set to the maximum number of
351 * bytes that can be written to sgl. This has to be at least
352 * the size of the integer a. On return it receives the actual
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100353 * length of the data written on success or the data that would
354 * be written if buffer was too small.
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700355 * @sign: if not NULL, it will be set to the sign of a.
356 *
357 * Return: 0 on success or error code in case of error
358 */
359int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
360 int *sign)
361{
362 u8 *p, *p2;
Nicolai Stanged7552902016-03-22 13:12:39 +0100363#if BYTES_PER_MPI_LIMB == 4
364 __be32 alimb;
365#elif BYTES_PER_MPI_LIMB == 8
366 __be64 alimb;
367#else
368#error please implement for this limb size.
369#endif
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700370 unsigned int n = mpi_get_size(a);
Michal Marek3ee0cb52016-02-17 14:46:59 +0100371 int i, x, y = 0, lzeros, buf_len;
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700372
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100373 if (!nbytes)
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700374 return -EINVAL;
375
376 if (sign)
377 *sign = a->sign;
378
Michal Marek3ee0cb52016-02-17 14:46:59 +0100379 lzeros = count_lzeros(a);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700380
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100381 if (*nbytes < n - lzeros) {
382 *nbytes = n - lzeros;
383 return -EOVERFLOW;
384 }
385
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700386 *nbytes = n - lzeros;
387 buf_len = sgl->length;
388 p2 = sg_virt(sgl);
389
Nicolai Stangef2d13622016-03-22 13:12:35 +0100390 for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
391 lzeros %= BYTES_PER_MPI_LIMB;
392 i >= 0; i--) {
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700393#if BYTES_PER_MPI_LIMB == 4
Nicolai Stanged7552902016-03-22 13:12:39 +0100394 alimb = cpu_to_be32(a->d[i]);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700395#elif BYTES_PER_MPI_LIMB == 8
Nicolai Stanged7552902016-03-22 13:12:39 +0100396 alimb = cpu_to_be64(a->d[i]);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700397#else
398#error please implement for this limb size.
399#endif
Nicolai Stange654842e2016-03-22 13:12:36 +0100400 if (lzeros) {
Nicolai Stangef2d13622016-03-22 13:12:35 +0100401 y = lzeros;
Nicolai Stange654842e2016-03-22 13:12:36 +0100402 lzeros = 0;
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700403 }
404
Nicolai Stanged7552902016-03-22 13:12:39 +0100405 p = (u8 *)&alimb + y;
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700406
407 for (x = 0; x < sizeof(alimb) - y; x++) {
408 if (!buf_len) {
409 sgl = sg_next(sgl);
410 if (!sgl)
411 return -EINVAL;
412 buf_len = sgl->length;
413 p2 = sg_virt(sgl);
414 }
415 *p2++ = *p++;
416 buf_len--;
417 }
418 y = 0;
419 }
420 return 0;
421}
422EXPORT_SYMBOL_GPL(mpi_write_to_sgl);
423
424/*
425 * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with
426 * data from the sgl
427 *
428 * This function works in the same way as the mpi_read_raw_data, but it
429 * takes an sgl instead of void * buffer. i.e. it allocates
430 * a new MPI and reads the content of the sgl to the MPI.
431 *
432 * @sgl: scatterlist to read from
433 * @len: number of bytes to read
434 *
435 * Return: Pointer to a new MPI or NULL on error
436 */
437MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len)
438{
439 struct scatterlist *sg;
440 int x, i, j, z, lzeros, ents;
441 unsigned int nbits, nlimbs, nbytes;
442 mpi_limb_t a;
443 MPI val = NULL;
444
445 lzeros = 0;
446 ents = sg_nents(sgl);
447
448 for_each_sg(sgl, sg, ents, i) {
449 const u8 *buff = sg_virt(sg);
450 int len = sg->length;
451
Stephan Mueller63349d02015-10-18 12:45:18 +0200452 while (len && !*buff) {
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700453 lzeros++;
Stephan Mueller63349d02015-10-18 12:45:18 +0200454 len--;
455 buff++;
456 }
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700457
458 if (len && *buff)
459 break;
460
461 ents--;
462 lzeros = 0;
463 }
464
465 sgl = sg;
466
467 if (!ents)
468 nbytes = 0;
469 else
470 nbytes = len - lzeros;
471
472 nbits = nbytes * 8;
473 if (nbits > MAX_EXTERN_MPI_BITS) {
474 pr_info("MPI: mpi too large (%u bits)\n", nbits);
475 return NULL;
476 }
477
478 if (nbytes > 0)
479 nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros));
480 else
481 nbits = 0;
482
483 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
484 val = mpi_alloc(nlimbs);
485 if (!val)
486 return NULL;
487
488 val->nbits = nbits;
489 val->sign = 0;
490 val->nlimbs = nlimbs;
491
492 if (nbytes == 0)
493 return val;
494
495 j = nlimbs - 1;
496 a = 0;
497 z = 0;
498 x = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
499 x %= BYTES_PER_MPI_LIMB;
500
501 for_each_sg(sgl, sg, ents, i) {
502 const u8 *buffer = sg_virt(sg) + lzeros;
503 int len = sg->length - lzeros;
504 int buf_shift = x;
505
506 if (sg_is_last(sg) && (len % BYTES_PER_MPI_LIMB))
507 len += BYTES_PER_MPI_LIMB - (len % BYTES_PER_MPI_LIMB);
508
509 for (; x < len + buf_shift; x++) {
510 a <<= 8;
511 a |= *buffer++;
512 if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) {
513 val->d[j--] = a;
514 a = 0;
515 }
516 }
517 z += x;
518 x = 0;
519 lzeros = 0;
520 }
521 return val;
522}
523EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl);