exchange

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

tokens.c (8295B)


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