KDBX/IO/HmacBlock.pm

filter: Statementstmt 97.6% 126 / 129 Branchbran 69.3% 43 / 62 Conditioncond 73.3% 11 / 15 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 22 / 22 Podpod 100% 1 / 1 totaltotal 87.0% 208 / 239 CC 36 SCAR 14.3
CC
36
Cov
87%
_write_next_hmac_block
7.0
_read_hashed_block
6.1
_FLUSH
5.5
CRAP 4.2
« KDBX/KDF.pm ↑ Summary KDBX/KDF/Argon2.pm »
1 package File::KDBX::IO::HmacBlock;
2 # ABSTRACT: HMAC block stream IO handle
3  
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Crypt::Digest qw(digest_data);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Crypt::Mac::HMAC qw(hmac);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use File::KDBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use File::KDBX::Util qw(:class :int :io);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 extends 'File::KDBX::IO';
15  
16 6 our $VERSION = '0.905'; # VERSION
17 6 our $BLOCK_SIZE = 1048576; # 1MiB
18 6 our $ERROR;
19  
20  
21 my %ATTRS = (
22 _block_index => int64(0),
23 22 ▶ _buffer => sub { \(my $buf = '') },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 _finished => 0,
25 20 ▶ block_size => sub { $BLOCK_SIZE },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 6 key => undef,
27 );
28 6 ▶while (my ($attr, $default) = each %ATTRS) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %ATTRS) 30 6 36
29 6 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
30 *$attr = sub {
31 567 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
32 567 ▶ *$self->{$attr} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 55 512 567
33 567 ▶ *$self->{$attr} //= (ref $default eq 'CODE') ? $default->($self) : $default;
Branch: 1 branch
BranchTrueFalseTotal
ref $default eq 'CODE' ? : 42 43 85
Condition100%3/3
*$self->{$attr} //= ref $default eq "CODE" ? &$default($self) : $default
l!l&&r!l&&!r
4824243
MC/DC100%2/2
*$self->{$attr} //= ref $default eq "CODE" ? &$default($self) : $default
*$self->{$attr} ref $default eq "CODE" ? &$default($self) : $default
Truth table100%3/3
A //= B
A= *$self->{$attr}B= ref $default eq "CODE" ? &$default($self) : $default
ABresult
1X1
011
000
34 30 };
35 }
36  
37  
38 sub new {
39 22 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=14.2
Pod documented
40 22 ▶ my %args = @_ % 2 == 1 ? (fh => shift, @_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 1 ? : 22 0 22
41 22 my $self = $class->SUPER::new;
42 22 ▶ $self->_fh($args{fh}) or throw 'IO handle required';
Branch: 1 branch
BranchTrueFalseTotal
$self->_fh($args{"fh"}) or throw("IO handle required") 0 22 22
43 22 ▶ $self->key($args{key}) or throw 'Key required';
Branch: 1 branch
BranchTrueFalseTotal
$self->key($args{"key"}) or throw("Key required") 0 22 22
44 22 $self->block_size($args{block_size});
45 22 $self->_buffer;
46 22 return $self;
47 }
48  
49 sub _FILL {
50 54 ▶ my ($self, $fh) = @_;
Subroutine _FILL: called CC=5 SCAR=16.1
51  
52 54 ▶ $ENV{DEBUG_STREAM} and print STDERR "FILL\t$self\n";
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DEBUG_STREAM"} and print STDERR "FILL\t$self\n" 0 54 54
53 54 ▶ return if $self->_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_finished 8 46 54
54  
55 46 my $block = eval { $self->_read_hashed_block($fh) };
56 46 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 1 45 46
57 1 $self->_set_error($err);
58 1 return;
59 }
60 45 ▶ if (length($block) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $block == 0) 11 34 45
61 11 $self->_finished(1);
62 11 return;
63 }
64 34 return $block;
65 }
66  
67 sub _WRITE {
68 17 ▶ my ($self, $buf, $fh) = @_;
Subroutine _WRITE: called CC=3 SCAR=11.2
69  
70 17 ▶ $ENV{DEBUG_STREAM} and print STDERR "WRITE\t$self ($fh)\n";
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DEBUG_STREAM"} and print STDERR "WRITE\t$self ($fh)\n" 0 17 17
71 17 ▶ return 0 if $self->_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_finished 0 17 17
72  
73 17 ${*$self->{_buffer}} .= $buf;
74  
75 17 $self->_FLUSH($fh); # TODO only if autoflush?
76  
77 17 return length($buf);
78 }
79  
80 sub _POPPED {
81 22 ▶ my ($self, $fh) = @_;
Subroutine _POPPED: called CC=4 SCAR=14.0
82  
83 22 ▶ $ENV{DEBUG_STREAM} and print STDERR "POPPED\t$self ($fh)\n";
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DEBUG_STREAM"} and print STDERR "POPPED\t$self ($fh)\n" 0 22 22
84 22 ▶ return if $self->_mode ne 'w';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_mode ne "w" 12 10 22
85  
86 10 $self->_FLUSH($fh);
87 10 eval {
88 10 $self->_write_next_hmac_block($fh); # partial block with remaining content
89 10 $self->_write_final_hmac_block($fh); # terminating block
90 };
91 10 ▶ $self->_set_error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 10 10
92 }
93  
94 sub _FLUSH {
95 27 ▶ my ($self, $fh) = @_;
Subroutine _FLUSH: called CC=5 SCAR=17.0
96  
97 27 ▶ $ENV{DEBUG_STREAM} and print STDERR "FLUSH\t$self ($fh)\n";
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DEBUG_STREAM"} and print STDERR "FLUSH\t$self ($fh)\n" 0 27 27
98 27 ▶ return if $self->_mode ne 'w';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_mode ne "w" 0 27 27
99  
100 27 eval {
101 27 ▶ while ($self->block_size <= length(${*$self->{_buffer}})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->block_size <= length ${*$self->{"_buffer"};}) 25 27 52
102 25 $self->_write_next_hmac_block($fh);
103 }
104 };
105 27 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 27 27
106 0 $self->_set_error($err);
107 0 return -1;
108 }
109  
110 27 return 0;
111 }
112  
113 sub _set_error {
114 1 ▶ my $self = shift;
Subroutine _set_error: called CC=4 SCAR=16.4
115 1 ▶ $ENV{DEBUG_STREAM} and print STDERR "err\t$self\n";
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DEBUG_STREAM"} and print STDERR "err\t$self\n" 0 1 1
116 1 ▶ if (exists &Errno::EPROTO) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists &Errno::EPROTO) { } 1 0 1
elsif (exists &Errno::EIO) { } 0 0 0
117 1 $! = &Errno::EPROTO;
118 }
119 elsif (exists &Errno::EIO) {
120 0 $! = &Errno::EIO;
121 }
122 1 $self->_error($ERROR = error(@_));
123 }
124  
125 ##############################################################################
126  
127 sub _read_hashed_block {
128 46 ▶ my $self = shift;
Subroutine _read_hashed_block: called CC=6 SCAR=18.0
129 46 my $fh = shift;
130  
131 46 ▶ read_all $fh, my $hmac, 32 or throw 'Failed to read HMAC';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, my $hmac, 32) or throw("Failed to read HMAC") 1 45 46
132  
133 45 ▶ read_all $fh, my $packed_size, 4 or throw 'Failed to read HMAC block size';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, my $packed_size, 4) or throw("Failed to read HMAC block size") 0 45 45
134 45 my ($size) = unpack('L<', $packed_size);
135  
136 45 my $block = '';
137 45 ▶ if (0 < $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $size) 34 11 45
138 34 ▶ read_all $fh, $block, $size
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, $block, $size) or throw("Failed to read HMAC block", "index", $self->_block_index, "size", $size) 0 34 34
139 or throw 'Failed to read HMAC block', index => $self->_block_index, size => $size;
140 }
141  
142 45 my $packed_index = pack_Ql($self->_block_index);
143 45 my $got_hmac = hmac('SHA256', $self->_hmac_key,
144 $packed_index,
145 $packed_size,
146 $block,
147 );
148  
149 45 ▶ $hmac eq $got_hmac
Branch: 1 branch
BranchTrueFalseTotal
$hmac eq $got_hmac or throw("Block authentication failed", "index", $self->_block_index, "got", $got_hmac, "expected", $hmac) 0 45 45
150 or throw 'Block authentication failed', index => $self->_block_index, got => $got_hmac, expected => $hmac;
151  
152 45 *$self->{_block_index}++;
153 45 return $block;
154 }
155  
156 sub _write_next_hmac_block {
157 45 ▶ my $self = shift;
Subroutine _write_next_hmac_block: called CC=7 SCAR=19.5
158 45 my $fh = shift;
159 45 ▶ my $buffer = shift // $self->_buffer;
Condition66%2/3
shift() // $self->_buffer
l!l&&r!l&&!r
10350
MC/DC0%0/2
shift() // $self->_buffer
shift() $self->_buffer
Truth table66%2/3
A // B
A= shift()B= $self->_buffer
ABresult
1X1
011
000
160 45 my $allow_empty = shift;
161  
162 45 my $size = length($$buffer);
163 45 ▶ $size = $self->block_size if $self->block_size < $size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->block_size < $size 23 22 45
164 45 ▶ return 0 if $size == 0 && !$allow_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 and not $allow_empty 2 43 45
Condition100%3/3
$size == 0 and not $allow_empty
!ll&&!rl&&r
33102
MC/DC100%2/2
$size == 0 and not $allow_empty
$size == 0 not $allow_empty
Truth table100%3/3
A and not(B)
A= $size == 0B= not $allow_empty
ABresult
0X0
100
111
165  
166 43 my $block = '';
167 43 ▶ $block = substr($$buffer, 0, $size, '') if 0 < $size;
Branch: 1 branch
BranchTrueFalseTotal
if 0 < $size 33 10 43
168  
169 43 my $packed_index = pack_Ql($self->_block_index);
170 43 my $packed_size = pack('L<', $size);
171 43 my $hmac = hmac('SHA256', $self->_hmac_key,
172 $packed_index,
173 $packed_size,
174 $block,
175 );
176  
177 43 ▶ $fh->print($hmac, $packed_size, $block)
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($hmac, $packed_size, $block) or throw("Failed to write HMAC block", "hmac", $hmac, "block_size", $size) 0 43 43
178 or throw 'Failed to write HMAC block', hmac => $hmac, block_size => $size;
179  
180 43 *$self->{_block_index}++;
181 43 return 0;
182 }
183  
184 sub _write_final_hmac_block {
185 10 ▶ my $self = shift;
Subroutine _write_final_hmac_block: called CC=1 SCAR=0.0
186 10 my $fh = shift;
187  
188 10 $self->_write_next_hmac_block($fh, \'', 1);
189 }
190  
191 sub _hmac_key {
192 88 ▶ my $self = shift;
Subroutine _hmac_key: called CC=3 SCAR=11.4
193 88 ▶ my $key = shift // $self->key;
Condition33%1/3
shift() // $self->key
l!l&&r!l&&!r
0880
MC/DC0%0/2
shift() // $self->key
shift() $self->key
Truth table33%1/3
A // B
A= shift()B= $self->key
ABresult
1X1
011
000
194 88 ▶ my $index = shift // $self->_block_index;
Condition66%2/3
shift() // $self->_block_index
l!l&&r!l&&!r
06721
MC/DC50%1/2
shift() // $self->_block_index
shift() $self->_block_index
Truth table66%2/3
A // B
A= shift()B= $self->_block_index
ABresult
1X1
011
000
195  
196 88 my $packed_index = pack_Ql($index);
197 88 my $hmac_key = digest_data('SHA512', $packed_index, $key);
198 88 return $hmac_key;
199 }
200  
201 6 1;
202  
203 __END__
« KDBX/KDF.pm ↑ Summary KDBX/KDF/Argon2.pm »