summaryrefslogtreecommitdiff
path: root/src/util/anastasis_crypto.c
blob: 70e3d8760dff7c697c144f14d41adf8417739f3b (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
/*
  This file is part of Anastasis
  Copyright (C) 2020 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.c
 * @brief anastasis crypto api
 * @author Christian Grothoff
 * @author Dominik Meister
 * @author Dennis Neufeld
 */

#include "platform.h"
#include "anastasis_crypto_lib.h"
#include <gcrypt.h>
#include <taler/taler_json_lib.h>
#include <gnunet/gnunet_util_lib.h>
#include <string.h>

#if defined(DEBUG) || defined(DEBUG2)
#define SCRYPT_ITERATION 1

#else
#define SCRYPT_ITERATION 1000
#endif


void
ANASTASIS_hash_answer (uint64_t code,
                       struct GNUNET_HashCode *hashed_code)
{
  char cbuf[40];

  GNUNET_snprintf (cbuf,
                   sizeof (cbuf),
                   "%llu",
                   (unsigned long long) code);
  GNUNET_CRYPTO_hash (cbuf,
                      strlen (cbuf),
                      hashed_code);
}


void
ANASTASIS_CRYPTO_secure_answer_hash (
  const char *answer,
  const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid,
  const struct ANASTASIS_CRYPTO_QuestionSaltP *salt,
  struct GNUNET_HashCode *result)
{
  struct GNUNET_HashCode pow;

  GNUNET_CRYPTO_pow_hash (&salt->pow_salt,
                          answer,
                          strlen (answer),
                          &pow);
  GNUNET_assert (GNUNET_YES ==
                 GNUNET_CRYPTO_kdf (
                   result,
                   sizeof (*result),
                   "Anastasis-secure-question-uuid-salting",
                   strlen ("Anastasis-secure-question-uuid-salting"),
                   &pow,
                   sizeof (pow),
                   uuid,
                   sizeof (*uuid),
                   NULL,
                   0));
}


/**
 * Compute @a key and @a iv.
 *
 * @param key_material key for calculation
 * @param key_m_len length of key
 * @param nonce nonce for calculation
 * @param salt salt value for calculation
 * @param[out] key where to write the en-/description key
 * @param[out] iv where to write the IV
 */
static void
get_iv_key (const void *key_material,
            size_t key_m_len,
            const struct ANASTASIS_CRYPTO_NonceP *nonce,
            const char *salt,
            const struct ANASTASIS_CRYPTO_SymKeyP *key,
            struct ANASTASIS_CRYPTO_IvP *iv)
{
  char res[sizeof (struct ANASTASIS_CRYPTO_SymKeyP)
           + sizeof (struct ANASTASIS_CRYPTO_IvP)];

  if (GNUNET_YES !=
      GNUNET_CRYPTO_hkdf (res,
                          sizeof (res),
                          GCRY_MD_SHA512,
                          GCRY_MD_SHA256,
                          key_material,
                          key_m_len,
                          nonce,
                          sizeof (struct ANASTASIS_CRYPTO_NonceP),
                          salt,
                          strlen (salt),
                          NULL,
                          0))
  {
    GNUNET_break (0);
    return;
  }
  memcpy ((void *) key,
          res,
          sizeof (*key));
  memcpy (iv,
          &res[sizeof (*key)],
          sizeof (*iv));
}


/**
 * Encryption of data like recovery document etc.
 *
 * @param nonce value to use for the nonce
 * @param key key which is used to derive a key/iv pair from
 * @param key_len length of key
 * @param data data to encrypt
 * @param data_size size of the data
 * @param salt salt value which is used for key derivation
 * @param[out] res ciphertext output
 * @param[out] res_size size of the ciphertext
 */
static void
anastasis_encrypt (const struct ANASTASIS_CRYPTO_NonceP *nonce,
                   const void *key,
                   size_t key_len,
                   const void *data,
                   size_t data_size,
                   const char *salt,
                   void **res,
                   size_t *res_size)
{
  struct ANASTASIS_CRYPTO_NonceP *nonceptr;
  gcry_cipher_hd_t cipher;
  struct ANASTASIS_CRYPTO_SymKeyP sym_key;
  struct ANASTASIS_CRYPTO_IvP iv;
  int rc;
  struct ANASTASIS_CRYPTO_AesTagP *tag;
  char *ciphertext;

  *res_size = data_size
              + sizeof (struct ANASTASIS_CRYPTO_NonceP)
              + sizeof (struct ANASTASIS_CRYPTO_AesTagP);
  if (*res_size <= data_size)
  {
    GNUNET_break (0);
    return;
  }
  *res = GNUNET_malloc (*res_size);
  if (*res_size != data_size
      + sizeof (struct ANASTASIS_CRYPTO_NonceP)
      + sizeof (struct ANASTASIS_CRYPTO_AesTagP))
  {
    GNUNET_break (0);
    return;
  }
  nonceptr = (struct ANASTASIS_CRYPTO_NonceP *) *res;
  tag = (struct ANASTASIS_CRYPTO_AesTagP *) &nonceptr[1];
  ciphertext = (char *) &tag[1];
  memcpy (nonceptr,
          nonce,
          sizeof (*nonce));
  get_iv_key (key,
              key_len,
              nonce,
              salt,
              &sym_key,
              &iv);
  GNUNET_assert (0 ==
                 gcry_cipher_open (&cipher,
                                   GCRY_CIPHER_AES256,
                                   GCRY_CIPHER_MODE_GCM,
                                   0));
  rc = gcry_cipher_setkey (cipher,
                           &sym_key,
                           sizeof (sym_key));
  GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
  rc = gcry_cipher_setiv (cipher,
                          &iv,
                          sizeof (iv));
  GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));

  GNUNET_assert (0 ==
                 gcry_cipher_encrypt (cipher,
                                      ciphertext,
                                      data_size,
                                      data,
                                      data_size));
  GNUNET_assert (0 ==
                 gcry_cipher_gettag (cipher,
                                     tag,
                                     sizeof (struct ANASTASIS_CRYPTO_AesTagP)));
  gcry_cipher_close (cipher);
}


