/*
- silcrng.c
+ silcrng.c
- Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+ Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 1997 - 2001 Pekka Riikonen
+ Copyright (C) 1997 - 2002 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
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
+ the Free Software Foundation; version 2 of the License.
+
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#include "silcincludes.h"
+#ifndef WIN32
#ifdef HAVE_GETSID
-extern __pid_t getsid (__pid_t __pid);
+extern pid_t getsid (pid_t __pid);
#endif
#ifdef HAVE_GETPGID
-extern __pid_t getpgid (__pid_t __pid);
+extern pid_t getpgid (pid_t __pid);
+#endif
#endif
#undef SILC_RNG_DEBUG
random pool. This is allocated when RNG object is allocated and
free'd when RNG object is free'd.
- uint8 threshhold
+ uint8 threshold
- Threshhold to indicate when it is required to acquire more
+ Threshold to indicate when it is required to acquire more
noise from the environment. More soft noise is acquired after
64 bits of output and hard noise every 160 bits of output.
*/
-typedef struct SilcRngObjectStruct {
+struct SilcRngStruct {
unsigned char pool[SILC_RNG_POOLSIZE];
unsigned char key[64];
SilcRngState state;
SilcHash sha1;
- uint8 threshhold;
+ uint8 threshold;
char *devrandom;
int fd_devurandom;
-} SilcRngObject;
+};
/* Allocates new RNG object. */
-SilcRng silc_rng_alloc()
+SilcRng silc_rng_alloc(void)
{
SilcRng new;
memset(new->pool, 0, sizeof(new->pool));
memset(new->key, 0, sizeof(new->key));
new->state = NULL;
- silc_hash_alloc("sha1", &new->sha1);
+ if (!silc_hash_alloc("sha1", &new->sha1)) {
+ silc_free(new);
+ SILC_LOG_ERROR(("Could not allocate sha1 hash, probably not registered"));
+ return NULL;
+ }
new->devrandom = strdup("/dev/random");
pclose(fd);
/* Add the buffer into random pool */
- silc_rng_add_noise(rng, buf, strlen(buf));
+ silc_rng_add_noise(rng, buf, i);
memset(buf, 0, sizeof(buf));
#endif
}
/* Returns random byte. */
-unsigned char silc_rng_get_byte(SilcRng rng)
+uint8 silc_rng_get_byte(SilcRng rng)
{
- rng->threshhold++;
+ rng->threshold++;
- /* Get more soft noise after 64 bits threshhold */
- if (rng->threshhold >= 8)
+ /* Get more soft noise after 64 bits threshold */
+ if (rng->threshold >= 8)
silc_rng_get_soft_noise(rng);
- /* Get hard noise after 160 bits threshhold, zero the threshhold. */
- if (rng->threshhold >= 20) {
- rng->threshhold = 0;
+ /* Get hard noise after 160 bits threshold, zero the threshold. */
+ if (rng->threshold >= 20) {
+ rng->threshold = 0;
silc_rng_get_hard_noise(rng);
}
/* Initialize global RNG. If `rng' is provided it is set as the global
RNG object (it can be allocated by the application for example). */
-int silc_rng_global_init(SilcRng rng)
+bool silc_rng_global_init(SilcRng rng)
{
if (rng)
global_rng = rng;
/* Uninitialize global RNG */
-int silc_rng_global_uninit()
+bool silc_rng_global_uninit(void)
{
if (global_rng) {
silc_rng_free(global_rng);
/* These are analogous to the functions above. */
-unsigned char silc_rng_global_get_byte()
+uint8 silc_rng_global_get_byte(void)
{
return global_rng ? silc_rng_get_byte(global_rng) : 0;
}
/* Return random byte as fast as possible. Reads from /dev/urandom if
available. If not then return from normal RNG (not so fast). */
-unsigned char silc_rng_global_get_byte_fast()
+uint8 silc_rng_global_get_byte_fast(void)
{
#ifndef SILC_WIN32
unsigned char buf[1];
#endif
}
-uint16 silc_rng_global_get_rn16()
+uint16 silc_rng_global_get_rn16(void)
{
return global_rng ? silc_rng_get_rn16(global_rng) : 0;
}
-uint32 silc_rng_global_get_rn32()
+uint32 silc_rng_global_get_rn32(void)
{
return global_rng ? silc_rng_get_rn32(global_rng) : 0;
}