summaryrefslogtreecommitdiff
path: root/src/libfrosix/frosix_api_keygen.c
blob: 15202e33dd0efd5d8453cd4cb6aea5293cd4d503 (plain)
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
/*
  This file is part of Frosix
  Copyright (C) 2020, 2021 Anastasis SARL

  Frosix 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, or (at your option) any later version.

  Frosix 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
  Frosix; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file reducer/frosix_api_keygen.c
 * @brief frosix reducer keygen api
 * @author Christian Grothoff
 * @author Dominik Meister
 * @author Dennis Neufeld
 * @author Joel Urech
 */

#include "platform.h"
#include "frosix.h"
#include "frosix_api.h"
#include <taler/taler_merchant_service.h>
#include <time.h>

#define FROSIX_KDF_CONTEXT "FROSIX-KEY"

/**
 * FIXME
*/
struct FROSIX_DkgProvider
{
  /**
   * Name of the provider
  */
  char *provider_name;

  /**
   * URL of the provider.
  */
  char *backend_url;

  /**
   * Public provider salt
  */
  struct FROSIX_ProviderSaltP provider_salt;

  /**
   * Long term public key of the provider
  */
  struct GNUNET_CRYPTO_EddsaPublicKey provider_public_key;

  /**
   * Index of the provider
  */
  uint8_t provider_index;

  /**
   * Authentication method.
  */
  char *auth_method;

  /**
   * Authentication data.
  */
  char *auth_data;

  /**
   * Answer to the question in @e auth_data.
   * This field is optional and only used if authentication method is of type
   * security question.
  */
  char *auth_answer;

  /**
   * Context String for this provider.
  */
  struct FROSIX_DkgContextStringP context_string;

  /**
   * Salt for the hash of the authentication data
  */
  struct GNUNET_HashCode hash_salt;

  /**
   * Salted hash of the challenge data
  */
  struct FROSIX_ChallengeHashP auth_hash;

  /**
   * Commitments which we get from round 1
  */
  struct FROSIX_DkgCommitment commitment;

  /**
   * Pre encryption key
  */
  struct FROSIX_EncryptionKey pre_enc_key;

  /**
   * All the secret shares received from this provider.
   * Number or length of this array is always `number_of_participants - 1`
  */
  struct FROSIX_DkgSecretShare *secret_shares;

  /**
   * Public key, should be the same at all providers
  */
  struct FROST_PublicKey public_key;

  /**
   * Provider's signature over public key and authentication data.
  */
  struct GNUNET_CRYPTO_EddsaSignature providers_signature;

  /**
   * Seed of provider
  */
  uint8_t seed[64];
};


/**
 * Struct to store all parsed data from the cli and /config and /seed
*/
struct FROSIX_DkgData
{
  /**
   * What is the threshold value?
  */
  uint8_t threshold;

  /**
   * Number of participating providers. Is the length of the @e providers array
  */
  uint8_t num_of_participants;

  /**
   * Our master key we have to export at the end of the DKG.
  */
  struct GNUNET_HashCode master_key;

  /**
   * Pointer to a list of providers
  */
  struct FROSIX_DkgProvider *providers;

  /**
   * Array of the public keys from all providers.
   * Length is @e num_of_participants
  */
  struct GNUNET_CRYPTO_EddsaPublicKey *providers_public_keys;

  /**
   * Number of years the key data should be stored at the providers.
  */
  uint64_t expiration;
};


/**
 * State of a keygen procedure
*/
struct FROSIX_DkgState
{
  /**
   * State we are updating.
   */
  struct FROSIX_DkgData *dkg_data;

  /**
   * Function to call when we are done.
   */
  FROSIX_ActionCallback cb;

  /**
   * Closure for @e cb.
   */
  void *cb_cls;

  /**
   * Redux action we returned to our controller.
   */
  struct FROSIX_ReduxAction ra;

  /**
   * Number of provider operations that are still awaiting completion.
   */
  uint8_t counter;

  /**
   * Last error code
  */
  enum TALER_ErrorCode error_code;

  /**
    * Array of config requests, with length `num_of_participants`
  */
  struct FROSIX_ConfigOperation **co;

  /**
   * Array of seed requests, with length `num_of_participants`
  */
  struct FROSIX_SeedGetOperation **sgo;

  /**
   * DKG-Commitment Requests
  */
  struct FROSIX_DkgCommitmentRequestOperation **dco;

  /**
   * DKG-Share Requests
  */
  struct FROSIX_DkgShareRequestOperation **dso;

  /**
   * DKG-Key Requests
  */
  struct FROSIX_DkgKeyStoreOperation **dko;
};


/**
 * Helper function to free all initialized memory of a
 * `struct FROSIX_DkgProvider`
 *
 * @param dp Pointer to the provider we want to free the memory.
*/
static void
free_dkg_provider_struct (struct FROSIX_DkgProvider *dp)
{
  if (NULL != dp->provider_name)
    GNUNET_free (dp->provider_name);

  if (NULL != dp->backend_url)
    GNUNET_free (dp->backend_url);

  if (NULL != dp->auth_method)
    GNUNET_free (dp->auth_method);

  if (NULL != dp->auth_data)
    GNUNET_free (dp->auth_data);

  if (NULL != dp->auth_answer)
    GNUNET_free (dp->auth_answer);

  FROST_free_dkg_commitment (&dp->commitment.commitment,
                             1);

  if (NULL != dp->secret_shares)
    GNUNET_free (dp->secret_shares);
}


