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]) - (BITS_PER_LONG - 8);
55 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
56 val = mpi_alloc(nlimbs);
64 i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
65 i %= BYTES_PER_MPI_LIMB;
66 for (j = nlimbs; j > 0; j--) {
68 for (; i < BYTES_PER_MPI_LIMB; i++) {
78 EXPORT_SYMBOL_GPL(mpi_read_raw_data);
80 MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread)
82 const uint8_t *buffer = xbuffer;
83 unsigned int nbits, nbytes;
87 return ERR_PTR(-EINVAL);
88 nbits = buffer[0] << 8 | buffer[1];
90 if (nbits > MAX_EXTERN_MPI_BITS) {
91 pr_info("MPI: mpi too large (%u bits)\n", nbits);
92 return ERR_PTR(-EINVAL);
95 nbytes = DIV_ROUND_UP(nbits, 8);
96 if (nbytes + 2 > *ret_nread) {
97 pr_info("MPI: mpi larger than buffer nbytes=%u ret_nread=%u\n",
99 return ERR_PTR(-EINVAL);
102 val = mpi_read_raw_data(buffer + 2, nbytes);
104 return ERR_PTR(-ENOMEM);
106 *ret_nread = nbytes + 2;
109 EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
111 static int count_lzeros(MPI a)
116 for (i = a->nlimbs - 1; i >= 0; i--) {
119 lzeros += sizeof(mpi_limb_t);
121 lzeros += count_leading_zeros(alimb) / 8;
129 * mpi_read_buffer() - read MPI to a bufer provided by user (msb first)
131 * @a: a multi precision integer
132 * @buf: bufer to which the output will be written to. Needs to be at
133 * leaset mpi_get_size(a) long.
134 * @buf_len: size of the buf.
135 * @nbytes: receives the actual length of the data written on success and
136 * the data to-be-written on -EOVERFLOW in case buf_len was too
138 * @sign: if not NULL, it will be set to the sign of a.
140 * Return: 0 on success or error code in case of error
142 int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
146 #if BYTES_PER_MPI_LIMB == 4
148 #elif BYTES_PER_MPI_LIMB == 8
151 #error please implement for this limb size.
153 unsigned int n = mpi_get_size(a);
162 lzeros = count_lzeros(a);
164 if (buf_len < n - lzeros) {
165 *nbytes = n - lzeros;
170 *nbytes = n - lzeros;
172 for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
173 lzeros %= BYTES_PER_MPI_LIMB;
175 #if BYTES_PER_MPI_LIMB == 4
176 alimb = cpu_to_be32(a->d[i]);
177 #elif BYTES_PER_MPI_LIMB == 8
178 alimb = cpu_to_be64(a->d[i]);
180 #error please implement for this limb size.
182 memcpy(p, (u8 *)&alimb + lzeros, BYTES_PER_MPI_LIMB - lzeros);
183 p += BYTES_PER_MPI_LIMB - lzeros;
188 EXPORT_SYMBOL_GPL(mpi_read_buffer);
191 * mpi_get_buffer() - Returns an allocated buffer with the MPI (msb first).
192 * Caller must free the return string.
193 * This function does return a 0 byte buffer with nbytes set to zero if the
194 * value of A is zero.
196 * @a: a multi precision integer.
197 * @nbytes: receives the length of this buffer.
198 * @sign: if not NULL, it will be set to the sign of the a.
200 * Return: Pointer to MPI buffer or NULL on error
202 void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign)
216 buf = kmalloc(n, GFP_KERNEL);
221 ret = mpi_read_buffer(a, buf, n, nbytes, sign);
229 EXPORT_SYMBOL_GPL(mpi_get_buffer);
232 * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first)
234 * This function works in the same way as the mpi_read_buffer, but it
235 * takes an sgl instead of u8 * buf.
237 * @a: a multi precision integer
238 * @sgl: scatterlist to write to. Needs to be at least
239 * mpi_get_size(a) long.
240 * @nbytes: the number of bytes to write. Leading bytes will be
242 * @sign: if not NULL, it will be set to the sign of a.
244 * Return: 0 on success or error code in case of error
246 int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned nbytes,
250 #if BYTES_PER_MPI_LIMB == 4
252 #elif BYTES_PER_MPI_LIMB == 8
255 #error please implement for this limb size.
257 unsigned int n = mpi_get_size(a);
266 buf_len = sgl->length;
274 buf_len = sgl->length;
278 i = min_t(unsigned, nbytes - n, buf_len);
285 for (i = a->nlimbs - 1; i >= 0; i--) {
286 #if BYTES_PER_MPI_LIMB == 4
287 alimb = a->d[i] ? cpu_to_be32(a->d[i]) : 0;
288 #elif BYTES_PER_MPI_LIMB == 8
289 alimb = a->d[i] ? cpu_to_be64(a->d[i]) : 0;
291 #error please implement for this limb size.
295 for (x = 0; x < sizeof(alimb); x++) {
300 buf_len = sgl->length;
309 EXPORT_SYMBOL_GPL(mpi_write_to_sgl);
312 * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with
315 * This function works in the same way as the mpi_read_raw_data, but it
316 * takes an sgl instead of void * buffer. i.e. it allocates
317 * a new MPI and reads the content of the sgl to the MPI.
319 * @sgl: scatterlist to read from
320 * @nbytes: number of bytes to read
322 * Return: Pointer to a new MPI or NULL on error
324 MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes)
326 struct scatterlist *sg;
327 int x, i, j, z, lzeros, ents;
328 unsigned int nbits, nlimbs;
333 ents = sg_nents(sgl);
335 for_each_sg(sgl, sg, ents, i) {
336 const u8 *buff = sg_virt(sg);
337 int len = sg->length;
339 while (len && !*buff) {
356 if (nbits > MAX_EXTERN_MPI_BITS) {
357 pr_info("MPI: mpi too large (%u bits)\n", nbits);
362 nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros)) -
365 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
366 val = mpi_alloc(nlimbs);
372 val->nlimbs = nlimbs;
379 z = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
380 z %= BYTES_PER_MPI_LIMB;
382 for_each_sg(sgl, sg, ents, i) {
383 const u8 *buffer = sg_virt(sg) + lzeros;
384 int len = sg->length - lzeros;
386 for (x = 0; x < len; x++) {
389 if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) {
399 EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl);