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   1237 use warnings;
  2         4  
  2         76  
5 2     2   12 use strict;
  2         3  
  2         51  
6              
7 2     2   735 use Crypt::Argon2 qw(argon2d_raw argon2id_raw);
  2         3335  
  2         120  
8 2     2   16 use File::KDBX::Constants qw(:kdf);
  2         4  
  2         371  
9 2     2   14 use File::KDBX::Error;
  2         5  
  2         103  
10 2     2   11 use File::KDBX::Util qw(:class);
  2         4  
  2         224  
11 2     2   13 use namespace::clean;
  2         4  
  2         17  
12              
13             extends 'File::KDBX::KDF';
14              
15             our $VERSION = '0.904'; # VERSION
16              
17              
18 16 50   16 1 65 sub salt { $_[0]->{+KDF_PARAM_ARGON2_SALT} or throw 'Salt is not set' }
19 7     7 1 27 sub seed { $_[0]->salt }
20 9   50 9 1 29 sub parallelism { $_[0]->{+KDF_PARAM_ARGON2_PARALLELISM} //= KDF_DEFAULT_ARGON2_PARALLELISM }
21 9   100 9 1 37 sub memory { $_[0]->{+KDF_PARAM_ARGON2_MEMORY} //= KDF_DEFAULT_ARGON2_MEMORY }
22 9   50 9 1 38 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 33 my $self = shift;
29 19         66 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     331 );
      100        
      66        
      100        
      66        
      33        
      33        
39             }
40              
41             sub _transform {
42 9     9   22 my $self = shift;
43 9         13 my $key = shift;
44              
45 9         27 my ($uuid, $salt, $iterations, $memory, $parallelism)
46             = ($self->uuid, $self->salt, $self->iterations, $self->memory, $self->parallelism);
47              
48 9 100       36 if ($uuid eq KDF_UUID_ARGON2D) {
    50          
49 5         7128 return argon2d_raw($key, $salt, $iterations, $memory, $parallelism, length($salt));
50             }
51             elsif ($uuid eq KDF_UUID_ARGON2ID) {
52 4         2337 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__