| 1 | package Crypt::DRBG::HMAC; | ||||||||||||||||||||||||||||
| 2 | 13 | $Crypt::DRBG::HMAC::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::HMAC - Fast, cryptographically secure PRNG | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | use Crypt::DRBG::HMAC; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | my $drbg = Crypt::DRBG::HMAC->new(auto => 1); | ||||||||||||||||||||||||||||
| 20 | my $data = $drbg->generate(42); | ||||||||||||||||||||||||||||
| 21 | ... # do something with your 42 bytes here | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | my $drbg2 = Crypt::DRBG::HMAC->new(seed => "my very secret seed"); | ||||||||||||||||||||||||||||
| 24 | my $data2 = $drbg->generate(42); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Crypt::DRBG::HMAC is an implementation of the HMAC_DRBG from NIST SP800-90A. It | ||||||||||||||||||||||||||||
| 29 | is a fast, cryptographically secure PRNG. By default, it uses HMAC-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::HMAC->new(%params) | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Creates a new Crypt::DRBG::HMAC. | ||||||||||||||||||||||||||||
| 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 | HMAC-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 HMAC for your specific algorithm. The function should take two | ||||||||||||||||||||||||||||
| 66 | arguments, the value and the key, in that order. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | For example, if you had C<Digest::BLAKE2> and C<Digest::HMAC> installed, you | ||||||||||||||||||||||||||||
| 69 | could do the following to use BLAKE2b: | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | my $func = sub { | ||||||||||||||||||||||||||||
| 72 | return Digest::HMAC::hmac(@_, \&Digest::BLAKEx::blake2b, 128); | ||||||||||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||||||||||
| 74 | my $drbg = Crypt::DRBG::HMAC->new(auto => 1, func => $func; | ||||||||||||||||||||||||||||
| 75 | my $data = $drbg->generate(42); | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | Note that the algo parameter is still required, explicitly or implicitly, in | ||||||||||||||||||||||||||||
| 78 | order to know how large a seed to use. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =back | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1391 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 1391 | my ($class, %params) = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1391 | ▶ | $class = ref($class) || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 88 | 1391 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1391 | ▶ | my $algo = $self->{algo} = $params{algo} || '512'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$params{'algo'} || '512'
MC/DC100%1/1
$params{'algo'} || '512'
$params{'algo'}
Truth table100%2/2
A
A= $params{'algo'}
| |||||||||||||||||||||||||||||
| 91 | 1391 | $algo =~ tr{/}{}d; | |||||||||||||||||||||||||||
| 92 | 1391 | ▶ | $self->{s_func} = ($params{func} || Digest::SHA->can("hmac_sha$algo")) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$params{'func'} || 'Digest::SHA'->can("hmac_sha$algo")
MC/DC0%0/2
$params{'func'} || 'Digest::SHA'->can("hmac_sha$algo")
$params{'func'}
'Digest::SHA'->can("hmac_sha$algo")
Truth table66%2/3
A || B
A= $params{'func'}B= 'Digest::SHA'->can("hmac_sha$algo")
| |||||||||||||||||||||||||||||
| 93 | die "Unsupported algorithm '$algo'"; | ||||||||||||||||||||||||||||
| 94 | 1391 | ▶ | $self->{seedlen} = $algo =~ /^(384|512)$/ ? 111 : 55; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 1391 | $self->{reseed_interval} = 4294967295; # (2^32)-1 | |||||||||||||||||||||||||||
| 96 | 1391 | $self->{bytes_per_request} = 2 ** 16; | |||||||||||||||||||||||||||
| 97 | 1391 | $self->{outlen} = substr($algo, -3) / 8; | |||||||||||||||||||||||||||
| 98 | 1391 | $self->{security_strength} = $self->{outlen} / 2; | |||||||||||||||||||||||||||
| 99 | 1391 | $self->{min_length} = $self->{security_strength}; | |||||||||||||||||||||||||||
| 100 | 1391 | $self->{max_length} = 4294967295; # (2^32)-1 | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1391 | $self->initialize(%params); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 1390 | return $self; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1390 | ▶ | sub _seed { | ||||||||||||||||||||||||||
|
Subroutine
_seed: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | 1390 | my ($self, $seed) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1390 | my $k = "\x00" x $self->{outlen}; | |||||||||||||||||||||||||||
| 111 | 1390 | my $v = "\x01" x $self->{outlen}; | |||||||||||||||||||||||||||
| 112 | 1390 | $self->{state} = {k => $k, v => $v}; | |||||||||||||||||||||||||||
| 113 | 1390 | return $self->_reseed($seed); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 1398 | ▶ | sub _reseed { | ||||||||||||||||||||||||||
|
Subroutine
_reseed: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 117 | 1398 | my ($self, $seed) = @_; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1398 | $self->_update($seed); | |||||||||||||||||||||||||||
| 120 | 1398 | $self->{reseed_counter} = 1; | |||||||||||||||||||||||||||
| 121 | 1398 | return 1; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 5525 | ▶ | sub _update { | ||||||||||||||||||||||||||
|
Subroutine
_update: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 125 | 5525 | my ($self, $seed) = @_; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 5525 | ▶ | my $data = defined $seed ? $seed : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 5525 | my $func = $self->{s_func}; | |||||||||||||||||||||||||||
| 130 | 5525 | my $state = $self->{state}; | |||||||||||||||||||||||||||
| 131 | 5525 | my ($k, $v) = @{$state}{qw/k v/}; | |||||||||||||||||||||||||||
| 132 | 5525 | $k = $func->("$v\x00$data", $k); | |||||||||||||||||||||||||||
| 133 | 5525 | $v = $func->($v, $k); | |||||||||||||||||||||||||||
| 134 | 5525 | ▶ | if (defined $seed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 4086 | $k = $func->("$v\x01$data", $k); | |||||||||||||||||||||||||||
| 136 | 4086 | $v = $func->($v, $k); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 5525 | @{$state}{qw/k v/} = ($k, $v); | |||||||||||||||||||||||||||
| 139 | 5525 | return 1; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 $drbg->generate($bytes, $additional_data) | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Generate and return $bytes bytes. $bytes cannot exceed 2^16. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | If $additional_data is specified, add this additional data to the DRBG. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 2783 | ▶ | sub _generate { | ||||||||||||||||||||||||||
|
Subroutine
_generate: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 151 | 2783 | my ($self, $len, $seed) = @_; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 2783 | $self->_check_reseed($len); | |||||||||||||||||||||||||||
| 154 | 2783 | ▶ | $self->_update($seed) if defined $seed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 2783 | my $count = ($len + ($self->{outlen} - 1)) / $self->{outlen}; | |||||||||||||||||||||||||||
| 157 | 2783 | my $data = ''; | |||||||||||||||||||||||||||
| 158 | 2783 | my $state = $self->{state}; | |||||||||||||||||||||||||||
| 159 | 2783 | my ($k, $v) = @{$state}{qw/k v/}; | |||||||||||||||||||||||||||
| 160 | 2783 | my $func = $self->{s_func}; | |||||||||||||||||||||||||||
| 161 | 2783 | for (1..$count) { | |||||||||||||||||||||||||||
| 162 | 11034 | $v = $func->($v, $k); | |||||||||||||||||||||||||||
| 163 | 11034 | $data .= $v; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 2783 | $self->{reseed_counter}++; | |||||||||||||||||||||||||||
| 166 | 2783 | @{$state}{qw/k v/} = ($k, $v); | |||||||||||||||||||||||||||
| 167 | 2783 | $self->_update($seed); | |||||||||||||||||||||||||||
| 168 | 2783 | return substr($data, 0, $len); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | brian m. carlson, C<< <sandals at crustytoothpaste.net> >> | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 BUGS | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Please report any bugs or feature requests to C<bug-crypt-drbg at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 178 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Crypt-DRBG>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 179 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | perldoc Crypt::DRBG::HMAC | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | You can also look for information at: | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =over 4 | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Crypt-DRBG> | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | L<http://annocpan.org/dist/Crypt-DRBG> | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | L<http://cpanratings.perl.org/d/Crypt-DRBG> | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | L<http://search.cpan.org/dist/Crypt-DRBG/> | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =back | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | Copyright 2015 brian m. carlson. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | This program is distributed under the MIT (X11) License: | ||||||||||||||||||||||||||||
| 222 | L<http://www.opensource.org/licenses/mit-license.php> | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | Permission is hereby granted, free of charge, to any person | ||||||||||||||||||||||||||||
| 225 | obtaining a copy of this software and associated documentation | ||||||||||||||||||||||||||||
| 226 | files (the "Software"), to deal in the Software without | ||||||||||||||||||||||||||||
| 227 | restriction, including without limitation the rights to use, | ||||||||||||||||||||||||||||
| 228 | copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||||||||||||||||||||||||
| 229 | copies of the Software, and to permit persons to whom the | ||||||||||||||||||||||||||||
| 230 | Software is furnished to do so, subject to the following | ||||||||||||||||||||||||||||
| 231 | conditions: | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | The above copyright notice and this permission notice shall be | ||||||||||||||||||||||||||||
| 234 | included in all copies or substantial portions of the Software. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||||||||||||||||||||||||||
| 237 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||||||||||||||||||||||||||||
| 238 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||||||||||||||||||||||||||
| 239 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||||||||||||||||||||||||||||
| 240 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||||||||||||||||||||||||||||
| 241 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||||||||||||||||||||||||||||
| 242 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||||||||||||||||||||||||||||
| 243 | OTHER DEALINGS IN THE SOFTWARE. | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =cut | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 13 | 1; # End of Crypt::DRBG::HMAC | |||||||||||||||||||||||||||