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