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
13 Secure PRNG, Tom St Denis
16 /* A secure PRNG using the RNG functions. Basically this is a
17 * wrapper that allows you to use a secure RNG as a PRNG
18 * in the various other functions.
23 const struct ltc_prng_descriptor sprng_desc =
38 @param prng [out] The PRNG state to initialize
39 @return CRYPT_OK if successful
41 int sprng_start(prng_state *prng)
43 LTC_UNUSED_PARAM(prng);
48 Add entropy to the PRNG state
49 @param in The data to add
50 @param inlen Length of the data to add
51 @param prng PRNG state to update
52 @return CRYPT_OK if successful
54 int sprng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
57 LTC_UNUSED_PARAM(inlen);
58 LTC_UNUSED_PARAM(prng);
63 Make the PRNG ready to read from
64 @param prng The PRNG to make active
65 @return CRYPT_OK if successful
67 int sprng_ready(prng_state *prng)
69 LTC_UNUSED_PARAM(prng);
75 @param out Destination
76 @param outlen Length of output
77 @param prng The active PRNG to read from
78 @return Number of octets read
80 unsigned long sprng_read(unsigned char *out, unsigned long outlen, prng_state *prng)
82 LTC_ARGCHK(out != NULL);
83 LTC_UNUSED_PARAM(prng);
84 return rng_get_bytes(out, outlen, NULL);
89 @param prng The PRNG to terminate
90 @return CRYPT_OK if successful
92 int sprng_done(prng_state *prng)
94 LTC_UNUSED_PARAM(prng);
100 @param out [out] Destination
101 @param outlen [in/out] Max size and resulting size of the state
102 @param prng The PRNG to export
103 @return CRYPT_OK if successful
105 int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
107 LTC_ARGCHK(outlen != NULL);
108 LTC_UNUSED_PARAM(out);
109 LTC_UNUSED_PARAM(prng);
117 @param in The PRNG state
118 @param inlen Size of the state
119 @param prng The PRNG to import
120 @return CRYPT_OK if successful
122 int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
124 LTC_UNUSED_PARAM(in);
125 LTC_UNUSED_PARAM(inlen);
126 LTC_UNUSED_PARAM(prng);
132 @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
140 unsigned char en[] = { 0x01, 0x02, 0x03, 0x04 };
141 unsigned char out[1000];
144 if ((err = sprng_start(&st)) != CRYPT_OK) return err;
145 if ((err = sprng_add_entropy(en, sizeof(en), &st)) != CRYPT_OK) return err;
146 if ((err = sprng_ready(&st)) != CRYPT_OK) return err;
147 if (sprng_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
148 if ((err = sprng_done(&st)) != CRYPT_OK) return err;
159 /* ref: $Format:%D$ */
160 /* git commit: $Format:%H$ */
161 /* commit time: $Format:%ai$ */