| 1 |
SYNOPSIS |
| 2 |
|
| 3 |
# Replace rand(). |
| 4 |
use Math::Random::Secure qw(rand); |
| 5 |
|
| 6 |
# Get a random number between 0 and 1 |
| 7 |
my $float = rand(); |
| 8 |
|
| 9 |
# Get a random integer (faster than int(rand)) |
| 10 |
use Math::Random::Secure qw(irand); |
| 11 |
my $int = irand(); |
| 12 |
|
| 13 |
# Random integer between 0 and 9 inclusive. |
| 14 |
$int = irand(10); |
| 15 |
|
| 16 |
# Random floating-point number greater than or equal to 0.0 and |
| 17 |
# less than 10.0. |
| 18 |
$float = rand(10); |
| 19 |
|
| 20 |
DESCRIPTION |
| 21 |
|
| 22 |
This module is intended to provide a cryptographically-secure |
| 23 |
replacement for Perl's built-in rand function. "Crytographically |
| 24 |
secure", in this case, means: |
| 25 |
|
| 26 |
* No matter how many numbers you see generated by the random number |
| 27 |
generator, you cannot guess the future numbers, and you cannot guess |
| 28 |
the seed. |
| 29 |
|
| 30 |
* There are so many possible seeds that it would take decades, |
| 31 |
centuries, or millenia for an attacker to try them all. |
| 32 |
|
| 33 |
* The seed comes from a source that generates relatively strong |
| 34 |
random data on your platform, so the seed itself will be as random as |
| 35 |
possible. |
| 36 |
|
| 37 |
See "IMPLEMENTATION DETAILS" for more information about the |
| 38 |
underlying systems used to implement all of these guarantees, and |
| 39 |
some important caveats if you're going to use this module for some |
| 40 |
very-high-security purpose. |
| 41 |
|
| 42 |
METHODS |
| 43 |
|
| 44 |
rand |
| 45 |
|
| 46 |
Should work exactly like Perl's built-in rand. Will automatically call |
| 47 |
srand if srand has never been called in this process or thread. |
| 48 |
|
| 49 |
There is one limitation--Math::Random::Secure is backed by a 32-bit |
| 50 |
random number generator. So if you are on a 64-bit platform and you |
| 51 |
specify a limit that is greater than 2^32, you are likely to get |
| 52 |
less-random data. |
| 53 |
|
| 54 |
srand |
| 55 |
|
| 56 |
Note: Under normal circumstances, you should not call this function, as |
| 57 |
rand and irand will automatically call it for you the first time they |
| 58 |
are used in a thread or process. |
| 59 |
|
| 60 |
Seeds the random number generator, much like Perl's built-in srand, |
| 61 |
except that it uses a much larger and more secure seed. The seed should |
| 62 |
be passed as a string of bytes, at least 8 bytes in length, and more |
| 63 |
ideally between 32 and 64 bytes. (See "seed" in |
| 64 |
Math::Random::Secure::RNG for more info.) |
| 65 |
|
| 66 |
If you do not pass a seed, a seed will be generated automatically using |
| 67 |
a secure mechanism. See "IMPLEMENTATION DETAILS" for more information. |
| 68 |
|
| 69 |
This function returns the seed that generated (or the seed that was |
| 70 |
passed in, if you passed one in). |
| 71 |
|
| 72 |
irand |
| 73 |
|
| 74 |
Works somewhat like "rand", except that it returns a 32-bit integer |
| 75 |
between 0 and 2^32. Should be faster than doing int(rand). |
| 76 |
|
| 77 |
Note that because it returns 32-bit integers, specifying a limit |
| 78 |
greater than 2^32 will have no effect. |
| 79 |
|
| 80 |
IMPLEMENTATION DETAILS |
| 81 |
|
| 82 |
Currently, Math::Random::Secure is backed by Math::Random::ISAAC, a |
| 83 |
cryptographically-strong random number generator with no known serious |
| 84 |
weaknesses. If there are significant weaknesses found in ISAAC, we will |
| 85 |
change our backend to a more-secure random number generator. The goal |
| 86 |
is for Math::Random::Secure to be cryptographically strong, not to |
| 87 |
represent some specific random number generator. |
| 88 |
|
| 89 |
Math::Random::Secure seeds itself using Crypt::Random::Source. The |
| 90 |
underlying implementation uses /dev/urandom on Unix-like platforms, and |
| 91 |
the RtlGenRandom or CryptGenRandom functions on Windows 2000 and above. |
| 92 |
(There is no support for versions of Windows before Windows 2000.) If |
| 93 |
any of these seeding sources are not available and you have other |
| 94 |
Crypt::Random::Source modules installed, Math::Random::Secure will use |
| 95 |
those other sources to seed itself. |
| 96 |
|
| 97 |
Making Math::Random::Secure Even More Secure |
| 98 |
|
| 99 |
We use /dev/urandom on Unix-like systems, because one of the |
| 100 |
requirements of duplicating rand is that we never block waiting for |
| 101 |
seed data, and /dev/random could do that. However, it's possible that |
| 102 |
/dev/urandom could run out of "truly random" data and start to use its |
| 103 |
built-in pseudo-random number generator to generate data. On most |
| 104 |
systems, this should still provide a very good seed for nearly all |
| 105 |
uses, but it may not be suitable for very high-security cryptographic |
| 106 |
circumstances. |
| 107 |
|
| 108 |
For Windows, there are known issues with CryptGenRandom on Windows 2000 |
| 109 |
and versions of Windows XP before Service Pack 3. However, there is no |
| 110 |
other built-in method of getting secure random data on Windows, and I |
| 111 |
suspect that these issues will not be significant for most applications |
| 112 |
of Math::Random::Secure. |
| 113 |
|
| 114 |
If either of these situations are a problem for your use, you can |
| 115 |
create your own Math::Random::Secure::RNG object with a different |
| 116 |
"seeder" argument, and set $Math::Random::Secure::RNG to your own |
| 117 |
instance of Math::Random::Secure::RNG. The "seeder" is an instance of |
| 118 |
Crypt::Random::Source::Base, which should allow you to use most |
| 119 |
random-data sources in existence for your seeder, should you wish. |
| 120 |
|
| 121 |
Seed Exhaustion |
| 122 |
|
| 123 |
Perl's built-in srand reads 32 bits from /dev/urandom. By default, we |
| 124 |
read 512 bits. This means that we are more likely to exhaust available |
| 125 |
truly-random data than the built-in srand is, and cause /dev/urandom to |
| 126 |
fall back on its psuedo-random number generator. Normally this is not a |
| 127 |
problem, since "srand" is only called once per Perl process or thread, |
| 128 |
but it is something that you should be aware of if you are going to be |
| 129 |
in a situation where you have many new Perl processes or threads and |
| 130 |
you have very high security requirements (on the order of generating |
| 131 |
private SSH or GPG keypairs, SSL private keys, etc.). |
| 132 |
|
| 133 |
SEE ALSO |
| 134 |
|
| 135 |
http://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator |
| 136 |
|
| 137 |
Describes the requirements and nature of a cryptographically-secure |
| 138 |
random number generator. |
| 139 |
|
| 140 |
http://en.wikipedia.org/wiki/CryptGenRandom, |
| 141 |
|
| 142 |
More information about the Windows functions we use to seed |
| 143 |
ourselves. The article also has some information about the weaknesses |
| 144 |
in Windows 2000's CryptGenRandom implementation. |
| 145 |
|
| 146 |
http://www.computerworld.com/s/article/9048438/Microsoft_confirms_that_XP_contains_random_number_generator_bug |
| 147 |
|
| 148 |
A news article about the Windows 2000/XP CryptGenRandom weakness, |
| 149 |
fixed in Vista and XP Service Pack 3. |
| 150 |
|
| 151 |
http://en.wikipedia.org/wiki/Random_number_generator_attack |
| 152 |
|
| 153 |
A description of ways to attack a random number generator, which can |
| 154 |
help in understanding why such a generator needs to be secure. |
| 155 |
|
| 156 |
Math::Random::Secure::RNG |
| 157 |
|
| 158 |
The underlying random-number generator and seeding code for |
| 159 |
Math::Random::Secure. |
| 160 |
|
| 161 |
Crypt::Source::Random |
| 162 |
|
| 163 |
Crypt::Random |
| 164 |
|
| 165 |
Math::TrulyRandom |
| 166 |
|
| 167 |
All of these modules contain generators for "truly random" data, but |
| 168 |
they don't contain a simple rand replacement and they can be very |
| 169 |
slow. |
| 170 |
|
| 171 |
SUPPORT |
| 172 |
|
| 173 |
Right now, the best way to get support for Math::Random::Secure is to |
| 174 |
email the author using the email address in the "AUTHORS" section |
| 175 |
below. |
| 176 |
|
| 177 |
BUGS |
| 178 |
|
| 179 |
Math::Random::Secure is relatively new, as of December 2010, but the |
| 180 |
modules that underlie it are very well-tested and have a long history. |
| 181 |
However, the author still welcomes all feedback and bug reports, |
| 182 |
particularly those having to do with the security assurances provided |
| 183 |
by this module. |
| 184 |
|
| 185 |
You can report a bug by emailing [email protected] or |
| 186 |
by using the RT web interface at |
| 187 |
https://rt.cpan.org/Ticket/Display.html?Queue=Math-Random-Secure. If |
| 188 |
your bug report is security-sensitive, you may also email it directly |
| 189 |
to the author using the email address in the "AUTHORS" section below. |
| 190 |
|