Created SILC Crypto Toolkit git repository.
[crypto.git] / lib / silccrypt / silcrng.c
index 224e33a2f47c986be8a3bee9fb80e5a04d3b1235..3bf7f39076cc937a3d941959a44c317fedf15f6b 100644 (file)
@@ -4,7 +4,7 @@
 
   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
@@ -16,7 +16,6 @@
   GNU General Public License for more details.
 
 */
-/* $Id$ */
 /*
  * Created: Sun Mar  9 00:09:18 1997
  *
@@ -25,7 +24,7 @@
  * This RNG has been rewritten twice since the creation.
  */
 
-#include "silc.h"
+#include "silccrypto.h"
 
 #ifndef WIN32
 #ifdef HAVE_GETSID
@@ -243,14 +242,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 +264,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 +277,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
@@ -305,7 +310,7 @@ static void silc_rng_get_soft_noise(SilcRng rng)
   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
@@ -342,7 +347,7 @@ static void silc_rng_get_hard_noise(SilcRng rng)
   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;
 
@@ -360,7 +365,7 @@ static void silc_rng_get_hard_noise(SilcRng rng)
 #endif
 
  out:
-  close(fd);
+  silc_file_close(fd);
   memset(buf, 0, sizeof(buf));
 #endif
 }
@@ -617,17 +622,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
@@ -695,9 +701,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)