LCOV - code coverage report
Current view: top level - util - crypto.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 78.8 % 231 182
Test Date: 2026-03-10 12:10:57 Functions: 83.8 % 37 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          784 : TALER_cs_withdraw_nonce_derive (
     255              :   const struct TALER_PlanchetMasterSecretP *ps,
     256              :   struct GNUNET_CRYPTO_CsSessionNonce *nonce)
     257              : {
     258          784 :   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          784 : }
     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              : void
     370           22 : TALER_cs_derive_blind_nonces_from_seed (
     371              :   const struct TALER_BlindingMasterSeedP *seed,
     372              :   bool for_melt,
     373              :   size_t num,
     374              :   const bool is_cs[static num],
     375              :   union GNUNET_CRYPTO_BlindSessionNonce nonces[static num])
     376           22 : {
     377          110 :   for (size_t i = 0; i < num; i++)
     378              :   {
     379           88 :     if (is_cs[i])
     380           88 :       TALER_cs_nonce_derive_indexed (
     381              :         seed,
     382              :         for_melt,
     383              :         i,
     384           88 :         &nonces[i].cs_nonce);
     385              :   }
     386           22 : }
     387              : 
     388              : 
     389              : void
     390          512 : TALER_rsa_pub_hash (const struct GNUNET_CRYPTO_RsaPublicKey *rsa,
     391              :                     struct TALER_RsaPubHashP *h_rsa)
     392              : {
     393          512 :   GNUNET_CRYPTO_rsa_public_key_hash (rsa,
     394              :                                      &h_rsa->hash);
     395              : 
     396          512 : }
     397              : 
     398              : 
     399              : void
     400          418 : TALER_cs_pub_hash (const struct GNUNET_CRYPTO_CsPublicKey *cs,
     401              :                    struct TALER_CsPubHashP *h_cs)
     402              : {
     403          418 :   GNUNET_CRYPTO_hash (cs,
     404              :                       sizeof(*cs),
     405              :                       &h_cs->hash);
     406          418 : }
     407              : 
     408              : 
     409              : enum GNUNET_GenericReturnValue
     410         1367 : TALER_planchet_prepare (
     411              :   const struct TALER_DenominationPublicKey *dk,
     412              :   const struct TALER_ExchangeBlindingValues *blinding_values,
     413              :   const union GNUNET_CRYPTO_BlindingSecretP *bks,
     414              :   const union GNUNET_CRYPTO_BlindSessionNonce *nonce,
     415              :   const struct TALER_CoinSpendPrivateKeyP *coin_priv,
     416              :   const struct TALER_AgeCommitmentHashP *ach,
     417              :   struct TALER_CoinPubHashP *c_hash,
     418              :   struct TALER_PlanchetDetail *pd)
     419              : {
     420              :   struct TALER_CoinSpendPublicKeyP coin_pub;
     421              : 
     422         1367 :   GNUNET_assert (blinding_values->blinding_inputs->cipher ==
     423              :                  dk->bsign_pub_key->cipher);
     424         1367 :   GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv->eddsa_priv,
     425              :                                       &coin_pub.eddsa_pub);
     426         1367 :   if (GNUNET_OK !=
     427         1367 :       TALER_denom_blind (dk,
     428              :                          bks,
     429              :                          nonce,
     430              :                          ach,
     431              :                          &coin_pub,
     432              :                          blinding_values,
     433              :                          c_hash,
     434              :                          &pd->blinded_planchet))
     435              :   {
     436            0 :     GNUNET_break (0);
     437            0 :     return GNUNET_SYSERR;
     438              :   }
     439         1367 :   TALER_denom_pub_hash (dk,
     440              :                         &pd->denom_pub_hash);
     441         1367 :   return GNUNET_OK;
     442              : }
     443              : 
     444              : 
     445              : void
     446          114 : TALER_planchet_detail_free (struct TALER_PlanchetDetail *pd)
     447              : {
     448          114 :   TALER_blinded_planchet_free (&pd->blinded_planchet);
     449          114 : }
     450              : 
     451              : 
     452              : enum GNUNET_GenericReturnValue
     453          190 : TALER_planchet_to_coin (
     454              :   const struct TALER_DenominationPublicKey *dk,
     455              :   const struct TALER_BlindedDenominationSignature *blind_sig,
     456              :   const union GNUNET_CRYPTO_BlindingSecretP *bks,
     457              :   const struct TALER_CoinSpendPrivateKeyP *coin_priv,
     458              :   const struct TALER_AgeCommitmentHashP *ach,
     459              :   const struct TALER_CoinPubHashP *c_hash,
     460              :   const struct TALER_ExchangeBlindingValues *alg_values,
     461              :   struct TALER_FreshCoin *coin)
     462              : {
     463          190 :   if (dk->bsign_pub_key->cipher !=
     464          190 :       blind_sig->blinded_sig->cipher)
     465              :   {
     466            0 :     GNUNET_break_op (0);
     467            0 :     return GNUNET_SYSERR;
     468              :   }
     469          190 :   if (dk->bsign_pub_key->cipher !=
     470          190 :       alg_values->blinding_inputs->cipher)
     471              :   {
     472            0 :     GNUNET_break_op (0);
     473            0 :     return GNUNET_SYSERR;
     474              :   }
     475          190 :   if (GNUNET_OK !=
     476          190 :       TALER_denom_sig_unblind (&coin->sig,
     477              :                                blind_sig,
     478              :                                bks,
     479              :                                c_hash,
     480              :                                alg_values,
     481              :                                dk))
     482              :   {
     483            0 :     GNUNET_break_op (0);
     484            0 :     return GNUNET_SYSERR;
     485              :   }
     486          190 :   if (GNUNET_OK !=
     487          190 :       TALER_denom_pub_verify (dk,
     488          190 :                               &coin->sig,
     489              :                               c_hash))
     490              :   {
     491            0 :     GNUNET_break_op (0);
     492            0 :     TALER_denom_sig_free (&coin->sig);
     493            0 :     return GNUNET_SYSERR;
     494              :   }
     495              : 
     496          190 :   coin->coin_priv = *coin_priv;
     497          190 :   coin->h_age_commitment = ach;
     498          190 :   return GNUNET_OK;
     499              : }
     500              : 
     501              : 
     502              : void
     503           88 : TALER_refresh_get_commitment (
     504              :   struct TALER_RefreshCommitmentP *rc,
     505              :   const struct TALER_PublicRefreshMasterSeedP *refresh_seed,
     506              :   const struct TALER_BlindingMasterSeedP *blinding_seed,
     507              :   const struct TALER_KappaTransferPublicKeys *k_tpbs,
     508              :   const struct TALER_KappaHashBlindedPlanchetsP *k_bps_h,
     509              :   const struct TALER_CoinSpendPublicKeyP *coin_pub,
     510              :   const struct TALER_Amount *amount_with_fee)
     511              : {
     512              :   struct GNUNET_HashContext *hash_context;
     513              : 
     514           88 :   hash_context = GNUNET_CRYPTO_hash_context_start ();
     515              : 
     516              :   /* First, the refresh master seed (from which the nonces, then signatures
     517              :      and finally private keys of the fresh coins are derived from) */
     518           88 :   GNUNET_assert (NULL != refresh_seed);
     519           88 :   GNUNET_CRYPTO_hash_context_read (hash_context,
     520              :                                    refresh_seed,
     521              :                                    sizeof (*refresh_seed));
     522              : 
     523              :   /* Then, in case of CS denominations, the blinding_seed from which all
     524              :      nonces are derived from, and therefore public R-values */
     525              :   {
     526           88 :     struct TALER_BlindingMasterSeedP blanko = {0};
     527           88 :     const struct TALER_BlindingMasterSeedP *pbms = &blanko;
     528              : 
     529           88 :     if (NULL != blinding_seed)
     530           59 :       pbms = blinding_seed;
     531           88 :     GNUNET_CRYPTO_hash_context_read (hash_context,
     532              :                                      pbms,
     533              :                                      sizeof(*pbms));
     534              :   }
     535              : 
     536              :   /* Next, add public key of coin and amount being refreshed */
     537              :   {
     538              :     struct TALER_AmountNBO melt_amountn;
     539              : 
     540           88 :     GNUNET_CRYPTO_hash_context_read (hash_context,
     541              :                                      coin_pub,
     542              :                                      sizeof (struct TALER_CoinSpendPublicKeyP));
     543           88 :     TALER_amount_hton (&melt_amountn,
     544              :                        amount_with_fee);
     545           88 :     GNUNET_CRYPTO_hash_context_read (hash_context,
     546              :                                      &melt_amountn,
     547              :                                      sizeof (struct TALER_AmountNBO));
     548              :   }
     549              : 
     550              :   /* Finally, add all the hashes of the blinded coins
     551              :    * (containing information about denominations), depths first */
     552          352 :   for (unsigned int k = 0; k<TALER_CNC_KAPPA; k++)
     553          264 :     GNUNET_CRYPTO_hash_context_read (hash_context,
     554          264 :                                      &k_bps_h->tuple[k],
     555              :                                      sizeof(k_bps_h->tuple[k]));
     556              : 
     557              :   /* Conclude */
     558           88 :   GNUNET_CRYPTO_hash_context_finish (hash_context,
     559              :                                      &rc->session_hash);
     560           88 : }
     561              : 
     562              : 
     563              : void
     564           44 : TALER_refresh_expand_seed_to_kappa_batch_seeds (
     565              :   const struct TALER_PublicRefreshMasterSeedP *refresh_master_seed,
     566              :   const struct TALER_CoinSpendPrivateKeyP *coin_priv,
     567              :   struct TALER_KappaPrivateRefreshBatchSeedsP *kappa_batch_seeds)
     568              : {
     569           44 :   GNUNET_assert (GNUNET_OK ==
     570              :                  GNUNET_CRYPTO_kdf (kappa_batch_seeds,
     571              :                                     sizeof (*kappa_batch_seeds),
     572              :                                     "refresh-batch-seeds",
     573              :                                     strlen ("refresh-batch-seeds"),
     574              :                                     refresh_master_seed,
     575              :                                     sizeof (*refresh_master_seed),
     576              :                                     coin_priv,
     577              :                                     sizeof(*coin_priv),
     578              :                                     NULL, 0));
     579           44 : }
     580              : 
     581              : 
     582              : void
     583          160 : TALER_refresh_expand_batch_seed_to_transfer_private_keys (
     584              :   const struct TALER_PrivateRefreshBatchSeedP *batch_seed,
     585              :   size_t num_transfer_pks,
     586              :   struct TALER_TransferPrivateKeyP transfer_pks[num_transfer_pks])
     587          160 : {
     588          160 :   GNUNET_assert (GNUNET_OK ==
     589              :                  GNUNET_CRYPTO_kdf (
     590              :                    transfer_pks,
     591              :                    sizeof (*transfer_pks) * num_transfer_pks,
     592              :                    "refresh-transfer-private-keys",
     593              :                    strlen ("refresh-transfer-private-keys"),
     594              :                    batch_seed,
     595              :                    sizeof (*batch_seed),
     596              :                    NULL, 0));
     597          160 : }
     598              : 
     599              : 
     600              : void
     601            0 : TALER_refresh_expand_batch_seed_to_transfer_secrets (
     602              :   const struct TALER_PrivateRefreshBatchSeedP *batch_seed,
     603              :   const struct TALER_CoinSpendPublicKeyP *coin_pub,
     604              :   size_t num_transfer_secrets,
     605              :   struct TALER_TransferSecretP transfer_secrets[num_transfer_secrets])
     606            0 : {
     607            0 :   struct TALER_TransferPrivateKeyP transfer_pks[num_transfer_secrets];
     608              : 
     609            0 :   TALER_refresh_expand_batch_seed_to_transfer_private_keys (
     610              :     batch_seed,
     611              :     num_transfer_secrets,
     612              :     transfer_pks);
     613              : 
     614            0 :   for (size_t i = 0; i < num_transfer_secrets; i++)
     615              :   {
     616            0 :     TALER_link_reveal_transfer_secret (
     617            0 :       &transfer_pks[i],
     618              :       coin_pub,
     619            0 :       &transfer_secrets[i]);
     620              :   }
     621            0 : }
     622              : 
     623              : 
     624              : void
     625            0 : TALER_refresh_expand_batch_seed_to_planchet_master_secrets (
     626              :   const struct TALER_PrivateRefreshBatchSeedP *batch_seed,
     627              :   const struct TALER_CoinSpendPublicKeyP *coin_pub,
     628              :   size_t num_planchet_secrets,
     629              :   struct TALER_PlanchetMasterSecretP planchet_secrets[num_planchet_secrets])
     630            0 : {
     631            0 :   struct TALER_TransferPrivateKeyP transfer_pks[num_planchet_secrets];
     632            0 :   struct TALER_TransferSecretP transfer_secrets[num_planchet_secrets];
     633              : 
     634            0 :   TALER_refresh_expand_batch_seed_to_transfer_private_keys (
     635              :     batch_seed,
     636              :     num_planchet_secrets,
     637              :     transfer_pks);
     638              : 
     639            0 :   for (size_t i = 0; i < num_planchet_secrets; i++)
     640              :   {
     641            0 :     TALER_link_reveal_transfer_secret (
     642            0 :       &transfer_pks[i],
     643              :       coin_pub,
     644              :       &transfer_secrets[i]);
     645              : 
     646            0 :     TALER_transfer_secret_to_planchet_secret (
     647            0 :       &transfer_secrets[i],
     648              :       i,
     649            0 :       &planchet_secrets[i]);
     650              :   }
     651            0 : }
     652              : 
     653              : 
     654              : void
     655          160 : TALER_refresh_expand_batch_seed_to_transfer_data (
     656              :   const struct TALER_PrivateRefreshBatchSeedP *batch_seed,
     657              :   const struct TALER_CoinSpendPublicKeyP *coin_pub,
     658              :   size_t num,
     659              :   struct TALER_PlanchetMasterSecretP planchet_secrets[num],
     660              :   struct TALER_TransferPublicKeyP transfer_pubs[num])
     661          160 : {
     662          160 :   struct TALER_TransferPrivateKeyP transfer_pks[num];
     663          160 :   struct TALER_TransferSecretP transfer_secrets[num];
     664              : 
     665          160 :   TALER_refresh_expand_batch_seed_to_transfer_private_keys (
     666              :     batch_seed,
     667              :     num,
     668              :     transfer_pks);
     669              : 
     670          800 :   for (size_t i = 0; i < num; i++)
     671              :   {
     672          640 :     TALER_link_reveal_transfer_secret (
     673          640 :       &transfer_pks[i],
     674              :       coin_pub,
     675              :       &transfer_secrets[i]);
     676              : 
     677          640 :     TALER_transfer_secret_to_planchet_secret (
     678          640 :       &transfer_secrets[i],
     679              :       i,
     680          640 :       &planchet_secrets[i]);
     681              : 
     682          640 :     GNUNET_CRYPTO_ecdhe_key_get_public (
     683          640 :       &transfer_pks[i].ecdhe_priv,
     684          640 :       &transfer_pubs[i].ecdhe_pub);
     685              :   }
     686          160 : }
     687              : 
     688              : 
     689              : void
     690            0 : TALER_refresh_expand_kappa_nonces_v27 (
     691              :   const struct TALER_PublicRefreshMasterSeedP *refresh_seed,
     692              :   struct TALER_KappaPublicRefreshNoncesP *kappa_nonces)
     693              : {
     694            0 :   GNUNET_assert (GNUNET_OK ==
     695              :                  GNUNET_CRYPTO_kdf (kappa_nonces,
     696              :                                     sizeof (*kappa_nonces),
     697              :                                     "refresh-kappa-nonces",
     698              :                                     strlen ("refresh-kappa-nonces"),
     699              :                                     refresh_seed,
     700              :                                     sizeof (*refresh_seed),
     701              :                                     NULL, 0));
     702            0 : }
     703              : 
     704              : 
     705              : void
     706            0 : TALER_refresh_signature_to_secrets_v27 (
     707              :   const struct TALER_PrivateRefreshNonceSignatureP *sig,
     708              :   size_t num_secrets,
     709              :   struct TALER_PlanchetMasterSecretP secrets[static num_secrets])
     710            0 : {
     711            0 :   GNUNET_assert (GNUNET_YES ==
     712              :                  GNUNET_CRYPTO_kdf (secrets,
     713              :                                     sizeof (*secrets) * num_secrets,
     714              :                                     "refresh-planchet-secret",
     715              :                                     strlen ("refresh-planchet-secret"),
     716              :                                     sig,
     717              :                                     sizeof(*sig),
     718              :                                     NULL,
     719              :                                     0));
     720            0 : }
     721              : 
     722              : 
     723              : void
     724         1697 : TALER_coin_pub_hash (const struct TALER_CoinSpendPublicKeyP *coin_pub,
     725              :                      const struct TALER_AgeCommitmentHashP *ach,
     726              :                      struct TALER_CoinPubHashP *coin_h)
     727              : {
     728         1697 :   if (TALER_AgeCommitmentHashP_isNullOrZero (ach))
     729              :   {
     730              :     /* No age commitment was set */
     731          755 :     GNUNET_CRYPTO_hash (&coin_pub->eddsa_pub,
     732              :                         sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
     733              :                         &coin_h->hash);
     734              :   }
     735              :   else
     736              :   {
     737              :     /* Coin comes with age commitment.  Take the hash of the age commitment
     738              :      * into account */
     739              :     struct GNUNET_HashContext *hash_context;
     740              : 
     741          942 :     hash_context = GNUNET_CRYPTO_hash_context_start ();
     742              : 
     743          942 :     GNUNET_CRYPTO_hash_context_read (
     744              :       hash_context,
     745          942 :       &coin_pub->eddsa_pub,
     746              :       sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey));
     747              : 
     748          942 :     GNUNET_CRYPTO_hash_context_read (
     749              :       hash_context,
     750              :       ach,
     751              :       sizeof(struct TALER_AgeCommitmentHashP));
     752              : 
     753          942 :     GNUNET_CRYPTO_hash_context_finish (
     754              :       hash_context,
     755              :       &coin_h->hash);
     756              :   }
     757         1697 : }
     758              : 
     759              : 
     760              : void
     761         1671 : TALER_coin_ev_hash (const struct TALER_BlindedPlanchet *blinded_planchet,
     762              :                     const struct TALER_DenominationHashP *denom_hash,
     763              :                     struct TALER_BlindedCoinHashP *bch)
     764              : {
     765              :   struct GNUNET_HashContext *hash_context;
     766              : 
     767         1671 :   hash_context = GNUNET_CRYPTO_hash_context_start ();
     768         1671 :   GNUNET_CRYPTO_hash_context_read (hash_context,
     769              :                                    denom_hash,
     770              :                                    sizeof(*denom_hash));
     771         1671 :   TALER_blinded_planchet_hash_ (blinded_planchet,
     772              :                                 hash_context);
     773         1671 :   GNUNET_CRYPTO_hash_context_finish (hash_context,
     774              :                                      &bch->hash);
     775         1671 : }
     776              : 
     777              : 
     778              : GNUNET_NETWORK_STRUCT_BEGIN
     779              : /**
     780              :  * Structure we hash to compute the group key for
     781              :  * a denomination group.
     782              :  */
     783              : struct DenominationGroupP
     784              : {
     785              :   /**
     786              :    * Value of coins in this denomination group.
     787              :    */
     788              :   struct TALER_AmountNBO value;
     789              : 
     790              :   /**
     791              :    * Fee structure for all coins in the group.
     792              :    */
     793              :   struct TALER_DenomFeeSetNBOP fees;
     794              : 
     795              :   /**
     796              :    * Age mask for the denomiation, in NBO.
     797              :    */
     798              :   uint32_t age_mask GNUNET_PACKED;
     799              : 
     800              :   /**
     801              :    * Cipher used for the denomination, in NBO.
     802              :    */
     803              :   uint32_t cipher GNUNET_PACKED;
     804              : };
     805              : GNUNET_NETWORK_STRUCT_END
     806              : 
     807              : 
     808              : void
     809          658 : TALER_denomination_group_get_key (
     810              :   const struct TALER_DenominationGroup *dg,
     811              :   struct GNUNET_HashCode *key)
     812              : {
     813          658 :   struct DenominationGroupP dgp = {
     814          658 :     .age_mask = htonl (dg->age_mask.bits),
     815          658 :     .cipher = htonl (dg->cipher)
     816              :   };
     817              : 
     818          658 :   TALER_amount_hton (&dgp.value,
     819              :                      &dg->value);
     820          658 :   TALER_denom_fee_set_hton (&dgp.fees,
     821              :                             &dg->fees);
     822          658 :   GNUNET_CRYPTO_hash (&dgp,
     823              :                       sizeof (dgp),
     824              :                       key);
     825          658 : }
     826              : 
     827              : 
     828              : void
     829           21 : TALER_kyc_measure_authorization_hash (
     830              :   const struct TALER_AccountAccessTokenP *access_token,
     831              :   uint64_t row,
     832              :   uint32_t offset,
     833              :   struct TALER_KycMeasureAuthorizationHashP *mah)
     834              : {
     835           21 :   uint64_t be64 = GNUNET_htonll (row);
     836           21 :   uint32_t be32 = htonl ((uint32_t) offset);
     837              : 
     838           21 :   GNUNET_assert (
     839              :     GNUNET_YES ==
     840              :     GNUNET_CRYPTO_kdf (mah,
     841              :                        sizeof (*mah),
     842              :                        &be64,
     843              :                        sizeof (be64),
     844              :                        access_token,
     845              :                        sizeof (*access_token),
     846              :                        &be32,
     847              :                        sizeof (be32),
     848              :                        NULL,
     849              :                        0));
     850           21 : }
     851              : 
     852              : 
     853              : void
     854            0 : TALER_merchant_instance_auth_hash_with_salt (
     855              :   struct TALER_MerchantAuthenticationHashP *auth_hash,
     856              :   struct TALER_MerchantAuthenticationSaltP *salt,
     857              :   const char *passphrase)
     858              : {
     859            0 :   GNUNET_assert (GNUNET_YES ==
     860              :                  GNUNET_CRYPTO_kdf (auth_hash,
     861              :                                     sizeof (*auth_hash),
     862              :                                     salt,
     863              :                                     sizeof (*salt),
     864              :                                     passphrase,
     865              :                                     strlen (passphrase),
     866              :                                     "merchant-instance-auth",
     867              :                                     strlen ("merchant-instance-auth"),
     868              :                                     NULL,
     869              :                                     0));
     870            0 : }
     871              : 
     872              : 
     873              : /* end of crypto.c */
        

Generated by: LCOV version 2.0-1