blib/lib/BSD/arc4random.pm

filter: Statementstmt 61.1% 55 / 90 Branchbran 32.3% 11 / 34 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 73.3% 11 / 15 Podpod 100% 7 / 7 totaltotal 54.3% 87 / 160 CC 20 SCAR 23.8
CC
20
Cov
52%
arc4random_bytes
42.0
arc4random_uniform
6.2
arc4random_addrandom
6.0
CRAP 10.8
↑ Summary arc4random.c »
1 # $MirOS: contrib/hosted/tg/code/BSD::arc4random/lib/BSD/arc4random.pm,v 1.10 2011/06/05 23:19:04 tg Exp $
2 #-
3 # Copyright (c) 2008, 2009, 2010, 2011
4 # Thorsten Glaser <tg@mirbsd.org>
5 # Copyright (c) 2009
6 # Benny Siegert <bsiegert@mirbsd.org>
7 #
8 # Provided that these terms and disclaimer and all copyright notices
9 # are retained or reproduced in an accompanying document, permission
10 # is granted to deal in this work without restriction, including un-
11 # limited rights to use, publicly perform, distribute, sell, modify,
12 # merge, give away, or sublicence.
13 #
14 # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15 # the utmost extent permitted by applicable law, neither express nor
16 # implied; without malicious intent or gross negligence. In no event
17 # may a licensor, author or contributor be held liable for indirect,
18 # direct, other damage, loss, or other issues arising in any way out
19 # of dealing in the work, even if advised of the possibility of such
20 # damage or existence of a defect, except proven that it results out
21 # of said person's immediate fault when using the work as intended.
22  
23 package BSD::arc4random;
24  
25 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 require Exporter;
30 1 require DynaLoader;
31 1 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 $VERSION = "1.50";
33 1 @ISA = qw(Exporter DynaLoader);
34 1 @EXPORT = qw();
35 1 @EXPORT_OK = qw(
36 $RANDOM
37 &arc4random
38 &arc4random_addrandom
39 &arc4random_bytes
40 &arc4random_pushb
41 &arc4random_pushk
42 &arc4random_stir
43 &arc4random_uniform
44 );
45 1 %EXPORT_TAGS = (
46 all => [ @EXPORT_OK ],
47 );
48 }
49  
50 1 ▶use vars qw($RANDOM); # public tied integer variable
Subroutine BEGIN: called CC=1 SCAR=0.0
51 sub have_kintf() {} # public constant function, prototyped
52  
53 1 my $have_threadlock = 1;
54 1 my $arcfour_lock;
55 1 eval { require threads::shared; };
56 1 ▶if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
57 0 $have_threadlock = 0; # module not available
58 } else {
59 # private thread lock
60 1 threads::shared::share($arcfour_lock);
61 };
62  
63 1 bootstrap BSD::arc4random $BSD::arc4random::VERSION;
64  
65 # public thread-safe functions
66 sub
67 arc4random()
68 9 ▶{
Subroutine arc4random: called CC=2 SCAR=7.2
Pod documented
69 9 ▶ lock($arcfour_lock) if $have_threadlock;
Branch: 1 branch
BranchTrueFalseTotal
if $have_threadlock 9 0 9
70 9 return &arc4random_xs();
71 }
72  
73 sub
74 arc4random_addrandom($)
75 0 ▶{
Subroutine arc4random_addrandom: not called CC=2 SCAR=17.9
Pod documented
76 0 my $buf = shift;
77  
78 0 ▶ lock($arcfour_lock) if $have_threadlock;
Branch: 1 branch
BranchTrueFalseTotal
if $have_threadlock 0 0 0
79 0 return &arc4random_addrandom_xs($buf);
80 }
81  
82 sub
83 arc4random_pushb($)
84 3 ▶{
Subroutine arc4random_pushb: called CC=2 SCAR=7.1
Pod documented
85 3 my $buf = shift;
86  
87 3 ▶ lock($arcfour_lock) if $have_threadlock;
Branch: 1 branch
BranchTrueFalseTotal
if $have_threadlock 3 0 3
88 3 return &arc4random_pushb_xs($buf);
89 }
90  
91 sub
92 arc4random_pushk($)
93 0 ▶{
Subroutine arc4random_pushk: not called CC=2 SCAR=17.9
Pod documented
94 0 my $buf = shift;
95  
96 0 ▶ lock($arcfour_lock) if $have_threadlock;
Branch: 1 branch
BranchTrueFalseTotal
if $have_threadlock 0 0 0
97 0 return &arc4random_pushk_xs($buf);
98 }
99  
100 sub
101 arc4random_stir()
102 0 ▶{
Subroutine arc4random_stir: not called CC=2 SCAR=17.9
Pod documented
103 0 ▶ lock($arcfour_lock) if $have_threadlock;
Branch: 1 branch
BranchTrueFalseTotal
if $have_threadlock 0 0 0
104 0 &arc4random_stir_xs();
105 0 return;
106 }
107  
108 sub
109 arc4random_bytes($;$)
110 0 ▶{
Subroutine arc4random_bytes: not called CC=6 SCAR=37.4
Pod documented
111 0 my ($len, $buf) = @_;
112 0 my $val;
113 0 my $vleft = 0;
114 0 my $rv = '';
115 0 my $idx = 0;
116  
117 0 ▶ if (defined($buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $buf) 0 0 0
118 0 $val = arc4random_pushb($buf);
119 0 $vleft = 4;
120 }
121 0 ▶ while (($len - $idx) >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len - $idx >= 4) 0 0 0
122 0 ▶ if ($vleft < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vleft < 4) 0 0 0
123 0 $val = arc4random();
124 0 $vleft = 4;
125 }
126 0 vec($rv, $idx / 4, 32) = $val;
127 0 $idx += 4;
128 0 $vleft = 0;
129 }
130 0 ▶ while ($idx < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < $len) 0 0 0
131 0 ▶ if ($vleft == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vleft == 0) 0 0 0
132 0 $val = arc4random();
133 0 $vleft = 4;
134 }
135 0 vec($rv, $idx, 8) = $val & 0xFF;
136 0 $idx++;
137 0 $val >>= 8;
138 0 $vleft--;
139 }
140 0 return $rv;
141 }
142  
143 # Perl implementation of arc4random_uniform(3)
144 # C implementation contributed by djm@openbsd.org, Jinmei_Tatuya@isc.org
145 #
146 # Calculate a uniformly distributed random number less than upper_bound
147 # avoiding "modulo bias".
148 #
149 # Uniformity is achieved by generating new random numbers until the one
150 # returned is outside the range [0, 2**32 % upper_bound). This
151 # guarantees the selected random number will be inside
152 # [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
153 # after reduction modulo upper_bound.
154  
155 sub
156 arc4random_uniform($)
157 8 ▶{
Subroutine arc4random_uniform: called CC=6 SCAR=18.2
Pod documented
158 8 my $upper_bound = shift;
159 8 my $r;
160 my $min;
161  
162 8 ▶ return 0 unless defined($upper_bound);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $upper_bound 0 8 8
163 # convert upper_bound to 32-bit UV (unsigned integer value)
164 8 $upper_bound &= 0xFFFFFFFF;
165 8 ▶ return 0 if $upper_bound < 2 || $upper_bound > 0xFFFFFFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $upper_bound < 2 or $upper_bound > 4294967295 0 8 8
Condition33%1/3
$upper_bound < 2 or $upper_bound > 4294967295
l!l&&r!l&&!r
008
MC/DC0%0/2
$upper_bound < 2 or $upper_bound > 4294967295
$upper_bound < 2 $upper_bound > 4294967295
Truth table33%1/3
A or B
A= $upper_bound < 2B= $upper_bound > 4294967295
ABresult
1X1
011
000
166  
167 # Calculate (2**32 % upper_bound) avoiding 64-bit math
168 8 ▶ if ($upper_bound > 0x80000000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upper_bound > 2147483648) { } 2 6 8
169 # 2**32 - upper_bound (only one "value area")
170 2 $min = 1 + (~$upper_bound & 0xFFFFFFFF);
171 } else {
172 # (2**32 - x) % x == 2**32 % x when x <= 2**31
173 6 $min = (0xFFFFFFFF - $upper_bound + 1) % $upper_bound;
174 }
175  
176 # This could theoretically loop forever but each retry has
177 # p > 0.5 (worst case, usually far better) of selecting a
178 # number inside the range we need, so it should rarely need
179 # to re-roll.
180 8 while (1) {
181 9 $r = arc4random();
182 9 ▶ last if $r >= $min;
Branch: 1 branch
BranchTrueFalseTotal
if $r >= $min 8 1 9
183 }
184  
185 8 return ($r % $upper_bound);
186 }
187  
188 # private implementation for a tied $RANDOM variable
189 sub
190 TIESCALAR
191 1 ▶{
Subroutine TIESCALAR: called CC=4 SCAR=17.2
192 1 my $class = shift;
193 1 my $max = shift;
194  
195 1 ▶ if (!defined($max) || ($max = int($max)) > 0xFFFFFFFE || $max < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $max or ($max = int $max) > 4294967294 or $max < 0) 0 1 1
Condition33%2/6
not defined $max or ($max = int $max) > 4294967294 or $max < 0
l!l&&r!l&&!r
001
not defined $max or ($max = int $max) > 4294967294
l!l&&r!l&&!r
001
MC/DC0%0/3
not defined $max or ($max = int $max) > 4294967294 or $max < 0
not defined $max ($max = int $max) > 4294967294 $max < 0
Truth table25%1/4
not(A) or B or C
A= not defined $maxB= ($max = int $max) > 4294967294C= $max < 0
ABCresult
1XX1
01X1
0011
0000
196 0 $max = 0;
197 }
198  
199 1 return bless \$max, $class;
200 }
201  
202 sub
203 FETCH
204 4 ▶{
Subroutine FETCH: called CC=2 SCAR=7.2
205 4 my $self = shift;
206  
207 4 ▶ return ($$self == 0 ? arc4random() : arc4random_uniform($$self + 1));
Branch: 1 branch
BranchTrueFalseTotal
$$self == 0 ? : 0 4 4
208 }
209  
210 sub
211 STORE
212 2 ▶{
Subroutine STORE: called CC=1 SCAR=0.0
213 2 my $self = shift;
214 2 my $value = shift;
215  
216 2 arc4random_pushb($value);
217 }
218  
219 # tie the public $RANDOM variable to an mksh-style implementation
220 1 tie $RANDOM, 'BSD::arc4random', 0x7FFF;
221  
222 # we are nice and re-seed perl's internal PRNG as well
223 1 srand(arc4random_pushb(pack("F*", rand(), rand(), rand(), rand())));
224  
225 1 1;
226 __END__
↑ Summary arc4random.c »