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
11 #if defined(LTC_MRSA) || (!defined(LTC_NO_MATH) && !defined(LTC_NO_PRNGS))
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, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) {
73 } while (res == LTC_MP_NO);
75 #ifdef LTC_CLEAN_STACK
83 #endif /* LTC_NO_MATH */
86 /* ref: $Format:%D$ */
87 /* git commit: $Format:%H$ */
88 /* commit time: $Format:%ai$ */