| 1 | package Crypt::KDF::OpenSSLPBEGenerator; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Digest::MD5; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT_OK); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Crypt::KDF::_base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | ($VERSION) = sprintf '%i.%03i', split(/\./,('$Revision: 0.1 $' =~ /Revision: (\S+)\s/)[0]); # $Date: $ | |||||||||
| 9 | |||||||||||
| 10 | 1 | require Exporter; | |||||||||
| 11 | 1 | @EXPORT_OK = qw(opensslpbekdf_generate); | |||||||||
| 12 | |||||||||||
| 13 | 1 | @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 | 0 | ▶ | { | ||||||||
|
Subroutine
opensslpbekdf_generate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 35 | 0 | 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 | 0 | ▶ | { | ||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||
| 55 | 0 | my $class = shift @_; | |||||||||
| 56 | 0 | my $self = {}; | |||||||||
| 57 | 0 | ▶ | bless($self, (ref($class) ? ref($class) : $class)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | my %opts = @_; | |||||||||
| 59 | 0 | ▶ | if(exists $opts{-digest}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | { | ||||||||||
| 61 | 0 | ▶ | $self->{-digest} = (ref($opts{-digest}) ? ref($opts{-digest}) : $opts{-digest}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | } | ||||||||||
| 63 | 0 | ▶ | if(exists $opts{-digestparam}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | { | ||||||||||
| 65 | 0 | $self->{-digestparam} = $opts{-digestparam}; | |||||||||
| 66 | } | ||||||||||
| 67 | 0 | ▶ | if(exists $opts{-seed}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | { | ||||||||||
| 69 | 0 | $self->{-seed} = $opts{-seed}; | |||||||||
| 70 | } | ||||||||||
| 71 | 0 | ▶ | if(exists $opts{-iv}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | { | ||||||||
|
Subroutine
init: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 91 | 0 | my $self = shift @_; | |||||||||
| 92 | 0 | my %opts = @_; | |||||||||
| 93 | 0 | ▶ | if(exists $opts{-digest}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | { | ||||||||||
| 95 | 0 | ▶ | $self->{-digest} = (ref($opts{-digest}) ? ref($opts{-digest}) : $opts{-digest}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | } | ||||||||||
| 97 | 0 | ▶ | if(exists $opts{-digestparam}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | { | ||||||||||
| 99 | 0 | $self->{-digestparam} = $opts{-digestparam}; | |||||||||
| 100 | } | ||||||||||
| 101 | 0 | ▶ | if(exists $opts{-seed}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | { | ||||||||||
| 103 | 0 | $self->{-seed} = $opts{-seed}; | |||||||||
| 104 | } | ||||||||||
| 105 | 0 | ▶ | if(exists $opts{-iv}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | { | ||||||||
|
Subroutine
kdf: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||
| 120 | 0 | my $self = shift @_; | |||||||||
| 121 | 0 | my $len = 16; | |||||||||
| 122 | 0 | ▶ | if($_[0]) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | { | ||||||||||
| 124 | 0 | $len = $_[0]; | |||||||||
| 125 | } | ||||||||||
| 126 | 0 | my $out=''; | |||||||||
| 127 | 0 | my $last=''; | |||||||||
| 128 | 0 | ▶ | while(length($out)<$len) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | { | ||||||||||
| 130 | 0 | my $d; | |||||||||
| 131 | 0 | ▶ | if(!exists $self->{-digest}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | { | ||||||||||
| 133 | 0 | $self->{-digest} = 'Digest::MD5'; | |||||||||
| 134 | } | ||||||||||
| 135 | 0 | ▶ | if(exists $self->{-digestparam}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | { | ||||||||||
| 137 | 0 | $d = $self->{-digest}->new(@{ $self->{-digestparam} }); | |||||||||
| 138 | } | ||||||||||
| 139 | else | ||||||||||
| 140 | { | ||||||||||
| 141 | 0 | $d = $self->{-digest}->new(); | |||||||||
| 142 | } | ||||||||||
| 143 | 0 | ▶ | if($last ne '') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | { | ||||||||||
| 145 | 0 | $d->add($last); | |||||||||
| 146 | } | ||||||||||
| 147 | 0 | $d->add($self->{-seed}); | |||||||||
| 148 | 0 | ▶ | if(exists $self->{-iv}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 1; | |||||||||
| 159 | |||||||||||
| 160 | __END__ | ||||||||||