-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests_and_examples.cpp
335 lines (305 loc) · 10.9 KB
/
tests_and_examples.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
330
331
332
333
334
335
#include "RSAES.hpp"
#include <iostream> // showing results of tests
#include <random> // Fast/easy randomness for generating test words
std::random_device rd;
std::mt19937 mt(rd());
std::string word_bank[50] = {
"pleasant",
"foot",
"elfin",
"calendar",
"settle",
"size",
"trip",
"float",
"sand",
"good",
"stain",
"trite",
"colorful",
"street",
"dusty",
"range",
"blot",
"direction",
"cent",
"white",
"angry",
"sack",
"wave",
"weather",
"stitch",
"ritzy",
"scintill",
"deliver",
"synonymo",
"quicksan",
"cub",
"sofa",
"callous",
"disagree",
"dashing",
"daughter",
"jar",
"sniff",
"ear",
"powder",
"wait",
"shame",
"needy",
"dreary",
"x-ray",
"labored",
"can",
"incompet",
"pricey",
"jagged"
};
std::uniform_int_distribution<unsigned short> dist_50(0, 49);
std::string test_high_level(){
try{
std::cout << "Start encryption manager 1 and grab the RSA public key:" << std::endl;
RSAES::EncryptionManager Bob(2048);
std::string msg = Bob.getPublicKey();
std::cout << msg << std::endl << std::endl;
std::cout << "Start encryption manager 2, generate a random AES key, and send it back encrypted over RSA:" << std::endl;
RSAES::EncryptionManager Allice(msg);
msg = Allice.getKeyResponse();
std::cout << msg << std::endl << std::endl;
std::cout << "Register the AES key with manager 1. Now we can send a message:" << std::endl;
Bob.registerPass(msg);
std::string msg_s = word_bank[dist_50(mt)];
int words = dist_50(mt);
for(int i=0; i<words; ++i)
(msg_s+=' ')+=word_bank[dist_50(mt)];
msg = Bob.encrypt(msg_s);
std::cout << msg << std::endl << std::endl;
std::cout << "Now we can decrypt it using manager 2:" << std::endl;
msg = Allice.decrypt(msg);
std::cout << msg << std::endl << std::endl;
if(msg!=msg_s)
throw std::runtime_error("Messages aren't same");
std::cout << "Pack up both objects as if we're saving data for a later session" << std::endl << std::endl;
std::string Bob_pack = Bob.pack();
std::string Allice_pack = Allice.pack();
Bob.destroy();
Allice.destroy();
std::cout << "For reference, here's the packed strings:" << std::endl << Bob_pack << std::endl << std::endl;
std::cout << "Unpack the managers for further use" << std::endl << std::endl;
RSAES::EncryptionManager Bob2, Allice2;
Bob2.unpack(Bob_pack);
Allice2.unpack(Allice_pack);
std::cout << "Re-packed string:" << std::endl << Bob2.pack() << std::endl;
std::cout << "Now let's go the other way. Encrypt with manager 2:" << std::endl;
msg_s = word_bank[dist_50(mt)];
words = dist_50(mt);
for(int i=0; i<words; ++i)
(msg_s+=' ')+=word_bank[dist_50(mt)];
msg = Allice2.encrypt(msg_s);
std::cout << msg << std::endl << std::endl;
std::cout << "And decrypt with manager 1:" << std::endl;
msg = Bob2.decrypt(msg);
std::cout << msg << std::endl << std::endl;
if(msg!=msg_s)
throw std::runtime_error("Messages aren't same");
} catch (const std::runtime_error& error){
return error.what();
}
return "";
}
std::string test_low_level_RSA(){
try{
std::cout << "Start an RSAmanager and grab the public key:" << std::endl;
RSAES::RSA::RSAmanager Bob(1024);
std::string keyStr = RSAES::RSA::packKey(Bob.public_key);
std::cout << keyStr << std::endl << std::endl;
std::cout << "Encrypt a message using the public key:" << std::endl;
std::string msg_s = word_bank[dist_50(mt)];
int words = dist_50(mt)/2;
for(int i=0; i<words; ++i)
(msg_s+=' ')+=word_bank[dist_50(mt)];
std::pair<mpz_t,mpz_t> *recvKey;
RSAES::RSA::unpackKey(&recvKey, keyStr.c_str());
std::string msg = RSAES::RSA::encrypt(msg_s, recvKey);
mpz_clear(recvKey->first);
mpz_clear(recvKey->second);
delete recvKey;
std::cout << msg << std::endl << std::endl;
std::cout << "Decrypt the message using the private key:" << std::endl;
msg = Bob.decrypt(msg);
std::cout << msg << std::endl << std::endl;
if(msg_s!=msg)
throw std::runtime_error("Messages aren't same");
} catch (const std::runtime_error& error){
return error.what();
}
return "";
}
std::string test_low_level_AES(){
try{
std::cout << "Start an AESkey and encrypt a message at 128 bits key size" << std::endl;
RSAES::AES::AESkey Bob(128);
std::string msg_s = word_bank[dist_50(mt)];
int words = dist_50(mt);
for(int i=0; i<words; ++i)
(msg_s+=' ')+=word_bank[dist_50(mt)];
std::string msg = RSAES::AES::big_encrypt(msg_s, Bob);
std::cout << msg << std::endl << std::endl;
std::cout << "Decrypt the message using the key" << std::endl;
msg = RSAES::AES::big_decrypt(msg, Bob);
std::cout << msg << std::endl << std::endl;
{
std::cout << "For reference, the key in base64:" << std::endl;
auto p = Bob.expanded_key.begin();
std::vector<unsigned char> exp(p, p+Bob.base); // send the un-expanded key so that we need less data to send
std::string AES_string(exp.begin(), exp.end());
size_t key_s;
unsigned char* prt = RSAES::UTIL::base64_encode((const unsigned char*)AES_string.c_str(), AES_string.size(), &key_s);
for(size_t i=0; i<key_s; ++i)
std::cout << prt[i];
free(prt);
putchar('\n');
putchar('\n');
}
if(msg_s!=msg)
throw std::runtime_error("Messages aren't same - small");
std::cout << "Start an AESkey and encrypt a message at 1 megabit key size" << std::endl;
RSAES::AES::AESkey Allice(1048576);
msg_s = word_bank[dist_50(mt)];
words = dist_50(mt);
for(int i=0; i<words; ++i)
(msg_s+=' ')+=word_bank[dist_50(mt)];
msg = RSAES::AES::big_encrypt(msg_s, Allice);
std::cout << msg << std::endl << std::endl;
std::cout << "Decrypt the message using the key" << std::endl;
msg = RSAES::AES::big_decrypt(msg, Allice);
std::cout << msg << std::endl << std::endl;
if(msg_s!=msg)
throw std::runtime_error("Messages aren't same - small");
} catch (const std::runtime_error& error){
return error.what();
}
return "";
}
bool test_gigabit(){
try{
std::cout << "Create and expand an AESkey of 1 gigabit key size" << std::endl;
RSAES::AES::AESkey Allice(1073741824);
std::cout << "Key created. Encrypt the message using the key" << std::endl;
std::string msg_s = word_bank[dist_50(mt)];
for(int i=0; i<50; ++i)
(msg_s+=' ')+=word_bank[dist_50(mt)];
std::string msg = RSAES::AES::big_encrypt(msg_s, Allice);
std::cout << msg << std::endl << std::endl;
std::cout << "Decrypt the message using the key" << std::endl;
msg = RSAES::AES::big_decrypt(msg, Allice);
std::cout << msg << std::endl << std::endl;
if(msg_s!=msg)
throw std::runtime_error("Messages aren't same - gigabit");
} catch (const std::runtime_error& error){
return false;
}
return true;
}
int main(){
bool s_gigabit = true;
unsigned int s_aes=0, f_aes=0, f_high=0, s_high=0, f_rsa=0, s_rsa=0;
bool
test_high = true
,test_rsa = false
,test_aes = false
,test_gig = false
;
if(test_high){
std::cout << "TESTING HIGH LEVEL INTERFACE" << std::endl;
for(unsigned int done=1; done; ++done){
std::string result = test_high_level();
if(result=="")
++s_high;
else {
++f_high;
std::cout << "TEST FAILED WITH MESSAGE: " << std::endl << result << std::endl;
exit(0);
}
std::cout << "-----------------------" << std::endl;
std::cout << "HIGH LEVEL INTERFACE" << std::endl;
std::cout << "SUCESSFULL TESTS: " << s_high << std::endl;
std::cout << "FAILED TESTS: " << f_high << std::endl;
std::cout << "-----------------------" << std::endl;
}
}
if(test_rsa){
std::cout << "TESTING LOW LEVEL INTERFACE - RSA" << std::endl;
for(unsigned int done=0; done<10; ++done){
std::string result = test_low_level_RSA();
if(result=="")
++s_rsa;
else {
++f_rsa;
std::cout << "TEST FAILED WITH MESSAGE: " << std::endl << result << std::endl;
}
std::cout << "-----------------------" << std::endl;
std::cout << "LOW LEVEL INTERFACE RSA" << std::endl;
std::cout << "SUCESSFULL TESTS: " << s_rsa << std::endl;
std::cout << "FAILED TESTS: " << f_rsa << std::endl;
std::cout << "-----------------------" << std::endl;
}
}
if(test_aes){
std::cout << "TESTING LOW LEVEL INTERFACE - AES" << std::endl;
for(unsigned int done=0; done<10; ++done){
std::string result = test_low_level_AES();
if(result=="")
++s_aes;
else {
++f_aes;
std::cout << "TEST FAILED WITH MESSAGE: " << std::endl << result << std::endl;
}
std::cout << "-----------------------" << std::endl;
std::cout << "LOW LEVEL INTERFACE AES" << std::endl;
std::cout << "SUCESSFULL TESTS: " << s_aes << std::endl;
std::cout << "FAILED TESTS: " << f_aes << std::endl;
std::cout << "-----------------------" << std::endl;
}
}
if(test_gig){
std::cout << "TESTING LOW LEVEL INTERFACE - AES AT 1 GIGABIT KEY SIZE" << std::endl;
s_gigabit = test_gigabit();
if(!s_gigabit)
std::cout << "TEST FAILED WITH MESSAGE: " << std::endl << "Messages aren't same - gigabit" << std::endl;
std::cout << "-----------------------" << std::endl;
std::cout << "GIGABIT AES LOW LEVEL " << std::endl;
std::cout << "TEST " << (s_gigabit?"PASSED":"FAILED") << std::endl;
std::cout << "-----------------------" << std::endl;
}
std::cout << std::endl << "TEST RESULTS:" << std::endl;
std::cout << "-----------------------" << std::endl;
if(test_high){
std::cout << "HIGH LEVEL INTERFACE" << std::endl;
std::cout << "SUCESSFULL TESTS: " << s_high << std::endl;
std::cout << "FAILED TESTS: " << f_high << std::endl;
std::cout << "-----------------------" << std::endl;
}
if(test_rsa){
std::cout << "LOW LEVEL INTERFACE RSA" << std::endl;
std::cout << "SUCESSFULL TESTS: " << s_rsa << std::endl;
std::cout << "FAILED TESTS: " << f_rsa << std::endl;
std::cout << "-----------------------" << std::endl;
}
if(test_aes){
std::cout << "LOW LEVEL INTERFACE AES" << std::endl;
std::cout << "SUCESSFULL TESTS: " << s_aes << std::endl;
std::cout << "FAILED TESTS: " << f_aes << std::endl;
std::cout << "-----------------------" << std::endl;
}
if(test_gig){
std::cout << "GIGABIT AES LOW LEVEL " << std::endl;
std::cout << "TEST " << (s_gigabit?"PASSED":"FAILED") << std::endl;
std::cout << "-----------------------" << std::endl;
}
if(!f_high && !f_rsa && !f_aes && s_gigabit)
std::cout << "PASSED TESTS" << std::endl;
else
std::cout << "FAILED TESTS" << std::endl;
return 0;
}