LCOV - code coverage report
Current view: top level - util - tokens.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 85 0.0 %
Date: 2025-06-05 21:03:14 Functions: 0 13 0.0 %

          Line data    Source code
       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 "platform.h"
      22             : #include "taler_util.h"
      23             : 
      24             : 
      25             : void
      26           0 : TALER_token_issue_sig_free (struct TALER_TokenIssueSignature *issue_sig)
      27             : {
      28           0 :   if (NULL != issue_sig->signature)
      29             :   {
      30           0 :     GNUNET_CRYPTO_unblinded_sig_decref (issue_sig->signature);
      31           0 :     issue_sig->signature = NULL;
      32             :   }
      33           0 : }
      34             : 
      35             : 
      36             : void
      37           0 : TALER_blinded_issue_sig_free (
      38             :   struct TALER_BlindedTokenIssueSignature *issue_sig)
      39             : {
      40           0 :   if (NULL != issue_sig->signature)
      41             :   {
      42           0 :     GNUNET_CRYPTO_blinded_sig_decref (issue_sig->signature);
      43           0 :     issue_sig->signature = NULL;
      44             :   }
      45           0 : }
      46             : 
      47             : 
      48             : void
      49           0 : TALER_token_use_setup_random (struct TALER_TokenUseMasterSecretP *master)
      50             : {
      51           0 :   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
      52             :                               master,
      53             :                               sizeof (*master));
      54           0 : }
      55             : 
      56             : 
      57             : void
      58           0 : 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           0 :   const struct GNUNET_CRYPTO_BlindingInputValues *bi
      64             :     = alg_values->blinding_inputs;
      65             : 
      66           0 :   switch (bi->cipher)
      67             :   {
      68           0 :   case GNUNET_CRYPTO_BSA_INVALID:
      69           0 :     GNUNET_break (0);
      70           0 :     memset (token_priv,
      71             :             0,
      72             :             sizeof (*token_priv));
      73           0 :     return;
      74           0 :   case GNUNET_CRYPTO_BSA_RSA:
      75           0 :     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           0 :     return;
      85           0 :   case GNUNET_CRYPTO_BSA_CS:
      86           0 :     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           0 :     return;
      98             :   }
      99           0 :   GNUNET_assert (0);
     100             : }
     101             : 
     102             : 
     103             : void
     104           0 : 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           0 :   const struct GNUNET_CRYPTO_BlindingInputValues *bi =
     110             :     alg_values->blinding_inputs;
     111             : 
     112           0 :   switch (bi->cipher)
     113             :   {
     114           0 :   case GNUNET_CRYPTO_BSA_INVALID:
     115           0 :     GNUNET_break (0);
     116           0 :     return;
     117           0 :   case GNUNET_CRYPTO_BSA_RSA:
     118           0 :     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           0 :     return;
     128           0 :   case GNUNET_CRYPTO_BSA_CS:
     129           0 :     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           0 :     return;
     141             :   }
     142           0 :   GNUNET_assert (0);
     143             : }
     144             : 
     145             : 
     146             : const struct TALER_TokenUseMerchantValues *
     147           0 : 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           0 :   return &alg_values;
     156             : }
     157             : 
     158             : 
     159             : void
     160           0 : TALER_token_blind_input_copy (struct TALER_TokenUseMerchantValues *bi_dst,
     161             :                               const struct TALER_TokenUseMerchantValues *bi_src)
     162             : {
     163           0 :   if (bi_src == TALER_token_blind_input_rsa_singleton ())
     164             :   {
     165           0 :     *bi_dst = *bi_src;
     166           0 :     return;
     167             :   }
     168             :   bi_dst->blinding_inputs
     169           0 :     = GNUNET_CRYPTO_blinding_input_values_incref (bi_src->blinding_inputs);
     170             : }
     171             : 
     172             : 
     173             : enum GNUNET_GenericReturnValue
     174           0 : 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           0 :     = GNUNET_CRYPTO_blind_sign (issue_priv->private_key,
     180             :                                 "tk",
     181           0 :                                 envelope->blinded_pub);
     182           0 :   if (NULL == issue_sig->signature)
     183           0 :     return GNUNET_SYSERR;
     184           0 :   return GNUNET_OK;
     185             : }
     186             : 
     187             : 
     188             : enum GNUNET_GenericReturnValue
     189           0 : 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           0 :   GNUNET_CRYPTO_hash (&use_pub->public_key,
     196             :                       sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
     197             :                       &h_use_pub);
     198             : 
     199           0 :   if (GNUNET_OK !=
     200           0 :       GNUNET_CRYPTO_blind_sig_verify (issue_pub->public_key,
     201           0 :                                       ub_sig->signature,
     202             :                                       &h_use_pub,
     203             :                                       sizeof (h_use_pub)))
     204             :   {
     205           0 :     GNUNET_break_op (0);
     206           0 :     return GNUNET_SYSERR;
     207             :   }
     208           0 :   return GNUNET_OK;
     209             : }
     210             : 
     211             : 
     212             : enum GNUNET_GenericReturnValue
     213           0 : 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           0 :     = GNUNET_CRYPTO_blind_sig_unblind (blinded_sig->signature,
     223             :                                        secret,
     224           0 :                                        &use_pub_hash->hash,
     225             :                                        sizeof (use_pub_hash->hash),
     226           0 :                                        alg_values->blinding_inputs,
     227           0 :                                        issue_pub->public_key);
     228           0 :   if (NULL == issue_sig->signature)
     229             :   {
     230           0 :     GNUNET_break_op (0);
     231           0 :     return GNUNET_SYSERR;
     232             :   }
     233           0 :   return GNUNET_OK;
     234             : }
     235             : 
     236             : 
     237             : void
     238           0 : TALER_token_issue_pub_free (struct TALER_TokenIssuePublicKey *token_pub)
     239             : {
     240           0 :   if (NULL != token_pub->public_key)
     241             :   {
     242           0 :     GNUNET_CRYPTO_blind_sign_pub_decref (token_pub->public_key);
     243           0 :     token_pub->public_key = NULL;
     244             :   }
     245           0 : }
     246             : 
     247             : 
     248             : int
     249           0 : TALER_token_issue_pub_cmp (
     250             :   struct TALER_TokenIssuePublicKey *tip1,
     251             :   const struct TALER_TokenIssuePublicKey *tip2)
     252             : {
     253           0 :   if (tip1->public_key->cipher !=
     254           0 :       tip2->public_key->cipher)
     255           0 :     return (tip1->public_key->cipher >
     256           0 :             tip2->public_key->cipher) ? 1 : -1;
     257           0 :   return GNUNET_CRYPTO_bsign_pub_cmp (tip1->public_key,
     258           0 :                                       tip2->public_key);
     259             : }
     260             : 
     261             : 
     262             : void
     263           0 : 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           0 :     = GNUNET_CRYPTO_bsign_pub_incref (tip_src->public_key);
     269           0 : }

Generated by: LCOV version 1.16