-
Notifications
You must be signed in to change notification settings - Fork 2
/
testkpabe.cpp
521 lines (378 loc) · 16.7 KB
/
testkpabe.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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
Testbed for empirical evaluation of KP-ABE schemes, according to Crampton, Pinto (CSF2014).
Code by: Alexandre Miranda Pinto
This file implements tests for the KPABE class.
The tests are run for two specific secret sharing schemes, the canonical Benaloh-Leichter and the tree of Shamir gates.
The tests run for each scheme are exactly the same.
*/
//----------------------------------------------------------------
//---------------------- KPABE Class ------------------------
#ifndef DEF_UTILS
#include "utils.h"
#endif
#ifndef DEF_SECRET_SHARING
#include "secretsharing.h"
#endif
#ifndef DEF_BL_CANON
#include "BLcanonical.h"
#endif
#ifndef DEF_SH_TREE
#include "ShTree.h"
#endif
#ifndef DEF_KPABE
#include "kpabe.h"
#endif
unsigned int polNAttr = 5;
unsigned int nattr = 20;
vector<int> pol_parts;
//-----------------------------------------------------------
//------------------- Main Level ----------------------------
int test1(int errors, KPABE& testClass, PFC& m_pfc, G1 &P, G2 &Q, Big order){
//------------------ Test 1: scheme params generation ------------------------
OUT("==============================<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>==============================");
OUT("Beginning of test 1");
G1 temp1 = m_pfc.mult(P,order+1);
G2 temp2 = m_pfc.mult(Q,order+1);
test_diagnosis("Test 1: number of attributes", testClass.numberAttr() == nattr, errors);
test_diagnosis("Test 1: P^order == 1", temp1 == P, errors);
test_diagnosis("Test 1: Q^order == 1", temp2 == Q, errors);
return errors;
}
int test2(int errors, KPABE& testClass, PFC& m_pfc, G1 &P, G2 &Q){
//------------------ Test 2: scheme setup ------------------------
OUT("==============================<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>==============================");
OUT("Beginning of test 2");
vector<Big> &privateKeyAtts = testClass.getPrivateAttributes();
Big& privateKeyBlinder = testClass.getPrivateKeyRand();
GT& publicCTBlinder = testClass.getPublicCTBlinder();
#ifdef AttOnG1_KeyOnG2
vector<G1> &publicKeyAtts = testClass.getPublicAttributes();
#endif
#ifdef AttOnG2_KeyOnG1
vector<G2> &publicKeyAtts = testClass.getPublicAttributes();
#endif
DEBUG("size of private key: " << privateKeyAtts.size() << " -- expected: " << nattr);
test_diagnosis("Test 2: attribute data structures", privateKeyAtts.size() == nattr, errors);
test_diagnosis("Test 2: attribute data structures", publicKeyAtts.size() == nattr, errors);
stringstream ss;
for (unsigned int i = 0; i < privateKeyAtts.size(); i++){
ss << "Test 2 - " << i << ": attributes' computation";
#ifdef AttOnG1_KeyOnG2
G1 temp1 = m_pfc.mult(P, privateKeyAtts[i]);
test_diagnosis(ss.str(), temp1 == publicKeyAtts[i], errors);
#endif
#ifdef AttOnG2_KeyOnG1
G2 temp2 = m_pfc.mult(Q, privateKeyAtts[i]);
test_diagnosis(ss.str(), temp2 == publicKeyAtts[i], errors);
#endif
ss.str("");
}
GT pair = m_pfc.pairing(Q,P);
test_diagnosis("Test 2a: blinding factor", m_pfc.power(pair, privateKeyBlinder) == publicCTBlinder, errors);
return errors;
}
int test3(int errors, KPABE& testClass, PFC& m_pfc, miracl *mip, G1 &P, G2 &Q, vector<int>& CTAtts, vector<int>& badCTAtts){
//------------------ Test 3: Encryption ------------------------
OUT("==============================<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>==============================");
OUT("Beginning of test 3");
GT& publicCTBlinder = testClass.getPublicCTBlinder();
#ifdef AttOnG1_KeyOnG2
vector<G1> &publicKeyAtts = testClass.getPublicAttributes();
#endif
#ifdef AttOnG2_KeyOnG1
vector<G2> &publicKeyAtts = testClass.getPublicAttributes();
#endif
GT CT;
Big sCT;
#ifdef AttOnG1_KeyOnG2
vector<G1> AttFrags;
vector<G1> BadAttFrags;
#endif
#ifdef AttOnG2_KeyOnG1
vector<G2> AttFrags;
vector<G2> BadAttFrags;
#endif
Big rand;
Big CTrand;
GT aux;
stringstream ss;
GT pair = m_pfc.pairing(Q,P);
m_pfc.random(rand); // picking a random message
const GT M = m_pfc.power(pair, rand);
mip->IOBASE=256;
const Big sM = (char *)"test message";
mip->IOBASE=16;
bool success = testClass.encryptS(badCTAtts, sM, sCT, AttFrags);
test_diagnosis("Test 3 - hash encrypt: bad attributes, return fail", !success, errors);
success = testClass.encryptS(CTAtts, sM, sCT, AttFrags);
test_diagnosis("Test 3 - hash encrypt: size of attribute frags", AttFrags.size() == CTAtts.size(), errors);
CTrand = testClass.getLastEncryptionRandomness();
test_diagnosis("Test 3 - hash encrypt: good attributes, succeed", success, errors);
aux = m_pfc.power(publicCTBlinder, CTrand);
test_diagnosis("Test 3 - hash encrypt: CT well-formedness", lxor(sM,m_pfc.hash_to_aes_key(aux)) == sCT, errors);
success = testClass.encrypt(badCTAtts, M, CT, AttFrags);
test_diagnosis("Test 3 - mult encrypt: bad attributes, return fail", !success, errors);
success = testClass.encrypt(CTAtts, M, CT, AttFrags);
test_diagnosis("Test 3 - mult encrypt: size of attribute frags", AttFrags.size() == CTAtts.size(), errors);
CTrand = testClass.getLastEncryptionRandomness();
test_diagnosis("Test 3 - mult encrypt: good attributes, succeed", success, errors);
aux = m_pfc.power(publicCTBlinder, CTrand);
test_diagnosis("Test 3 - mult encrypt: CT well-formedness", (M * aux) == CT, errors);
// test_diagnosis("Test 3: CT well-formedness", (M * aux) == CT, errors);
// CT=lxor(M,m_pfc.hash_to_aes_key(blinder));
for (unsigned int i = 0; i < CTAtts.size(); i++){
ss << "Test 3 - " << i << ": attribute fragments well-formedness";
#ifdef AttOnG1_KeyOnG2
G1 temp1 = m_pfc.mult(publicKeyAtts[CTAtts[i]], CTrand);
test_diagnosis(ss.str(), temp1 == AttFrags[i], errors);
#endif
#ifdef AttOnG2_KeyOnG1
G2 temp2 = m_pfc.mult(publicKeyAtts[CTAtts[i]], CTrand);
test_diagnosis(ss.str(), temp2 == AttFrags[i], errors);
#endif
ss.str("");
}
return errors;
}
int test4(int errors, KPABE& testClass, PFC& m_pfc, G1 &P, G2 &Q, Big order){
//------------------ Test 4: Key Generation ------------------------
OUT("==============================<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>==============================");
OUT("Beginning of test 4");
stringstream ss;
Big& privateKeyBlinder = testClass.getPrivateKeyRand();
vector<Big> &privateKeyAtts = testClass.getPrivateAttributes();
#ifdef AttOnG1_KeyOnG2
vector<G1> AttFrags;
vector<G1> BadAttFrags;
#endif
#ifdef AttOnG2_KeyOnG1
vector<G2> AttFrags;
vector<G2> BadAttFrags;
#endif
// ShamirAccessPolicy policy(threshold, pol_parts, order);
// ShamirSS shamir(policy, m_pfc);
shared_ptr<SecretSharing> scheme = testClass.getScheme();
shared_ptr<AccessPolicy> policy = scheme->getPolicy();
std::vector<ShareTuple> shares = scheme->distribute_random(privateKeyBlinder);
vector<Big> randomness = scheme->getDistribRandomness();
Big privateAtt;
DEBUG("------------ Start GEN KEY --------------");
#ifdef AttOnG1_KeyOnG2
vector<G2> keyFrags = testClass.genKey(randomness);
#endif
#ifdef AttOnG2_KeyOnG1
vector<G1> keyFrags = testClass.genKey(randomness);
#endif
DEBUG("------------ Start TEST COMPARISON --------------");
for (unsigned int i = 0; i < policy->getNumShares(); i++){
ss << "Test 4 - " << i << ": key fragments well-formedness";
privateAtt = privateKeyAtts[shares[i].getPartIndex()];
#ifdef AttOnG1_KeyOnG2
G2 tmp = m_pfc.mult(Q, moddiv(shares[i].getShare(),privateAtt,order)); // for some reason, if tmp is not defined and this expression is written as is on
// in the next line, the compiler will interpret the result of m_pfc::mult as G2& and will not compile.
// This error does not occur for AttOnG2_KeyOnG1
test_diagnosis(ss.str(), tmp == keyFrags[i], errors);
#endif
#ifdef AttOnG2_KeyOnG1
test_diagnosis(ss.str(), m_pfc.mult(P, moddiv(shares[i].getShare(),privateAtt,order)) == keyFrags[i], errors);
#endif
DEBUG("Iter: " << i << " Share: " << shares[i].getShare()
<< " Attribute: " << privateAtt);
ss.str("");
}
return errors;
}
int test5(int errors, KPABE& testClass, PFC& m_pfc, miracl* mip, G1 &P, G2 &Q, vector<int> authCTAtts, vector<int> unauthCTAtts){
//------------------ Test 5: Decryption ------------------------
OUT("==============================<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>==============================");
OUT("Beginning of test 5");
shared_ptr<SecretSharing> scheme = testClass.getScheme();
shared_ptr<AccessPolicy> policy = scheme->getPolicy();
// ShamirAccessPolicy policy(threshold, pol_parts, order);
// Big& privateKeyBlinder = testClass.getPrivateKeyRand();
// ShamirSS shamir(policy, m_pfc);
// std::vector<ShareTuple> shares = shamir.distribute_random(privateKeyBlinder);
// DEBUG("[TEST5] Distributed secret (t): " << privateKeyBlinder);
#ifdef AttOnG1_KeyOnG2
vector<G2> keyFrags = testClass.genKey();
#endif
#ifdef AttOnG2_KeyOnG1
vector<G1> keyFrags = testClass.genKey();
#endif
mip->IOBASE=256;
const Big sM = (char *)"hello world to be encrypted";
mip->IOBASE=16;
#ifdef AttOnG1_KeyOnG2
vector<G1> AttFrags;
vector<G1> BadAttFrags;
#endif
#ifdef AttOnG2_KeyOnG1
vector<G2> AttFrags;
vector<G2> BadAttFrags;
#endif
Big sCT;
Big sPT;
bool success;
success = testClass.encryptS(authCTAtts, sM, sCT, AttFrags);
test_diagnosis("Test 5: string encryption with authorized attributes", success, errors);
success = testClass.decryptS(keyFrags, authCTAtts, sCT, AttFrags, sPT);
mip->IOBASE=256;
DEBUG("[TEST5] found plaintext : " << sPT);
mip->IOBASE=16;
test_diagnosis("Test 5: string decryption with authorized attributes success", success, errors);
test_diagnosis("Test 5: string decryption with authorized attributes equality", sPT == sM, errors);
success = testClass.encryptS(unauthCTAtts, sM, sCT, BadAttFrags);
test_diagnosis("Test 5: string encryption with unauthorized attributes", success, errors);
success = testClass.decryptS( keyFrags, unauthCTAtts, sCT, BadAttFrags, sPT);
test_diagnosis("Test 5: string decryption with unauthorized attributes", !success, errors);
// -----------------
Big rand;
m_pfc.random(rand); // picking a random message
GT pair = m_pfc.pairing(Q,P);
const GT GroupM = m_pfc.power(pair, rand);
GT GroupPT; // plaintetx
GT GroupCT; // main part of ciphertext
DEBUG("[TEST5] plaintext to be encrypted : " << m_pfc.hash_to_aes_key(GroupM));
success = testClass.encrypt(authCTAtts, GroupM, GroupCT, AttFrags);
test_diagnosis("Test 5: algebraic encryption with authorized attributes", success, errors);
success = testClass.decrypt( keyFrags, authCTAtts, GroupCT, AttFrags, GroupPT);
DEBUG("[TEST5] found plaintext : " << m_pfc.hash_to_aes_key(GroupPT));
test_diagnosis("Test 5: algebraic decryption with authorized attributes success", success, errors);
test_diagnosis("Test 5: algebraic decryption with authorized attributes equality", GroupPT == GroupM, errors);
success = testClass.encrypt(unauthCTAtts, GroupM, GroupCT, BadAttFrags);
test_diagnosis("Test 5: algebraic encryption with unauthorized attributes", success, errors);
success = testClass.decrypt( keyFrags, unauthCTAtts, GroupCT, BadAttFrags, GroupPT);
test_diagnosis("Test 5: algebraic decryption with unauthorized attributes", !success, errors);
return errors;
}
void debugVectorBig(string s, vector<Big> vec){
for (unsigned int i = 0; i < vec.size(); i++) {
DEBUG( s << "(" << i << ") = " << vec[i]);
}
}
void debugVectorG2(string s, vector<G2> vec, vector<Big> exps, G2 g, PFC& m_pfc){
G2 temp2;
for (unsigned int i = 0; i < vec.size(); i++) {
temp2 = m_pfc.mult(g, exps[i]);
if (temp2 == vec[i]) {
DEBUG(s << "(" << i << ") ---> Exponent: " << exps[i]);
} else {
DEBUG(s << "(" << i << ") ---> Wrong exponent");
}
}
}
const string name() {
return "[KPABETest class:]";
}
int runTests(KPABE testClass, PFC &pfc, miracl *mip, G1 &P, G2 &Q, Big order,
vector<int> authCTAtts, vector<int> badCTAtts, vector<int> unauthCTAtts ){
int errors = 0;
errors += test1(errors, testClass, pfc, P, Q, order);
errors += test2(errors, testClass, pfc, P, Q);
errors += test3(errors, testClass, pfc, mip, P, Q, authCTAtts, badCTAtts);
errors += test4(errors, testClass, pfc, P, Q, order);
errors += test5(errors, testClass, pfc, mip, P, Q, authCTAtts, unauthCTAtts);
return errors;
}
void classSetup(KPABE &testClass, G1 &P, G2 &Q, Big &order, int nparts){
testClass.paramsgen(P, Q, order);
testClass.setup();
for (int i = 0; i < nparts; i++){
pol_parts.push_back(i);
}
}
int main() {
// miracl *mip = mirsys(5000,0); // C version: this is necessary to get the MIRACL functioning, which means that then I can call Bigs and so forth.
// Miracl precision(5,0); // C++ version for the above, together with the next line
// miracl* mip = &precision;
// The constructor of PFC (in bn_pair.cpp) already invokes mirsys and initializes the mip pointer.
// Because of this, I don't do that explicitly here.
// It also sets the base to 16, but I include that here for clarity. One should not have to read the code of library classes to understand this code
// DEBUG("Starting Miracl setup");
PFC pfc(AES_SECURITY); // initialise pairing-friendly curve
miracl *mip=get_mip(); // get handle on mip (Miracl Instance Pointer)
mip->IOBASE=16;
// DEBUG("Finished Miracl setup");
time_t seed; // crude randomisation. Check if this is the version that is crypto-secure.
time(&seed);
irand((long)seed);
// DEBUG("Finished rand setup");
REPORT("Tests for KPABE scheme based on Canonical Benaloh-Leichter");
std::string expr = op_OR + "(1, " + op_AND + "(2,3,4), " + op_AND + "(2,5), " + op_AND + "(4,5))";
shared_ptr<BLAccessPolicy> policy = make_shared<BLAccessPolicy>(expr, polNAttr);
shared_ptr<BLSS> testSchemeBL = make_shared<BLSS>(policy, pfc);
// DEBUG("Created BL");
KPABE testClass(testSchemeBL, pfc, nattr);
// DEBUG("Created KPABE");
G1 P;
G2 Q;
Big order = pfc.order();
classSetup(testClass, P, Q, order, nattr);
std::string test_name = "Test KPABE: " "BL canonical";
// DEBUG("Finished KPABE Setup");
// DEBUG("Running tests");
// attribute indices for the ciphertext:
// the scheme has 20 attributes
// the policy has 5 attributes.
vector<int> authCTAtts; // all valid attribute indices, authorized set
authCTAtts.push_back(7);
authCTAtts.push_back(8);
authCTAtts.push_back(2);
authCTAtts.push_back(4);
authCTAtts.push_back(19);
authCTAtts.push_back(3);
vector<int> badCTAtts; // one invalid attribute index
badCTAtts.push_back(7);
badCTAtts.push_back(8);
badCTAtts.push_back(2);
badCTAtts.push_back(4);
badCTAtts.push_back(29);
badCTAtts.push_back(3);
vector<int> unauthCTAtts; // all valid attributes, unauthorized set
unauthCTAtts.push_back(3);
unauthCTAtts.push_back(11);
unauthCTAtts.push_back(17);
unauthCTAtts.push_back(9);
unauthCTAtts.push_back(12);
unauthCTAtts.push_back(5);
unauthCTAtts.push_back(15);
unauthCTAtts.push_back(11);
int result = 0;
result += runTests(testClass, pfc, mip, P, Q, order, authCTAtts, badCTAtts, unauthCTAtts);
print_test_result(result, test_name);
//==============================================================================================================
REPORT("Tests for KPABE scheme based on Tree of Shamir schemes");
std::string exprSH = op_THR + "(2, 1, " + op_AND + "(2,3,4), " + op_THR + "(2,2,5, " + op_OR + "(1,4,5)))";
shared_ptr<ShTreeAccessPolicy> policySH = make_shared<ShTreeAccessPolicy>(exprSH, polNAttr);
shared_ptr<ShTreeSS> testSchemeShTree = make_shared<ShTreeSS>(policySH, pfc);
// DEBUG("Created ShTree");
KPABE testClassSH(testSchemeShTree, pfc, nattr);
classSetup(testClassSH, P, Q, order, nattr);
std::string test_nameSH = "Test KPABE: " "Shamir Tree";
vector<int> authCTAttsSH; // all valid attribute indices, authorized set
authCTAttsSH.push_back(7);
authCTAttsSH.push_back(8);
authCTAttsSH.push_back(2);
authCTAttsSH.push_back(4);
authCTAttsSH.push_back(19);
authCTAttsSH.push_back(3);
vector<int> badCTAttsSH; // one invalid attribute index
badCTAttsSH.push_back(7);
badCTAttsSH.push_back(8);
badCTAttsSH.push_back(2);
badCTAttsSH.push_back(4);
badCTAttsSH.push_back(29);
badCTAttsSH.push_back(3);
vector<int> unauthCTAttsSH; // all valid attributes, unauthorized set
unauthCTAttsSH.push_back(3);
unauthCTAttsSH.push_back(11);
unauthCTAttsSH.push_back(17);
unauthCTAttsSH.push_back(9);
unauthCTAttsSH.push_back(12);
unauthCTAttsSH.push_back(5);
unauthCTAttsSH.push_back(15);
unauthCTAttsSH.push_back(11);
result += runTests(testClassSH, pfc, mip, P, Q, order, authCTAttsSH, badCTAttsSH, unauthCTAttsSH);
print_test_result(result, test_nameSH);
return 0;
}