1 package File
::KDBX
::KDF
;
2 # ABSTRACT: A key derivation function
7 use Crypt
::PRNG
qw(random_bytes);
8 use File
::KDBX
::Constants
qw(:version :kdf);
10 use File
::KDBX
::Util
qw(format_uuid);
12 use Scalar
::Util
qw(blessed);
15 our $VERSION = '0.902'; # VERSION
24 my $uuid = $args{+KDF_PARAM_UUID
} //= delete $args{uuid
} or throw
'Missing KDF UUID', args
=> \
%args;
25 my $formatted_uuid = format_uuid
($uuid);
27 my $kdf = $KDFS{$uuid} or throw
"Unsupported KDF ($formatted_uuid)", uuid
=> $uuid;
28 ($class, my %registration_args) = @$kdf;
31 my $self = bless {KDF_PARAM_UUID
() => $uuid}, $class;
32 return $self->init(%args, %registration_args);
40 @$self{keys %args} = values %args;
46 sub uuid
{ $_[0]->{+KDF_PARAM_UUID
} }
49 sub seed
{ die 'Not implemented' }
56 if (blessed
$key && $key->can('raw_key')) {
57 return $self->_transform($key->raw_key) if $self->uuid eq KDF_UUID_AES
;
58 return $self->_transform($key->raw_key($self->seed, @_));
61 return $self->_transform($key);
64 sub _transform
{ die 'Not implemented' }
69 $self->{+KDF_PARAM_AES_SEED
} = random_bytes
(length($self->seed));
79 my $formatted_id = format_uuid
($id);
80 $package = "${class}::${package}" if $package !~ s/^\+// && $package !~ /^\Q${class}::\E/;
82 my %blacklist = map { File
::KDBX
::Util
::uuid
($_) => 1 } split(/,/, $ENV{FILE_KDBX_KDF_BLACKLIST
} // '');
83 if ($blacklist{$id} || $blacklist{$package}) {
84 alert
"Ignoring blacklisted KDF ($formatted_id)", id
=> $id, package => $package;
88 if (defined $KDFS{$id}) {
89 alert
"Overriding already-registered KDF ($formatted_id) with package $package",
94 $KDFS{$id} = [$package, @args];
99 delete $KDFS{$_} for @_;
103 __PACKAGE__-
>register(KDF_UUID_AES
, 'AES');
104 __PACKAGE__-
>register(KDF_UUID_AES_CHALLENGE_RESPONSE
, 'AES');
105 __PACKAGE__-
>register(KDF_UUID_ARGON2D
, 'Argon2');
106 __PACKAGE__-
>register(KDF_UUID_ARGON2ID
, 'Argon2');
119 File::KDBX::KDF - A key derivation function
127 A KDF (key derivation function) is used in the transformation of a master key (i.e. one or more component
128 keys) to produce the final encryption key protecting a KDBX database. The L<File::KDBX> distribution comes
129 with several pre-registered KDFs ready to go:
135 C<C9D9F39A-628A-4460-BF74-0D08C18A4FEA> - AES
139 C<7C02BB82-79A7-4AC0-927D-114A00648238> - AES (challenge-response variant)
143 C<EF636DDF-8C29-444B-91F7-A9A403E30A0C> - Argon2d
147 C<9E298B19-56DB-4773-B23D-FC3EC6F0A1E6> - Argon2id
151 B<NOTE:> If you want your KDBX file to be readable by other KeePass implementations, you must use a UUID and
152 algorithm that they support. From the list above, all are well-supported except the AES challenge-response
153 variant which is kind of a pseudo KDF and isn't usually written into files. All of these are good. AES has
154 a longer track record, but Argon2 has better ASIC resistance.
156 You can also L</register> your own KDF. Here is a skeleton:
158 package File::KDBX::KDF::MyKDF;
160 use parent 'File::KDBX::KDF';
162 File::KDBX::KDF->register(
163 # $uuid, $package, %args
164 "\x12\x34\x56\x78\x9a\xbc\xde\xfg\x12\x34\x56\x78\x9a\xbc\xde\xfg" => __PACKAGE__,
167 sub init { ... } # optional
169 sub _transform { my ($key) = @_; ... }
177 Get the UUID used to determine which function to use.
183 Get the seed (or salt, depending on the function).
189 $kdf = File::KDBX::KDF->new(parameters => \%params);
195 $kdf = $kdf->init(%attributes);
197 Called by method to set attributes. You normally shouldn't call this.
201 $transformed_key = $kdf->transform($key);
202 $transformed_key = $kdf->transform($key, $challenge);
204 Transform a key. The input key can be either a L<File::KDBX::Key> or a raw binary key, and the
205 transformed key will be a raw key.
207 This can take awhile, depending on the KDF parameters.
209 If a challenge is provided (and the KDF is AES except for the KeePassXC variant), it will be passed to the key
210 so challenge-response keys can produce raw keys. See L<File::KDBX::Key/raw_key>.
212 =head2 randomize_seed
214 $kdf->randomize_seed;
216 Generate a new random seed/salt.
220 File::KDBX::KDF->register($uuid => $package, %args);
222 Register a KDF. Registered KDFs can be used to encrypt and decrypt KDBX databases. A KDF's UUID B<must> be
223 unique and B<musn't change>. A KDF UUID is written into each KDBX file and the associated KDF must be
224 registered with the same UUID in order to decrypt the KDBX file.
226 C<$package> should be a Perl package relative to C<File::KDBX::KDF::> or prefixed with a C<+> if it is
227 a fully-qualified package. C<%args> are passed as-is to the KDF's L</init> method.
231 File::KDBX::KDF->unregister($uuid);
233 Unregister a KDF. Unregistered KDFs can no longer be used to encrypt and decrypt KDBX databases, until
234 reregistered (see L</register>).
238 Please report any bugs or feature requests on the bugtracker website
239 L<https://github.com/chazmcgarvey/File-KDBX/issues>
241 When submitting a bug or request, please include a test-file or a
242 patch to an existing test-file that illustrates the bug or desired
247 Charles McGarvey <ccm@cpan.org>
249 =head1 COPYRIGHT AND LICENSE
251 This software is copyright (c) 2022 by Charles McGarvey.
253 This is free software; you can redistribute it and/or modify it under
254 the same terms as the Perl 5 programming language system itself.