/**
 * Function to free all initialized memory of a provider from a
 * `struct FROSIX_DkgData`
 *
 * @param dd the struct to clean up
*/
static void
free_dkg_data_struct (struct FROSIX_DkgData *dd)
{
  /* go through all providers - if any */
  if (NULL != dd->providers)
  {
    for (unsigned int i = 0; i < dd->num_of_participants; i++)
    {
      if (NULL != &dd->providers[i])
        free_dkg_provider_struct (&dd->providers[i]);
    }
    GNUNET_free (dd->providers);
  }

  if (NULL != dd->providers_public_keys)
    GNUNET_free (dd->providers_public_keys);
}


/**
 * Function called when keygen is being aborted.
 * Frees all initialized memory!
 *
 * @param cls a `struct FROSIX_DkgState`
*/
static void
keygen_cancel_cb (void *cls)
{
  struct FROSIX_DkgState *ds = cls;

  /* free dkg data */
  if (NULL != ds->dkg_data)
  {
    free_dkg_data_struct (ds->dkg_data);
    GNUNET_free (ds->dkg_data);
  }

  /* free operations */
  if (NULL != ds->co)
    GNUNET_free (ds->co);

  if (NULL != ds->sgo)
    GNUNET_free (ds->sgo);

  if (NULL != ds->dco)
    GNUNET_free (ds->dco);

  if (NULL != ds->dso)
    GNUNET_free (ds->dso);

  if (NULL != ds->dko)
    GNUNET_free (ds->dko);

  /* free dkg state */
  GNUNET_free (ds);
}


/**
 * FIXME: move to frosix common crypto functions library!
*/
static void
derive_encryption_key (
  struct FROSIX_EncryptionKey *enc_key,
  const struct FROSIX_EncryptionKey *pre_enc_key,
  const struct FROST_PublicKey *pk,
  const struct FROSIX_ProviderSaltP *provider_salt,
  uint8_t provider_index)
{
  GNUNET_CRYPTO_kdf (enc_key,
                     sizeof (*enc_key),
                     FROSIX_KDF_CONTEXT,
                     sizeof (FROSIX_KDF_CONTEXT),
                     pre_enc_key,
                     sizeof (*pre_enc_key),
                     pk,
                     sizeof (*pk),
                     provider_salt,
                     sizeof (*provider_salt),
                     &provider_index,
                     sizeof (provider_index),
                     NULL,
                     0);
}

/**
 * Helper function to build an array out of all provider public keys.
 * The assembled array is then stored in our @a dd struct.
 *
 * @param dd Our main struct to store all relevant data in.
*/
static void
keygen_build_public_key_array (struct FROSIX_DkgData *dd)
{
  /* prepare array of providers public keys */
  dd->providers_public_keys = GNUNET_malloc (
    dd->num_of_participants
    * sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));

  GNUNET_assert (NULL != dd->providers_public_keys);

  for (unsigned int i = 0; i < dd->num_of_participants; i++)
  {
    memcpy (&dd->providers_public_keys[i],
            &dd->providers[i].provider_public_key,
            sizeof (dd->providers->provider_public_key));
  }
}


