exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

crypto.c (26481B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2014-2022 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file util/crypto.c
     18  * @brief Cryptographic utility functions
     19  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
     20  * @author Florian Dold
     21  * @author Benedikt Mueller
     22  * @author Christian Grothoff
     23  * @author Özgür Kesim
     24  */
     25 #include "platform.h"
     26 #include "taler/taler_util.h"
     27 #include <gcrypt.h>
     28 
     29 /**
     30  * Function called by libgcrypt on serious errors.
     31  * Prints an error message and aborts the process.
     32  *
     33  * @param cls NULL
     34  * @param wtf unknown
     35  * @param msg error message
     36  */
     37 static void
     38 fatal_error_handler (void *cls,
     39                      int wtf,
     40                      const char *msg)
     41 {
     42   (void) cls;
     43   (void) wtf;
     44   fprintf (stderr,
     45            "Fatal error in libgcrypt: %s\n",
     46            msg);
     47   abort ();
     48 }
     49 
     50 
     51 /**
     52  * Initialize libgcrypt.
     53  */
     54 void __attribute__ ((constructor))
     55 TALER_gcrypt_init ()
     56 {
     57   gcry_set_fatalerror_handler (&fatal_error_handler,
     58                                NULL);
     59   if (! gcry_check_version (NEED_LIBGCRYPT_VERSION))
     60   {
     61     fprintf (stderr,
     62              "libgcrypt version mismatch\n");
     63     abort ();
     64   }
     65   /* Disable secure memory (we should never run on a system that
     66      even uses swap space for memory). */
     67   gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
     68   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
     69 }
     70 
     71 
     72 enum GNUNET_GenericReturnValue
     73 TALER_test_coin_valid (
     74   const struct TALER_CoinPublicInfo *coin_public_info,
     75   const struct TALER_DenominationPublicKey *denom_pub)
     76 {
     77   struct TALER_CoinPubHashP c_hash;
     78 #if ENABLE_SANITY_CHECKS
     79   struct TALER_DenominationHashP d_hash;
     80 
     81   TALER_denom_pub_hash (denom_pub,
     82                         &d_hash);
     83   GNUNET_assert (0 ==
     84                  GNUNET_memcmp (&d_hash,
     85                                 &coin_public_info->denom_pub_hash));
     86 #endif
     87 
     88   TALER_coin_pub_hash (&coin_public_info->coin_pub,
     89                        coin_public_info->no_age_commitment
     90                           ? NULL
     91                           : &coin_public_info->h_age_commitment,
     92                        &c_hash);
     93 
     94   if (GNUNET_OK !=
     95       TALER_denom_pub_verify (denom_pub,
     96                               &coin_public_info->denom_sig,
     97                               &c_hash))
     98   {
     99     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    100                 "coin signature is invalid\n");
    101     return GNUNET_NO;
    102   }
    103   return GNUNET_YES;
    104 }
    105 
    106 
    107 void
    108 TALER_link_derive_transfer_secret (
    109   const struct TALER_CoinSpendPrivateKeyP *coin_priv,
    110   const struct TALER_TransferPrivateKeyP *trans_priv,
    111   struct TALER_TransferSecretP *ts)
    112 {
    113   struct TALER_CoinSpendPublicKeyP coin_pub;
    114 
    115   GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv->eddsa_priv,
    116                                       &coin_pub.eddsa_pub);
    117   GNUNET_assert (GNUNET_OK ==
    118                  GNUNET_CRYPTO_ecdh_eddsa (&trans_priv->ecdhe_priv,
    119                                            &coin_pub.eddsa_pub,
    120                                            &ts->key));
    121 }
    122 
    123 
    124 void
    125 TALER_link_reveal_transfer_secret (
    126   const struct TALER_TransferPrivateKeyP *trans_priv,
    127   const struct TALER_CoinSpendPublicKeyP *coin_pub,
    128   struct TALER_TransferSecretP *transfer_secret)
    129 {
    130   GNUNET_assert (GNUNET_OK ==
    131                  GNUNET_CRYPTO_ecdh_eddsa (&trans_priv->ecdhe_priv,
    132                                            &coin_pub->eddsa_pub,
    133                                            &transfer_secret->key));
    134 }
    135 
    136 
    137 void
    138 TALER_link_recover_transfer_secret (
    139   const struct TALER_TransferPublicKeyP *trans_pub,
    140   const struct TALER_CoinSpendPrivateKeyP *coin_priv,
    141   struct TALER_TransferSecretP *transfer_secret)
    142 {
    143   GNUNET_assert (GNUNET_OK ==
    144                  GNUNET_CRYPTO_eddsa_ecdh (&coin_priv->eddsa_priv,
    145                                            &trans_pub->ecdhe_pub,
    146                                            &transfer_secret->key));
    147 }
    148 
    149 
    150 void
    151 TALER_withdraw_expand_secrets (
    152   size_t num_coins,
    153   const struct TALER_WithdrawMasterSeedP *seed,
    154   struct TALER_PlanchetMasterSecretP secrets[static num_coins])
    155 {
    156   _Static_assert (sizeof (seed->seed_data) == sizeof (secrets->key_data));
    157   GNUNET_assert (0 < num_coins);
    158 
    159   if (1 == num_coins)
    160   {
    161     GNUNET_memcpy (&secrets[0].key_data,
    162                    &seed->seed_data,
    163                    sizeof (secrets[0].key_data));
    164   }
    165   else
    166   {
    167     uint32_t be_salt = htonl (num_coins);
    168 
    169     GNUNET_assert (GNUNET_OK ==
    170                    GNUNET_CRYPTO_hkdf_gnunet (
    171                      secrets,
    172                      sizeof (*secrets) * num_coins,
    173                      &be_salt,
    174                      sizeof (be_salt),
    175                      seed,
    176                      sizeof (*seed),
    177                      GNUNET_CRYPTO_kdf_arg_string ("taler-withdraw-secrets")));
    178   }
    179 }
    180 
    181 
    182 void
    183 TALER_withdraw_expand_kappa_seed (
    184   const struct TALER_WithdrawMasterSeedP *seed,
    185   struct TALER_KappaWithdrawMasterSeedP *seeds)
    186 {
    187   uint32_t be_salt = htonl (TALER_CNC_KAPPA);
    188 
    189   GNUNET_assert (GNUNET_OK ==
    190                  GNUNET_CRYPTO_hkdf_gnunet (
    191                    seeds,
    192                    sizeof (*seeds),
    193                    &be_salt,
    194                    sizeof (be_salt),
    195                    seed,
    196                    sizeof (*seed),
    197                    GNUNET_CRYPTO_kdf_arg_string ("taler-kappa-seeds")));
    198 }
    199 
    200 
    201 void
    202 TALER_planchet_master_setup_random (
    203   struct TALER_PlanchetMasterSecretP *ps)
    204 {
    205   GNUNET_CRYPTO_random_block (ps,
    206                               sizeof (*ps));
    207 }
    208 
    209 
    210 void
    211 TALER_withdraw_master_seed_setup_random (
    212   struct TALER_WithdrawMasterSeedP *seed)
    213 {
    214   GNUNET_CRYPTO_random_block (seed,
    215                               sizeof (*seed));
    216 }
    217 
    218 
    219 void
    220 TALER_refresh_master_setup_random (
    221   struct TALER_PublicRefreshMasterSeedP *rms)
    222 {
    223   GNUNET_CRYPTO_random_block (rms,
    224                               sizeof (*rms));
    225 }
    226 
    227 
    228 void
    229 TALER_transfer_secret_to_planchet_secret (
    230   const struct TALER_TransferSecretP *secret_seed,
    231   uint32_t coin_num_salt,
    232   struct TALER_PlanchetMasterSecretP *ps)
    233 {
    234   uint32_t be_salt = htonl (coin_num_salt);
    235 
    236   GNUNET_assert (GNUNET_OK ==
    237                  GNUNET_CRYPTO_hkdf_gnunet (
    238                    ps,
    239                    sizeof (*ps),
    240                    &be_salt,
    241                    sizeof (be_salt),
    242                    secret_seed,
    243                    sizeof (*secret_seed),
    244                    GNUNET_CRYPTO_kdf_arg_string ("taler-coin-derivation")));
    245 }
    246 
    247 
    248 void
    249 TALER_cs_withdraw_seed_to_blinding_seed (
    250   const struct TALER_WithdrawMasterSeedP *seed,
    251   struct TALER_BlindingMasterSeedP *blinding_seed)
    252 {
    253   GNUNET_assert (GNUNET_YES ==
    254                  GNUNET_CRYPTO_hkdf_gnunet (
    255                    blinding_seed,
    256                    sizeof (*blinding_seed),
    257                    "withdraw-blinding",
    258                    strlen ("withdraw-blinding"),
    259                    seed,
    260                    sizeof (*seed)));
    261 }
    262 
    263 
    264 void
    265 TALER_cs_refresh_seed_to_blinding_seed (
    266   const struct TALER_PublicRefreshMasterSeedP *seed,
    267   const struct TALER_CoinSpendPrivateKeyP *coin_priv,
    268   struct TALER_BlindingMasterSeedP *blinding_seed)
    269 {
    270   GNUNET_assert (GNUNET_YES ==
    271                  GNUNET_CRYPTO_hkdf_gnunet (
    272                    blinding_seed,
    273                    sizeof (*blinding_seed),
    274                    "refresh-blinding",
    275                    strlen ("refresh-blinding"),
    276                    coin_priv,
    277                    sizeof (*coin_priv),
    278                    GNUNET_CRYPTO_kdf_arg_auto (seed)));
    279 }
    280 
    281 
    282 void
    283 TALER_cs_nonce_derive_indexed (
    284   const struct TALER_BlindingMasterSeedP *seed,
    285   bool for_melt,
    286   uint32_t index,
    287   struct GNUNET_CRYPTO_CsSessionNonce *nonce)
    288 {
    289   uint32_t be_salt = htonl (index);
    290   const char *operation = for_melt ? "refresh-n" : "withdraw-n";
    291 
    292   GNUNET_assert (GNUNET_YES ==
    293                  GNUNET_CRYPTO_hkdf_gnunet (
    294                    nonce,
    295                    sizeof (*nonce),
    296                    &be_salt,
    297                    sizeof (be_salt),
    298                    operation,
    299                    strlen (operation),
    300                    GNUNET_CRYPTO_kdf_arg_auto (seed)));
    301 }
    302 
    303 
    304 void
    305 TALER_cs_derive_nonces_from_seed (
    306   const struct TALER_BlindingMasterSeedP *seed,
    307   bool for_melt,
    308   size_t num,
    309   const uint32_t indices[static num],
    310   struct GNUNET_CRYPTO_CsSessionNonce nonces[static num])
    311 {
    312   GNUNET_assert (TALER_MAX_COINS >= num);
    313 
    314   for (size_t i = 0; i < num; i++)
    315     TALER_cs_nonce_derive_indexed (
    316       seed,
    317       for_melt,
    318       indices[i],
    319       &nonces[i]);
    320 }
    321 
    322 
    323 void
    324 TALER_cs_derive_only_cs_blind_nonces_from_seed (
    325   const struct TALER_BlindingMasterSeedP *seed,
    326   bool for_melt,
    327   size_t num,
    328   const uint32_t indices[static num],
    329   union GNUNET_CRYPTO_BlindSessionNonce nonces[static num])
    330 {
    331   GNUNET_assert (TALER_MAX_COINS >= num);
    332 
    333   for (size_t i = 0; i < num; i++)
    334     TALER_cs_nonce_derive_indexed (
    335       seed,
    336       for_melt,
    337       indices[i],
    338       &nonces[i].cs_nonce);
    339 }
    340 
    341 
    342 void
    343 TALER_cs_derive_blind_nonces_from_seed (
    344   const struct TALER_BlindingMasterSeedP *seed,
    345   bool for_melt,
    346   size_t num,
    347   const bool is_cs[static num],
    348   union GNUNET_CRYPTO_BlindSessionNonce nonces[static num])
    349 {
    350   for (size_t i = 0; i < num; i++)
    351   {
    352     if (is_cs[i])
    353       TALER_cs_nonce_derive_indexed (seed,
    354                                      for_melt,
    355                                      i,
    356                                      &nonces[i].cs_nonce);
    357   }
    358 }
    359 
    360 
    361 void
    362 TALER_rsa_pub_hash (
    363   const struct GNUNET_CRYPTO_RsaPublicKey *rsa,
    364   struct TALER_RsaPubHashP *h_rsa)
    365 {
    366   GNUNET_CRYPTO_rsa_public_key_hash (rsa,
    367                                      &h_rsa->hash);
    368 }
    369 
    370 
    371 void
    372 TALER_cs_pub_hash (
    373   const struct GNUNET_CRYPTO_CsPublicKey *cs,
    374   struct TALER_CsPubHashP *h_cs)
    375 {
    376   GNUNET_CRYPTO_hash (cs,
    377                       sizeof (*cs),
    378                       &h_cs->hash);
    379 }
    380 
    381 
    382 enum GNUNET_GenericReturnValue
    383 TALER_planchet_prepare (
    384   const struct TALER_DenominationPublicKey *dk,
    385   const struct TALER_ExchangeBlindingValues *blinding_values,
    386   const union GNUNET_CRYPTO_BlindingSecretP *bks,
    387   const union GNUNET_CRYPTO_BlindSessionNonce *nonce,
    388   const struct TALER_CoinSpendPrivateKeyP *coin_priv,
    389   const struct TALER_AgeCommitmentHashP *ach,
    390   struct TALER_CoinPubHashP *c_hash,
    391   struct TALER_PlanchetDetail *pd)
    392 {
    393   struct TALER_CoinSpendPublicKeyP coin_pub;
    394 
    395   GNUNET_assert (blinding_values->blinding_inputs->cipher ==
    396                  dk->bsign_pub_key->cipher);
    397   GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv->eddsa_priv,
    398                                       &coin_pub.eddsa_pub);
    399   if (GNUNET_OK !=
    400       TALER_denom_blind (dk,
    401                          bks,
    402                          nonce,
    403                          ach,
    404                          &coin_pub,
    405                          blinding_values,
    406                          c_hash,
    407                          &pd->blinded_planchet))
    408   {
    409     GNUNET_break (0);
    410     return GNUNET_SYSERR;
    411   }
    412   TALER_denom_pub_hash (dk,
    413                         &pd->denom_pub_hash);
    414   return GNUNET_OK;
    415 }
    416 
    417 
    418 void
    419 TALER_planchet_detail_free (struct TALER_PlanchetDetail *pd)
    420 {
    421   TALER_blinded_planchet_free (&pd->blinded_planchet);
    422 }
    423 
    424 
    425 enum GNUNET_GenericReturnValue
    426 TALER_planchet_to_coin (
    427   const struct TALER_DenominationPublicKey *dk,
    428   const struct TALER_BlindedDenominationSignature *blind_sig,
    429   const union GNUNET_CRYPTO_BlindingSecretP *bks,
    430   const struct TALER_CoinSpendPrivateKeyP *coin_priv,
    431   const struct TALER_AgeCommitmentHashP *ach,
    432   const struct TALER_CoinPubHashP *c_hash,
    433   const struct TALER_ExchangeBlindingValues *alg_values,
    434   struct TALER_FreshCoin *coin)
    435 {
    436   if (dk->bsign_pub_key->cipher !=
    437       blind_sig->blinded_sig->cipher)
    438   {
    439     GNUNET_break_op (0);
    440     return GNUNET_SYSERR;
    441   }
    442   if (dk->bsign_pub_key->cipher !=
    443       alg_values->blinding_inputs->cipher)
    444   {
    445     GNUNET_break_op (0);
    446     return GNUNET_SYSERR;
    447   }
    448   if (GNUNET_OK !=
    449       TALER_denom_sig_unblind (&coin->sig,
    450                                blind_sig,
    451                                bks,
    452                                c_hash,
    453                                alg_values,
    454                                dk))
    455   {
    456     GNUNET_break_op (0);
    457     return GNUNET_SYSERR;
    458   }
    459   if (GNUNET_OK !=
    460       TALER_denom_pub_verify (dk,
    461                               &coin->sig,
    462                               c_hash))
    463   {
    464     GNUNET_break_op (0);
    465     TALER_denom_sig_free (&coin->sig);
    466     return GNUNET_SYSERR;
    467   }
    468 
    469   coin->coin_priv = *coin_priv;
    470   coin->h_age_commitment = ach;
    471   return GNUNET_OK;
    472 }
    473 
    474 
    475 /**
    476  * Compute the commitment for a /melt operation from the respective
    477  * public inputs.  Common implementation of all versions of the
    478  * refresh commitment, see `enum TALER_RefreshCommitmentVersion`.
    479  *
    480  * @param[out] rc set to the value the wallet must commit to
    481  * @param refresh_seed refresh master seed to include
    482  * @param blinding_seed blinding master seed for CS denominations, might be NULL
    483  * @param k_tpbs TALER_CNC_KAPPA many batches of n transfer public keys,
    484  *        NULL for the legacy versions which did not include them
    485  * @param k_bps_h TALER_CNC_KAPPA many hashes of blinded coin envelopes
    486  * @param coin_pub public key of the coin to be melted
    487  * @param amount_with_fee amount to be melted, including fee
    488  */
    489 static void
    490 refresh_get_commitment (
    491   struct TALER_RefreshCommitmentP *rc,
    492   const struct TALER_PublicRefreshMasterSeedP *refresh_seed,
    493   const struct TALER_BlindingMasterSeedP *blinding_seed,
    494   const struct TALER_KappaTransferPublicKeys *k_tpbs,
    495   const struct TALER_KappaHashBlindedPlanchetsP *k_bps_h,
    496   const struct TALER_CoinSpendPublicKeyP *coin_pub,
    497   const struct TALER_Amount *amount_with_fee)
    498 {
    499   struct GNUNET_HashContext *hash_context;
    500 
    501   /**
    502    * We follow
    503    * https://docs.taler.net/core/api-common.html#taler-refreshcommitmentp
    504    */
    505 
    506   hash_context = GNUNET_CRYPTO_hash_context_start ();
    507 
    508   /* First, the refresh master seed (from which the nonces, then signatures
    509      and finally private keys of the fresh coins are derived from) */
    510   GNUNET_assert (NULL != refresh_seed);
    511   GNUNET_CRYPTO_hash_context_read (
    512     hash_context,
    513     refresh_seed,
    514     sizeof (*refresh_seed));
    515 
    516   /* Then, we add the transfer public keys */
    517   if (NULL != k_tpbs)
    518   {
    519     for (unsigned int k = 0; k < TALER_CNC_KAPPA; k++)
    520       GNUNET_CRYPTO_hash_context_read (
    521         hash_context,
    522         k_tpbs->batch[k],
    523         sizeof (struct TALER_TransferPublicKeyP) * k_tpbs->num_transfer_pubs);
    524   }
    525 
    526   /* Then, in case of CS denominations, the blinding_seed from which all
    527      nonces are derived from, and therefore public R-values */
    528   {
    529     struct TALER_BlindingMasterSeedP blanko = {0};
    530     const struct TALER_BlindingMasterSeedP *pbms = &blanko;
    531 
    532     if (NULL != blinding_seed)
    533       pbms = blinding_seed;
    534 
    535     GNUNET_CRYPTO_hash_context_read (
    536       hash_context,
    537       pbms,
    538       sizeof (*pbms));
    539   }
    540 
    541   /* Next, add public key of coin and amount being refreshed */
    542   {
    543     struct TALER_AmountNBO melt_amountn;
    544 
    545     GNUNET_CRYPTO_hash_context_read (hash_context,
    546                                      coin_pub,
    547                                      sizeof (struct TALER_CoinSpendPublicKeyP));
    548     TALER_amount_hton (&melt_amountn,
    549                        amount_with_fee);
    550     GNUNET_CRYPTO_hash_context_read (hash_context,
    551                                      &melt_amountn,
    552                                      sizeof (struct TALER_AmountNBO));
    553   }
    554 
    555   /* Finally, add all the hashes of the blinded coins
    556    * (containing information about denominations), depths first */
    557   for (unsigned int k = 0; k < TALER_CNC_KAPPA; k++)
    558     GNUNET_CRYPTO_hash_context_read (hash_context,
    559                                      &k_bps_h->tuple[k],
    560                                      sizeof (k_bps_h->tuple[k]));
    561 
    562   /* Conclude */
    563   GNUNET_CRYPTO_hash_context_finish (hash_context,
    564                                      &rc->session_hash);
    565 }
    566 
    567 
    568 void
    569 TALER_refresh_get_commitment (
    570   struct TALER_RefreshCommitmentP *rc,
    571   const struct TALER_PublicRefreshMasterSeedP *refresh_seed,
    572   const struct TALER_BlindingMasterSeedP *blinding_seed,
    573   const struct TALER_KappaTransferPublicKeys *k_tpbs,
    574   const struct TALER_KappaHashBlindedPlanchetsP *k_bps_h,
    575   const struct TALER_CoinSpendPublicKeyP *coin_pub,
    576   const struct TALER_Amount *amount_with_fee)
    577 {
    578   GNUNET_assert (NULL != k_tpbs);
    579   refresh_get_commitment (rc,
    580                           refresh_seed,
    581                           blinding_seed,
    582                           k_tpbs,
    583                           k_bps_h,
    584                           coin_pub,
    585                           amount_with_fee);
    586 }
    587 
    588 
    589 void
    590 TALER_refresh_get_commitment_legacy_vDOLDPLUS_broken (
    591   struct TALER_RefreshCommitmentP *rc,
    592   const struct TALER_PublicRefreshMasterSeedP *refresh_seed,
    593   const struct TALER_BlindingMasterSeedP *blinding_seed,
    594   const struct TALER_KappaHashBlindedPlanchetsP *k_bps_h,
    595   const struct TALER_CoinSpendPublicKeyP *coin_pub,
    596   const struct TALER_Amount *amount_with_fee)
    597 {
    598   /* The transfer public keys were sent, but erroneously not hashed */
    599   refresh_get_commitment (rc,
    600                           refresh_seed,
    601                           blinding_seed,
    602                           NULL,
    603                           k_bps_h,
    604                           coin_pub,
    605                           amount_with_fee);
    606 }
    607 
    608 
    609 void
    610 TALER_refresh_get_commitment_legacy_v27 (
    611   struct TALER_RefreshCommitmentP *rc,
    612   const struct TALER_PublicRefreshMasterSeedP *refresh_seed,
    613   const struct TALER_BlindingMasterSeedP *blinding_seed,
    614   const struct TALER_KappaHashBlindedPlanchetsP *k_bps_h,
    615   const struct TALER_CoinSpendPublicKeyP *coin_pub,
    616   const struct TALER_Amount *amount_with_fee)
    617 {
    618   refresh_get_commitment (rc,
    619                           refresh_seed,
    620                           blinding_seed,
    621                           NULL,
    622                           k_bps_h,
    623                           coin_pub,
    624                           amount_with_fee);
    625 }
    626 
    627 
    628 void
    629 TALER_refresh_expand_seed_to_kappa_batch_seeds (
    630   const struct TALER_PublicRefreshMasterSeedP *refresh_master_seed,
    631   const struct TALER_CoinSpendPrivateKeyP *coin_priv,
    632   struct TALER_KappaPrivateRefreshBatchSeedsP *kappa_batch_seeds)
    633 {
    634   GNUNET_assert (GNUNET_OK ==
    635                  GNUNET_CRYPTO_hkdf_gnunet (
    636                    kappa_batch_seeds,
    637                    sizeof (*kappa_batch_seeds),
    638                    "refresh-batch-seeds",
    639                    strlen ("refresh-batch-seeds"),
    640                    refresh_master_seed,
    641                    sizeof (*refresh_master_seed),
    642                    GNUNET_CRYPTO_kdf_arg_auto (coin_priv)));
    643 }
    644 
    645 
    646 void
    647 TALER_refresh_expand_batch_seed_to_transfer_private_keys (
    648   const struct TALER_PrivateRefreshBatchSeedP *batch_seed,
    649   size_t num_transfer_pks,
    650   struct TALER_TransferPrivateKeyP transfer_pks[num_transfer_pks])
    651 {
    652   GNUNET_assert (GNUNET_OK ==
    653                  GNUNET_CRYPTO_hkdf_gnunet (
    654                    transfer_pks,
    655                    sizeof (*transfer_pks) * num_transfer_pks,
    656                    "refresh-transfer-private-keys",
    657                    strlen ("refresh-transfer-private-keys"),
    658                    batch_seed,
    659                    sizeof (*batch_seed)));
    660 }
    661 
    662 
    663 void
    664 TALER_refresh_expand_batch_seed_to_transfer_secrets (
    665   const struct TALER_PrivateRefreshBatchSeedP *batch_seed,
    666   const struct TALER_CoinSpendPublicKeyP *coin_pub,
    667   size_t num_transfer_secrets,
    668   struct TALER_TransferSecretP transfer_secrets[num_transfer_secrets])
    669 {
    670   struct TALER_TransferPrivateKeyP transfer_pks[num_transfer_secrets];
    671 
    672   TALER_refresh_expand_batch_seed_to_transfer_private_keys (
    673     batch_seed,
    674     num_transfer_secrets,
    675     transfer_pks);
    676 
    677   for (size_t i = 0; i < num_transfer_secrets; i++)
    678     TALER_link_reveal_transfer_secret (
    679       &transfer_pks[i],
    680       coin_pub,
    681       &transfer_secrets[i]);
    682 }
    683 
    684 
    685 void
    686 TALER_refresh_expand_batch_seed_to_planchet_master_secrets (
    687   const struct TALER_PrivateRefreshBatchSeedP *batch_seed,
    688   const struct TALER_CoinSpendPublicKeyP *coin_pub,
    689   size_t num_planchet_secrets,
    690   struct TALER_PlanchetMasterSecretP planchet_secrets[num_planchet_secrets])
    691 {
    692   struct TALER_TransferPrivateKeyP transfer_pks[num_planchet_secrets];
    693   struct TALER_TransferSecretP transfer_secrets[num_planchet_secrets];
    694 
    695   TALER_refresh_expand_batch_seed_to_transfer_private_keys (
    696     batch_seed,
    697     num_planchet_secrets,
    698     transfer_pks);
    699 
    700   for (size_t i = 0; i < num_planchet_secrets; i++)
    701   {
    702     TALER_link_reveal_transfer_secret (
    703       &transfer_pks[i],
    704       coin_pub,
    705       &transfer_secrets[i]);
    706 
    707     TALER_transfer_secret_to_planchet_secret (
    708       &transfer_secrets[i],
    709       i,
    710       &planchet_secrets[i]);
    711   }
    712 }
    713 
    714 
    715 void
    716 TALER_refresh_expand_batch_seed_to_transfer_data (
    717   const struct TALER_PrivateRefreshBatchSeedP *batch_seed,
    718   const struct TALER_CoinSpendPublicKeyP *coin_pub,
    719   size_t num,
    720   struct TALER_PlanchetMasterSecretP planchet_secrets[num],
    721   struct TALER_TransferPublicKeyP transfer_pubs[num])
    722 {
    723   struct TALER_TransferPrivateKeyP transfer_pks[num];
    724   struct TALER_TransferSecretP transfer_secrets[num];
    725 
    726   TALER_refresh_expand_batch_seed_to_transfer_private_keys (
    727     batch_seed,
    728     num,
    729     transfer_pks);
    730 
    731   for (size_t i = 0; i < num; i++)
    732   {
    733     TALER_link_reveal_transfer_secret (
    734       &transfer_pks[i],
    735       coin_pub,
    736       &transfer_secrets[i]);
    737     TALER_transfer_secret_to_planchet_secret (
    738       &transfer_secrets[i],
    739       i,
    740       &planchet_secrets[i]);
    741     GNUNET_CRYPTO_ecdhe_key_get_public (
    742       &transfer_pks[i].ecdhe_priv,
    743       &transfer_pubs[i].ecdhe_pub);
    744   }
    745 }
    746 
    747 
    748 void
    749 TALER_refresh_expand_kappa_nonces_v27 (
    750   const struct TALER_PublicRefreshMasterSeedP *refresh_seed,
    751   struct TALER_KappaPublicRefreshNoncesP *kappa_nonces)
    752 {
    753   GNUNET_assert (GNUNET_OK ==
    754                  GNUNET_CRYPTO_hkdf_gnunet (
    755                    kappa_nonces,
    756                    sizeof (*kappa_nonces),
    757                    "refresh-kappa-nonces",
    758                    strlen ("refresh-kappa-nonces"),
    759                    refresh_seed,
    760                    sizeof (*refresh_seed)));
    761 }
    762 
    763 
    764 void
    765 TALER_refresh_signature_to_secrets_v27 (
    766   const struct TALER_PrivateRefreshNonceSignatureP *sig,
    767   size_t num_secrets,
    768   struct TALER_PlanchetMasterSecretP secrets[static num_secrets])
    769 {
    770   GNUNET_assert (GNUNET_YES ==
    771                  GNUNET_CRYPTO_hkdf_gnunet (
    772                    secrets,
    773                    sizeof (*secrets) * num_secrets,
    774                    "refresh-planchet-secret",
    775                    strlen ("refresh-planchet-secret"),
    776                    sig,
    777                    sizeof (*sig)));
    778 }
    779 
    780 
    781 void
    782 TALER_coin_pub_hash (
    783   const struct TALER_CoinSpendPublicKeyP *coin_pub,
    784   const struct TALER_AgeCommitmentHashP *ach,
    785   struct TALER_CoinPubHashP *coin_h)
    786 {
    787   if (TALER_AgeCommitmentHashP_isNullOrZero (ach))
    788   {
    789     /* No age commitment was set */
    790     GNUNET_CRYPTO_hash (
    791       &coin_pub->eddsa_pub,
    792       sizeof (coin_pub->eddsa_pub),
    793       &coin_h->hash);
    794   }
    795   else
    796   {
    797     /* Coin comes with age commitment.
    798      * Take the hash of the age commitment into account. */
    799     struct GNUNET_HashContext *hash_context;
    800 
    801     hash_context = GNUNET_CRYPTO_hash_context_start ();
    802     GNUNET_CRYPTO_hash_context_read (
    803       hash_context,
    804       &coin_pub->eddsa_pub,
    805       sizeof (coin_pub->eddsa_pub));
    806     GNUNET_CRYPTO_hash_context_read (
    807       hash_context,
    808       ach,
    809       sizeof (struct TALER_AgeCommitmentHashP));
    810     GNUNET_CRYPTO_hash_context_finish (
    811       hash_context,
    812       &coin_h->hash);
    813   }
    814 }
    815 
    816 
    817 void
    818 TALER_coin_ev_hash (
    819   const struct TALER_BlindedPlanchet *blinded_planchet,
    820   const struct TALER_DenominationHashP *denom_hash,
    821   struct TALER_BlindedCoinHashP *bch)
    822 {
    823   struct GNUNET_HashContext *hash_context;
    824 
    825   hash_context = GNUNET_CRYPTO_hash_context_start ();
    826   GNUNET_CRYPTO_hash_context_read (
    827     hash_context,
    828     denom_hash,
    829     sizeof (*denom_hash));
    830   TALER_blinded_planchet_hash_ (
    831     blinded_planchet,
    832     hash_context);
    833   GNUNET_CRYPTO_hash_context_finish (
    834     hash_context,
    835     &bch->hash);
    836 }
    837 
    838 
    839 GNUNET_NETWORK_STRUCT_BEGIN
    840 /**
    841  * Structure we hash to compute the group key for
    842  * a denomination group.
    843  */
    844 struct DenominationGroupP
    845 {
    846   /**
    847    * Value of coins in this denomination group.
    848    */
    849   struct TALER_AmountNBO value;
    850 
    851   /**
    852    * Fee structure for all coins in the group.
    853    */
    854   struct TALER_DenomFeeSetNBOP fees;
    855 
    856   /**
    857    * Age mask for the denomiation, in NBO.
    858    */
    859   uint32_t age_mask GNUNET_PACKED;
    860 
    861   /**
    862    * Cipher used for the denomination, in NBO.
    863    */
    864   uint32_t cipher GNUNET_PACKED;
    865 };
    866 GNUNET_NETWORK_STRUCT_END
    867 
    868 void
    869 TALER_denomination_group_get_key (
    870   const struct TALER_DenominationGroup *dg,
    871   struct GNUNET_HashCode *key)
    872 {
    873   struct DenominationGroupP dgp = {
    874     .age_mask = htonl (dg->age_mask.bits),
    875     .cipher = htonl (dg->cipher)
    876   };
    877 
    878   TALER_amount_hton (
    879     &dgp.value,
    880     &dg->value);
    881   TALER_denom_fee_set_hton (
    882     &dgp.fees,
    883     &dg->fees);
    884   GNUNET_CRYPTO_hash (
    885     &dgp,
    886     sizeof (dgp),
    887     key);
    888 }
    889 
    890 
    891 void
    892 TALER_kyc_measure_authorization_hash (
    893   const struct TALER_AccountAccessTokenP *access_token,
    894   uint64_t row,
    895   uint32_t offset,
    896   struct TALER_KycMeasureAuthorizationHashP *mah)
    897 {
    898   uint64_t be64 = GNUNET_htonll (row);
    899   uint32_t be32 = htonl ((uint32_t) offset);
    900 
    901   GNUNET_assert (GNUNET_YES ==
    902                  GNUNET_CRYPTO_hkdf_gnunet (
    903                    mah,
    904                    sizeof (*mah),
    905                    &be64,
    906                    sizeof (be64),
    907                    access_token,
    908                    sizeof (*access_token),
    909                    GNUNET_CRYPTO_kdf_arg_auto (&be32)));
    910 }
    911 
    912 
    913 void
    914 TALER_merchant_instance_auth_hash_with_salt (
    915   struct TALER_MerchantAuthenticationHashP *auth_hash,
    916   struct TALER_MerchantAuthenticationSaltP *salt,
    917   const char *passphrase)
    918 {
    919   GNUNET_assert (GNUNET_YES ==
    920                  GNUNET_CRYPTO_hkdf_gnunet (
    921                    auth_hash,
    922                    sizeof (*auth_hash),
    923                    salt,
    924                    sizeof (*salt),
    925                    passphrase,
    926                    strlen (passphrase),
    927                    GNUNET_CRYPTO_kdf_arg_string ("merchant-instance-auth")));
    928 }
    929 
    930 
    931 /* end of crypto.c */