]>
git.ipfire.org Git - people/ms/strongswan.git/blob - programs/pluto/mp_defs.c
1 /* some multiprecision utilities
2 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * RCSID $Id: mp_defs.c,v 1.1 2006/01/05 12:37:11 as Exp $
19 #include "constants.h"
24 /* Convert MP_INT to network form (binary octets, big-endian).
25 * We do the malloc; caller must eventually do free.
28 mpz_to_n(const MP_INT
*mp
, size_t bytes
)
35 r
.ptr
= alloc_bytes(r
.len
, "host representation of large integer");
42 for (i
= r
.len
-1; i
>= 0; i
--)
44 r
.ptr
[i
] = mpz_mdivmod_ui(&temp2
, NULL
, &temp1
, 1 << BITS_PER_BYTE
);
45 mpz_set(&temp1
, &temp2
);
48 passert(mpz_sgn(&temp1
) == 0); /* we must have done all the bits */
55 /* Convert network form (binary bytes, big-endian) to MP_INT.
56 * The *mp must not be previously mpz_inited.
59 n_to_mpz(MP_INT
*mp
, const u_char
*nbytes
, size_t nlen
)
63 mpz_init_set_ui(mp
, 0);
65 for (i
= 0; i
!= nlen
; i
++)
67 mpz_mul_ui(mp
, mp
, 1 << BITS_PER_BYTE
);
68 mpz_add_ui(mp
, mp
, nbytes
[i
]);