-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
276 lines (189 loc) · 5.99 KB
/
main.cpp
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
#include <string>
#include <iostream>
#include <fstream>
#include <openssl/rand.h>
#include <filesystem>
#include "./types.h"
// @todo configure everything with defines before includes in a config.h
#include "./kdf.hpp"
#include "./integrity.hpp"
#include "./pcbc.hpp"
#define ENCRYPTED_EXT std::string(".mars")
void encrypt(const std::string filename, const std::string password)
{
#pragma region Create brotli compressed archive
std::string tar_path = filename + ".tar.br";
std::string command = "tar -cO " + filename + " | brotli --best -f - -o " + tar_path;
system(command.c_str());
#pragma endregion
#pragma region Read tar.br
std::ifstream fs(tar_path, std::ios::binary);
// extra parens are required????
std::string file((std::istreambuf_iterator<char>(fs)),
(std::istreambuf_iterator<char>()));
fs.close();
size_t file_size = file.length();
#pragma endregion
#pragma region Overwrite file and delete it
std::ofstream of(tar_path, std::ios::binary);
for (size_t f = 0; f < file_size; f += 1)
{
of.put(0);
}
of.close();
// remove file
std::remove(tar_path.c_str());
#pragma endregion
#pragma region File digest
byte digest[DIGEST_SIZE];
hash((byte *)file.data(), file_size, digest);
// prepend digest to file
file = std::string((char *)digest, DIGEST_SIZE) + file;
file_size = file.length();
// printf("Digest = %s\n", OPENSSL_buf2hexstr(digest_buffer, DIGEST_SIZE));
#pragma endregion
#pragma region Key derivation
byte salt[SALT_SIZE];
RAND_bytes(salt, SALT_SIZE);
// printf("Password (%zu) = %s\n", password.length(), password.c_str());
// printf("Salt = %s\n", OPENSSL_buf2hexstr(salt, SALT_SIZE));
byte key[KEY_SIZE];
kdf((byte *)password.c_str(), password.length(), salt, SALT_SIZE, key);
// printf("Key = %s\n", OPENSSL_buf2hexstr(key, KEY_SIZE));
#pragma endregion
#pragma region File encryption
byte iv[IV_SIZE];
RAND_bytes(iv, IV_SIZE);
byte *ciphertext;
size_t ciphertext_size;
encrypt_aes256_pcbc((byte *)file.data(), file_size, key, iv, ciphertext, ciphertext_size);
OPENSSL_cleanse(key, KEY_SIZE);
OPENSSL_cleanse(file.data(), file_size);
#pragma endregion
#pragma region Writing to disk
std::ofstream encrypted(filename + ".mars", std::ios::binary | std::ios::trunc);
// << can't be used
encrypted.write((char *)salt, SALT_SIZE);
// encrypted.write((char *)digest, DIGEST_SIZE);
encrypted.write((char *)iv, IV_SIZE);
encrypted.write((char *)ciphertext, ciphertext_size);
// vs code doesn't immediately update files in side bar, so it may appear ofstream didn't create the file
encrypted.close();
printf(
"\e[33m\e[3m"
"Final size = %zu bytes"
"\e[0m\n"
"\e[92m"
"Data encrypted successfully!\n",
SALT_SIZE + IV_SIZE + ciphertext_size);
#pragma endregion
}
void decrypt(const std::string filename, const std::string password)
{
#pragma region Read data
byte salt[SALT_SIZE];
byte iv[IV_SIZE];
std::ifstream encrypted(filename, std::ios::binary);
encrypted.read((char *)salt, SALT_SIZE);
// encrypted.read((char *)digest, DIGEST_SIZE);
encrypted.read((char *)iv, IV_SIZE);
std::string ciphertext((std::istreambuf_iterator<char>(encrypted)),
(std::istreambuf_iterator<char>()));
size_t ciphertext_size = ciphertext.length();
#pragma endregion
#pragma region Key derivation
byte key[KEY_SIZE];
kdf((byte *)password.c_str(), password.length(), salt, SALT_SIZE, key);
#pragma endregion
#pragma region Decryption
byte *plaintext;
size_t plaintext_size;
decrypt_aes256_pcbc((byte *)ciphertext.data(), ciphertext_size, key, iv, plaintext, plaintext_size);
OPENSSL_cleanse(key, KEY_SIZE);
// extract digest
byte *digest = plaintext;
plaintext += DIGEST_SIZE;
plaintext_size -= DIGEST_SIZE;
#pragma endregion
#pragma Integrity check
if (!verify(plaintext, plaintext_size, digest))
{
printf("\e[91m"
"Integrity is broken or password is incorrect!\n");
abort();
}
#pragma endregion
#pragma region Decompress and untar
std::string path = filename.substr(0, filename.length() - ENCRYPTED_EXT.length()) + ".tar.br";
std::ofstream fs;
fs.open(path, std::ios::binary);
fs.write((char *)plaintext, plaintext_size);
fs.close();
OPENSSL_cleanse(plaintext, plaintext_size);
std::string command = "brotli -d " + path + " --stdout | tar -x";
system(command.c_str());
// overwrite with 0s
fs.open(path, std::ios::binary);
for (size_t f = 0; f < plaintext_size; f += 1)
{
fs.put(0);
}
fs.close();
std::remove((path).c_str());
printf("\e[92m"
"Data decrypted successfully!\n");
#pragma endregion
}
int main(int argc, char **argv)
{
if (argc != 2)
{
// 4 - underline
printf(
"\e[43m\e[3m\e[1m"
"Usage:"
"\e[0m\n\n"
" \e[3m"
"Encrypt:"
"\e[0m\n"
" \e[41m"
"mars"
"\e[0m"
" "
"\e[33m"
"path/to/folder"
"\e[0m\n"
"\n"
" \e[3m"
"Decrypt:"
"\e[0m\n"
" \e[41m"
"mars"
"\e[49m \e[36m"
"encrypted.mars\n"
"\e[39m\e[3m");
return 0;
}
std::string filename(argv[1]);
if (!std::filesystem::exists(filename))
{
print_error(("Path \"" + filename + "\" does not exist!").c_str());
abort();
}
#pragma region Password input
printf("\e[33m\e[3m\e[1m"
"Input password:\n"
"\e[0m");
std::string password;
getline(std::cin, password);
system("clear");
#pragma endregion
if (filename.ends_with(ENCRYPTED_EXT))
{
decrypt(filename, password);
}
else
{
encrypt(filename, password);
}
}