summaryrefslogtreecommitdiff
path: root/src/util/anastasis-crypto-tvg.c
blob: b426ee507ca8de9778cbd6d4e0776c2edca29e2a (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
/*
  This file is part of Anastasis
  Copyright (C) 2020,2021 Anastasis SARL

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  Anastasis 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 Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/


/**
 * @file util/anastasis-crypto-tgv.c
 * @brief Generate test vectors for cryptographic operations.
 * @author Florian Dold
 *
 *
 * Test vectors have the following format (TypeScript pseudo code):
 *
 * interface TestVectorFile {
 *   encoding: "base32crockford";
 *   producer?: string;
 *   vectors: TestVector[];
 * }
 *
 * enum Operation {
 *  Hash("hash"),
 *  ...
 * }
 *
 * interface TestVector {
 *   operation: Operation;
 *   // Inputs for the operation
 *   [ k: string]: string | number;
 * };
 *
 *
 */
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_signatures.h>
#include <gnunet/gnunet_testing_lib.h>
#include <jansson.h>
#include <gcrypt.h>
#include "anastasis_crypto_lib.h"


/**
 * Should we verify or output test vectors?
 */
static int verify_flag = GNUNET_NO;


/**
 * Global exit code.
 */
static int global_ret = 0;


/**
 * Create a fresh test vector for a given operation label.
 *
 * @param vecs array of vectors to append the new vector to
 * @param vecname label for the operation of the vector
 * @returns the fresh test vector
 */
static json_t *
vec_for (json_t *vecs, const char *vecname)
{
  json_t *t = json_object ();

  json_object_set_new (t,
                       "operation",
                       json_string (vecname));
  json_array_append_new (vecs, t);
  return t;
}


/**
 * Add a base32crockford encoded value
 * to a test vector.
 *
 * @param vec test vector to add to
 * @param label label for the value
 * @param data data to add
 * @param size size of data
 */
static void
d2j (json_t *vec,
     const char *label,
     const void *data,
     size_t size)
{
  char *buf;
  json_t *json;

  buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
  json = json_string (buf);
  GNUNET_free (buf);
  GNUNET_break (NULL != json);

  json_object_set_new (vec, label, json);
}


static void
d2j_append (json_t *arr,
            const void *data,
            size_t size)
{
  char *buf;
  json_t *json;

  buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
  json = json_string (buf);
  GNUNET_free (buf);
  GNUNET_break (NULL != json);

  json_array_append_new (arr,
                         json);
}


#define d2j_auto(vec, label, d) d2j (vec, label, d, sizeof (*d))
#define d2j_append_auto(arr,  d) d2j_append (arr, d, sizeof (*d))
#define random_auto(d) GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, \
                                                   d, \
                                                   sizeof (*d));

/**
 * Add a number to a test vector.
 *
 * @param vec test vector to add to
 * @param label label for the value
 * @param data data to add
 * @param size size of data
 */
static void
uint2j (json_t *vec,
        const char *label,
        unsigned int num)
{
  json_t *json = json_integer (num);

  json_object_set_new (vec, label, json);
}


static int
expect_data_fixed (json_t *vec,
                   const char *name,
                   void *data,
                   size_t expect_len)
{
  const char *s = json_string_value (json_object_get (vec, name));

  if (NULL == s)
    return GNUNET_NO;

  if (GNUNET_OK != GNUNET_STRINGS_string_to_data (s,
                                                  strlen (s),
                                                  data,
                                                  expect_len))
    return GNUNET_NO;
  return GNUNET_OK;
}


static int
expect_data_dynamic (json_t *vec,
                     const char *name,
                     void **data,
                     size_t *ret_len)
{
  const char *s = json_string_value (json_object_get (vec, name));
  char *tmp;
  size_t len;

  if (NULL == s)
    return GNUNET_NO;

  len = (strlen (s) * 5) / 8;
  if (NULL != ret_len)
    *ret_len = len;
  tmp = GNUNET_malloc (len);

  if (GNUNET_OK != GNUNET_STRINGS_string_to_data (s, strlen (s), tmp, len))
  {
    GNUNET_free (tmp);
    return GNUNET_NO;
  }
  *data = tmp;
  return GNUNET_OK;
}


