blib/lib/Crypt/URandom.pm

filter: Statementstmt 69.2% 81 / 117 Branchbran 30.0% 9 / 30 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 90.4% 19 / 21 Podpod 100% 2 / 2 totaltotal 60.0% 111 / 185 CC 19 SCAR 37.0
CC
19
Cov
58%
_read_urandom_fs
132.0
_init
20.0
_urandom
4.0
CRAP 40.3
↑ Summary URandom.xs »
1 package Crypt::URandom;
2  
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Exporter();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 *import = \&Exporter::import;
9 8 our @EXPORT_OK = qw(
10 urandom
11 urandom_ub
12 getrandom
13 );
14  
15 8 our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK, );
16  
17 8 our @CARP_NOT = ('Crypt::URandom');
18  
19 8 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
20 8 our $VERSION = '0.55';
21 eval {
22 8 require XSLoader;
23  
24 8 XSLoader::load( __PACKAGE__, $VERSION );
25 8 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require XSLoader; XSLoader::load(__PACKAGE__, $Crypt::URandom::VERSION) } } or do { () } 8 0 8
26 };
27 }
28  
29 ## no critic (ProhibitConstantPragma)
30 # using constant for the speed benefit of constant-folding of values
31  
32 8 ▶use constant CRYPT_SILENT => 64; # hex 40
Subroutine BEGIN: called CC=1 SCAR=0.0
33 8 ▶use constant PROV_RSA_FULL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 8 ▶use constant VERIFY_CONTEXT => 4_026_531_840; # hex 'F0000000'
Subroutine BEGIN: called CC=1 SCAR=0.0
35 8 ▶use constant W2K_MAJOR_VERSION => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 8 ▶use constant W2K_MINOR_VERSION => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 8 ▶use constant OS_FREEBSD => $OSNAME eq 'freebsd';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 8 ▶use constant OS_WIN32 => $OSNAME eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 8 ▶use constant PATH => do {
Subroutine BEGIN: called CC=1 SCAR=0.0
40 8 my $path = '/dev/urandom';
41 8 if ( OS_FREEBSD() ) {
42 $path = '/dev/random'; # FreeBSD's /dev/random is non-blocking
43 }
44 8 $path;
45 8 };
46 8 ▶use constant GETRANDOM_AVAILABLE => do {
Subroutine BEGIN: called CC=2 SCAR=6.9
47 8 my $result = 0;
48 eval {
49 8 my $correct_length = 2;
50 8 $result = getrandom($correct_length);
51 8 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $correct_length = 2; $result = &getrandom($correct_length) } } or do { $result = undef } 7 1 8
52 1 $result = undef;
53 };
54 8 $result;
55 8 };
56 8 ▶use constant SYSTEM_CALL_FAILED => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 ## use critic
59  
60 8 my $_initialised;
61 my $_context;
62 8 my $_cryptgenrandom;
63 8 my $_rtlgenrand;
64 8 my $_urandom_handle;
65  
66 0 ▶sub _init {
Subroutine _init: not called CC=4 SCAR=30.0
67 0 ▶ if ( !( ( defined $_initialised ) && ( $_initialised == $PROCESS_ID ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_initialised and $_initialised == $Crypt::URandom::PROCESS_ID) 0 0 0
Condition0%0/3
defined $_initialised and $_initialised == $Crypt::URandom::PROCESS_ID
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_initialised and $_initialised == $Crypt::URandom::PROCESS_ID
defined $_initialised $_initialised == $Crypt::URandom::PROCESS_ID
Truth table0%0/3
A and B
A= defined $_initialisedB= $_initialised == $Crypt::URandom::PROCESS_ID
ABresult
0X0
100
111
68 0 if ( OS_WIN32() ) {
69 require Win32;
70 require Win32::API;
71 require Win32::API::Type;
72 my ( $major, $minor ) = ( Win32::GetOSVersion() )[ 1, 2 ];
73 my $ntorlower = ( $major < W2K_MAJOR_VERSION() ) ? 1 : 0;
74 my $w2k =
75 ( $major == W2K_MAJOR_VERSION()
76 and $minor == W2K_MINOR_VERSION() )
77 ? 1
78 : 0;
79  
80 if ($ntorlower) {
81 Carp::croak(
82 'No secure alternative for random number generation for Win32 versions older than W2K'
83 );
84 }
85 elsif ($w2k) {
86  
87 my $crypt_acquire_context_a =
88 Win32::API->new( 'advapi32', 'CryptAcquireContextA', 'PPPNN',
89 'I' );
90 if ( !defined $crypt_acquire_context_a ) {
91 Carp::croak(
92 "Could not import CryptAcquireContext: $EXTENDED_OS_ERROR"
93 );
94 }
95  
96 my $context = chr(0) x Win32::API::Type->sizeof('PULONG');
97 my $result =
98 $crypt_acquire_context_a->Call( $context, 0, 0,
99 PROV_RSA_FULL(), CRYPT_SILENT() | VERIFY_CONTEXT() );
100 my $pack_type = Win32::API::Type::packing('PULONG');
101 $context = unpack $pack_type, $context;
102 if ( !$result ) {
103 Carp::croak(
104 "CryptAcquireContext failed: $EXTENDED_OS_ERROR");
105 }
106  
107 my $crypt_gen_random =
108 Win32::API->new( 'advapi32', 'CryptGenRandom', 'NNP', 'I' );
109 if ( !defined $crypt_gen_random ) {
110 Carp::croak(
111 "Could not import CryptGenRandom: $EXTENDED_OS_ERROR");
112 }
113 $_context = $context;
114 $_cryptgenrandom = $crypt_gen_random;
115 }
116 else {
117 my $rtlgenrand =
118 Win32::API->new( 'advapi32', <<'_RTLGENRANDOM_PROTO_' );
119 INT SystemFunction036(
120 PVOID RandomBuffer,
121 ULONG RandomBufferLength
122 )
123 _RTLGENRANDOM_PROTO_
124 if ( !defined $rtlgenrand ) {
125 Carp::croak(
126 "Could not import SystemFunction036: $EXTENDED_OS_ERROR"
127 );
128 }
129 $_rtlgenrand = $rtlgenrand;
130 }
131 }
132 else {
133 0 require FileHandle;
134 0 ▶ $_urandom_handle = FileHandle->new( PATH(), Fcntl::O_RDONLY() )
Branch: 1 branch
BranchTrueFalseTotal
$_urandom_handle = "FileHandle"->new("/dev/urandom", 0) or Carp::croak('Failed to open /dev/urandom' . " for reading:$Crypt::URandom::OS_ERROR") 0 0 0
135 or Carp::croak(
136 q[Failed to open ] . PATH() . qq[ for reading:$OS_ERROR] );
137 0 binmode $_urandom_handle;
138 }
139 0 $_initialised = $PROCESS_ID;
140 }
141 0 return;
142 }
143  
144 7 ▶sub urandom_ub {
Subroutine urandom_ub: called CC=1 SCAR=0.0
Pod documented
145 7 my ($length) = @_;
146 7 return _urandom( 'sysread', $length );
147 }
148  
149 14 ▶sub urandom {
Subroutine urandom: called CC=1 SCAR=0.0
Pod documented
150 14 my ($length) = @_;
151 14 return _urandom( 'read', $length );
152 }
153  
154 21 ▶sub _urandom {
Subroutine _urandom: called CC=4 SCAR=13.9
155 21 my ( $type, $length ) = @_;
156  
157 21 my $length_ok;
158 21 ▶ if ( defined $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length) 20 1 21
159 20 ▶ if ( $length =~ /^\d+$/xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length =~ /^\d+$/msx) 19 1 20
160 19 $length_ok = 1;
161 }
162 }
163 21 ▶ if ( !$length_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($length_ok) 2 19 21
164 2 Carp::croak(
165 'The length argument must be supplied and must be an integer');
166 }
167 19 if ( !GETRANDOM_AVAILABLE() ) {
168 _init();
169 }
170 19 if ( OS_WIN32() ) {
171 my $urandom = chr(0) x $length;
172 if ($_cryptgenrandom) {
173  
174 my $result = $_cryptgenrandom->Call( $_context, $length, $urandom );
175 if ( !$result ) {
176 Carp::croak("CryptGenRandom failed: $EXTENDED_OS_ERROR");
177 }
178 }
179 elsif ($_rtlgenrand) {
180  
181 my $result = $_rtlgenrand->Call( $urandom, $length );
182 if ( !$result ) {
183 Carp::croak("RtlGenRand failed: $EXTENDED_OS_ERROR");
184 }
185 }
186 return $urandom;
187 }
188 elsif ( GETRANDOM_AVAILABLE() ) {
189 19 return getrandom($length);
190 }
191 else {
192 return _read_urandom_fs( $type, $length );
193 }
194 0 return;
195 }
196  
197 0 ▶sub _read_urandom_fs {
Subroutine _read_urandom_fs: not called CC=11 SCAR=48.8
198 0 my ( $type, $length ) = @_;
199 0 my $original_length = $length;
200 0 my $urandom;
201 BUFFER_FILLED: {
202 0 my $result;
203 0 ▶ if ( defined $urandom ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $urandom) { } 0 0 0
204 0 $length = $original_length - ( length $urandom );
205 0 $result = $_urandom_handle->$type( my $buffer, $length );
206 0 ▶ if ( defined $buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $buffer) 0 0 0
207 0 $urandom .= $buffer;
208 }
209 }
210 else {
211 0 $result = $_urandom_handle->$type( my $buffer, $length );
212 0 ▶ if ( defined $buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $buffer) 0 0 0
213 0 $urandom .= $buffer;
214 }
215 }
216 0 ▶ if ( ( defined $urandom )
Branch: 5 branches
BranchTrueFalseTotal
if (defined $urandom and length $urandom == $original_length) { } 0 0 0
elsif (not defined $result) { } 0 0 0
elsif ($result == 0) { } 0 0 0
elsif ($result == -1 and $Crypt::URandom::OS_ERROR == POSIX::EINTR()) { } 0 0 0
elsif ($result != -1) { } 0 0 0
Condition0%0/3
defined $urandom and length $urandom == $original_length
!ll&&!rl&&r
000
MC/DC0%0/2
defined $urandom and length $urandom == $original_length
defined $urandom length $urandom == $original_length
Truth table0%0/3
A and B
A= defined $urandomB= length $urandom == $original_length
ABresult
0X0
100
111
217 && ( length $urandom == $original_length ) )
218 {
219 }
220 elsif ( !defined $result ) {
221 0 my $error = $EXTENDED_OS_ERROR;
222 0 $_urandom_handle = undef;
223 0 $_initialised = undef;
224 0 Carp::croak( q[Failed to read from ] . PATH() . qq[:$error] );
225 }
226 elsif ( $result == 0 ) {
227 0 $_urandom_handle = undef;
228 0 $_initialised = undef;
229 0 Carp::croak( PATH() . q[ has returned EOF] );
230 }
231 ▶ elsif (( $result == SYSTEM_CALL_FAILED() )
Condition0%0/3
$result == -1 and $Crypt::URandom::OS_ERROR == POSIX::EINTR()
!ll&&!rl&&r
000
MC/DC0%0/2
$result == -1 and $Crypt::URandom::OS_ERROR == POSIX::EINTR()
$result == -1 $Crypt::URandom::OS_ERROR == POSIX::EINTR()
Truth table0%0/3
A and B
A= $result == -1B= $Crypt::URandom::OS_ERROR == POSIX::EINTR()
ABresult
0X0
100
111
232 && ( $OS_ERROR == POSIX::EINTR() ) )
233 {
234 0 redo BUFFER_FILLED;
235 }
236 elsif ( $result != SYSTEM_CALL_FAILED() ) {
237 0 redo BUFFER_FILLED;
238 }
239 else {
240 0 my $error = $EXTENDED_OS_ERROR;
241 0 $_urandom_handle = undef;
242 0 $_initialised = undef;
243 0 Carp::croak( q[Failed to read from ] . PATH() . qq[:$error] );
244 }
245 }
246 0 return $urandom;
247 }
248  
249 8 1; # Magic true value required at end of module
250 __END__
↑ Summary URandom.xs »