| 1 | package File::KDBX::Cipher; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: A block cipher mode or cipher stream | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Devel::GlobalDestruction; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use File::KDBX::Constants qw(:cipher :random_stream); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use File::KDBX::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use File::KDBX::Util qw(:class erase format_uuid); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use Module::Load; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 11 | our $VERSION = '0.904'; # VERSION | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | my %CIPHERS; | ||||||||||||||||||||||||||
|
Subroutine
uuid: not called CC=3 SCAR=19.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | |||||||||||||||||||||||||||
|
Subroutine
stream_id: not called CC=3 SCAR=17.8
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | |||||||||||||||||||||||||||
|
Subroutine
key: called CC=3 SCAR=14.7
Pod
documented
Branch: 1 branch
Condition0%0/2
$_[0]{"uuid"} //= undef
MC/DC0%0/1
$_[0]{"uuid"} //= undef
$_[0]{"uuid"}
Truth table0%0/2
A
A= $_[0]{"uuid"}
| |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | has 'uuid', is => 'ro'; | ||||||||||||||||||||||||||
|
Subroutine
iv: called CC=3 SCAR=12.7
Pod
documented
Branch: 1 branch
Condition0%0/2
$_[0]{"stream_id"} //= undef
MC/DC0%0/1
$_[0]{"stream_id"} //= undef
$_[0]{"stream_id"}
Truth table0%0/2
A
A= $_[0]{"stream_id"}
| |||||||||||||||||||||||||||||
| 21 | 238 | ▶ | has 'stream_id', is => 'ro'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_[0]{"key"} //= undef
MC/DC0%0/1
$_[0]{"key"} //= undef
$_[0]{"key"}
Truth table50%1/2
A
A= $_[0]{"key"}
| |||||||||||||||||||||||||||||
| 22 | 238 | ▶ | has 'key', is => 'ro'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_[0]{"iv"} //= undef
MC/DC0%0/1
$_[0]{"iv"} //= undef
$_[0]{"iv"}
Truth table50%1/2
A
A= $_[0]{"iv"}
| |||||||||||||||||||||||||||||
| 23 | 11 | has 'iv', is => 'ro'; | |||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub iv_size { 0 } | ||||||||||||||||||||||||||
|
Subroutine
iv_size: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub key_size { -1 } | ||||||||||||||||||||||||||
|
Subroutine
key_size: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub block_size { 0 } | ||||||||||||||||||||||||||
|
Subroutine
block_size: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 238 | ▶ | sub algorithm { $_[0]->{algorithm} or throw 'Block cipher algorithm is not set' } | ||||||||||||||||||||||||||
|
Subroutine
algorithm: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub new { | ||||||||||||||||||||||||||||
| 31 | 142 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 142 | my %args = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 142 | ▶ | return $class->new_from_uuid(delete $args{uuid}, %args) if defined $args{uuid}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 101 | ▶ | return $class->new_from_stream_id(delete $args{stream_id}, %args) if defined $args{stream_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | throw 'Must pass uuid or stream_id'; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub new_from_uuid { | ||||||||||||||||||||||||||||
| 41 | 41 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new_from_uuid: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 41 | my $uuid = shift; | |||||||||||||||||||||||||||
| 43 | 41 | my %args = @_; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 41 | ▶ | $args{key} or throw 'Missing encryption key'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 41 | ▶ | $args{iv} or throw 'Missing encryption IV'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 41 | my $formatted_uuid = format_uuid($uuid); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 41 | ▶ | my $cipher = $CIPHERS{$uuid} or throw "Unsupported cipher ($formatted_uuid)", uuid => $uuid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 41 | ($class, my %registration_args) = @$cipher; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 41 | my @args = (%args, %registration_args, uuid => $uuid); | |||||||||||||||||||||||||||
| 54 | 41 | load $class; | |||||||||||||||||||||||||||
| 55 | 41 | my $self = bless {@args}, $class; | |||||||||||||||||||||||||||
| 56 | 41 | return $self->init(@args); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub new_from_stream_id { | ||||||||||||||||||||||||||||
| 60 | 101 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new_from_stream_id: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 101 | my $id = shift; | |||||||||||||||||||||||||||
| 62 | 101 | my %args = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 101 | ▶ | $args{key} or throw 'Missing encryption key'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 101 | ▶ | my $cipher = $CIPHERS{$id} or throw "Unsupported stream cipher ($id)", id => $id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 101 | ($class, my %registration_args) = @$cipher; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 101 | my @args = (%args, %registration_args, stream_id => $id); | |||||||||||||||||||||||||||
| 70 | 101 | load $class; | |||||||||||||||||||||||||||
| 71 | 101 | my $self = bless {@args}, $class; | |||||||||||||||||||||||||||
| 72 | 101 | return $self->init(@args); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 10 | ▶ | sub init { $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 233 | ▶ | sub DESTROY { !in_global_destruction and erase \$_[0]->{key} } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub encrypt { die 'Not implemented' } | ||||||||||||||||||||||||||
|
Subroutine
encrypt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub decrypt { die 'Not implemented' } | ||||||||||||||||||||||||||
|
Subroutine
decrypt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub finish { '' } | ||||||||||||||||||||||||||
|
Subroutine
finish: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub encrypt_finish { | ||||||||||||||||||||||||||||
| 91 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
encrypt_finish: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 2 | my $out = $self->encrypt(@_); | |||||||||||||||||||||||||||
| 93 | 2 | $out .= $self->finish; | |||||||||||||||||||||||||||
| 94 | 2 | return $out; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | sub decrypt_finish { | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
decrypt_finish: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 0 | my $out = $self->decrypt(@_); | |||||||||||||||||||||||||||
| 101 | 0 | $out .= $self->finish; | |||||||||||||||||||||||||||
| 102 | 0 | return $out; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | sub register { | ||||||||||||||||||||||||||||
| 107 | 88 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=9 SCAR=27.6
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 88 | my $id = shift; | |||||||||||||||||||||||||||
| 109 | 88 | my $package = shift; | |||||||||||||||||||||||||||
| 110 | 88 | my @args = @_; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 88 | ▶ | my $formatted_id = looks_like_number($id) ? $id : format_uuid($id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 88 | ▶ | $package = "${class}::${package}" if $package !~ s/^\+// && $package !~ /^\Q${class}::\E/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$package =~ s/^\+// or $package =~ /^\Q$class::\E/
MC/DC0%0/2
$package =~ s/^\+// or $package =~ /^\Q$class::\E/
$package =~ s/^\+//
$package =~ /^\Q$class::\E/
Truth table33%1/3
A or B
A= $package =~ s/^\+//B= $package =~ /^\Q$class::\E/
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my %blacklist = map { (looks_like_number($_) ? $_ : File::KDBX::Util::uuid($_)) => 1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$ENV{"FILE_KDBX_CIPHER_BLACKLIST"} // ""
MC/DC0%0/1
$ENV{"FILE_KDBX_CIPHER_BLACKLIST"} // ""
$ENV{"FILE_KDBX_CIPHER_BLACKLIST"}
Truth table50%1/2
A
A= $ENV{"FILE_KDBX_CIPHER_BLACKLIST"}
| |||||||||||||||||||||||||||||
| 116 | 88 | split(/,/, $ENV{FILE_KDBX_CIPHER_BLACKLIST} // ''); | |||||||||||||||||||||||||||
| 117 | 88 | ▶ | if ($blacklist{$id} || $blacklist{$package}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$blacklist{$id} or $blacklist{$package}
MC/DC0%0/2
$blacklist{$id} or $blacklist{$package}
$blacklist{$id}
$blacklist{$package}
Truth table33%1/3
A or B
A= $blacklist{$id}B= $blacklist{$package}
| |||||||||||||||||||||||||||||
| 118 | 0 | alert "Ignoring blacklisted cipher ($formatted_id)", id => $id, package => $package; | |||||||||||||||||||||||||||
| 119 | 0 | return; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 88 | ▶ | if (defined $CIPHERS{$id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | alert "Overriding already-registered cipher ($formatted_id) with package $package", | |||||||||||||||||||||||||||
| 124 | id => $id, | ||||||||||||||||||||||||||||
| 125 | package => $package; | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 88 | $CIPHERS{$id} = [$package, @args]; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub unregister { | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | delete $CIPHERS{$_} for @_; | ||||||||||||||||||||||||||
|
Subroutine
unregister: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | BEGIN { | ||||||||||||||||||||||||||||
| 137 | 11 | ▶ | __PACKAGE__->register(CIPHER_UUID_AES128, 'CBC', algorithm => 'AES', key_size => 16); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 138 | 11 | __PACKAGE__->register(CIPHER_UUID_AES256, 'CBC', algorithm => 'AES', key_size => 32); | |||||||||||||||||||||||||||
| 139 | 11 | __PACKAGE__->register(CIPHER_UUID_SERPENT, 'CBC', algorithm => 'Serpent', key_size => 32); | |||||||||||||||||||||||||||
| 140 | 11 | __PACKAGE__->register(CIPHER_UUID_TWOFISH, 'CBC', algorithm => 'Twofish', key_size => 32); | |||||||||||||||||||||||||||
| 141 | 11 | __PACKAGE__->register(CIPHER_UUID_CHACHA20, 'Stream', algorithm => 'ChaCha'); | |||||||||||||||||||||||||||
| 142 | 11 | __PACKAGE__->register(CIPHER_UUID_SALSA20, 'Stream', algorithm => 'Salsa20'); | |||||||||||||||||||||||||||
| 143 | 11 | __PACKAGE__->register(STREAM_ID_CHACHA20, 'Stream', algorithm => 'ChaCha'); | |||||||||||||||||||||||||||
| 144 | 11 | __PACKAGE__->register(STREAM_ID_SALSA20, 'Stream', algorithm => 'Salsa20'); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 11 | 1; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||