X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcmath%2Fsilcprimegen.c;h=515f2b400fc8a0dc6d5c724f43e2fd9ddd260a25;hb=a818c5b5411bbc4436d1c5f011236985c96bb787;hp=737a09ad8bcf97eccae4a4982b29c45214dce3c2;hpb=bb1973faaa81ead23b3d5e05b45cddd8a47d51f7;p=silc.git diff --git a/lib/silcmath/silcprimegen.c b/lib/silcmath/silcprimegen.c index 737a09ad..515f2b40 100644 --- a/lib/silcmath/silcprimegen.c +++ b/lib/silcmath/silcprimegen.c @@ -17,29 +17,11 @@ GNU General Public License for more details. */ -/* - * Created: Mon Dec 8 16:35:37 GMT+0200 1997 - */ -/* - * $Id$ - * $Log$ - * Revision 1.2 2000/07/05 06:06:52 priikone - * Global cosmetic change. - * - * Revision 1.1.1.1 2000/06/27 11:36:51 priikone - * Importet from internal CVS/Added Log headers. - * - * - */ +/* Created: Mon Dec 8 16:35:37 GMT+0200 1997 */ +/* $Id$ */ #include "silcincludes.h" -/* XXX This must be temporary solution!! yucky! */ -/* Global random pool used for all prime generation. All primes generated - in SILC uses this same pool. Before primes can be generated one must - call silc_math_primegen_init. */ -static SilcRng primegen_rng; - /* Fixed primetable for small prime division. We use this primetable to test if possible prime is divisible any of these. Primetable is NULL @@ -71,7 +53,7 @@ static SilcRng primegen_rng; */ -static unsigned int primetable[] = +static SilcUInt32 primetable[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, @@ -213,31 +195,37 @@ static unsigned int primetable[] = If argument verbose is TRUE this will display some status information about the progress of generation. */ -int silc_math_gen_prime(SilcInt *prime, unsigned int bits, int verbose) +bool silc_math_gen_prime(SilcMPInt *prime, SilcUInt32 bits, bool verbose) { - unsigned char *numbuf; - unsigned int i, b, k; - unsigned int *spmods; - SilcInt r, base, tmp, tmp2, oprime; - - /* XXX */ - assert(primegen_rng != NULL); + unsigned char *numbuf = NULL; + SilcUInt32 i, b, k; + SilcUInt32 *spmods; + SilcMPInt r, base, tmp, tmp2, oprime; silc_mp_init(&r); - silc_mp_init_set_ui(&base, 2); + silc_mp_init(&base); silc_mp_init(&tmp); silc_mp_init(&tmp2); silc_mp_init(&oprime); + silc_mp_set_ui(&base, 2); + SILC_LOG_DEBUG(("Generating new prime")); - /* Get random number */ - numbuf = silc_rng_get_rn_string(primegen_rng, (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 */ @@ -246,11 +234,11 @@ int silc_math_gen_prime(SilcInt *prime, unsigned int bits, int verbose) /* Number could be even number, so we'll make it odd. */ silc_mp_set_ui(&tmp, 1); - silc_mp_ior(prime, prime, &tmp); /* OR operator */ + silc_mp_or(prime, prime, &tmp); /* OR operator */ /* Init modulo table with the prime candidate and the primes in the primetable. */ - spmods = silc_calloc(1, sizeof(primetable) * sizeof(unsigned int)); + 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); @@ -284,7 +272,7 @@ int silc_math_gen_prime(SilcInt *prime, unsigned int bits, int verbose) /* Does the prime pass the Fermat's prime test. * r = 2 ^ p mod p, if r == 2, then p is probably a prime. */ - silc_mp_powm(&r, &base, &oprime, &oprime); + silc_mp_pow_mod(&r, &base, &oprime, &oprime); if (silc_mp_cmp_ui(&r, 2) != 0) { if (verbose) { printf("."); @@ -310,11 +298,11 @@ int silc_math_gen_prime(SilcInt *prime, unsigned int bits, int verbose) } silc_free(spmods); - silc_mp_clear(&r); - silc_mp_clear(&base); - silc_mp_clear(&tmp); - silc_mp_clear(&tmp2); - silc_mp_clear(&oprime); + silc_mp_uninit(&r); + silc_mp_uninit(&base); + silc_mp_uninit(&tmp); + silc_mp_uninit(&tmp2); + silc_mp_uninit(&oprime); return TRUE; } @@ -322,14 +310,15 @@ int silc_math_gen_prime(SilcInt *prime, unsigned int bits, int verbose) /* Performs primality testings for given number. Returns TRUE if the number is probably a prime. */ -int silc_math_prime_test(SilcInt *p) +bool silc_math_prime_test(SilcMPInt *p) { - SilcInt r, base, tmp; + SilcMPInt r, base, tmp; int i, ret = 0; silc_mp_init(&r); silc_mp_init(&tmp); - silc_mp_init_set_ui(&base, 2); + silc_mp_init(&base); + silc_mp_set_ui(&base, 2); SILC_LOG_DEBUG(("Testing probability of prime")); @@ -346,13 +335,13 @@ int silc_math_prime_test(SilcInt *p) /* Does the prime pass the Fermat's prime test. * r = 2 ^ p mod p, if r == 2, then p is probably a prime. */ - silc_mp_powm(&r, &base, p, p); + silc_mp_pow_mod(&r, &base, p, p); if (silc_mp_cmp_ui(&r, 2) != 0) ret = -1; - silc_mp_clear(&r); - silc_mp_clear(&tmp); - silc_mp_clear(&base); + silc_mp_uninit(&r); + silc_mp_uninit(&tmp); + silc_mp_uninit(&base); if (ret) return FALSE; @@ -360,24 +349,3 @@ int silc_math_prime_test(SilcInt *p) /* Number is probably a prime */ return TRUE; } - -/* XXX This must temporary solution!! */ -/* Initializes the random pool used to generated primes */ - -void silc_math_primegen_init() -{ - SILC_LOG_DEBUG(("Start")); - - if (primegen_rng == NULL) { - primegen_rng = silc_rng_alloc(); - silc_rng_init(primegen_rng); - } -} - -/* XXX This must temporary solution!! */ -/* Uninitializes random pool */ - -void silc_math_primegen_uninit() -{ - silc_rng_free(primegen_rng); -}