2020-02-29 16:42:10 +01:00
|
|
|
/*
|
|
|
|
This file is part of TALER
|
2022-02-08 14:02:27 +01:00
|
|
|
Copyright (C) 2015-2022 Taler Systems SA
|
2020-02-29 16:42:10 +01:00
|
|
|
|
|
|
|
TALER is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
TALER; see the file COPYING. If not, see
|
|
|
|
<http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @file lib/exchange_api_refresh_common.c
|
|
|
|
* @brief Serialization logic shared between melt and reveal steps during refreshing
|
|
|
|
* @author Christian Grothoff
|
|
|
|
*/
|
|
|
|
#include "platform.h"
|
|
|
|
#include "exchange_api_refresh_common.h"
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
TALER_EXCHANGE_free_melt_data_ (struct MeltData *md)
|
|
|
|
{
|
2022-02-04 22:02:48 +01:00
|
|
|
TALER_denom_pub_free (&md->melted_coin.pub_key);
|
|
|
|
TALER_denom_sig_free (&md->melted_coin.sig);
|
2020-02-29 16:42:10 +01:00
|
|
|
if (NULL != md->fresh_pks)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i<md->num_fresh_coins; i++)
|
2021-10-26 09:34:14 +02:00
|
|
|
TALER_denom_pub_free (&md->fresh_pks[i]);
|
2020-02-29 16:42:10 +01:00
|
|
|
GNUNET_free (md->fresh_pks);
|
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i<TALER_CNC_KAPPA; i++)
|
2020-07-05 16:56:41 +02:00
|
|
|
GNUNET_free (md->fresh_coins[i]);
|
2020-04-21 14:53:05 +02:00
|
|
|
/* Finally, clean up a bit... */
|
|
|
|
GNUNET_CRYPTO_zero_keys (md,
|
|
|
|
sizeof (struct MeltData));
|
2020-02-29 16:42:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-04 22:02:48 +01:00
|
|
|
enum GNUNET_GenericReturnValue
|
|
|
|
TALER_EXCHANGE_get_melt_data_ (
|
|
|
|
const struct TALER_PlanchetSecretsP *ps,
|
2022-02-06 19:00:01 +01:00
|
|
|
const struct TALER_EXCHANGE_RefreshData *rd,
|
2022-02-04 23:58:41 +01:00
|
|
|
const struct TALER_ExchangeWithdrawValues *alg_values,
|
2022-02-04 22:02:48 +01:00
|
|
|
struct MeltData *md)
|
2020-02-29 16:42:10 +01:00
|
|
|
{
|
|
|
|
struct TALER_Amount total;
|
|
|
|
struct TALER_CoinSpendPublicKeyP coin_pub;
|
|
|
|
struct TALER_TransferSecretP trans_sec[TALER_CNC_KAPPA];
|
|
|
|
struct TALER_RefreshCommitmentEntry rce[TALER_CNC_KAPPA];
|
|
|
|
|
2022-02-06 19:00:01 +01:00
|
|
|
GNUNET_CRYPTO_eddsa_key_get_public (&rd->melt_priv.eddsa_priv,
|
2020-02-29 16:42:10 +01:00
|
|
|
&coin_pub.eddsa_pub);
|
|
|
|
/* build up melt data structure */
|
2022-02-06 19:00:01 +01:00
|
|
|
memset (md,
|
2021-10-27 13:23:14 +02:00
|
|
|
0,
|
2022-02-06 19:00:01 +01:00
|
|
|
sizeof (*md));
|
|
|
|
md->num_fresh_coins = rd->fresh_pks_len;
|
|
|
|
md->melted_coin.coin_priv = rd->melt_priv;
|
|
|
|
md->melted_coin.melt_amount_with_fee = rd->melt_amount;
|
|
|
|
md->melted_coin.fee_melt = rd->melt_pk.fee_refresh;
|
|
|
|
md->melted_coin.original_value = rd->melt_pk.value;
|
|
|
|
md->melted_coin.expire_deposit = rd->melt_pk.expire_deposit;
|
2020-02-29 16:42:10 +01:00
|
|
|
GNUNET_assert (GNUNET_OK ==
|
2022-02-06 19:00:01 +01:00
|
|
|
TALER_amount_set_zero (rd->melt_amount.currency,
|
2020-02-29 16:42:10 +01:00
|
|
|
&total));
|
2022-02-06 19:00:01 +01:00
|
|
|
TALER_denom_pub_deep_copy (&md->melted_coin.pub_key,
|
|
|
|
&rd->melt_pk.key);
|
|
|
|
TALER_denom_sig_deep_copy (&md->melted_coin.sig,
|
|
|
|
&rd->melt_sig);
|
|
|
|
md->fresh_pks = GNUNET_new_array (rd->fresh_pks_len,
|
|
|
|
struct TALER_DenominationPublicKey);
|
2022-02-04 22:02:48 +01:00
|
|
|
for (unsigned int i = 0; i<rd->fresh_pks_len; i++)
|
2020-02-29 16:42:10 +01:00
|
|
|
{
|
2022-02-06 19:00:01 +01:00
|
|
|
TALER_denom_pub_deep_copy (&md->fresh_pks[i],
|
|
|
|
&rd->fresh_pks[i].key);
|
2020-04-08 23:52:01 +02:00
|
|
|
if ( (0 >
|
2020-02-29 16:42:10 +01:00
|
|
|
TALER_amount_add (&total,
|
|
|
|
&total,
|
2022-02-04 22:02:48 +01:00
|
|
|
&rd->fresh_pks[i].value)) ||
|
2020-04-08 23:52:01 +02:00
|
|
|
(0 >
|
2020-02-29 16:42:10 +01:00
|
|
|
TALER_amount_add (&total,
|
|
|
|
&total,
|
2022-02-04 22:02:48 +01:00
|
|
|
&rd->fresh_pks[i].fee_withdraw)) )
|
2020-02-29 16:42:10 +01:00
|
|
|
{
|
|
|
|
GNUNET_break (0);
|
2022-02-06 19:00:01 +01:00
|
|
|
TALER_EXCHANGE_free_melt_data_ (md);
|
|
|
|
memset (md,
|
|
|
|
0,
|
|
|
|
sizeof (*md));
|
2022-02-04 22:02:48 +01:00
|
|
|
return GNUNET_SYSERR;
|
2020-02-29 16:42:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* verify that melt_amount is above total cost */
|
|
|
|
if (1 ==
|
|
|
|
TALER_amount_cmp (&total,
|
2022-02-06 19:00:01 +01:00
|
|
|
&rd->melt_amount) )
|
2020-02-29 16:42:10 +01:00
|
|
|
{
|
|
|
|
/* Eh, this operation is more expensive than the
|
|
|
|
@a melt_amount. This is not OK. */
|
|
|
|
GNUNET_break (0);
|
2022-02-06 19:00:01 +01:00
|
|
|
TALER_EXCHANGE_free_melt_data_ (md);
|
|
|
|
memset (md,
|
|
|
|
0,
|
|
|
|
sizeof (*md));
|
2022-02-04 22:02:48 +01:00
|
|
|
return GNUNET_SYSERR;
|
2020-02-29 16:42:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* build up coins */
|
|
|
|
for (unsigned int i = 0; i<TALER_CNC_KAPPA; i++)
|
|
|
|
{
|
2022-02-08 00:12:56 +01:00
|
|
|
TALER_planchet_secret_to_transfer_priv (
|
|
|
|
ps,
|
|
|
|
i,
|
|
|
|
&md->melted_coin.transfer_priv[i]);
|
2020-02-29 16:42:10 +01:00
|
|
|
GNUNET_CRYPTO_ecdhe_key_get_public (
|
2022-02-06 19:00:01 +01:00
|
|
|
&md->melted_coin.transfer_priv[i].ecdhe_priv,
|
2020-02-29 16:42:10 +01:00
|
|
|
&rce[i].transfer_pub.ecdhe_pub);
|
2022-02-04 22:02:48 +01:00
|
|
|
TALER_link_derive_transfer_secret (&rd->melt_priv,
|
2022-02-06 19:00:01 +01:00
|
|
|
&md->melted_coin.transfer_priv[i],
|
2022-02-04 22:02:48 +01:00
|
|
|
&trans_sec[i]);
|
2022-02-06 19:00:01 +01:00
|
|
|
md->fresh_coins[i] = GNUNET_new_array (rd->fresh_pks_len,
|
|
|
|
struct TALER_PlanchetSecretsP);
|
2022-02-04 22:02:48 +01:00
|
|
|
rce[i].new_coins = GNUNET_new_array (rd->fresh_pks_len,
|
2020-02-29 16:42:10 +01:00
|
|
|
struct TALER_RefreshCoinData);
|
2022-02-04 22:02:48 +01:00
|
|
|
for (unsigned int j = 0; j<rd->fresh_pks_len; j++)
|
2020-02-29 16:42:10 +01:00
|
|
|
{
|
2022-02-06 19:00:01 +01:00
|
|
|
struct TALER_PlanchetSecretsP *fc = &md->fresh_coins[i][j];
|
2020-02-29 16:42:10 +01:00
|
|
|
struct TALER_RefreshCoinData *rcd = &rce[i].new_coins[j];
|
|
|
|
struct TALER_PlanchetDetail pd;
|
2021-10-24 12:25:57 +02:00
|
|
|
struct TALER_CoinPubHash c_hash;
|
2022-02-06 19:44:05 +01:00
|
|
|
struct TALER_CoinSpendPrivateKeyP coin_priv;
|
|
|
|
union TALER_DenominationBlindingKeyP bks;
|
2020-02-29 16:42:10 +01:00
|
|
|
|
2022-02-07 12:33:35 +01:00
|
|
|
TALER_transfer_secret_to_planchet_secret (&trans_sec[i],
|
|
|
|
j,
|
|
|
|
fc);
|
2022-02-06 19:44:05 +01:00
|
|
|
TALER_planchet_setup_coin_priv (fc,
|
|
|
|
&alg_values[j],
|
|
|
|
&coin_priv);
|
|
|
|
TALER_planchet_blinding_secret_create (fc,
|
|
|
|
&alg_values[j],
|
|
|
|
&bks);
|
2022-02-09 17:30:02 +01:00
|
|
|
/* Note: we already did this for the /csr request,
|
|
|
|
so this computation is redundant, and here additionally
|
|
|
|
repeated KAPPA times. Could be avoided with slightly
|
|
|
|
more bookkeeping in the future */
|
2022-02-10 23:39:00 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Refresh using PS(%u)=%s\n",
|
|
|
|
j,
|
|
|
|
TALER_B2S (&ps));
|
2022-02-09 17:30:02 +01:00
|
|
|
TALER_cs_refresh_nonce_derive (
|
|
|
|
ps,
|
|
|
|
j,
|
|
|
|
&pd.blinded_planchet.details.cs_blinded_planchet.nonce);
|
2020-02-29 16:42:10 +01:00
|
|
|
if (GNUNET_OK !=
|
2022-02-06 19:00:01 +01:00
|
|
|
TALER_planchet_prepare (&md->fresh_pks[j],
|
2022-02-06 19:44:05 +01:00
|
|
|
&alg_values[j],
|
|
|
|
&bks,
|
|
|
|
&coin_priv,
|
2020-04-12 16:44:28 +02:00
|
|
|
&c_hash,
|
2020-02-29 16:42:10 +01:00
|
|
|
&pd))
|
|
|
|
{
|
|
|
|
GNUNET_break_op (0);
|
2022-02-06 19:00:01 +01:00
|
|
|
TALER_EXCHANGE_free_melt_data_ (md);
|
|
|
|
memset (md,
|
|
|
|
0,
|
|
|
|
sizeof (*md));
|
|
|
|
return GNUNET_SYSERR;
|
2020-02-29 16:42:10 +01:00
|
|
|
}
|
2022-02-06 19:00:01 +01:00
|
|
|
rcd->dk = &md->fresh_pks[j];
|
2022-02-07 13:43:29 +01:00
|
|
|
rcd->blinded_planchet = pd.blinded_planchet;
|
2020-02-29 16:42:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute refresh commitment */
|
2022-02-06 19:00:01 +01:00
|
|
|
TALER_refresh_get_commitment (&md->rc,
|
2020-02-29 16:42:10 +01:00
|
|
|
TALER_CNC_KAPPA,
|
2022-02-06 19:00:01 +01:00
|
|
|
rd->fresh_pks_len,
|
2020-02-29 16:42:10 +01:00
|
|
|
rce,
|
|
|
|
&coin_pub,
|
2022-02-04 22:02:48 +01:00
|
|
|
&rd->melt_amount);
|
2020-02-29 16:42:10 +01:00
|
|
|
for (unsigned int i = 0; i < TALER_CNC_KAPPA; i++)
|
|
|
|
{
|
2022-02-06 19:00:01 +01:00
|
|
|
for (unsigned int j = 0; j < rd->fresh_pks_len; j++)
|
2022-02-07 13:43:29 +01:00
|
|
|
TALER_blinded_planchet_free (&rce[i].new_coins[j].blinded_planchet);
|
2020-07-05 16:56:41 +02:00
|
|
|
GNUNET_free (rce[i].new_coins);
|
2020-02-29 16:42:10 +01:00
|
|
|
}
|
2022-02-04 22:02:48 +01:00
|
|
|
return GNUNET_OK;
|
2020-02-29 16:42:10 +01:00
|
|
|
}
|