| 1 | package File::KDBX::KDF; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: A key derivation function | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 14 | ▶ | use Crypt::PRNG qw(random_bytes); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 14 | ▶ | use File::KDBX::Constants qw(:version :kdf); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 14 | ▶ | use File::KDBX::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 14 | ▶ | use File::KDBX::Util qw(format_uuid); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 14 | ▶ | use Module::Load; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 14 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 14 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 14 | our $VERSION = '0.905'; # VERSION | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 14 | my %KDFS; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub new { | ||||||||||||||||||||||||||||
| 21 | 177 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 177 | my %args = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 177 | ▶ | my $uuid = $args{+KDF_PARAM_UUID} //= delete $args{uuid} or throw 'Missing KDF UUID', args => \%args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args{"\$UUID"} //= delete $args{"uuid"}
MC/DC0%0/2
$args{"\$UUID"} //= delete $args{"uuid"}
$args{"\$UUID"}
delete $args{"uuid"}
Truth table66%2/3
A //= B
A= $args{"\$UUID"}B= delete $args{"uuid"}
| |||||||||||||||||||||||||||||
| 25 | 177 | my $formatted_uuid = format_uuid($uuid); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 177 | ▶ | my $kdf = $KDFS{$uuid} or throw "Unsupported KDF ($formatted_uuid)", uuid => $uuid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 177 | ($class, my %registration_args) = @$kdf; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 177 | load $class; | |||||||||||||||||||||||||||
| 31 | 177 | my $self = bless {KDF_PARAM_UUID() => $uuid}, $class; | |||||||||||||||||||||||||||
| 32 | 177 | return $self->init(%args, %registration_args); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub init { | ||||||||||||||||||||||||||||
| 37 | 177 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 177 | my %args = @_; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 177 | @$self{keys %args} = values %args; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 177 | return $self; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 182 | ▶ | sub uuid { $_[0]->{+KDF_PARAM_UUID} } | ||||||||||||||||||||||||||
|
Subroutine
uuid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub seed { die 'Not implemented' } | ||||||||||||||||||||||||||
|
Subroutine
seed: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub transform { | ||||||||||||||||||||||||||||
| 53 | 51 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
transform: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 51 | my $key = shift; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 51 | ▶ | if (blessed $key && $key->can('raw_key')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($key) and $key->can("raw_key")
MC/DC50%1/2
builtin::blessed($key) and $key->can("raw_key")
builtin::blessed($key)
$key->can("raw_key")
Truth table66%2/3
A and B
A= builtin::blessed($key)B= $key->can("raw_key")
| |||||||||||||||||||||||||||||
| 57 | 45 | ▶ | return $self->_transform($key->raw_key) if $self->uuid eq KDF_UUID_AES; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 21 | return $self->_transform($key->raw_key($self->seed, @_)); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 6 | return $self->_transform($key); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | sub _transform { die 'Not implemented' } | ||||||||||||||||||||||||||
|
Subroutine
_transform: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub randomize_seed { | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
randomize_seed: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 0 | $self->{+KDF_PARAM_AES_SEED} = random_bytes(length($self->seed)); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub register { | ||||||||||||||||||||||||||||
| 74 | 56 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=7 SCAR=23.8
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 56 | my $id = shift; | |||||||||||||||||||||||||||
| 76 | 56 | my $package = shift; | |||||||||||||||||||||||||||
| 77 | 56 | my @args = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 56 | my $formatted_id = format_uuid($id); | |||||||||||||||||||||||||||
| 80 | 56 | ▶ | $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/
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 56 | ▶ | my %blacklist = map { File::KDBX::Util::uuid($_) => 1 } split(/,/, $ENV{FILE_KDBX_KDF_BLACKLIST} // ''); | ||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{"FILE_KDBX_KDF_BLACKLIST"} // ""
MC/DC0%0/1
$ENV{"FILE_KDBX_KDF_BLACKLIST"} // ""
$ENV{"FILE_KDBX_KDF_BLACKLIST"}
Truth table50%1/2
A
A= $ENV{"FILE_KDBX_KDF_BLACKLIST"}
| |||||||||||||||||||||||||||||
| 83 | 56 | ▶ | 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}
| |||||||||||||||||||||||||||||
| 84 | 0 | alert "Ignoring blacklisted KDF ($formatted_id)", id => $id, package => $package; | |||||||||||||||||||||||||||
| 85 | 0 | return; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 56 | ▶ | if (defined $KDFS{$id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | alert "Overriding already-registered KDF ($formatted_id) with package $package", | |||||||||||||||||||||||||||
| 90 | id => $id, | ||||||||||||||||||||||||||||
| 91 | package => $package; | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 56 | $KDFS{$id} = [$package, @args]; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | sub unregister { | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | delete $KDFS{$_} for @_; | ||||||||||||||||||||||||||
|
Subroutine
unregister: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | BEGIN { | ||||||||||||||||||||||||||||
| 103 | 14 | ▶ | __PACKAGE__->register(KDF_UUID_AES, 'AES'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 104 | 14 | __PACKAGE__->register(KDF_UUID_AES_CHALLENGE_RESPONSE, 'AES'); | |||||||||||||||||||||||||||
| 105 | 14 | __PACKAGE__->register(KDF_UUID_ARGON2D, 'Argon2'); | |||||||||||||||||||||||||||
| 106 | 14 | __PACKAGE__->register(KDF_UUID_ARGON2ID, 'Argon2'); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 14 | 1; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||