/**
 * This function gets called after we have received a positive result from all
 * providers. It prepares a json struct, which we then return back to the cli.
 *
 * @param ds The state of our keygen process.
*/
static void
keygen_prepare_result (struct FROSIX_DkgState *ds)
{
  /* first check if signature from providers can be verified. */
  // FIXME: define constant for size of ps struct
  for (unsigned int i = 0; i < ds->dkg_data->num_of_participants; i++)
  {
    struct FROSIX_DkgProvider *dp = &ds->dkg_data->providers[i];

    struct FROSIX_DkgKeySignaturePS ks = {
      .purpose.purpose = htonl (104),
      .purpose.size = htonl (sizeof (ks)),
      .public_key = dp->public_key,
      .auth_hash = dp->auth_hash,
    };

    GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_verify (
                     104,
                     &ks,
                     &dp->providers_signature,
                     &dp->provider_public_key));
  }

  /* check if every reported public key is the same */
  for (unsigned int i = 0; i < ds->dkg_data->num_of_participants; i++)
  {
    if (GNUNET_OK != FROST_point_cmp (
          &ds->dkg_data->providers[i].public_key.pk,
          &ds->dkg_data->providers[0].public_key.pk))
    {
      json_t *error_message;
      error_message = GNUNET_JSON_PACK (
        GNUNET_JSON_pack_string ("Frosix_client_error",
                                 "Public keys do not match"));

      ds->cb (ds->cb_cls,
              0,
              error_message);

      return;
    }
  }

  /* calculate encryption keys */
  struct FROSIX_EncryptionKey enc_keys[ds->dkg_data->num_of_participants];
  for (unsigned int i = 0; i < ds->dkg_data->num_of_participants; i++)
  {
    struct FROSIX_DkgProvider *dp = &ds->dkg_data->providers[i];
    derive_encryption_key (&enc_keys[i],
                           &dp->pre_enc_key,
                           &dp->public_key,
                           &dp->provider_salt,
                           dp->provider_index);
  }

  /* build json */
  json_t *result;
  json_t *providers = json_array ();

  for (unsigned int i = 0; i < ds->dkg_data->num_of_participants; i++)
  {
    /* build single provider */
    struct FROSIX_DkgProvider *dp = &ds->dkg_data->providers[i];
    json_t *provider;
    provider = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_uint64 ("provider_index",
                               dp->provider_index),
      GNUNET_JSON_pack_string ("provider_name",
                               dp->provider_name),
      GNUNET_JSON_pack_string ("backend_url",
                               dp->backend_url),
      GNUNET_JSON_pack_data_auto ("encryption_key",
                                  &enc_keys[i]),
      GNUNET_JSON_pack_string ("auth_method",
                               dp->auth_method),
      GNUNET_JSON_pack_string ("auth_data",
                               dp->auth_data),
      GNUNET_JSON_pack_data_auto ("auth_nonce",
                                  &dp->hash_salt),
      GNUNET_JSON_pack_data_auto ("auth_hash",
                                  &dp->auth_hash),
      GNUNET_JSON_pack_data_auto ("provider_signature",
                                  &dp->providers_signature),
      GNUNET_JSON_pack_data_auto ("provider_public_key",
                                  &dp->provider_public_key));

    /* add to provider array */
    GNUNET_assert (0 ==
                   json_array_append_new (
                     providers,
                     provider));
  }

  result = GNUNET_JSON_PACK (
    GNUNET_JSON_pack_data_auto ("public_key",
                                &ds->dkg_data->providers[0].public_key),
    GNUNET_JSON_pack_uint64 ("number_of_participants",
                             ds->dkg_data->num_of_participants),
    GNUNET_JSON_pack_uint64 ("threshold",
                             ds->dkg_data->threshold),
    GNUNET_JSON_pack_array_steal ("providers",
                                  providers));

  /* free all initialized data */
  free_dkg_data_struct (ds->dkg_data);

  ds->cb (ds->cb_cls,
          ds->error_code,
          result);
}


/**
 * Callback to process a POST /dkg-key request
 *
 * @param cls closure
 * @param dcd the decoded response body
*/
static void
dkg_key_request_cb (void *cls,
                    const struct FROSIX_DkgKeyStoreDetails *dkd)
{
  GNUNET_assert (NULL != cls);
  struct FROSIX_DkgState *ds = cls;

  if (0 >= ds->counter)
  {
    /* We haven't expected this callback, thus just ignore it */
    return;
  }

  /* check if the call was successful */
  if (0 == dkd->http_status)
  {
    ds->counter = 0;

    // FIXME: cancel all other running and not yet returned requests
    json_t *error_message;
    error_message = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_string ("Frosix_client_error",
                               "Error requesting dkg key from provider"));

    ds->cb (ds->cb_cls,
            0,
            error_message);

    return;
  }

  /* set error code */
  ds->error_code = dkd->ec;

  /* check if provider index is valid */
  GNUNET_assert (0 < dkd->provider_index);
  GNUNET_assert (ds->dkg_data->num_of_participants >= dkd->provider_index);

  /* copy commitment in our struct - just assign pointer */
  struct FROSIX_DkgProvider *dp = &ds->dkg_data->providers[dkd->provider_index
                                                           - 1];

  memcpy (&dp->public_key,
          &dkd->details.public_key,
          sizeof (dkd->details.public_key));

  memcpy (&dp->providers_signature,
          &dkd->details.signature,
          sizeof (dkd->details.signature));

  /* call next dkg-commitment request */
  ds->counter--;
  if (0 == ds->counter)
  {
    /* we are the last, go ahead with checking our result */
    GNUNET_free (ds->dko);
    ds->dko = NULL;
    keygen_prepare_result (ds);
  }
}


