forked from thenickdude/PlanC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.cpp
More file actions
288 lines (215 loc) · 9.3 KB
/
crypto.cpp
File metadata and controls
288 lines (215 loc) · 9.3 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
#include <cassert>
#include "crypto.h"
#include "common.h"
#include "cryptopp/blowfish.h"
#include "cryptopp/aes.h"
#include "cryptopp/modes.h"
#include "cryptopp/sha.h"
#include "cryptopp/pwdbased.h"
static const CryptoPP::byte BLOWFISH_IV[CryptoPP::Blowfish::BLOCKSIZE] = {12, 34, 56, 78, 90, 87, 65, 43};
static const CryptoPP::byte AES_IV[CryptoPP::AES::BLOCKSIZE] = {121, 92, 86, 51, 153, 89, 163, 254, 47, 51, 47, 174, 253, 149, 129, 140};
const Code42Cipher* code42Ciphers[] = {
new Code42NullCipher(),
new Code42Blowfish128(),
new Code42Blowfish448(),
new Code42AES128(),
new Code42AES256(),
new Code42AES256RandomIV()
};
static Code42Blowfish448 blowfish;
static uint32_t decodeUInt32BE(const char *buffer) {
uint8_t *bytes = (uint8_t *) buffer;
return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
}
/**
* Check if the given account password can be used to unlock the given ArchiveSecureDataKey/SecureDataKey. The key must be deobfuscated
* and base64-decoded first.
*/
bool passwordUnlocksSecureDataKey(const std::string &decoded, const std::string &password) {
/* ArchiveSecureDataKey has this format:
*
* Start Content
* 0 Length of key bytes as a big-endian 4 byte integer
* 4 Key bytes
* 4 + len Password hash, being:
* Hash - SHA-1 of salt||password, then SHA-1 applied to the resulting hash for 4242 iterations, finally base64 encoded
* : - colon separator
* Salt - 8 byte random salt, base64 encoded
*/
const int SHA1_ITERATIONS = 4242;
const char *keyAndPasswordHash = decoded.c_str();
int keyLen = decodeUInt32BE(keyAndPasswordHash);
int hashAndSaltLen = decoded.length() - keyLen - 4;
const char *key = keyAndPasswordHash + 4;
const char *hashAndSalt = key + keyLen;
const char *separator = strchr(hashAndSalt, ':');
assert(separator != NULL);
const char *hash = hashAndSalt;
const char *salt = separator + 1;
int hashLen = salt - hash - 1;
int saltLen = (hashAndSalt + hashAndSaltLen) - salt;
std::string hashDecoded, saltDecoded;
saltDecoded = base64Decode(salt, saltLen);
hashDecoded = base64Decode(hash, hashLen);
CryptoPP::SHA1 hasher;
CryptoPP::byte currentHash[CryptoPP::SHA1::DIGESTSIZE];
hasher.Update((const CryptoPP::byte*) saltDecoded.data(), saltDecoded.length());
hasher.Update((const CryptoPP::byte*) password.data(), password.length());
hasher.Final(currentHash);
for (int i = 0; i < SHA1_ITERATIONS; i++) {
hasher.Update(currentHash, sizeof(currentHash));
hasher.Final(currentHash);
}
return memcmp(hashDecoded.data(), currentHash, sizeof(currentHash)) == 0;
}
std::string decryptSecureDataKey(const std::string &decoded, const std::string & password) {
const char *keyAndPasswordHash = decoded.c_str();
// Slice the encrypted key out of the start of the key/password hash pair:
int encryptedKeyLen = decodeUInt32BE(keyAndPasswordHash);
std::string encryptedKey = std::string(keyAndPasswordHash + 4, encryptedKeyLen);
// Then decrypt it with the user's account password as the key
return blowfish.decrypt(encryptedKey, password);
}
/**
* Hash a password using Code42 SHA-1 scheme, the resulting string containing:
*
* Hash - SHA-1 of salt||password, then SHA-1 applied to the resulting hash for 4242 iterations, finally base64 encoded
* : - colon separator
* Salt - 8 byte random salt, base64 encoded
*
* Test vector: passphrase = hello, salt = world, output = Dl/cd5yqjjk5vkd29/ZGF/GVDu4=:d29ybGQ=
*
* @param passphrase
* @param salt
* @param iterations
* @return
*/
std::string hashPassphraseC42SHA1(const std::string &passphrase, const std::string &salt, int iterations = 4242) {
CryptoPP::SHA1 hasher;
CryptoPP::byte currentHash[CryptoPP::SHA1::DIGESTSIZE];
hasher.Update((const CryptoPP::byte*) salt.data(), salt.length());
hasher.Update((const CryptoPP::byte*) passphrase.data(), passphrase.length());
hasher.Final(currentHash);
for (int i = 0; i < iterations; i++) {
hasher.Update(currentHash, sizeof(currentHash));
hasher.Final(currentHash);
}
return base64Encode((char*)currentHash, sizeof(currentHash)) + ":" + base64Encode(salt);
}
/**
* Test vector: userID=1234, passphrase=hello, output=783630546C5438426B3D3A4D54497A4E413D3D5246355A45456D4679447A672F546477576643366C6A6D663056513D3A4D54497A4E413D3D
* @param userID
* @param passphrase
* @return
*/
std::string deriveCustomArchiveKeyV2(const std::string &userID, const std::string &passphrase) {
const int OUTPUT_LENGTH = 56;
const int HASH_ITERATIONS = 50000;
std::string passphraseReverse(passphrase.rbegin(), passphrase.rend());
std::string result = hashPassphraseC42SHA1(passphrase, userID, HASH_ITERATIONS) + hashPassphraseC42SHA1(passphraseReverse, userID, HASH_ITERATIONS);
// Extend the hash with null bytes if needed
if (result.length() < OUTPUT_LENGTH) {
result += std::string(OUTPUT_LENGTH - result.length(), (char) 0x00);
}
// Keep the trailing bytes
if (result.length() > OUTPUT_LENGTH) {
result = result.substr(result.length() - OUTPUT_LENGTH);
}
return result;
}
/**
* Decrypt a value using AES-256 CBC, where the first block is the message IV, and verify the message padding is correct.
*/
std::string Code42AES256RandomIV::decrypt(const std::string & cipherText, const std::string &key) const {
// We expect the encrypted value to be padded to a full block size (padding)
if (cipherText.length() % CryptoPP::AES::BLOCKSIZE != 0) {
throw BadPaddingException();
}
// The first block of the input is the random IV:
const CryptoPP::byte *iv = (const CryptoPP::byte *) cipherText.data();
const CryptoPP::byte *encrypted = (const CryptoPP::byte *) cipherText.data() + CryptoPP::AES::BLOCKSIZE;
int encryptedSize = cipherText.length() - CryptoPP::AES::BLOCKSIZE;
uint8_t *buffer = new uint8_t[encryptedSize];
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption decryptor((const CryptoPP::byte *)key.data(), 256 / 8, iv);
decryptor.ProcessData(buffer, encrypted, encryptedSize);
// Verify padding is correct after decryption:
uint8_t padByte = buffer[encryptedSize - 1];
if (padByte <= 0 || padByte > CryptoPP::AES::BLOCKSIZE) {
throw BadPaddingException();
}
for (int i = 1; i < padByte; i++) {
if (buffer[encryptedSize - 1 - i] != padByte) {
throw BadPaddingException();
}
}
int unpaddedLength = encryptedSize - padByte;
std::string result((const char *) buffer, unpaddedLength);
delete[] buffer;
return result;
}
std::string Code42AESStaticIV::decrypt(const std::string & cipherText, const std::string &key) const {
// We expect the encrypted value to be padded to a full block size (padding)
if (cipherText.length() % CryptoPP::AES::BLOCKSIZE != 0) {
throw BadPaddingException();
}
const CryptoPP::byte *encrypted = (const CryptoPP::byte *) cipherText.data();
int encryptedSize = cipherText.length();
uint8_t *buffer = new uint8_t[encryptedSize];
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption decryptor((const CryptoPP::byte *)key.data(), 256 / 8, AES_IV);
decryptor.ProcessData(buffer, encrypted, encryptedSize);
// Verify padding is correct after decryption:
uint8_t padByte = buffer[encryptedSize - 1];
if (padByte <= 0 || padByte > CryptoPP::AES::BLOCKSIZE) {
throw BadPaddingException();
}
for (int i = 1; i < padByte; i++) {
if (buffer[encryptedSize - 1 - i] != padByte) {
throw BadPaddingException();
}
}
int unpaddedLength = encryptedSize - padByte;
std::string result((const char *) buffer, unpaddedLength);
delete[] buffer;
return result;
}
std::string Code42Blowfish448::decrypt(const std::string & cipherText, const std::string & key) const {
// We expect the encrypted value to be padded to a full block size (padding)
if (cipherText.length() % CryptoPP::Blowfish::BLOCKSIZE != 0) {
throw BadPaddingException();
}
int encryptedSize = cipherText.length();
CryptoPP::CBC_Mode<CryptoPP::Blowfish>::Decryption decryptor;
CryptoPP::byte *buffer = new CryptoPP::byte[encryptedSize];
// Trim overlong key
std::string newKey(key);
if (newKey.length() > CryptoPP::Blowfish::MAX_KEYLENGTH) {
newKey.resize(CryptoPP::Blowfish::MAX_KEYLENGTH);
}
decryptor.SetKeyWithIV((const CryptoPP::byte *)newKey.data(), newKey.length(), BLOWFISH_IV);
decryptor.ProcessData(buffer, (const CryptoPP::byte *) cipherText.data(), cipherText.length());
// Verify padding is correct after decryption:
uint8_t padByte = buffer[encryptedSize - 1];
if (padByte <= 0 || padByte > CryptoPP::Blowfish::BLOCKSIZE) {
throw BadPaddingException();
}
for (int i = 1; i < padByte; i++) {
if (buffer[encryptedSize - 1 - i] != padByte) {
throw BadPaddingException();
}
}
int unpaddedLength = encryptedSize - padByte;
std::string result((const char *) buffer, unpaddedLength);
delete[] buffer;
return result;
}
std::string generateSmallBusinessKeyV2(const std::string &passphrase, const std::string &salt) {
CryptoPP::PKCS5_PBKDF2_HMAC<CryptoPP::SHA512> generator;
CryptoPP::byte derived[32];
generator.DeriveKey(
derived, sizeof(derived), 0,
(const CryptoPP::byte*) passphrase.data(), passphrase.length(),
(const CryptoPP::byte*) salt.data(), salt.length(),
10000
);
return std::string((const char *)derived, sizeof(derived));
}