usnistgov / SP800-90B_EntropyAssessment

The SP800-90B_EntropyAssessment C++package implements the min-entropy assessment methods included in Special Publication 800-90B.
202 stars 87 forks source link

Seed the pseudo random number generator correctly. #45

Closed paulidale closed 5 years ago

paulidale commented 6 years ago

Seeding the pseudo random number generator before every shuffle using a value that changes once per second will mean that the different threads will be using the same sequence of random values. This is not intended.

Instead, seed the pseudo random number generator onces globally and each thread will get a different sequence of values.

paulidale commented 6 years ago

~~It might be more prudent to use rand_r and to seed using gettid() ^ time(NULL). Getting this thread safe could be fun.~~

paulidale commented 6 years ago

This has been updated so that the access to rand(3) is thread safe.

celic commented 6 years ago

Thanks for the suggestion. Yeah you are correct... by seeding in each shuffle, if they happen in quick succession, there isn't really any randomness going on. A single seed call would be better. The code is under development to update it with the final version of the draft so I will add this in.

paulidale commented 6 years ago

C++ will deconstruct the mutex at the end of the function and this unlocks it.

It's worse than reseeding with the same value (which is bad), the non thread-safe calls to rand(3) will also have problems.