Skip to content

improve random seed to a slightly more random number #9

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions src/openbsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
#include <cstddef>
#include <cstring>
#include <ctime>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

inline
void arc4random_buf(void *buf, size_t nbytes)
{

for( size_t n = 0; n < nbytes; ++ n)
((char*)(buf))[n] = rand() %256;
}

inline
void arc4random_init(void)
{
srand( (unsigned int) time(NULL));
struct timeval tv;
gettimeofday(&tv, 0);
// this is not very good, but we lack a portable non-blocking API
srand( ((unsigned int) tv.tv_usec) ^ (unsigned int)getpid());
Comment on lines +22 to +25

Choose a reason for hiding this comment

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

I appreciate the problem, and I also value the simplicity. But: on the operating system lending this file its name, there's a non-blocking /dev/urandom. That's not POSIX, but quite the same on Linux, FreeBSD, and as far as I know also MacOS, and its various mutations.

Is there a strong argument for doing a gettimeofday syscall and a getpid syscall to supply low-quality randomness, rather than open, read, and close to get a good seed?

Copy link
Author

Choose a reason for hiding this comment

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

the key point is "as far as we know". Also this code might be used in places that can't see /dev. The seed is not a secret it just needs to vary a bit. I think this library has chosen to be "something that just works", and then this choice is good enough.

Choose a reason for hiding this comment

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

I see, though I'd like to point out that one of the common places where this kind of time-based seeding fails short is on booting of embedded devices without RTC. E.g., router turns on, takes (down to few milliseconds) exactly the same time to load initrd, boot kernel, launch admin interface, which launches the "first user setup" routine, which hence always ends up with the same time-based seed. I don't know how PIDs are randomized on such a system.

Now, you'll be right to exclaim "but a few milliseconds is still way much variation in µs!", and you'd be very right. But, fun awaits at least under Linux with userland time functions on platforms that don't have a hardware high-res timer register; in many cases, time gets systick-quantized, even on x86_64.

Hence my caveat: I 100% would merge this, it's indubitably an improvement, but for the cases where a non-varying seed is especially problematic, namely, when someone with the same device can reconstruct the same seed, I'm not sure it's a solution :)

Copy link
Owner

Choose a reason for hiding this comment

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

Hi guys,

thank you discussing and supporting this project.
This author did some investigations around that:

https://codingnest.com/generating-random-numbers-using-c-standard-library-the-problems/

It looks like there is not simple way to implement a platform independent solution.
/dev/urandom and gettimeofday would prevent us using the code within MS Windows.

}


Expand Down
2 changes: 2 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ int main()
std::string password = "top_secret";

std::string hash = bcrypt::generateHash(password);
std::cout << "Hash: " << hash << std::endl;

hash=bcrypt::generateHash(password);
std::cout << "Hash: " << hash << std::endl;

std::cout << "\"" << password << "\" : " << bcrypt::validatePassword(password,hash) << std::endl;
Expand Down