-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
444 lines (388 loc) · 19.3 KB
/
Copy pathmain.cpp
File metadata and controls
444 lines (388 loc) · 19.3 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* CMD Parameter:
* -m <Path to the Message JSON>
* -p /dev/ttyUSB1 (Port for the FPGA comunication)
*/
#include "global.hpp"
#include "jsonHandling.hpp"
#include "menuPreparison.hpp"
#include "menu.hpp"
#include "turingBomb.hpp"
#include "checkingMachine.hpp"
#include "enigma.hpp"
#include "json/json.h"
// C library headers
#include <stdio.h>
#include <string.h>
#include <chrono>
// Linux headers
#include <fcntl.h> // Contains file controls like O_RDWR
#include <errno.h> // Error integer and strerror() function
#include <termios.h> // Contains POSIX terminal control definitions
#include <unistd.h> // write(), read(), close()
using namespace std;
#define MAX_ROTORS 5
#define BAUDRATE B115200
using namespace std;
void print_runtime(chrono::high_resolution_clock::time_point t_start);
double add_time(chrono::high_resolution_clock::time_point t_start, double d_t_total);
/**This is the main function
* @brief In the main all aplication function are called
*
* @return int
*/
int main(int argc, char **argv)
{
//********************************
// Check the Parameter
//********************************
string s_file_path;
string s_mode = "CPU";
int serial_port;
double d_t_total = 0.0;
uint ui_run_count = 0;
for(int i = 0; i < argc; i++)
{
if (strcmp(argv[i], "-m")==0)
{
s_file_path = argv[i+1];
}
else if (strcmp(argv[i], "-p")==0)
{
s_mode = argv[i+1];
}
}
if(s_file_path.empty()){
cerr << "Wrong Parameter! Please give me the message that should be encrypted!" << endl;
return 99;
}
if (strcmp(s_mode.c_str(), "CPU")!=0)
{
//https://blog.mbedded.ninja/programming/operating-systems/linux/linux-serial-ports-using-c-cpp/
//setup serial connection
serial_port = open(s_mode.c_str(), O_RDWR); // CMD Parameter: "/dev/ttyUSB1"
//Check for errors
if (serial_port < 0)
{
printf("Error %i from open: %s\n", errno, strerror(errno));
}
// Create new termios struc, we call it 'tty' for convention
struct termios tty;
memset(&tty, 0, sizeof tty);
// Read in existing settings, and handle any error
if(tcgetattr(serial_port, &tty) != 0) {
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
}
tty.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD; // | CRTSCTS
tty.c_iflag = IGNPAR | ICRNL;
tty.c_oflag = 0;
tty.c_lflag = ICANON;
tty.c_cc[VINTR] = 0; /* Ctrl-c */
tty.c_cc[VQUIT] = 0; /* Ctrl-\ */
tty.c_cc[VERASE] = 0; /* del */
tty.c_cc[VKILL] = 0; /* @ */
tty.c_cc[VEOF] = 4; /* Ctrl-d */
tty.c_cc[VTIME] = 0; /* inter-character timer unused */
tty.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
tty.c_cc[VSWTC] = 0; /* '\0' */
tty.c_cc[VSTART] = 0; /* Ctrl-q */
tty.c_cc[VSTOP] = 0; /* Ctrl-s */
tty.c_cc[VSUSP] = 0; /* Ctrl-z */
tty.c_cc[VEOL] = 0; /* '\0' */
tty.c_cc[VREPRINT] = 0; /* Ctrl-r */
tty.c_cc[VDISCARD] = 0; /* Ctrl-u */
tty.c_cc[VWERASE] = 0; /* Ctrl-w */
tty.c_cc[VLNEXT] = 0; /* Ctrl-v */
tty.c_cc[VEOL2] = 0; /* '\0' */
// Save tty settings, also checking for error
if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
}
}
chrono::high_resolution_clock::time_point t_real_start = chrono::high_resolution_clock::now();
chrono::high_resolution_clock::time_point t_start = chrono::high_resolution_clock::now();
string s_cry_msg,s_clear_crib;
try
{
jsonHandling json_file(s_file_path);
json_file.parse_file();
s_cry_msg = json_file.get_json_value("crypted");
s_clear_crib = json_file.get_json_value("clear_crib");
}
catch(const std::exception& e)
{
cerr << e.what() << endl;
return 99;
}
//Print read msg and make a char vector out of the clear crib
cout << "The crypted message is: " << s_cry_msg << endl;
cout << "The clear crib is: " << s_clear_crib << endl;
vector<char> vc_clear_crib(s_clear_crib.begin(), s_clear_crib.end());
//Do Preparison
menuPreparison preparison(s_cry_msg, s_clear_crib,USED_ROTORS,MAX_ROTORS);
//Check the posible position
int i_stock_count = preparison.store_posible_crib_position();
cout << "I found " << i_stock_count << " possible positions." <<endl;
string s_message = "n.a.";
cout << "Let's go" << endl;
st_Menu st_my_menu;
char * c_message, * c_clearCrib;
c_message =preparison.get_msg_as_array();
c_clearCrib = preparison.get_clear_crib_as_array();
int i_cribSize = preparison.get_crib_size();
int i_messageSize = preparison.get_msg_size();
//cout << "I will check this rotor variation: " << iv_rotors[0] << iv_rotors[1] << iv_rotors[2] << endl;
//for loop that checks all posible crib positions
for(int j=0; j<i_stock_count;j++)
{
char * c_crib = preparison.get_posible_crib_position_as_array(j);
int i_cribPos = preparison.get_crib_pos(j);
cout << "The current crib is at position: " << i_cribPos << endl;
//Menu erstellen
menu TURBOmenu(preparison.get_posible_crib_position(j), vc_clear_crib);
TURBOmenu.generate_graph();
for(uint i = 0; i < preparison.get_posible_crib_position(j).size(); i++)
{
cout << preparison.get_posible_crib_position(j)[i];
}
cout <<endl;
// if the generation of a menu is posible run the TURBO
if (TURBOmenu.generate_tabel())
{
//for loop that checks all rotor variations
for(int i=0; i<preparison.get_count_rotor_variations();i++)
{
vector<int> vi_current_rotor_variation = preparison.get_rotor_variation(i);
TURBOmenu.set_rotor_variation(vi_current_rotor_variation);
st_my_menu = TURBOmenu.get_Menu();
// //Turing Bomb Stuff
// //chrono::high_resolution_clock::time_point t_start = chrono::high_resolution_clock::now();
char c_stopResult[4];
char c_plugBoardSetting[ABC_SIZE];
char c_startPosE[3] = {'Y','W','Y'};
int i_coreOffset[AMOUNTOFDRUMS] = {0,0,0,0,0};
char c_ringSetting[DRUMARRAY_YSIZE];
//just for testing
// st_my_menu.i_rotors[0] = 2;
// st_my_menu.i_rotors[1] = 5;
// st_my_menu.i_rotors[2] = 3;
TuringBomb ob_fistTuringBomb(st_my_menu);
CheckingMachine ob_CheckingMachine;
Enigma ob_firstEnigma;
ob_firstEnigma.b_setReflector(st_my_menu.c_reflector);
ob_firstEnigma.b_setDrums(st_my_menu.i_rotors);
ob_firstEnigma.b_setCoreOffset(i_coreOffset);
cout << "Start" << endl;
t_start = chrono::high_resolution_clock::now();
//***********************************
//FPGA RUN
//***********************************
if (strcmp(s_mode.c_str(), "CPU")!=0){
unsigned char return_msg[] = {'\r'};
// send reset to BRAM
unsigned char reset_instruction_msg[] = {'R','S','E','T'};
write(serial_port, reset_instruction_msg, sizeof(reset_instruction_msg));
write(serial_port, return_msg, sizeof(return_msg));
//send drum possition
unsigned char drum_instruction_msg[] = {'D','R','U','M'};
write(serial_port, drum_instruction_msg, sizeof(drum_instruction_msg));
for(int i = 0; i<DRUMARRAY_XSIZE ; i++)
{
unsigned char msg_drum_pos[] = {';', st_my_menu.c_drums[i][0], st_my_menu.c_drums[i][1], st_my_menu.c_drums[i][2]};
write(serial_port, msg_drum_pos, sizeof(msg_drum_pos));
}
write(serial_port, return_msg, sizeof(return_msg));
//send the drums
unsigned char drumtyp_instruction_msg[] = {'D','T','Y','P'};
write(serial_port, drumtyp_instruction_msg, sizeof(drumtyp_instruction_msg));
for(int i = 0; i<DRUMARRAY_YSIZE ; i++)
{
char rotor_c;
sprintf(&rotor_c,"%d",st_my_menu.i_rotors[i]);
unsigned char msg_drum[] = { ';', rotor_c};
write(serial_port, msg_drum, sizeof(msg_drum));
}
write(serial_port, return_msg, sizeof(return_msg));
//send DB-Connections
unsigned char dbcon_instruction_msg[] = {'I','O','D','B',';',st_my_menu.c_test_register,st_my_menu.c_input_voltage};
write(serial_port, dbcon_instruction_msg, sizeof(dbcon_instruction_msg));
char c_enigmaDBConnections[2];
char c_test[2];
int i_DBconnectionCount;
int i_foundNumber = 0;
for(int i_enigma = 0; i_enigma < DRUMARRAY_XSIZE; i_enigma = i_enigma+1)
{
//set up the diagonal board connections
i_DBconnectionCount = 0;
for(int i_route = 0; i_route < ROUTENARRAY_XSIZE; i_route = i_route +1)
{
for(int i_searchCon = 0; i_searchCon < ROUTENARRAY_YSIZE; i_searchCon++)
{
//find first number in route
if(isdigit(st_my_menu.c_routen[i_route][i_searchCon]))
{
//prevent for overflow
if(i_route < ROUTENARRAY_XSIZE-1)
{
//check if the number has two digits
if(isdigit(st_my_menu.c_routen[i_route][i_searchCon+1]))
{
i_foundNumber = ((st_my_menu.c_routen[i_route][i_searchCon]-48)*10) +
((st_my_menu.c_routen[i_route][i_searchCon+1])-48);
}
//number consists of one digit
else
{
i_foundNumber = ((st_my_menu.c_routen[i_route][i_searchCon])-48);
}
if(i_foundNumber == i_enigma+1)
{
if(i_DBconnectionCount < 2)
{
c_enigmaDBConnections[i_DBconnectionCount] = st_my_menu.c_routen[i_route][0];
}
i_DBconnectionCount = i_DBconnectionCount+1;
}
}
}
}
}
unsigned char msg_drum[] = { ';', c_enigmaDBConnections[0], c_enigmaDBConnections[1]};
write(serial_port, msg_drum, sizeof(msg_drum));
}
write(serial_port, return_msg, sizeof(return_msg));
//test if everything is written to BRAM
// cout << "Close Port" << endl;
// close(serial_port);
// exit(0);
//send solution request
unsigned char solutionrequest_instruction_msg[] = {'R','Q','S','T'};
write(serial_port, solutionrequest_instruction_msg, sizeof(solutionrequest_instruction_msg));
write(serial_port, return_msg, sizeof(return_msg));
//wait for response
char read_buf[275];
memset(&read_buf, '\0', sizeof(read_buf));
int buffer_counter_i = 8;
char msg_buffer_c[9];
while(true)
{
//send solution request
unsigned char solutionrequest_instruction_msg[] = {'R','Q','S','T'};
write(serial_port, solutionrequest_instruction_msg, sizeof(solutionrequest_instruction_msg));
write(serial_port, return_msg, sizeof(return_msg));
read(serial_port, &read_buf, sizeof(read_buf));
cout << read_buf << endl;
if(read_buf[0] == 'S' && read_buf[1] == 'O' && read_buf[2] == 'L' && read_buf[3] == 'U' && read_buf[8] != '\0')
{
cout << "Found Solution" << endl;
cout << read_buf << endl;
c_stopResult[0] = read_buf[5];
c_stopResult[1] = read_buf[6];
c_stopResult[2] = read_buf[7];
c_stopResult[3] = read_buf[8];
cout << "Result: "<<c_stopResult[0]<<c_stopResult[1]<<c_stopResult[2]<<c_stopResult[3] << endl;
if(ob_CheckingMachine.calcPlugBoardConnections(st_my_menu,c_stopResult,c_plugBoardSetting))
{
c_ringSetting[0] = c_stopResult[0];
c_ringSetting[1] = c_stopResult[1];
c_ringSetting[2] = c_stopResult[2];
ob_firstEnigma.b_setDrumsToPos(c_startPosE);
ob_firstEnigma.b_setRingSetting(c_ringSetting);
ob_firstEnigma.b_setPlugBoard(c_plugBoardSetting);
if(ob_firstEnigma.b_decryptMessage(c_message,i_messageSize,c_crib,c_clearCrib,i_cribSize,i_cribPos,c_plugBoardSetting, st_my_menu, c_ringSetting))
{
//print_runtime(t_start);
ui_run_count ++;
d_t_total = add_time(t_start, d_t_total);
cout << "The TURBO run " << ui_run_count << " times" << endl;
cout << "The TURBO runs needed " << d_t_total << " seconds"<< endl;
print_runtime(t_real_start);
//send reset
unsigned char reset_instruction_msg[] = {'R','S','E','T'};
write(serial_port, reset_instruction_msg, sizeof(reset_instruction_msg));
write(serial_port, return_msg, sizeof(return_msg));
close(serial_port);
exit(0);
}
}
else
{
//send solution request
unsigned char solutionrequest_instruction_msg[] = {'R','Q','S','T'};
write(serial_port, solutionrequest_instruction_msg, sizeof(solutionrequest_instruction_msg));
write(serial_port, return_msg, sizeof(return_msg));
memset(&read_buf, '\0', sizeof(read_buf));
}
}
else if(read_buf[0] == 'S' && read_buf[1] == 'T' && read_buf[2] == 'O' && read_buf[3] == 'P')
{
cout << "Stop" << endl;
break;
}
else if(read_buf[0] == 'N' && read_buf[1] == 'S' && read_buf[2] == 'O' && read_buf[3] == 'L')
{
//no sulution in BRAM .... try again!
}
//
}
ui_run_count ++;
d_t_total = add_time(t_start, d_t_total);
}
//***********************************
// CPU RUN
//***********************************
else
{
while(ob_fistTuringBomb.b_findNextStop(c_stopResult))
{
//DKXD
//c_stopResult[0] = 'D';//read_buf[5];
//c_stopResult[1] = 'K';//read_buf[6];
//c_stopResult[2] = 'X';//read_buf[7];
//c_stopResult[3] = 'D';//read_buf[8];
//cout << st_my_menu.i_rotors[0] << st_my_menu.i_rotors[1] << st_my_menu.i_rotors[2] << endl;
//cout << c_stopResult[0] << c_stopResult[1] << c_stopResult[2] << endl;//c_stopResult[3] << endl;
//ob_fistTuringBomb.printDB();
//getchar();
//print_runtime(t_start);
if(ob_CheckingMachine.calcPlugBoardConnections(st_my_menu,c_stopResult,c_plugBoardSetting))
{
c_ringSetting[0] = c_stopResult[0];
c_ringSetting[1] = c_stopResult[1];
c_ringSetting[2] = c_stopResult[2];
ob_firstEnigma.b_setDrumsToPos(c_startPosE);
ob_firstEnigma.b_setRingSetting(c_ringSetting);
ob_firstEnigma.b_setPlugBoard(c_plugBoardSetting);
if(ob_firstEnigma.b_decryptMessage(c_message,i_messageSize,c_crib,c_clearCrib,i_cribSize,i_cribPos,c_plugBoardSetting, st_my_menu, c_ringSetting))
{
//DKXB
ui_run_count ++;
d_t_total = add_time(t_start, d_t_total);
cout << "Ring settings : " << c_ringSetting[0] << c_ringSetting[1] << c_ringSetting[2] << endl;
cout << "Diagonalboard solutuion : " << c_stopResult[3] << endl;
cout << "Plugboard setting : " << c_plugBoardSetting << endl;
cout << "The TURBO runs " << ui_run_count << " times" << endl;
cout << "The TURBO runs needed " << d_t_total << " seconds"<< endl;
print_runtime(t_real_start);
exit(0);
}
}
}
ui_run_count ++;
d_t_total = add_time(t_start, d_t_total);
}
}
}
}
cout << "It was not possible to encrypt this message!" << endl;
return 99;
}
void print_runtime(chrono::high_resolution_clock::time_point t_start){
chrono::high_resolution_clock::time_point t_end = chrono::high_resolution_clock::now();
chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t_end - t_start);
cout << "I need " << time_span.count() << " seconds for the whole computation"<< endl;
}
double add_time(chrono::high_resolution_clock::time_point t_start, double d_t_total){
chrono::high_resolution_clock::time_point t_end = chrono::high_resolution_clock::now();
chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t_end - t_start);
return d_t_total + time_span.count();
}