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