line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::KDF::_base;
|
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
15
|
use strict;
|
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
248
|
|
4
|
3
|
|
|
3
|
|
15
|
use vars qw($VERSION);
|
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
2229
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
### ($VERSION) = sprintf '%i.%03i', split(/\./,('$Revision: 0.1 $' =~ /Revision: (\S+)\s/)[0]); # $Date: $
|
7
|
|
|
|
|
|
|
$VERSION = '0.02';
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Crypt::KDF::_base
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=cut
|
14
|
|
|
|
|
|
|
|
15
|
0
|
|
|
0
|
0
|
0
|
sub new { die "must subclass"; }
|
16
|
|
|
|
|
|
|
|
17
|
0
|
|
|
0
|
0
|
0
|
sub init { die "must subclass"; }
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub kdf_hex
|
20
|
|
|
|
|
|
|
{
|
21
|
8
|
|
|
8
|
0
|
273
|
my $self = shift @_;
|
22
|
8
|
|
|
|
|
10
|
my $ret;
|
23
|
8
|
50
|
|
|
|
164
|
if($_[0])
|
24
|
|
|
|
|
|
|
{
|
25
|
8
|
|
|
|
|
40
|
$ret=$self->kdf($_[0]);
|
26
|
|
|
|
|
|
|
}
|
27
|
|
|
|
|
|
|
else
|
28
|
|
|
|
|
|
|
{
|
29
|
0
|
|
|
|
|
0
|
$ret=$self->kdf();
|
30
|
|
|
|
|
|
|
}
|
31
|
8
|
|
|
|
|
35
|
return unpack('H*',$ret);
|
32
|
|
|
|
|
|
|
}
|
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
0
|
0
|
|
sub kdf { die "must subclass"; }
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 METHODS
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head2 $digest = $kdf->digest( $digest )
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Sets/gets the digest to be used as the source of derived keys.
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=cut
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub digest
|
45
|
|
|
|
|
|
|
{
|
46
|
0
|
|
|
0
|
1
|
|
my $self = shift @_;
|
47
|
0
|
0
|
|
|
|
|
if($_[0])
|
48
|
|
|
|
|
|
|
{
|
49
|
0
|
0
|
|
|
|
|
$self->{-digest} = (ref($_[0]) ? ref($_[0]) : $_[0]);
|
50
|
|
|
|
|
|
|
}
|
51
|
0
|
|
|
|
|
|
return $self->{-digest};
|
52
|
|
|
|
|
|
|
}
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head2 $digestparam = $kdf->digestparam( $digestparam )
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Sets/gets the optional parameters for the digest used to derive keys.
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=cut
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub digestparam
|
61
|
|
|
|
|
|
|
{
|
62
|
0
|
|
|
0
|
1
|
|
my $self = shift @_;
|
63
|
0
|
0
|
|
|
|
|
if($_[0])
|
64
|
|
|
|
|
|
|
{
|
65
|
0
|
|
|
|
|
|
$self->{-digestparam} = $_[0];
|
66
|
|
|
|
|
|
|
}
|
67
|
0
|
|
|
|
|
|
return $self->{-digestparam};
|
68
|
|
|
|
|
|
|
}
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head2 $seed = $kdf->seed( $seed )
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Sets/gets the seed to be used to derive keys.
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub seed
|
77
|
|
|
|
|
|
|
{
|
78
|
0
|
|
|
0
|
1
|
|
my $self = shift @_;
|
79
|
0
|
0
|
|
|
|
|
if($_[0])
|
80
|
|
|
|
|
|
|
{
|
81
|
0
|
|
|
|
|
|
$self->{-seed} = $_[0];
|
82
|
|
|
|
|
|
|
}
|
83
|
0
|
|
|
|
|
|
return $self->{-seed};
|
84
|
|
|
|
|
|
|
}
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 $counter = $kdf->counter( $counter )
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Sets/gets the start value of counter used to derive keys.
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=cut
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub counter
|
93
|
|
|
|
|
|
|
{
|
94
|
0
|
|
|
0
|
1
|
|
my $self = shift @_;
|
95
|
0
|
0
|
|
|
|
|
if($_[0])
|
96
|
|
|
|
|
|
|
{
|
97
|
0
|
|
|
|
|
|
$self->{-counter} = $_[0];
|
98
|
|
|
|
|
|
|
}
|
99
|
0
|
|
|
|
|
|
return $self->{-counter};
|
100
|
|
|
|
|
|
|
}
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head2 $iv = $kdf->iv( $iv )
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Sets/gets the optional iv to be used to derive keys.
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=cut
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub iv
|
109
|
|
|
|
|
|
|
{
|
110
|
0
|
|
|
0
|
1
|
|
my $self = shift @_;
|
111
|
0
|
0
|
|
|
|
|
if($_[0])
|
112
|
|
|
|
|
|
|
{
|
113
|
0
|
|
|
|
|
|
$self->{-iv} = $_[0];
|
114
|
|
|
|
|
|
|
}
|
115
|
0
|
|
|
|
|
|
return $self->{-iv};
|
116
|
|
|
|
|
|
|
}
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 ( $derivedKey, $derivedIV ) = $kdf->kdf_iv( $kLen, $ivLen )
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Return length bytes generated from the derivation function.
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=cut
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub kdf_iv
|
125
|
|
|
|
|
|
|
{
|
126
|
0
|
|
|
0
|
1
|
|
my $self = shift @_;
|
127
|
0
|
|
|
|
|
|
my $kLen = 16;
|
128
|
0
|
0
|
|
|
|
|
if($_[0])
|
129
|
|
|
|
|
|
|
{
|
130
|
0
|
|
|
|
|
|
$kLen = $_[0];
|
131
|
|
|
|
|
|
|
}
|
132
|
0
|
|
|
|
|
|
my $ivLen = 16;
|
133
|
0
|
0
|
|
|
|
|
if($_[1])
|
134
|
|
|
|
|
|
|
{
|
135
|
0
|
|
|
|
|
|
$ivLen = $_[1];
|
136
|
|
|
|
|
|
|
}
|
137
|
0
|
|
|
|
|
|
my $len=$kLen+$ivLen;
|
138
|
0
|
|
|
|
|
|
my $out=$self->kdf($len);
|
139
|
0
|
|
|
|
|
|
return (substr($out,0,$kLen), substr($out,$kLen,$ivLen));
|
140
|
|
|
|
|
|
|
}
|
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
1;
|
143
|
|
|
|
|
|
|
__END__
|