GNU General Public License for more details.
*/
+/* $Id$ */
/*
* Created: Sun Mar 9 00:09:18 1997
*
*/
/* XXX: Some operations block resulting slow initialization.
* XXX: I have some pending changes to make this better. */
-/*
- * $Id$
- * $Log$
- * Revision 1.2 2000/07/05 06:08:43 priikone
- * Global cosmetic change.
- *
- * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
- * Imported from internal CVS/Added Log headers.
- *
- *
- */
#include "silcincludes.h"
#undef SILC_RNG_DEBUG
/* #define SILC_RNG_DEBUG */
+static unsigned int silc_rng_get_position(SilcRng rng);
+static void silc_rng_stir_pool(SilcRng rng);
+static void silc_rng_xor(SilcRng rng, unsigned int val, unsigned int pos);
+static void silc_rng_add_noise(SilcRng rng, unsigned char *buffer,
+ unsigned int len);
+static void silc_rng_exec_command(SilcRng rng, char *command);
+static void silc_rng_get_hard_noise(SilcRng rng);
+static void silc_rng_get_medium_noise(SilcRng rng);
+static void silc_rng_get_soft_noise(SilcRng rng);
+
/*
SILC SilcRng State context.
(i * (sizeof(rng->pool) / SILC_RNG_STATE_NUM));
next->pos =
(i * (sizeof(rng->pool) / SILC_RNG_STATE_NUM)) + 8;
-#if 0
- next->pos = sizeof(rng->pool) -
- ((i * (sizeof(rng->pool) / SILC_RNG_STATE_NUM))) + 8;
-#endif
next->next = rng->state;
rng->state = next;
}
/* This function gets 'soft' noise from environment. */
-void silc_rng_get_soft_noise(SilcRng rng)
+static void silc_rng_get_soft_noise(SilcRng rng)
{
struct tms ptime;
/* This function gets noise from different commands */
-void silc_rng_get_medium_noise(SilcRng rng)
+static void silc_rng_get_medium_noise(SilcRng rng)
{
silc_rng_exec_command(rng, "ps -lefaww 2> /dev/null");
silc_rng_exec_command(rng, "ls -afiln 2> /dev/null");
/* This function gets 'hard' noise from environment. This tries to
get the noise from /dev/random if available. */
-void silc_rng_get_hard_noise(SilcRng rng)
+static void silc_rng_get_hard_noise(SilcRng rng)
{
char buf[32];
int fd, len, i;
/* Execs command and gets noise from its output */
-void silc_rng_exec_command(SilcRng rng, char *command)
+static void silc_rng_exec_command(SilcRng rng, char *command)
{
char buf[2048];
FILE *fd;
/* This function adds the contents of the buffer as noise into random
pool. After adding the noise the pool is stirred. */
-void silc_rng_add_noise(SilcRng rng, unsigned char *buffer,
- unsigned int len)
+static void silc_rng_add_noise(SilcRng rng, unsigned char *buffer,
+ unsigned int len)
{
unsigned int i, pos;
/* XOR's data into the pool */
-void silc_rng_xor(SilcRng rng, unsigned int val, unsigned int pos)
+static void silc_rng_xor(SilcRng rng, unsigned int val, unsigned int pos)
{
assert(rng != NULL);
rng->pool[pos] ^= val + val;
/* This function stirs the random pool by encrypting buffer in CFB
(cipher feedback) mode with SHA1 algorithm. */
-void silc_rng_stir_pool(SilcRng rng)
+static void silc_rng_stir_pool(SilcRng rng)
{
int i;
unsigned long iv[5];
/* Returns next position where data is fetched from the pool or
put to the pool. */
-unsigned int silc_rng_get_position(SilcRng rng)
+static unsigned int silc_rng_get_position(SilcRng rng)
{
SilcRngState next;
unsigned int pos;
return string;
}
+
+/* Returns random number binary data. */
+
+unsigned char *silc_rng_get_rn_data(SilcRng rng, unsigned int len)
+{
+ int i;
+ unsigned char *data;
+
+ data = silc_calloc(len + 1, sizeof(*data));
+
+ for (i = 0; i < len; i++)
+ data[i] = silc_rng_get_byte(rng);
+
+ return data;
+}
+
+/* Global RNG. This is global RNG that application can initialize so
+ that any part of code anywhere can use RNG without having to allocate
+ new RNG object everytime. If this is not initialized then these routines
+ will fail. Note: currently in SILC applications always initialize this. */
+
+SilcRng global_rng = NULL;
+
+/* Initialize global RNG. If `rng' is provided it is set as the global
+ RNG object (it can be allocated by the application for example). */
+
+int silc_rng_global_init(SilcRng rng)
+{
+ if (rng)
+ global_rng = rng;
+ else
+ global_rng = silc_rng_alloc();
+
+ return TRUE;
+}
+
+/* Uninitialize global RNG */
+
+int silc_rng_global_uninit()
+{
+ if (global_rng) {
+ silc_rng_free(global_rng);
+ global_rng = NULL;
+ }
+
+ return TRUE;
+}
+
+/* These are analogous to the functions above. */
+
+unsigned char silc_rng_global_get_byte()
+{
+ return global_rng ? silc_rng_get_byte(global_rng) : 0;
+}
+
+unsigned short silc_rng_global_get_rn16()
+{
+ return global_rng ? silc_rng_get_rn16(global_rng) : 0;
+}
+
+unsigned int silc_rng_global_get_rn32()
+{
+ return global_rng ? silc_rng_get_rn32(global_rng) : 0;
+}
+
+unsigned char *silc_rng_global_get_rn_string(unsigned int len)
+{
+ return global_rng ? silc_rng_get_rn_string(global_rng, len) : NULL;
+}
+
+unsigned char *silc_rng_global_get_rn_data(unsigned int len)
+{
+ return global_rng ? silc_rng_get_rn_data(global_rng, len) : NULL;
+}