5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 Pekka Riikonen
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.
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.
20 /****h* silccrypt/SilcRNGAPI
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.
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.
39 /****s* silccrypt/SilcRNGAPI/SilcRng
43 * typedef struct SilcRngStruct *SilcRng;
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.
53 typedef struct SilcRngStruct *SilcRng;
57 /****f* silccrypt/SilcRNGAPI/silc_rng_alloc
61 * SilcRng silc_rng_alloc(void);
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.
71 SilcRng silc_rng_alloc(void);
73 /****f* silccrypt/SilcRNGAPI/silc_rng_free
77 * void silc_rng_free(SilcRng rng);
81 * Frees the random number generator and destroys the random number
85 void silc_rng_free(SilcRng rng);
87 /****f* silccrypt/SilcRNGAPI/silc_rng_init
91 * void silc_rng_init(SilcRng rng);
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
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.
106 void silc_rng_init(SilcRng rng);
108 /****f* silccrypt/SilcRNGAPI/silc_rng_get_byte
112 * uint8 silc_rng_get_byte(SilcRng rng);
116 * Returns one 8-bit random byte from the random number generator.
119 uint8 silc_rng_get_byte(SilcRng rng);
121 /****f* silccrypt/SilcRNGAPI/silc_rng_get_rn16
125 * uint16 silc_rng_get_rn16(SilcRng rng);
129 * Returns one 16-bit random number from the random number generator.
132 uint16 silc_rng_get_rn16(SilcRng rng);
134 /****f* silccrypt/SilcRNGAPI/silc_rng_get_rn32
138 * uint32 silc_rng_get_rn32(SilcRng rng);
142 * Returns one 32-bit random number from the random number generator.
145 uint32 silc_rng_get_rn32(SilcRng rng);
147 /****f* silccrypt/SilcRNGAPI/silc_rng_get_rn_string
151 * unsigned char *silc_rng_get_rn_string(SilcRng rng, uint32 len);
155 * Returns random string in HEX form of the length of `len' bytes.
156 * The caller must free returned data buffer.
159 unsigned char *silc_rng_get_rn_string(SilcRng rng, uint32 len);
161 /****f* silccrypt/SilcRNGAPI/silc_rng_get_rn_data
165 * unsigned char *silc_rng_get_rn_data(SilcRng rng, uint32 len);
169 * Returns random binary data of the length of `len' bytes. The
170 * caller must free returned data buffer.
173 unsigned char *silc_rng_get_rn_data(SilcRng rng, uint32 len);
175 /****f* silccrypt/SilcRNGAPI/silc_rng_add_noise
179 * void silc_rng_add_noise(SilcRng rng, unsigned char *buffer, uint32 len);
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.
188 void silc_rng_add_noise(SilcRng rng, unsigned char *buffer, uint32 len);
190 /****f* silccrypt/SilcRNGAPI/silc_rng_global_init
194 * bool silc_rng_global_init(SilcRng rng);
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.
206 * If `rng' was non-NULL, the silc_rng_init must have been called for
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.
214 bool silc_rng_global_init(SilcRng rng);
216 /****f* silccrypt/SilcRNGAPI/silc_rng_global_uninit
220 * bool silc_rng_global_uninit(void);
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.
228 bool silc_rng_global_uninit(void);
230 /****f* silccrypt/SilcRNGAPI/silc_rng_global_get_byte
234 * uint8 silc_rng_global_get_byte(void);
238 * Returns one 8-bit random byte from the random number generator.
241 uint8 silc_rng_global_get_byte(void);
243 /****f* silccrypt/SilcRNGAPI/silc_rng_global_get_byte_fast
247 * uint8 silc_rng_global_get_byte_fast(void);
251 * Returns one 8-bit random byte from the random number generator as
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.
262 uint8 silc_rng_global_get_byte_fast(void);
264 /****f* silccrypt/SilcRNGAPI/silc_rng_global_get_rn16
268 * uint16 silc_rng_global_get_rn16(void);
272 * Returns one 16-bit random number from the random number generator.
275 uint16 silc_rng_global_get_rn16(void);
277 /****f* silccrypt/SilcRNGAPI/silc_rng_global_get_rn32
281 * uint32 silc_rng_global_get_rn32(void);
285 * Returns one 32-bit random number from the random number generator.
288 uint32 silc_rng_global_get_rn32(void);
290 /****f* silccrypt/SilcRNGAPI/silc_rng_global_get_rn_string
294 * unsigned char *silc_rng_global_get_rn_string(uint32 len);
298 * Returns random string in HEX form of the length of `len' bytes.
299 * The caller must free returned data buffer.
302 unsigned char *silc_rng_global_get_rn_string(uint32 len);
304 /****f* silccrypt/SilcRNGAPI/silc_rng_global_get_rn_data
308 * unsigned char *silc_rng_global_get_rn_data(uint32 len);
312 * Returns random binary data of the length of `len' bytes. The
313 * caller must free returned data buffer.
316 unsigned char *silc_rng_global_get_rn_data(uint32 len);
318 /****f* silccrypt/SilcRNGAPI/silc_rng_global_add_noise
322 * void silc_rng_global_add_noise(unsigned char *buffer, uint32 len);
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.
332 void silc_rng_global_add_noise(unsigned char *buffer, uint32 len);