/**
 * FIXME
*/
static void
start_dkg_key (struct FROSIX_DkgState *ds)
{
  ds->dko = GNUNET_malloc (ds->dkg_data->num_of_participants * sizeof (char *));
  ds->counter = 0;

  for (unsigned int i = 0; i < ds->dkg_data->num_of_participants; i++)
  {
    struct FROSIX_DkgData *dd = ds->dkg_data;
    struct FROSIX_DkgProvider *dp = &ds->dkg_data->providers[i];

    /* set up array of secret shares */
    struct FROSIX_DkgKeyStoreShare secret_shares[dd->num_of_participants - 1];

    for (unsigned int j = 0; j < dd->num_of_participants; j++)
    {
      if (j < i)
      {
        /* check if target of the secret share matches */
        if (dp->provider_index != dd->providers[j].secret_shares[i - 1].target)
        {
          json_t *error_message;
          error_message = GNUNET_JSON_PACK (
            GNUNET_JSON_pack_string ("Frosix_client_error",
                                     "Error while assigning the secret shares"));

          ds->cb (ds->cb_cls,
                  0,
                  error_message);

          return;
        }

        secret_shares[j].identifier =
          dd->providers[j].secret_shares[i - 1].issuer;

        memcpy (&secret_shares[j].secret_share,
                &dd->providers[j].secret_shares[i - 1].encrypted_share,
                sizeof (dd->providers[j].secret_shares[i - 1].encrypted_share));
        memcpy (&secret_shares[j].ephemeral_key,
                &dd->providers[j].secret_shares[i - 1].ephemeral_key,
                sizeof (dd->providers[j].secret_shares[i - 1].ephemeral_key));
      }
      else if (j > i)
      {
        /* check if target of the secret share matches */
        if (dp->provider_index != dd->providers[j].secret_shares[i].target)
        {
          json_t *error_message;
          error_message = GNUNET_JSON_PACK (
            GNUNET_JSON_pack_string ("Frosix_client_error",
                                     "Error while assigning the secret shares"));

          ds->cb (ds->cb_cls,
                  0,
                  error_message);

          return;
        }

        secret_shares[j - 1].identifier =
          dd->providers[j].secret_shares[i].issuer;
        memcpy (&secret_shares[j - 1].secret_share,
                &dd->providers[j].secret_shares[i].encrypted_share,
                sizeof (dd->providers[j].secret_shares[i].encrypted_share));
        memcpy (&secret_shares[j - 1].ephemeral_key,
                &dd->providers[j].secret_shares[i].ephemeral_key,
                sizeof (dd->providers[j].secret_shares[i].ephemeral_key));
      }
    }

    /* compute request id */
    struct FROSIX_DkgRequestIdP request_id;
    FROSIX_compute_dkg_request_id (
      &request_id,
      &dp->context_string,
      &dp->auth_hash,
      &dp->provider_salt,
      dp->provider_index,
      ds->dkg_data->num_of_participants,
      ds->dkg_data->threshold);

    ds->dko[i] = FROSIX_dkg_key_store (
      FROSIX_REDUX_ctx_,
      dp->backend_url,
      &request_id,
      dp->provider_index,
      dd->threshold,
      dd->num_of_participants,
      &dp->context_string,
      &dp->auth_hash,
      dd->providers_public_keys,
      &dp->pre_enc_key,
      dd->expiration,
      secret_shares,
      dd->num_of_participants - 1,
      &dkg_key_request_cb,
      ds);

    ds->counter++;
  }
}


/**
 * Callback to process a POST /dkg-shares request
 *
 * @param cls closure
 * @param dcd the decoded response body
*/
static void
dkg_share_request_cb (void *cls,
                      const struct FROSIX_DkgShareRequestDetails *dsd)
{
  GNUNET_assert (NULL != cls);
  struct FROSIX_DkgState *ds = cls;

  if (0 >= ds->counter)
  {
    /* We haven't expected this callback, thus just ignore it */
    return;
  }

  /* check if the call was successful */
  if (0 == dsd->http_status)
  {
    ds->counter = 0;

    // FIXME: cancel all other running and not yet returned requests
    json_t *error_message;
    error_message = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_string ("Frosix_client_error",
                               "Error requesting dkg shares from provider"));

    ds->cb (ds->cb_cls,
            0,
            error_message);

    return;
  }

  /* set error code */
  ds->error_code = dsd->ec;

  /* check if provider index is valid */
  GNUNET_assert (0 < dsd->provider_index);
  GNUNET_assert (ds->dkg_data->num_of_participants >= dsd->provider_index);

  /* check number of secret shares */
  if (ds->dkg_data->num_of_participants - 1 != dsd->shares_len)
  {
    ds->counter = 0;

    // FIXME: cancel all other running and not yet returned requests
    json_t *error_message;
    error_message = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_string ("Frosix_client_error",
                               "Error in dkg shares response"));

    ds->cb (ds->cb_cls,
            0,
            error_message);

    return;
  }

  /* check if our provider index is same as in the response */
  for (unsigned int i = 0; i < dsd->shares_len; i++)
  {
    if (dsd->provider_index != dsd->shares[i].issuer)
    {
      ds->counter = 0;

      // FIXME: cancel all other running and not yet returned requests
      json_t *error_message;
      error_message = GNUNET_JSON_PACK (
        GNUNET_JSON_pack_string ("Frosix_client_error",
                                 "Error in dkg shares response"));

      ds->cb (ds->cb_cls,
              0,
              error_message);

      return;
    }
  }

  /* copy commitment in our struct - just assign pointer */
  ds->dkg_data->providers[dsd->provider_index - 1].secret_shares = dsd->shares;

  /* call next dkg-commitment request */
  ds->counter--;
  if (0 == ds->counter)
  {
    /* we are the last, go ahead with dkg-key */
    GNUNET_free (ds->dso);
    ds->dso = NULL;
    start_dkg_key (ds);
  }
}


