aboutsummaryrefslogtreecommitdiff
path: root/test_crypto.c
blob: 6a49927631cd81e3849df957aff07fcd1d68f831 (plain) (blame)
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
/* This file is part of libbrandt.
 * Copyright (C) 2016 GNUnet e.V.
 *
 * libbrandt is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * libbrandt is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * libbrandt.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file test_crypto.c
 * @brief testing crypto and smc functions.
 * @author Markus Teich
 */

#include "platform.h"

/* For testing static functions and variables we include the whole source */
#include "crypto.c"

#include "test.h"


static uint16_t              bidders;
static uint16_t              prizes;
static struct BRANDT_Auction *ad;

static int
test_serialization ()
{
	gcry_mpi_point_t oldp = gcry_mpi_point_new (0);
	gcry_mpi_point_t newp = gcry_mpi_point_new (0);
	gcry_mpi_t       oldi = gcry_mpi_new (256);
	gcry_mpi_t       newi = gcry_mpi_new (256);
	struct ec_mpi    serp;
	struct ec_mpi    seri;

	ec_keypair_create (oldp, oldi);

	ec_point_serialize (&serp, oldp);
	mpi_serialize (&seri, oldi);

	ec_point_parse (newp, &serp);
	mpi_parse (newi, &seri);

	CHECK (!ec_point_cmp (oldp, newp), "serialization changed point");
	CHECK (!gcry_mpi_cmp (oldi, newi), "serialization changed mpi");

	mpi_serialize (&seri, GCRYMPI_CONST_ONE);
	mpi_parse (newi, &seri);
	CHECK (!gcry_mpi_cmp (GCRYMPI_CONST_ONE, newi), "serializing mpi 1 fail");

	gcry_mpi_point_release (oldp);
	gcry_mpi_point_release (newp);
	gcry_mpi_release (oldi);
	gcry_mpi_release (newi);
	return 1;
}


static int
test_smc_zkp_dl ()
{
	struct proof_dl  proof;
	gcry_mpi_t       x = gcry_mpi_new (256);
	gcry_mpi_point_t v = gcry_mpi_point_new (0);

	ec_skey_create (x);

	smc_zkp_dl (v, x, &proof);
	CHECK (gcry_mpi_ec_curve_point (v, ec_ctx), "not on curve");
	CHECK (!smc_zkp_dl_check (v, &proof), "zkp dl wrong");

	gcry_mpi_release (x);
	gcry_mpi_point_release (v);
	return 1;
}


static int
test_smc_zkp_2dle ()
{
	struct proof_2dle proof;
	gcry_mpi_t        x = gcry_mpi_new (256);
	gcry_mpi_point_t  g1 = gcry_mpi_point_new (0);
	gcry_mpi_point_t  g2 = gcry_mpi_point_new (0);
	gcry_mpi_point_t  v = gcry_mpi_point_new (0);
	gcry_mpi_point_t  w = gcry_mpi_point_new (0);

	ec_keypair_create (g1, x);
	ec_keypair_create (g2, x);

	smc_zkp_2dle (v, w, g1, g2, x, &proof);
	CHECK (gcry_mpi_ec_curve_point (g1, ec_ctx), "not on curve");
	CHECK (gcry_mpi_ec_curve_point (g2, ec_ctx), "not on curve");
	CHECK (gcry_mpi_ec_curve_point (v, ec_ctx), "not on curve");
	CHECK (gcry_mpi_ec_curve_point (w, ec_ctx), "not on curve");
	CHECK (!smc_zkp_2dle_check (v, w, g1, g2, &proof), "zkp 2dle wrong");

	gcry_mpi_release (x);
	gcry_mpi_point_release (g1);
	gcry_mpi_point_release (g2);
	gcry_mpi_point_release (v);
	gcry_mpi_point_release (w);
	return 1;
}


static int
test_smc_zkp_0og ()
{
	struct proof_0og proof;
	gcry_mpi_point_t y = gcry_mpi_point_new (0);
	gcry_mpi_point_t alpha = gcry_mpi_point_new (0);
	gcry_mpi_point_t beta = gcry_mpi_point_new (0);

	/* get random public key point. We don't need the secret key to check the
	 * proof here */
	ec_keypair_create (y, NULL);

	smc_zkp_0og (tests_run % 2, y, NULL, alpha, beta, &proof);
	CHECK (gcry_mpi_ec_curve_point (alpha, ec_ctx), "not on curve");
	CHECK (gcry_mpi_ec_curve_point (beta, ec_ctx), "not on curve");
	CHECK (!smc_zkp_0og_check (y, alpha, beta, &proof), "zkp 0og is wrong");

	gcry_mpi_point_release (y);
	gcry_mpi_point_release (alpha);
	gcry_mpi_point_release (beta);
	return 1;
}


static int
test_setup_auction_data ()
{
	uint16_t i;

	ad = GNUNET_new_array (bidders, struct BRANDT_Auction);

	for (i = 0; i < bidders; i++)
	{
		ad[i].n = bidders;
		ad[i].i = i;
		ad[i].k = prizes;
		ad[i].b = i;
	}
	return 1;
}