/**
 * Decryption of data like encrypted recovery document etc.
 *
 * @param key key which is used to derive a key/iv pair from
 * @param key_len length of key
 * @param data data to decrypt
 * @param data_size size of the data
 * @param salt salt value which is used for key derivation
 * @param[out] res plaintext output
 * @param[out] res_size size of the plaintext
 */
static void
anastasis_decrypt (const void *key,
                   size_t key_len,
                   const void *data,
                   size_t data_size,
                   const char *salt,
                   void **res,
                   size_t *res_size)
{
  const struct ANASTASIS_CRYPTO_NonceP *nonce;
  gcry_cipher_hd_t cipher;
  const struct ANASTASIS_CRYPTO_SymKeyP sym_key;
  struct ANASTASIS_CRYPTO_IvP iv;
  int rc;
  const struct ANASTASIS_CRYPTO_AesTagP *tag;
  const char *ciphertext;

  *res_size = data_size
              - sizeof (struct ANASTASIS_CRYPTO_NonceP)
              - sizeof (struct ANASTASIS_CRYPTO_AesTagP);
  if (*res_size >= data_size)
  {
    GNUNET_break (0);
    return;
  }
  *res = GNUNET_malloc (*res_size);
  if (*res_size != data_size
      - sizeof (struct ANASTASIS_CRYPTO_NonceP)
      - sizeof (struct ANASTASIS_CRYPTO_AesTagP))
  {
    GNUNET_break (0);
    GNUNET_free (*res);
    return;
  }

  nonce = (const struct ANASTASIS_CRYPTO_NonceP *) data;
  tag = (struct ANASTASIS_CRYPTO_AesTagP *) &nonce[1];
  ciphertext = (const char *) &tag[1];
  get_iv_key (key,
              key_len,
              nonce,
              salt,
              &sym_key,
              &iv);
  GNUNET_assert (0 ==
                 gcry_cipher_open (&cipher,
                                   GCRY_CIPHER_AES256,
                                   GCRY_CIPHER_MODE_GCM,
                                   0));
  rc = gcry_cipher_setkey (cipher,
                           &sym_key,
                           sizeof (sym_key));
  GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));

  rc = gcry_cipher_setiv (cipher,
                          &iv,
                          sizeof (iv));
  GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));

  GNUNET_assert (0 == gcry_cipher_decrypt (cipher,
                                           *res,
                                           *res_size,
                                           ciphertext,
                                           *res_size));
  if (0 !=
      gcry_cipher_checktag (cipher,
                            tag,
                            sizeof (struct ANASTASIS_CRYPTO_AesTagP)))
  {
    GNUNET_break (0);
    GNUNET_free (*res);
    return;
  }
  gcry_cipher_close (cipher);
}