/**
 * FIXME
*/
static void
start_dkg_shares (struct FROSIX_DkgState *ds)
{
  ds->dso = GNUNET_malloc (ds->dkg_data->num_of_participants * sizeof (char *));
  ds->counter = 0;

  for (unsigned int i = 0; i < ds->dkg_data->num_of_participants; i++)
  {
    struct FROSIX_DkgData *dd = ds->dkg_data;
    struct FROSIX_DkgProvider *dp = &ds->dkg_data->providers[i];

    /* set up array of dkg commitments */
    struct FROSIX_DkgCommitment dkg_commitments[dd->num_of_participants - 1];
    for (unsigned int j = 0; j < dd->num_of_participants; j++)
    {
      if (j < i)
      {
        dkg_commitments[j] = dd->providers[j].commitment;
      }
      else if (j > i)
      {
        dkg_commitments[j - 1] = dd->providers[j].commitment;
      }
    }

    /* compute request id */
    struct FROSIX_DkgRequestIdP request_id;
    FROSIX_compute_dkg_request_id (
      &request_id,
      &dp->context_string,
      &dp->auth_hash,
      &dp->provider_salt,
      dp->provider_index,
      ds->dkg_data->num_of_participants,
      ds->dkg_data->threshold);

    ds->dso[i] = FROSIX_dkg_share_request (
      FROSIX_REDUX_ctx_,
      dp->backend_url,
      &request_id,
      dp->provider_index,
      dd->threshold,
      dd->num_of_participants,
      &dp->context_string,
      &dp->auth_hash,
      dkg_commitments,
      dd->num_of_participants - 1,
      dd->providers_public_keys,
      &dkg_share_request_cb,
      ds);

    ds->counter++;
  }

  /* free dkg commitments */
  for (unsigned int i = 0; i < ds->dkg_data->num_of_participants; i++)
  {
    FROST_free_dkg_commitment (
      &ds->dkg_data->providers[i].commitment.commitment,
      1);
  }
}


/**
 * Callback to process a POST /dkg-commitment request
 *
 * @param cls closure
 * @param dcd the decoded response body
*/
static void
dkg_commitment_request_cb (void *cls,
                           const struct FROSIX_DkgCommitmentRequestDetails *dcd)
{
  GNUNET_assert (NULL != cls);
  struct FROSIX_DkgState *ds = cls;

  if (0 >= ds->counter)
  {
    /* We haven't expected this callback, thus just ignore it */
    return;
  }

  /* check if the call was successful */
  if (0 == dcd->http_status)
  {
    ds->counter = 0;

    // FIXME: cancel all other running and not yet returned requests
    json_t *error_message;
    error_message = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_string ("Frosix_client_error",
                               "Error requesting dkg commitment from provider"));

    ds->cb (ds->cb_cls,
            0,
            error_message);

    return;
  }

  /* set error code */
  ds->error_code = dcd->ec;

  /* check if provider index is valid */
  GNUNET_assert (0 < dcd->provider_index);
  GNUNET_assert (ds->dkg_data->num_of_participants >= dcd->provider_index);

  /* check if our provider index is same as in the response */
  if (dcd->provider_index != dcd->dkg_commitment.identifier)
  {
    ds->counter--;

    // FIXME: cancel all other running and not yet returned requests
    json_t *error_message;
    error_message = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_string ("Frosix_client_error",
                               "Error in dkg commitment response"));

    ds->cb (ds->cb_cls,
            0,
            error_message);

    return;
  }

  /* check if length of commitments equals our threshold value */
  if (dcd->dkg_commitment.shares_commitments_length != ds->dkg_data->threshold)
  {
    ds->counter = 0;

    // FIXME: cancel all other running and not yet returned requests
    json_t *error_message;
    error_message = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_string ("Frosix_client_error",
                               "Error in dkg commitment response"));

    ds->cb (ds->cb_cls,
            0,
            error_message);

    return;
  }

  /* copy commitment in our struct */
  struct FROSIX_DkgProvider *dp = &ds->dkg_data->providers[dcd->provider_index
                                                           - 1];

  /* Fill return values in our central struct */
  memcpy (&dp->commitment.encryption_public_key,
          &dcd->public_key,
          sizeof (dcd->public_key));

  FROST_initialize_dkg_commitment (
    &dp->commitment.commitment,
    dcd->provider_index,
    dcd->dkg_commitment.shares_commitments_length);

  for (unsigned int i = 0; i < dcd->dkg_commitment.shares_commitments_length;
       i++)
  {
    memcpy (&dp->commitment.commitment.share_comm[i],
            &dcd->dkg_commitment.share_comm[i],
            sizeof (dcd->dkg_commitment.share_comm[i]));
  }

  memcpy (&dp->commitment.commitment.zkp.r,
          &dcd->dkg_commitment.zkp.r,
          sizeof (dcd->dkg_commitment.zkp.r));

  memcpy (&dp->commitment.commitment.zkp.z,
          &dcd->dkg_commitment.zkp.z,
          sizeof (dcd->dkg_commitment.zkp.z));

  /* call next dkg-commitment request */
  ds->counter--;
  if (0 == ds->counter)
  {
    /* we are the last, go ahead with dkg-share */
    GNUNET_free (ds->dco);
    ds->dco = NULL;
    start_dkg_shares (ds);
  }
}


