comparison libtomcrypt/src/prngs/rng_make_prng.c @ 285:1b9e69c058d2

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3) to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 13:23:58 +0000
parents
children 0cbe8f6dbf9e
comparison
equal deleted inserted replaced
281:997e6f7dc01e 285:1b9e69c058d2
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 "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.3 $ */
69 /* $Date: 2005/05/05 14:35:59 $ */