tomcrypt/rand_prime.c

62 lines
1.5 KiB
C
Raw Normal View History

2004-01-25 12:40:34 -05:00
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
*
* LibTomCrypt is a library that provides various cryptographic
* algorithms in a highly modular and flexible manner.
*
* The library is free for all purposes without any express
2004-05-12 16:42:16 -04:00
* guarantee it works.
2004-01-25 12:40:34 -05:00
*
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
*/
2003-03-02 19:59:24 -05:00
#include "mycrypt.h"
#ifdef MPI
2003-12-24 13:59:57 -05:00
struct rng_data {
prng_state *prng;
int wprng;
};
static int rand_prime_helper(unsigned char *dst, int len, void *dat)
{
return (int)prng_descriptor[((struct rng_data *)dat)->wprng].read(dst, len, ((struct rng_data *)dat)->prng);
}
2003-03-02 19:59:24 -05:00
int rand_prime(mp_int *N, long len, prng_state *prng, int wprng)
{
2003-12-24 13:59:57 -05:00
struct rng_data rng;
int type, err;
2003-03-02 19:59:24 -05:00
_ARGCHK(N != NULL);
/* allow sizes between 2 and 256 bytes for a prime size */
2004-05-12 16:42:16 -04:00
if (len < 16 || len > 4096) {
2003-03-02 19:59:24 -05:00
return CRYPT_INVALID_PRIME_SIZE;
}
2003-12-24 13:59:57 -05:00
/* valid PRNG? Better be! */
2003-03-02 20:02:42 -05:00
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
return err;
2003-03-02 19:59:24 -05:00
}
2003-12-24 13:59:57 -05:00
/* setup our callback data, then world domination! */
rng.prng = prng;
rng.wprng = wprng;
2003-03-02 19:59:24 -05:00
2003-12-24 13:59:57 -05:00
/* get type */
if (len < 0) {
2004-05-12 16:42:16 -04:00
type = LTM_PRIME_BBS;
2003-12-24 13:59:57 -05:00
len = -len;
} else {
type = 0;
2003-03-02 19:59:24 -05:00
}
2003-12-24 13:59:57 -05:00
/* New prime generation makes the code even more cryptoish-insane. Do you know what this means!!!
-- Gir: Yeah, oh wait, er, no.
*/
2004-05-30 22:36:47 -04:00
return mpi_to_ltc_error(mp_prime_random_ex(N, mp_prime_rabin_miller_trials(len), len, type, rand_prime_helper, &rng));
2003-03-02 19:59:24 -05:00
}
#endif