blob: a999ee1cddc5805638d598185dd40ba2c95e888e [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>
Nicolai Stange90f864e2016-03-22 13:12:41 +010024#include <linux/string.h>
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030025#include "mpi-internal.h"
26
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030027#define MAX_EXTERN_MPI_BITS 16384
28
David Howellse1045992012-09-24 17:11:27 +010029/**
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 */
34MPI 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
Chen Gang5402b802013-06-12 14:04:40 -070042 while (nbytes > 0 && buffer[0] == 0) {
David Howellse1045992012-09-24 17:11:27 +010043 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
Andy Shevchenko0d2a1b22013-01-30 11:30:06 +020057 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
David Howellse1045992012-09-24 17:11:27 +010058 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}
80EXPORT_SYMBOL_GPL(mpi_read_raw_data);
81
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030082MPI 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;
Dmitry Kasatkin3cccd152012-01-26 19:13:16 +020088 MPI val = NULL;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030089
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
Andy Shevchenko0d2a1b22013-01-30 11:30:06 +0200101 nbytes = DIV_ROUND_UP(nbits, 8);
102 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300103 val = mpi_alloc(nlimbs);
104 if (!val)
Dmitry Kasatkin3cccd152012-01-26 19:13:16 +0200105 return NULL;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300106 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
127leave:
128 *ret_nread = nread;
129 return val;
130}
131EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
132
Michal Marek3ee0cb52016-02-17 14:46:59 +0100133static 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
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700150/**
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.
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100157 * @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.
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700160 * @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
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300163 */
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700164int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
165 int *sign)
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300166{
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700167 uint8_t *p;
Nicolai Stange90f864e2016-03-22 13:12:41 +0100168#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
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700175 unsigned int n = mpi_get_size(a);
Michal Marek3ee0cb52016-02-17 14:46:59 +0100176 int i, lzeros;
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700177
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100178 if (!buf || !nbytes)
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700179 return -EINVAL;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300180
181 if (sign)
182 *sign = a->sign;
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700183
Michal Marek3ee0cb52016-02-17 14:46:59 +0100184 lzeros = count_lzeros(a);
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700185
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100186 if (buf_len < n - lzeros) {
187 *nbytes = n - lzeros;
188 return -EOVERFLOW;
189 }
190
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700191 p = buf;
Tadeusz Struk0f74fbf2015-08-24 07:52:14 -0700192 *nbytes = n - lzeros;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300193
Nicolai Stangef00fa242016-03-22 13:12:40 +0100194 for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
195 lzeros %= BYTES_PER_MPI_LIMB;
196 i >= 0; i--) {
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300197#if BYTES_PER_MPI_LIMB == 4
Nicolai Stange90f864e2016-03-22 13:12:41 +0100198 alimb = cpu_to_be32(a->d[i]);
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300199#elif BYTES_PER_MPI_LIMB == 8
Nicolai Stange90f864e2016-03-22 13:12:41 +0100200 alimb = cpu_to_be64(a->d[i]);
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300201#else
202#error please implement for this limb size.
203#endif
Nicolai Stange90f864e2016-03-22 13:12:41 +0100204 memcpy(p, &alimb, BYTES_PER_MPI_LIMB);
205 p += BYTES_PER_MPI_LIMB;
Tadeusz Struk0f74fbf2015-08-24 07:52:14 -0700206 if (lzeros > 0) {
Nicolai Stangef00fa242016-03-22 13:12:40 +0100207 mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
208 mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
209 + lzeros;
210 *limb1 = *limb2;
211 p -= lzeros;
Tadeusz Struk0f74fbf2015-08-24 07:52:14 -0700212 lzeros -= sizeof(alimb);
213 }
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300214 }
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700215 return 0;
216}
217EXPORT_SYMBOL_GPL(mpi_read_buffer);
218
219/*
220 * mpi_get_buffer() - Returns an allocated buffer with the MPI (msb first).
221 * Caller must free the return string.
222 * This function does return a 0 byte buffer with nbytes set to zero if the
223 * value of A is zero.
224 *
225 * @a: a multi precision integer.
226 * @nbytes: receives the length of this buffer.
227 * @sign: if not NULL, it will be set to the sign of the a.
228 *
229 * Return: Pointer to MPI buffer or NULL on error
230 */
231void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
232{
Tadeusz Struk0f74fbf2015-08-24 07:52:14 -0700233 uint8_t *buf;
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700234 unsigned int n;
235 int ret;
236
237 if (!nbytes)
238 return NULL;
239
240 n = mpi_get_size(a);
241
242 if (!n)
243 n++;
244
245 buf = kmalloc(n, GFP_KERNEL);
246
247 if (!buf)
248 return NULL;
249
250 ret = mpi_read_buffer(a, buf, n, nbytes, sign);
251
252 if (ret) {
253 kfree(buf);
254 return NULL;
255 }
Tadeusz Strukd37e2962015-06-15 13:18:36 -0700256 return buf;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300257}
258EXPORT_SYMBOL_GPL(mpi_get_buffer);
259
260/****************
261 * Use BUFFER to update MPI.
262 */
263int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign)
264{
265 const uint8_t *buffer = xbuffer, *p;
266 mpi_limb_t alimb;
267 int nlimbs;
268 int i;
269
Andy Shevchenko0d2a1b22013-01-30 11:30:06 +0200270 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300271 if (RESIZE_IF_NEEDED(a, nlimbs) < 0)
272 return -ENOMEM;
273 a->sign = sign;
274
275 for (i = 0, p = buffer + nbytes - 1; p >= buffer + BYTES_PER_MPI_LIMB;) {
276#if BYTES_PER_MPI_LIMB == 4
277 alimb = (mpi_limb_t) *p--;
278 alimb |= (mpi_limb_t) *p-- << 8;
279 alimb |= (mpi_limb_t) *p-- << 16;
280 alimb |= (mpi_limb_t) *p-- << 24;
281#elif BYTES_PER_MPI_LIMB == 8
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 alimb |= (mpi_limb_t) *p-- << 32;
287 alimb |= (mpi_limb_t) *p-- << 40;
288 alimb |= (mpi_limb_t) *p-- << 48;
289 alimb |= (mpi_limb_t) *p-- << 56;
290#else
291#error please implement for this limb size.
292#endif
293 a->d[i++] = alimb;
294 }
295 if (p >= buffer) {
296#if BYTES_PER_MPI_LIMB == 4
297 alimb = *p--;
298 if (p >= buffer)
299 alimb |= (mpi_limb_t) *p-- << 8;
300 if (p >= buffer)
301 alimb |= (mpi_limb_t) *p-- << 16;
302 if (p >= buffer)
303 alimb |= (mpi_limb_t) *p-- << 24;
304#elif BYTES_PER_MPI_LIMB == 8
305 alimb = (mpi_limb_t) *p--;
306 if (p >= buffer)
307 alimb |= (mpi_limb_t) *p-- << 8;
308 if (p >= buffer)
309 alimb |= (mpi_limb_t) *p-- << 16;
310 if (p >= buffer)
311 alimb |= (mpi_limb_t) *p-- << 24;
312 if (p >= buffer)
313 alimb |= (mpi_limb_t) *p-- << 32;
314 if (p >= buffer)
315 alimb |= (mpi_limb_t) *p-- << 40;
316 if (p >= buffer)
317 alimb |= (mpi_limb_t) *p-- << 48;
318 if (p >= buffer)
319 alimb |= (mpi_limb_t) *p-- << 56;
320#else
321#error please implement for this limb size.
322#endif
323 a->d[i++] = alimb;
324 }
325 a->nlimbs = i;
326
327 if (i != nlimbs) {
328 pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i,
329 nlimbs);
330 BUG();
331 }
332 return 0;
333}
334EXPORT_SYMBOL_GPL(mpi_set_buffer);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700335
336/**
337 * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first)
338 *
339 * This function works in the same way as the mpi_read_buffer, but it
340 * takes an sgl instead of u8 * buf.
341 *
342 * @a: a multi precision integer
343 * @sgl: scatterlist to write to. Needs to be at least
344 * mpi_get_size(a) long.
345 * @nbytes: in/out param - it has the be set to the maximum number of
346 * bytes that can be written to sgl. This has to be at least
347 * the size of the integer a. On return it receives the actual
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100348 * length of the data written on success or the data that would
349 * be written if buffer was too small.
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700350 * @sign: if not NULL, it will be set to the sign of a.
351 *
352 * Return: 0 on success or error code in case of error
353 */
354int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
355 int *sign)
356{
357 u8 *p, *p2;
Nicolai Stanged7552902016-03-22 13:12:39 +0100358#if BYTES_PER_MPI_LIMB == 4
359 __be32 alimb;
360#elif BYTES_PER_MPI_LIMB == 8
361 __be64 alimb;
362#else
363#error please implement for this limb size.
364#endif
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700365 unsigned int n = mpi_get_size(a);
Michal Marek3ee0cb52016-02-17 14:46:59 +0100366 int i, x, y = 0, lzeros, buf_len;
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700367
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100368 if (!nbytes)
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700369 return -EINVAL;
370
371 if (sign)
372 *sign = a->sign;
373
Michal Marek3ee0cb52016-02-17 14:46:59 +0100374 lzeros = count_lzeros(a);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700375
Andrzej Zaborowski9cbe21d2015-11-13 12:01:32 +0100376 if (*nbytes < n - lzeros) {
377 *nbytes = n - lzeros;
378 return -EOVERFLOW;
379 }
380
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700381 *nbytes = n - lzeros;
382 buf_len = sgl->length;
383 p2 = sg_virt(sgl);
384
Nicolai Stangef2d13622016-03-22 13:12:35 +0100385 for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
386 lzeros %= BYTES_PER_MPI_LIMB;
387 i >= 0; i--) {
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700388#if BYTES_PER_MPI_LIMB == 4
Nicolai Stanged7552902016-03-22 13:12:39 +0100389 alimb = cpu_to_be32(a->d[i]);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700390#elif BYTES_PER_MPI_LIMB == 8
Nicolai Stanged7552902016-03-22 13:12:39 +0100391 alimb = cpu_to_be64(a->d[i]);
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700392#else
393#error please implement for this limb size.
394#endif
Nicolai Stange654842e2016-03-22 13:12:36 +0100395 if (lzeros) {
Nicolai Stangef2d13622016-03-22 13:12:35 +0100396 y = lzeros;
Nicolai Stange654842e2016-03-22 13:12:36 +0100397 lzeros = 0;
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700398 }
399
Nicolai Stanged7552902016-03-22 13:12:39 +0100400 p = (u8 *)&alimb + y;
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700401
402 for (x = 0; x < sizeof(alimb) - y; x++) {
403 if (!buf_len) {
404 sgl = sg_next(sgl);
405 if (!sgl)
406 return -EINVAL;
407 buf_len = sgl->length;
408 p2 = sg_virt(sgl);
409 }
410 *p2++ = *p++;
411 buf_len--;
412 }
413 y = 0;
414 }
415 return 0;
416}
417EXPORT_SYMBOL_GPL(mpi_write_to_sgl);
418
419/*
420 * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with
421 * data from the sgl
422 *
423 * This function works in the same way as the mpi_read_raw_data, but it
424 * takes an sgl instead of void * buffer. i.e. it allocates
425 * a new MPI and reads the content of the sgl to the MPI.
426 *
427 * @sgl: scatterlist to read from
428 * @len: number of bytes to read
429 *
430 * Return: Pointer to a new MPI or NULL on error
431 */
432MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len)
433{
434 struct scatterlist *sg;
435 int x, i, j, z, lzeros, ents;
436 unsigned int nbits, nlimbs, nbytes;
437 mpi_limb_t a;
438 MPI val = NULL;
439
440 lzeros = 0;
441 ents = sg_nents(sgl);
442
443 for_each_sg(sgl, sg, ents, i) {
444 const u8 *buff = sg_virt(sg);
445 int len = sg->length;
446
Stephan Mueller63349d02015-10-18 12:45:18 +0200447 while (len && !*buff) {
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700448 lzeros++;
Stephan Mueller63349d02015-10-18 12:45:18 +0200449 len--;
450 buff++;
451 }
Tadeusz Struk2d4d1ee2015-10-08 09:26:50 -0700452
453 if (len && *buff)
454 break;
455
456 ents--;
457 lzeros = 0;
458 }
459
460 sgl = sg;
461
462 if (!ents)
463 nbytes = 0;
464 else
465 nbytes = len - lzeros;
466
467 nbits = nbytes * 8;
468 if (nbits > MAX_EXTERN_MPI_BITS) {
469 pr_info("MPI: mpi too large (%u bits)\n", nbits);
470 return NULL;
471 }
472
473 if (nbytes > 0)
474 nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros));
475 else
476 nbits = 0;
477
478 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
479 val = mpi_alloc(nlimbs);
480 if (!val)
481 return NULL;
482
483 val->nbits = nbits;
484 val->sign = 0;
485 val->nlimbs = nlimbs;
486
487 if (nbytes == 0)
488 return val;
489
490 j = nlimbs - 1;
491 a = 0;
492 z = 0;
493 x = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
494 x %= BYTES_PER_MPI_LIMB;
495
496 for_each_sg(sgl, sg, ents, i) {
497 const u8 *buffer = sg_virt(sg) + lzeros;
498 int len = sg->length - lzeros;
499 int buf_shift = x;
500
501 if (sg_is_last(sg) && (len % BYTES_PER_MPI_LIMB))
502 len += BYTES_PER_MPI_LIMB - (len % BYTES_PER_MPI_LIMB);
503
504 for (; x < len + buf_shift; x++) {
505 a <<= 8;
506 a |= *buffer++;
507 if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) {
508 val->d[j--] = a;
509 a = 0;
510 }
511 }
512 z += x;
513 x = 0;
514 lzeros = 0;
515 }
516 return val;
517}
518EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl);