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 "mpi-internal.h"
26 #define MAX_EXTERN_MPI_BITS 16384
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
33 MPI
mpi_read_raw_data(const void *xbuffer
, size_t nbytes
)
35 const uint8_t *buffer
= xbuffer
;
37 unsigned nbits
, nlimbs
;
41 while (nbytes
> 0 && buffer
[0] == 0) {
47 if (nbits
> MAX_EXTERN_MPI_BITS
) {
48 pr_info("MPI: mpi too large (%u bits)\n", nbits
);
52 nbits
-= count_leading_zeros(buffer
[0]);
56 nlimbs
= DIV_ROUND_UP(nbytes
, BYTES_PER_MPI_LIMB
);
57 val
= mpi_alloc(nlimbs
);
65 i
= BYTES_PER_MPI_LIMB
- nbytes
% BYTES_PER_MPI_LIMB
;
66 i
%= BYTES_PER_MPI_LIMB
;
67 for (j
= nlimbs
; j
> 0; j
--) {
69 for (; i
< BYTES_PER_MPI_LIMB
; i
++) {
79 EXPORT_SYMBOL_GPL(mpi_read_raw_data
);
81 MPI
mpi_read_from_buffer(const void *xbuffer
, unsigned *ret_nread
)
83 const uint8_t *buffer
= xbuffer
;
85 unsigned nbits
, nbytes
, nlimbs
, nread
= 0;
91 nbits
= buffer
[0] << 8 | buffer
[1];
93 if (nbits
> MAX_EXTERN_MPI_BITS
) {
94 pr_info("MPI: mpi too large (%u bits)\n", nbits
);
100 nbytes
= DIV_ROUND_UP(nbits
, 8);
101 nlimbs
= DIV_ROUND_UP(nbytes
, BYTES_PER_MPI_LIMB
);
102 val
= mpi_alloc(nlimbs
);
105 i
= BYTES_PER_MPI_LIMB
- nbytes
% BYTES_PER_MPI_LIMB
;
106 i
%= BYTES_PER_MPI_LIMB
;
108 j
= val
->nlimbs
= nlimbs
;
112 for (; i
< BYTES_PER_MPI_LIMB
; i
++) {
113 if (++nread
> *ret_nread
) {
115 ("MPI: mpi larger than buffer nread=%d ret_nread=%d\n",
130 EXPORT_SYMBOL_GPL(mpi_read_from_buffer
);
132 static int count_lzeros(MPI a
)
137 for (i
= a
->nlimbs
- 1; i
>= 0; i
--) {
140 lzeros
+= sizeof(mpi_limb_t
);
142 lzeros
+= count_leading_zeros(alimb
) / 8;
150 * mpi_read_buffer() - read MPI to a bufer provided by user (msb first)
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.
156 * @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
159 * @sign: if not NULL, it will be set to the sign of a.
161 * Return: 0 on success or error code in case of error
163 int mpi_read_buffer(MPI a
, uint8_t *buf
, unsigned buf_len
, unsigned *nbytes
,
168 unsigned int n
= mpi_get_size(a
);
177 lzeros
= count_lzeros(a
);
179 if (buf_len
< n
- lzeros
) {
180 *nbytes
= n
- lzeros
;
185 *nbytes
= n
- lzeros
;
187 for (i
= a
->nlimbs
- 1 - lzeros
/ BYTES_PER_MPI_LIMB
,
188 lzeros
%= BYTES_PER_MPI_LIMB
;
191 #if BYTES_PER_MPI_LIMB == 4
196 #elif BYTES_PER_MPI_LIMB == 8
206 #error please implement for this limb size.
210 mpi_limb_t
*limb1
= (void *)p
- sizeof(alimb
);
211 mpi_limb_t
*limb2
= (void *)p
- sizeof(alimb
)
215 lzeros
-= sizeof(alimb
);
220 EXPORT_SYMBOL_GPL(mpi_read_buffer
);
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.
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.
232 * Return: Pointer to MPI buffer or NULL on error
234 void *mpi_get_buffer(MPI a
, unsigned *nbytes
, int *sign
)
248 buf
= kmalloc(n
, GFP_KERNEL
);
253 ret
= mpi_read_buffer(a
, buf
, n
, nbytes
, sign
);
261 EXPORT_SYMBOL_GPL(mpi_get_buffer
);
264 * Use BUFFER to update MPI.
266 int mpi_set_buffer(MPI a
, const void *xbuffer
, unsigned nbytes
, int sign
)
268 const uint8_t *buffer
= xbuffer
, *p
;
273 nlimbs
= DIV_ROUND_UP(nbytes
, BYTES_PER_MPI_LIMB
);
274 if (RESIZE_IF_NEEDED(a
, nlimbs
) < 0)
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;
294 #error please implement for this limb size.
299 #if BYTES_PER_MPI_LIMB == 4
302 alimb
|= (mpi_limb_t
) *p
-- << 8;
304 alimb
|= (mpi_limb_t
) *p
-- << 16;
306 alimb
|= (mpi_limb_t
) *p
-- << 24;
307 #elif BYTES_PER_MPI_LIMB == 8
308 alimb
= (mpi_limb_t
) *p
--;
310 alimb
|= (mpi_limb_t
) *p
-- << 8;
312 alimb
|= (mpi_limb_t
) *p
-- << 16;
314 alimb
|= (mpi_limb_t
) *p
-- << 24;
316 alimb
|= (mpi_limb_t
) *p
-- << 32;
318 alimb
|= (mpi_limb_t
) *p
-- << 40;
320 alimb
|= (mpi_limb_t
) *p
-- << 48;
322 alimb
|= (mpi_limb_t
) *p
-- << 56;
324 #error please implement for this limb size.
331 pr_emerg("MPI: mpi_set_buffer: Assertion failed (%d != %d)", i
,
337 EXPORT_SYMBOL_GPL(mpi_set_buffer
);
340 * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first)
342 * This function works in the same way as the mpi_read_buffer, but it
343 * takes an sgl instead of u8 * buf.
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
351 * length of the data written on success or the data that would
352 * be written if buffer was too small.
353 * @sign: if not NULL, it will be set to the sign of a.
355 * Return: 0 on success or error code in case of error
357 int mpi_write_to_sgl(MPI a
, struct scatterlist
*sgl
, unsigned *nbytes
,
361 #if BYTES_PER_MPI_LIMB == 4
363 #elif BYTES_PER_MPI_LIMB == 8
366 #error please implement for this limb size.
368 unsigned int n
= mpi_get_size(a
);
369 int i
, x
, y
= 0, lzeros
, buf_len
;
377 lzeros
= count_lzeros(a
);
379 if (*nbytes
< n
- lzeros
) {
380 *nbytes
= n
- lzeros
;
384 *nbytes
= n
- lzeros
;
385 buf_len
= sgl
->length
;
388 for (i
= a
->nlimbs
- 1 - lzeros
/ BYTES_PER_MPI_LIMB
,
389 lzeros
%= BYTES_PER_MPI_LIMB
;
391 #if BYTES_PER_MPI_LIMB == 4
392 alimb
= cpu_to_be32(a
->d
[i
]);
393 #elif BYTES_PER_MPI_LIMB == 8
394 alimb
= cpu_to_be64(a
->d
[i
]);
396 #error please implement for this limb size.
403 p
= (u8
*)&alimb
+ y
;
405 for (x
= 0; x
< sizeof(alimb
) - y
; x
++) {
410 buf_len
= sgl
->length
;
420 EXPORT_SYMBOL_GPL(mpi_write_to_sgl
);
423 * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with
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.
430 * @sgl: scatterlist to read from
431 * @len: number of bytes to read
433 * Return: Pointer to a new MPI or NULL on error
435 MPI
mpi_read_raw_from_sgl(struct scatterlist
*sgl
, unsigned int len
)
437 struct scatterlist
*sg
;
438 int x
, i
, j
, z
, lzeros
, ents
;
439 unsigned int nbits
, nlimbs
, nbytes
;
444 ents
= sg_nents(sgl
);
446 for_each_sg(sgl
, sg
, ents
, i
) {
447 const u8
*buff
= sg_virt(sg
);
448 int len
= sg
->length
;
450 while (len
&& !*buff
) {
468 nbytes
= len
- lzeros
;
471 if (nbits
> MAX_EXTERN_MPI_BITS
) {
472 pr_info("MPI: mpi too large (%u bits)\n", nbits
);
477 nbits
-= count_leading_zeros(*(u8
*)(sg_virt(sgl
) + lzeros
));
481 nlimbs
= DIV_ROUND_UP(nbytes
, BYTES_PER_MPI_LIMB
);
482 val
= mpi_alloc(nlimbs
);
488 val
->nlimbs
= nlimbs
;
496 x
= BYTES_PER_MPI_LIMB
- nbytes
% BYTES_PER_MPI_LIMB
;
497 x
%= BYTES_PER_MPI_LIMB
;
499 for_each_sg(sgl
, sg
, ents
, i
) {
500 const u8
*buffer
= sg_virt(sg
) + lzeros
;
501 int len
= sg
->length
- lzeros
;
504 if (sg_is_last(sg
) && (len
% BYTES_PER_MPI_LIMB
))
505 len
+= BYTES_PER_MPI_LIMB
- (len
% BYTES_PER_MPI_LIMB
);
507 for (; x
< len
+ buf_shift
; x
++) {
510 if (((z
+ x
+ 1) % BYTES_PER_MPI_LIMB
) == 0) {
521 EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl
);