Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SessionCipher.cpp « objects « fdosecrets « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 374580eed5a33d35d21165b90ecf9c9dd365468d (plain)
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
/*
 *  Copyright (C) 2019 Aetf <aetf@unlimitedcodeworks.xyz>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 or (at your option)
 *  version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "SessionCipher.h"

#include "crypto/CryptoHash.h"
#include "crypto/Random.h"
#include "crypto/SymmetricCipher.h"

#include <gcrypt.h>

#include <memory>

namespace
{
    constexpr const auto IETF1024_SECOND_OAKLEY_GROUP_P_HEX = "FFFFFFFFFFFFFFFFC90FDAA22168C234"
                                                              "C4C6628B80DC1CD129024E088A67CC74"
                                                              "020BBEA63B139B22514A08798E3404DD"
                                                              "EF9519B3CD3A431B302B0A6DF25F1437"
                                                              "4FE1356D6D51C245E485B576625E7EC6"
                                                              "F44C42E9A637ED6B0BFF5CB6F406B7ED"
                                                              "EE386BFB5A899FA5AE9F24117C4B1FE6"
                                                              "49286651ECE65381FFFFFFFFFFFFFFFF";
    constexpr const size_t KEY_SIZE_BYTES = 128;
    constexpr const int AES_KEY_LEN = 16; // 128 bits

    const auto IETF1024_SECOND_OAKLEY_GROUP_P = MpiFromHex(IETF1024_SECOND_OAKLEY_GROUP_P_HEX, false);
} // namespace

namespace FdoSecrets
{

    QVariant CipherPair::negotiationOutput() const
    {
        return {};
    }

    DhIetf1024Sha256Aes128CbcPkcs7::DhIetf1024Sha256Aes128CbcPkcs7(const QByteArray& clientPublicKeyBytes)
        : m_valid(false)
    {
        // read client public key
        auto clientPub = MpiFromBytes(clientPublicKeyBytes, false);

        // generate server side private, 128 bytes
        GcryptMPI serverPrivate(gcry_mpi_snew(KEY_SIZE_BYTES * 8));
        gcry_mpi_randomize(serverPrivate.get(), KEY_SIZE_BYTES * 8, GCRY_STRONG_RANDOM);

        // generate server side public key
        GcryptMPI serverPublic(gcry_mpi_snew(KEY_SIZE_BYTES * 8));
        // the generator of Second Oakley Group is 2
        gcry_mpi_powm(serverPublic.get(), GCRYMPI_CONST_TWO, serverPrivate.get(), IETF1024_SECOND_OAKLEY_GROUP_P.get());

        initialize(std::move(clientPub), std::move(serverPublic), std::move(serverPrivate));
    }

    bool
    DhIetf1024Sha256Aes128CbcPkcs7::initialize(GcryptMPI clientPublic, GcryptMPI serverPublic, GcryptMPI serverPrivate)
    {
        QByteArray commonSecretBytes;
        if (!diffieHullman(clientPublic, serverPrivate, commonSecretBytes)) {
            return false;
        }

        m_privateKey = MpiToBytes(serverPrivate);
        m_publicKey = MpiToBytes(serverPublic);

        m_aesKey = hkdf(commonSecretBytes);

        m_valid = true;
        return true;
    }

    bool DhIetf1024Sha256Aes128CbcPkcs7::diffieHullman(const GcryptMPI& clientPub,
                                                       const GcryptMPI& serverPrivate,
                                                       QByteArray& commonSecretBytes)
    {
        if (!clientPub || !serverPrivate) {
            return false;
        }

        // calc common secret
        GcryptMPI commonSecret(gcry_mpi_snew(KEY_SIZE_BYTES * 8));
        gcry_mpi_powm(commonSecret.get(), clientPub.get(), serverPrivate.get(), IETF1024_SECOND_OAKLEY_GROUP_P.get());
        commonSecretBytes = MpiToBytes(commonSecret);

        return true;
    }

    QByteArray DhIetf1024Sha256Aes128CbcPkcs7::hkdf(const QByteArray& IKM)
    {
        // HKDF-Extract(salt, IKM) -> PRK
        // PRK = HMAC-Hash(salt, IKM)

        // we use NULL salt as per spec
        auto PRK = CryptoHash::hmac(IKM,
                                    QByteArrayLiteral("\0\0\0\0\0\0\0\0"
                                                      "\0\0\0\0\0\0\0\0"
                                                      "\0\0\0\0\0\0\0\0"
                                                      "\0\0\0\0\0\0\0\0"),
                                    CryptoHash::Sha256);

        // HKDF-Expand(PRK, info, L) -> OKM
        // N = ceil(L/HashLen)
        // T = T(1) | T(2) | T(3) | ... | T(N)
        // OKM = first L octets of T
        // where:
        //   T(0) = empty string (zero length)
        //   T(1) = HMAC-Hash(PRK, T(0) | info | 0x01)
        //   T(2) = HMAC-Hash(PRK, T(1) | info | 0x02)
        //   T(3) = HMAC-Hash(PRK, T(2) | info | 0x03)
        //   ...
        //
        //   (where the constant concatenated to the end of each T(n) is a
        //   single octet.)

        // we use empty info as per spec
        // HashLen = 32 (sha256)
        // L = 16 (16 * 8 = 128 bits)
        // N = ceil(16/32) = 1

        auto T1 = CryptoHash::hmac(QByteArrayLiteral("\x01"), PRK, CryptoHash::Sha256);

        // resulting AES key is first 128 bits
        Q_ASSERT(T1.size() >= AES_KEY_LEN);
        auto OKM = T1.left(AES_KEY_LEN);
        return OKM;
    }

    SecretStruct DhIetf1024Sha256Aes128CbcPkcs7::encrypt(const SecretStruct& input)
    {
        SecretStruct output = input;
        output.value.clear();
        output.parameters.clear();

        SymmetricCipher encrypter(SymmetricCipher::Aes128, SymmetricCipher::Cbc, SymmetricCipher::Encrypt);

        auto IV = randomGen()->randomArray(SymmetricCipher::algorithmIvSize(SymmetricCipher::Aes128));
        if (!encrypter.init(m_aesKey, IV)) {
            qWarning() << "Error encrypt: " << encrypter.errorString();
            return output;
        }

        output.parameters = IV;

        bool ok;
        output.value = input.value;
        output.value = encrypter.process(padPkcs7(output.value, encrypter.blockSize()), &ok);
        if (!ok) {
            qWarning() << "Error encrypt: " << encrypter.errorString();
            return output;
        }

        return output;
    }

    QByteArray& DhIetf1024Sha256Aes128CbcPkcs7::padPkcs7(QByteArray& input, int blockSize)
    {
        // blockSize must be a power of 2.
        Q_ASSERT_X(blockSize > 0 && !(blockSize & (blockSize - 1)), "padPkcs7", "blockSize must be a power of 2");

        int padLen = blockSize - (input.size() & (blockSize - 1));

        input.append(QByteArray(padLen, static_cast<char>(padLen)));
        return input;
    }

    SecretStruct DhIetf1024Sha256Aes128CbcPkcs7::decrypt(const SecretStruct& input)
    {
        auto IV = input.parameters;
        SymmetricCipher decrypter(SymmetricCipher::Aes128, SymmetricCipher::Cbc, SymmetricCipher::Decrypt);
        if (!decrypter.init(m_aesKey, IV)) {
            qWarning() << "Error decoding: " << decrypter.errorString();
            return input;
        }
        bool ok;
        SecretStruct output = input;
        output.parameters.clear();
        output.value = decrypter.process(input.value, &ok);

        if (!ok) {
            qWarning() << "Error decoding: " << decrypter.errorString();
            return input;
        }

        unpadPkcs7(output.value);
        return output;
    }

    QByteArray& DhIetf1024Sha256Aes128CbcPkcs7::unpadPkcs7(QByteArray& input)
    {
        if (input.isEmpty()) {
            return input;
        }

        int padLen = input[input.size() - 1];
        input.chop(padLen);
        return input;
    }

    bool DhIetf1024Sha256Aes128CbcPkcs7::isValid() const
    {
        return m_valid;
    }

    QVariant DhIetf1024Sha256Aes128CbcPkcs7::negotiationOutput() const
    {
        return m_publicKey;
    }

} // namespace FdoSecrets