BaseKDFGenerator.pm

filter: Statementstmt 48.4% 31 / 64 Branchbran 17.6% 6 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 4 / 7 Podpod 100% 4 / 4 totaltotal 41.2% 45 / 109 CC 18 SCAR 37.3
CC
18
Cov
38%
new
72.0
init
56.0
kdf
5.1
CRAP 41.6
« OpenSSLPBEGenerator.pm ↑ Summary KDF1Generator.pm »
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
BranchTrueFalseTotal
ref $class ? : 0 0 0
58 0 my %opts = @_;
59 0 ▶ if(exists $opts{-digest})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-digest"}) 0 0 0
60 {
61 0 ▶ $self->{-digest} = (ref($opts{-digest}) ? ref($opts{-digest}) : $opts{-digest});
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'-digest'} ? : 0 0 0
62 }
63 0 ▶ if(exists $opts{-digestparam})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-digestparam"}) 0 0 0
64 {
65 0 $self->{-digestparam} = $opts{-digestparam};
66 }
67 0 ▶ if(exists $opts{-counter})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-counter"}) 0 0 0
68 {
69 0 $self->{-counter} = $opts{-counter};
70 }
71 0 ▶ if(exists $opts{-seed})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-seed"}) 0 0 0
72 {
73 0 $self->{-seed} = $opts{-seed};
74 }
75 0 ▶ if(exists $opts{-iv})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-iv"}) 0 0 0
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
BranchTrueFalseTotal
if (exists $opts{"-digest"}) 0 0 0
99 {
100 0 ▶ $self->{-digest} = (ref($opts{-digest}) ? ref($opts{-digest}) : $opts{-digest});
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'-digest'} ? : 0 0 0
101 }
102 0 ▶ if(exists $opts{-digestparam})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-digestparam"}) 0 0 0
103 {
104 0 $self->{-digestparam} = $opts{-digestparam};
105 }
106 0 ▶ if(exists $opts{-counter})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-counter"}) 0 0 0
107 {
108 0 $self->{-counter} = $opts{-counter};
109 }
110 0 ▶ if(exists $opts{-seed})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-seed"}) 0 0 0
111 {
112 0 $self->{-seed} = $opts{-seed};
113 }
114 0 ▶ if(exists $opts{-iv})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"-iv"}) 0 0 0
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
BranchTrueFalseTotal
if ($_[0]) 8 0 8
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
BranchTrueFalseTotal
if (length $out < $len) 12 8 20
138 {
139 12 my $d;
140 12 ▶ if(exists $self->{-digestparam})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'-digestparam'}) { } 0 12 12
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
BranchTrueFalseTotal
if (exists $self->{"-iv"}) 6 6 12
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__
« OpenSSLPBEGenerator.pm ↑ Summary KDF1Generator.pm »