1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
15 Generate a random prime, Tom St Denis
20 int rand_prime(void *N, long len, prng_state *prng, int wprng)
25 LTC_ARGCHK(N != NULL);
35 /* allow sizes between 2 and 512 bytes for a prime size */
36 if (len < 2 || len > 512) {
37 return CRYPT_INVALID_PRIME_SIZE;
40 /* valid PRNG? Better be! */
41 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
45 /* allocate buffer to work with */
46 buf = XCALLOC(1, len);
53 if (prng_descriptor[wprng].read(buf, len, prng) != (unsigned long)len) {
55 return CRYPT_ERROR_READPRNG;
59 buf[0] |= 0x80 | 0x40;
60 buf[len-1] |= 0x01 | ((type & USE_BBS) ? 0x02 : 0x00);
63 if ((err = mp_read_unsigned_bin(N, buf, len)) != CRYPT_OK) {
69 if ((err = mp_prime_is_prime(N, 8, &res)) != CRYPT_OK) {
73 } while (res == LTC_MP_NO);
75 #ifdef LTC_CLEAN_STACK
85 /* $Source: /cvs/libtom/libtomcrypt/src/math/rand_prime.c,v $ */
86 /* $Revision: 1.6 $ */
87 /* $Date: 2006/03/31 14:15:35 $ */