void
ANASTASIS_CRYPTO_user_identifier_derive (
  const json_t *id_data,
  const struct ANASTASIS_CRYPTO_ProviderSaltP *server_salt,
  struct ANASTASIS_CRYPTO_UserIdentifierP *id)
{
  char *json_enc;
  struct GNUNET_HashCode hash;

  json_enc = json_dumps (id_data,
                         JSON_COMPACT | JSON_SORT_KEYS);
  GNUNET_assert (NULL != json_enc);
  GNUNET_CRYPTO_pow_hash (&server_salt->salt,
                          json_enc,
                          strlen (json_enc),
                          &hash);
  id->hash = hash;
  free (json_enc);
}


void
ANASTASIS_CRYPTO_account_private_key_derive (
  const struct ANASTASIS_CRYPTO_UserIdentifierP *id,
  struct ANASTASIS_CRYPTO_AccountPrivateKeyP *priv_key)
{
  /* priv_key = ver_secret */
  if (GNUNET_YES !=
      GNUNET_CRYPTO_hkdf (&priv_key->priv,
                          sizeof (priv_key->priv),
                          GCRY_MD_SHA512,
                          GCRY_MD_SHA256,
                          id,
                          sizeof (struct ANASTASIS_CRYPTO_UserIdentifierP),
                          "ver",
                          strlen ("ver"),
                          NULL,
                          0))
  {
    GNUNET_break (0);
    return;
  }
  /* go from ver_secret to proper private key (eddsa_d_to_a() in spec) */
  priv_key->priv.d[0] = (priv_key->priv.d[0] & 0x7f) | 0x40;
  priv_key->priv.d[31] &= 0xf8;
}


void
ANASTASIS_CRYPTO_account_public_key_derive (
  const struct ANASTASIS_CRYPTO_UserIdentifierP *id,
  struct ANASTASIS_CRYPTO_AccountPublicKeyP *pub_key)
{
  struct ANASTASIS_CRYPTO_AccountPrivateKeyP priv;

  ANASTASIS_CRYPTO_account_private_key_derive (id,
                                               &priv);
  GNUNET_CRYPTO_eddsa_key_get_public (&priv.priv,
                                      &pub_key->pub);
}


void
ANASTASIS_CRYPTO_recovery_document_encrypt (
  const struct ANASTASIS_CRYPTO_UserIdentifierP *id,
  const void *rec_doc,
  size_t rd_size,
  void **enc_rec_doc,
  size_t *erd_size)
{
  const char *salt = "erd";
  struct ANASTASIS_CRYPTO_NonceP nonce;

  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
                              &nonce,
                              sizeof (nonce));
  anastasis_encrypt (&nonce,
                     id,
                     sizeof (struct ANASTASIS_CRYPTO_UserIdentifierP),
                     rec_doc,
                     rd_size,
                     salt,
                     enc_rec_doc,
                     erd_size);
}


void
ANASTASIS_CRYPTO_recovery_document_decrypt (
  const struct ANASTASIS_CRYPTO_UserIdentifierP *id,
  const void *enc_rec_doc,
  size_t erd_size,
  void **rec_doc,
  size_t *rd_size)
{
  const char *salt = "erd";

  anastasis_decrypt (id,
                     sizeof (struct ANASTASIS_CRYPTO_UserIdentifierP),
                     enc_rec_doc,
                     erd_size,
                     salt,
                     rec_doc,
                     rd_size);
}


