Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 1997 - 2003 Pekka Riikonen
+ Copyright (C) 1997 - 2014 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
* This RNG has been rewritten twice since the creation.
*/
-#include "silcincludes.h"
+#include "silc.h"
#ifndef WIN32
#ifdef HAVE_GETSID
}
new->devrandom = strdup("/dev/random");
+ if (!new->devrandom) {
+ silc_rng_free(new);
+ return NULL;
+ }
return new;
}
/* Initialize the states for the RNG. */
rng->state = silc_calloc(1, sizeof(*rng->state));
+ if (!rng->state) {
+ SILC_LOG_ERROR(("Error allocating memory for RNG"));
+ return;
+ }
rng->state->low = 0;
rng->state->pos = 8;
rng->state->next = NULL;
first = rng->state;
for (i = SILC_RNG_STATE_NUM - 1; i >= 1; i--) {
next = silc_calloc(1, sizeof(*rng->state));
+ if (!next) {
+ SILC_LOG_ERROR(("Error allocating memory for RNG"));
+ return;
+ }
next->low =
(i * (sizeof(rng->pool) / SILC_RNG_STATE_NUM));
next->pos =
silc_rng_xor(rng, getpgid(getpid()) << 8, 2);
silc_rng_xor(rng, getpgid(getpid()) << 8, 3);
#endif
+#ifdef HAVE_GETGID
silc_rng_xor(rng, getgid(), 4);
#endif
+#endif
#ifdef HAVE_GETPGRP
silc_rng_xor(rng, getpgrp(), 5);
#endif
#ifdef HAVE_GETSID
silc_rng_xor(rng, getsid(getpid()) << 16, 6);
#endif
+#ifndef SILC_SYMBIAN
silc_rng_xor(rng, times(&ptime), 7);
silc_rng_xor(rng, ptime.tms_utime, 8);
silc_rng_xor(rng, (ptime.tms_utime + ptime.tms_stime), pos++);
silc_rng_xor(rng, (ptime.tms_stime ^ ptime.tms_cutime), pos++);
silc_rng_xor(rng, (ptime.tms_cutime + ptime.tms_stime), pos++);
silc_rng_xor(rng, (ptime.tms_stime << 8), pos++);
+#endif /* SILC_SYMBIAN */
#endif
silc_rng_xor(rng, clock() << 4, pos++);
#ifndef SILC_WIN32
#ifdef HAVE_SETSID
silc_rng_xor(rng, getsid(getpid()) << 16, pos++);
#endif
+#ifndef SILC_SYMBIAN
silc_rng_xor(rng, times(&ptime), pos++);
silc_rng_xor(rng, ptime.tms_utime, pos++);
+#endif /* SILC_SYMBIAN */
#ifdef HAVE_GETPGRP
silc_rng_xor(rng, getpgrp(), pos++);
#endif
silc_rng_xor(rng, (r.ru_utime.tv_sec ^ r.ru_utime.tv_usec), pos++);
silc_rng_xor(rng, (r.ru_stime.tv_sec + r.ru_stime.tv_usec), pos++);
silc_rng_xor(rng, (r.ru_stime.tv_sec ^ r.ru_stime.tv_usec), pos++);
+#ifndef SILC_SYMBIAN
silc_rng_xor(rng, (r.ru_maxrss + r.ru_ixrss), pos++);
silc_rng_xor(rng, (r.ru_maxrss ^ r.ru_ixrss), pos++);
silc_rng_xor(rng, (r.ru_idrss + r.ru_idrss), pos++);
silc_rng_xor(rng, (r.ru_nsignals << 16), pos++);
silc_rng_xor(rng, (r.ru_nvcsw + r.ru_nivcsw), pos++);
silc_rng_xor(rng, (r.ru_nvcsw ^ r.ru_nivcsw), pos++);
-#endif
+#endif /* SILC_SYMBIAN */
+#endif /* HAVE_GETRUSAGE */
#ifdef SILC_RNG_DEBUG
SILC_LOG_HEXDUMP(("pool"), rng->pool, sizeof(rng->pool));
static void silc_rng_get_hard_noise(SilcRng rng)
{
-#ifndef SILC_WIN32
+#if defined(SILC_UNIX)
unsigned char buf[32];
int fd, len, i;
static void silc_rng_exec_command(SilcRng rng, char *command)
{
-#ifndef SILC_WIN32
+#if defined(SILC_UNIX)
unsigned char buf[1024];
FILE *fd;
int i;
SilcUInt8 silc_rng_get_byte_fast(SilcRng rng)
{
-#ifndef SILC_WIN32
+#if defined(SILC_UNIX)
unsigned char buf[1];
if (rng->fd_devurandom == -1) {
unsigned char *string;
string = silc_calloc((len * 2 + 1), sizeof(unsigned char));
+ if (!string)
+ return NULL;
for (i = 0; i < len; i++)
sprintf(string + 2 * i, "%02x", silc_rng_get_byte(rng));
unsigned char *data;
data = silc_calloc(len + 1, sizeof(*data));
+ if (!data)
+ return NULL;
for (i = 0; i < len; i++)
data[i] = silc_rng_get_byte(rng);