/**
 * compute round @a index of the protocol specified by @a type and @a oc
 *
 * @param[in] type auction type
 * @param[in] oc outcome type
 * @param[in] index round index
 */
#define ROUND(type, oc, index) do { \
		for (uint16_t i = 0; i < bidders; i++) \
		{ \
			handler_prep[type][oc][index] (&ad[i]); \
			bufs[i] = handler_out[type][oc][index] (&ad[i], &lens[i]); \
			CHECK (bufs[i], "failed to gen message buffer"); \
		} \
 \
		for (uint16_t i = 0; i < bidders; i++) \
		{ \
			/* enable seller mode for receiving decryption messages */ \
			if (msg_decrypt == index) \
				ad[i].seller_mode = 1; \
			for (uint16_t s = 0; s < bidders; s++) \
			{ \
				if (s == i) \
					continue; \
				CHECK (handler_in[type][oc][index] (&ad[i], \
				                                    bufs[s] + \
				                                    sizeof (struct msg_head), \
				                                    lens[s] - \
				                                    sizeof (struct msg_head), \
				                                    s), \
				       "failed to parse message buffer"); \
			} \
			/* disable seller mode again */ \
			if (msg_decrypt == index) \
				ad[i].seller_mode = 0; \
		} \
 \
		for (uint16_t i = 0; i < bidders; i++) \
			free (bufs[i]); \
} while (0)


static int
test_auction (enum auction_type atype, enum outcome_type oc)
{
	unsigned char *bufs[bidders];
	size_t        lens[bidders];
	int32_t       winner = -1;
	int32_t       price = -1;

	weprintf ("testing auction type %d and outcome format %d...", atype, oc);
	ROUND (atype, oc, msg_init);
	ROUND (atype, oc, msg_bid);
	ROUND (atype, oc, msg_outcome);
	ROUND (atype, oc, msg_decrypt);

	/* outcome */
	for (uint16_t i = 0; i < ad->n; i++)
	{
		struct BRANDT_Result *res;
		uint16_t             reslen;

		res = handler_res[atype][oc] (&ad[i], &reslen);
		if (res && -1 == price && -1 != res->price)
			price = res->price;
		if (res)
			weprintf ("price: %d", res->price);
		CHECK (!res || res->price == price, "different prices detected");
		if (res && -1 == winner && -1 != res->bidder)
			winner = res->bidder;
		CHECK (!res || res->bidder == winner, "different winners detected");
	}

	CHECK (-1 != winner, "no winner detected");
	CHECK (-1 != price, "no price detected");
	fputs ("good: one winner detected\n", stderr);
	return 1;
}


static void
cleanup_auction_data ()
{
	for (uint16_t i = 0; i < bidders; i++)
	{
		gcry_mpi_point_release (ad[i].Y);
		gcry_mpi_release (ad[i].x);
		smc_free1 (ad[i].y, ad[i].n);
		smc_free2 (ad[i].alpha, ad[i].n, ad[i].k);
		smc_free2 (ad[i].beta, ad[i].n, ad[i].k);
		smc_free2 (ad[i].gamma2, ad[i].n, ad[i].k);
		smc_free2 (ad[i].delta2, ad[i].n, ad[i].k);
		smc_free2 (ad[i].phi2, ad[i].n, ad[i].k);
		smc_free3 (ad[i].gamma3, ad[i].n, ad[i].n, ad[i].k);
		smc_free3 (ad[i].delta3, ad[i].n, ad[i].n, ad[i].k);
		smc_free3 (ad[i].phi3, ad[i].n, ad[i].n, ad[i].k);
		smc_free1 (ad[i].tmpa1, ad[i].k);
		smc_free1 (ad[i].tmpb1, ad[i].k);
	}
	free (ad);
}


static int
test_all_auctions ()
{
	for (size_t atype = 0; atype < auction_last; atype++)
	{
		if (auction_firstPrice != atype) /* others not yet implemented */
			continue;

		for (size_t oc = 0; oc < outcome_last; oc++)
		{
			if (!test_setup_auction_data () || !test_auction (atype, oc))
			{
				cleanup_auction_data ();
				return 0;
			}
			cleanup_auction_data ();
		}
	}

	return 1;
}


int
main (int argc, char *argv[])
{
	int                                 repeat = 1;
	struct GNUNET_CRYPTO_EccDlogContext *edc;

	bidders = 3;
	prizes = 3;

	edc = GNUNET_CRYPTO_ecc_dlog_prepare (1024, 16);
	BRANDT_init (edc);

	/* tests that need to run only once */
	RUN (test_serialization);

	for (tests_run = 0; tests_run < repeat; tests_run++)
	{
		RUN (test_smc_zkp_dl);
		RUN (test_smc_zkp_2dle);
		RUN (test_smc_zkp_0og);
	}

	RUN (test_all_auctions);

	GNUNET_CRYPTO_ecc_dlog_release (edc);
	return ret;
}