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   1068 use warnings;
  2         5  
  2         63  
5 2     2   9 use strict;
  2         4  
  2         47  
6              
7 2     2   745 use Crypt::Argon2 qw(argon2d_raw argon2id_raw);
  2         3365  
  2         104  
8 2     2   14 use File::KDBX::Constants qw(:kdf);
  2         4  
  2         339  
9 2     2   15 use File::KDBX::Error;
  2         3  
  2         88  
10 2     2   10 use File::KDBX::Util qw(:class);
  2         4  
  2         167  
11 2     2   13 use namespace::clean;
  2         6  
  2         12  
12              
13             extends 'File::KDBX::KDF';
14              
15             our $VERSION = '0.905'; # VERSION
16              
17              
18 16 50   16 1 81 sub salt { $_[0]->{+KDF_PARAM_ARGON2_SALT} or throw 'Salt is not set' }
19 7     7 1 13 sub seed { $_[0]->salt }
20 9   50 9 1 23 sub parallelism { $_[0]->{+KDF_PARAM_ARGON2_PARALLELISM} //= KDF_DEFAULT_ARGON2_PARALLELISM }
21 9   100 9 1 26 sub memory { $_[0]->{+KDF_PARAM_ARGON2_MEMORY} //= KDF_DEFAULT_ARGON2_MEMORY }
22 9   50 9 1 28 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         51 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     219 );
      100        
      66        
      100        
      66        
      33        
      33        
39             }
40              
41             sub _transform {
42 9     9   16 my $self = shift;
43 9         13 my $key = shift;
44              
45 9         22 my ($uuid, $salt, $iterations, $memory, $parallelism)
46             = ($self->uuid, $self->salt, $self->iterations, $self->memory, $self->parallelism);
47              
48 9 100       30 if ($uuid eq KDF_UUID_ARGON2D) {
    50          
49 5         9332 return argon2d_raw($key, $salt, $iterations, $memory, $parallelism, length($salt));
50             }
51             elsif ($uuid eq KDF_UUID_ARGON2ID) {
52 4         2367 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__