/**
 * Function to derive the master key and all context strings.
 *
 * @param[in,out] dd the struct to fill in the entropy
 * @param[in] client_entropy high entropy bytes to derive more high entropy bytes
*/
static void
keygen_derive_entropy (struct FROSIX_DkgData *dd,
                       const struct GNUNET_HashCode *client_entropy,
                       const struct GNUNET_HashCode *provider_entropy)
{
  {
    /* derive high entropy bytes */
    size_t length = sizeof (dd->master_key)
                    + (dd->num_of_participants
                       * sizeof (dd->providers->context_string));

    char *derived_entropy = GNUNET_malloc (length);

    /* kill process if allocation failed */
    GNUNET_assert (NULL != derived_entropy);

    GNUNET_assert (GNUNET_CRYPTO_kdf (derived_entropy,
                                      length,
                                      "FROSIX",
                                      strlen ("FROSIX"),
                                      client_entropy,
                                      sizeof (*client_entropy),
                                      provider_entropy,
                                      sizeof (*provider_entropy),
                                      NULL,
                                      0));

    /* copy the derived bytes to the right place */
    unsigned int byte_counter = 0;
    memcpy (&dd->master_key,
            &derived_entropy[byte_counter],
            sizeof (dd->master_key));
    byte_counter += sizeof (dd->master_key);

    for (unsigned int i = 0; i < dd->num_of_participants; i++)
    {
      memcpy (&dd->providers[i].context_string,
              &derived_entropy[byte_counter],
              sizeof (dd->providers->context_string));
      byte_counter += sizeof (dd->providers->context_string);
    }

    /* free allocated memory */
    GNUNET_free (derived_entropy);
  }

  {
    /* derive authentication salts and pre encryption keys */
    size_t length = dd->num_of_participants
                    * (sizeof (dd->providers->hash_salt)
                       + sizeof (dd->providers->pre_enc_key));

    char *client_entropy = GNUNET_malloc (length);

    /* kill process if allocation failed */
    GNUNET_assert (NULL != client_entropy);

    GNUNET_CRYPTO_kdf (client_entropy,
                       length,
                       "FROSIX",
                       strlen ("FROSIX"),
                       &dd->master_key,
                       sizeof (dd->master_key),
                       NULL,
                       0);

    /* copy bytes to the right place */
    unsigned int byte_counter = 0;
    for (unsigned int i = 0; i < dd->num_of_participants; i++)
    {
      memcpy (&dd->providers[i].hash_salt,
              &client_entropy[byte_counter],
              sizeof (dd->providers->hash_salt));
      byte_counter += sizeof (dd->providers->hash_salt);
    }

    for (unsigned int i = 0; i < dd->num_of_participants; i++)
    {
      memcpy (&dd->providers[i].pre_enc_key,
              &client_entropy[byte_counter],
              sizeof (dd->providers->pre_enc_key));
      byte_counter += sizeof (dd->providers->pre_enc_key);
    }

    /* free allocated memory */
    GNUNET_free (client_entropy);
  }
}


/**
 * FIXME
*/
static void
merge_provider_entropy (struct GNUNET_HashCode *provider_entropy,
                        const struct FROSIX_DkgState *ds)
{
  struct GNUNET_HashContext *hc = GNUNET_CRYPTO_hash_context_start ();
  for (unsigned int i = 0; i < ds->dkg_data->num_of_participants; i++)
  {
    GNUNET_CRYPTO_hash_context_read (hc,
                                     ds->dkg_data->providers[i].seed,
                                     sizeof (ds->dkg_data->providers[i].seed));
  }

  GNUNET_CRYPTO_hash_context_finish (hc,
                                     provider_entropy);
}


/**
 * FIXME
*/
static void
start_dkg_commitments (struct FROSIX_DkgState *ds)
{
  /* measure cpu speed */
  // unsigned int target_difficulty = 5000; // FIXME
  unsigned int amplifier = 1;
  {
    struct FROST_PowSalt salt;
    GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
                                &salt,
                                sizeof (salt));

    /* start */
    // clock_t start = clock ();
    struct FROST_HashCode hash;
    FROST_pow_hash (&hash,
                    "Frosix",
                    strlen ("Frosix"),
                    &salt,
                    1);
    /* end */
    // clock_t end = clock ();
    // float seconds = (float) (end - start) / 1000;
    // amplifier = target_difficulty / seconds;

  }

  /* merge provider seeds to a single hash code */
  struct GNUNET_HashCode provider_entropy;
  merge_provider_entropy (&provider_entropy,
                          ds);

  /* derive master key, context strings, salts and pre encryption keys */
  struct GNUNET_HashCode client_entropy;
  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
                              &client_entropy,
                              sizeof (client_entropy));

  keygen_derive_entropy (ds->dkg_data,
                         &client_entropy,
                         &provider_entropy);

  /* compute salted hash of authentication data */
  for (unsigned int i = 0; i < ds->dkg_data->num_of_participants; i++)
  {
    struct FROSIX_DkgProvider *dp = &ds->dkg_data->providers[i];
    if (NULL != dp->auth_answer)
    {
      /* we have a security question here */
      struct GNUNET_CRYPTO_Edx25519PrivateKey priv;
      struct GNUNET_CRYPTO_Edx25519PublicKey pub;
      FROSIX_hash_pow (&dp->auth_hash.hash,
                       &priv,
                       &pub,
                       &dp->hash_salt,
                       dp->auth_answer,
                       amplifier);
    }
    else
    {
      FROSIX_compute_auth_hash (&dp->auth_hash,
                                dp->auth_data,
                                &dp->hash_salt);
    }
  }

  ds->dco = GNUNET_malloc (ds->dkg_data->num_of_participants * sizeof (char *));
  ds->counter = 0;

  for (unsigned int i = 0; i < ds->dkg_data->num_of_participants; i++)
  {
    struct FROSIX_DkgProvider *dp = &ds->dkg_data->providers[i];

    /* compute request id */
    struct FROSIX_DkgRequestIdP request_id;
    FROSIX_compute_dkg_request_id (
      &request_id,
      &dp->context_string,
      &dp->auth_hash,
      &dp->provider_salt,
      dp->provider_index,
      ds->dkg_data->num_of_participants,
      ds->dkg_data->threshold);

    ds->dco[i] = FROSIX_dkg_commitment_request (
      FROSIX_REDUX_ctx_,
      dp->backend_url,
      &request_id,
      dp->provider_index,
      ds->dkg_data->threshold,
      ds->dkg_data->num_of_participants,
      &dp->context_string,
      &dp->auth_hash,
      ds->dkg_data->providers_public_keys,
      &dkg_commitment_request_cb,
      ds);

    ds->counter++;
  }
}


