exchange

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

tokens.c (7533B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2024 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 tokens.c
     18  * @brief token family utility functions
     19  * @author Christian Blättler
     20  */
     21 #include "taler/taler_util.h"
     22 
     23 
     24 void
     25 TALER_token_issue_sig_free (struct TALER_TokenIssueSignature *issue_sig)
     26 {
     27   if (NULL != issue_sig->signature)
     28   {
     29     GNUNET_CRYPTO_unblinded_sig_decref (issue_sig->signature);
     30     issue_sig->signature = NULL;
     31   }
     32 }
     33 
     34 
     35 void
     36 TALER_blinded_issue_sig_free (
     37   struct TALER_BlindedTokenIssueSignature *issue_sig)
     38 {
     39   if (NULL != issue_sig->signature)
     40   {
     41     GNUNET_CRYPTO_blinded_sig_decref (issue_sig->signature);
     42     issue_sig->signature = NULL;
     43   }
     44 }
     45 
     46 
     47 void
     48 TALER_token_use_setup_random (struct TALER_TokenUseMasterSecretP *master)
     49 {
     50   GNUNET_CRYPTO_random_block (master,
     51                               sizeof (*master));
     52 }
     53 
     54 
     55 void
     56 TALER_token_use_setup_priv (
     57   const struct TALER_TokenUseMasterSecretP *master,
     58   const struct TALER_TokenUseMerchantValues *alg_values,
     59   struct TALER_TokenUsePrivateKeyP *token_priv)
     60 {
     61   const struct GNUNET_CRYPTO_BlindingInputValues *bi
     62     = alg_values->blinding_inputs;
     63 
     64   switch (bi->cipher)
     65   {
     66   case GNUNET_CRYPTO_BSA_INVALID:
     67     GNUNET_break (0);
     68     memset (token_priv,
     69             0,
     70             sizeof (*token_priv));
     71     return;
     72   case GNUNET_CRYPTO_BSA_RSA:
     73     GNUNET_assert (GNUNET_YES ==
     74                    GNUNET_CRYPTO_hkdf_gnunet (
     75                      token_priv,
     76                      sizeof (*token_priv),
     77                      "token",
     78                      strlen ("token"),
     79                      master,
     80                      sizeof(*master)));
     81     return;
     82   case GNUNET_CRYPTO_BSA_CS:
     83     GNUNET_assert (GNUNET_YES ==
     84                    GNUNET_CRYPTO_hkdf_gnunet (
     85                      token_priv,
     86                      sizeof (*token_priv),
     87                      "token",
     88                      strlen ("token"),
     89                      master,
     90                      sizeof(*master),
     91                      GNUNET_CRYPTO_kdf_arg_auto (&bi->details.cs_values)));
     92     return;
     93   }
     94   GNUNET_assert (0);
     95 }
     96 
     97 
     98 void
     99 TALER_token_use_blinding_secret_create (
    100   const struct TALER_TokenUseMasterSecretP *master,
    101   const struct TALER_TokenUseMerchantValues *alg_values,
    102   union GNUNET_CRYPTO_BlindingSecretP *bks)
    103 {
    104   const struct GNUNET_CRYPTO_BlindingInputValues *bi =
    105     alg_values->blinding_inputs;
    106 
    107   switch (bi->cipher)
    108   {
    109   case GNUNET_CRYPTO_BSA_INVALID:
    110     GNUNET_break (0);
    111     return;
    112   case GNUNET_CRYPTO_BSA_RSA:
    113     GNUNET_assert (GNUNET_YES ==
    114                    GNUNET_CRYPTO_hkdf_gnunet (
    115                      &bks->rsa_bks,
    116                      sizeof (bks->rsa_bks),
    117                      "bks",
    118                      strlen ("bks"),
    119                      master,
    120                      sizeof(*master)));
    121     return;
    122   case GNUNET_CRYPTO_BSA_CS:
    123     GNUNET_assert (GNUNET_YES ==
    124                    GNUNET_CRYPTO_hkdf_gnunet (
    125                      &bks->nonce,
    126                      sizeof (bks->nonce),
    127                      "bseed",
    128                      strlen ("bseed"),
    129                      master,
    130                      sizeof(*master),
    131                      GNUNET_CRYPTO_kdf_arg_auto (&bi->details.cs_values)));
    132     return;
    133   }
    134   GNUNET_assert (0);
    135 }
    136 
    137 
    138 const struct TALER_TokenUseMerchantValues *
    139 TALER_token_blind_input_rsa_singleton ()
    140 {
    141   static struct GNUNET_CRYPTO_BlindingInputValues bi = {
    142     .cipher = GNUNET_CRYPTO_BSA_RSA
    143   };
    144   static struct TALER_TokenUseMerchantValues alg_values = {
    145     .blinding_inputs = &bi
    146   };
    147   return &alg_values;
    148 }
    149 
    150 
    151 void
    152 TALER_token_blind_input_copy (struct TALER_TokenUseMerchantValues *bi_dst,
    153                               const struct TALER_TokenUseMerchantValues *bi_src)
    154 {
    155   if (bi_src == TALER_token_blind_input_rsa_singleton ())
    156   {
    157     *bi_dst = *bi_src;
    158     return;
    159   }
    160   bi_dst->blinding_inputs
    161     = GNUNET_CRYPTO_blinding_input_values_incref (bi_src->blinding_inputs);
    162 }
    163 
    164 
    165 enum GNUNET_GenericReturnValue
    166 TALER_token_issue_sign (const struct TALER_TokenIssuePrivateKey *issue_priv,
    167                         const struct TALER_TokenEnvelope *envelope,
    168                         struct TALER_BlindedTokenIssueSignature *issue_sig)
    169 {
    170   issue_sig->signature
    171     = GNUNET_CRYPTO_blind_sign (issue_priv->private_key,
    172                                 "tk",
    173                                 envelope->blinded_pub);
    174   if (NULL == issue_sig->signature)
    175     return GNUNET_SYSERR;
    176   return GNUNET_OK;
    177 }
    178 
    179 
    180 enum GNUNET_GenericReturnValue
    181 TALER_token_issue_verify (const struct TALER_TokenUsePublicKeyP *use_pub,
    182                           const struct TALER_TokenIssuePublicKey *issue_pub,
    183                           const struct TALER_TokenIssueSignature *ub_sig)
    184 {
    185   struct GNUNET_HashCode h_use_pub;
    186 
    187   GNUNET_CRYPTO_hash (&use_pub->public_key,
    188                       sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
    189                       &h_use_pub);
    190 
    191   if (GNUNET_OK !=
    192       GNUNET_CRYPTO_blind_sig_verify (issue_pub->public_key,
    193                                       ub_sig->signature,
    194                                       &h_use_pub,
    195                                       sizeof (h_use_pub)))
    196   {
    197     GNUNET_break_op (0);
    198     return GNUNET_SYSERR;
    199   }
    200   return GNUNET_OK;
    201 }
    202 
    203 
    204 enum GNUNET_GenericReturnValue
    205 TALER_token_issue_sig_unblind (
    206   struct TALER_TokenIssueSignature *issue_sig,
    207   const struct TALER_BlindedTokenIssueSignature *blinded_sig,
    208   const union GNUNET_CRYPTO_BlindingSecretP *secret,
    209   const struct TALER_TokenUsePublicKeyHashP *use_pub_hash,
    210   const struct TALER_TokenUseMerchantValues *alg_values,
    211   const struct TALER_TokenIssuePublicKey *issue_pub)
    212 {
    213   issue_sig->signature
    214     = GNUNET_CRYPTO_blind_sig_unblind (blinded_sig->signature,
    215                                        secret,
    216                                        &use_pub_hash->hash,
    217                                        sizeof (use_pub_hash->hash),
    218                                        alg_values->blinding_inputs,
    219                                        issue_pub->public_key);
    220   if (NULL == issue_sig->signature)
    221   {
    222     GNUNET_break_op (0);
    223     return GNUNET_SYSERR;
    224   }
    225   return GNUNET_OK;
    226 }
    227 
    228 
    229 void
    230 TALER_token_issue_pub_free (struct TALER_TokenIssuePublicKey *token_pub)
    231 {
    232   if (NULL != token_pub->public_key)
    233   {
    234     GNUNET_CRYPTO_blind_sign_pub_decref (token_pub->public_key);
    235     token_pub->public_key = NULL;
    236   }
    237 }
    238 
    239 
    240 int
    241 TALER_token_issue_pub_cmp (
    242   struct TALER_TokenIssuePublicKey *tip1,
    243   const struct TALER_TokenIssuePublicKey *tip2)
    244 {
    245   if (tip1->public_key->cipher !=
    246       tip2->public_key->cipher)
    247     return (tip1->public_key->cipher >
    248             tip2->public_key->cipher) ? 1 : -1;
    249   return GNUNET_CRYPTO_bsign_pub_cmp (tip1->public_key,
    250                                       tip2->public_key);
    251 }
    252 
    253 
    254 void
    255 TALER_token_issue_pub_copy (
    256   struct TALER_TokenIssuePublicKey *tip_dst,
    257   const struct TALER_TokenIssuePublicKey *tip_src)
    258 {
    259   tip_dst->public_key
    260     = GNUNET_CRYPTO_bsign_pub_incref (tip_src->public_key);
    261 }