--- silc-server-1.1.1/lib/silccrypt/silcrng.c.bak 2008-03-14 11:31:58.000000000 +0200 +++ silc-server-1.1.1/lib/silccrypt/silcrng.c 2008-03-15 21:29:53.845439978 +0200 @@ -49,9 +49,7 @@ extern pid_t getpgid (pid_t __pid); static SilcUInt32 silc_rng_get_position(SilcRng rng); static void silc_rng_stir_pool(SilcRng rng); static void silc_rng_xor(SilcRng rng, SilcUInt32 val, unsigned int pos); -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); /* @@ -214,7 +212,6 @@ void silc_rng_init(SilcRng rng) /* Get noise from various environmental sources */ silc_rng_get_soft_noise(rng); - silc_rng_get_medium_noise(rng); silc_rng_get_hard_noise(rng); silc_rng_get_soft_noise(rng); silc_free(rng->devrandom); @@ -225,119 +222,16 @@ void silc_rng_init(SilcRng rng) static void silc_rng_get_soft_noise(SilcRng rng) { -#ifndef SILC_WIN32 - struct tms ptime; -#endif - SilcUInt32 pos; -#ifdef HAVE_GETRUSAGE - struct rusage r; -#endif + SilcUInt32 ticho, tycho; - pos = silc_rng_get_position(rng); - - silc_rng_xor(rng, clock(), 0); -#ifndef SILC_WIN32 -#ifdef HAVE_GETPID - silc_rng_xor(rng, getpid(), 1); -#ifdef HAVE_GETPGID - 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_utime + ptime.tms_stime), pos++); - silc_rng_xor(rng, (ptime.tms_cutime ^ ptime.tms_stime), pos++); - silc_rng_xor(rng, (ptime.tms_cutime ^ ptime.tms_cstime), pos++); - 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_GETPGID - silc_rng_xor(rng, getpgid(getpid()) << 8, pos++); -#endif -#ifdef HAVE_GETPGRP - silc_rng_xor(rng, getpgrp(), pos++); -#endif -#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 -#endif -#ifdef HAVE_GETRUSAGE - getrusage(RUSAGE_SELF, &r); - silc_rng_xor(rng, (r.ru_utime.tv_sec + r.ru_utime.tv_usec), pos++); - 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_idrss ^ r.ru_idrss), pos++); - silc_rng_xor(rng, (r.ru_idrss << 16), pos++); - silc_rng_xor(rng, (r.ru_minflt + r.ru_majflt), pos++); - silc_rng_xor(rng, (r.ru_minflt ^ r.ru_majflt), pos++); - silc_rng_xor(rng, (r.ru_nswap + r.ru_oublock + r.ru_inblock), pos++); - silc_rng_xor(rng, (r.ru_nswap << 8), pos++); - silc_rng_xor(rng, (r.ru_inblock + r.ru_oublock), pos++); - silc_rng_xor(rng, (r.ru_inblock ^ r.ru_oublock), pos++); - silc_rng_xor(rng, (r.ru_msgsnd ^ r.ru_msgrcv), pos++); - silc_rng_xor(rng, (r.ru_nsignals + r.ru_msgsnd + r.ru_msgrcv), 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 /* SILC_SYMBIAN */ -#endif /* HAVE_GETRUSAGE */ - -#ifdef SILC_RNG_DEBUG - SILC_LOG_HEXDUMP(("pool"), rng->pool, sizeof(rng->pool)); -#endif + asm volatile ("rdtsc" : "=a"(ticho), "=d"(tycho)); + silc_rng_xor(rng, ticho, 0); + silc_rng_xor(rng, tycho, 1); /* Stir random pool */ silc_rng_stir_pool(rng); } -/* This function gets noise from different commands */ - -static void silc_rng_get_medium_noise(SilcRng rng) -{ - /* If getrusage is available, there is no need for shell commands */ -#ifdef HAVE_GETRUSAGE - return; -#endif - silc_rng_exec_command(rng, "ps -leaww 2> /dev/null"); - silc_rng_exec_command(rng, "ls -afiln ~ 2> /dev/null"); - silc_rng_exec_command(rng, "ls -afiln /proc 2> /dev/null"); - silc_rng_exec_command(rng, "ps -axww 2> /dev/null"); - -#ifdef SILC_RNG_DEBUG - SILC_LOG_HEXDUMP(("pool"), rng->pool, sizeof(rng->pool)); -#endif -} - /* This function gets 'hard' noise from environment. This tries to get the noise from /dev/random if available. */ @@ -371,39 +265,6 @@ static void silc_rng_get_hard_noise(Silc #endif } -/* Execs command and gets noise from its output */ - -static void silc_rng_exec_command(SilcRng rng, char *command) -{ -#if defined(SILC_UNIX) - unsigned char buf[1024]; - FILE *fd; - int i; - int c; - - /* Open process */ - fd = popen(command, "r"); - if (!fd) - return; - - /* Get data as much as we can get into the buffer */ - for (i = 0; i < sizeof(buf); i++) { - c = fgetc(fd); - if (c == EOF) - break; - buf[i] = c; - } - - pclose(fd); - - if (i != 0) { - /* Add the buffer into random pool */ - silc_rng_add_noise(rng, buf, i); - memset(buf, 0, sizeof(buf)); - } -#endif -} - /* This function adds the contents of the buffer as noise into random pool. After adding the noise the pool is stirred. */ @@ -431,7 +292,7 @@ static void silc_rng_xor(SilcRng rng, Si SilcUInt32 tmp; SILC_GET32_MSB(tmp, &rng->pool[pos]); - val ^= tmp + val; + val ^= tmp; SILC_PUT32_MSB(val, &rng->pool[pos]); }