Added silc_mp_suninit. silc_rng_get_rn_data changed non-allocating
authorPekka Riikonen <priikone@silcnet.org>
Sun, 8 Jul 2007 17:28:58 +0000 (17:28 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sun, 8 Jul 2007 17:28:58 +0000 (17:28 +0000)
one.

lib/silcmath/mp_tma.c
lib/silcmath/silcmp.h
lib/silcmath/silcprimegen.c

index e4e123eea2f6f55aa56bf4437c7da10a2db80b1c..2abf8f668408fbb48b26e881d04b156d99ad9965 100644 (file)
@@ -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);
index 313e7548a6c07afa85955267e2217d78ed52e758..662de1720c0072cbfa3f64ff6f623a1c004081d3 100644 (file)
@@ -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
index ed9b8848aaa908c7f071776c930e0541d6ddceea..1b56acd8fa62d742b765f5147f9dc7a2e6ab259f 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  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);