Integer type name change.
[silc.git] / lib / silccrypt / silcrng.h
1 /*
2
3   silcrng.h 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2002 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 /****h* silccrypt/SilcRNGAPI
21  *
22  * DESCRIPTION
23  *
24  * SILC Random Number Generator is cryptographically strong pseudo random
25  * number generator. It is used to generate all the random numbers needed
26  * in the SILC sessions. All key material and other sources needing random
27  * numbers use this generator.
28  *
29  * The interface provides functions for retrieving different size of
30  * random number and arbitrary length of random data buffers. The interface
31  * also defines Global RNG API which makes it possible to call any
32  * RNG API function without specific RNG context.
33  *
34  ***/
35
36 #ifndef SILCRNG_H
37 #define SILCRNG_H
38
39 /****s* silccrypt/SilcRNGAPI/SilcRng
40  *
41  * NAME
42  * 
43  *    typedef struct SilcRngStruct *SilcRng;
44  *
45  * DESCRIPTION
46  *
47  *    This context is the actual Random Number Generator and is allocated
48  *    by silc_rng_alloc and given as argument usually to all silc_rng_*
49  *    functions.  It is freed by the silc_rng_free function.  The RNG is
50  *    initialized by calling the silc_rng_init function.
51  *
52  ***/
53 typedef struct SilcRngStruct *SilcRng;
54
55 /* Prototypes */
56
57 /****f* silccrypt/SilcRNGAPI/silc_rng_alloc
58  *
59  * SYNOPSIS
60  *
61  *    SilcRng silc_rng_alloc(void);
62  *
63  * DESCRIPTION
64  *
65  *    Allocates new SILC random number generator and returns context to
66  *    it.  After the RNG is allocated it must be initialized by calling
67  *    silc_rng_init before it actually can be used to produce any random
68  *    number.  This function returns NULL if RNG could not allocated.
69  *
70  ***/
71 SilcRng silc_rng_alloc(void);
72
73 /****f* silccrypt/SilcRNGAPI/silc_rng_free
74  *
75  * SYNOPSIS
76  *
77  *    void silc_rng_free(SilcRng rng);
78  *
79  * DESCRIPTION
80  *
81  *    Frees the random number generator and destroys the random number
82  *    pool.
83  *
84  ***/
85 void silc_rng_free(SilcRng rng);
86
87 /****f* silccrypt/SilcRNGAPI/silc_rng_init
88  *
89  * SYNOPSIS
90  *
91  *    void silc_rng_init(SilcRng rng);
92  *
93  * DESCRIPTION
94  *
95  *    This function is used to initialize the random number generator.
96  *    This is the function that must be called after the RNG is allocated
97  *    by calling silc_rng_alloc.  RNG cannot be used before this function
98  *    is called.
99  *
100  * NOTES
101  *
102  *    This function may be slow since it will acquire secret noise from
103  *    the environment in an attempt to set the RNG in unguessable state.
104  *
105  ***/
106 void silc_rng_init(SilcRng rng);
107
108 /****f* silccrypt/SilcRNGAPI/silc_rng_get_byte
109  *
110  * SYNOPSIS
111  *
112  *    SilcUInt8 silc_rng_get_byte(SilcRng rng);
113  *
114  * DESCRIPTION
115  *
116  *    Returns one 8-bit random byte from the random number generator.
117  *
118  ***/
119 SilcUInt8 silc_rng_get_byte(SilcRng rng);
120
121 /****f* silccrypt/SilcRNGAPI/silc_rng_get_rn16
122  *
123  * SYNOPSIS
124  *
125  *    SilcUInt16 silc_rng_get_rn16(SilcRng rng);
126  *
127  * DESCRIPTION
128  *
129  *    Returns one 16-bit random number from the random number generator.
130  *
131  ***/
132 SilcUInt16 silc_rng_get_rn16(SilcRng rng);
133
134 /****f* silccrypt/SilcRNGAPI/silc_rng_get_rn32
135  *
136  * SYNOPSIS
137  *
138  *    SilcUInt32 silc_rng_get_rn32(SilcRng rng);
139  *
140  * DESCRIPTION
141  *
142  *    Returns one 32-bit random number from the random number generator.
143  *
144  ***/
145 SilcUInt32 silc_rng_get_rn32(SilcRng rng);
146
147 /****f* silccrypt/SilcRNGAPI/silc_rng_get_rn_string
148  *
149  * SYNOPSIS
150  *
151  *    unsigned char *silc_rng_get_rn_string(SilcRng rng, SilcUInt32 len);
152  *
153  * DESCRIPTION
154  *
155  *    Returns random string in HEX form of the length of `len' bytes.
156  *    The caller must free returned data buffer.
157  *
158  ***/
159 unsigned char *silc_rng_get_rn_string(SilcRng rng, SilcUInt32 len);
160
161 /****f* silccrypt/SilcRNGAPI/silc_rng_get_rn_data
162  *
163  * SYNOPSIS
164  *
165  *    unsigned char *silc_rng_get_rn_data(SilcRng rng, SilcUInt32 len);
166  *
167  * DESCRIPTION
168  *
169  *    Returns random binary data of the length of `len' bytes.  The 
170  *    caller must free returned data buffer.
171  *
172  ***/
173 unsigned char *silc_rng_get_rn_data(SilcRng rng, SilcUInt32 len);
174
175 /****f* silccrypt/SilcRNGAPI/silc_rng_add_noise
176  *
177  * SYNOPSIS
178  *
179  *    void silc_rng_add_noise(SilcRng rng, unsigned char *buffer, SilcUInt32 len);
180  *
181  * DESCRIPTION
182  *
183  *    Add the data buffer indicated by `buffer' of length of `len' bytes 
184  *    as noise to the random number generator.  The random number generator
185  *    is restirred (reseeded) when this function is called.
186  *
187  ***/
188 void silc_rng_add_noise(SilcRng rng, unsigned char *buffer, SilcUInt32 len);
189
190 /****f* silccrypt/SilcRNGAPI/silc_rng_global_init
191  *
192  * SYNOPSIS
193  *
194  *    bool silc_rng_global_init(SilcRng rng);
195  *
196  * DESCRIPTION
197  *
198  *    This function sets the `rng' if non-NULL as global RNG context.
199  *    When any of the silc_rng_global_* functions is called the `rng' is
200  *    used as RNG.  If `rng' is NULL this will allocate new RNG as global
201  *    RNG.  The application in this case must free it later by calling
202  *    silc_rng_global_uninit.  Returns TRUE after Global RNG is initialized.
203  *
204  * NOTES
205  *
206  *    If `rng' was non-NULL, the silc_rng_init must have been called for
207  *    the `rng' already.
208  *
209  *    This function can be used to define the `rng' as global RNG and then
210  *    use silc_rng_global_* functions easily without need to provide
211  *    the RNG as argument.
212  *
213  ***/
214 bool silc_rng_global_init(SilcRng rng);
215
216 /****f* silccrypt/SilcRNGAPI/silc_rng_global_uninit
217  *
218  * SYNOPSIS
219  *
220  *    bool silc_rng_global_uninit(void);
221  *
222  * DESCRIPTION
223  *
224  *    Uninitialized the Global RNG object and frees it.  This should not
225  *    be called if silc_rng_global_init was called with non-NULL RNG.
226  *
227  ***/
228 bool silc_rng_global_uninit(void);
229
230 /****f* silccrypt/SilcRNGAPI/silc_rng_global_get_byte
231  *
232  * SYNOPSIS
233  *
234  *    SilcUInt8 silc_rng_global_get_byte(void);
235  *
236  * DESCRIPTION
237  *
238  *    Returns one 8-bit random byte from the random number generator.
239  *
240  ***/
241 SilcUInt8 silc_rng_global_get_byte(void);
242
243 /****f* silccrypt/SilcRNGAPI/silc_rng_global_get_byte_fast
244  *
245  * SYNOPSIS
246  *
247  *    SilcUInt8 silc_rng_global_get_byte_fast(void);
248  *
249  * DESCRIPTION
250  *
251  *    Returns one 8-bit random byte from the random number generator as
252  *    fast as possible.
253  *
254  * NOTES
255  *
256  *    This will read the data from /dev/urandom if it is available in the
257  *    operating system, since this may be faster than retrieving a byte
258  *    from the SILC RNG.  If /dev/urandom is not available this will take
259  *    the byte from SILC RNG and is effectively same as silc_rng_get_byte.
260  *
261  ***/
262 SilcUInt8 silc_rng_global_get_byte_fast(void);
263
264 /****f* silccrypt/SilcRNGAPI/silc_rng_global_get_rn16
265  *
266  * SYNOPSIS
267  *
268  *    SilcUInt16 silc_rng_global_get_rn16(void);
269  *
270  * DESCRIPTION
271  *
272  *    Returns one 16-bit random number from the random number generator.
273  *
274  ***/
275 SilcUInt16 silc_rng_global_get_rn16(void);
276
277 /****f* silccrypt/SilcRNGAPI/silc_rng_global_get_rn32
278  *
279  * SYNOPSIS
280  *
281  *    SilcUInt32 silc_rng_global_get_rn32(void);
282  *
283  * DESCRIPTION
284  *
285  *    Returns one 32-bit random number from the random number generator.
286  *
287  ***/
288 SilcUInt32 silc_rng_global_get_rn32(void);
289
290 /****f* silccrypt/SilcRNGAPI/silc_rng_global_get_rn_string
291  *
292  * SYNOPSIS
293  *
294  *    unsigned char *silc_rng_global_get_rn_string(SilcUInt32 len);
295  *
296  * DESCRIPTION
297  *
298  *    Returns random string in HEX form of the length of `len' bytes.
299  *    The caller must free returned data buffer.
300  *
301  ***/
302 unsigned char *silc_rng_global_get_rn_string(SilcUInt32 len);
303
304 /****f* silccrypt/SilcRNGAPI/silc_rng_global_get_rn_data
305  *
306  * SYNOPSIS
307  *
308  *    unsigned char *silc_rng_global_get_rn_data(SilcUInt32 len);
309  *
310  * DESCRIPTION
311  *
312  *    Returns random binary data of the length of `len' bytes.  The 
313  *    caller must free returned data buffer.
314  *
315  ***/
316 unsigned char *silc_rng_global_get_rn_data(SilcUInt32 len);
317
318 /****f* silccrypt/SilcRNGAPI/silc_rng_global_add_noise
319  *
320  * SYNOPSIS
321  *
322  *    void silc_rng_global_add_noise(unsigned char *buffer, SilcUInt32 len);
323  *
324  * DESCRIPTION
325  *
326  *    Add the data buffer indicated by `buffer' of length of `len' bytes 
327  *    as noise to the random number generator.  The random number generator
328  *    is restirred (reseeded) when this function is called.
329  *
330  ***/
331
332 void silc_rng_global_add_noise(unsigned char *buffer, SilcUInt32 len);
333
334 #endif