1 /*
2  *  Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 2 or (at your option)
7  *  version 3 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef KEEPASSX_KEEPASS2_H
19 #define KEEPASSX_KEEPASS2_H
20 
21 #include <QList>
22 #include <QMap>
23 #include <QSharedPointer>
24 #include <QUuid>
25 #include <QVariantMap>
26 #include <QtGlobal>
27 
28 #include "crypto/SymmetricCipher.h"
29 #include "crypto/kdf/Kdf.h"
30 
31 namespace KeePass2
32 {
33 
34     constexpr quint32 SIGNATURE_1 = 0x9AA2D903;
35     constexpr quint32 SIGNATURE_2 = 0xB54BFB67;
36 
37     constexpr quint32 FILE_VERSION_CRITICAL_MASK = 0xFFFF0000;
38     constexpr quint32 FILE_VERSION_4 = 0x00040000;
39     constexpr quint32 FILE_VERSION_3_1 = 0x00030001;
40     constexpr quint32 FILE_VERSION_3 = 0x00030000;
41     constexpr quint32 FILE_VERSION_2 = 0x00020000;
42     constexpr quint32 FILE_VERSION_MIN = FILE_VERSION_2;
43 
44     constexpr quint16 VARIANTMAP_VERSION = 0x0100;
45     constexpr quint16 VARIANTMAP_CRITICAL_MASK = 0xFF00;
46 
47     const QSysInfo::Endian BYTEORDER = QSysInfo::LittleEndian;
48 
49     extern const QUuid CIPHER_AES128;
50     extern const QUuid CIPHER_AES256;
51     extern const QUuid CIPHER_TWOFISH;
52     extern const QUuid CIPHER_CHACHA20;
53 
54     extern const QUuid KDF_AES_KDBX3;
55     extern const QUuid KDF_AES_KDBX4;
56     extern const QUuid KDF_ARGON2D;
57     extern const QUuid KDF_ARGON2ID;
58 
59     extern const QByteArray INNER_STREAM_SALSA20_IV;
60 
61     extern const QString KDFPARAM_UUID;
62     extern const QString KDFPARAM_AES_ROUNDS;
63     extern const QString KDFPARAM_AES_SEED;
64     extern const QString KDFPARAM_ARGON2_SALT;
65     extern const QString KDFPARAM_ARGON2_PARALLELISM;
66     extern const QString KDFPARAM_ARGON2_MEMORY;
67     extern const QString KDFPARAM_ARGON2_ITERATIONS;
68     extern const QString KDFPARAM_ARGON2_VERSION;
69     extern const QString KDFPARAM_ARGON2_SECRET;
70     extern const QString KDFPARAM_ARGON2_ASSOCDATA;
71 
72     extern const QList<QPair<QUuid, QString>> CIPHERS;
73     extern const QList<QPair<QUuid, QString>> KDFS;
74 
75     enum class HeaderFieldID
76     {
77         EndOfHeader = 0,
78         Comment = 1,
79         CipherID = 2,
80         CompressionFlags = 3,
81         MasterSeed = 4,
82         TransformSeed = 5,
83         TransformRounds = 6,
84         EncryptionIV = 7,
85         ProtectedStreamKey = 8,
86         StreamStartBytes = 9,
87         InnerRandomStreamID = 10,
88         KdfParameters = 11,
89         PublicCustomData = 12
90     };
91 
92     enum class InnerHeaderFieldID : quint8
93     {
94         End = 0,
95         InnerRandomStreamID = 1,
96         InnerRandomStreamKey = 2,
97         Binary = 3
98     };
99 
100     enum class ProtectedStreamAlgo
101     {
102         ArcFourVariant = 1,
103         Salsa20 = 2,
104         ChaCha20 = 3,
105         InvalidProtectedStreamAlgo = -1
106     };
107 
108     enum class VariantMapFieldType : quint8
109     {
110         End = 0,
111         // Byte = 0x02,
112         // UInt16 = 0x03,
113         UInt32 = 0x04,
114         UInt64 = 0x05,
115         // Signed mask: 0x08
116         Bool = 0x08,
117         // SByte = 0x0A,
118         // Int16 = 0x0B,
119         Int32 = 0x0C,
120         Int64 = 0x0D,
121         // Float = 0x10,
122         // Double = 0x11,
123         // Decimal = 0x12,
124         // Char = 0x17, // 16-bit Unicode character
125         String = 0x18,
126         // Array mask: 0x40
127         ByteArray = 0x42
128     };
129 
130     QByteArray hmacKey(const QByteArray& masterSeed, const QByteArray& transformedMasterKey);
131     QSharedPointer<Kdf> kdfFromParameters(const QVariantMap& p);
132     QVariantMap kdfToParameters(const QSharedPointer<Kdf>& kdf);
133     QSharedPointer<Kdf> uuidToKdf(const QUuid& uuid);
134     ProtectedStreamAlgo idToProtectedStreamAlgo(quint32 id);
135 
136 } // namespace KeePass2
137 
138 #endif // KEEPASSX_KEEPASS2_H
139