LCOV - code coverage report
Current view: top level - util - crypto.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 74.6 % 244 182
Test Date: 2025-12-28 14:06:02 Functions: 81.6 % 38 31

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

Generated by: LCOV version 2.0-1