libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
key.hh
Go to the documentation of this file.
1/*
2 * libkeepass - KeePass key database importer/exporter
3 * Copyright (C) 2014 Christian Kindahl
4 * Copyright (C) 2024 Dominik Krümpelmann
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
25#pragma once
26#include <array>
27#include <cstddef>
28#include <cstdint>
29#include <iosfwd>
30#include <string>
31#include <vector>
32
33#include "libkeepass/export.hh"
34#include "libkeepass/secure.hh"
35
36namespace keepass {
37
45class LIBKEEPASS_API Key final {
46public:
48
53 static constexpr std::size_t kMaxKeyFileBytes = 1024 * 1024;
54
59 enum class SubKeyResolution {
62 kHashSubKeys,
63
67 kHashSubKeysOnlyIfCompositeKey
68 };
69
73 enum class Kdf {
75 kAes,
76
78 kArgon2d,
79
81 kArgon2id
82 };
83
84private:
85 struct CompositeKey {
86 SecureBuffer<32> password_key_;
87 SecureBuffer<32> keyfile_key_;
88
89 void Resolve(SubKeyResolution resolution, SecureBuffer<32>& out) const;
90 } key_;
91
93 bool has_transformed_key_ = false;
94 SecureBuffer<32> transformed_key_;
95
96public:
98 Key() = default;
99
101 Key(const Key& other);
102
104 Key& operator=(const Key& other);
105
107
110 explicit Key(const std::string& password);
111
113
117 Key(const std::string& password, const std::string& keyfile);
118
120
127 explicit Key(const std::vector<uint8_t>& transformed_key);
128
130
133 void SetPassword(const std::string& password);
134
136
142 void SetKeyFile(const std::string& path);
143
145
151 void SetKeyFile(std::istream& src);
152
154
158 void SetKeyFileData(const uint8_t* data, std::size_t size);
159
161
166 static Key FromBytes(const std::string& password, const std::vector<uint8_t>& keyfile);
167
169
174 static Key FromStream(const std::string& password, std::istream& keyfile);
175
177
183 SecureBuffer<32> Transform(const SecureBuffer<32>& seed, uint64_t rounds,
184 SubKeyResolution resolution) const;
185
187
197 SecureBuffer<32> TransformArgon2(Kdf kdf, const SecureBytes& salt, uint64_t iterations,
198 uint64_t memory_bytes, uint32_t parallelism,
199 uint32_t argon2_version, SubKeyResolution resolution) const;
200};
201
202} // namespace keepass
Manages database encryption keys for KDB and KDBX formats.
Definition key.hh:45
Key(const std::vector< uint8_t > &transformed_key)
Constructs a key from a pre-derived transformed master key.
Key(const std::string &password)
Constructs a key with the given password.
static Key FromBytes(const std::string &password, const std::vector< uint8_t > &keyfile)
Creates a composite key from a password and an in-memory key file.
void SetKeyFile(const std::string &path)
Loads a key file and derives the key file component from it.
Key & operator=(const Key &other)
Copy-assigns, allocating wiped copies of all sub keys.
static Key FromStream(const std::string &password, std::istream &keyfile)
Creates a composite key from a password and a key-file stream.
SecureBuffer< 32 > TransformArgon2(Kdf kdf, const SecureBytes &salt, uint64_t iterations, uint64_t memory_bytes, uint32_t parallelism, uint32_t argon2_version, SubKeyResolution resolution) const
Derives the composite key using Argon2 for KDBX 4 databases.
Key(const std::string &password, const std::string &keyfile)
Constructs a composite key from a password and a key file.
void SetPassword(const std::string &password)
Sets the password used to derive the encryption key.
void SetKeyFile(std::istream &src)
Loads a key file from an in-memory stream.
Key(const Key &other)
Copy-constructs, allocating wiped copies of all sub keys.
Kdf
Definition key.hh:73
SecureBuffer< 32 > Transform(const SecureBuffer< 32 > &seed, uint64_t rounds, SubKeyResolution resolution) const
Derives the composite key using AES-KDF for KDBX 3 databases.
Key()=default
Default constructor. Creates an empty key with no password or key file.
void SetKeyFileData(const uint8_t *data, std::size_t size)
Loads a key file from an in-memory byte range.
SubKeyResolution
Definition key.hh:59
Fixed-size byte buffer that is zeroized on destruction.
Definition secure.hh:79
Dynamic byte buffer that is zeroized in best-effort locked memory.
Definition secure.hh:164
Visibility/export macro for the shared library.
#define LIBKEEPASS_API
Marks a class, function or variable as part of the public ABI.
Definition export.hh:43
Secure memory handling: zeroization, locked allocations and sensitive value containers.