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 * Use BUFFER to update MPI.
256 int mpi_set_buffer(MPI a
, const void *xbuffer
, unsigned nbytes
, int sign
)
258 const uint8_t *buffer
= xbuffer
, *p
;
263 nlimbs
= DIV_ROUND_UP(nbytes
, BYTES_PER_MPI_LIMB
);
264 if (RESIZE_IF_NEEDED(a
, nlimbs
) < 0)
268 for (i
= 0, p
= buffer
+ nbytes
- 1; p
>= buffer
+ BYTES_PER_MPI_LIMB
;) {
269 #if BYTES_PER_MPI_LIMB == 4
270 alimb
= (mpi_limb_t
) *p
--;
271 alimb
|= (mpi_limb_t
) *p
-- << 8;
272 alimb
|= (mpi_limb_t
) *p
-- << 16;
273 alimb
|= (mpi_limb_t
) *p
-- << 24;
274 #elif BYTES_PER_MPI_LIMB == 8
275 alimb
= (mpi_limb_t
) *p
--;
276 alimb
|= (mpi_limb_t
) *p
-- << 8;
277 alimb
|= (mpi_limb_t
) *p
-- << 16;
278 alimb
|= (mpi_limb_t
) *p
-- << 24;
279 alimb
|= (mpi_limb_t
) *p
-- << 32;
280 alimb
|= (mpi_limb_t
) *p
-- << 40;
281 alimb
|= (mpi_limb_t
) *p
-- << 48;
282 alimb
|= (mpi_limb_t
) *p
-- << 56;
284 #error please implement for this limb size.
289 #if BYTES_PER_MPI_LIMB == 4
292 alimb
|= (mpi_limb_t
) *p
-- << 8;
294 alimb
|= (mpi_limb_t
) *p
-- << 16;
296 alimb
|= (mpi_limb_t
) *p
-- << 24;
297 #elif BYTES_PER_MPI_LIMB == 8
298 alimb
= (mpi_limb_t
) *p
--;
300 alimb
|= (mpi_limb_t
) *p
-- << 8;
302 alimb
|= (mpi_limb_t
) *p
-- << 16;
304 alimb
|= (mpi_limb_t
) *p
-- << 24;
306 alimb
|= (mpi_limb_t
) *p
-- << 32;
308 alimb
|= (mpi_limb_t
) *p
-- << 40;
310 alimb
|= (mpi_limb_t
) *p
-- << 48;
312 alimb
|= (mpi_limb_t
) *p
-- << 56;
314 #error please implement for this limb size.
321 pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i
,
327 EXPORT_SYMBOL_GPL(mpi_set_buffer
);
330 * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first)
332 * This function works in the same way as the mpi_read_buffer, but it
333 * takes an sgl instead of u8 * buf.
335 * @a: a multi precision integer
336 * @sgl: scatterlist to write to. Needs to be at least
337 * mpi_get_size(a) long.
338 * @nbytes: in/out param - it has the be set to the maximum number of
339 * bytes that can be written to sgl. This has to be at least
340 * the size of the integer a. On return it receives the actual
341 * length of the data written on success or the data that would
342 * be written if buffer was too small.
343 * @sign: if not NULL, it will be set to the sign of a.
345 * Return: 0 on success or error code in case of error
347 int mpi_write_to_sgl(MPI a
, struct scatterlist
*sgl
, unsigned *nbytes
,
351 #if BYTES_PER_MPI_LIMB == 4
353 #elif BYTES_PER_MPI_LIMB == 8
356 #error please implement for this limb size.
358 unsigned int n
= mpi_get_size(a
);
359 int i
, x
, y
= 0, lzeros
, buf_len
;
367 lzeros
= count_lzeros(a
);
369 if (*nbytes
< n
- lzeros
) {
370 *nbytes
= n
- lzeros
;
374 *nbytes
= n
- lzeros
;
375 buf_len
= sgl
->length
;
378 for (i
= a
->nlimbs
- 1 - lzeros
/ BYTES_PER_MPI_LIMB
,
379 lzeros
%= BYTES_PER_MPI_LIMB
;
381 #if BYTES_PER_MPI_LIMB == 4
382 alimb
= cpu_to_be32(a
->d
[i
]);
383 #elif BYTES_PER_MPI_LIMB == 8
384 alimb
= cpu_to_be64(a
->d
[i
]);
386 #error please implement for this limb size.
393 p
= (u8
*)&alimb
+ y
;
395 for (x
= 0; x
< sizeof(alimb
) - y
; x
++) {
400 buf_len
= sgl
->length
;
410 EXPORT_SYMBOL_GPL(mpi_write_to_sgl
);
413 * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with
416 * This function works in the same way as the mpi_read_raw_data, but it
417 * takes an sgl instead of void * buffer. i.e. it allocates
418 * a new MPI and reads the content of the sgl to the MPI.
420 * @sgl: scatterlist to read from
421 * @nbytes: number of bytes to read
423 * Return: Pointer to a new MPI or NULL on error
425 MPI
mpi_read_raw_from_sgl(struct scatterlist
*sgl
, unsigned int nbytes
)
427 struct scatterlist
*sg
;
428 int x
, i
, j
, z
, lzeros
, ents
;
429 unsigned int nbits
, nlimbs
;
434 ents
= sg_nents(sgl
);
436 for_each_sg(sgl
, sg
, ents
, i
) {
437 const u8
*buff
= sg_virt(sg
);
438 int len
= sg
->length
;
440 while (len
&& !*buff
) {
457 if (nbits
> MAX_EXTERN_MPI_BITS
) {
458 pr_info("MPI: mpi too large (%u bits)\n", nbits
);
463 nbits
-= count_leading_zeros(*(u8
*)(sg_virt(sgl
) + lzeros
)) -
466 nlimbs
= DIV_ROUND_UP(nbytes
, BYTES_PER_MPI_LIMB
);
467 val
= mpi_alloc(nlimbs
);
473 val
->nlimbs
= nlimbs
;
480 z
= BYTES_PER_MPI_LIMB
- nbytes
% BYTES_PER_MPI_LIMB
;
481 z
%= BYTES_PER_MPI_LIMB
;
483 for_each_sg(sgl
, sg
, ents
, i
) {
484 const u8
*buffer
= sg_virt(sg
) + lzeros
;
485 int len
= sg
->length
- lzeros
;
487 for (x
= 0; x
< len
; x
++) {
490 if (((z
+ x
+ 1) % BYTES_PER_MPI_LIMB
) == 0) {
500 EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl
);
This page took 0.04196 seconds and 5 git commands to generate.