Added SILC Thread Queue API
[silc.git] / lib / silcmath / silcprimegen.c
index 80bbf54eaea4a0124df4ddac6f1b3f1a56133dd8..b2fe429db40e9ebcd574c204541365b266d9c1f4 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
@@ -19,7 +19,7 @@
 /* Created: Mon Dec  8 16:35:37 GMT+0200 1997 */
 /* $Id$ */
 
-#include "silcincludes.h"
+#include "silc.h"
 
 /*
    Fixed primetable for small prime division. We use this primetable to
@@ -194,14 +194,14 @@ static SilcUInt32 primetable[] =
    If argument verbose is TRUE this will display some status information
    about the progress of generation. */
 
-bool silc_math_gen_prime(SilcMPInt *prime, SilcUInt32 bits, bool verbose,
-                        SilcRng rng)
+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;
-  bool valid = FALSE;
+  SilcBool valid = FALSE;
 
   silc_mp_init(&r);
   silc_mp_init(&base);
@@ -214,13 +214,15 @@ bool silc_math_gen_prime(SilcMPInt *prime, SilcUInt32 bits, bool verbose,
   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);
@@ -321,7 +323,7 @@ bool silc_math_gen_prime(SilcMPInt *prime, SilcUInt32 bits, bool verbose,
 /* Performs primality testings for given number. Returns TRUE if the
    number is probably a prime. */
 
-bool silc_math_prime_test(SilcMPInt *p)
+SilcBool silc_math_prime_test(SilcMPInt *p)
 {
   SilcMPInt r, base, tmp;
   int i, ret = 0;
@@ -339,8 +341,13 @@ bool silc_math_prime_test(SilcMPInt *p)
     silc_mp_mod_ui(&tmp, p, primetable[i]);
 
     /* If mod is 0, the number is composite */
-    if (silc_mp_cmp_ui(&tmp, 0) == 0)
-      ret = -1;
+    if (silc_mp_cmp_ui(&tmp, 0) == 0) {
+      SILC_LOG_DEBUG(("Number is not prime"));
+      silc_mp_uninit(&r);
+      silc_mp_uninit(&tmp);
+      silc_mp_uninit(&base);
+      return FALSE;
+    }
   }
 
   /* Does the prime pass the Fermat's prime test.
@@ -354,8 +361,10 @@ bool silc_math_prime_test(SilcMPInt *p)
   silc_mp_uninit(&tmp);
   silc_mp_uninit(&base);
 
-  if (ret)
+  if (ret) {
+    SILC_LOG_DEBUG(("Number is not prime"));
     return FALSE;
+  }
 
   /* Number is probably a prime */
   return TRUE;