| 1 | package Crypt::KDF::BaseKDFGenerator; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use vars qw($VERSION @ISA @EXPORT_OK); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Crypt::KDF::_base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | ($VERSION) = sprintf '%i.%03i', split(/\./,('$Revision: 0.1 $' =~ /Revision: (\S+)\s/)[0]); # $Date: $ | |||||||||
| 8 | |||||||||||
| 9 | 2 | require Exporter; | |||||||||
| 10 | 2 | @EXPORT_OK = qw(baseKdf_generate); | |||||||||
| 11 | |||||||||||
| 12 | 2 | @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 | 0 | ▶ | { | ||||||||
|
Subroutine
baseKdf_generate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 34 | 0 | my ($digest, $seed, $counter, $len) = @_; | |||||||||
| 35 | 0 | my $kdf = Crypt::KDF::BaseKDFGenerator->new(-digest => $digest, -seed => $seed, -counter => $counter); | |||||||||
| 36 | 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 | 0 | ▶ | { | ||||||||
|
Subroutine
new: not called CC=8 SCAR=42.8
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{-counter}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | { | ||||||||||
| 69 | 0 | $self->{-counter} = $opts{-counter}; | |||||||||
| 70 | } | ||||||||||
| 71 | 0 | ▶ | if(exists $opts{-seed}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | { | ||||||||||
| 73 | 0 | $self->{-seed} = $opts{-seed}; | |||||||||
| 74 | } | ||||||||||
| 75 | 0 | ▶ | if(exists $opts{-iv}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | { | ||||||||||
| 77 | 0 | $self->{-iv} = $opts{-iv}; | |||||||||
| 78 | } | ||||||||||
| 79 | 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 | 0 | ▶ | { | ||||||||
|
Subroutine
init: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||
| 96 | 0 | my $self = shift @_; | |||||||||
| 97 | 0 | my %opts = @_; | |||||||||
| 98 | 0 | ▶ | if(exists $opts{-digest}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | { | ||||||||||
| 100 | 0 | ▶ | $self->{-digest} = (ref($opts{-digest}) ? ref($opts{-digest}) : $opts{-digest}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | } | ||||||||||
| 102 | 0 | ▶ | if(exists $opts{-digestparam}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | { | ||||||||||
| 104 | 0 | $self->{-digestparam} = $opts{-digestparam}; | |||||||||
| 105 | } | ||||||||||
| 106 | 0 | ▶ | if(exists $opts{-counter}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | { | ||||||||||
| 108 | 0 | $self->{-counter} = $opts{-counter}; | |||||||||
| 109 | } | ||||||||||
| 110 | 0 | ▶ | if(exists $opts{-seed}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | { | ||||||||||
| 112 | 0 | $self->{-seed} = $opts{-seed}; | |||||||||
| 113 | } | ||||||||||
| 114 | 0 | ▶ | if(exists $opts{-iv}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | { | ||||||||||
| 116 | 0 | $self->{-iv} = $opts{-iv}; | |||||||||
| 117 | } | ||||||||||
| 118 | 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 | 8 | ▶ | { | ||||||||
|
Subroutine
kdf: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||
| 129 | 8 | my $self = shift @_; | |||||||||
| 130 | 8 | my $len = 16; | |||||||||
| 131 | 8 | ▶ | if($_[0]) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | { | ||||||||||
| 133 | 8 | $len = $_[0]; | |||||||||
| 134 | } | ||||||||||
| 135 | 8 | my $out=''; | |||||||||
| 136 | 8 | my $ct=$self->{-counter}; | |||||||||
| 137 | 8 | ▶ | while(length($out)<$len) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | { | ||||||||||
| 139 | 12 | my $d; | |||||||||
| 140 | 12 | ▶ | if(exists $self->{-digestparam}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | { | ||||||||||
| 142 | 0 | $d = $self->{-digest}->new(@{ $self->{-digestparam} }); | |||||||||
| 143 | } | ||||||||||
| 144 | else | ||||||||||
| 145 | { | ||||||||||
| 146 | 12 | $d = $self->{-digest}->new(); | |||||||||
| 147 | } | ||||||||||
| 148 | 12 | $d->add($self->{-seed}); | |||||||||
| 149 | 12 | $d->add(pack('N',$ct)); | |||||||||
| 150 | 12 | ▶ | if(exists $self->{-iv}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | { | ||||||||||
| 152 | 6 | $d->add($self->{-iv}); | |||||||||
| 153 | } | ||||||||||
| 154 | 12 | $out.=$d->digest(); | |||||||||
| 155 | 12 | $ct++; | |||||||||
| 156 | } | ||||||||||
| 157 | 8 | return substr($out,0,$len); | |||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | 2 | 1; | |||||||||
| 161 | |||||||||||
| 162 | __END__ | ||||||||||