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
|
#include "Cryptor.h"
bool Cryptor::haveKey = false;
unsigned char Cryptor::key[CryptoPP::AES::DEFAULT_KEYLENGTH];
void Cryptor::encryptAndSave(std::string remoteHost, std::string port, std::string directory, std::string payload) {
// Key
if (!haveKey)
assembleKey(true);
// IV
unsigned char iv[CryptoPP::AES::BLOCKSIZE];
CryptoPP::AutoSeededRandomPool randl;
randl.GenerateBlock(iv, sizeof(iv));
// Encrypt
std::string cipher;
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption enc(key, sizeof(key), iv);
CryptoPP::StringSource(payload, true, new CryptoPP::StreamTransformationFilter(enc, new CryptoPP::StringSink(cipher)));
// Save Data
std::string encIV, encCipher;
CryptoPP::StringSource(iv, sizeof(iv), true, new CryptoPP::HexEncoder(new CryptoPP::StringSink(encIV)));
CryptoPP::StringSource(cipher, true, new CryptoPP::HexEncoder(new CryptoPP::StringSink(encCipher)));
if (remoteHost == "") {
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();
}
else {
Socket s;
std::string err = "";
s.conn(remoteHost, port);
s.sendline("store");
s.sendline(directory);
s.sendline(encIV);
s.sendline(encCipher);
err = s.readline();
s.clo();
if (err != "OK") throw 1;
}
}
std::string Cryptor::loadAndDecrypt(std::string remoteHost, std::string port, std::string directory) {
// Load Data
std::string encIV, encCipher, ivstr, cipher;
if (remoteHost == "") {
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();
}
else {
Socket s;
std::string err = "";
s.conn(remoteHost, port);
s.sendline("fetch");
s.sendline(directory);
err = s.readline();
if (err != "OK") {
s.clo();
throw 1;
}
encIV = s.readline();
encCipher = s.readline();
s.clo();
}
CryptoPP::StringSource(encIV, true, new CryptoPP::HexDecoder(new CryptoPP::StringSink(ivstr)));
CryptoPP::StringSource(encCipher, true, new CryptoPP::HexDecoder(new CryptoPP::StringSink(cipher)));
// Key
if (!haveKey)
assembleKey();
// IV
unsigned char iv[CryptoPP::AES::BLOCKSIZE];
memcpy(iv, ivstr.c_str(), ivstr.size());
// Decrypt
std::string payload;
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption dec(key, sizeof(key), iv);
CryptoPP::StringSource(cipher, true, new CryptoPP::StreamTransformationFilter(dec, new CryptoPP::StringSink(payload)));
return payload;
}
std::string Cryptor::createRandomPassword(PasswordSpec spec) {
CryptoPP::AutoSeededRandomPool randl;
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 r[1];
char c;
randl.GenerateBlock(r, sizeof(r));
c = validChars[r[0] % validChars.size()];
password += c;
}
return password;
}
void Cryptor::rekey() {
haveKey = false;
}
void Cryptor::sha256(std::string str) {
CryptoPP::SHA256 hash;
std::string rtr;
CryptoPP::StringSource(str, true, new CryptoPP::HashFilter(hash, new CryptoPP::StringSink(rtr), false, CryptoPP::AES::DEFAULT_KEYLENGTH));
memcpy(key, rtr.c_str(), rtr.size());
haveKey = true;
}
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;
}
|