/**
 Callback to process a GET /seed request
 *
 * @param cls closure
 * @param ps the decoded response body
*/
static void
seed_request_cb (void *cls,
                 unsigned int http_status,
                 const struct FROSIX_ProviderSeed *ps)
{
  GNUNET_assert (NULL != cls);
  struct FROSIX_DkgState *ds = cls;

  if (0 >= ds->counter)
  {
    /* We haven't expected this callback, thus just ignore it */
    return;
  }

  /* check if the call was successful */
  if (0 == http_status)
  {
    ds->counter = 0;

    // FIXME: cancel all other running and not yet returned requests
    json_t *error_message;
    error_message = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_string ("Frosix_client_error",
                               "Error requesting seed from provider"));

    ds->cb (ds->cb_cls,
            0,
            error_message);

    return;
  }

  /* check if provider index is valid */
  GNUNET_assert (0 < ps->provider_index);
  GNUNET_assert (ds->dkg_data->num_of_participants >= ps->provider_index);

  /* copy seed to our main struct */
  struct FROSIX_DkgProvider *dp = &ds->dkg_data->providers[ps->provider_index
                                                           - 1];

  memcpy (&dp->seed,
          ps->seed,
          FROSIX_SEED_SIZE);

  ds->counter--;
  if (0 == ds->counter)
  {
    GNUNET_free (ds->sgo);
    ds->sgo = NULL;
    /* we are the last, go ahead with dkg-commitment */
    start_dkg_commitments (ds);
  }
}


/**
 * FIXME
*/
static void
start_seed_requests (struct FROSIX_DkgState *ds)
{
  ds->counter = 0;
  ds->sgo = GNUNET_malloc (ds->dkg_data->num_of_participants * sizeof (char *));

  for (unsigned int i = 0; i < ds->dkg_data->num_of_participants; i++)
  {
    ds->sgo[i] = FROSIX_seed_get (FROSIX_REDUX_ctx_,
                                  ds->dkg_data->providers[i].backend_url,
                                  i + 1,
                                  &seed_request_cb,
                                  ds);
    ds->counter++;
  }
}


/**
 * Callback to process a GET /config request
 *
 * @param cls closure
 * @param fcfg the decoded response body
*/
static void
config_request_cb (void *cls,
                   unsigned int http_status,
                   const struct FROSIX_Config *fcfg)
{
  GNUNET_assert (NULL != cls);
  struct FROSIX_DkgState *ds = cls;

  if (0 >= ds->counter)
  {
    /* We haven't expected this callback, thus just ignore it */
    return;
  }

  /* check if the call was successful */
  if (0 == http_status)
  {
    ds->counter = 0;

    // FIXME: cancel all other running and not yet returned requests
    json_t *error_message;
    error_message = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_string ("Frosix_client_error",
                               "Error requesting config from provider"));

    ds->cb (ds->cb_cls,
            0,
            error_message);

    return;
  }

  /* check if provider index is valid */
  GNUNET_assert (0 < fcfg->provider_index);
  GNUNET_assert (ds->dkg_data->num_of_participants >= fcfg->provider_index);

  {
    /* copy config data to our main struct */
    struct FROSIX_DkgProvider *dp =
      &ds->dkg_data->providers[fcfg->provider_index - 1];

    size_t len = strlen (fcfg->business_name);
    dp->provider_name = strndup (fcfg->business_name,
                                 len);

    memcpy (&dp->provider_salt,
            &fcfg->provider_salt,
            sizeof (fcfg->provider_salt));

    memcpy (&dp->provider_public_key,
            &fcfg->public_key,
            sizeof (fcfg->public_key));
  }

  ds->counter--;
  if (0 == ds->counter)
  {
    GNUNET_free (ds->co);
    ds->co = NULL;

    /* we are the last, build array of public keys
    and go ahead with the seeds */
    keygen_build_public_key_array (ds->dkg_data);

    start_seed_requests (ds);
  }
};