/**
 * Check a single vector.
 *
 * @param operation operator of the vector
 * @param vec the vector, a JSON object.
 *
 * @returns GNUNET_OK if the vector is okay
 */
static int
checkvec (const char *operation,
          json_t *vec)
{
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "checking %s\n", operation);

  if (0 == strcmp (operation, "hash"))
  {
    void *data;
    size_t data_len;
    struct GNUNET_HashCode hash_out;
    struct GNUNET_HashCode hc;

    if (GNUNET_OK != expect_data_dynamic (vec,
                                          "input",
                                          &data,
                                          &data_len))
    {
      GNUNET_break (0);
      return GNUNET_SYSERR;
    }
    if (GNUNET_OK != expect_data_fixed (vec,
                                        "output",
                                        &hash_out,
                                        sizeof (hash_out)))
    {
      GNUNET_free (data);
      GNUNET_break (0);
      return GNUNET_NO;
    }

    GNUNET_CRYPTO_hash (data, data_len, &hc);

    if (0 != GNUNET_memcmp (&hc, &hash_out))
    {
      GNUNET_free (data);
      GNUNET_break (0);
      return GNUNET_NO;
    }
    GNUNET_free (data);
  }

  return GNUNET_OK;
}


/**
 * Check test vectors from stdin.
 *
 * @returns global exit code
 */
static int
check_vectors ()
{
  json_error_t err;
  json_t *vecfile = json_loadf (stdin, 0, &err);
  const char *encoding;
  json_t *vectors;

  if (NULL == vecfile)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "unable to parse JSON\n");
    return 1;
  }
  encoding = json_string_value (json_object_get (vecfile,
                                                 "encoding"));
  if ( (NULL == encoding) || (0 != strcmp (encoding, "base32crockford")) )
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "unsupported or missing encoding\n");
    json_decref (vecfile);
    return 1;
  }
  vectors = json_object_get (vecfile, "vectors");
  if (! json_is_array (vectors))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bad vectors\n");
    json_decref (vecfile);
    return 1;
  }
  {
    /* array is a JSON array */
    size_t index;
    json_t *value;
    int ret;

    json_array_foreach (vectors, index, value) {
      const char *op = json_string_value (json_object_get (value,
                                                           "operation"));

      if (NULL == op)
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "missing operation\n");
        ret = GNUNET_SYSERR;
        break;
      }
      ret = checkvec (op, value);
      if (GNUNET_OK != ret)
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "bad vector %u\n",
                    (unsigned int) index);
        break;
      }
    }
    return (ret == GNUNET_OK) ? 0 : 1;
  }
}


/**
 * Output test vectors.
 *
 * @returns global exit code
 */
