This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
suncrypt.cpp
329 lines (261 loc) · 9.83 KB
/
suncrypt.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
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
322
323
324
325
326
327
328
329
//
// main.cpp
// suncrypt
//
// Created by Jason Ho on 8/30/17.
// Copyright © 2017 JasonHo. All rights reserved.
//
#include <iostream>
#include <gcrypt.h>
#include <string>
#include <iomanip>
#include <fstream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;
// data structure to contain data to be transmitted
struct container {
unsigned long data_len;
unsigned char buffer[5000000];
};
int transfer_mode(const char file_name[], const char host_addr[]);
int local_mode(const char file_name[]);
int key_gen(unsigned char key[]);
int encrypt(unsigned char key[], const unsigned char *plaintext, unsigned char **ciphertext, unsigned char **mac, long cipher_len);
int read_file(const char file_name[], unsigned char **plaintext, long *cipher_len);
int write_file(const char file_name[], unsigned char *ciphertext, unsigned char *mac, long cipher_len);
int parse_addr(const char addr[]);
int main(int argc, const char * argv[]) {
gcry_check_version(NULL);
gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0);
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
int error_code = 0;
if(argv[2][1] == 'd')
error_code = transfer_mode(argv[1], argv[3]);
else if(argv[2][1] == 'l')
error_code = local_mode(argv[1]);
else {
cout << "No such commend." << endl;
return 1;
}
return error_code;
}
// -d option
int transfer_mode(const char file_name[], const char host_addr[]) {
int error_code = 0;
// file handling and encryption
unsigned char key[gcry_cipher_get_algo_keylen(GCRY_CIPHER_AES128)];
error_code = key_gen(key);
if(error_code) {
cout << "Key generation failed." << endl;
return error_code;
}
unsigned char *plaintext;
long cipher_len;
error_code = read_file(file_name, &plaintext, &cipher_len);
if(error_code)
return 2;
unsigned char *ciphertext = new unsigned char[cipher_len];
unsigned char *mac = new unsigned char[gcry_md_get_algo_dlen(GCRY_MD_SHA512)];
error_code = encrypt(key, plaintext, &ciphertext, &mac, cipher_len);
if(error_code) {
cout << "Encryption failed." << endl;
delete[] plaintext;
delete[] ciphertext;
delete[] mac;
return error_code;
}
// setup socket connection
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
cout << "Failed to create socket." << endl;
return 3;
}
struct sockaddr_in serv_addr;
memset(&serv_addr, '0', sizeof(sockaddr_in));
serv_addr.sin_family = AF_INET;
string addr(host_addr);
unsigned long sep = addr.find(':');
serv_addr.sin_port = htons(atoi(addr.substr(sep + 1, addr.length() - sep - 1).c_str()));
error_code = inet_pton(AF_INET, addr.substr(0, sep).c_str(), &serv_addr.sin_addr);
if(error_code != 1) {
cout << "Invalid IP address." << endl;
return 3;
}
error_code = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(sockaddr_in));
if(error_code < 0) {
cout << "Failed to connect to server." << endl;
return 3;
}
// sending data
struct container package;
unsigned char *buf_ptr = package.buffer;
package.data_len = cipher_len + gcry_md_get_algo_dlen(GCRY_MD_SHA512);
for(int i = 0; i < cipher_len; i++)
*(buf_ptr++) = ciphertext[i];
for(int i = 0; i < gcry_md_get_algo_dlen(GCRY_MD_SHA512); i++)
*(buf_ptr++) = mac[i];
// keep sending till all data is transmitted
unsigned int out_bytes = 0;
unsigned char *ptr = (unsigned char *)&package;
while(out_bytes < sizeof(unsigned long) + sizeof(unsigned char) * package.data_len) {
long bytes_sent = send(sockfd, ptr + out_bytes, sizeof(unsigned long) + sizeof(unsigned char) * package.data_len - out_bytes, 0);
if(bytes_sent < 0) {
cout << "Unable to send file." << endl;
close(sockfd);
return 3;
}
else {
out_bytes += bytes_sent;
cout << bytes_sent << " bytes sent." << endl;
}
}
cout << "Successfully sent file to " << host_addr << endl;
delete[] plaintext;
delete[] ciphertext;
delete[] mac;
return 0;
}
// -l option
int local_mode(const char file_name[]) {
int error_code = 0;
unsigned char key[gcry_cipher_get_algo_keylen(GCRY_CIPHER_AES128)];
error_code = key_gen(key);
if(error_code) {
cout << "Key generation failed." << endl;
return error_code;
}
unsigned char *plaintext;
long cipher_len;
error_code = read_file(file_name, &plaintext, &cipher_len);
if(error_code)
return 2;
unsigned char *ciphertext = new unsigned char[cipher_len];
unsigned char *mac = new unsigned char[gcry_md_get_algo_dlen(GCRY_MD_SHA512)];
error_code = encrypt(key, plaintext, &ciphertext, &mac, cipher_len);
if(error_code) {
cout << "Encryption failed." << endl;
delete[] plaintext;
delete[] ciphertext;
delete[] mac;
return error_code;
}
error_code = write_file(file_name, ciphertext, mac, cipher_len);
if(error_code)
return error_code;
delete[] plaintext;
delete[] ciphertext;
delete[] mac;
return 0;
}
int key_gen(unsigned char key[]) {
string password;
cout << "Password: " << flush;
cin >> password;
gpg_error_t error_code = gcry_kdf_derive(password.c_str(), password.size(), GCRY_KDF_PBKDF2, GCRY_MD_SHA512, "NaCl", 4, 4096, gcry_cipher_get_algo_keylen(GCRY_CIPHER_AES128), key);
if(error_code)
return error_code;
// print key in hexadecimal format
cout << "Key: " << hex << uppercase << flush;
for(int i = 0; i < gcry_cipher_get_algo_keylen(GCRY_CIPHER_AES128); i++)
cout << setw(2) << setfill('0') << short(key[i]) << ' ';
cout << dec << endl;
return 0;
}
int encrypt(unsigned char key[], const unsigned char *plaintext, unsigned char **ciphertext, unsigned char **mac, long cipher_len) {
gcry_error_t error_code;
// encryption
gcry_cipher_hd_t encrypt_handle;
error_code = gcry_cipher_open(&encrypt_handle, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
if(error_code) {
cout << "Failed to allocate encryption handle." << endl;
return error_code;
}
error_code = gcry_cipher_setkey(encrypt_handle, key, gcry_cipher_get_algo_keylen(GCRY_CIPHER_AES128));
if(error_code) {
cout << "Failed to set encryption key." << endl;
return error_code;
}
// make IV to be the same length as AES block size
unsigned long uint_count = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES128) / sizeof(unsigned int);
unsigned int *iv = new unsigned int[uint_count];
memset(iv, 0, gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES128));
iv[uint_count - 1] = 5844;
error_code = gcry_cipher_setiv(encrypt_handle, iv, gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES128));
if(error_code) {
cout << "Failed to set IV." << endl;
return error_code;
}
delete[] iv;
error_code = gcry_cipher_encrypt(encrypt_handle, *ciphertext, cipher_len, plaintext, cipher_len);
if(error_code) {
cout << "Failed to encrypt." << endl;
return error_code;
}
gcry_cipher_close(encrypt_handle);
// MAC
gcry_md_hd_t md_handle;
error_code = gcry_md_open(&md_handle, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC);
if(error_code) {
cout << "Failed to allocate md handle." << endl;
return error_code;
}
error_code = gcry_md_setkey(md_handle, key, gcry_cipher_get_algo_keylen(GCRY_CIPHER_AES128));
if(error_code) {
cout << "Failed to set mac key." << endl;
return error_code;
}
gcry_md_write(md_handle, *ciphertext, cipher_len);
unsigned char *temp = gcry_md_read(md_handle, GCRY_MD_SHA512);
for(int i = 0; i < gcry_md_get_algo_dlen(GCRY_MD_SHA512); i++)
(*mac)[i] = temp[i];
gcry_md_close(md_handle);
return 0;
}
// read file from disk
int read_file(const char file_name[], unsigned char **plaintext, long *cipher_len) {
ifstream ifs;
ifs.open(file_name, ios::binary);
if(!ifs.good()) {
cout << string(file_name) + " doesn't exist." << endl;
return 1;
}
// get plaintext length
ifs.seekg(0, ifs.end);
long plain_len = ifs.tellg();
ifs.seekg(0, ifs.beg);
// make cipher_len multiple of AES block size
*cipher_len = (plain_len / gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES128) + 1) * gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES128);
*plaintext = new unsigned char[*cipher_len];
for(int i = 0; i < plain_len; i++)
ifs >> noskipws >> (*plaintext)[i];
// PKCS7 padding
long pad = *cipher_len - plain_len;
for(long i = plain_len; i < *cipher_len; i++)
(*plaintext)[i] = pad;
ifs.close();
return 0;
}
// write file to disk when -l option is used
int write_file(const char file_name[], unsigned char *ciphertext, unsigned char *mac, long cipher_len) {
//check if output file already exists
ifstream ifs;
ifs.open((string(file_name) + ".uf").c_str(), ios::in);
if(ifs.good()) {
cout << string(file_name) + ".uf already exists!" << endl;
return 33;
}
ifs.close();
// write to file
ofstream ofs;
ofs.open((string(file_name) + ".uf").c_str(), ios::out);
for(int i = 0; i < cipher_len; i++)
ofs << noskipws << ciphertext[i];
for(int i = 0; i < gcry_md_get_algo_dlen(GCRY_MD_SHA512); i++)
ofs << noskipws << mac[i];
cout << "Successfully encrypted " << file_name << " to " << file_name << ".uf (" << ofs.tellp() << " bytes written)." << endl;
ofs.close();
return 0;
}