-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.cpp
More file actions
118 lines (97 loc) · 3.16 KB
/
generator.cpp
File metadata and controls
118 lines (97 loc) · 3.16 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <cassert>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include "AES.h"
using namespace std;
#define NUM_FILES 100000
#define MAX_BYTES_IDX 16
#define MAX_KEY 256
int main(int argc, char **argv) {
// //
// // check if Td4 is an inverse table of Te4
// //
// for (int i=0;i<256;i++) {
// int j = Te4[i]>>24;
// printf("%dth Te4 is %d, ",i,j);
// int k = Td4[j]>>24;
// printf("%dth Td4 is %d, same:%c\n",j,k,i==k?'Y':'N');
// assert(i==k);
// }
//check input values and define shared variables
if(argc < 2) {
printf("USAGE: generator FILE\n");
return 1;
}
string path(argv[1]);
string ct;
static char CMRs[NUM_FILES][MAX_BYTES_IDX][MAX_KEY];
for (uint file_no=1;file_no<=NUM_FILES;file_no++){
//
//open and read file
//
string filename = path;
filename += to_string(file_no) + ".txt";
FILE * f = fopen(filename.c_str(),"rb");
fseek(f, 0, SEEK_END);
uint f_size = ftell(f);
rewind(f);
ct.resize(f_size);
fread(&ct[0],1,f_size,f);
fclose(f);
//
//compute the number of CMR for given byte index and inferred key
//
for(uint byte_index = 0; byte_index<MAX_BYTES_IDX;byte_index++) {
for(uint inf_key = 0; inf_key<MAX_KEY;inf_key++) {
char bucket[16] = {0};
for(int tid=0;tid<32;tid++){
unsigned char ct_byte_for_tid = ct.c_str()[tid*16+byte_index];
unsigned char inv_idx_tt = ct_byte_for_tid ^ inf_key;
unsigned char value = Td4[inv_idx_tt]>>24;
bucket[value>>4]++;
}
int cnt = 0;
for(int index=0;index<16;index++){
cnt += bucket[index]>0;
}
//
// store the number of CMR
//
CMRs[file_no-1][byte_index][inf_key] = cnt;
}
}
}
string output_path = "./data";
mkdir(output_path.c_str(),0777);
for (uint byte_index = 0; byte_index<MAX_BYTES_IDX;byte_index++) {
for(uint inf_key = 0; inf_key<MAX_KEY;inf_key++) {
string byte_path = output_path + "/byte" + to_string(byte_index);
mkdir(byte_path.c_str(),0777);
//
// get output file ready
//
string output_filename = byte_path + "/key" + to_string(inf_key);
FILE* fo = fopen(output_filename.c_str(),"a");
if(fo==NULL){
printf("Cannot open the output File : %s \n",output_filename.c_str());
return 1;
}
//
// print out the number of CMR
//
for (uint file_no=1;file_no<=NUM_FILES;file_no++) {
fprintf(fo,"sampleNo:%d,CMRs:%d\n",file_no,CMRs[file_no-1][byte_index][inf_key]);
}
//
// close the output file
//
fclose(fo);
}
}
return 0;
}