Integer type name change.
[silc.git] / lib / silcmath / silcprimegen.c
index e868637c66fbb99df9d0ced69a24b72564e4c5dd..515f2b400fc8a0dc6d5c724f43e2fd9ddd260a25 100644 (file)
@@ -53,7 +53,7 @@
 
 */
 
-static uint32 primetable[] =
+static SilcUInt32 primetable[] =
 {
   2, 3, 5, 7, 11, 13, 17, 19,
   23, 29, 31, 37, 41, 43, 47, 53,
@@ -195,11 +195,11 @@ static uint32 primetable[] =
    If argument verbose is TRUE this will display some status information
    about the progress of generation. */
 
-int silc_math_gen_prime(SilcMPInt *prime, uint32 bits, int verbose)
+bool silc_math_gen_prime(SilcMPInt *prime, SilcUInt32 bits, bool verbose)
 {
-  unsigned char *numbuf;
-  uint32 i, b, k;
-  uint32 *spmods;
+  unsigned char *numbuf = NULL;
+  SilcUInt32 i, b, k;
+  SilcUInt32 *spmods;
   SilcMPInt r, base, tmp, tmp2, oprime;
 
   silc_mp_init(&r);
@@ -212,13 +212,20 @@ int silc_math_gen_prime(SilcMPInt *prime, uint32 bits, int verbose)
 
   SILC_LOG_DEBUG(("Generating new prime"));
 
-  /* Get random number */
-  numbuf = silc_rng_global_get_rn_string((bits / 8));
-  if (!numbuf)
-    return FALSE;
+  /* Get random number and assure that the first digit is not zero since
+     our conversion routines does not like the first digit being zero. */
+  do {
+    if (numbuf) {
+      memset(numbuf, 0, (bits / 8));
+      silc_free(numbuf);
+    }
+    numbuf = silc_rng_global_get_rn_string((bits / 8));
+    if (!numbuf)
+      return FALSE;
+  } while (numbuf[0] == '0');
 
   /* Convert into MP and set the size */
-  silc_mp_set_str(prime, numbuf, 16);
+  silc_mp_set_str(prime, numbuf, 16);  
   silc_mp_mod_2exp(prime, prime, bits);
 
   /* Empty buffer */
@@ -231,7 +238,7 @@ int silc_math_gen_prime(SilcMPInt *prime, uint32 bits, int verbose)
 
   /* Init modulo table with the prime candidate and the primes
      in the primetable. */
-  spmods = silc_calloc(1, sizeof(primetable) * sizeof(uint32));
+  spmods = silc_calloc(1, sizeof(primetable) * sizeof(SilcUInt32));
   for (i = 0; primetable[i] != 0; i++) {
     silc_mp_mod_ui(&tmp, prime, primetable[i]);
     spmods[i] = silc_mp_get_ui(&tmp);
@@ -303,7 +310,7 @@ int silc_math_gen_prime(SilcMPInt *prime, uint32 bits, int verbose)
 /* Performs primality testings for given number. Returns TRUE if the 
    number is probably a prime. */
 
-int silc_math_prime_test(SilcMPInt *p)
+bool silc_math_prime_test(SilcMPInt *p)
 {
   SilcMPInt r, base, tmp;
   int i, ret = 0;