Added SILC Thread Queue API
[silc.git] / lib / silccrypt / silcrng.c
index e62e58b7a90cf9311f0df091d463c0a9fddf219a..ab680bf04c929389a8d38ed558d1c92c6bfede9c 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2003 Pekka Riikonen
+  Copyright (C) 1997 - 2007 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
@@ -25,7 +25,7 @@
  * This RNG has been rewritten twice since the creation.
  */
 
-#include "silcincludes.h"
+#include "silc.h"
 
 #ifndef WIN32
 #ifdef HAVE_GETSID
@@ -243,14 +243,17 @@ static void silc_rng_get_soft_noise(SilcRng rng)
   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++);
@@ -262,6 +265,7 @@ static void silc_rng_get_soft_noise(SilcRng rng)
   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
@@ -274,8 +278,10 @@ static void silc_rng_get_soft_noise(SilcRng rng)
 #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
@@ -286,6 +292,7 @@ static void silc_rng_get_soft_noise(SilcRng rng)
   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++);
@@ -302,8 +309,9 @@ static void silc_rng_get_soft_noise(SilcRng rng)
   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));
 #endif
@@ -335,7 +343,7 @@ static void silc_rng_get_medium_noise(SilcRng rng)
 
 static void silc_rng_get_hard_noise(SilcRng rng)
 {
-#ifndef SILC_WIN32
+#if defined(SILC_UNIX)
   unsigned char buf[32];
   int fd, len, i;
 
@@ -367,7 +375,7 @@ static void silc_rng_get_hard_noise(SilcRng rng)
 
 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;
@@ -549,7 +557,7 @@ SilcUInt8 silc_rng_get_byte(SilcRng rng)
 
 SilcUInt8 silc_rng_get_byte_fast(SilcRng rng)
 {
-#ifndef SILC_WIN32
+#if defined(SILC_UNIX)
   unsigned char buf[1];
 
   if (rng->fd_devurandom == -1) {
@@ -615,17 +623,18 @@ unsigned char *silc_rng_get_rn_string(SilcRng rng, SilcUInt32 len)
 
 /* 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
@@ -638,7 +647,7 @@ 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). */
 
-bool silc_rng_global_init(SilcRng rng)
+SilcBool silc_rng_global_init(SilcRng rng)
 {
   if (rng) {
     global_rng = rng;
@@ -653,7 +662,7 @@ bool silc_rng_global_init(SilcRng rng)
 
 /* Uninitialize global RNG */
 
-bool silc_rng_global_uninit(void)
+SilcBool silc_rng_global_uninit(void)
 {
   if (global_rng) {
     silc_rng_free(global_rng);
@@ -693,9 +702,11 @@ unsigned char *silc_rng_global_get_rn_string(SilcUInt32 len)
   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)