line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::KDF::BaseKDFGenerator;
|
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
25
|
use strict;
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
387
|
|
4
|
2
|
|
|
2
|
|
12
|
use vars qw($VERSION @ISA @EXPORT_OK);
|
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
117
|
|
5
|
2
|
|
|
2
|
|
1414
|
use Crypt::KDF::_base;
|
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
1592
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
($VERSION) = sprintf '%i.%03i', split(/\./,('$Revision: 0.1 $' =~ /Revision: (\S+)\s/)[0]); # $Date: $
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
require Exporter;
|
10
|
|
|
|
|
|
|
@EXPORT_OK = qw(baseKdf_generate);
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
@ISA=qw{ Crypt::KDF::_base };
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Crypt::KDF::BaseKDFGenerator - Basic KDF generator for derived keys and ivs as defined by IEEE P1363a/ISO 18033.
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
This implementation is based on ISO 18033/P1363a.
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 FUNCTIONS
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head2 $derivedKey = baseKdf_generate( $digest, $seed, $counter, $len )
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Quick functional interface to use KDF.
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=cut
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub baseKdf_generate
|
33
|
|
|
|
|
|
|
{
|
34
|
0
|
|
|
0
|
1
|
0
|
my ($digest, $seed, $counter, $len) = @_;
|
35
|
0
|
|
|
|
|
0
|
my $kdf = Crypt::KDF::BaseKDFGenerator->new(-digest => $digest, -seed => $seed, -counter => $counter);
|
36
|
0
|
|
|
|
|
0
|
return $kdf->kdf($len);
|
37
|
|
|
|
|
|
|
}
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 METHODS
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head2 $kdf = Crypt::KDF::BaseKDFGenerator->new( [options] )
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Construct a Basic KDF generator.
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
-counter start value of counter used to derive keys.
|
46
|
|
|
|
|
|
|
-digest the digest to be used as the source of derived keys.
|
47
|
|
|
|
|
|
|
-digestparam optional parameters for the digest used to derive keys.
|
48
|
|
|
|
|
|
|
-seed the seed to be used to derive keys.
|
49
|
|
|
|
|
|
|
-iv optional iv to be used to derive keys.
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub new
|
54
|
|
|
|
|
|
|
{
|
55
|
0
|
|
|
0
|
1
|
0
|
my $class = shift @_;
|
56
|
0
|
|
|
|
|
0
|
my $self = {};
|
57
|
0
|
0
|
|
|
|
0
|
bless($self, (ref($class) ? ref($class) : $class));
|
58
|
0
|
|
|
|
|
0
|
my %opts = @_;
|
59
|
0
|
0
|
|
|
|
0
|
if(exists $opts{-digest})
|
60
|
|
|
|
|
|
|
{
|
61
|
0
|
0
|
|
|
|
0
|
$self->{-digest} = (ref($opts{-digest}) ? ref($opts{-digest}) : $opts{-digest});
|
62
|
|
|
|
|
|
|
}
|
63
|
0
|
0
|
|
|
|
0
|
if(exists $opts{-digestparam})
|
64
|
|
|
|
|
|
|
{
|
65
|
0
|
|
|
|
|
0
|
$self->{-digestparam} = $opts{-digestparam};
|
66
|
|
|
|
|
|
|
}
|
67
|
0
|
0
|
|
|
|
0
|
if(exists $opts{-counter})
|
68
|
|
|
|
|
|
|
{
|
69
|
0
|
|
|
|
|
0
|
$self->{-counter} = $opts{-counter};
|
70
|
|
|
|
|
|
|
}
|
71
|
0
|
0
|
|
|
|
0
|
if(exists $opts{-seed})
|
72
|
|
|
|
|
|
|
{
|
73
|
0
|
|
|
|
|
0
|
$self->{-seed} = $opts{-seed};
|
74
|
|
|
|
|
|
|
}
|
75
|
0
|
0
|
|
|
|
0
|
if(exists $opts{-iv})
|
76
|
|
|
|
|
|
|
{
|
77
|
0
|
|
|
|
|
0
|
$self->{-iv} = $opts{-iv};
|
78
|
|
|
|
|
|
|
}
|
79
|
0
|
|
|
|
|
0
|
return $self;
|
80
|
|
|
|
|
|
|
}
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 $kdf->init( [options] )
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Initialize the Basic KDF generator.
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
-counter start value of counter used to derive keys.
|
87
|
|
|
|
|
|
|
-digest the digest to be used as the source of derived keys.
|
88
|
|
|
|
|
|
|
-digestparam optional parameters for the digest used to derive keys.
|
89
|
|
|
|
|
|
|
-seed the seed to be used to derive keys.
|
90
|
|
|
|
|
|
|
-iv optional iv to be used to derive keys.
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=cut
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub init
|
95
|
|
|
|
|
|
|
{
|
96
|
0
|
|
|
0
|
1
|
0
|
my $self = shift @_;
|
97
|
0
|
|
|
|
|
0
|
my %opts = @_;
|
98
|
0
|
0
|
|
|
|
0
|
if(exists $opts{-digest})
|
99
|
|
|
|
|
|
|
{
|
100
|
0
|
0
|
|
|
|
0
|
$self->{-digest} = (ref($opts{-digest}) ? ref($opts{-digest}) : $opts{-digest});
|
101
|
|
|
|
|
|
|
}
|
102
|
0
|
0
|
|
|
|
0
|
if(exists $opts{-digestparam})
|
103
|
|
|
|
|
|
|
{
|
104
|
0
|
|
|
|
|
0
|
$self->{-digestparam} = $opts{-digestparam};
|
105
|
|
|
|
|
|
|
}
|
106
|
0
|
0
|
|
|
|
0
|
if(exists $opts{-counter})
|
107
|
|
|
|
|
|
|
{
|
108
|
0
|
|
|
|
|
0
|
$self->{-counter} = $opts{-counter};
|
109
|
|
|
|
|
|
|
}
|
110
|
0
|
0
|
|
|
|
0
|
if(exists $opts{-seed})
|
111
|
|
|
|
|
|
|
{
|
112
|
0
|
|
|
|
|
0
|
$self->{-seed} = $opts{-seed};
|
113
|
|
|
|
|
|
|
}
|
114
|
0
|
0
|
|
|
|
0
|
if(exists $opts{-iv})
|
115
|
|
|
|
|
|
|
{
|
116
|
0
|
|
|
|
|
0
|
$self->{-iv} = $opts{-iv};
|
117
|
|
|
|
|
|
|
}
|
118
|
0
|
|
|
|
|
0
|
return $self;
|
119
|
|
|
|
|
|
|
}
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head2 $derivedKey = $kdf->kdf( $length )
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Return length bytes generated from the derivation function.
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=cut
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub kdf
|
128
|
|
|
|
|
|
|
{
|
129
|
8
|
|
|
8
|
1
|
11
|
my $self = shift @_;
|
130
|
8
|
|
|
|
|
11
|
my $len = 16;
|
131
|
8
|
50
|
|
|
|
18
|
if($_[0])
|
132
|
|
|
|
|
|
|
{
|
133
|
8
|
|
|
|
|
12
|
$len = $_[0];
|
134
|
|
|
|
|
|
|
}
|
135
|
8
|
|
|
|
|
12
|
my $out='';
|
136
|
8
|
|
|
|
|
79
|
my $ct=$self->{-counter};
|
137
|
8
|
|
|
|
|
42
|
while(length($out)<$len)
|
138
|
|
|
|
|
|
|
{
|
139
|
12
|
|
|
|
|
15
|
my $d;
|
140
|
12
|
50
|
|
|
|
31
|
if(exists $self->{-digestparam})
|
141
|
|
|
|
|
|
|
{
|
142
|
0
|
|
|
|
|
0
|
$d = $self->{-digest}->new(@{ $self->{-digestparam} });
|
|
0
|
|
|
|
|
0
|
|
143
|
|
|
|
|
|
|
}
|
144
|
|
|
|
|
|
|
else
|
145
|
|
|
|
|
|
|
{
|
146
|
12
|
|
|
|
|
76
|
$d = $self->{-digest}->new();
|
147
|
|
|
|
|
|
|
}
|
148
|
12
|
|
|
|
|
66
|
$d->add($self->{-seed});
|
149
|
12
|
|
|
|
|
56
|
$d->add(pack('N',$ct));
|
150
|
12
|
100
|
|
|
|
36
|
if(exists $self->{-iv})
|
151
|
|
|
|
|
|
|
{
|
152
|
6
|
|
|
|
|
38
|
$d->add($self->{-iv});
|
153
|
|
|
|
|
|
|
}
|
154
|
12
|
|
|
|
|
58
|
$out.=$d->digest();
|
155
|
12
|
|
|
|
|
190
|
$ct++;
|
156
|
|
|
|
|
|
|
}
|
157
|
8
|
|
|
|
|
27
|
return substr($out,0,$len);
|
158
|
|
|
|
|
|
|
}
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
1;
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
__END__
|