comparison libtomcrypt/src/prngs/rng_make_prng.c @ 297:79bf1023cf11 agent-client

propagate from branch 'au.asn.ucc.matt.dropbear' (head 0501e6f661b5415eb76f3b312d183c3adfbfb712) to branch 'au.asn.ucc.matt.dropbear.cli-agent' (head 01038174ec27245b51bd43a66c01ad930880f67b)
author Matt Johnston <matt@ucc.asn.au>
date Tue, 21 Mar 2006 16:20:59 +0000
parents 1b9e69c058d2
children 0cbe8f6dbf9e
comparison
equal deleted inserted replaced
225:ca7e76d981d9 297:79bf1023cf11
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 $ */