1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use super::errors::HeaderError;
use super::header::{HeaderField, InnerHeaderId, OuterHeaderId};
use super::variant_dict::{self, VariantDict};
use crate::utils;
use std::convert::{TryFrom, TryInto};
use uuid::Uuid;

pub const KEEPASS_MAGIC_NUMBER: u32 = 0x9AA2_D903;
pub const KDBX_MAGIC_NUMBER: u32 = 0xB54B_FB67;

const AES128_UUID: &str = "61ab05a1-9464-41c3-8d74-3a563df8dd35";
const AES256_UUID: &str = "31c1f2e6-bf71-4350-be58-05216afc5aff";
const TWOFISH_UUID: &str = "ad68f29f-576f-4bb9-a36a-d47af965346c";
const CHACHA20_UUID: &str = "d6038a2b-8b6f-4cb5-a524-339a31dbb59a";
const AES_3_1_UUID: &str = "c9d9f39a-628a-4460-bf74-0d08c18a4fea";
const AES_4_UUID: &str = "7c02bb82-79a7-4ac0-927d-114a00648238";
const ARGON2D_UUID: &str = "ef636ddf-8c29-444b-91f7-a9a403e30a0c";
const ARGON2ID_UUID: &str = "9e298b19-56db-4773-b23d-fc3ec6f0a1e6";
const COMPRESSION_TYPE_NONE: u32 = 0;
const COMPRESSION_TYPE_GZIP: u32 = 1;

#[derive(PartialEq, Eq, Debug, Copy, Clone)]
/// Encryption cipher used for decryption the main database data
pub enum Cipher {
    /// AES 128 in CBC mode
    Aes128,
    /// AES 256 in CBC mode
    Aes256,
    /// TwoFish in CBC mode
    TwoFish,
    /// ChaCha20 in streaming mode
    ChaCha20,
    /// Cipher unknown to this library
    Unknown(uuid::Uuid),
}

const CIPHER_TABLE: [(&str, Cipher); 4] = [
    (AES128_UUID, Cipher::Aes128),
    (AES256_UUID, Cipher::Aes256),
    (TWOFISH_UUID, Cipher::TwoFish),
    (CHACHA20_UUID, Cipher::ChaCha20),
];

impl From<uuid::Uuid> for Cipher {
    fn from(uuid: uuid::Uuid) -> Cipher {
        utils::value_from_uuid_table(&CIPHER_TABLE, uuid).unwrap_or(Cipher::Unknown(uuid))
    }
}

impl From<Cipher> for uuid::Uuid {
    fn from(cipher: Cipher) -> uuid::Uuid {
        match cipher {
            Cipher::Unknown(uuid) => uuid,
            _ => utils::uuid_from_uuid_table(&CIPHER_TABLE, cipher).unwrap(),
        }
    }
}

impl From<Cipher> for HeaderField<OuterHeaderId> {
    fn from(cipher: Cipher) -> HeaderField<OuterHeaderId> {
        let uuid: uuid::Uuid = cipher.into();
        HeaderField::new(OuterHeaderId::CipherId, uuid.as_bytes().to_vec())
    }
}

/// Inner stream cipher identifier used for encrypting protected fields
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum InnerStreamCipherAlgorithm {
    /// ArcFour algorithm
    ArcFour,
    /// Salsa20 stream cipher
    Salsa20,
    /// ChaCha20 stream cipher
    ChaCha20,
    /// Unknown stream cipher
    Unknown(u32),
}

impl From<InnerStreamCipherAlgorithm> for HeaderField<InnerHeaderId> {
    fn from(cipher: InnerStreamCipherAlgorithm) -> HeaderField<InnerHeaderId> {
        HeaderField::new(
            InnerHeaderId::InnerRandomStreamCipherId,
            u32::from(cipher).to_le_bytes().as_ref().to_vec(),
        )
    }
}

impl From<u32> for InnerStreamCipherAlgorithm {
    fn from(id: u32) -> InnerStreamCipherAlgorithm {
        match id {
            1 => InnerStreamCipherAlgorithm::ArcFour,
            2 => InnerStreamCipherAlgorithm::Salsa20,
            3 => InnerStreamCipherAlgorithm::ChaCha20,
            x => InnerStreamCipherAlgorithm::Unknown(x),
        }
    }
}