/**
 * FIXME
*/
static void
start_config_requests (struct FROSIX_DkgState *ds)
{
  ds->counter = 0;
  ds->co = GNUNET_malloc (ds->dkg_data->num_of_participants * sizeof (char *));

  for (unsigned int i = 0; i < ds->dkg_data->num_of_participants; i++)
  {
    ds->dkg_data->providers[i].provider_index = i + 1;
    ds->co[i] = FROSIX_get_config (FROSIX_REDUX_ctx_,
                                   ds->dkg_data->providers[i].backend_url,
                                   i + 1,
                                   &config_request_cb,
                                   ds);
    ds->counter++;
  }
}


/**
 * Helper function to parse a keygen input
 *
 * @param[in,out] dkg_data A initialized struct
 * @param[in] arguments Input from the cli
*/
enum GNUNET_GenericReturnValue
parse_keygen_arguments (struct FROSIX_DkgData *dkg_data,
                        const json_t *arguments)
{
  json_t *providers = NULL;

  struct GNUNET_JSON_Specification spec[] = {
    GNUNET_JSON_spec_uint8 ("threshold",
                            &dkg_data->threshold),
    GNUNET_JSON_spec_uint64 ("expiration",
                             &dkg_data->expiration),
    GNUNET_JSON_spec_json ("providers",
                           &providers),
    GNUNET_JSON_spec_end ()
  };

  if (GNUNET_OK != GNUNET_JSON_parse (arguments,
                                      spec,
                                      NULL,
                                      NULL))
  {
    GNUNET_break (0);
    GNUNET_JSON_parse_free (spec);
    return GNUNET_NO;
  }

  /* get number of providers */
  size_t num_of_providers = json_array_size (providers);

  /* validate number of providers */
  if (num_of_providers > 254
      || num_of_providers < 2
      || dkg_data->threshold < 1
      || num_of_providers <= dkg_data->threshold)
    return GNUNET_NO;

  dkg_data->num_of_participants = num_of_providers;

  /* initialize providers */
  dkg_data->providers =  GNUNET_new_array (dkg_data->num_of_participants,
                                           struct FROSIX_DkgProvider);

  /* parse provider data */
  for (unsigned int i = 0; i < dkg_data->num_of_participants; i++)
  {
    struct GNUNET_JSON_Specification prov_spec[] = {
      GNUNET_JSON_spec_string ("url",
                               (const
                                char**) &dkg_data->providers[i].backend_url),
      GNUNET_JSON_spec_fixed_auto ("public_key",
                                   &dkg_data->providers[i].provider_public_key),
      GNUNET_JSON_spec_string ("auth_method",
                               (const
                                char**) &dkg_data->providers[i].auth_method),
      GNUNET_JSON_spec_string ("auth_data",
                               (const
                                char**) &dkg_data->providers[i].auth_data),
      GNUNET_JSON_spec_mark_optional (
        GNUNET_JSON_spec_string ("auth_answer",
                                 (const
                                  char**) &dkg_data->providers[i].auth_answer),
        NULL),
      GNUNET_JSON_spec_end ()
    };

    if (GNUNET_OK != GNUNET_JSON_parse (json_array_get (providers,
                                                        i),
                                        prov_spec,
                                        NULL,
                                        NULL))
    {
      GNUNET_break (0);
      GNUNET_JSON_parse_free (prov_spec);
      GNUNET_JSON_parse_free (spec);
      return GNUNET_NO;
    }

    GNUNET_JSON_parse_free (prov_spec);
  }

  GNUNET_JSON_parse_free (spec);
  return GNUNET_OK;
}


struct FROSIX_ReduxAction *
FROSIX_redux_keygen_start (const json_t *arguments,
                           FROSIX_ActionCallback cb,
                           void *cb_cls)
{
  /* initialize dkg data struct */
  struct FROSIX_DkgData *dkg_data = GNUNET_new (struct FROSIX_DkgData);

  /* parse arguments from cli */
  if (GNUNET_OK != parse_keygen_arguments (dkg_data,
                                           arguments))
  {
    free_dkg_data_struct (dkg_data);
    GNUNET_free (dkg_data);
    json_t *error_message;
    error_message = GNUNET_JSON_PACK (
      GNUNET_JSON_pack_string ("Frosix_client_error",
                               "Error while parsing input"));

    cb (cb_cls,
        0,
        error_message);

    return NULL;
  }

  /* lets create a state */
  struct FROSIX_DkgState *ds = GNUNET_new (struct FROSIX_DkgState);
  ds->cb = cb;
  ds->cb_cls = cb_cls;
  ds->dkg_data = dkg_data;
  ds->ra.cleanup = &keygen_cancel_cb;
  ds->ra.cleanup_cls = ds;

  /* get configs from all parsed providers */
  start_config_requests (ds);

  return &ds->ra;
}