forked from steganos-oss/locknote2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaeslayer.cpp
More file actions
321 lines (283 loc) · 7.92 KB
/
Copy pathaeslayer.cpp
File metadata and controls
321 lines (283 loc) · 7.92 KB
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
#include <array>
#include <vector>
#include <cstring>
#include <algorithm>
#include "cryptopp/sha.h"
#include "cryptopp/pwdbased.h"
#include "cryptopp/scrypt.h"
#include "cryptopp/aes.h"
#include "cryptopp/modes.h"
#include "cryptopp/hmac.h"
#include "cryptopp/misc.h"
#include "aeslayer.h"
NAMESPACE_BEGIN(CryptoPP)
namespace
{
constexpr std::array<byte, AESLayer::FORMAT_HEADER_SIZE - 1> kFormatMagic{ 'L', 'N', '2', 0x02 };
constexpr unsigned int kScryptBlockSize = 8;
constexpr unsigned int kScryptParallelization = 5;
constexpr unsigned int kIvDerivationCost = 2;
bool IsKnownKdfMode(const byte modeValue)
{
return modeValue == static_cast<byte>(AESLayer::KdfMode::Scrypt) ||
modeValue == static_cast<byte>(AESLayer::KdfMode::Pbkdf2Sha256);
}
AESLayer::KdfMode ToKdfMode(const byte modeValue)
{
return modeValue == static_cast<byte>(AESLayer::KdfMode::Pbkdf2Sha256)
? AESLayer::KdfMode::Pbkdf2Sha256
: AESLayer::KdfMode::Scrypt;
}
void DeriveKeyAndIv(
const AESLayer::KdfMode mode,
ConstByteArrayParameter const& passphrase,
const byte* salt,
const byte* ivSeed,
SecByteBlock& key,
SecByteBlock& iv)
{
if (mode == AESLayer::KdfMode::Pbkdf2Sha256)
{
PKCS5_PBKDF2_HMAC<SHA256> pbkdf;
const byte purposeUnused = 0;
pbkdf.DeriveKey(
key.begin(),
key.size(),
purposeUnused,
passphrase.begin(),
passphrase.size(),
salt,
AESLayer::SALT_SIZE,
AESLayer::KEY_ITERATIONS,
0.0);
pbkdf.DeriveKey(
iv.begin(),
iv.size(),
purposeUnused,
passphrase.begin(),
passphrase.size(),
ivSeed,
AESLayer::IV_SEED_SIZE,
AESLayer::KEY_ITERATIONS,
0.0);
return;
}
Scrypt scrypt;
scrypt.DeriveKey(
key.begin(),
key.size(),
passphrase.begin(),
passphrase.size(),
salt,
AESLayer::SALT_SIZE,
AESLayer::DERIVATION_COST,
kScryptBlockSize,
kScryptParallelization);
scrypt.DeriveKey(
iv.begin(),
iv.size(),
passphrase.begin(),
passphrase.size(),
ivSeed,
AESLayer::IV_SEED_SIZE,
kIvDerivationCost,
kScryptBlockSize,
kScryptParallelization);
}
bool ValidatePkcs7Padding(const byte* buffer, const size_t bufferSize, size_t& plainTextLength)
{
if (buffer == nullptr || bufferSize == 0)
{
return false;
}
const byte paddingByte = buffer[bufferSize - 1];
if (paddingByte == 0 || paddingByte > AES::BLOCKSIZE || paddingByte > bufferSize)
{
return false;
}
for (size_t i = 0; i < paddingByte; ++i)
{
if (buffer[bufferSize - 1 - i] != paddingByte)
{
return false;
}
}
plainTextLength = bufferSize - paddingByte;
return true;
}
bool VerifyAndDecrypt(
const AESLayer::KdfMode mode,
ConstByteArrayParameter const& passphrase,
const byte* authenticatedBegin,
const size_t authenticatedSize,
const byte* payload,
const size_t payloadSize,
const byte* salt,
const byte* ivSeed,
const byte* digest,
byte* output,
size_t& plainTextLength)
{
if (payloadSize == 0 || (payloadSize % AES::BLOCKSIZE) != 0)
{
return false;
}
SecByteBlock key(SHA256::DIGESTSIZE);
SecByteBlock iv(AESLayer::IV_SIZE);
DeriveKeyAndIv(mode, passphrase, salt, ivSeed, key, iv);
std::array<byte, HMAC<SHA256>::DIGESTSIZE> checkDigest{};
HMAC<SHA256>(key.begin(), key.size()).CalculateDigest(
checkDigest.data(),
authenticatedBegin,
authenticatedSize);
if (!VerifyBufsEqual(checkDigest.data(), digest, checkDigest.size()))
{
return false;
}
CBC_Mode<AES>::Decryption decryptor(key.begin(), key.size(), iv.begin());
decryptor.ProcessData(output, payload, payloadSize);
return ValidatePkcs7Padding(output, payloadSize, plainTextLength);
}
}
unsigned int AESLayer::Encrypt(
RandomNumberGenerator& rng,
ConstByteArrayParameter const& passphrase,
byte* output,
const std::string& plaintext,
const EncryptionOptions& options)
{
const unsigned int paddingLength = AES::BLOCKSIZE - (plaintext.size() % AES::BLOCKSIZE);
const unsigned int paddedSize = static_cast<unsigned int>(plaintext.size()) + paddingLength;
std::vector<byte> paddedPlainText(paddedSize, 0);
if (!plaintext.empty())
{
std::memcpy(paddedPlainText.data(), plaintext.data(), plaintext.size());
}
std::fill_n(paddedPlainText.data() + plaintext.size(), paddingLength, static_cast<byte>(paddingLength));
byte* authenticatedBegin = nullptr;
size_t authenticatedSize = 0;
byte* salt = nullptr;
byte* payload = nullptr;
byte* ivSeed = nullptr;
byte* digest = nullptr;
// Keep default scrypt output in legacy format for backward compatibility.
if (options.m_kdfMode == KdfMode::Scrypt)
{
salt = output;
payload = salt + AESLayer::SALT_SIZE;
ivSeed = payload + paddedSize;
digest = ivSeed + AESLayer::IV_SEED_SIZE;
authenticatedBegin = payload;
authenticatedSize = static_cast<size_t>(digest - payload);
}
else
{
// New payload format:
// [magic "LN2\x02"][kdf_mode][salt][ciphertext][iv_seed][digest]
byte* magic = output;
std::copy(kFormatMagic.begin(), kFormatMagic.end(), magic);
byte* modeByte = magic + kFormatMagic.size();
*modeByte = static_cast<byte>(options.m_kdfMode);
salt = modeByte + 1;
payload = salt + AESLayer::SALT_SIZE;
ivSeed = payload + paddedSize;
digest = ivSeed + AESLayer::IV_SEED_SIZE;
authenticatedBegin = output;
authenticatedSize = static_cast<size_t>(digest - output);
}
rng.GenerateBlock(salt, AESLayer::SALT_SIZE);
rng.GenerateBlock(ivSeed, AESLayer::IV_SEED_SIZE);
SecByteBlock key(SHA256::DIGESTSIZE);
SecByteBlock iv(AESLayer::IV_SIZE);
DeriveKeyAndIv(options.m_kdfMode, passphrase, salt, ivSeed, key, iv);
CBC_Mode<AES>::Encryption encryptor(key.begin(), key.size(), iv.begin());
encryptor.ProcessData(payload, paddedPlainText.data(), paddedSize);
HMAC<SHA256>(key.begin(), key.size()).CalculateDigest(
digest,
authenticatedBegin,
authenticatedSize);
SecureWipeBuffer(paddedPlainText.data(), paddedPlainText.size());
return static_cast<unsigned int>((digest + HMAC<SHA256>::DIGESTSIZE) - output);
}
DecodingResult AESLayer::Decrypt(ConstByteArrayParameter const& passphrase, byte* output, ConstByteArrayParameter const& input)
{
if (input.size() < LEGACY_MINIMUM_CIPHERTEXT_LENGTH)
{
return DecodingResult();
}
const byte* begin = input.begin();
const byte* end = input.end();
// Preferred modern format with embedded KDF metadata.
if (input.size() >= MINIMUM_CIPHERTEXT_LENGTH && std::equal(kFormatMagic.begin(), kFormatMagic.end(), begin))
{
const byte modeValue = begin[kFormatMagic.size()];
if (IsKnownKdfMode(modeValue))
{
const byte* salt = begin + FORMAT_HEADER_SIZE;
const byte* payload = salt + AESLayer::SALT_SIZE;
const byte* digest = end - HMAC<SHA256>::DIGESTSIZE;
const byte* ivSeed = digest - AESLayer::IV_SEED_SIZE;
if (ivSeed > payload)
{
size_t plainTextLength = 0;
if (VerifyAndDecrypt(
ToKdfMode(modeValue),
passphrase,
begin,
static_cast<size_t>(digest - begin),
payload,
static_cast<size_t>(ivSeed - payload),
salt,
ivSeed,
digest,
output,
plainTextLength))
{
return DecodingResult(plainTextLength);
}
}
}
}
// Legacy format fallback (without format header) for backward compatibility.
const byte* salt = begin;
const byte* payload = salt + AESLayer::SALT_SIZE;
const byte* digest = end - HMAC<SHA256>::DIGESTSIZE;
const byte* ivSeed = digest - AESLayer::IV_SEED_SIZE;
if (ivSeed <= payload)
{
return DecodingResult();
}
size_t plainTextLength = 0;
if (VerifyAndDecrypt(
KdfMode::Scrypt,
passphrase,
payload,
static_cast<size_t>(digest - payload),
payload,
static_cast<size_t>(ivSeed - payload),
salt,
ivSeed,
digest,
output,
plainTextLength))
{
return DecodingResult(plainTextLength);
}
if (VerifyAndDecrypt(
KdfMode::Pbkdf2Sha256,
passphrase,
payload,
static_cast<size_t>(digest - payload),
payload,
static_cast<size_t>(ivSeed - payload),
salt,
ivSeed,
digest,
output,
plainTextLength))
{
return DecodingResult(plainTextLength);
}
return DecodingResult();
}
NAMESPACE_END