static int
output_vectors ()
{
  json_t *vecfile = json_object ();
  json_t *vecs = json_array ();

  json_object_set_new (vecfile,
                       "encoding",
                       json_string ("base32crockford"));
  json_object_set_new (vecfile,
                       "producer",
                       json_string (
                         "GNU Anastasis (C implementation) " PACKAGE_VERSION " "
                         VCS_VERSION));
  json_object_set_new (vecfile,
                       "vectors",
                       vecs);

  {
    json_t *vec = vec_for (vecs, "hash");
    struct GNUNET_HashCode hc;
    char *str = "Hello, GNUnet";

    GNUNET_CRYPTO_hash (str, strlen (str), &hc);

    d2j (vec, "input", str, strlen (str));
    d2j (vec, "output", &hc, sizeof (struct GNUNET_HashCode));
  }

  {
    json_t *vec = vec_for (vecs, "user_identifier_derive");
    struct ANASTASIS_CRYPTO_ProviderSaltP server_salt;
    struct ANASTASIS_CRYPTO_UserIdentifierP id;
    json_t *id_data = json_pack ("{s:s, s:s}",
                                 "name",
                                 "Fleabag",
                                 "ssn",
                                 "AB123");
    GNUNET_assert (NULL != id_data);
    random_auto (&server_salt);

    ANASTASIS_CRYPTO_user_identifier_derive (id_data,
                                             &server_salt,
                                             &id);
    json_object_set_new (vec, "input_id_data", id_data);
    d2j_auto (vec, "input_server_salt", &server_salt);
    d2j_auto (vec, "output_id", &id);
  }

  {
    json_t *vec = vec_for (vecs, "account_keypair_derive");
    struct ANASTASIS_CRYPTO_UserIdentifierP id;
    struct ANASTASIS_CRYPTO_AccountPrivateKeyP priv_key;
    struct ANASTASIS_CRYPTO_AccountPublicKeyP pub_key;

    random_auto (&id);
    ANASTASIS_CRYPTO_account_public_key_derive (&id, &pub_key);
    ANASTASIS_CRYPTO_account_private_key_derive (&id, &priv_key);

    d2j_auto (vec, "input_id", &id);
    d2j_auto (vec, "output_priv_key", &priv_key);
    d2j_auto (vec, "output_pub_key", &pub_key);

  }

  {
    json_t *vec = vec_for (vecs, "secure_answer_hash");
    const char *answer = "Blah";
    struct ANASTASIS_CRYPTO_TruthUUIDP uuid;
    struct ANASTASIS_CRYPTO_QuestionSaltP salt;
    struct GNUNET_HashCode result;

    random_auto (&uuid);
    random_auto (&salt);
    ANASTASIS_CRYPTO_secure_answer_hash (answer, &uuid, &salt, &result);
    json_object_set_new (vec, "input_answer", json_string (answer));
    d2j_auto (vec, "input_uuid", &uuid);
    d2j_auto (vec, "input_salt", &salt);
    d2j_auto (vec, "output_hash", &result);
  }

  {
    json_t *vec = vec_for (vecs, "recovery_document_encryption");
    struct ANASTASIS_CRYPTO_UserIdentifierP id;
    void *rec_doc = "my recovery doc";
    size_t rd_size = strlen (rec_doc) + 1;
    void *enc_rec_doc;
    size_t erd_size;

    random_auto (&id);

    ANASTASIS_CRYPTO_recovery_document_encrypt (&id,
                                                rec_doc,
                                                rd_size,
                                                &enc_rec_doc,
                                                &erd_size);
    d2j_auto (vec, "input_user_id", &id);
    d2j (vec, "input_recovery_document", rec_doc, rd_size);
    d2j (vec, "output_encrypted_recovery_document", &enc_rec_doc, erd_size);
  }

  {
    /* With extra salt */
    json_t *vec = vec_for (vecs, "keyshare_encryption");
    struct ANASTASIS_CRYPTO_KeyShareP key_share;
    struct ANASTASIS_CRYPTO_UserIdentifierP id;
    char *xsalt = "myanswer";
    struct ANASTASIS_CRYPTO_EncryptedKeyShareP enc_key_share;

    random_auto (&key_share);
    random_auto (&id);

    ANASTASIS_CRYPTO_keyshare_encrypt (&key_share,
                                       &id,
                                       xsalt,
                                       &enc_key_share);
    d2j_auto (vec, "input_key_share", &key_share);
    d2j_auto (vec, "input_user_id", &id);
    json_object_set_new (vec, "input_xsalt", json_string (xsalt));
    d2j_auto (vec, "output_enc_key_share", &enc_key_share);
  }

  {
    /* Without extra salt */
    json_t *vec = vec_for (vecs, "keyshare_encryption");
    struct ANASTASIS_CRYPTO_KeyShareP key_share;
    struct ANASTASIS_CRYPTO_UserIdentifierP id;
    char *xsalt = NULL;
    struct ANASTASIS_CRYPTO_EncryptedKeyShareP enc_key_share;

    random_auto (&key_share);
    random_auto (&id);

    ANASTASIS_CRYPTO_keyshare_encrypt (&key_share,
                                       &id,
                                       xsalt,
                                       &enc_key_share);
    d2j_auto (vec, "input_key_share", &key_share);
    d2j_auto (vec, "input_user_id", &id);
    json_object_set_new (vec, "input_xsalt", json_null ());
    d2j_auto (vec, "output_enc_key_share", &enc_key_share);
  }

  {
    json_t *vec = vec_for (vecs, "truth_encryption");

    struct ANASTASIS_CRYPTO_NonceP nonce;
    struct ANASTASIS_CRYPTO_TruthKeyP truth_enc_key;
    char truth[256];
    size_t truth_size = 256;
    void *enc_truth;
    size_t ect_size;

    random_auto (&nonce);
    random_auto (&truth);
    random_auto (&truth_enc_key);

    ANASTASIS_CRYPTO_truth_encrypt (&nonce,
                                    &truth_enc_key,
                                    truth,
                                    truth_size,
                                    &enc_truth,
                                    &ect_size);

    d2j_auto (vec, "input_nonce", &nonce);
    d2j_auto (vec, "input_truth_enc_key", &truth_enc_key);
    d2j (vec, "input_truth", &truth, truth_size);
    d2j (vec, "output_encrypted_truth", enc_truth, ect_size);
  }

  {
    json_t *vec = vec_for (vecs, "policy_key_derive");

    struct ANASTASIS_CRYPTO_KeyShareP key_shares[2];
    unsigned int keyshare_length = 2;
    struct ANASTASIS_CRYPTO_MasterSaltP salt;
    struct ANASTASIS_CRYPTO_PolicyKeyP policy_key;
    json_t *key_shares_json = json_array ();

    random_auto (&key_shares[0]);
    random_auto (&key_shares[1]);
    random_auto (&salt);

    ANASTASIS_CRYPTO_policy_key_derive (key_shares,
                                        keyshare_length,
                                        &salt,
                                        &policy_key);

    d2j_append_auto (key_shares_json, &key_shares[0]);
    d2j_append_auto (key_shares_json, &key_shares[1]);
    json_object_set_new (vec, "input_key_shares", key_shares_json);
    d2j_auto (vec, "input_salt", &salt);
    d2j_auto (vec, "output_policy_key", &policy_key);
  }

  {
    // json_t *vec = vec_for (vecs, "core_secret_encryption");
    // struct ANASTASIS_CRYPTO_PolicyKeyP policy_keys[2];
    // unsigned int policy_keys_length = 2;
    // char core_secret[256];
    // size_t core_secret_size = 256;
    // void *enc_core_secret;
    // struct ANASTASIS_CRYPTO_EncryptedMasterKeyP encrypted_master_keys[2];
    // json_t *policy_keys_json = json_array ();
    // json_t *encrypted_master_keys_json = json_array ();

    // random_auto (&policy_keys[0]);
    // random_auto (&policy_keys[1]);
    // random_auto (&core_secret);

    // ANASTASIS_CRYPTO_core_secret_encrypt (policy_keys, policy_keys_length,
    //                                       core_secret, core_secret_size,
    //                                       &enc_core_secret,
    //                                       encrypted_master_keys);

    // d2j_append_auto (policy_keys_json, &policy_keys_json[0]);
    // d2j_append_auto (policy_keys_json, &policy_keys_json[1]);
    // d2j_append_auto (encrypted_master_keys_json, &encrypted_master_keys[0]);
    // d2j_append_auto (encrypted_master_keys_json, &encrypted_master_keys[1]);

    // d2j_auto (vec, "input_core_secret", &core_secret);
    // json_object_set_new (vec, "input_policy_keys", policy_keys_json);
    // json_object_set_new (vec, "output_encrypted_core_secret", encrypted_master_keys_json);
    // json_object_set_new (vec, "output_encrypted_master_keys", encrypted_master_keys_json);
  }


  json_dumpf (vecfile, stdout, JSON_INDENT (2));
  json_decref (vecfile);
  printf ("\n");

  return 0;
}


/**
 * Main function that will be run.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param cfg configuration
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  if (GNUNET_YES == verify_flag)
    global_ret = check_vectors ();
  else
    global_ret = output_vectors ();
}


/**
 * The main function of the test vector generation tool.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc,
      char *const *argv)
{
  const struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_option_flag ('V',
                               "verify",
                               gettext_noop (
                                 "verify a test vector from stdin"),
                               &verify_flag),
    GNUNET_GETOPT_OPTION_END
  };

  GNUNET_assert (GNUNET_OK ==
                 GNUNET_log_setup ("anastasis-crypto-tvg",
                                   "INFO",
                                   NULL));
  if (GNUNET_OK !=
      GNUNET_PROGRAM_run (argc, argv,
                          "anastasis-crypto-tvg",
                          "Generate test vectors for cryptographic operations",
                          options,
                          &run, NULL))
    return 1;
  return global_ret;
}


/* end of anastasis-crypto-tvg.c */