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 <linux/string.h>
25 #include "mpi-internal.h"
27 #define MAX_EXTERN_MPI_BITS 16384
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
34 MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes)
36 const uint8_t *buffer = xbuffer;
38 unsigned nbits, nlimbs;
42 while (nbytes > 0 && buffer[0] == 0) {
48 if (nbits > MAX_EXTERN_MPI_BITS) {
49 pr_info("MPI: mpi too large (%u bits)\n", nbits);
53 nbits -= count_leading_zeros(buffer[0]);
57 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
58 val = mpi_alloc(nlimbs);
66 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
67 i %= BYTES_PER_MPI_LIMB;
68 for (j = nlimbs; j > 0; j--) {
70 for (; i < BYTES_PER_MPI_LIMB; i++) {
80 EXPORT_SYMBOL_GPL(mpi_read_raw_data);
82 MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
84 const uint8_t *buffer = xbuffer;
86 unsigned nbits, nbytes, nlimbs, nread = 0;
92 nbits = buffer[0] << 8 | buffer[1];
94 if (nbits > MAX_EXTERN_MPI_BITS) {
95 pr_info("MPI: mpi too large (%u bits)\n", nbits);
101 nbytes = DIV_ROUND_UP(nbits, 8);
102 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
103 val = mpi_alloc(nlimbs);
106 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
107 i %= BYTES_PER_MPI_LIMB;
109 j = val->nlimbs = nlimbs;
113 for (; i < BYTES_PER_MPI_LIMB; i++) {
114 if (++nread > *ret_nread) {
116 ("MPI: mpi larger than buffer nread=%d ret_nread=%d\n",
131 EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
133 static int count_lzeros(MPI a)
138 for (i = a->nlimbs - 1; i >= 0; i--) {
141 lzeros += sizeof(mpi_limb_t);
143 lzeros += count_leading_zeros(alimb) / 8;
151 * mpi_read_buffer() - read MPI to a bufer provided by user (msb first)
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.
157 * @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
160 * @sign: if not NULL, it will be set to the sign of a.
162 * Return: 0 on success or error code in case of error
164 int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
168 #if BYTES_PER_MPI_LIMB == 4
170 #elif BYTES_PER_MPI_LIMB == 8
173 #error please implement for this limb size.
175 unsigned int n = mpi_get_size(a);
184 lzeros = count_lzeros(a);
186 if (buf_len < n - lzeros) {
187 *nbytes = n - lzeros;
192 *nbytes = n - lzeros;
194 for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
195 lzeros %= BYTES_PER_MPI_LIMB;
197 #if BYTES_PER_MPI_LIMB == 4
198 alimb = cpu_to_be32(a->d[i]);
199 #elif BYTES_PER_MPI_LIMB == 8
200 alimb = cpu_to_be64(a->d[i]);
202 #error please implement for this limb size.
204 memcpy(p, (u8 *)&alimb + lzeros, BYTES_PER_MPI_LIMB - lzeros);
205 p += BYTES_PER_MPI_LIMB - lzeros;
210 EXPORT_SYMBOL_GPL(mpi_read_buffer);
213 * mpi_get_buffer() - Returns an allocated buffer with the MPI (msb first).
214 * Caller must free the return string.
215 * This function does return a 0 byte buffer with nbytes set to zero if the
216 * value of A is zero.
218 * @a: a multi precision integer.
219 * @nbytes: receives the length of this buffer.
220 * @sign: if not NULL, it will be set to the sign of the a.
222 * Return: Pointer to MPI buffer or NULL on error
224 void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
238 buf = kmalloc(n, GFP_KERNEL);
243 ret = mpi_read_buffer(a, buf, n, nbytes, sign);
251 EXPORT_SYMBOL_GPL(mpi_get_buffer);
254 * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first)
256 * This function works in the same way as the mpi_read_buffer, but it
257 * takes an sgl instead of u8 * buf.
259 * @a: a multi precision integer
260 * @sgl: scatterlist to write to. Needs to be at least
261 * mpi_get_size(a) long.
262 * @nbytes: in/out param - it has the be set to the maximum number of
263 * bytes that can be written to sgl. This has to be at least
264 * the size of the integer a. On return it receives the actual
265 * length of the data written on success or the data that would
266 * be written if buffer was too small.
267 * @sign: if not NULL, it will be set to the sign of a.
269 * Return: 0 on success or error code in case of error
271 int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
275 #if BYTES_PER_MPI_LIMB == 4
277 #elif BYTES_PER_MPI_LIMB == 8
280 #error please implement for this limb size.
282 unsigned int n = mpi_get_size(a);
283 int i, x, y = 0, lzeros, buf_len;
291 lzeros = count_lzeros(a);
293 if (*nbytes < n - lzeros) {
294 *nbytes = n - lzeros;
298 *nbytes = n - lzeros;
299 buf_len = sgl->length;
302 for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
303 lzeros %= BYTES_PER_MPI_LIMB;
305 #if BYTES_PER_MPI_LIMB == 4
306 alimb = cpu_to_be32(a->d[i]);
307 #elif BYTES_PER_MPI_LIMB == 8
308 alimb = cpu_to_be64(a->d[i]);
310 #error please implement for this limb size.
317 p = (u8 *)&alimb + y;
319 for (x = 0; x < sizeof(alimb) - y; x++) {
324 buf_len = sgl->length;
334 EXPORT_SYMBOL_GPL(mpi_write_to_sgl);
337 * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with
340 * This function works in the same way as the mpi_read_raw_data, but it
341 * takes an sgl instead of void * buffer. i.e. it allocates
342 * a new MPI and reads the content of the sgl to the MPI.
344 * @sgl: scatterlist to read from
345 * @nbytes: number of bytes to read
347 * Return: Pointer to a new MPI or NULL on error
349 MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes)
351 struct scatterlist *sg;
352 int x, i, j, z, lzeros, ents;
353 unsigned int nbits, nlimbs;
358 ents = sg_nents(sgl);
360 for_each_sg(sgl, sg, ents, i) {
361 const u8 *buff = sg_virt(sg);
362 int len = sg->length;
364 while (len && !*buff) {
381 if (nbits > MAX_EXTERN_MPI_BITS) {
382 pr_info("MPI: mpi too large (%u bits)\n", nbits);
387 nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros)) -
390 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
391 val = mpi_alloc(nlimbs);
397 val->nlimbs = nlimbs;
404 z = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
405 z %= BYTES_PER_MPI_LIMB;
407 for_each_sg(sgl, sg, ents, i) {
408 const u8 *buffer = sg_virt(sg) + lzeros;
409 int len = sg->length - lzeros;
411 for (x = 0; x < len; x++) {
414 if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) {
424 EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl);