1 #include <gtest/gtest.h>
6 static dipe_param_t param
;
8 TEST(DipeTest
, DipeMasterKeygen
) {
9 dipe_master_publickey_t pk
;
10 dipe_master_secretkey_t sk
;
12 dipe_master_keygen(param
, 12, &pk
, &sk
);
14 dipe_free_master_secretkey(sk
);
15 dipe_free_master_publickey(pk
);
18 TEST(DipeTest
, DipeKeygen
) {
19 dipe_master_publickey_t pk
;
20 dipe_master_secretkey_t msk
;
23 for (size_t i
= 0; i
< 5; ++i
) {
24 element_init_Zr(y
[i
], *dipe_get_pairing(param
));
28 dipe_master_keygen(param
, 5, &pk
, &msk
);
29 dipe_keygen(param
, msk
, "1234567890abcdef", y
, &sk
);
31 dipe_free_master_secretkey(msk
);
32 dipe_free_master_publickey(pk
);
33 dipe_free_secretkey(sk
);
35 for (size_t i
= 0; i
< 5; ++i
) {
40 TEST(DipeTest
, DipeDecapFail
) {
42 dipe_master_publickey_t pk
;
43 dipe_master_secretkey_t msk
;
47 for (size_t i
= 0; i
< 5; ++i
) {
48 element_init_Zr(y
[i
], *dipe_get_pairing(param
));
52 dipe_master_keygen(param
, 5, &pk
, &msk
);
53 dipe_keygen(param
, msk
, "1234567890abcdef", y
, &sk
);
55 element_t ptxt
, ptxtp
;
56 element_init_GT(ptxt
, *dipe_get_pairing(param
));
57 element_init_GT(ptxtp
, *dipe_get_pairing(param
));
61 dipe_encap(param
, 1, &pk
, 1, y
, ptxt
, &ctxt
);
62 dipe_decap(param
, 1, &sk
, "1234567890abcdef", y
, ctxt
, ptxtp
);
64 EXPECT_NE(element_cmp(ptxt
, ptxtp
), 0);
66 for (size_t i
= 0; i
< 5; ++i
) {
73 dipe_free_master_secretkey(msk
);
74 dipe_free_master_publickey(pk
);
75 dipe_free_secretkey(sk
);
79 TEST(DipeTest
, DipeDecapTrivialSuccess
) {
81 dipe_master_publickey_t pk
;
82 dipe_master_secretkey_t msk
;
86 for (size_t i
= 0; i
< 5; ++i
) {
87 element_init_Zr(y
[i
], *dipe_get_pairing(param
));
91 dipe_master_keygen(param
, 5, &pk
, &msk
);
92 dipe_keygen(param
, msk
, "1234567890abcdef", y
, &sk
);
94 element_t ptxt
, ptxtp
;
95 element_init_GT(ptxt
, *dipe_get_pairing(param
));
96 element_init_GT(ptxtp
, *dipe_get_pairing(param
));
100 dipe_encap(param
, 1, &pk
, 1, y
, ptxt
, &ctxt
);
101 dipe_decap(param
, 1, &sk
, "1234567890abcdef", y
, ctxt
, ptxtp
);
103 EXPECT_EQ(element_cmp(ptxt
, ptxtp
), 0);
105 for (size_t i
= 0; i
< 5; ++i
) {
110 element_clear(ptxtp
);
112 dipe_free_master_secretkey(msk
);
113 dipe_free_master_publickey(pk
);
114 dipe_free_secretkey(sk
);
115 dipe_free_ctxt(ctxt
);
118 TEST(DipeTest
, DipeDecapSuccess
) {
120 dipe_master_publickey_t pk
;
121 dipe_master_secretkey_t msk
;
127 for (size_t i
= 0; i
< 6; i
+=2) {
128 element_init_Zr(y
[i
], *dipe_get_pairing(param
));
129 element_init_Zr(x
[i
], *dipe_get_pairing(param
));
130 element_init_Zr(y
[i
+1], *dipe_get_pairing(param
));
131 element_init_Zr(x
[i
+1], *dipe_get_pairing(param
));
134 element_set1(x
[i
+1]);
135 element_random(y
[i
+1]);
136 element_neg(x
[i
], y
[i
+1]);
139 dipe_master_keygen(param
, 6, &pk
, &msk
);
140 dipe_keygen(param
, msk
, "1234567890abcdef", y
, &sk
);
142 element_t ptxt
, ptxtp
;
143 element_init_GT(ptxt
, *dipe_get_pairing(param
));
144 element_init_GT(ptxtp
, *dipe_get_pairing(param
));
146 element_random(ptxt
);
148 dipe_encap(param
, 1, &pk
, 1, x
, ptxt
, &ctxt
);
150 dipe_decap(param
, 1, &sk
, "1234567890abcdef", y
, ctxt
, ptxtp
);
151 EXPECT_EQ(element_cmp(ptxt
, ptxtp
), 0);
153 /* non-matching CID */
154 dipe_decap(param
, 1, &sk
, "fedcba0987654321", y
, ctxt
, ptxtp
);
155 EXPECT_NE(element_cmp(ptxt
, ptxtp
), 0);
157 /* inconsistent y value */
158 dipe_decap(param
, 1, &sk
, "1234567890abcdef", x
, ctxt
, ptxtp
);
159 EXPECT_NE(element_cmp(ptxt
, ptxtp
), 0);
161 for (size_t i
= 0; i
< 6; ++i
) {
167 element_clear(ptxtp
);
169 dipe_free_master_secretkey(msk
);
170 dipe_free_master_publickey(pk
);
171 dipe_free_secretkey(sk
);
172 dipe_free_ctxt(ctxt
);
175 TEST(DipeTest
, DipeDecryptSuccessSmall
) {
180 dipe_master_publickey_t pk
;
181 dipe_master_secretkey_t msk
;
187 for (size_t i
= 0; i
< 6; i
+=2) {
188 element_init_Zr(y
[i
], *dipe_get_pairing(param
));
189 element_init_Zr(x
[i
], *dipe_get_pairing(param
));
190 element_init_Zr(y
[i
+1], *dipe_get_pairing(param
));
191 element_init_Zr(x
[i
+1], *dipe_get_pairing(param
));
194 element_set1(x
[i
+1]);
195 element_random(y
[i
+1]);
196 element_neg(x
[i
], y
[i
+1]);
199 dipe_master_keygen(param
, 6, &pk
, &msk
);
200 dipe_keygen(param
, msk
, "1234567890abcdef", y
, &sk
);
201 overhead
= dipe_ciphertext_overhead(param
, 6, 0);
203 for (size_t clen
= overhead
; clen
< 672; ++clen
) {
204 memset(ctxt
, 0, 672);
205 memset(ptxt
, 0, 672);
206 memcpy(ptxt
, "test", 4);
208 dipe_encrypt(param
, 1, &pk
, 1, x
, 4, ptxt
, clen
, ctxt
);
209 ptxt_len
= dipe_decrypt(param
, 1, &sk
, 0, "1234567890abcdef", y
, clen
, ctxt
, ptxt
);
211 if (clen
>= overhead
+ 4) {
212 EXPECT_EQ(ptxt_len
, 4);
213 EXPECT_STREQ(ptxt
, "test");
216 EXPECT_EQ(ptxt_len
, 0);
220 for (size_t i
= 0; i
< 6; ++i
) {
225 dipe_free_master_secretkey(msk
);
226 dipe_free_master_publickey(pk
);
227 dipe_free_secretkey(sk
);
230 TEST(DipeTest
, DipeDecryptSuccessTightSmall
) {
233 dipe_master_publickey_t pk
;
234 dipe_master_secretkey_t msk
;
240 for (size_t i
= 0; i
< 6; i
+=2) {
241 element_init_Zr(y
[i
], *dipe_get_pairing(param
));
242 element_init_Zr(x
[i
], *dipe_get_pairing(param
));
243 element_init_Zr(y
[i
+1], *dipe_get_pairing(param
));
244 element_init_Zr(x
[i
+1], *dipe_get_pairing(param
));
247 element_set1(x
[i
+1]);
248 element_random(y
[i
+1]);
249 element_neg(x
[i
], y
[i
+1]);
252 dipe_master_keygen(param
, 6, &pk
, &msk
);
253 dipe_keygen(param
, msk
, "1234567890abcdef", y
, &sk
);
254 overhead
= dipe_ciphertext_overhead(param
, 6, 0);
256 char ctxt
[overhead
+ 4];
257 char ptxt
[overhead
+ 4];
259 for (size_t clen
= overhead
; clen
<= overhead
+ 4; ++clen
) {
260 memset(ctxt
, 0, overhead
+ 4);
261 memset(ptxt
, 0, overhead
+ 4);
262 memcpy(ptxt
, "test", 4);
264 dipe_encrypt(param
, 1, &pk
, 1, x
, 4, ptxt
, clen
, ctxt
);
265 ptxt_len
= dipe_decrypt(param
, 1, &sk
, 0, "1234567890abcdef", y
, clen
, ctxt
, ptxt
);
267 if (clen
>= overhead
+ 4) {
268 EXPECT_EQ(ptxt_len
, 4);
269 EXPECT_STREQ(ptxt
, "test");
272 EXPECT_EQ(ptxt_len
, 0);
276 for (size_t i
= 0; i
< 6; ++i
) {
281 dipe_free_master_secretkey(msk
);
282 dipe_free_master_publickey(pk
);
283 dipe_free_secretkey(sk
);
287 TEST(DipeTest
, DipeDecryptSuccessLargeBlocksize
) {
292 dipe_master_publickey_t pk
;
293 dipe_master_secretkey_t msk
;
299 for (size_t i
= 0; i
< 6; i
+=2) {
300 element_init_Zr(y
[i
], *dipe_get_pairing(param
));
301 element_init_Zr(x
[i
], *dipe_get_pairing(param
));
302 element_init_Zr(y
[i
+1], *dipe_get_pairing(param
));
303 element_init_Zr(x
[i
+1], *dipe_get_pairing(param
));
306 element_set1(x
[i
+1]);
307 element_random(y
[i
+1]);
308 element_neg(x
[i
], y
[i
+1]);
311 dipe_master_keygen(param
, 6, &pk
, &msk
);
312 dipe_keygen(param
, msk
, "1234567890abcdef", y
, &sk
);
313 overhead
= dipe_ciphertext_overhead(param
, 6, 0);
315 for (size_t clen
= overhead
; clen
<= 672; ++clen
) {
316 memset(ctxt
, 0, 672);
317 memset(ptxt
, 0, 672);
318 memcpy(ptxt
, "aaaaaaaaaaaaaaaabbbbbbbbbbbb", 28);
320 dipe_encrypt(param
, 1, &pk
, 1, x
, 28, ptxt
, clen
, ctxt
);
321 ptxt_len
= dipe_decrypt(param
, 1, &sk
, 0, "1234567890abcdef", y
, clen
, ctxt
, ptxt
);
323 if (clen
>= overhead
+ 28) {
324 EXPECT_EQ(ptxt_len
, 28);
325 EXPECT_STREQ(ptxt
, "aaaaaaaaaaaaaaaabbbbbbbbbbbb");
328 EXPECT_EQ(ptxt_len
, 0);
332 for (size_t i
= 0; i
< 6; ++i
) {
337 dipe_free_master_secretkey(msk
);
338 dipe_free_master_publickey(pk
);
339 dipe_free_secretkey(sk
);
342 TEST(DipeTest
, DipeDecryptSuccessTightLargeBlocksize
) {
345 dipe_master_publickey_t pk
;
346 dipe_master_secretkey_t msk
;
352 for (size_t i
= 0; i
< 6; i
+=2) {
353 element_init_Zr(y
[i
], *dipe_get_pairing(param
));
354 element_init_Zr(x
[i
], *dipe_get_pairing(param
));
355 element_init_Zr(y
[i
+1], *dipe_get_pairing(param
));
356 element_init_Zr(x
[i
+1], *dipe_get_pairing(param
));
359 element_set1(x
[i
+1]);
360 element_random(y
[i
+1]);
361 element_neg(x
[i
], y
[i
+1]);
364 dipe_master_keygen(param
, 6, &pk
, &msk
);
365 dipe_keygen(param
, msk
, "1234567890abcdef", y
, &sk
);
366 overhead
= dipe_ciphertext_overhead(param
, 6, 0);
368 char ctxt
[overhead
+ 28];
369 char ptxt
[overhead
+ 28];
372 for (size_t clen
= overhead
; clen
<= overhead
+ 28; ++clen
) {
373 memset(ctxt
, 0, overhead
+ 28);
374 memset(ptxt
, 0, overhead
+ 28);
375 memcpy(ptxt
, "aaaaaaaaaaaaaaaabbbbbbbbbbbb", 28);
377 dipe_encrypt(param
, 1, &pk
, 1, x
, 28, ptxt
, clen
, ctxt
);
378 ptxt_len
= dipe_decrypt(param
, 1, &sk
, 0, "1234567890abcdef", y
, clen
, ctxt
, ptxt
);
380 if (clen
>= overhead
+ 28) {
381 EXPECT_EQ(ptxt_len
, 28);
382 EXPECT_STREQ(ptxt
, "aaaaaaaaaaaaaaaabbbbbbbbbbbb");
385 EXPECT_EQ(ptxt_len
, 0);
389 for (size_t i
= 0; i
< 6; ++i
) {
394 dipe_free_master_secretkey(msk
);
395 dipe_free_master_publickey(pk
);
396 dipe_free_secretkey(sk
);
400 TEST(DipeTest
, DipeDecryptSuccessLargeNonBlocksize
) {
405 dipe_master_publickey_t pk
;
406 dipe_master_secretkey_t msk
;
412 for (size_t i
= 0; i
< 6; i
+=2) {
413 element_init_Zr(y
[i
], *dipe_get_pairing(param
));
414 element_init_Zr(x
[i
], *dipe_get_pairing(param
));
415 element_init_Zr(y
[i
+1], *dipe_get_pairing(param
));
416 element_init_Zr(x
[i
+1], *dipe_get_pairing(param
));
419 element_set1(x
[i
+1]);
420 element_random(y
[i
+1]);
421 element_neg(x
[i
], y
[i
+1]);
424 dipe_master_keygen(param
, 6, &pk
, &msk
);
425 dipe_keygen(param
, msk
, "1234567890abcdef", y
, &sk
);
426 overhead
= dipe_ciphertext_overhead(param
, 6, 0);
428 for (size_t clen
= overhead
; clen
<= 672; ++clen
) {
429 memset(ctxt
, 0, 672);
430 memset(ptxt
, 0, 672);
431 memcpy(ptxt
, "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccc", 35);
433 dipe_encrypt(param
, 1, &pk
, 1, x
, 35, ptxt
, clen
, ctxt
);
434 ptxt_len
= dipe_decrypt(param
, 1, &sk
, 0, "1234567890abcdef", y
, clen
, ctxt
, ptxt
);
436 if (clen
>= overhead
+ 35) {
437 EXPECT_EQ(ptxt_len
, 35);
438 EXPECT_STREQ(ptxt
, "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccc");
441 EXPECT_EQ(ptxt_len
, 0);
445 for (size_t i
= 0; i
< 6; ++i
) {
450 dipe_free_master_secretkey(msk
);
451 dipe_free_master_publickey(pk
);
452 dipe_free_secretkey(sk
);
455 TEST(DipeTest
, DipeDecryptSuccessTightLargeNonBlocksize
) {
458 dipe_master_publickey_t pk
;
459 dipe_master_secretkey_t msk
;
465 for (size_t i
= 0; i
< 6; i
+=2) {
466 element_init_Zr(y
[i
], *dipe_get_pairing(param
));
467 element_init_Zr(x
[i
], *dipe_get_pairing(param
));
468 element_init_Zr(y
[i
+1], *dipe_get_pairing(param
));
469 element_init_Zr(x
[i
+1], *dipe_get_pairing(param
));
472 element_set1(x
[i
+1]);
473 element_random(y
[i
+1]);
474 element_neg(x
[i
], y
[i
+1]);
477 dipe_master_keygen(param
, 6, &pk
, &msk
);
478 dipe_keygen(param
, msk
, "1234567890abcdef", y
, &sk
);
479 overhead
= dipe_ciphertext_overhead(param
, 6, 0);
481 char ctxt
[overhead
+ 35];
482 char ptxt
[overhead
+ 35];
485 for (size_t clen
= overhead
; clen
<= overhead
+ 35; ++clen
) {
486 memset(ctxt
, 0, overhead
+ 35);
487 memset(ptxt
, 0, overhead
+ 35);
488 memcpy(ptxt
, "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccc", 35);
490 dipe_encrypt(param
, 1, &pk
, 1, x
, 35, ptxt
, clen
, ctxt
);
491 ptxt_len
= dipe_decrypt(param
, 1, &sk
, 0, "1234567890abcdef", y
, clen
, ctxt
, ptxt
);
493 if (clen
>= overhead
+ 35) {
494 EXPECT_EQ(ptxt_len
, 35);
495 EXPECT_STREQ(ptxt
, "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccc");
498 EXPECT_EQ(ptxt_len
, 0);
502 for (size_t i
= 0; i
< 6; ++i
) {
507 dipe_free_master_secretkey(msk
);
508 dipe_free_master_publickey(pk
);
509 dipe_free_secretkey(sk
);
512 TEST(DipeTest
, DipeDecryptSuccessMultiKey
) {
515 dipe_master_publickey_t pk
[3];
516 dipe_master_secretkey_t msk
[3];
517 dipe_secretkey_t sk
[3];
522 for (size_t i
= 0; i
< 6; i
+=2) {
523 element_init_Zr(y
[i
], *dipe_get_pairing(param
));
524 element_init_Zr(x
[i
], *dipe_get_pairing(param
));
525 element_init_Zr(y
[i
+1], *dipe_get_pairing(param
));
526 element_init_Zr(x
[i
+1], *dipe_get_pairing(param
));
529 element_set1(x
[i
+1]);
530 element_random(y
[i
+1]);
531 element_neg(x
[i
], y
[i
+1]);
534 for (size_t i
= 0; i
< 3; ++i
) {
535 dipe_master_keygen(param
, 6, &pk
[i
], &msk
[i
]);
536 dipe_keygen(param
, msk
[i
], "1234567890abcdef", y
, &sk
[i
]);
538 overhead
= dipe_ciphertext_overhead(param
, 6, 0);
540 char ctxt
[overhead
+ 35];
541 char ptxt
[overhead
+ 35];
544 for (size_t clen
= overhead
; clen
<= overhead
+ 35; ++clen
) {
545 memset(ctxt
, 0, overhead
+ 35);
546 memset(ptxt
, 0, overhead
+ 35);
547 memcpy(ptxt
, "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccc", 35);
549 dipe_encrypt(param
, 3, pk
, 3, x
, 35, ptxt
, clen
, ctxt
);
550 ptxt_len
= dipe_decrypt(param
, 3, sk
, 0, "1234567890abcdef", y
, clen
, ctxt
, ptxt
);
552 if (clen
>= overhead
+ 35) {
553 EXPECT_EQ(ptxt_len
, 35);
554 EXPECT_STREQ(ptxt
, "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccc");
557 EXPECT_EQ(ptxt_len
, 0);
561 for (size_t i
= 0; i
< 6; ++i
) {
566 for (size_t i
= 0; i
< 3; ++i
) {
567 dipe_free_master_secretkey(msk
[i
]);
568 dipe_free_master_publickey(pk
[i
]);
569 dipe_free_secretkey(sk
[i
]);
573 TEST(DipeTest
, DipeDecryptSuccessThreshold
) {
576 dipe_master_publickey_t pk
[3];
577 dipe_master_secretkey_t msk
[3];
578 dipe_secretkey_t sk
[3];
583 for (size_t i
= 0; i
< 6; i
+=2) {
584 element_init_Zr(y
[i
], *dipe_get_pairing(param
));
585 element_init_Zr(x
[i
], *dipe_get_pairing(param
));
586 element_init_Zr(y
[i
+1], *dipe_get_pairing(param
));
587 element_init_Zr(x
[i
+1], *dipe_get_pairing(param
));
590 element_set1(x
[i
+1]);
591 element_random(y
[i
+1]);
592 element_neg(x
[i
], y
[i
+1]);
595 for (size_t i
= 0; i
< 3; ++i
) {
596 dipe_master_keygen(param
, 6, &pk
[i
], &msk
[i
]);
597 dipe_keygen(param
, msk
[i
], "1234567890abcdef", y
, &sk
[i
]);
599 overhead
= dipe_ciphertext_overhead(param
, 6, 2);
601 char ctxt
[overhead
+ 35];
602 char ptxt
[overhead
+ 35];
605 for (size_t clen
= overhead
+20; clen
<= overhead
+ 35; ++clen
) {
606 for (size_t kid
= 0; kid
< 3; ++kid
) {
607 memset(ctxt
, 0, overhead
+ 35);
608 memset(ptxt
, 0, overhead
+ 35);
609 memcpy(ptxt
, "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccc", 35);
611 dipe_encrypt(param
, 3, pk
, 1, x
, 35, ptxt
, clen
, ctxt
);
612 ptxt_len
= dipe_decrypt(param
, 1, &sk
[kid
], 2, "1234567890abcdef", y
, clen
, ctxt
, ptxt
);
614 if (clen
>= overhead
+ 35) {
615 EXPECT_EQ(ptxt_len
, 35);
616 EXPECT_STREQ(ptxt
, "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccc");
619 EXPECT_EQ(ptxt_len
, 0);
624 for (size_t i
= 0; i
< 6; ++i
) {
629 for (size_t i
= 0; i
< 3; ++i
) {
630 dipe_free_master_secretkey(msk
[i
]);
631 dipe_free_master_publickey(pk
[i
]);
632 dipe_free_secretkey(sk
[i
]);
639 int main(int argc
, char **argv
) {
640 ::testing::InitGoogleTest(&argc
, argv
);
642 dipe_init(fopen("../tests/a.param", "r"), ¶m
);
644 int result
= RUN_ALL_TESTS();
645 dipe_free_param(param
);