1 /* ---- PRNG Stuff ---- */
2 module tomcrypt.prng;
3 
4 import core.stdc.config;
5 import tomcrypt.custom;
6 import tomcrypt.tomcrypt;
7 
8 extern(C) nothrow:
9 
10 version(LTC_YARROW)
11 {
12     struct yarrow_prng 
13     {
14         int                   cipher, hash;
15         ubyte[MAXBLOCKSIZE]   pool;
16         symmetric_CTR         ctr;
17         mixin(LTC_MUTEX_TYPE("prng_lock"));
18     }
19 }
20 
21 version(LTC_RC4)
22 {
23     struct rc4_prng 
24     {
25         int x, y;
26         ubyte[256] buf;
27     }
28 }
29 
30 version(LTC_FORTUNA)
31 {
32     struct fortuna_prng 
33     {
34         hash_state[LTC_FORTUNA_POOLS] pool;     /* the  pools */
35     
36         symmetric_key skey;
37     
38         ubyte[32]       K;          /* the current key */
39         ubyte[16]       IV;         /* IV for CTR mode */
40         
41         c_ulong         pool_idx,   /* current pool we will add to */
42                         pool0_len,  /* length of 0'th pool */
43                         wd;            
44     
45         ulong           reset_cnt;  /* number of times we have reset */
46         mixin(LTC_MUTEX_TYPE("prng_lock"));
47     }
48 }
49 
50 version(LTC_SOBER128)
51 {
52     struct sober128_prng {
53         uint[17]     R,          /* Working storage for the shift register */
54                      initR;      /* saved register contents */ 
55         uint         konst,      /* key dependent constant */
56                      sbuf;       /* partial word encryption buffer */
57     
58         int          nbuf,       /* number of part-word stream bits buffered */
59                      flag,       /* first add_entropy call or not? */
60                      set;        /* did we call add_entropy to set key? */
61         
62     }
63 }
64 
65 union prng_state 
66 {
67     char[1] dummy;
68     version(LTC_YARROW)
69     {
70         yarrow_prng    yarrow;
71     }
72     version(LTC_RC4)
73     {
74         rc4_prng       rc4;
75     }
76     version(LTC_FORTUNA)
77     {
78         fortuna_prng   fortuna;
79     }
80     version(LTC_SOBER128)
81     {
82         sober128_prng  sober128;
83     }
84 }
85 
86 /** PRNG descriptor */
87 struct ltc_prng_descriptor 
88 {
89     /** Name of the PRNG */
90     char *name;
91     /** size _in bytes of exported state */
92     int  export_size;
93     /** Start a PRNG state
94         @param prng   [_out] The state to initialize
95         @return CRYPT_OK if successful
96     */
97     int function(prng_state *prng) nothrow start;
98     /** Add entropy to the PRNG
99         @param _in         The entropy
100         @param inlen      Length of the entropy (octets)\
101         @param prng       The PRNG state
102         @return CRYPT_OK if successful
103     */
104     int function(const ubyte* _in, c_ulong inlen, prng_state *prng) nothrow add_entropy;
105     /** Ready a PRNG state to read from
106         @param prng       The PRNG state to ready
107         @return CRYPT_OK if successful
108     */
109     int function(prng_state *prng) nothrow ready;
110     /** Read from the PRNG
111         @param _out     [_out] Where to store the data
112         @param outlen  Length of data desired (octets)
113         @param prng    The PRNG state to read from
114         @return Number of octets read
115     */
116     c_ulong function(ubyte* _out, c_ulong outlen, prng_state *prng) nothrow read;
117     /** Terminate a PRNG state
118         @param prng   The PRNG state to terminate
119         @return CRYPT_OK if successful
120     */
121     int function(prng_state *prng) nothrow done;
122     /** Export a PRNG state  
123         @param _out     [_out] The destination for the state
124         @param outlen  [_in/_out] The max size and resulting size of the PRNG state
125         @param prng    The PRNG to export
126         @return CRYPT_OK if successful
127     */
128     int function(ubyte* _out, c_ulong *outlen, prng_state *prng) nothrow pexport;
129     /** Import a PRNG state
130         @param _in      The data to import
131         @param inlen   The length of the data to import (octets)
132         @param prng    The PRNG to initialize/import
133         @return CRYPT_OK if successful
134     */
135     int function(const ubyte* _in, c_ulong inlen, prng_state *prng) nothrow pimport;
136     /** Self-test the PRNG
137         @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
138     */
139     int function() nothrow test;
140 }
141 
142 extern __gshared ltc_prng_descriptor[] prng_descriptor;
143 
144 version(LTC_YARROW)
145 {
146     int yarrow_start(prng_state *prng);
147     int yarrow_add_entropy(const ubyte* _in, c_ulong inlen, prng_state *prng);
148     int yarrow_ready(prng_state *prng);
149     c_ulong yarrow_read(ubyte* _out, c_ulong outlen, prng_state *prng);
150     int yarrow_done(prng_state *prng);
151     int  yarrow_export(ubyte* _out, c_ulong *outlen, prng_state *prng);
152     int  yarrow_import(const ubyte* _in, c_ulong inlen, prng_state *prng);
153     int  yarrow_test();
154     extern const __gshared ltc_prng_descriptor yarrow_desc;
155 }
156 
157 version(LTC_FORTUNA)
158 {
159     int fortuna_start(prng_state *prng);
160     int fortuna_add_entropy(const ubyte* _in, c_ulong inlen, prng_state *prng);
161     int fortuna_ready(prng_state *prng);
162     c_ulong fortuna_read(ubyte* _out, c_ulong outlen, prng_state *prng);
163     int fortuna_done(prng_state *prng);
164     int  fortuna_export(ubyte* _out, c_ulong *outlen, prng_state *prng);
165     int  fortuna_import(const ubyte* _in, c_ulong inlen, prng_state *prng);
166     int  fortuna_test();
167     extern const __gshared ltc_prng_descriptor fortuna_desc;
168 }
169 
170 version(LTC_RC4)
171 {
172     int rc4_start(prng_state *prng);
173     int rc4_add_entropy(const ubyte* _in, c_ulong inlen, prng_state *prng);
174     int rc4_ready(prng_state *prng);
175     c_ulong rc4_read(ubyte* _out, c_ulong outlen, prng_state *prng);
176     int  rc4_done(prng_state *prng);
177     int  rc4_export(ubyte* _out, c_ulong *outlen, prng_state *prng);
178     int  rc4_import(const ubyte* _in, c_ulong inlen, prng_state *prng);
179     int  rc4_test();
180     extern const __gshared ltc_prng_descriptor rc4_desc;
181 }
182 
183 version(LTC_SPRNG)
184 {
185     int sprng_start(prng_state *prng);
186     int sprng_add_entropy(const ubyte* _in, c_ulong inlen, prng_state *prng);
187     int sprng_ready(prng_state *prng);
188     c_ulong sprng_read(ubyte* _out, c_ulong outlen, prng_state *prng);
189     int sprng_done(prng_state *prng);
190     int  sprng_export(ubyte* _out, c_ulong *outlen, prng_state *prng);
191     int  sprng_import(const ubyte* _in, c_ulong inlen, prng_state *prng);
192     int  sprng_test();
193     extern const __gshared ltc_prng_descriptor sprng_desc;
194 }
195 
196 version(LTC_SOBER128)
197 {
198     int sober128_start(prng_state *prng);
199     int sober128_add_entropy(const ubyte* _in, c_ulong inlen, prng_state *prng);
200     int sober128_ready(prng_state *prng);
201     c_ulong sober128_read(ubyte* _out, c_ulong outlen, prng_state *prng);
202     int sober128_done(prng_state *prng);
203     int  sober128_export(ubyte* _out, c_ulong *outlen, prng_state *prng);
204     int  sober128_import(const ubyte* _in, c_ulong inlen, prng_state *prng);
205     int  sober128_test();
206     extern const __gshared ltc_prng_descriptor sober128_desc;
207 }
208 
209 int find_prng(const char *name);
210 int register_prng(const ltc_prng_descriptor *prng);
211 int unregister_prng(const ltc_prng_descriptor *prng);
212 int prng_is_valid(int idx);
213 mixin(LTC_MUTEX_PROTO("ltc_prng_mutex"));
214 
215 /* Slow RNG you **might** be able to use to seed a PRNG with.  Be careful as this
216  * might not work on all platforms as planned
217  */
218 c_ulong rng_get_bytes(ubyte* _out, 
219                       c_ulong outlen, 
220                       void function() callback);
221 
222 int rng_make_prng(int bits, int wprng, prng_state *prng, void function() callback);
223 
224 
225 /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_prng.h,v $ */
226 /* $Revision: 1.9 $ */
227 /* $Date: 2007/05/12 14:32:35 $ */