lib/Crypt/DRBG/Hash.pm

filter: Statementstmt 83.1% 89 / 107 Branchbran 60.0% 6 / 10 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 86.6% 13 / 15 Podpod 100% 1 / 1 totaltotal 78.7% 115 / 146 CC 14 SCAR 14.8
CC
14
Cov
80%
_add_32
12.0
new
8.0
_add_64
3.0
CRAP 4.4
« blib/lib/Crypt/DRBG.pm ↑ Summary blib/lib/Crypt/DRBG/HMAC.pm »
1 package Crypt::DRBG::Hash;
2 13 $Crypt::DRBG::Hash::VERSION = '0.001000';
3 13 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use parent 'Crypt::DRBG';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Crypt::DRBG::Hash - Fast, cryptographically secure PRNG
14  
15 =head1 SYNOPSIS
16  
17 use Crypt::DRBG::Hash;
18  
19 my $drbg = Crypt::DRBG::Hash->new(auto => 1);
20 my $data = $drbg->generate(42);
21 ... # do something with your 42 bytes here
22  
23 my $drbg2 = Crypt::DRBG::Hash->new(seed => "my very secret seed");
24 my $data2 = $drbg->generate(42);
25  
26 =head1 DESCRIPTION
27  
28 Crypt::DRBG::Hash is an implementation of the Hash_DRBG from NIST SP800-90A. It
29 is a fast, cryptographically secure PRNG. By default, it uses SHA-512.
30  
31 However, if provided a seed, it will produce the same sequence of bytes I<if
32 called the same way each time>. This makes it useful for simulations that
33 require good but repeatable random numbers.
34  
35 Note, however, that due to the way the DRBGs are designed, making a single
36 request and making multiple requests for the same number of bytes will result in
37 different data. For example, two 16-byte requests will not produce the same
38 values as one 32-byte request.
39  
40 This class derives from Crypt::DRBG, which provides several utility functions.
41  
42 =head1 SUBROUTINES/METHODS
43  
44 =head2 Crypt::DRBG::Hash->new(%params)
45  
46 Creates a new Crypt::DRBG::Hash.
47  
48 %params can contain all valid values for Crypt::DRBG::initialize, plus the
49 following.
50  
51 =over 4
52  
53 =item algo
54  
55 The algorithm to use for generating bytes. The default is "512", for
56 SHA-512. This provides optimal performance for 64-bit machines.
57  
58 If Perl (and hence Digest::SHA) was built with a compiler lacking 64-bit integer
59 support, use "256" here. "256" may also provide better performance for 32-bit
60 machines.
61  
62 =item func
63  
64 If you would like to use a different hash function, you can specify a function
65 implemeting your specific algorithm.
66  
67 For example, if you had C<Digest::BLAKE2> installed, you could do the following
68 to use BLAKE2b:
69  
70 my $drbg = Crypt::DRBG::Hash->new(
71 auto => 1,
72 func => \&Digest::BLAKE2::blake2b,
73 algo => 512
74 );
75 my $data = $drbg->generate(42);
76  
77 Note that the algo parameter is still required, in order to know how large a
78 seed to use.
79  
80 =back
81  
82 =back
83  
84 =cut
85  
86 1355 ▶sub new {
Subroutine new: called CC=7 SCAR=20.8
Pod documented
87 1355 my ($class, %params) = @_;
88  
89 1355 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
013550
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
90 1355 my $self = bless {}, $class;
91  
92 1355 ▶ my $algo = $self->{algo} = $params{algo} || '512';
Condition100%2/2
$params{'algo'} || '512'
l!l
134411
MC/DC100%1/1
$params{'algo'} || '512'
$params{'algo'}
Truth table100%2/2
A
A= $params{'algo'}
Aresult
11
00
93 1355 $algo =~ tr{/}{}d;
94 1355 ▶ $self->{s_func} = ($params{func} || Digest::SHA->can("sha$algo")) or
Branch: 1 branch
BranchTrueFalseTotal
$self->{"s_func"} = $params{"func"} || "Digest::SHA"->can("sha$algo") or die "Unsupported algorithm '${algo}'" 1355 0 1355
Condition66%2/3
$params{'func'} || 'Digest::SHA'->can("sha$algo")
l!l&&r!l&&!r
113540
MC/DC0%0/2
$params{'func'} || 'Digest::SHA'->can("sha$algo")
$params{'func'} 'Digest::SHA'->can("sha$algo")
Truth table66%2/3
A || B
A= $params{'func'}B= 'Digest::SHA'->can("sha$algo")
ABresult
1X1
011
000
95 die "Unsupported algorithm '$algo'";
96 1355 ▶ $self->{seedlen} = $algo =~ /^(384|512)$/ ? 111 : 55;
Branch: 1 branch
BranchTrueFalseTotal
$algo =~ /^(384|512)$/ ? : 459 896 1355
97 1355 $self->{reseed_interval} = 4294967295; # (2^32)-1
98 1355 $self->{bytes_per_request} = 2 ** 16;
99 1355 $self->{outlen} = substr($algo, -3) / 8;
100 1355 $self->{security_strength} = $self->{outlen} / 2;
101 1355 $self->{min_length} = $self->{security_strength};
102 1355 $self->{max_length} = 4294967295; # (2^32)-1
103  
104 # If we have a 64-bit Perl, make things much faster.
105 1355 my $is_64 = (4294967295 + 2) != 1;
106 1355 ▶ if ($is_64) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_64) { } 1355 0 1355
107 1355 $self->{s_add} = \&_add_64;
108 }
109 else {
110 0 require Math::BigInt;
111 0 eval { Math::BigInt->import(try => 'GMP') };
112  
113 $self->{s_mask} =
114 0 (Math::BigInt->bone << ($self->{seedlen} * 8)) - 1;
115 0 $self->{s_add} = \&_add_32;
116 }
117  
118 1355 $self->initialize(%params);
119  
120 1354 return $self;
121 }
122  
123 10790 ▶sub _add {
Subroutine _add: called CC=1 SCAR=0.0
124 10790 my ($self, @args) = @_;
125 10790 my $func = $self->{s_add};
126 10790 return $self->$func(@args);
127 }
128  
129 2708 ▶sub _derive {
Subroutine _derive: called CC=2 SCAR=6.9
130 2708 my ($self, $hashdata, $len) = @_;
131  
132 2708 my $count = ($len + ($self->{outlen} - 1)) / $self->{outlen};
133 2708 my $data = '';
134 2708 my $func = $self->{s_func};
135 2708 for (1..$count) {
136 5864 $data .= $func->(pack('CN', $_, $len * 8) . $hashdata);
137 }
138 2708 return substr($data, 0, $len);
139 }
140  
141 1354 ▶sub _seed {
Subroutine _seed: called CC=1 SCAR=0.0
142 1354 my ($self, $seed) = @_;
143  
144 1354 my $v = $self->_derive($seed, $self->{seedlen});
145 1354 my $c = $self->_derive("\x00$v", $self->{seedlen});
146 1354 $self->{state} = {c => $c, v => $v};
147 1354 $self->{reseed_counter} = 1;
148 1354 return 1;
149 }
150  
151 0 ▶sub _reseed {
Subroutine _reseed: not called CC=1 SCAR=6.9
152 0 my ($self, $seed) = @_;
153  
154 0 return $self->_seed("\x01$self->{state}{v}$seed");
155 }
156  
157 0 ▶sub _add_32 {
Subroutine _add_32: not called CC=3 SCAR=24.8
158 0 my ($self, @args) = @_;
159 0 my @items = map { Math::BigInt->new("0x" . unpack("H*", $_)) } @args;
160 0 my $final = Math::BigInt->bzero;
161 0 foreach my $val (@items) {
162 0 $final += $val;
163 }
164 0 $final &= $self->{s_mask};
165 0 my $data = substr($final->as_hex, 2);
166 0 ▶ $data = "0$data" if length($data) & 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $data & 1 0 0 0
167 0 $data = pack("H*", $data);
168 0 return ("\x00" x ($self->{seedlen} - length($data))) . $data;
169 }
170  
171 14828 ▶sub _add_64 {
Subroutine _add_64: called CC=3 SCAR=11.0
172 14828 my ($self, $x, @args) = @_;
173  
174 13 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
175  
176 14828 my $nbytes = $self->{seedlen} + 1;
177 14828 my $nu32s = $nbytes / 4;
178 # Optimize based on the fact that the first argument is always full-length.
179 14828 my @result = unpack('V*', reverse "\x00$x");
180 my @vals = map {
181 14828 [unpack('V*', reverse(("\x00" x ($nbytes - length($_))) . $_))]
182 } @args;
183  
184 14828 foreach my $i (0..($nu32s-1)) {
185 277200 my $total = $result[$i];
186 277200 foreach my $val (@vals) {
187 377888 $total += $val->[$i];
188 }
189 277200 $result[$i+1] += $total >> 32;
190 277200 $result[$i] = $total;
191 }
192 14828 return substr(reverse(pack("V*", @result[0..($nu32s-1)])), 1);
193 }
194  
195 2694 ▶sub _hashgen {
Subroutine _hashgen: called CC=2 SCAR=6.9
196 2694 my ($self, $v, $len) = @_;
197  
198 2694 my $func = $self->{s_func};
199 2694 my $count = int(($len + ($self->{outlen} - 1)) / $self->{outlen});
200 2694 my $data = '';
201 2694 for (1..$count) {
202 10758 $data .= $func->($v);
203 10758 $v = $self->_add($v, "\x01");
204 }
205 2694 return substr($data, 0, $len);
206 }
207  
208 =head2 $drbg->generate($bytes, $additional_data)
209  
210 Generate and return $bytes bytes. $bytes cannot exceed 2^16.
211  
212 If $additional_data is specified, add this additional data to the DRBG.
213  
214 =cut
215  
216 2694 ▶sub _generate {
Subroutine _generate: called CC=2 SCAR=6.9
217 2694 my ($self, $len, $seed) = @_;
218  
219 2694 $self->_check_reseed($len);
220  
221 2694 my ($func, $add) = @{$self}{qw/s_func s_add/};
222 2694 my ($c, $v) = @{$self->{state}}{qw/c v/};
223 2694 ▶ if (defined $seed) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seed) 1344 1350 2694
224 1344 my $w = $func->("\x02$v$seed");
225 1344 $v = $self->$add($v, $w);
226 }
227 2694 my $data = $self->_hashgen($v, $len);
228 2694 my $h = $func->("\x03$v");
229 2694 $v = $self->$add($v, $h, $c, pack("N*", $self->{reseed_counter}));
230 2694 $self->{reseed_counter}++;
231 2694 $self->{state}{v} = $v;
232 2694 return substr($data, 0, $len);
233 }
234  
235 =head1 AUTHOR
236  
237 brian m. carlson, C<< <sandals at crustytoothpaste.net> >>
238  
239 =head1 BUGS
240  
241 Please report any bugs or feature requests to C<bug-crypt-drbg at rt.cpan.org>, or through
242 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Crypt-DRBG>. I will be notified, and then you'll
243 automatically be notified of progress on your bug as I make changes.
244  
245  
246  
247  
248 =head1 SUPPORT
249  
250 You can find documentation for this module with the perldoc command.
251  
252 perldoc Crypt::DRBG::Hash
253  
254  
255 You can also look for information at:
256  
257 =over 4
258  
259 =item * RT: CPAN's request tracker (report bugs here)
260  
261 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Crypt-DRBG>
262  
263 =item * AnnoCPAN: Annotated CPAN documentation
264  
265 L<http://annocpan.org/dist/Crypt-DRBG>
266  
267 =item * CPAN Ratings
268  
269 L<http://cpanratings.perl.org/d/Crypt-DRBG>
270  
271 =item * Search CPAN
272  
273 L<http://search.cpan.org/dist/Crypt-DRBG/>
274  
275 =back
276  
277  
278 =head1 ACKNOWLEDGEMENTS
279  
280  
281 =head1 LICENSE AND COPYRIGHT
282  
283 Copyright 2015 brian m. carlson.
284  
285 This program is distributed under the MIT (X11) License:
286 L<http://www.opensource.org/licenses/mit-license.php>
287  
288 Permission is hereby granted, free of charge, to any person
289 obtaining a copy of this software and associated documentation
290 files (the "Software"), to deal in the Software without
291 restriction, including without limitation the rights to use,
292 copy, modify, merge, publish, distribute, sublicense, and/or sell
293 copies of the Software, and to permit persons to whom the
294 Software is furnished to do so, subject to the following
295 conditions:
296  
297 The above copyright notice and this permission notice shall be
298 included in all copies or substantial portions of the Software.
299  
300 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
301 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
302 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
303 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
304 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
305 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
306 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
307 OTHER DEALINGS IN THE SOFTWARE.
308  
309  
310 =cut
311  
312 13 1; # End of Crypt::DRBG::Hash
« blib/lib/Crypt/DRBG.pm ↑ Summary blib/lib/Crypt/DRBG/HMAC.pm »