blob: 2fd8d418526ca9c03f26a15c28c7416d5da6b3ae [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
Nicolai Stangef00fa242016-03-22 13:12:40 +0100187 for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
188 lzeros %= BYTES_PER_MPI_LIMB;
189 i >= 0; i--) {
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300190 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
Tadeusz Struk0f74fbf2015-08-24 07:52:14 -0700208
209 if (lzeros > 0) {
Nicolai Stangef00fa242016-03-22 13:12:40 +0100210 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;
Tadeusz Struk0f74fbf2015-08-24 07:52:14 -0700215 lzeros -= sizeof(alimb);
216 }
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300217 }
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700218 return 0;
219}
220EXPORT_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 */
234void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
235{
Tadeusz Struk0f74fbf2015-08-24 07:52:14 -0700236 uint8_t *buf;
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700237 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 }
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700259 return buf;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300260}
261EXPORT_SYMBOL_GPL(mpi_get_buffer);
262
263/****************
264 * Use BUFFER to update MPI.
265 */
266int 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
Andy Shevchenko0d2a1b22013-01-30 11:30:06 +0200273 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300274 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}
337EXPORT_SYMBOL_GPL(mpi_set_buffer);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700338
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
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100351 * length of the data written on success or the data that would
352 * be written if buffer was too small.
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700353 * @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 */
357int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
358 int *sign)
359{
360 u8 *p, *p2;
Nicolai Stanged7552902016-03-22 13:12:39 +0100361#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
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700368 unsigned int n = mpi_get_size(a);
Michal Marek3ee0cb52016-02-17 14:46:59 +0100369 int i, x, y = 0, lzeros, buf_len;
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700370
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100371 if (!nbytes)
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700372 return -EINVAL;
373
374 if (sign)
375 *sign = a->sign;
376
Michal Marek3ee0cb52016-02-17 14:46:59 +0100377 lzeros = count_lzeros(a);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700378
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100379 if (*nbytes < n - lzeros) {
380 *nbytes = n - lzeros;
381 return -EOVERFLOW;
382 }
383
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700384 *nbytes = n - lzeros;
385 buf_len = sgl->length;
386 p2 = sg_virt(sgl);
387
Nicolai Stangef2d13622016-03-22 13:12:35 +0100388 for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
389 lzeros %= BYTES_PER_MPI_LIMB;
390 i >= 0; i--) {
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700391#if BYTES_PER_MPI_LIMB == 4
Nicolai Stanged7552902016-03-22 13:12:39 +0100392 alimb = cpu_to_be32(a->d[i]);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700393#elif BYTES_PER_MPI_LIMB == 8
Nicolai Stanged7552902016-03-22 13:12:39 +0100394 alimb = cpu_to_be64(a->d[i]);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700395#else
396#error please implement for this limb size.
397#endif
Nicolai Stange654842e2016-03-22 13:12:36 +0100398 if (lzeros) {
Nicolai Stangef2d13622016-03-22 13:12:35 +0100399 y = lzeros;
Nicolai Stange654842e2016-03-22 13:12:36 +0100400 lzeros = 0;
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700401 }
402
Nicolai Stanged7552902016-03-22 13:12:39 +0100403 p = (u8 *)&alimb + y;
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700404
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}
420EXPORT_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 */
435MPI 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
Stephan Mueller63349d02015-10-18 12:45:18 +0200450 while (len && !*buff) {
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700451 lzeros++;
Stephan Mueller63349d02015-10-18 12:45:18 +0200452 len--;
453 buff++;
454 }
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700455
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}
521EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl);