aboutsummaryrefslogtreecommitdiff
path: root/crypto.h
blob: 4ba850ecce791ceab6bbb6d0c6fd6cd771e018e9 (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
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
/* 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 crypto.h
 * @brief Interface of the crypto primitives.
 * @author Markus Teich
 */

#ifndef _BRANDT_CRYPTO_H
#define _BRANDT_CRYPTO_H

#include <gcrypt.h>
#include <stdint.h>

#include <gnunet/gnunet_util_lib.h>

#include "internals.h"

void brandt_crypto_init ();


/* --- EC --- */

/* used for serialized mpis and serialized curve points (they are the same size
 * when compressed) */
struct ec_mpi {
	unsigned char data[256 / 8];
};

void ec_point_serialize (struct ec_mpi *dst, const gcry_mpi_point_t src);
void ec_point_parse (gcry_mpi_point_t dst, const struct ec_mpi *src);

gcry_mpi_point_t *smc_init1 (uint16_t size1);
gcry_mpi_point_t **smc_init2 (uint16_t size1, uint16_t size2);
gcry_mpi_point_t ***smc_init3 (uint16_t size1, uint16_t size2, uint16_t size3);
void smc_free1 (gcry_mpi_point_t *dst, uint16_t size1);
void smc_free2 (gcry_mpi_point_t **dst, uint16_t size1, uint16_t size2);
void smc_free3 (gcry_mpi_point_t ***dst,
                uint16_t         size1,
                uint16_t         size2,
                uint16_t         size3);

void smc_sums_partial (gcry_mpi_point_t out[],
                       gcry_mpi_point_t in[],
                       uint16_t         len,
                       uint16_t         stepi,
                       uint16_t         stepo);
void smc_sum (gcry_mpi_point_t out,
              gcry_mpi_point_t in[],
              uint16_t         len,
              uint16_t         step);

void ec_point_copy (gcry_mpi_point_t dst, const gcry_mpi_point_t src);
int ec_point_cmp (const gcry_mpi_point_t a, const gcry_mpi_point_t b);
void ec_skey_create (gcry_mpi_t skey);
void ec_keypair_create (gcry_mpi_point_t pkey, gcry_mpi_t skey);
void ec_keypair_create_base (gcry_mpi_point_t       pkey,
                             gcry_mpi_t             skey,
                             const gcry_mpi_point_t base);


/* --- Zero knowledge proofs --- */

struct proof_dl {
	struct ec_mpi r;
	struct ec_mpi a;
};

struct proof_2dle {
	struct ec_mpi r;
	struct ec_mpi a;
	struct ec_mpi b;
};

struct proof_0og {
	struct ec_mpi a1;
	struct ec_mpi a2;
	struct ec_mpi b1;
	struct ec_mpi b2;
	struct ec_mpi d1;
	struct ec_mpi d2;
	struct ec_mpi r1;
	struct ec_mpi r2;
};

void smc_zkp_dl (gcry_mpi_point_t v,
                 const gcry_mpi_t x,
                 struct proof_dl  *proof);
int smc_zkp_dl_check (const gcry_mpi_point_t v,
                      const struct proof_dl  *proof);

void smc_zkp_2dle (const gcry_mpi_point_t v,
                   const gcry_mpi_point_t w,
                   const gcry_mpi_point_t g1,
                   const gcry_mpi_point_t g2,
                   const gcry_mpi_t       x,
                   struct proof_2dle      *proof);
int smc_zkp_2dle_check (const gcry_mpi_point_t  v,
                        const gcry_mpi_point_t  w,
                        const gcry_mpi_point_t  g1,
                        const gcry_mpi_point_t  g2,
                        const struct proof_2dle *proof);

void smc_zkp_0og (int                    m_is_gen,
                  const gcry_mpi_point_t y,
                  gcry_mpi_t             r,
                  gcry_mpi_point_t       alpha,
                  gcry_mpi_point_t       beta,
                  struct proof_0og       *proof);
int smc_zkp_0og_check (const gcry_mpi_point_t y,
                       const gcry_mpi_point_t alpha,
                       const gcry_mpi_point_t beta,
                       const struct proof_0og *proof);


/* --- Protocol implementation --- */

void smc_prep_keyshare (struct BRANDT_Auction *ad);
unsigned char *smc_gen_keyshare (struct BRANDT_Auction *ad, size_t *buflen);
int smc_recv_keyshare (struct BRANDT_Auction *ad,
                       const unsigned char   *buf,
                       size_t                buflen,
                       uint16_t              sender_index);

void smc_prep_bid (struct BRANDT_Auction *ad);
unsigned char *smc_encrypt_bid (struct BRANDT_Auction *ad, size_t *buflen);
int smc_recv_encrypted_bid (struct BRANDT_Auction *ad,
                            const unsigned char   *buf,
                            size_t                buflen,
                            uint16_t              sender_index);

void fp_priv_prep_outcome (struct BRANDT_Auction *ad);
unsigned char *fp_priv_compute_outcome (struct BRANDT_Auction *ad,
                                        size_t                *buflen);
int fp_priv_recv_outcome (struct BRANDT_Auction *ad,
                          const unsigned char   *buf,
                          size_t                buflen,
                          uint16_t              sender);

void fp_priv_prep_decryption (struct BRANDT_Auction *ad);
unsigned char *fp_priv_decrypt_outcome (struct BRANDT_Auction *ad,
                                        size_t                *buflen);
int fp_priv_recv_decryption (struct BRANDT_Auction *ad,
                             const unsigned char   *buf,
                             size_t                buflen,
                             uint16_t              sender);

struct BRANDT_Result *fp_priv_determine_outcome (struct BRANDT_Auction *ad,
                                                 uint16_t              *len);

void fp_pub_prep_outcome (struct BRANDT_Auction *ad);
unsigned char *fp_pub_compute_outcome (struct BRANDT_Auction *ad,
                                       size_t                *buflen);
int fp_pub_recv_outcome (struct BRANDT_Auction *ad,
                         const unsigned char   *buf,
                         size_t                buflen,
                         uint16_t              sender);

void fp_pub_prep_decryption (struct BRANDT_Auction *ad);
unsigned char *fp_pub_decrypt_outcome (struct BRANDT_Auction *ad,
                                       size_t                *buflen);
int fp_pub_recv_decryption (struct BRANDT_Auction *ad,
                            const unsigned char   *buf,
                            size_t                buflen,
                            uint16_t              sender);

struct BRANDT_Result *fp_pub_determine_outcome (struct BRANDT_Auction *ad,
                                                uint16_t              *len);


void mp_priv_prep_outcome (struct BRANDT_Auction *ad);

struct BRANDT_Result *mp_priv_determine_outcome (struct BRANDT_Auction *ad,
                                                 uint16_t              *len);


void mp_pub_prep_outcome (struct BRANDT_Auction *ad);
unsigned char *mp_pub_compute_outcome (struct BRANDT_Auction *ad,
                                       size_t                *buflen);
int mp_pub_recv_outcome (struct BRANDT_Auction *ad,
                         const unsigned char   *buf,
                         size_t                buflen,
                         uint16_t              sender);

void mp_pub_prep_decryption (struct BRANDT_Auction *ad);
unsigned char *mp_pub_decrypt_outcome (struct BRANDT_Auction *ad,
                                       size_t                *buflen);
int mp_pub_recv_decryption (struct BRANDT_Auction *ad,
                            const unsigned char   *buf,
                            size_t                buflen,
                            uint16_t              sender);

struct BRANDT_Result *mp_pub_determine_outcome (struct BRANDT_Auction *ad,
                                                uint16_t              *len);


/* --- Round dictionaries --- */

typedef void
(*RoundPrep)(struct BRANDT_Auction *ad);

typedef int
(*MsgIn)(struct BRANDT_Auction *ad,
         const unsigned char   *buf,
         size_t                buflen,
         uint16_t              sender);


typedef unsigned char *
(*MsgOut)(struct BRANDT_Auction *ad,
          size_t                *buflen);


/**
 * Functions of this type determine the outcome of an auction.
 *
 * \todo: export *proof* of erroneous behaviour / outcome.
 *
 * @param[in] ad Pointer to the auction data struct.
 * @param[out] len Amount of items in @a results.
 * @return An array of results for one or more bidders. Each bidder
 * will only be listed once.
 */
typedef struct BRANDT_Result *
(*Result)(struct BRANDT_Auction *ad,
          uint16_t              *len);


static const RoundPrep handler_prep[auction_last][outcome_last][msg_last] = {
	[auction_firstPrice] =      {
		[outcome_private] =     {
			[msg_init] = &smc_prep_keyshare,
			[msg_bid] = &smc_prep_bid,
			[msg_outcome] = &fp_priv_prep_outcome,
			[msg_decrypt] = &fp_priv_prep_decryption,
		},
		[outcome_public] =      {
			[msg_init] = &smc_prep_keyshare,
			[msg_bid] = &smc_prep_bid,
			[msg_outcome] = &fp_pub_prep_outcome,
			[msg_decrypt] = &fp_pub_prep_decryption,
		},
	},
	[auction_mPlusFirstPrice] = {
		[outcome_private] =     {
			[msg_init] = &smc_prep_keyshare,
			[msg_bid] = &smc_prep_bid,
			[msg_outcome] = &mp_priv_prep_outcome,
			[msg_decrypt] = &fp_priv_prep_decryption,
		},
		[outcome_public] =      {
			[msg_init] = &smc_prep_keyshare,
			[msg_bid] = &smc_prep_bid,
			[msg_outcome] = &mp_pub_prep_outcome,
			[msg_decrypt] = &mp_pub_prep_decryption,
		},
	},
};


/**
 * stores the function pointers to receive functions for each state.
 *
 * The first index denotes if a first price auction or a M+1st price auction is
 * used. If it is 0, it is a first price auction, if it is 1, it is a M+1st
 * price auction.
 *
 * The second index denotes if the outcome should be public or private. A value
 * of 0 means a private outcome, while a value of 1 means public outcome.
 */
static const MsgIn handler_in[auction_last][outcome_last][msg_last] = {
	[auction_firstPrice] =      {
		[outcome_private] =     {
			[msg_init] = &smc_recv_keyshare,
			[msg_bid] = &smc_recv_encrypted_bid,
			[msg_outcome] = &fp_priv_recv_outcome,
			[msg_decrypt] = &fp_priv_recv_decryption,
		},
		[outcome_public] =      {
			[msg_init] = &smc_recv_keyshare,
			[msg_bid] = &smc_recv_encrypted_bid,
			[msg_outcome] = &fp_pub_recv_outcome,
			[msg_decrypt] = &fp_pub_recv_decryption,
		},
	},
	[auction_mPlusFirstPrice] = {
		[outcome_private] =     {
			[msg_init] = &smc_recv_keyshare,
			[msg_bid] = &smc_recv_encrypted_bid,
			[msg_outcome] = &fp_priv_recv_outcome,
			[msg_decrypt] = &fp_priv_recv_decryption,
		},
		[outcome_public] =      {
			[msg_init] = &smc_recv_keyshare,
			[msg_bid] = &smc_recv_encrypted_bid,
			[msg_outcome] = &mp_pub_recv_outcome,
			[msg_decrypt] = &mp_pub_recv_decryption,
		},
	},
};


/**
 * stores the function pointers to message buffer creating functions for each
 * state.
 *
 * The first index denotes if a first price auction or a M+1st price auction is
 * used. If it is 0, it is a first price auction, if it is 1, it is a M+1st
 * price auction.
 *
 * The second index denotes if the outcome should be public or private. A value
 * of 0 means a private outcome, while a value of 1 means public outcome.
 */
static const MsgOut handler_out[auction_last][outcome_last][msg_last] = {
	[auction_firstPrice] =      {
		[outcome_private] =     {
			[msg_init] = &smc_gen_keyshare,
			[msg_bid] = &smc_encrypt_bid,
			[msg_outcome] = &fp_priv_compute_outcome,
			[msg_decrypt] = &fp_priv_decrypt_outcome,
		},
		[outcome_public] =      {
			[msg_init] = &smc_gen_keyshare,
			[msg_bid] = &smc_encrypt_bid,
			[msg_outcome] = &fp_pub_compute_outcome,
			[msg_decrypt] = &fp_pub_decrypt_outcome,
		},
	},
	[auction_mPlusFirstPrice] = {
		[outcome_private] =     {
			[msg_init] = &smc_gen_keyshare,
			[msg_bid] = &smc_encrypt_bid,
			[msg_outcome] = &fp_priv_compute_outcome,
			[msg_decrypt] = &fp_priv_decrypt_outcome,
		},
		[outcome_public] =      {
			[msg_init] = &smc_gen_keyshare,
			[msg_bid] = &smc_encrypt_bid,
			[msg_outcome] = &mp_pub_compute_outcome,
			[msg_decrypt] = &mp_pub_decrypt_outcome,
		},
	},
};

/**
 * stores the function pointers to outcome determination functions for each
 * auction mode.
 *
 * The first index denotes if a first price auction or a M+1st price auction is
 * used. If it is 0, it is a first price auction, if it is 1, it is a M+1st
 * price auction.
 *
 * The second index denotes if the outcome should be public or private. A value
 * of 0 means a private outcome, while a value of 1 means public outcome.
 */
static const Result handler_res[auction_last][outcome_last] = {
	[auction_firstPrice] =      {
		[outcome_private] = &fp_priv_determine_outcome,
		[outcome_public] = &fp_pub_determine_outcome,
	},
	[auction_mPlusFirstPrice] = {
		[outcome_private] = &mp_priv_determine_outcome,
		[outcome_public] = &mp_pub_determine_outcome,
	},
};



#endif /* ifndef _BRANDT_CRYPTO_H */