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 /* portable way to get secure random bits to feed a PRNG */ |
|
12 #include "mycrypt.h" |
|
13 |
|
14 int rng_make_prng(int bits, int wprng, prng_state *prng, |
|
15 void (*callback)(void)) |
|
16 { |
|
17 unsigned char buf[256]; |
|
18 int err; |
|
19 |
|
20 _ARGCHK(prng != NULL); |
|
21 |
|
22 /* check parameter */ |
|
23 if ((err = prng_is_valid(wprng)) != CRYPT_OK) { |
|
24 return err; |
|
25 } |
|
26 |
|
27 if (bits < 64 || bits > 1024) { |
|
28 return CRYPT_INVALID_PRNGSIZE; |
|
29 } |
|
30 |
|
31 if ((err = prng_descriptor[wprng].start(prng)) != CRYPT_OK) { |
|
32 return err; |
|
33 } |
|
34 |
|
35 bits = ((bits/8)+((bits&7)!=0?1:0)) * 2; |
|
36 if (rng_get_bytes(buf, (unsigned long)bits, callback) != (unsigned long)bits) { |
|
37 return CRYPT_ERROR_READPRNG; |
|
38 } |
|
39 |
|
40 if ((err = prng_descriptor[wprng].add_entropy(buf, (unsigned long)bits, prng)) != CRYPT_OK) { |
|
41 return err; |
|
42 } |
|
43 |
|
44 if ((err = prng_descriptor[wprng].ready(prng)) != CRYPT_OK) { |
|
45 return err; |
|
46 } |
|
47 |
|
48 #ifdef CLEAN_STACK |
|
49 zeromem(buf, sizeof(buf)); |
|
50 #endif |
|
51 return CRYPT_OK; |
|
52 } |
|
53 |