diff options
author | Markus Teich <markus.teich@stusta.mhn.de> | 2016-06-21 00:20:47 +0200 |
---|---|---|
committer | Markus Teich <markus.teich@stusta.mhn.de> | 2016-06-21 00:20:47 +0200 |
commit | 24191a69683ca8fb7d01c26ec889f13a3f7d8ba8 (patch) | |
tree | a390dd517ebd46056d900a1d5e67898d9dec8495 /crypto.c | |
parent | 5e2d5638614454d37c007d76b289bc871ae5287f (diff) | |
download | libbrandt-24191a69683ca8fb7d01c26ec889f13a3f7d8ba8.tar.gz libbrandt-24191a69683ca8fb7d01c26ec889f13a3f7d8ba8.zip |
add (de)serialization + test. add some docu and stubs
Diffstat (limited to 'crypto.c')
-rw-r--r-- | crypto.c | 372 |
1 files changed, 266 insertions, 106 deletions
@@ -88,89 +88,6 @@ brandt_hash (const void *block, size_t size, struct brandt_hash_code *ret) | |||
88 | } | 88 | } |
89 | 89 | ||
90 | 90 | ||
91 | /* --- MPI --- */ | ||
92 | |||
93 | /** | ||
94 | * If target != size, move @a target bytes to the end of the size-sized | ||
95 | * buffer and zero out the first @a target - @a size bytes. | ||
96 | * | ||
97 | * @param buf original buffer | ||
98 | * @param size number of bytes in @a buf | ||
99 | * @param target target size of the buffer | ||
100 | */ | ||
101 | static void | ||
102 | adjust (void *buf, size_t size, size_t target) | ||
103 | { | ||
104 | char *p = buf; | ||
105 | |||
106 | if (size < target) | ||
107 | { | ||
108 | memmove (&p[target - size], buf, size); | ||
109 | memset (buf, 0, target - size); | ||
110 | } | ||
111 | } | ||
112 | |||
113 | |||
114 | /** | ||
115 | * Output the given MPI value to the given buffer in | ||
116 | * network byte order. | ||
117 | * The MPI @a val may not be negative. | ||
118 | * | ||
119 | * @param buf where to output to | ||
120 | * @param size number of bytes in @a buf | ||
121 | * @param val value to write to @a buf | ||
122 | */ | ||
123 | void | ||
124 | brandt_mpi_print_unsigned (void *buf, size_t size, gcry_mpi_t val) | ||
125 | { | ||
126 | size_t rsize; | ||
127 | gcry_error_t rc; | ||
128 | |||
129 | if (gcry_mpi_get_flag (val, GCRYMPI_FLAG_OPAQUE)) | ||
130 | { | ||
131 | /* Store opaque MPIs left aligned into the buffer. */ | ||
132 | unsigned int nbits; | ||
133 | const void *p; | ||
134 | |||
135 | p = gcry_mpi_get_opaque (val, &nbits); | ||
136 | brandt_assert (NULL != p); | ||
137 | rsize = (nbits + 7) / 8; | ||
138 | if (rsize > size) | ||
139 | rsize = size; | ||
140 | memcpy (buf, p, rsize); | ||
141 | if (rsize < size) | ||
142 | memset (((char *)buf) + rsize, 0, size - rsize); | ||
143 | } | ||
144 | else | ||
145 | { | ||
146 | /* Store regular MPIs as unsigned integers right aligned into the buffer. */ | ||
147 | rsize = size; | ||
148 | rc = gcry_mpi_print (GCRYMPI_FMT_USG, buf, rsize, &rsize, val); | ||
149 | brandt_assert_gpgerr (rc); | ||
150 | adjust (buf, rsize, size); | ||
151 | } | ||
152 | } | ||
153 | |||
154 | |||
155 | /** | ||
156 | * Convert data buffer into MPI value. | ||
157 | * The buffer is interpreted as network | ||
158 | * byte order, unsigned integer. | ||
159 | * | ||
160 | * @param result where to store MPI value (allocated) | ||
161 | * @param data raw data (GCRYMPI_FMT_USG) | ||
162 | * @param size number of bytes in @a data | ||
163 | */ | ||
164 | void | ||
165 | brandt_mpi_scan_unsigned (gcry_mpi_t *result, const void *data, size_t size) | ||
166 | { | ||
167 | gcry_error_t rc; | ||
168 | |||
169 | rc = gcry_mpi_scan (result, GCRYMPI_FMT_USG, data, size, &size); | ||
170 | brandt_assert_gpgerr (rc); | ||
171 | } | ||
172 | |||
173 | |||
174 | /* --- EC --- */ | 91 | /* --- EC --- */ |
175 | 92 | ||
176 | /** | 93 | /** |
@@ -289,6 +206,145 @@ ec_point_cmp (const gcry_mpi_point_t a, const gcry_mpi_point_t b) | |||
289 | } | 206 | } |
290 | 207 | ||
291 | 208 | ||
209 | /** | ||
210 | * mpi_serialize outputs the given MPI value to the given destination buffer in | ||
211 | * network byte order. The MPI @a src may not be negative. | ||
212 | * | ||
213 | * @param[out] dst where to output to | ||
214 | * @param[in] src value to write to @a dst | ||
215 | */ | ||
216 | void | ||
217 | mpi_serialize (struct ec_mpi *dst, gcry_mpi_t src) | ||
218 | { | ||
219 | size_t rsize = 0; | ||
220 | unsigned int nbits; | ||
221 | const void *p; | ||
222 | gcry_error_t rc; | ||
223 | |||
224 | if (gcry_mpi_get_flag (src, GCRYMPI_FLAG_OPAQUE)) | ||
225 | { | ||
226 | /* Store opaque MPIs left aligned into the buffer. Used by Ed25519 point | ||
227 | * compression */ | ||
228 | p = gcry_mpi_get_opaque (src, &nbits); | ||
229 | brandt_assert (p); | ||
230 | rsize = (nbits + 7) / 8; | ||
231 | if (rsize > sizeof (struct ec_mpi)) | ||
232 | rsize = sizeof (struct ec_mpi); | ||
233 | memcpy (dst, p, rsize); | ||
234 | if (rsize < sizeof (struct ec_mpi)) | ||
235 | memset (((char *)dst) + rsize, 0, sizeof (struct ec_mpi) - rsize); | ||
236 | } | ||
237 | else | ||
238 | { | ||
239 | /* Store regular MPIs as unsigned ints right aligned into the buffer. */ | ||
240 | rc = gcry_mpi_print (GCRYMPI_FMT_USG, (void *)dst, | ||
241 | sizeof (struct ec_mpi), &rsize, src); | ||
242 | brandt_assert_gpgerr (rc); | ||
243 | |||
244 | /* Shift the output to the right, if shorter than available space */ | ||
245 | if (rsize && rsize < sizeof (struct ec_mpi)) | ||
246 | { | ||
247 | memmove (&dst[sizeof (struct ec_mpi) - rsize], dst, rsize); | ||
248 | memset (dst, 0, sizeof (struct ec_mpi) - rsize); | ||
249 | } | ||
250 | } | ||
251 | } | ||
252 | |||
253 | |||
254 | /** | ||
255 | * mpi_parse converts src buffer into MPI value. | ||
256 | * The buffer is interpreted as network byte order, unsigned integer. | ||
257 | * | ||
258 | * @param[out] dst where to store MPI value. Must be initialized. | ||
259 | * @param[in] src raw data source (GCRYMPI_FMT_USG) | ||
260 | */ | ||
261 | void | ||
262 | mpi_parse (gcry_mpi_t dst, const struct ec_mpi *src) | ||
263 | { | ||
264 | gcry_mpi_t ret; | ||
265 | gcry_error_t rc; | ||
266 | |||
267 | rc = gcry_mpi_scan (&ret, GCRYMPI_FMT_USG, | ||
268 | src, sizeof (struct ec_mpi), NULL); | ||
269 | brandt_assert_gpgerr (rc); | ||
270 | |||
271 | gcry_mpi_snatch (dst, ret); | ||
272 | } | ||
273 | |||
274 | |||
275 | /** | ||
276 | * ec_point_serialize outputs the given curve point to the @a dst buffer. | ||
277 | * | ||
278 | * @param[out] dst where to write the raw data to | ||
279 | * @param[in] src curve point to write to @a dst | ||
280 | */ | ||
281 | void | ||
282 | ec_point_serialize (struct ec_mpi *dst, const gcry_mpi_point_t src) | ||
283 | { | ||
284 | gcry_sexp_t s; | ||
285 | gcry_ctx_t ctx; | ||
286 | gcry_error_t rc; | ||
287 | gcry_mpi_t q; | ||
288 | |||
289 | brandt_assert (dst); | ||
290 | |||
291 | rc = gcry_sexp_build (&s, NULL, "(public-key(ecc(curve " CURVE ")))"); | ||
292 | brandt_assert_gpgerr (rc); | ||
293 | brandt_assert (NULL != s); | ||
294 | |||
295 | rc = gcry_mpi_ec_new (&ctx, s, NULL); | ||
296 | brandt_assert_gpgerr (rc); | ||
297 | gcry_sexp_release (s); | ||
298 | |||
299 | rc = gcry_mpi_ec_set_point ("q", src, ctx); | ||
300 | brandt_assert_gpgerr (rc); | ||
301 | |||
302 | q = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0); | ||
303 | brandt_assert (NULL != q); | ||
304 | gcry_ctx_release (ctx); | ||
305 | |||
306 | mpi_serialize (dst, q); | ||
307 | gcry_mpi_release (q); | ||
308 | } | ||
309 | |||
310 | |||
311 | /** | ||
312 | * ec_point_parse parses a point on the Ed25519 curve from @a src into @a dst. | ||
313 | * | ||
314 | * @param[out] dst where to store the curve point. Must be initialized | ||
315 | * @param[in] src raw data source | ||
316 | */ | ||
317 | void | ||
318 | ec_point_parse (gcry_mpi_point_t dst, const struct ec_mpi *src) | ||
319 | { | ||
320 | gcry_sexp_t s; | ||
321 | gcry_ctx_t ctx; | ||
322 | gcry_mpi_point_t ret; | ||
323 | gcry_error_t rc; | ||
324 | |||
325 | rc = gcry_sexp_build (&s, NULL, "(public-key(ecc(curve " CURVE ")(q %b)))", | ||
326 | sizeof (struct ec_mpi), src); | ||
327 | brandt_assert_gpgerr (rc); | ||
328 | |||
329 | rc = gcry_mpi_ec_new (&ctx, s, NULL); | ||
330 | brandt_assert_gpgerr (rc); | ||
331 | gcry_sexp_release (s); | ||
332 | |||
333 | ret = gcry_mpi_ec_get_point ("q", ctx, 0); | ||
334 | brandt_assert (ret); | ||
335 | gcry_ctx_release (ctx); | ||
336 | gcry_mpi_ec_mul (dst, GCRYMPI_CONST_ONE, ret, ec_ctx); | ||
337 | } | ||
338 | |||
339 | |||
340 | /** | ||
341 | * smc_init2 creates a 2 dimensional array of curve points | ||
342 | * | ||
343 | * @param[in] size1 size of the first dimension | ||
344 | * @param[in] size2 size of the second dimension | ||
345 | * @return a pointer to the array. If not used anymore use smc_free2 to reclaim | ||
346 | * the memory. | ||
347 | */ | ||
292 | static gcry_mpi_point_t ** | 348 | static gcry_mpi_point_t ** |
293 | smc_init2 (uint16_t size1, uint16_t size2) | 349 | smc_init2 (uint16_t size1, uint16_t size2) |
294 | { | 350 | { |
@@ -310,6 +366,13 @@ smc_init2 (uint16_t size1, uint16_t size2) | |||
310 | } | 366 | } |
311 | 367 | ||
312 | 368 | ||
369 | /** | ||
370 | * smc_free2 releases all points in @a dst and frees the memory | ||
371 | * | ||
372 | * @param[in,out] dst The 2 dimensional array to clean up | ||
373 | * @param[in] size1 size of the first dimension | ||
374 | * @param[in] size2 size of the second dimension | ||
375 | */ | ||
313 | static void | 376 | static void |
314 | smc_free2 (gcry_mpi_point_t **dst, uint16_t size1, uint16_t size2) | 377 | smc_free2 (gcry_mpi_point_t **dst, uint16_t size1, uint16_t size2) |
315 | { | 378 | { |
@@ -322,6 +385,15 @@ smc_free2 (gcry_mpi_point_t **dst, uint16_t size1, uint16_t size2) | |||
322 | } | 385 | } |
323 | 386 | ||
324 | 387 | ||
388 | /** | ||
389 | * smc_init3 creates a 3 dimensional array of curve points | ||
390 | * | ||
391 | * @param[in] size1 size of the first dimension | ||
392 | * @param[in] size2 size of the second dimension | ||
393 | * @param[in] size3 size of the third dimension | ||
394 | * @return a pointer to the array. If not used anymore use smc_free3 to reclaim | ||
395 | * the memory. | ||
396 | */ | ||
325 | static gcry_mpi_point_t *** | 397 | static gcry_mpi_point_t *** |
326 | smc_init3 (uint16_t size1, uint16_t size2, uint16_t size3) | 398 | smc_init3 (uint16_t size1, uint16_t size2, uint16_t size3) |
327 | { | 399 | { |
@@ -351,6 +423,14 @@ smc_init3 (uint16_t size1, uint16_t size2, uint16_t size3) | |||
351 | } | 423 | } |
352 | 424 | ||
353 | 425 | ||
426 | /** | ||
427 | * smc_free3 releases all points in @a dst and frees the memory | ||
428 | * | ||
429 | * @param[in,out] dst The 3 dimensional array to clean up | ||
430 | * @param[in] size1 size of the first dimension | ||
431 | * @param[in] size2 size of the second dimension | ||
432 | * @param[in] size3 size of the third dimension | ||
433 | */ | ||
354 | static void | 434 | static void |
355 | smc_free3 (gcry_mpi_point_t ***dst, | 435 | smc_free3 (gcry_mpi_point_t ***dst, |
356 | uint16_t size1, | 436 | uint16_t size1, |
@@ -425,6 +505,12 @@ smc_compute_pkey (struct AuctionData *ad) | |||
425 | } | 505 | } |
426 | 506 | ||
427 | 507 | ||
508 | /** | ||
509 | * smc_gen_keyshare creates the private additive keyshare and computes the | ||
510 | * public multiplicative key share | ||
511 | * | ||
512 | * @param[in,out] ad Pointer to the AuctionData struct to operate on | ||
513 | */ | ||
428 | void | 514 | void |
429 | smc_gen_keyshare (struct AuctionData *ad) | 515 | smc_gen_keyshare (struct AuctionData *ad) |
430 | { | 516 | { |
@@ -439,6 +525,21 @@ smc_gen_keyshare (struct AuctionData *ad) | |||
439 | } | 525 | } |
440 | 526 | ||
441 | 527 | ||
528 | /** | ||
529 | * smc_encrypt_bid \todo | ||
530 | * | ||
531 | * @param ad TODO | ||
532 | * @param j TODO | ||
533 | * @param a1 TODO | ||
534 | * @param a2 TODO | ||
535 | * @param b1 TODO | ||
536 | * @param b2 TODO | ||
537 | * @param c TODO | ||
538 | * @param d1 TODO | ||
539 | * @param d2 TODO | ||
540 | * @param r1 TODO | ||
541 | * @param r2 TODO | ||
542 | */ | ||
442 | void | 543 | void |
443 | smc_encrypt_bid (struct AuctionData *ad, | 544 | smc_encrypt_bid (struct AuctionData *ad, |
444 | uint16_t j, | 545 | uint16_t j, |
@@ -458,6 +559,27 @@ smc_encrypt_bid (struct AuctionData *ad, | |||
458 | 559 | ||
459 | 560 | ||
460 | /** | 561 | /** |
562 | * smc_compute_outcome \todo | ||
563 | * | ||
564 | * @param ad TODO | ||
565 | */ | ||
566 | void | ||
567 | smc_compute_outcome (struct AuctionData *ad) | ||
568 | { | ||
569 | uint16_t i, j; | ||
570 | |||
571 | // create temporary table with partial sums | ||
572 | |||
573 | |||
574 | for (i = 0; i < ad->n; i++) | ||
575 | { | ||
576 | |||
577 | } | ||
578 | /*\todo ZKP*/ | ||
579 | } | ||
580 | |||
581 | |||
582 | /** | ||
461 | * smc_zkp_dl | 583 | * smc_zkp_dl |
462 | * | 584 | * |
463 | * @param v \todo | 585 | * @param v \todo |
@@ -481,6 +603,7 @@ smc_zkp_dl (const gcry_mpi_point_t v, | |||
481 | 603 | ||
482 | /* compute challange c */ | 604 | /* compute challange c */ |
483 | /**\todo: generate c from HASH(g,v,a) and don't output it */ | 605 | /**\todo: generate c from HASH(g,v,a) and don't output it */ |
606 | // brandt_hash (const void *block, size_t size, struct brandt_hash_code *ret) | ||
484 | ec_skey_create (c); | 607 | ec_skey_create (c); |
485 | gcry_mpi_mod (c, c, ec_n); | 608 | gcry_mpi_mod (c, c, ec_n); |
486 | 609 | ||
@@ -524,6 +647,19 @@ smc_zkp_dl_check (const gcry_mpi_point_t v, | |||
524 | } | 647 | } |
525 | 648 | ||
526 | 649 | ||
650 | /** | ||
651 | * smc_zkp_2dle \todo | ||
652 | * | ||
653 | * @param v TODO | ||
654 | * @param w TODO | ||
655 | * @param g1 TODO | ||
656 | * @param g2 TODO | ||
657 | * @param x TODO | ||
658 | * @param a TODO | ||
659 | * @param b TODO | ||
660 | * @param c TODO | ||
661 | * @param r TODO | ||
662 | */ | ||
527 | void | 663 | void |
528 | smc_zkp_2dle (const gcry_mpi_point_t v, | 664 | smc_zkp_2dle (const gcry_mpi_point_t v, |
529 | const gcry_mpi_point_t w, | 665 | const gcry_mpi_point_t w, |
@@ -552,6 +688,19 @@ smc_zkp_2dle (const gcry_mpi_point_t v, | |||
552 | } | 688 | } |
553 | 689 | ||
554 | 690 | ||
691 | /** | ||
692 | * smc_zkp_2dle_check \todo | ||
693 | * | ||
694 | * @param v TODO | ||
695 | * @param w TODO | ||
696 | * @param g1 TODO | ||
697 | * @param g2 TODO | ||
698 | * @param a TODO | ||
699 | * @param b TODO | ||
700 | * @param c TODO | ||
701 | * @param r TODO | ||
702 | * @return TODO | ||
703 | */ | ||
555 | int | 704 | int |
556 | smc_zkp_2dle_check (const gcry_mpi_point_t v, | 705 | smc_zkp_2dle_check (const gcry_mpi_point_t v, |
557 | const gcry_mpi_point_t w, | 706 | const gcry_mpi_point_t w, |
@@ -583,6 +732,23 @@ smc_zkp_2dle_check (const gcry_mpi_point_t v, | |||
583 | } | 732 | } |
584 | 733 | ||
585 | 734 | ||
735 | /** | ||
736 | * smc_zkp_0og \todo | ||
737 | * | ||
738 | * @param alpha TODO | ||
739 | * @param m TODO | ||
740 | * @param y TODO | ||
741 | * @param beta TODO | ||
742 | * @param a1 TODO | ||
743 | * @param a2 TODO | ||
744 | * @param b1 TODO | ||
745 | * @param b2 TODO | ||
746 | * @param c TODO | ||
747 | * @param d1 TODO | ||
748 | * @param d2 TODO | ||
749 | * @param r1 TODO | ||
750 | * @param r2 TODO | ||
751 | */ | ||
586 | void | 752 | void |
587 | smc_zkp_0og (gcry_mpi_point_t alpha, | 753 | smc_zkp_0og (gcry_mpi_point_t alpha, |
588 | const gcry_mpi_point_t m, | 754 | const gcry_mpi_point_t m, |
@@ -691,6 +857,23 @@ smc_zkp_0og (gcry_mpi_point_t alpha, | |||
691 | } | 857 | } |
692 | 858 | ||
693 | 859 | ||
860 | /** | ||
861 | * smc_zkp_0og_check \todo | ||
862 | * | ||
863 | * @param alpha TODO | ||
864 | * @param y TODO | ||
865 | * @param beta TODO | ||
866 | * @param a1 TODO | ||
867 | * @param a2 TODO | ||
868 | * @param b1 TODO | ||
869 | * @param b2 TODO | ||
870 | * @param c TODO | ||
871 | * @param d1 TODO | ||
872 | * @param d2 TODO | ||
873 | * @param r1 TODO | ||
874 | * @param r2 TODO | ||
875 | * @return TODO | ||
876 | */ | ||
694 | int | 877 | int |
695 | smc_zkp_0og_check (const gcry_mpi_point_t alpha, | 878 | smc_zkp_0og_check (const gcry_mpi_point_t alpha, |
696 | const gcry_mpi_point_t y, | 879 | const gcry_mpi_point_t y, |
@@ -764,29 +947,6 @@ smc_zkp_0og_check (const gcry_mpi_point_t alpha, | |||
764 | //} | 947 | //} |
765 | 948 | ||
766 | 949 | ||
767 | //gcry_mpi_point_t | ||
768 | //deserialize_point(const struct brandt_point* data, const int len) | ||
769 | //{ | ||
770 | // gcry_sexp_t s; | ||
771 | // gcry_ctx_t ctx; | ||
772 | // gcry_mpi_point_t ret; | ||
773 | // gcry_error_t rc; | ||
774 | // | ||
775 | // rc = gcry_sexp_build(&s, NULL, "(public-key(ecc(curve " CURVE ")(q %b)))", | ||
776 | // len, data); | ||
777 | // brandt_assert_gpgerr(rc); | ||
778 | // | ||
779 | // rc = gcry_mpi_ec_new(&ctx, s, NULL); | ||
780 | // brandt_assert_gpgerr(rc); | ||
781 | // gcry_sexp_release(s); | ||
782 | // | ||
783 | // ret = gcry_mpi_ec_get_point("q", ctx, 0); | ||
784 | // brandt_assert(ret); | ||
785 | // gcry_ctx_release(ctx); | ||
786 | // return ret; | ||
787 | //} | ||
788 | |||
789 | |||
790 | ///** | 950 | ///** |
791 | // * Generate a random value mod n. | 951 | // * Generate a random value mod n. |
792 | // * | 952 | // * |