-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
302 lines (244 loc) · 8.04 KB
/
test.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
using namespace std;
#include "test.hpp"
#include <gcrypt.h>
#include <sodium.h>
#include <iostream>
#include <time.h>
#include <fstream>
#include <string>
#include <sstream>
#include "pbc.h"
#include "Gate.h"
#include "Tag.h"
#include "pub_sub.h"
#include "kpabe.hpp"
#include "Timer.hpp"
vector<Timer> test(string text, int testSize)
{
vector<Timer> times = {};
addTime("Launch Time", clock(), times);
// Initialise GCRYPT
if (!gcry_check_version(GCRYPT_VERSION))
{
fputs("libgcrypt version mismatch\n", stderr);
exit(2);
}
/* A 256 bit key */
uint8_t *betaKey = (unsigned char *)"01234567890123456789012345678901";
vector<int> attributeUniverse{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
vector<Tag> pubArray = {};
vector<Tag> subArray = {};
vector<vector<string>> data;
ifstream infile("data.txt");
while (infile)
{
string s;
if (!getline(infile, s))
break;
istringstream ss(s);
vector<string> record;
while (ss)
{
string s;
if (!getline(ss, s, ','))
break;
record.push_back(s);
}
data.push_back( record );
}
for (int i = 0; i < testSize; i++)
{
pubArray.push_back(Tag(data[0][i].c_str(),data[1][i].c_str()));
}
for(int i = 0; i < testSize; i++){
subArray.push_back(Tag(data[0][i].c_str(),data[2][i].c_str(), true, false, '='));
}
vector<Gate*> gates;
generateTree(subArray,gates);
Gate* theRoot = gates.back();
//Subscriber Tree
Gate AND1(Gate::Type::AND, 1, false, 2, false);
Gate OR1(Gate::Type::OR, &AND1, 0, false);
Gate OR3(Gate::Type::OR, 3, false, 4, false);
Gate OR4(Gate::Type::OR, &OR1, &OR3);
OR4.makeParent();
//KP-ABE
PrivateParams priv;
PublicParams pub;
setup(attributeUniverse, pub, priv);
//Make access policy from subscriber tree and generate key
Node* root = new Node;
*root = OR4.createABETree();
auto key = keyGeneration(priv, *root);
addTime("Init ", clock(), times);
// Create an attribute-based secret (attributes 1 and 3).
element_s secret;
vector<int> encryptionAttributes{0, 1, 2, 3, 4};
auto Cw = createSecret(pub, encryptionAttributes, secret);
//Encrypt the message
std::vector<uint8_t> ciphertext = encrypt(pub, encryptionAttributes, text, Cw);
addTime("Encrypt of Payload by Pub", clock(), times);
//Encryption of Interests
for (int i = 0; i < subArray.size(); i++){
subArray[i].genSubHash(betaKey);
}
addTime("Interests Encryption", clock(), times);
//Generate r permutation and send to B2
if (sodium_init() == -1)
{
printf("Sodium Library failed to initilise");
}
int r = randombytes_random();
//From Sub to B1 - PRP
interestPermutation(r, subArray);
addTime("Permutation Time on Sub", clock(), times);
//Encryption time of Tags
for (int i = 0; i < pubArray.size(); i++){
pubArray[i].genHash(betaKey);
}
addTime("Encrypt Of Tags", clock(), times);
//Done by B1 - Search
vector<bool> matches = matchInterests(pubArray, subArray);
addTime("Search and decryption time on B1", clock(), times);
//Done by B2 - PRP Reverse
interestPermutationReverse(r, matches);
addTime("PRP reverse B2", clock(), times);
//Send to B3 - Structure
//Done by B3 - Evaluation of Tree
int eval = theRoot->evaluate(matches);
if (eval == 1) ;//cout << "Evaluated" << endl;
else cout << "Evaluation Failiure" << endl;
addTime("Evaluation of tree by B3", clock(), times);
//Decryption of payload
vector<int> testAttributes{0, 1, 2, 4};
string result;
try
{
result = decrypt(key, Cw, testAttributes, ciphertext);
}
catch (const UnsatError &e)
{
cout << "Decryption Error" << endl;
}
addTime("Decryption of Payload by sub", clock(), times);
totalTime(times, false);
totalTime(times);
if (result == text) ;//cout << "Decrypted Successfully" << endl;
else cout << "Unsuccessful Decryption" << endl;
return times;
}
vector<Timer> singleBrokerTest(string text, int testSize)
{
vector<Timer> times = {};
addTime("Launch Time", clock(), times);
// Initialise GCRYPT
if (!gcry_check_version(GCRYPT_VERSION))
{
fputs("libgcrypt version mismatch\n", stderr);
exit(2);
}
/* A 256 bit key */
uint8_t *betaKey = (unsigned char *)"01234567890123456789012345678901";
vector<int> attributeUniverse{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
vector<Tag> pubArray = {};
vector<Tag> subArray = {};
vector<vector<string>> data;
ifstream infile("data.txt");
while (infile)
{
string s;
if (!getline(infile, s))
break;
istringstream ss(s);
vector<string> record;
while (ss)
{
string s;
if (!getline(ss, s, ','))
break;
record.push_back(s);
}
data.push_back( record );
}
for (int i = 0; i < testSize; i++)
{
pubArray.push_back(Tag(data[0][i].c_str(),data[1][i].c_str()));
}
for(int i = 0; i < testSize; i++){
subArray.push_back(Tag(data[0][i].c_str(),data[2][i].c_str(), true, false, '='));
}
vector<Gate*> gates;
generateTree(subArray,gates);
Gate* theRoot = gates.back();
//Subscriber Tree
// Gate AND1(Gate::Type::AND, 1, false, 2, false);
// Gate OR1(Gate::Type::OR, &AND1, 0, false);
// Gate OR3(Gate::Type::OR, 3, false, 4, false);
// Gate OR4(Gate::Type::OR, &OR1, &OR3);
// OR4.makeParent();
// //KP-ABE
// PrivateParams priv;
// PublicParams pub;
// setup(attributeUniverse, pub, priv);
// //Make access policy from subscriber tree and generate key
// Node* root = new Node;
// *root = OR4.createABETree();
// auto key = keyGeneration(priv, *root);
addTime("Init ", clock(), times);
// Create an attribute-based secret (attributes 1 and 3).
// element_s secret;
// vector<int> encryptionAttributes{0, 1, 2, 3, 4};
// auto Cw = createSecret(pub, encryptionAttributes, secret);
//Encrypt the message
// std::vector<uint8_t> ciphertext = encrypt(pub, encryptionAttributes, text, Cw);
// addTime("Encrypt of Payload by Pub", clock(), times);
//Encryption of Interests
for (int i = 0; i < subArray.size(); i++){
subArray[i].genSubHash(betaKey);
}
addTime("Interests Encryption", clock(), times);
//Encryption time of Tags
for (int i = 0; i < pubArray.size(); i++){
pubArray[i].genHash(betaKey);
}
addTime("Encrypt Of Tags", clock(), times);
//Done by B1 - Search
vector<bool> matches = matchInterestsSingleBroker(pubArray, subArray);
addTime("Search and decryption time on B1", clock(), times);
//Send to B3 - Structure
//Done by B3 - Evaluation of Tree
int eval = theRoot->evaluate(matches);
if (eval == 1) ;//cout << "Evaluated" << endl;
else cout << "Evaluation Failiure" << endl;
addTime("Evaluation of tree by B3", clock(), times);
//Decryption of payload
// vector<int> testAttributes{0, 1, 2, 4};
// string result;
// try
// {
// result = decrypt(key, Cw, testAttributes, ciphertext);
// }
// catch (const UnsatError &e)
// {
// cout << "Decryption Error" << endl;
// }
// addTime("Decryption of Payload by sub", clock(), times);
// totalTime(times, false);
// totalTime(times);
// if (result == text) ;//cout << "Decrypted Successfully" << endl;
// else cout << "Unsuccessful Decryption" << endl;
return times;
}
static const char alphanum[] = "0123456789!@#$^&*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
string randomString(int len){
if (sodium_init() == -1){
printf("Sodium Library failed to initilise");
}
srand (randombytes_random());
string text = "";
for(int i = 0; i < len; i++){
int num = rand() % 70;
text += alphanum[num];
}
return text;
}