]> git.siccegge.de Git - software/DIPE.git/blob - tests/testDIPE.cpp
782409c0f554760afd2b6349add774d6c6e2ad71
[software/DIPE.git] / tests / testDIPE.cpp
1 #include <gtest/gtest.h>
2 #include <pbc/pbc.h>
3
4 #include "DIPE.h"
5
6 static dipe_param_t param;
7
8 TEST(DipeTest, DipeMasterKeygen) {
9 dipe_master_publickey_t pk;
10 dipe_master_secretkey_t sk;
11
12 dipe_master_keygen(param, 12, &pk, &sk);
13
14 dipe_free_master_secretkey(sk);
15 dipe_free_master_publickey(pk);
16 }
17
18 TEST(DipeTest, DipeKeygen) {
19 dipe_master_publickey_t pk;
20 dipe_master_secretkey_t msk;
21 dipe_secretkey_t sk;
22 element_t y[5];
23 for (size_t i = 0; i < 5; ++i) {
24 element_init_Zr(y[i], *dipe_get_pairing(param));
25 element_random(y[i]);
26 }
27
28 dipe_master_keygen(param, 5, &pk, &msk);
29 dipe_keygen(param, msk, "1234567890abcdef", y, &sk);
30
31 dipe_free_master_secretkey(msk);
32 dipe_free_master_publickey(pk);
33 dipe_free_secretkey(sk);
34
35 for (size_t i = 0; i < 5; ++i) {
36 element_clear(y[i]);
37 }
38 }
39
40 TEST(DipeTest, DipeDecapFail) {
41 dipe_ctxt_t ctxt;
42 dipe_master_publickey_t pk;
43 dipe_master_secretkey_t msk;
44 dipe_secretkey_t sk;
45
46 element_t y[5];
47 for (size_t i = 0; i < 5; ++i) {
48 element_init_Zr(y[i], *dipe_get_pairing(param));
49 element_random(y[i]);
50 }
51
52 dipe_master_keygen(param, 5, &pk, &msk);
53 dipe_keygen(param, msk, "1234567890abcdef", y, &sk);
54
55 element_t ptxt, ptxtp;
56 element_init_GT(ptxt, *dipe_get_pairing(param));
57 element_init_GT(ptxtp, *dipe_get_pairing(param));
58
59 element_random(ptxt);
60
61 dipe_encap(param, pk, y, ptxt, &ctxt);
62 dipe_decap(param, sk, "1234567890abcdef", y, ctxt, ptxtp);
63
64 EXPECT_NE(element_cmp(ptxt, ptxtp), 0);
65
66 for (size_t i = 0; i < 5; ++i) {
67 element_clear(y[i]);
68 }
69
70 element_clear(ptxt);
71 element_clear(ptxtp);
72
73 dipe_free_master_secretkey(msk);
74 dipe_free_master_publickey(pk);
75 dipe_free_secretkey(sk);
76 dipe_free_ctxt(ctxt);
77 }
78
79 TEST(DipeTest, DipeDecapTrivialSuccess) {
80 dipe_ctxt_t ctxt;
81 dipe_master_publickey_t pk;
82 dipe_master_secretkey_t msk;
83 dipe_secretkey_t sk;
84
85 element_t y[5];
86 for (size_t i = 0; i < 5; ++i) {
87 element_init_Zr(y[i], *dipe_get_pairing(param));
88 element_set0(y[i]);
89 }
90
91 dipe_master_keygen(param, 5, &pk, &msk);
92 dipe_keygen(param, msk, "1234567890abcdef", y, &sk);
93
94 element_t ptxt, ptxtp;
95 element_init_GT(ptxt, *dipe_get_pairing(param));
96 element_init_GT(ptxtp, *dipe_get_pairing(param));
97
98 element_random(ptxt);
99
100 dipe_encap(param, pk, y, ptxt, &ctxt);
101 dipe_decap(param, sk, "1234567890abcdef", y, ctxt, ptxtp);
102
103 EXPECT_EQ(element_cmp(ptxt, ptxtp), 0);
104
105 for (size_t i = 0; i < 5; ++i) {
106 element_clear(y[i]);
107 }
108
109 element_clear(ptxt);
110 element_clear(ptxtp);
111
112 dipe_free_master_secretkey(msk);
113 dipe_free_master_publickey(pk);
114 dipe_free_secretkey(sk);
115 dipe_free_ctxt(ctxt);
116 }
117
118 TEST(DipeTest, DipeDecapSuccess) {
119 dipe_ctxt_t ctxt;
120 dipe_master_publickey_t pk;
121 dipe_master_secretkey_t msk;
122 dipe_secretkey_t sk;
123
124 element_t y[6];
125 element_t x[6];
126
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));
132
133 element_set1(y[i]);
134 element_set1(x[i+1]);
135 element_random(y[i+1]);
136 element_neg(x[i], y[i+1]);
137 }
138
139 dipe_master_keygen(param, 6, &pk, &msk);
140 dipe_keygen(param, msk, "1234567890abcdef", y, &sk);
141
142 element_t ptxt, ptxtp;
143 element_init_GT(ptxt, *dipe_get_pairing(param));
144 element_init_GT(ptxtp, *dipe_get_pairing(param));
145
146 element_random(ptxt);
147
148 dipe_encap(param, pk, x, ptxt, &ctxt);
149
150 dipe_decap(param, sk, "1234567890abcdef", y, ctxt, ptxtp);
151 EXPECT_EQ(element_cmp(ptxt, ptxtp), 0);
152
153 /* non-matching CID */
154 dipe_decap(param, sk, "fedcba0987654321", y, ctxt, ptxtp);
155 EXPECT_NE(element_cmp(ptxt, ptxtp), 0);
156
157 /* inconsistent y value */
158 dipe_decap(param, sk, "1234567890abcdef", x, ctxt, ptxtp);
159 EXPECT_NE(element_cmp(ptxt, ptxtp), 0);
160
161 for (size_t i = 0; i < 6; ++i) {
162 element_clear(y[i]);
163 element_clear(x[i]);
164 }
165
166 element_clear(ptxt);
167 element_clear(ptxtp);
168
169 dipe_free_master_secretkey(msk);
170 dipe_free_master_publickey(pk);
171 dipe_free_secretkey(sk);
172 dipe_free_ctxt(ctxt);
173 }
174
175 TEST(DipeTest, DipeDecryptSuccessSmall) {
176 char ctxt[672];
177 char ptxt[672];
178 size_t ptxt_len;
179 size_t overhead;
180 dipe_master_publickey_t pk;
181 dipe_master_secretkey_t msk;
182 dipe_secretkey_t sk;
183
184 element_t y[6];
185 element_t x[6];
186
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));
192
193 element_set1(y[i]);
194 element_set1(x[i+1]);
195 element_random(y[i+1]);
196 element_neg(x[i], y[i+1]);
197 }
198
199 dipe_master_keygen(param, 6, &pk, &msk);
200 dipe_keygen(param, msk, "1234567890abcdef", y, &sk);
201 overhead = dipe_ciphertext_overhead(param, 6);
202
203 for (size_t clen = overhead; clen < 672; ++clen) {
204 memset(ctxt, 0, 672);
205 memset(ptxt, 0, 672);
206 memcpy(ptxt, "test", 4);
207
208 dipe_encrypt(param, pk, x, 4, ptxt, clen, ctxt);
209 ptxt_len = dipe_decrypt(param, sk, "1234567890abcdef", y, clen, ctxt, ptxt);
210
211 if (clen >= overhead + 4) {
212 EXPECT_EQ(ptxt_len, 4);
213 EXPECT_STREQ(ptxt, "test");
214 }
215 else {
216 EXPECT_EQ(ptxt_len, 0);
217 }
218 }
219
220 for (size_t i = 0; i < 6; ++i) {
221 element_clear(y[i]);
222 element_clear(x[i]);
223 }
224
225 dipe_free_master_secretkey(msk);
226 dipe_free_master_publickey(pk);
227 dipe_free_secretkey(sk);
228 }
229
230 TEST(DipeTest, DipeDecryptSuccessTightSmall) {
231 size_t ptxt_len;
232 size_t overhead;
233 dipe_master_publickey_t pk;
234 dipe_master_secretkey_t msk;
235 dipe_secretkey_t sk;
236
237 element_t y[6];
238 element_t x[6];
239
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));
245
246 element_set1(y[i]);
247 element_set1(x[i+1]);
248 element_random(y[i+1]);
249 element_neg(x[i], y[i+1]);
250 }
251
252 dipe_master_keygen(param, 6, &pk, &msk);
253 dipe_keygen(param, msk, "1234567890abcdef", y, &sk);
254 overhead = dipe_ciphertext_overhead(param, 6);
255
256 char ctxt[overhead + 4];
257 char ptxt[overhead + 4];
258
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);
263
264 dipe_encrypt(param, pk, x, 4, ptxt, clen, ctxt);
265 ptxt_len = dipe_decrypt(param, sk, "1234567890abcdef", y, clen, ctxt, ptxt);
266
267 if (clen >= overhead + 4) {
268 EXPECT_EQ(ptxt_len, 4);
269 EXPECT_STREQ(ptxt, "test");
270 }
271 else {
272 EXPECT_EQ(ptxt_len, 0);
273 }
274 }
275
276 for (size_t i = 0; i < 6; ++i) {
277 element_clear(y[i]);
278 element_clear(x[i]);
279 }
280
281 dipe_free_master_secretkey(msk);
282 dipe_free_master_publickey(pk);
283 dipe_free_secretkey(sk);
284 }
285
286
287 TEST(DipeTest, DipeDecryptSuccessLargeBlocksize) {
288 char ctxt[672];
289 char ptxt[672];
290 size_t ptxt_len;
291 size_t overhead;
292 dipe_master_publickey_t pk;
293 dipe_master_secretkey_t msk;
294 dipe_secretkey_t sk;
295
296 element_t y[6];
297 element_t x[6];
298
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));
304
305 element_set1(y[i]);
306 element_set1(x[i+1]);
307 element_random(y[i+1]);
308 element_neg(x[i], y[i+1]);
309 }
310
311 dipe_master_keygen(param, 6, &pk, &msk);
312 dipe_keygen(param, msk, "1234567890abcdef", y, &sk);
313 overhead = dipe_ciphertext_overhead(param, 6);
314
315 for (size_t clen = overhead; clen <= 672; ++clen) {
316 memset(ctxt, 0, 672);
317 memset(ptxt, 0, 672);
318 memcpy(ptxt, "aaaaaaaaaaaaaaaabbbbbbbbbbbb", 28);
319
320 dipe_encrypt(param, pk, x, 28, ptxt, clen, ctxt);
321 ptxt_len = dipe_decrypt(param, sk, "1234567890abcdef", y, clen, ctxt, ptxt);
322
323 if (clen >= overhead + 28) {
324 EXPECT_EQ(ptxt_len, 28);
325 EXPECT_STREQ(ptxt, "aaaaaaaaaaaaaaaabbbbbbbbbbbb");
326 }
327 else {
328 EXPECT_EQ(ptxt_len, 0);
329 }
330 }
331
332 for (size_t i = 0; i < 6; ++i) {
333 element_clear(y[i]);
334 element_clear(x[i]);
335 }
336
337 dipe_free_master_secretkey(msk);
338 dipe_free_master_publickey(pk);
339 dipe_free_secretkey(sk);
340 }
341
342 TEST(DipeTest, DipeDecryptSuccessTightLargeBlocksize) {
343 size_t ptxt_len;
344 size_t overhead;
345 dipe_master_publickey_t pk;
346 dipe_master_secretkey_t msk;
347 dipe_secretkey_t sk;
348
349 element_t y[6];
350 element_t x[6];
351
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));
357
358 element_set1(y[i]);
359 element_set1(x[i+1]);
360 element_random(y[i+1]);
361 element_neg(x[i], y[i+1]);
362 }
363
364 dipe_master_keygen(param, 6, &pk, &msk);
365 dipe_keygen(param, msk, "1234567890abcdef", y, &sk);
366 overhead = dipe_ciphertext_overhead(param, 6);
367
368 char ctxt[overhead + 28];
369 char ptxt[overhead + 28];
370
371
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);
376
377 dipe_encrypt(param, pk, x, 28, ptxt, clen, ctxt);
378 ptxt_len = dipe_decrypt(param, sk, "1234567890abcdef", y, clen, ctxt, ptxt);
379
380 if (clen >= overhead + 28) {
381 EXPECT_EQ(ptxt_len, 28);
382 EXPECT_STREQ(ptxt, "aaaaaaaaaaaaaaaabbbbbbbbbbbb");
383 }
384 else {
385 EXPECT_EQ(ptxt_len, 0);
386 }
387 }
388
389 for (size_t i = 0; i < 6; ++i) {
390 element_clear(y[i]);
391 element_clear(x[i]);
392 }
393
394 dipe_free_master_secretkey(msk);
395 dipe_free_master_publickey(pk);
396 dipe_free_secretkey(sk);
397 }
398
399
400 TEST(DipeTest, DipeDecryptSuccessLargeNonBlocksize) {
401 char ctxt[672];
402 char ptxt[672];
403 size_t ptxt_len;
404 size_t overhead;
405 dipe_master_publickey_t pk;
406 dipe_master_secretkey_t msk;
407 dipe_secretkey_t sk;
408
409 element_t y[6];
410 element_t x[6];
411
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));
417
418 element_set1(y[i]);
419 element_set1(x[i+1]);
420 element_random(y[i+1]);
421 element_neg(x[i], y[i+1]);
422 }
423
424 dipe_master_keygen(param, 6, &pk, &msk);
425 dipe_keygen(param, msk, "1234567890abcdef", y, &sk);
426 overhead = dipe_ciphertext_overhead(param, 6);
427
428 for (size_t clen = overhead; clen <= 672; ++clen) {
429 memset(ctxt, 0, 672);
430 memset(ptxt, 0, 672);
431 memcpy(ptxt, "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccc", 35);
432
433 dipe_encrypt(param, pk, x, 35, ptxt, clen, ctxt);
434 ptxt_len = dipe_decrypt(param, sk, "1234567890abcdef", y, clen, ctxt, ptxt);
435
436 if (clen >= overhead + 35) {
437 EXPECT_EQ(ptxt_len, 35);
438 EXPECT_STREQ(ptxt, "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccc");
439 }
440 else {
441 EXPECT_EQ(ptxt_len, 0);
442 }
443 }
444
445 for (size_t i = 0; i < 6; ++i) {
446 element_clear(y[i]);
447 element_clear(x[i]);
448 }
449
450 dipe_free_master_secretkey(msk);
451 dipe_free_master_publickey(pk);
452 dipe_free_secretkey(sk);
453 }
454
455 TEST(DipeTest, DipeDecryptSuccessTightLargeNonBlocksize) {
456 size_t ptxt_len;
457 size_t overhead;
458 dipe_master_publickey_t pk;
459 dipe_master_secretkey_t msk;
460 dipe_secretkey_t sk;
461
462 element_t y[6];
463 element_t x[6];
464
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));
470
471 element_set1(y[i]);
472 element_set1(x[i+1]);
473 element_random(y[i+1]);
474 element_neg(x[i], y[i+1]);
475 }
476
477 dipe_master_keygen(param, 6, &pk, &msk);
478 dipe_keygen(param, msk, "1234567890abcdef", y, &sk);
479 overhead = dipe_ciphertext_overhead(param, 6);
480
481 char ctxt[overhead + 35];
482 char ptxt[overhead + 35];
483
484
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);
489
490 dipe_encrypt(param, pk, x, 35, ptxt, clen, ctxt);
491 ptxt_len = dipe_decrypt(param, sk, "1234567890abcdef", y, clen, ctxt, ptxt);
492
493 if (clen >= overhead + 35) {
494 EXPECT_EQ(ptxt_len, 35);
495 EXPECT_STREQ(ptxt, "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccc");
496 }
497 else {
498 EXPECT_EQ(ptxt_len, 0);
499 }
500 }
501
502 for (size_t i = 0; i < 6; ++i) {
503 element_clear(y[i]);
504 element_clear(x[i]);
505 }
506
507 dipe_free_master_secretkey(msk);
508 dipe_free_master_publickey(pk);
509 dipe_free_secretkey(sk);
510 }
511
512
513
514
515 int main(int argc, char **argv) {
516 ::testing::InitGoogleTest(&argc, argv);
517
518 dipe_init(fopen("../tests/a.param", "r"), &param);
519
520 int result = RUN_ALL_TESTS();
521 dipe_free_param(param);
522 return result;
523 }