void
ANASTASIS_CRYPTO_keyshare_encrypt (
  const struct ANASTASIS_CRYPTO_KeyShareP *key_share,
  const struct ANASTASIS_CRYPTO_UserIdentifierP *id,
  const char *xsalt,
  struct ANASTASIS_CRYPTO_EncryptedKeyShareP *enc_key_share)
{
  const char *salt = "eks";
  size_t eks_size = 0;
  void *eks = NULL;
  struct ANASTASIS_CRYPTO_NonceP nonce;

  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
                              &nonce,
                              sizeof (nonce));
  anastasis_encrypt (&nonce,
                     id,
                     sizeof (struct ANASTASIS_CRYPTO_UserIdentifierP),
                     key_share,
                     sizeof (struct ANASTASIS_CRYPTO_KeyShareP),
                     (NULL == xsalt) ? salt : xsalt,
                     &eks,
                     &eks_size);
  GNUNET_assert (eks_size ==
                 sizeof (struct ANASTASIS_CRYPTO_EncryptedKeyShareP));
  memcpy (enc_key_share,
          eks,
          sizeof (struct ANASTASIS_CRYPTO_EncryptedKeyShareP));
  GNUNET_free (eks);
}


void
ANASTASIS_CRYPTO_keyshare_decrypt (
  const struct ANASTASIS_CRYPTO_EncryptedKeyShareP *enc_key_share,
  const struct ANASTASIS_CRYPTO_UserIdentifierP *id,
  const char *xsalt,
  struct ANASTASIS_CRYPTO_KeyShareP *key_share)
{
  const char *salt = "eks";
  size_t ks_size = 0;
  void *ks = NULL;

  anastasis_decrypt (id,
                     sizeof (struct ANASTASIS_CRYPTO_UserIdentifierP),
                     enc_key_share,
                     sizeof (struct ANASTASIS_CRYPTO_EncryptedKeyShareP),
                     (NULL == xsalt) ? salt : xsalt,
                     &ks,
                     &ks_size);
  GNUNET_assert (ks_size ==
                 sizeof (struct ANASTASIS_CRYPTO_KeyShareP));
  memcpy (key_share,
          ks,
          sizeof (struct ANASTASIS_CRYPTO_KeyShareP));
  GNUNET_free (ks);
}


void
ANASTASIS_CRYPTO_truth_encrypt (
  const struct ANASTASIS_CRYPTO_NonceP *nonce,
  const struct ANASTASIS_CRYPTO_TruthKeyP *truth_enc_key,
  const void *truth,
  size_t truth_size,
  void **enc_truth,
  size_t *ect_size)
{
  const char *salt = "ect";

  anastasis_encrypt (nonce,
                     truth_enc_key,
                     sizeof (struct ANASTASIS_CRYPTO_TruthKeyP),
                     truth,
                     truth_size,
                     salt,
                     enc_truth,
                     ect_size);
}


void
ANASTASIS_CRYPTO_truth_decrypt (
  const struct ANASTASIS_CRYPTO_TruthKeyP *truth_enc_key,
  const void *enc_truth,
  size_t ect_size,
  void **truth,
  size_t *truth_size)
{
  const char *salt = "ect";

  anastasis_decrypt (truth_enc_key,
                     sizeof (struct ANASTASIS_CRYPTO_TruthKeyP),
                     enc_truth,
                     ect_size,
                     salt,
                     truth,
                     truth_size);
}


void
ANASTASIS_CRYPTO_keyshare_create (
  struct ANASTASIS_CRYPTO_KeyShareP *key_share)
{
  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
                              key_share,
                              sizeof (struct ANASTASIS_CRYPTO_KeyShareP));
}


void
ANASTASIS_CRYPTO_policy_key_derive (
  const struct ANASTASIS_CRYPTO_KeyShareP *key_shares,
  unsigned int keyshare_length,
  const struct ANASTASIS_CRYPTO_MasterSaltP *salt,
  struct ANASTASIS_CRYPTO_PolicyKeyP *policy_key)
{
  GNUNET_CRYPTO_hkdf (policy_key,
                      sizeof (*policy_key),
                      GCRY_MD_SHA512,
                      GCRY_MD_SHA256,
                      key_shares,
                      keyshare_length * sizeof (*key_shares),
                      salt,
                      sizeof (*salt),
                      NULL, 0);
}


