aboutsummaryrefslogtreecommitdiff
path: root/src/util/tokens.c
blob: 5d99c5dfec76cd3cd7a4cda9b52e8c3ed8b6a62f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
  This file is part of TALER
  Copyright (C) 2024 Taler Systems SA

  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 tokens.c
 * @brief token family utility functions
 * @author Christian Blättler
 */
#include "platform.h"
#include "taler_util.h"


void
TALER_token_issue_sig_free (struct TALER_TokenIssueSignatureP *issue_sig)
{
  if (NULL != issue_sig->signature)
  {
    GNUNET_CRYPTO_unblinded_sig_decref (issue_sig->signature);
    issue_sig->signature = NULL;
  }
}


void
TALER_blinded_issue_sig_free (
  struct TALER_TokenIssueBlindSignatureP *issue_sig)
{
  if (NULL != issue_sig->signature)
  {
    GNUNET_CRYPTO_blinded_sig_decref (issue_sig->signature);
    issue_sig->signature = NULL;
  }
}


void
TALER_token_use_setup_random (struct TALER_TokenUseMasterSecretP *master)
{
  GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_STRONG,
                              master,
                              sizeof (*master));
}


void
TALER_token_use_setup_priv (
  const struct TALER_TokenUseMasterSecretP *master,
  const struct TALER_TokenUseMerchantValues *alg_values,
  struct TALER_TokenUsePrivateKeyP *token_priv)
{
  /* TODO: Maybe extract common code between this
           function and TALER_planchet_setup_coin_priv (). */
  const struct GNUNET_CRYPTO_BlindingInputValues *bi
    = alg_values->blinding_inputs;

  switch (bi->cipher)
  {
  case GNUNET_CRYPTO_BSA_INVALID:
    GNUNET_break (0);
    memset (token_priv,
            0,
            sizeof (*token_priv));
    return;
  case GNUNET_CRYPTO_BSA_RSA:
    GNUNET_assert (GNUNET_YES ==
                   GNUNET_CRYPTO_kdf (token_priv,
                                      sizeof (*token_priv),
                                      "token",
                                      strlen ("token"),
                                      master,
                                      sizeof(*master),
                                      NULL,
                                      0));
    return;
  case GNUNET_CRYPTO_BSA_CS:
    GNUNET_assert (GNUNET_YES ==
                   GNUNET_CRYPTO_kdf (token_priv,
                                      sizeof (*token_priv),
                                      "token",
                                      strlen ("token"),
                                      master,
                                      sizeof(*master),
                                      &bi->details.cs_values,
                                      sizeof(bi->details.cs_values),
                                      NULL,
                                      0));
    return;
  }
  GNUNET_assert (0);
}


void
TALER_token_use_blinding_secret_create (
  const struct TALER_TokenUseMasterSecretP *master,
  const struct TALER_TokenUseMerchantValues *alg_values,
  union GNUNET_CRYPTO_BlindingSecretP *bks)
{
  /* TODO: Extract common code between this
           function and TALER_planchet_blinding_secret_create (). */
  const struct GNUNET_CRYPTO_BlindingInputValues *bi =
    alg_values->blinding_inputs;

  switch (bi->cipher)
  {
  case GNUNET_CRYPTO_BSA_INVALID:
    GNUNET_break (0);
    return;
  case GNUNET_CRYPTO_BSA_RSA:
    GNUNET_assert (GNUNET_YES ==
                   GNUNET_CRYPTO_kdf (&bks->rsa_bks,
                                      sizeof (bks->rsa_bks),
                                      "bks",
                                      strlen ("bks"),
                                      master,
                                      sizeof(*master),
                                      NULL,
                                      0));
    return;
  case GNUNET_CRYPTO_BSA_CS:
    GNUNET_assert (GNUNET_YES ==
                   GNUNET_CRYPTO_kdf (&bks->nonce,
                                      sizeof (bks->nonce),
                                      "bseed",
                                      strlen ("bseed"),
                                      master,
                                      sizeof(*master),
                                      &bi->details.cs_values,
                                      sizeof(bi->details.cs_values),
                                      NULL,
                                      0));
    return;
  }
  GNUNET_assert (0);
}


const struct TALER_TokenUseMerchantValues *
TALER_token_blind_input_rsa_singleton ()
{
  static struct GNUNET_CRYPTO_BlindingInputValues bi = {
    .cipher = GNUNET_CRYPTO_BSA_RSA
  };
  static struct TALER_TokenUseMerchantValues alg_values = {
    .blinding_inputs = &bi
  };
  return &alg_values;
}


void
TALER_token_blind_input_copy (struct TALER_TokenUseMerchantValues *bi_dst,
                              const struct TALER_TokenUseMerchantValues *bi_src)
{
  if (bi_src == TALER_token_blind_input_rsa_singleton ())
  {
    *bi_dst = *bi_src;
    return;
  }
  bi_dst->blinding_inputs
    = GNUNET_CRYPTO_blinding_input_values_incref (bi_src->blinding_inputs);
}


enum GNUNET_GenericReturnValue
TALER_token_issue_sign (const struct TALER_TokenIssuePrivateKeyP *issue_priv,
                        const struct TALER_TokenEnvelopeP *envelope,
                        struct TALER_TokenIssueBlindSignatureP *issue_sig)
{
  issue_sig->signature
    = GNUNET_CRYPTO_blind_sign (issue_priv->private_key,
                                "tk", /* TODO: What is a good value here? */
                                envelope->blinded_pub);
  if (NULL == issue_sig->signature)
    return GNUNET_SYSERR;
  return GNUNET_OK;
}

enum GNUNET_GenericReturnValue
TALER_token_issue_verify (const struct TALER_TokenUsePublicKeyP *use_pub,
                          const struct TALER_TokenIssuePublicKeyP *issue_pub,
                          const struct TALER_TokenIssueSignatureP *ub_sig)
{
  struct GNUNET_HashCode h_use_pub;

  GNUNET_CRYPTO_hash (&use_pub->public_key,
                      sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
                      &h_use_pub);

  if (GNUNET_OK !=
      GNUNET_CRYPTO_blind_sig_verify (issue_pub->public_key,
                                      ub_sig->signature,
                                      &h_use_pub,
                                      sizeof (h_use_pub)))
  {
    GNUNET_break_op (0);
    return GNUNET_SYSERR;
  }
  return GNUNET_OK;
}

enum GNUNET_GenericReturnValue
TALER_token_issue_sig_unblind (
  struct TALER_TokenIssueSignatureP *issue_sig,
  const struct TALER_TokenIssueBlindSignatureP *blinded_sig,
  const union GNUNET_CRYPTO_BlindingSecretP *secret,
  const struct TALER_TokenUsePublicKeyHashP *use_pub_hash,
  const struct TALER_TokenUseMerchantValues *alg_values,
  const struct TALER_TokenIssuePublicKeyP *issue_pub)
{
  issue_sig->signature
        = GNUNET_CRYPTO_blind_sig_unblind (blinded_sig->signature,
                                           secret,
                                           &use_pub_hash->hash,
                                           sizeof (use_pub_hash->hash),
                                           alg_values->blinding_inputs,
                                           issue_pub->public_key);
  if (NULL == issue_sig->signature)
  {
    GNUNET_break_op (0);
    return GNUNET_SYSERR;
  }
  return GNUNET_OK;
}