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 #ifdef LTC_RNG_MAKE_PRNG
14 portable way to get secure random bits to feed a PRNG (Tom St Denis)
18 Create a PRNG from a RNG
19 @param bits Number of bits of entropy desired (64 ... 1024)
20 @param wprng Index of which PRNG to setup
21 @param prng [out] PRNG state to initialize
22 @param callback A pointer to a void function for when the RNG is slow, this can be NULL
23 @return CRYPT_OK if successful
25 int rng_make_prng(int bits, int wprng, prng_state *prng,
26 void (*callback)(void))
28 unsigned char buf[256];
31 LTC_ARGCHK(prng != NULL);
34 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
38 if (bits < 64 || bits > 1024) {
39 return CRYPT_INVALID_PRNGSIZE;
42 if ((err = prng_descriptor[wprng].start(prng)) != CRYPT_OK) {
46 bits = ((bits/8)+((bits&7)!=0?1:0)) * 2;
47 if (rng_get_bytes(buf, (unsigned long)bits, callback) != (unsigned long)bits) {
48 return CRYPT_ERROR_READPRNG;
51 if ((err = prng_descriptor[wprng].add_entropy(buf, (unsigned long)bits, prng)) != CRYPT_OK) {
55 if ((err = prng_descriptor[wprng].ready(prng)) != CRYPT_OK) {
59 #ifdef LTC_CLEAN_STACK
60 zeromem(buf, sizeof(buf));
64 #endif /* #ifdef LTC_RNG_MAKE_PRNG */
67 /* ref: $Format:%D$ */
68 /* git commit: $Format:%H$ */
69 /* commit time: $Format:%ai$ */