From 990808eb3f1546e5c8b2b4b56ca67dd56a521958 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Sun, 8 Jul 2007 17:28:58 +0000 Subject: [PATCH] Added silc_mp_suninit. silc_rng_get_rn_data changed non-allocating one. --- lib/silcmath/mp_tma.c | 6 ++++++ lib/silcmath/silcmp.h | 30 +++++++++++++++++++++++++++++- lib/silcmath/silcprimegen.c | 14 ++++++++------ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/silcmath/mp_tma.c b/lib/silcmath/mp_tma.c index e4e123ee..2abf8f66 100644 --- a/lib/silcmath/mp_tma.c +++ b/lib/silcmath/mp_tma.c @@ -38,6 +38,12 @@ void silc_mp_uninit(SilcMPInt *mp) tma_mp_clear(mp); } +void silc_mp_suninit(SilcStack stack, SilcMPInt *mp) +{ + if (!stack) + tma_mp_clear(mp); +} + size_t silc_mp_size(SilcMPInt *mp) { return tma_mp_unsigned_bin_size(mp); diff --git a/lib/silcmath/silcmp.h b/lib/silcmath/silcmp.h index 313e7548..662de172 100644 --- a/lib/silcmath/silcmp.h +++ b/lib/silcmath/silcmp.h @@ -68,13 +68,28 @@ typedef SILC_MP_INT SilcMPInt; * * DESCRIPTION * - * Initializes the SilcMPInt *that is the actual MP Integer. + * Initializes the SilcMPInt that is the actual MP Integer. * This must be called before any of the silc_mp_ routines can be * used. The integer is uninitialized with the silc_mp_uninit function. * ***/ void silc_mp_init(SilcMPInt *mp); +/****f* silcmath/SilcMPAPI/silc_mp_sinit + * + * SYNOPSIS + * + * SilcBool silc_mp_sinit(SilcStack stack, SilcMPInt *mp); + * + * DESCRIPTION + * + * Initializes the SilcMPInt that is the actual MP Integer. + * This must be called before any of the silc_mp_ routines can be + * used. The integer is uninitialized with the silc_mp_suninit function. + * If `stack' is non-NULL it will be used as the memory source. If it + * is NULL, this call is equivalent to silc_mp_init. + * + ***/ SilcBool silc_mp_sinit(SilcStack stack, SilcMPInt *mp); /****f* silcmath/SilcMPAPI/silc_mp_uninit @@ -90,6 +105,19 @@ SilcBool silc_mp_sinit(SilcStack stack, SilcMPInt *mp); ***/ void silc_mp_uninit(SilcMPInt *mp); +/****f* silcmath/SilcMPAPI/silc_mp_suninit + * + * SYNOPSIS + * + * void silc_mp_suninit(SilcStack stack, SilcMPInt *mp); + * + * DESCRIPTION + * + * Uninitializes the MP Integer. + * + ***/ +void silc_mp_suninit(SilcStack stack, SilcMPInt *mp); + /****f* silcmath/SilcMPAPI/silc_mp_size * * SYNOPSIS diff --git a/lib/silcmath/silcprimegen.c b/lib/silcmath/silcprimegen.c index ed9b8848..1b56acd8 100644 --- a/lib/silcmath/silcprimegen.c +++ b/lib/silcmath/silcprimegen.c @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 1997 - 2005 Pekka Riikonen + Copyright (C) 1997 - 2007 Pekka Riikonen 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 @@ -197,7 +197,7 @@ static SilcUInt32 primetable[] = SilcBool silc_math_gen_prime(SilcMPInt *prime, SilcUInt32 bits, SilcBool verbose, SilcRng rng) { - unsigned char *numbuf = NULL; + unsigned char *numbuf; SilcUInt32 i, b, k; SilcUInt32 *spmods; SilcMPInt r, base, tmp, tmp2, oprime; @@ -214,13 +214,15 @@ SilcBool silc_math_gen_prime(SilcMPInt *prime, SilcUInt32 bits, SILC_LOG_DEBUG(("Generating new prime")); while (valid == FALSE) { + numbuf = silc_malloc((((bits + 7) / 8) + 1) * sizeof(*numbuf)); + if (!numbuf) + return FALSE; + /* Get random number */ if (rng) - numbuf = silc_rng_get_rn_data(rng, (bits / 8)); + silc_rng_get_rn_data(rng, (bits / 8), numbuf, (bits / 8)); else - numbuf = silc_rng_global_get_rn_data((bits / 8)); - if (!numbuf) - return FALSE; + silc_rng_global_get_rn_data(rng, (bits / 8), numbuf, (bits / 8)); /* Convert into MP and set the size */ silc_mp_bin2mp(numbuf, (bits / 8), prime); -- 2.24.0