File Coverage

blib/lib/File/KDBX/KDF/Argon2.pm
Criterion Covered Total %
statement 35 39 89.7
branch 4 6 66.6
condition 18 29 62.0
subroutine 14 17 82.3
pod 9 9 100.0
total 80 100 80.0


line stmt bran cond sub pod time code
1             package File::KDBX::KDF::Argon2;
2             # ABSTRACT: The Argon2 family of key derivation functions
3              
4 2     2   894 use warnings;
  2         4  
  2         59  
5 2     2   11 use strict;
  2         2  
  2         43  
6              
7 2     2   1084 use Crypt::Argon2 qw(argon2d_raw argon2id_raw);
  2         3185  
  2         101  
8 2     2   11 use File::KDBX::Constants qw(:kdf);
  2         5  
  2         261  
9 2     2   11 use File::KDBX::Error;
  2         4  
  2         73  
10 2     2   9 use File::KDBX::Util qw(:class);
  2         3  
  2         172  
11 2     2   12 use namespace::clean;
  2         3  
  2         11  
12              
13             extends 'File::KDBX::KDF';
14              
15             our $VERSION = '0.906'; # VERSION
16              
17              
18 16 50   16 1 58 sub salt { $_[0]->{+KDF_PARAM_ARGON2_SALT} or throw 'Salt is not set' }
19 7     7 1 16 sub seed { $_[0]->salt }
20 9   50 9 1 27 sub parallelism { $_[0]->{+KDF_PARAM_ARGON2_PARALLELISM} //= KDF_DEFAULT_ARGON2_PARALLELISM }
21 9   100 9 1 27 sub memory { $_[0]->{+KDF_PARAM_ARGON2_MEMORY} //= KDF_DEFAULT_ARGON2_MEMORY }
22 9   50 9 1 24 sub iterations { $_[0]->{+KDF_PARAM_ARGON2_ITERATIONS} //= KDF_DEFAULT_ARGON2_ITERATIONS }
23 0   0 0 1 0 sub version { $_[0]->{+KDF_PARAM_ARGON2_VERSION} //= KDF_DEFAULT_ARGON2_VERSION }
24 0     0 1 0 sub secret { $_[0]->{+KDF_PARAM_ARGON2_SECRET} }
25 0     0 1 0 sub assocdata { $_[0]->{+KDF_PARAM_ARGON2_ASSOCDATA} }
26              
27             sub init {
28 19     19 1 27 my $self = shift;
29 19         60 my %args = @_;
30             return $self->SUPER::init(
31             KDF_PARAM_ARGON2_SALT() => $args{+KDF_PARAM_ARGON2_SALT} // $args{salt},
32             KDF_PARAM_ARGON2_PARALLELISM() => $args{+KDF_PARAM_ARGON2_PARALLELISM} // $args{parallelism},
33             KDF_PARAM_ARGON2_MEMORY() => $args{+KDF_PARAM_ARGON2_MEMORY} // $args{memory},
34             KDF_PARAM_ARGON2_ITERATIONS() => $args{+KDF_PARAM_ARGON2_ITERATIONS} // $args{iterations},
35             KDF_PARAM_ARGON2_VERSION() => $args{+KDF_PARAM_ARGON2_VERSION} // $args{version},
36             KDF_PARAM_ARGON2_SECRET() => $args{+KDF_PARAM_ARGON2_SECRET} // $args{secret},
37             KDF_PARAM_ARGON2_ASSOCDATA() => $args{+KDF_PARAM_ARGON2_ASSOCDATA} // $args{assocdata},
38 19   66     236 );
      100        
      66        
      100        
      66        
      33        
      33        
39             }
40              
41             sub _transform {
42 9     9   14 my $self = shift;
43 9         12 my $key = shift;
44              
45 9         26 my ($uuid, $salt, $iterations, $memory, $parallelism)
46             = ($self->uuid, $self->salt, $self->iterations, $self->memory, $self->parallelism);
47              
48 9 100       27 if ($uuid eq KDF_UUID_ARGON2D) {
    50          
49 5         7052 return argon2d_raw($key, $salt, $iterations, $memory, $parallelism, length($salt));
50             }
51             elsif ($uuid eq KDF_UUID_ARGON2ID) {
52 4         1734 return argon2id_raw($key, $salt, $iterations, $memory, $parallelism, length($salt));
53             }
54              
55 0           throw 'Unknown Argon2 type', uuid => $uuid;
56             }
57              
58             1;
59              
60             __END__