impl From<InnerStreamCipherAlgorithm> for u32 {
    fn from(id: InnerStreamCipherAlgorithm) -> u32 {
        match id {
            InnerStreamCipherAlgorithm::ArcFour => 1,
            InnerStreamCipherAlgorithm::Salsa20 => 2,
            InnerStreamCipherAlgorithm::ChaCha20 => 3,
            InnerStreamCipherAlgorithm::Unknown(x) => x,
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[allow(non_camel_case_types)]
/// Algorithm used for converting from credentials to crypto keys
pub enum KdfAlgorithm {
    /// Argon2d KDF
    Argon2d,
    /// Argon2id KDF
    Argon2id,
    /// AES 256 as used in KDBX4+
    Aes256_Kdbx4,
    /// AES 256 as used in KDBX3.1
    Aes256_Kdbx3_1,
    /// Unknown key derivation function
    Unknown(uuid::Uuid),
}

pub(crate) const KDF_TABLE: [(&str, KdfAlgorithm); 4] = [
    (AES_3_1_UUID, KdfAlgorithm::Aes256_Kdbx3_1),
    (AES_4_UUID, KdfAlgorithm::Aes256_Kdbx4),
    (ARGON2D_UUID, KdfAlgorithm::Argon2d),
    (ARGON2ID_UUID, KdfAlgorithm::Argon2id),
];

impl From<uuid::Uuid> for KdfAlgorithm {
    fn from(uuid: uuid::Uuid) -> KdfAlgorithm {
        utils::value_from_uuid_table(&KDF_TABLE, uuid).unwrap_or(KdfAlgorithm::Unknown(uuid))
    }
}

impl From<KdfAlgorithm> for uuid::Uuid {
    fn from(algo: KdfAlgorithm) -> uuid::Uuid {
        match algo {
            KdfAlgorithm::Unknown(uuid) => uuid,
            _ => utils::uuid_from_uuid_table(&KDF_TABLE, algo).unwrap(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// Options for converting credentials to crypto keys
pub enum KdfParams {
    /// Argon 2 KDF
    Argon2 {
        /// Argon2 variant
        variant: argon2::Variant,
        /// Amount of memory to use for key gen
        memory_bytes: u64,
        /// Argon2 version used (this library supports v19/0x13)
        version: u32,
        /// Random seed data to use for the KDF
        salt: Vec<u8>,
        /// Number of parallel tasks to use
        lanes: u32,
        /// Passes of the KDF to use for key gen
        iterations: u64,
    },
    /// AES256 KDF
    Aes {
        /// Rounds of AES to use for key generation
        rounds: u64,
        /// Random seed data to use for the KDF
        salt: Vec<u8>,
    },
    /// Some KDF unknown to this library
    Unknown {
        /// UUID to identify the KDF used
        uuid: uuid::Uuid,
        /// Parameters to the KDF
        params: variant_dict::VariantDict,
    },
}

impl TryFrom<VariantDict> for KdfParams {
    type Error = HeaderError;
    fn try_from(mut vdict: VariantDict) -> Result<Self, HeaderError> {
        let uuid_val = vdict.remove("$UUID").ok_or_else(|| {
            HeaderError::MalformedField(
                OuterHeaderId::KdfParameters,
                "No UUID for kdf parameters".into(),
            )
        })?;
        let uuid_array: Vec<u8> = uuid_val.try_into().map_err(|_| {
            HeaderError::MalformedField(
                OuterHeaderId::KdfParameters,
                "KDF UUID not a byte array".into(),
            )
        })?;
        let uuid = Uuid::from_slice(&uuid_array).map_err(|_| {
            HeaderError::MalformedField(
                OuterHeaderId::KdfParameters,
                "KDF UUID not a valid UUID".into(),
            )
        })?;

        let kdf_algorithm = KdfAlgorithm::from(uuid);

        match kdf_algorithm {
            KdfAlgorithm::Argon2d | KdfAlgorithm::Argon2id => {
                let memory_bytes =
                    KdfParams::opt_from_vdict("M", KdfAlgorithm::Argon2d, &mut vdict)?;
                let version = KdfParams::opt_from_vdict("V", KdfAlgorithm::Argon2d, &mut vdict)?;
                let salt = KdfParams::opt_from_vdict("S", KdfAlgorithm::Argon2d, &mut vdict)?;
                let iterations = KdfParams::opt_from_vdict("I", KdfAlgorithm::Argon2d, &mut vdict)?;
                let lanes = KdfParams::opt_from_vdict("P", KdfAlgorithm::Argon2d, &mut vdict)?;
                Ok(KdfParams::Argon2 {
                    variant: utils::argon2_algo_to_variant(kdf_algorithm),
                    memory_bytes,
                    version,
                    salt,
                    iterations,
                    lanes,
                })
            }
            KdfAlgorithm::Aes256_Kdbx3_1 | KdfAlgorithm::Aes256_Kdbx4 => {
                let rounds =
                    KdfParams::opt_from_vdict("R", KdfAlgorithm::Aes256_Kdbx4, &mut vdict)?;
                let salt = KdfParams::opt_from_vdict("S", KdfAlgorithm::Aes256_Kdbx4, &mut vdict)?;
                Ok(KdfParams::Aes { rounds, salt })
            }
            _ => Ok(KdfParams::Unknown {
                uuid,
                params: vdict,
            }),
        }
    }
}

impl From<KdfParams> for VariantDict {
    fn from(params: KdfParams) -> VariantDict {
        let mut vdict = variant_dict::VariantDict::new();
        match params {
            KdfParams::Argon2 {
                variant,
                memory_bytes,
                version,
                salt,
                lanes,
                iterations,
            } => {
                vdict.insert(
                    "$UUID".into(),
                    variant_dict::Value::Array(
                        uuid::Uuid::from(utils::argon2_variant_to_algo(variant))
                            .as_bytes()
                            .to_vec(),
                    ),
                );
                vdict.insert("M".into(), variant_dict::Value::Uint64(memory_bytes));
                vdict.insert("V".into(), variant_dict::Value::Uint32(version));
                vdict.insert("S".into(), variant_dict::Value::Array(salt));
                vdict.insert("I".into(), variant_dict::Value::Uint64(iterations));
                vdict.insert("P".into(), variant_dict::Value::Uint32(lanes));
            }
            KdfParams::Aes { rounds, salt } => {
                vdict.insert(
                    "$UUID".into(),
                    variant_dict::Value::Array(
                        uuid::Uuid::from(KdfAlgorithm::Aes256_Kdbx4)
                            .as_bytes()
                            .to_vec(),
                    ),
                );
                vdict.insert("R".into(), variant_dict::Value::Uint64(rounds));
                vdict.insert("S".into(), variant_dict::Value::Array(salt));
            }
            KdfParams::Unknown { uuid, params } => {
                vdict.insert(
                    "$UUID".into(),
                    variant_dict::Value::Array(uuid.as_bytes().to_vec()),
                );
                vdict.extend(params.into_iter())
            }
        }
        vdict
    }
}

impl From<KdfParams> for HeaderField<OuterHeaderId> {
    fn from(params: KdfParams) -> HeaderField<OuterHeaderId> {
        let mut buf = Vec::new();
        let vdict: VariantDict = params.into();
        variant_dict::write_variant_dict(&mut buf, &vdict).unwrap();
        HeaderField::new(OuterHeaderId::KdfParameters, buf)
    }
}

impl KdfParams {
    fn opt_from_vdict<T>(
        key: &str,
        algo: KdfAlgorithm,
        vdict: &mut variant_dict::VariantDict,
    ) -> Result<T, HeaderError>
    where
        T: TryFrom<variant_dict::Value>,
    {
        vdict
            .remove(key)
            .and_then(|val| val.try_into().ok())
            .ok_or_else(|| HeaderError::MissingKdfParam(key.to_string(), algo))
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
/// Compression method used prior to encryption
pub enum CompressionType {
    /// The encrypted data is uncompressed
    None,
    /// The encrypted data uses gzip compression
    Gzip,
    /// The crypted data uses a compression method unsupported by this library
    Unknown(u32),
}

impl From<CompressionType> for u32 {
    fn from(compression_type: CompressionType) -> u32 {
        match compression_type {
            CompressionType::None => COMPRESSION_TYPE_NONE,
            CompressionType::Gzip => COMPRESSION_TYPE_GZIP,
            CompressionType::Unknown(val) => val,
        }
    }
}

impl From<u32> for CompressionType {
    fn from(id: u32) -> CompressionType {
        match id {
            COMPRESSION_TYPE_NONE => CompressionType::None,
            COMPRESSION_TYPE_GZIP => CompressionType::Gzip,
            _ => CompressionType::Unknown(id),
        }
    }
}

impl From<CompressionType> for HeaderField<OuterHeaderId> {
    fn from(compression_type: CompressionType) -> HeaderField<OuterHeaderId> {
        let compression_type_id: u32 = compression_type.into();
        HeaderField::new(
            OuterHeaderId::CompressionFlags,
            Vec::from(compression_type_id.to_le_bytes().as_ref()),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use uuid::Uuid;
    #[test]
    fn kdf_from_slice() {
        let aes31 = Uuid::parse_str(AES_3_1_UUID).unwrap();
        let aes4 = Uuid::parse_str(AES_4_UUID).unwrap();
        let argon2 = Uuid::parse_str(ARGON2D_UUID).unwrap();
        let invalid = Uuid::parse_str(AES128_UUID).unwrap();

        assert_eq!(KdfAlgorithm::from(aes31), KdfAlgorithm::Aes256_Kdbx3_1);
        assert_eq!(KdfAlgorithm::from(aes4), KdfAlgorithm::Aes256_Kdbx4);
        assert_eq!(KdfAlgorithm::from(argon2), KdfAlgorithm::Argon2d);
        assert_eq!(KdfAlgorithm::from(invalid), KdfAlgorithm::Unknown(invalid));
    }
}