| 1 | package File::KDBX::KDF::AES; | ||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Using the AES cipher as a key derivation function | ||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use Crypt::Cipher; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use Crypt::Digest qw(digest_data); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use File::KDBX::Constants qw(:bool :kdf); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use File::KDBX::Error; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use File::KDBX::Util qw(:class :load can_fork); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | 12 | extends 'File::KDBX::KDF'; | |||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | 12 | our $VERSION = '0.905'; # VERSION | |||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||
| 18 | # Rounds higher than this are eligible for forking: | ||||||||||||||||||||||||||||||||||||||
| 19 | 12 | my $FORK_OPTIMIZATION_THRESHOLD = 100_000; | |||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | BEGIN { | ||||||||||||||||||||||||||||||||||||||
| 22 | 12 | ▶ | my $use_fork = $ENV{NO_FORK} || !can_fork; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=13.1
Condition33%1/3
$ENV{'NO_FORK'} || !can_fork()
MC/DC0%0/2
$ENV{'NO_FORK'} || !can_fork()
$ENV{'NO_FORK'}
!can_fork()
Truth table33%1/3
A || not(B)
A= $ENV{'NO_FORK'}B= !can_fork()
| |||||||||||||||||||||||||||||||||||||||
| 23 | 12 | ▶ | *_USE_FORK = $use_fork ? \&TRUE : \&FALSE; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | 42 | ▶ | sub rounds { $_[0]->{+KDF_PARAM_AES_ROUNDS} || KDF_DEFAULT_AES_ROUNDS } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
rounds: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 28 | 56 | ▶ | sub seed { $_[0]->{+KDF_PARAM_AES_SEED} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
seed: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||
| 30 | sub init { | ||||||||||||||||||||||||||||||||||||||
| 31 | 158 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 32 | 158 | my %args = @_; | |||||||||||||||||||||||||||||||||||||
| 33 | return $self->SUPER::init( | ||||||||||||||||||||||||||||||||||||||
| 34 | KDF_PARAM_AES_ROUNDS() => $args{+KDF_PARAM_AES_ROUNDS} // $args{rounds}, | ||||||||||||||||||||||||||||||||||||||
| 35 | KDF_PARAM_AES_SEED() => $args{+KDF_PARAM_AES_SEED} // $args{seed}, | ||||||||||||||||||||||||||||||||||||||
| 36 | 158 | ▶ | ); | ||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$args{"R"} // $args{"rounds"}
$args{"S"} // $args{"seed"}
MC/DC0%0/2
$args{"R"} // $args{"rounds"}
$args{"R"}
$args{"rounds"}
MC/DC0%0/2
$args{"S"} // $args{"seed"}
$args{"S"}
$args{"seed"}
Truth table66%2/3
A // B
A= $args{"R"}B= $args{"rounds"}
Truth table66%2/3
A // B
A= $args{"S"}B= $args{"seed"}
| |||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||
| 39 | sub _transform { | ||||||||||||||||||||||||||||||||||||||
| 40 | 42 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_transform: called CC=8 SCAR=32.9
| |||||||||||||||||||||||||||||||||||||||
| 41 | 42 | my $key = shift; | |||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||
| 43 | 42 | my $seed = $self->seed; | |||||||||||||||||||||||||||||||||||||
| 44 | 42 | my $rounds = $self->rounds; | |||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||
| 46 | 42 | ▶ | length($key) == 32 or throw 'Raw key must be 32 bytes', size => length($key); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 47 | 40 | ▶ | length($seed) == 32 or throw 'Invalid seed length', size => length($seed); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||
| 49 | 40 | my ($key_l, $key_r) = unpack('(a16)2', $key); | |||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||
| 51 | 40 | goto NO_FORK if !_USE_FORK || $rounds < $FORK_OPTIMIZATION_THRESHOLD; | |||||||||||||||||||||||||||||||||||||
| 52 | { | ||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my $pid = open(my $read, '-|') // do { alert "fork failed: $!"; goto NO_FORK }; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
open(my $read, "-|") // do {
alert("fork failed: $!");
goto NO_FORK
}
MC/DC0%0/2
open(my $read, "-|") // do {
alert("fork failed: $!");
goto NO_FORK
}
open(my $read, "-|")
do {
alert("fork failed: $!");
goto NO_FORK
}
Truth table0%0/3
A // B
A= open(my $read, "-|")B= do {
alert("fork failed: $!");
goto NO_FORK
}
| |||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($pid == 0) { # child | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my $l = _transform_half($seed, $key_l, $rounds); | |||||||||||||||||||||||||||||||||||||
| 56 | 0 | require POSIX; | |||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | print $l or POSIX::_exit(1); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 58 | 0 | POSIX::_exit(0); | |||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $r = _transform_half($seed, $key_r, $rounds); | |||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | read($read, my $l, length($key_l)) == length($key_l) or do { alert "read failed: $!", goto NO_FORK }; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | close($read) or do { alert "worker thread exited abnormally", status => $?; goto NO_FORK }; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 63 | 0 | return digest_data('SHA256', $l, $r); | |||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | # FIXME: This used to work but now it crashes frequently. Threads are now discouraged anyway, but it might | ||||||||||||||||||||||||||||||||||||||
| 67 | # be nice if this was available for no-fork platforms. | ||||||||||||||||||||||||||||||||||||||
| 68 | # if ($ENV{THREADS} && eval 'use threads; 1') { | ||||||||||||||||||||||||||||||||||||||
| 69 | # my $l = threads->create(\&_transform_half, $key_l, $seed, $rounds); | ||||||||||||||||||||||||||||||||||||||
| 70 | # my $r = _transform_half($key_r, $seed, $rounds); | ||||||||||||||||||||||||||||||||||||||
| 71 | # return digest_data('SHA256', $l->join, $r); | ||||||||||||||||||||||||||||||||||||||
| 72 | # } | ||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||
| 74 | NO_FORK: | ||||||||||||||||||||||||||||||||||||||
| 75 | 40 | my $l = _transform_half($seed, $key_l, $rounds); | |||||||||||||||||||||||||||||||||||||
| 76 | 40 | my $r = _transform_half($seed, $key_r, $rounds); | |||||||||||||||||||||||||||||||||||||
| 77 | 40 | return digest_data('SHA256', $l, $r); | |||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||
| 80 | sub _transform_half_pp { | ||||||||||||||||||||||||||||||||||||||
| 81 | 2 | ▶ | my $seed = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_transform_half_pp: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 82 | 2 | my $key = shift; | |||||||||||||||||||||||||||||||||||||
| 83 | 2 | my $rounds = shift; | |||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||
| 85 | 2 | my $c = Crypt::Cipher->new('AES', $seed); | |||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||
| 87 | 2 | my $result = $key; | |||||||||||||||||||||||||||||||||||||
| 88 | 2 | ▶ | for (my $i = 0; $i < $rounds; ++$i) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 89 | 20 | $result = $c->encrypt($result); | |||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||
| 92 | 2 | return $result; | |||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||
| 95 | BEGIN { | ||||||||||||||||||||||||||||||||||||||
| 96 | 12 | ▶ | my $use_xs = load_xs; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 97 | 12 | ▶ | *_transform_half = $use_xs ? \&File::KDBX::XS::kdf_aes_transform_half : \&_transform_half_pp; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||
| 100 | 12 | 1; | |||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||||||||||||