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
|
#include "Cryptor.h"
bool Cryptor::haveKey = false;
unsigned char Cryptor::key[AES_BLOCK_LENGTH];
static void toHex(char *output, const void *input, size_t size) {
const unsigned char *_input = (const unsigned char *)input;
for (size_t i = 0; i < size; i++) {
sprintf(output+(i*2), "%02hhX", _input[i]);
}
}
static void fromHex(void *output, const char *input) {
unsigned char *_output = (unsigned char *)output;
size_t size = strlen(input);
if (size & 1) {
throw 1;
} else {
size /= 2;
}
for (size_t i = 0; i < size; i++) {
if (sscanf(input+(i*2), "%02hhx", &_output[i]) != 1) {
throw 1;
}
}
}
void Cryptor::encryptAndSave(std::string remoteHost, std::string port, std::string directory, std::string payload) {
// Key
if (!haveKey) {
assembleKey(true);
}
// IV
unsigned char iv[AES_BLOCK_LENGTH];
generateRandom(iv, sizeof(iv));
// Encrypt
size_t ciphertextLen = 0;
unsigned char *ciphertext = new unsigned char[payload.size() + 64];
const mbedtls_cipher_info_t *cipherInfo = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CBC);
mbedtls_cipher_context_t cipher;
mbedtls_cipher_init(&cipher);
mbedtls_cipher_setup(&cipher, cipherInfo);
mbedtls_cipher_setkey(&cipher, key, sizeof(key)*8, MBEDTLS_ENCRYPT);
mbedtls_cipher_set_padding_mode(&cipher, MBEDTLS_PADDING_PKCS7);
mbedtls_cipher_crypt(&cipher, iv, sizeof(iv),
(const unsigned char *)payload.c_str(), payload.size(),
ciphertext, &ciphertextLen);
mbedtls_cipher_free(&cipher);
// Save Data
char encIV[(AES_BLOCK_LENGTH * 2) + 1];
char *encCipher = new char[(ciphertextLen * 2) + 1];
toHex(encIV, iv, sizeof(iv));
toHex(encCipher, ciphertext, ciphertextLen);
std::string _encIV(encIV);
std::string _encCipher(encCipher);
delete[] encCipher;
delete[] ciphertext;
if (directory[directory.size() - 1] != '/') {
directory += "/";
}
directory += KEYCHAIN_FILE;
std::ofstream f(directory.c_str());
f << _encIV << std::endl;
f << _encCipher << std::endl;
f.close();
}
std::string Cryptor::loadAndDecrypt(std::string remoteHost, std::string port, std::string directory) {
// Load Data
std::string encIV, encCipher;
if (directory[directory.size() - 1] != '/') {
directory += "/";
}
directory += KEYCHAIN_FILE;
std::ifstream f(directory.c_str());
if (!f.good()) {
f.close();
throw 1;
}
f >> encIV;
f >> encCipher;
f.close();
// Decode data
unsigned char *ciphertext = new unsigned char[encCipher.size() / 2];
unsigned char iv[AES_BLOCK_LENGTH];
fromHex(ciphertext, encCipher.c_str());
fromHex(iv, encIV.c_str());
// Key
if (!haveKey) {
assembleKey();
}
// Decrypt
size_t plaintextLen = 0;
char *plaintext = new char[(encCipher.size() / 2) + 64];
const mbedtls_cipher_info_t *cipherInfo = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CBC);
mbedtls_cipher_context_t cipher;
mbedtls_cipher_init(&cipher);
mbedtls_cipher_setup(&cipher, cipherInfo);
mbedtls_cipher_setkey(&cipher, key, sizeof(key)*8, MBEDTLS_DECRYPT);
mbedtls_cipher_set_padding_mode(&cipher, MBEDTLS_PADDING_PKCS7);
mbedtls_cipher_crypt(&cipher, iv, sizeof(iv),
ciphertext, (encCipher.size() / 2),
(unsigned char *)plaintext, &plaintextLen);
mbedtls_cipher_free(&cipher);
std::string payload(plaintext, plaintextLen);
delete[] plaintext;
delete[] ciphertext;
return payload;
}
std::string Cryptor::createRandomPassword(PasswordSpec spec) {
std::string password;
std::vector<char> validChars;
// Always allow lower-case alphabetic characters
for (int i = 0; i < 26; i++)
validChars.push_back('a' + i);
// Allow special chars?
if (!spec.ns) {
validChars.push_back('`');
validChars.push_back('~');
validChars.push_back('!');
validChars.push_back('@');
validChars.push_back('#');
validChars.push_back('$');
validChars.push_back('%');
validChars.push_back('^');
validChars.push_back('&');
validChars.push_back('*');
validChars.push_back('(');
validChars.push_back(')');
validChars.push_back('-');
validChars.push_back('_');
validChars.push_back('=');
validChars.push_back('+');
validChars.push_back('[');
validChars.push_back('{');
validChars.push_back(']');
validChars.push_back('}');
validChars.push_back('\\');
validChars.push_back('|');
validChars.push_back(';');
validChars.push_back(':');
validChars.push_back(',');
validChars.push_back('<');
validChars.push_back('.');
validChars.push_back('>');
validChars.push_back('/');
validChars.push_back('?');
}
// Allow capital letters?
if (!spec.nc) {
for (int i = 0; i < 26; i++)
validChars.push_back('A' + i);
}
// Allow numbers?
if (!spec.nn) {
for (int i = 0; i < 10; i++)
validChars.push_back('0' + i);
}
// Build string
for (int i = 0; i < spec.ml; i++) {
unsigned char c;
generateRandom(&c, sizeof(c));
password += validChars[c % validChars.size()];
}
return password;
}
void Cryptor::rekey() {
haveKey = false;
}
void Cryptor::sha256(std::string str) {
unsigned char hashbuf[32];
mbedtls_sha256((const unsigned char *)str.c_str(), str.size(), hashbuf, 0);
memcpy(key, hashbuf, sizeof(key));
haveKey = true;
}
void Cryptor::generateRandom(void *output, size_t size) {
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context random;
mbedtls_entropy_init(&entropy);
mbedtls_ctr_drbg_init(&random);
mbedtls_ctr_drbg_seed(&random, mbedtls_entropy_func, &entropy, NULL, 0);
mbedtls_ctr_drbg_random(&random, (unsigned char *)output, size);
mbedtls_ctr_drbg_free(&random);
mbedtls_entropy_free(&entropy);
}
std::string Cryptor::readPassword(bool confirm) {
std::string password;
#ifdef WIN32
HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(hstdin, &mode);
SetConsoleMode(hstdin, mode & ~ENABLE_ECHO_INPUT);
password = promptPassword(confirm);
SetConsoleMode(hstdin, mode);
#else
termios oldt;
tcgetattr(STDIN_FILENO, &oldt);
termios newt = oldt;
newt.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
password = promptPassword(confirm);
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
#endif // WIN32
return password;
}
std::string Cryptor::readPassword() {
return readPassword(false);
}
void Cryptor::assembleKey(bool confirm) {
sha256(readPassword(confirm));
}
void Cryptor::assembleKey() {
assembleKey(false);
}
std::string Cryptor::promptPassword(bool confirm) {
std::string password;
while (true) {
std::cout << PASSWORD_PROMPT;
std::cin >> password;
std::cout << std::endl;
if (confirm) {
std::string conf;
std::cout << PASSWORD_CONF;
std::cin >> conf;
std::cout << std::endl;
if (password == conf)
break;
else
std::cout << PASSWORD_ERROR << std::endl;
}
else
break;
}
return password;
}
|