Secure/RNG.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 78.5% 11 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 1 totaltotal 91.5% 76 / 83 CC 9 SCAR 7.2
CC
9
Cov
93%
_check_seed
4.0
_build_seeder
2.1
BEGIN
2.0
CRAP 2.1
« Secure.pm ↑ Summary
1 package Math::Random::Secure::RNG;
2 5 $Math::Random::Secure::RNG::VERSION = '0.080001';
3 # ABSTRACT: The underlying PRNG, as an object.
4  
5 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Math::Random::ISAAC;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Crypt::Random::Source::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use constant ON_WINDOWS => $^O =~ /Win32/i ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /Win32/i ? : 0 5 5
9 5 ▶use if ON_WINDOWS, 'Crypt::Random::Source::Strong::Win32';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 has seeder => (
12 is => 'ro',
13 lazy => 1,
14 builder => '_build_seeder',
15 );
16 # Default to a 512-bit key, which should be impossible to break. I wrote
17 # to the author of ISAAC and he said it's fine to not use a full 256
18 # integers to seed ISAAC.
19 5 has seed_size => (
20 is => 'ro',
21 # isa => 'Int',
22 default => 64,
23 );
24  
25 5 has seed => (
26 is => 'rw',
27 isa => \&_check_seed,
28 lazy => 1,
29 builder => '_build_seed',
30 clearer => 'clear_seed',
31 predicate => 'has_seed',
32 );
33  
34 5 has rng => (
35 is => 'ro',
36 lazy => 1,
37 builder => '_build_rng',
38 handles => ['irand', 'rand'],
39 clearer => 'clear_rng',
40 );
41  
42 has _for_pid => (
43 is => 'rw',
44 9 ▶ default => sub { $$ },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 5 );
46  
47 1 ▶sub _clear_for_pid { shift->_for_pid($$) }
Subroutine _clear_for_pid: called CC=1 SCAR=0.0
48  
49 5 before qw(irand rand) => '_maybe_clear_seed';
50  
51 80605 ▶sub _maybe_clear_seed {
Subroutine _maybe_clear_seed: called CC=2 SCAR=6.9
52 80605 my $self = shift;
53  
54 80605 ▶ if ($self->_for_pid != $$) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_for_pid != $$) 1 80604 80605
55 1 $self->clear_seed;
56 1 $self->_clear_for_pid;
57 }
58 }
59  
60  
61 9 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
62 9 my ($self) = @_;
63  
64 9 ▶ if ($self->seed_size < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->seed_size < 8) 2 7 9
65 2 warn "Setting seed_size to less than 8 is not recommended";
66 }
67 }
68  
69 22 ▶sub _check_seed {
Subroutine _check_seed: called CC=4 SCAR=13.9
70 22 my ($seed) = @_;
71 22 ▶ if (length($seed) < 8) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $seed < 8) { } 4 18 22
elsif (length $seed <= 10 and $seed =~ /^\d+$/) { } 4 28 32
72 4 warn "Your seed is less than 8 bytes (64 bits). It could be"
73 . " easy to crack";
74 }
75 # If it looks like we were seeded with a 32-bit integer, warn the
76 # user that they are making a dangerous, easily-crackable mistake.
77 # We do this during BUILD so that it happens during srand() in
78 # Math::Secure::RNG.
79 ▶ elsif (length($seed) <= 10 and $seed =~ /^\d+$/) {
Condition66%2/3
length $seed <= 10 and $seed =~ /^\d+$/
!ll&&!rl&&r
1404
MC/DC50%1/2
length $seed <= 10 and $seed =~ /^\d+$/
length $seed <= 10 $seed =~ /^\d+$/
Truth table66%2/3
A and B
A= length $seed <= 10B= $seed =~ /^\d+$/
ABresult
0X0
100
111
80 4 warn "RNG seeded with a 32-bit integer, this is easy to crack";
81 }
82  
83 # _check_seed is used as a type constraint, so needs to return 1.
84 22 return 1;
85 }
86  
87 3 ▶sub _build_seeder {
Subroutine _build_seeder: called CC=2 SCAR=7.2
88 3 my $factory = Crypt::Random::Source::Factory->new();
89 # On Windows, we want to always pick Crypt::Random::Source::Strong::Win32,
90 # which this will do.
91 3 if (ON_WINDOWS) {
92 return $factory->get_strong;
93 }
94  
95 3 my $source = $factory->get;
96 # Never allow rand() to be used as a source, it cannot possibly be
97 # cryptographically strong with 2^15 or 2^32 bits for its seed.
98 3 ▶ if ($source->isa('Crypt::Random::Source::Weak::rand')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa("Crypt::Random::Source::Weak::rand")) 0 3 3
99 0 $source = $factory->get_strong;
100 }
101 3 return $source;
102 }
103  
104 6 ▶sub _build_seed {
Subroutine _build_seed: called CC=1 SCAR=0.0
105 6 my ($self) = @_;
106 6 return $self->seeder->get($self->seed_size);
107 }
108  
109 12 ▶sub _build_rng {
Subroutine _build_rng: called CC=2 SCAR=7.0
110 12 my ($self) = @_;
111 12 my @seed_ints = unpack('L*', $self->seed);
112 12 my $rng = Math::Random::ISAAC->new(@seed_ints);
113 # One part of having a cryptographically-secure RNG is not being
114 # able to see the seed in the internal state of the RNG.
115 12 $self->clear_seed;
116 # It's faster to skip the frontend interface of Math::Random::ISAAC
117 # and just use the backend directly. However, in case the internal
118 # code of Math::Random::ISAAC changes at some point, we do make sure
119 # that the {backend} element actually exists first.
120 12 ▶ return $rng->{backend} ? $rng->{backend} : $rng;
Branch: 1 branch
BranchTrueFalseTotal
$rng->{'backend'} ? : 12 0 12
121 }
122  
123 5 1;
124  
125 __END__
« Secure.pm ↑ Summary