Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seed random with a real random data #377

Merged
merged 8 commits into from
Oct 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/addagent/manage_agents.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,8 @@ int add_agent()
time2 = time(0);


/* Source is time1+ time2 +pid + ppid */
#ifndef WIN32
#ifdef __OpenBSD__
srandomdev();
#else
srandom((unsigned)(time2 + time1 + getpid() + getppid()));
#endif
#else
srandom(time2 + time1 + getpid());
#endif

srandom_init();

rand1 = random();

Expand Down
12 changes: 2 additions & 10 deletions src/addagent/manage_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,8 @@ int k_bulkload(const char *cmdbulk)
time2 = time(0);


/* Source is time1+ time2 +pid + ppid */
#ifndef WIN32
#ifdef __OpenBSD__
srandomdev();
#else
srandom((unsigned)(time2 + time1 + getpid() + getppid()));
#endif
#else
srandom(time2 + time1 + getpid());
#endif

srandom_init();

rand1 = random();

Expand Down
10 changes: 1 addition & 9 deletions src/addagent/validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@ char *OS_AddNewAgent(const char *name, const char *ip, const char *id)
char nid[9];


#ifndef WIN32
#ifdef __OpenBSD__
srandomdev();
#else
srandom((unsigned)(time(0) + getpid() + getppid()));
#endif
#else
srandom(time(0) + getpid());
#endif
srandom_init();

muname = getuname();

Expand Down
6 changes: 1 addition & 5 deletions src/client-agent/agentd.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ void AgentdStart(const char *dir, int uid, int gid, const char *user, const char


/* Initial random numbers */
#ifdef __OpenBSD__
srandomdev();
#else
srandom((unsigned)(time(0) + getpid()+ pid + getppid()));
#endif
srandom_init();

random();

Expand Down
9 changes: 9 additions & 0 deletions src/headers/randombytes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


#ifndef __RANDOMBYTES_H
#define __RANDOMBYTES_H

void randombytes(void *ptr, unsigned int length);
void srandom_init(void);

#endif
1 change: 1 addition & 0 deletions src/headers/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ extern const char *__local_name;
#include "read-agents.h"
#include "report_op.h"
#include "string_op.h"
#include "randombytes.h"

#include "os_xml/os_xml.h"
#include "os_regex/os_regex.h"
Expand Down
10 changes: 1 addition & 9 deletions src/logcollector/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,7 @@ int main(int argc, char **argv)
const char *cfg = DEFAULTCPATH;

/* Setuping up random */
#ifndef WIN32
#ifdef __OpenBSD__
srandomdev();
#else
srandom((unsigned int)time(0));
#endif
#else
srandom(time(0))
#endif
srandom_init();

/* Setting the name */
OS_SetName(ARGV0);
Expand Down
8 changes: 2 additions & 6 deletions src/remoted/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,8 @@ int main(int argc, char **argv)
StartSIG(ARGV0);


/* Creating some randoness */
#ifdef __OpenBSD__
srandomdev();
#else
srandom((unsigned)(time(0) + getpid()+ i));
#endif
/* Setup random */
srandom_init();

random();

Expand Down
63 changes: 63 additions & 0 deletions src/shared/randombytes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifdef WIN32
#include "windows.h"
#else
#include <sys/stat.h>
#include <fcntl.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include "shared.h"


void randombytes(void *ptr, unsigned int length)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declaration was void randombytes(unsigned char * ptr,unsigned int length);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks needs this to be void so that we can file just about anything. Might be some downsides to that. Do you know of any?

{

char failed = 0;

#ifdef WIN32

static HCRYPTPROV prov = 0;
if (prov == 0) {
if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, 0)) {
failed = 1;
}
}
if (!failed && !CryptGenRandom(prov, length, ptr)) {
failed = 1;
}

#else

int fh;
if ((fh = open("/dev/urandom", O_RDONLY)) >= 0 || (fh = open("/dev/random", O_RDONLY)) >= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

open() returns an int: http://linux.die.net/man/3/open

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crap changed from fopen to open :) and did not correct everything. Will fix thank you

if (read(fh, ptr, length) == 0) {
failed = 1;
}
close(fh);
} else {
failed = 1;
}

#endif

if (failed) {
ErrorExit("%s: ERROR: randombytes failed for all possible methods for accessing random data", __local_name);
}
}


void srandom_init(void)
{

#ifndef WIN32
unsigned int seed;
#ifdef __OpenBSD__
srandomdev();
#else
randombytes(&seed, sizeof seed);
srandom(seed);
#endif // __OpenBSD__
#endif // Win32

}