/* * mechanisms for preshared keys (public, private, and preshared secrets) * * this is the library for reading (and later, writing!) the ipsec.secrets * files. * * Copyright (C) 1998-2004 D. Hugh Redelmeier. * Copyright (C) 2005 Michael Richardson * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. See . * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. */ #include #include #include #include #include #include #include #include #include "sysdep.h" #include "lswlog.h" #include "constants.h" #include "lswalloc.h" #include "mpzfuncs.h" /* * Convert MP_INT to network form (binary octets, big-endian). * We do the alloc_bytes(); caller must eventually do pfree. */ static chunk_t mpz_to_n(const MP_INT *mp, size_t bytes) { chunk_t r; MP_INT temp1, temp2; int i; r.len = bytes; r.ptr = alloc_bytes(r.len, "host representation of large integer"); mpz_init(&temp1); mpz_init(&temp2); mpz_set(&temp1, mp); for (i = r.len - 1; i >= 0; i--) { r.ptr[i] = mpz_mdivmod_ui(&temp2, NULL, &temp1, 1 << BITS_PER_BYTE); mpz_set(&temp1, &temp2); } passert(mpz_sgn(&temp1) == 0); /* we must have done all the bits */ mpz_clear(&temp1); mpz_clear(&temp2); return r; } chunk_t mpz_to_n_autosize(const MP_INT *mp) { int bytes = (mpz_sizeinbase(mp, 2) + 7) / 8; return mpz_to_n(mp, bytes); } /* * Convert network form (binary bytes, big-endian) to MP_INT. * The *mp must not be previously mpz_inited. */ void n_to_mpz(MP_INT *mp, const u_char *nbytes, size_t nlen) { size_t i; mpz_init_set_ui(mp, 0); for (i = 0; i != nlen; i++) { mpz_mul_ui(mp, mp, 1 << BITS_PER_BYTE); mpz_add_ui(mp, mp, nbytes[i]); } }