void
ANASTASIS_CRYPTO_core_secret_encrypt (
  const struct ANASTASIS_CRYPTO_PolicyKeyP *policy_keys,
  unsigned int policy_keys_length,
  const void *core_secret,
  size_t core_secret_size,
  void **enc_core_secret,
  struct ANASTASIS_CRYPTO_EncryptedMasterKeyP *encrypted_master_keys)
{
  struct GNUNET_CRYPTO_SymmetricSessionKey sk;
  struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
  struct GNUNET_HashCode master_key;

  *enc_core_secret = GNUNET_malloc (core_secret_size);
  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
                              &master_key,
                              sizeof (struct GNUNET_HashCode));
  GNUNET_CRYPTO_hash_to_aes_key (&master_key,
                                 &sk,
                                 &iv);
  GNUNET_assert (GNUNET_SYSERR !=
                 GNUNET_CRYPTO_symmetric_encrypt (core_secret,
                                                  core_secret_size,
                                                  &sk,
                                                  &iv,
                                                  *enc_core_secret));
  for (unsigned int i = 0; i < policy_keys_length; i++)
  {
    struct GNUNET_CRYPTO_SymmetricSessionKey i_sk;
    struct GNUNET_CRYPTO_SymmetricInitializationVector i_iv;
    struct GNUNET_HashCode key = policy_keys[i].key;

    GNUNET_CRYPTO_hash_to_aes_key (&key,
                                   &i_sk,
                                   &i_iv);
    GNUNET_assert (
      GNUNET_SYSERR !=
      GNUNET_CRYPTO_symmetric_encrypt (&master_key,
                                       sizeof (struct GNUNET_HashCode),
                                       &i_sk,
                                       &i_iv,
                                       &encrypted_master_keys[i]));
  }
}


void
ANASTASIS_CRYPTO_core_secret_recover (
  const struct ANASTASIS_CRYPTO_EncryptedMasterKeyP *encrypted_master_key,
  const struct ANASTASIS_CRYPTO_PolicyKeyP *policy_key,
  const void *encrypted_core_secret,
  size_t encrypted_core_secret_size,
  void **core_secret,
  size_t *core_secret_size)
{
  struct GNUNET_CRYPTO_SymmetricSessionKey mk_sk;
  struct GNUNET_CRYPTO_SymmetricInitializationVector mk_iv;
  struct GNUNET_CRYPTO_SymmetricSessionKey core_sk;
  struct GNUNET_CRYPTO_SymmetricInitializationVector core_iv;
  struct GNUNET_HashCode master_key;
  struct GNUNET_HashCode key = policy_key->key;

  *core_secret = GNUNET_malloc (encrypted_core_secret_size);
  GNUNET_CRYPTO_hash_to_aes_key (&key,
                                 &mk_sk,
                                 &mk_iv);
  GNUNET_assert (
    GNUNET_SYSERR !=
    GNUNET_CRYPTO_symmetric_decrypt (
      encrypted_master_key,
      sizeof (struct ANASTASIS_CRYPTO_EncryptedMasterKeyP),
      &mk_sk,
      &mk_iv,
      &master_key));
  GNUNET_CRYPTO_hash_to_aes_key (&master_key,
                                 &core_sk,
                                 &core_iv);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "At %s:%d encrypted core secret is %s-%llu b\n", __FILE__,
              __LINE__,
              TALER_b2s (encrypted_core_secret, encrypted_core_secret_size),
              (unsigned long long) encrypted_core_secret_size);
  *core_secret_size = GNUNET_CRYPTO_symmetric_decrypt (encrypted_core_secret,
                                                       encrypted_core_secret_size,
                                                       &core_sk,
                                                       &core_iv,
                                                       *core_secret);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "At %s:%d decrypted core secret is %s-%llu b\n", __FILE__,
              __LINE__,
              TALER_b2s (*core_secret, *core_secret_size),
              (unsigned long long) *core_secret_size);
  GNUNET_assert (GNUNET_SYSERR != *core_secret_size);
}


/* end of anastasis_crypto.c */