-
Notifications
You must be signed in to change notification settings - Fork 1.1k
seed random with a real random data #377
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
Changes from 6 commits
b22c4e9
7412d4e
150cc1a
f98efe9
675ae12
1c393e2
65638e7
3f063e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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) | ||
{ | ||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. open() returns an int: http://linux.die.net/man/3/open There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 in randombytes failed on all possible methods for accessing random data", __local_name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better but can we make it the same formatting many of the other error messages get. Here is a random example I found: https://github.com/ossec/ossec-hids/blob/master/src/shared/file_op.c#L454
Wish this was abstracted a bit better so you didn't have to do this each time. |
||
} | ||
} | ||
|
||
|
||
void srandom_init(void) | ||
{ | ||
|
||
#ifndef WIN32 | ||
unsigned int seed; | ||
#ifdef __OpenBSD__ | ||
srandomdev(); | ||
#else | ||
randombytes(&seed, sizeof seed); | ||
srandom(seed); | ||
#endif // __OpenBSD__ | ||
#endif // Win32 | ||
|
||
} |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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?