Crypt::Random::Seed (3)
Leading comments
Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16) Standard preamble: ========================================================================
NAME
Crypt::Random::Seed - Simple method to get strong randomnessVERSION
Version 0.03SYNOPSIS
use Crypt::Random::Seed; my $source = new Crypt::Random::Seed; die "No strong sources exist" unless defined $source; my $seed_string = $source->random_bytes(4); my @seed_values = $source->random_values(4); # Only non-blocking sources my $nonblocking_source = Crypt::Random::Seed->new( NonBlocking=>1 ); # Blacklist sources (never choose the listed sources) my $nowin32_source = Crypt::Random::Seed->new( Never=>['Win32'] ); # Whitelist sources (only choose from these sources) my $devr_source = Crypt::Random::Seed->new( Only=>['TESHA2'] ); # Supply a custom source. my $user_src = Crypt::Random::Seed->new( Source=>sub { myfunc(shift) } ); # Or supply a list of [name, sub, is_blocking, is_strong] $user_src = Crypt::Random::Seed->new( Source=>['MyRandomFunction',sub {myfunc(shift)},0,1] ); # Given a source there are a few things we can do: say "My randomness source is ", $source->name(); say "I am a blocking source" if $source->is_blocking(); say "I am a strong randomness source" if $source->is_strong() say "Four 8-bit numbers:", join(",", map { ord $source->random_bytes(1) } 1..4);' say "Four 32-bit numbers:", join(",", $source->random_values(4));
DESCRIPTION
A simple mechanism to get strong randomness. The main purpose of this module is to provide a simple way to generate a seed for aThe randomness sources used are, in order:
- User supplied.
- If the constructor is called with a Source defined, then it is used. It is not checked vs. other flags (NonBlocking, Never, Only).
- Win32 Crypto API.
-
This will use "CryptGenRandom" on Windows 2000 and "RtlGenRand" on
Windows XPand newer. According toMSDN, these are well-seeded CSPRNGs (FIPS186-2 or AES-CTR), so will be non-blocking.
- EGD/PRNGD.
-
This looks for sockets that speak the EGD<egd.sourceforge.net> protocol, includingPRNGD<prngd.sourceforge.net>. These are userspace entropy daemons that are commonly used by OpenSSL, OpenSSH, and GnuGP. The locations searched are "/var/run/egd-pool", "/dev/egd-pool", "/etc/egd-pool", and "/etc/entropy".EGDis blocking, whilePRNGDis non-blocking (like the Win32API, it is really a seededCSPRNG). However there is no way to tell them apart, so we treat it as blocking. If your O/S supports /dev/random, considerHAVEGED<www.issihosts.com/haveged> as an alternative (a system daemon that refills /dev/random as needed).
- /dev/random.
-
The strong source of randomness on most UNIX-like systems. Cygwin uses
this, though it maps to the Win32 API. On almost all systems this is a blocking source of randomness --- if it runs out of estimated entropy, it will hang until more has come into the system. If this is an issue, which it often is on embedded devices, running a tool such asHAVEGED<www.issihosts.com/haveged> will help immensely.
- /dev/urandom.
- A nonblocking source of randomness that we label as weak, since it will continue providing output even if the actual entropy has been exhausted.
- TESHA2.
- Crypt::Random::TESHA2 is a Perl module that generates random bytes from an entropy pool fed with timer/scheduler variations. Measurements and tests are performed on installation to determine whether the source is considered strong or weak. This is entirely in portable userspace, which is good for ease of use, but really requires user verification that it is working as expected if we expect it to be strong. The concept is similar to Math::TrulyRandom though updated to something closer to what TrueRand 2.1 does vs. the obsolete version 1 that Math::TrulyRandom implements. It is very slow and has wide speed variability across platforms : I've seen numbers ranging from 40 to 150,000 bits per second.
A source can also be supplied in the constructor. Each of these sources will have its debatable points about perceived strength. E.g. Why is /dev/urandom considered weak while Win32 is strong? Can any userspace method such as TrueRand or
SOURCE TABLE
This table summarizes the default sources:
+------------------+-------------+------------+--------------------+ | SOURCE | STRENGTH | BLOCKING | NOTE | |------------------+-------------+------------+--------------------| | RtlGenRandom | Strong(1) | No | Default WinXP+ | |------------------+-------------+------------+--------------------| | CryptGenRandom | Strong(1) | No | Default Win2000 | |------------------+-------------+------------+--------------------| | EGD | Strong | Yes(2) | also PRNGD, etc. | |------------------+-------------+------------+--------------------| | /dev/random | Strong | Yes | Typical UNIX | |------------------+-------------+------------+--------------------| | /dev/urandom | Weak | No | Typical UNIX NB | |------------------+-------------+------------+--------------------| | TESHA2-strong | Strong | No | | |------------------+-------------+------------+--------------------| | TESHA2-weak | Weak | No | | +------------------+-------------+------------+--------------------+
The alias 'Win32' can be used in whitelist and blacklist and will match both the Win32 sources "RtlGenRandom" and "CryptGenRandom". The alias '
1) Both CryptGenRandom and RtlGenRandom are considered strong by this package, even though both are seeded CSPRNGs so should be the equal of /dev/urandom in this respect. The CryptGenRandom function used in Windows 2000 has some known issues so should be considered weaker. 2) EGD is blocking, PRNGD is not. We cannot tell the two apart. There are other software products that use the same protocol, and each will act differently. E.g. EGD mixes in system entropy on every request, while PRNGD mixes on a time schedule.
STRENGTH
In theory, a strong generator will provide true entropy. Even if a third
party knew a previous result and the entire state of the generator at any
time up to when their value was returned, they could still not effectively
predict the result of the next returned value. This implies the generator
must either be blocking to wait for entropy (e.g. /dev/random) or go through
some possibly time-consuming process to gather it (Creating a satisfactory strength measurement is problematic. The Win32 Crypto
Because of this confusion, I have removed the "Weak" configuration option that was present in version 0.01. It will now be ignored. You should be able to use a combination of whitelist, blacklist, and the source's "is_strong" return value to decide if this meets your needs. On Win32, you really only have a choice of Win32 and
BLOCKING
Win32,
IN PRACTICE
Use the default to get the best source known. If you know more about the
sources available, you can use a whitelist, blacklist, or a custom source.
In general, to get the best source (typically Win32 or /dev/random):
my $source = Crypt::Random::Seed->new();
To get a good non-blocking source (Win32 or /dev/urandom):
my $source = Crypt::Random::Seed->new(NonBlocking => 1);
METHODS
new
The constructor with no arguments will find the first available source in its fixed list and return an object that performs the defined methods. If no sources could be found (quite unusual) then the returned value will be undef.Optional parameters are passed in as a hash and may be mixed.
NonBlocking => boolean
Only non-blocking sources will be allowed. In practice this means
Only => [list of strings]
Takes an array reference containing one or more string source names. No source whose name does not match one of these strings will be chosen. The string 'Win32' will match either of the Win32 sources, and '
Never => [list of strings]
Takes an array reference containing one or more string source names. No source whose name matches one of these strings will be chosen. The string 'Win32' will match either of the Win32 sources, and '
Source => sub { ... }
Uses the given anonymous subroutine as the generator. The subroutine will be given an integer (the argument to "random_bytes") and should return random data in a string of the given length. For the purposes of the other object methods, the returned object will have the name 'User', and be considered non-blocking and non-strong.
Source => ['name', sub { ... }, is_blocking, is_strong]
Similar to the simpler source routine, but also allows the other source parameters to be defined. The name may not be one of the standard names listed in the ``name'' section.
random_bytes($n)
Takes an integer and returns a string of that size filled with random data. Returns an empty string if the argument is not defined or is not more than zero.random_values($n)
Takes an integer and returns an array of that many random 32-bit values. Returns an empty array if the argument is not defined or is not more than zero.name
Returns the text name of the random source. This will be one of: "User" for user defined, "CryptGenRandom" for Windows 2000 Cryptois_strong
Returns 1 or 0 indicating whether the source is considered a strong source of randomness. See the ``is_blocking
Returns 1 or 0 indicating whether the source can block on read. Be aware that even if a source doesn't block, it may be extremely slow.AUTHORS
Dana Jacobsen <dana@acm.org>ACKNOWLEDGEMENTS
To the best of my knowledge, Max Kanat-Alexander was the original author of the Perl code that uses the Win32David Oswald gave me a lot of help with
SEE ALSO
The first question one may ask is ``Why yet another module of this type?'' None of the modules onCrypt::Random::Source
A comprehensive system using multiple plugins. It has a niceCrypt::URandom
A great little module that is almost what I was looking for. Crypt::Random::Seed will act the same if given the constructor:
my $source = Crypt::Random::Seed->new( NonBlocking => 1, Only => [qw(/dev/random /dev/urandom Win32)] ); croak "No randomness source available" unless defined $source;
Or you can leave out the "Only" and have
Crypt::Random
Requires Math::Pari which makes it unacceptable in some environments. Has more features (numbers in arbitrary bigint intervals or bit sizes). Crypt::Random::Seed is taking a simpler approach, just handling returning octets and letting upstream modules handle the rest.Data::Entropy
An interesting module that contains a source encapsulation (defaults to system rand, but has many plugins), a goodUpstream modules
Some modules that could use this module to help them: Bytes::Random::Secure, Math::Random::ISAAC, Math::Random::Secure, and Math::Random::MT to name a few.COPYRIGHT
Copyright 2013 by Dana Jacobsen <dana@acm.org>This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The software is provided ``