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, tomstdenis (at) gmail.com, http://libtomcrypt.com 10 */ 11 #include "tomcrypt.h" 12 13 /** 14 @file rng_make_prng.c 15 portable way to get secure random bits to feed a PRNG (Tom St Denis) 16 */ 17 18 /** 19 Create a PRNG from a RNG 20 @param bits Number of bits of entropy desired (64 ... 1024) 21 @param wprng Index of which PRNG to setup 22 @param prng [out] PRNG state to initialize 23 @param callback A pointer to a void function for when the RNG is slow, this can be NULL 24 @return CRYPT_OK if successful 25 */ 26 int rng_make_prng(int bits, int wprng, prng_state *prng, 27 void (*callback)(void)) 28 { 29 unsigned char buf[256]; 30 int err; 31 32 LTC_ARGCHK(prng != NULL); 33 34 /* check parameter */ 35 if ((err = prng_is_valid(wprng)) != CRYPT_OK) { 36 return err; 37 } 38 39 if (bits < 64 || bits > 1024) { 40 return CRYPT_INVALID_PRNGSIZE; 41 } 42 43 if ((err = prng_descriptor[wprng].start(prng)) != CRYPT_OK) { 44 return err; 45 } 46 47 bits = ((bits/8)+((bits&7)!=0?1:0)) * 2; 48 if (rng_get_bytes(buf, (unsigned long)bits, callback) != (unsigned long)bits) { 49 return CRYPT_ERROR_READPRNG; 50 } 51 52 if ((err = prng_descriptor[wprng].add_entropy(buf, (unsigned long)bits, prng)) != CRYPT_OK) { 53 return err; 54 } 55 56 if ((err = prng_descriptor[wprng].ready(prng)) != CRYPT_OK) { 57 return err; 58 } 59 60 #ifdef LTC_CLEAN_STACK 61 zeromem(buf, sizeof(buf)); 62 #endif 63 return CRYPT_OK; 64 } 65 66 67 /* $Source: /cvs/libtom/libtomcrypt/src/prngs/rng_make_prng.c,v $ */ 68 /* $Revision: 1.4 $ */ 69 /* $Date: 2006/03/31 14:15:35 $ */ 70