Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 1997 - 2003 Pekka Riikonen
+ Copyright (C) 1997 - 2008 Pekka Riikonen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
GNU General Public License for more details.
*/
-/* $Id$ */
/*
* Created: Sun Mar 9 00:09:18 1997
*
* This RNG has been rewritten twice since the creation.
*/
-#include "silc.h"
+#include "silccrypto.h"
#ifndef WIN32
#ifdef HAVE_GETSID
silc_rng_xor(rng, (r.ru_nvcsw ^ r.ru_nivcsw), pos++);
#endif /* SILC_SYMBIAN */
#endif /* HAVE_GETRUSAGE */
-
+
#ifdef SILC_RNG_DEBUG
SILC_LOG_HEXDUMP(("pool"), rng->pool, sizeof(rng->pool));
#endif
int fd, len, i;
/* Get noise from /dev/[u]random if available */
- fd = open(rng->devrandom, O_RDONLY);
+ fd = silc_file_open(rng->devrandom, O_RDONLY);
if (fd < 0)
return;
#endif
out:
- close(fd);
+ silc_file_close(fd);
memset(buf, 0, sizeof(buf));
#endif
}
/* Returns non-zero random number binary data. */
-unsigned char *silc_rng_get_rn_data(SilcRng rng, SilcUInt32 len)
+SilcBool silc_rng_get_rn_data(SilcRng rng, SilcUInt32 len, unsigned char *buf,
+ SilcUInt32 buf_size)
{
int i;
- unsigned char *data;
- data = silc_calloc(len + 1, sizeof(*data));
+ if (len > buf_size)
+ return FALSE;
for (i = 0; i < len; i++)
- data[i] = silc_rng_get_byte(rng);
+ buf[i] = silc_rng_get_byte(rng);
- return data;
+ return TRUE;
}
/* Global RNG. This is global RNG that application can initialize so
return global_rng ? silc_rng_get_rn_string(global_rng, len) : NULL;
}
-unsigned char *silc_rng_global_get_rn_data(SilcUInt32 len)
+SilcBool silc_rng_global_get_rn_data(SilcRng rng, SilcUInt32 len,
+ unsigned char *buf, SilcUInt32 buf_size)
{
- return global_rng ? silc_rng_get_rn_data(global_rng, len) : NULL;
+ return global_rng ? silc_rng_get_rn_data(global_rng, len, buf,
+ buf_size) : FALSE;
}
void silc_rng_global_add_noise(unsigned char *buffer, SilcUInt32 len)