3
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
|
2 * |
|
3 * LibTomCrypt is a library that provides various cryptographic |
|
4 * algorithms in a highly modular and flexible manner. |
|
5 * |
|
6 * The library is free for all purposes without any express |
|
7 * guarantee it works. |
|
8 * |
|
9 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 #include "mycrypt.h" |
|
12 |
|
13 #ifdef MPI |
|
14 |
|
15 struct rng_data { |
|
16 prng_state *prng; |
|
17 int wprng; |
|
18 }; |
|
19 |
|
20 static int rand_prime_helper(unsigned char *dst, int len, void *dat) |
|
21 { |
|
22 return (int)prng_descriptor[((struct rng_data *)dat)->wprng].read(dst, len, ((struct rng_data *)dat)->prng); |
|
23 } |
|
24 |
|
25 int rand_prime(mp_int *N, long len, prng_state *prng, int wprng) |
|
26 { |
|
27 struct rng_data rng; |
|
28 int type, err; |
|
29 |
|
30 _ARGCHK(N != NULL); |
|
31 |
|
32 /* allow sizes between 2 and 256 bytes for a prime size */ |
|
33 if (len < 16 || len > 4096) { |
|
34 return CRYPT_INVALID_PRIME_SIZE; |
|
35 } |
|
36 |
|
37 /* valid PRNG? Better be! */ |
|
38 if ((err = prng_is_valid(wprng)) != CRYPT_OK) { |
|
39 return err; |
|
40 } |
|
41 |
|
42 /* setup our callback data, then world domination! */ |
|
43 rng.prng = prng; |
|
44 rng.wprng = wprng; |
|
45 |
|
46 /* get type */ |
|
47 if (len < 0) { |
|
48 type = LTM_PRIME_BBS; |
|
49 len = -len; |
|
50 } else { |
|
51 type = 0; |
|
52 } |
|
53 |
|
54 /* New prime generation makes the code even more cryptoish-insane. Do you know what this means!!! |
|
55 -- Gir: Yeah, oh wait, er, no. |
|
56 */ |
15
|
57 return mpi_to_ltc_error(mp_prime_random_ex(N, mp_prime_rabin_miller_trials(len), len, type, rand_prime_helper, &rng)); |
3
|
58 } |
|
59 |
|
60 #endif |
|
61 |