KDBX/Cipher/Stream.pm

filter: Statementstmt 91.4% 75 / 82 Branchbran 73.0% 19 / 26 Conditioncond 42.1% 8 / 19 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 81.8% 18 / 22 Podpod 100% 12 / 12 totaltotal 78.4% 135 / 172 SCAR 32.5
CC
22
Cov
80%
key_size
12.0
iv_size
12.0
init
9.7
CRAP 25.7
« KDBX/Loader/V3.pm ↑ Summary KDBX/KDF/AES.pm »
1 package File::KDBX::Cipher::Stream;
2 # ABSTRACT: A cipher stream encrypter/decrypter
3  
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use Crypt::Digest qw(digest_data);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use File::KDBX::Constants qw(:cipher :random_stream);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use File::KDBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use File::KDBX::Util qw(:class);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 extends 'File::KDBX::Cipher';
16  
17 226 ▶our $VERSION = '0.904'; # VERSION
Subroutine counter: called CC=3 SCAR=11.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) 0 226 226
18 226 ▶ 
Subroutine offset: called CC=3 SCAR=11.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) 0 226 226
19 226 ▶ 
Condition66%2/3
$_[0]{"counter"} //= $default
l!l&&r!l&&!r
1200106
MC/DC50%1/2
$_[0]{"counter"} //= $default
$_[0]{"counter"} $default
Truth table66%2/3
A //= B
A= $_[0]{"counter"}B= $default
ABresult
1X1
011
000
20 226 ▶has 'counter', is => 'ro', default => 0;
Condition100%2/2
$_[0]{"offset"} //= undef
l!l
91135
MC/DC100%1/1
$_[0]{"offset"} //= undef
$_[0]{"offset"}
Truth table100%2/2
A
A= $_[0]{"offset"}
Aresult
11
00
21 9 has 'offset', is => 'ro';
22 0 ▶sub key_size { { Salsa20 => 32, ChaCha => 32 }->{$_[0]->{algorithm} || ''} // 0 }
Subroutine key_size: not called CC=3 SCAR=24.8
Pod documented
Condition0%0/4
{"Salsa20", 32, "ChaCha", 32}->{$_[0]{"algorithm"} or ""} // 0
l!l
00
$_[0]{'algorithm'} || ''
l!l
00
MC/DC0%0/1
{"Salsa20", 32, "ChaCha", 32}->{$_[0]{"algorithm"} or ""} // 0
{"Salsa20", 32, "ChaCha", 32}->{$_[0]{"algorithm"} or ""}
MC/DC0%0/1
$_[0]{'algorithm'} || ''
$_[0]{'algorithm'}
Truth table0%0/2
A
A= {"Salsa20", 32, "ChaCha", 32}->{$_[0]{"algorithm"} or ""}
Aresult
11
00
Truth table0%0/2
A
A= $_[0]{'algorithm'}
Aresult
11
00
23 0 ▶sub iv_size { { Salsa20 => 8, ChaCha => 12 }->{$_[0]->{algorithm} || ''} // -1 }
Subroutine iv_size: not called CC=3 SCAR=24.8
Pod documented
Condition0%0/4
{"Salsa20", 8, "ChaCha", 12}->{$_[0]{"algorithm"} or ""} // -1
l!l
00
$_[0]{'algorithm'} || ''
l!l
00
MC/DC0%0/1
{"Salsa20", 8, "ChaCha", 12}->{$_[0]{"algorithm"} or ""} // -1
{"Salsa20", 8, "ChaCha", 12}->{$_[0]{"algorithm"} or ""}
MC/DC0%0/1
$_[0]{'algorithm'} || ''
$_[0]{'algorithm'}
Truth table0%0/2
A
A= {"Salsa20", 8, "ChaCha", 12}->{$_[0]{"algorithm"} or ""}
Aresult
11
00
Truth table0%0/2
A
A= $_[0]{'algorithm'}
Aresult
11
00
24 0 ▶sub block_size { 1 }
Subroutine block_size: not called CC=1 SCAR=6.9
Pod documented
25  
26 sub init {
27 132 ▶ my $self = shift;
Subroutine init: called CC=9 SCAR=22.7
Pod documented
28 132 my %args = @_;
29  
30 132 ▶ if (my $uuid = $args{uuid}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $uuid = $args{'uuid'}) { } 31 101 132
elsif (my $id = $args{'stream_id'}) { } 101 0 101
31 31 ▶ if ($uuid eq CIPHER_UUID_CHACHA20 && length($args{iv}) == 16) {
Branch: 2 branches
BranchTrueFalseTotal
if ($uuid eq "\326\cC\212+\213oL\265\245\$3\2321\333\265\232" and length $args{'iv'} == 16) { } 30 1 31
elsif ($uuid eq "qn\034\212\356\cWK\334\223\256\251w\270\202\203:") { } 0 1 1
Condition66%2/3
$uuid eq "\326\3\212+\213oL\265\245\$3\2321\333\265\232" and length $args{"iv"} == 16
!ll&&!rl&&r
0130
MC/DC50%1/2
$uuid eq "\326\3\212+\213oL\265\245\$3\2321\333\265\232" and length $args{"iv"} == 16
$uuid eq "\326\3\212+\213oL\265\245\$3\2321\333\265\232" length $args{"iv"} == 16
Truth table66%2/3
A and B
A= $uuid eq "\326\3\212+\213oL\265\245\$3\2321\333\265\232"B= length $args{"iv"} == 16
ABresult
0X0
100
111
32 # extract the counter
33 30 my $buf = substr($self->{iv}, 0, 4, '');
34 30 $self->{counter} = unpack('L<', $buf);
35 }
36 elsif ($uuid eq CIPHER_UUID_SALSA20) {
37 # only need eight bytes...
38 0 $self->{iv} = substr($args{iv}, 8);
39 }
40 }
41 elsif (my $id = $args{stream_id}) {
42 101 ▶ my $key_ref = ref $args{key} ? $args{key} : \$args{key};
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'key'} ? : 0 101 101
43 101 ▶ if ($id == STREAM_ID_CHACHA20) {
Branch: 2 branches
BranchTrueFalseTotal
if ($id == 3) { } 83 18 101
elsif ($id == 2) { } 18 0 18
44 83 ($self->{key}, $self->{iv}) = unpack('a32 a12', digest_data('SHA512', $$key_ref));
45 }
46 elsif ($id == STREAM_ID_SALSA20) {
47 18 ($self->{key}, $self->{iv}) = (digest_data('SHA256', $$key_ref), STREAM_SALSA20_IV);
48 }
49 }
50  
51 132 return $self;
52 }
53  
54  
55 sub crypt {
56 309 ▶ my $self = shift;
Subroutine crypt: called CC=2 SCAR=6.9
Pod documented
57 309 my $stream = $self->_stream;
58 309 ▶ return join('', map { $stream->crypt(ref $_ ? $$_ : $_) } grep { defined } @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 219 90 309
59 }
60  
61  
62 sub keystream {
63 0 ▶ my $self = shift;
Subroutine keystream: not called CC=1 SCAR=6.9
Pod documented
64 0 return $self->_stream->keystream(@_);
65 }
66  
67  
68 sub dup {
69 91 ▶ my $self = shift;
Subroutine dup: called CC=1 SCAR=0.0
Pod documented
70 91 my $class = blessed($self);
71  
72 91 my $dup = bless {%$self, @_}, $class;
73 91 delete $dup->{stream};
74 91 return $dup;
75 }
76  
77 sub _stream {
78 309 ▶ my $self = shift;
Subroutine _stream: called CC=5 SCAR=16.2
79  
80 309 ▶ $self->{stream} //= do {
Condition66%2/3
$self->{"stream"} //= do { my $s = eval { do { my $pkg = 'Crypt::Stream::' . $self->algorithm; my $counter = $self->counter; my $pos = 0; if (defined(my $offset = $self->offset)) { $counter = int $offset / 64; $pos = $offset % 64; } ; my $s = $pkg->new($self->key, $self->iv, $counter); $s->keystream($pos) if $pos; $s } }; if (my $err = $@) { throw("Failed to initialize stream cipher library", "error", $err, "algorithm", $self->{"algorithm"}, "key_length", length $self->key, "iv_length", length $self->iv, "iv", unpack("H*", $self->iv), "key", unpack("H*", $self->key)); } ; $s }
l!l&&r!l&&!r
832260
MC/DC0%0/2
$self->{"stream"} //= do { my $s = eval { do { my $pkg = 'Crypt::Stream::' . $self->algorithm; my $counter = $self->counter; my $pos = 0; if (defined(my $offset = $self->offset)) { $counter = int $offset / 64; $pos = $offset % 64; } ; my $s = $pkg->new($self->key, $self->iv, $counter); $s->keystream($pos) if $pos; $s } }; if (my $err = $@) { throw("Failed to initialize stream cipher library", "error", $err, "algorithm", $self->{"algorithm"}, "key_length", length $self->key, "iv_length", length $self->iv, "iv", unpack("H*", $self->iv), "key", unpack("H*", $self->key)); } ; $s }
$self->{"stream"} do { my $s = eval { do { my $pkg = 'Crypt::Stream::' . $self->algorithm; my $counter = $self->counter; my $pos = 0; if (defined(my $offset = $self->offset)) { $counter = int $offset / 64; $pos = $offset % 64; } ; my $s = $pkg->new($self->key, $self->iv, $counter); $s->keystream($pos) if $pos; $s } }; if (my $err = $@) { throw("Failed to initialize stream cipher library", "error", $err, "algorithm", $self->{"algorithm"}, "key_length", length $self->key, "iv_length", length $self->iv, "iv", unpack("H*", $self->iv), "key", unpack("H*", $self->key)); } ; $s }
Truth table66%2/3
A //= B
A= $self->{"stream"}B= do { my $s = eval { do { my $pkg = 'Crypt::Stream::' . $self->algorithm; my $counter = $self->counter; my $pos = 0; if (defined(my $offset = $self->offset)) { $counter = int $offset / 64; $pos = $offset % 64; } ; my $s = $pkg->new($self->key, $self->iv, $counter); $s->keystream($pos) if $pos; $s } }; if (my $err = $@) { throw("Failed to initialize stream cipher library", "error", $err, "algorithm", $self->{"algorithm"}, "key_length", length $self->key, "iv_length", length $self->iv, "iv", unpack("H*", $self->iv), "key", unpack("H*", $self->key)); } ; $s }
ABresult
1X1
011
000
81 226 my $s = eval {
82 226 my $pkg = 'Crypt::Stream::'.$self->algorithm;
83 226 my $counter = $self->counter;
84 226 my $pos = 0;
85 226 ▶ if (defined (my $offset = $self->offset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $offset = $self->offset)) 91 135 226
86 91 $counter = int($offset / 64);
87 91 $pos = $offset % 64;
88 }
89 226 my $s = $pkg->new($self->key, $self->iv, $counter);
90 # seek to correct position within block
91 226 ▶ $s->keystream($pos) if $pos;
Branch: 1 branch
BranchTrueFalseTotal
if $pos 2 224 226
92 226 $s;
93 };
94 226 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 226 226
95 throw 'Failed to initialize stream cipher library',
96 error => $err,
97 algorithm => $self->{algorithm},
98 0 key_length => length($self->key),
99 iv_length => length($self->iv),
100 iv => unpack('H*', $self->iv),
101 key => unpack('H*', $self->key);
102 }
103 226 $s;
104 };
105 }
106  
107 72 ▶sub encrypt { goto &crypt }
Subroutine encrypt: called CC=1 SCAR=0.0
Pod documented
108 18 ▶sub decrypt { goto &crypt }
Subroutine decrypt: called CC=1 SCAR=0.0
Pod documented
109  
110 218 ▶sub finish { delete $_[0]->{stream}; '' }
Subroutine finish: called CC=1 SCAR=0.0
Pod documented
111  
112 9 1;
113  
114 __END__
« KDBX/Loader/V3.pm ↑ Summary KDBX/KDF/AES.pm »