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 "taler/taler_util.h"
22 :
23 :
24 : void
25 0 : TALER_token_issue_sig_free (struct TALER_TokenIssueSignature *issue_sig)
26 : {
27 0 : if (NULL != issue_sig->signature)
28 : {
29 0 : GNUNET_CRYPTO_unblinded_sig_decref (issue_sig->signature);
30 0 : issue_sig->signature = NULL;
31 : }
32 0 : }
33 :
34 :
35 : void
36 0 : TALER_blinded_issue_sig_free (
37 : struct TALER_BlindedTokenIssueSignature *issue_sig)
38 : {
39 0 : if (NULL != issue_sig->signature)
40 : {
41 0 : GNUNET_CRYPTO_blinded_sig_decref (issue_sig->signature);
42 0 : issue_sig->signature = NULL;
43 : }
44 0 : }
45 :
46 :
47 : void
48 0 : TALER_token_use_setup_random (struct TALER_TokenUseMasterSecretP *master)
49 : {
50 0 : GNUNET_CRYPTO_random_block (master,
51 : sizeof (*master));
52 0 : }
53 :
54 :
55 : void
56 0 : 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 0 : const struct GNUNET_CRYPTO_BlindingInputValues *bi
62 : = alg_values->blinding_inputs;
63 :
64 0 : switch (bi->cipher)
65 : {
66 0 : case GNUNET_CRYPTO_BSA_INVALID:
67 0 : GNUNET_break (0);
68 0 : memset (token_priv,
69 : 0,
70 : sizeof (*token_priv));
71 0 : return;
72 0 : case GNUNET_CRYPTO_BSA_RSA:
73 0 : 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 0 : return;
82 0 : case GNUNET_CRYPTO_BSA_CS:
83 0 : 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 0 : return;
93 : }
94 0 : GNUNET_assert (0);
95 : }
96 :
97 :
98 : void
99 0 : 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 0 : const struct GNUNET_CRYPTO_BlindingInputValues *bi =
105 : alg_values->blinding_inputs;
106 :
107 0 : switch (bi->cipher)
108 : {
109 0 : case GNUNET_CRYPTO_BSA_INVALID:
110 0 : GNUNET_break (0);
111 0 : return;
112 0 : case GNUNET_CRYPTO_BSA_RSA:
113 0 : 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 0 : return;
122 0 : case GNUNET_CRYPTO_BSA_CS:
123 0 : 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 0 : return;
133 : }
134 0 : GNUNET_assert (0);
135 : }
136 :
137 :
138 : const struct TALER_TokenUseMerchantValues *
139 0 : 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 0 : return &alg_values;
148 : }
149 :
150 :
151 : void
152 0 : TALER_token_blind_input_copy (struct TALER_TokenUseMerchantValues *bi_dst,
153 : const struct TALER_TokenUseMerchantValues *bi_src)
154 : {
155 0 : if (bi_src == TALER_token_blind_input_rsa_singleton ())
156 : {
157 0 : *bi_dst = *bi_src;
158 0 : return;
159 : }
160 : bi_dst->blinding_inputs
161 0 : = GNUNET_CRYPTO_blinding_input_values_incref (bi_src->blinding_inputs);
162 : }
163 :
164 :
165 : enum GNUNET_GenericReturnValue
166 0 : 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 0 : = GNUNET_CRYPTO_blind_sign (issue_priv->private_key,
172 : "tk",
173 0 : envelope->blinded_pub);
174 0 : if (NULL == issue_sig->signature)
175 0 : return GNUNET_SYSERR;
176 0 : return GNUNET_OK;
177 : }
178 :
179 :
180 : enum GNUNET_GenericReturnValue
181 0 : 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 0 : GNUNET_CRYPTO_hash (&use_pub->public_key,
188 : sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
189 : &h_use_pub);
190 :
191 0 : if (GNUNET_OK !=
192 0 : GNUNET_CRYPTO_blind_sig_verify (issue_pub->public_key,
193 0 : ub_sig->signature,
194 : &h_use_pub,
195 : sizeof (h_use_pub)))
196 : {
197 0 : GNUNET_break_op (0);
198 0 : return GNUNET_SYSERR;
199 : }
200 0 : return GNUNET_OK;
201 : }
202 :
203 :
204 : enum GNUNET_GenericReturnValue
205 0 : 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 0 : = GNUNET_CRYPTO_blind_sig_unblind (blinded_sig->signature,
215 : secret,
216 0 : &use_pub_hash->hash,
217 : sizeof (use_pub_hash->hash),
218 0 : alg_values->blinding_inputs,
219 0 : issue_pub->public_key);
220 0 : if (NULL == issue_sig->signature)
221 : {
222 0 : GNUNET_break_op (0);
223 0 : return GNUNET_SYSERR;
224 : }
225 0 : return GNUNET_OK;
226 : }
227 :
228 :
229 : void
230 0 : TALER_token_issue_pub_free (struct TALER_TokenIssuePublicKey *token_pub)
231 : {
232 0 : if (NULL != token_pub->public_key)
233 : {
234 0 : GNUNET_CRYPTO_blind_sign_pub_decref (token_pub->public_key);
235 0 : token_pub->public_key = NULL;
236 : }
237 0 : }
238 :
239 :
240 : int
241 0 : TALER_token_issue_pub_cmp (
242 : struct TALER_TokenIssuePublicKey *tip1,
243 : const struct TALER_TokenIssuePublicKey *tip2)
244 : {
245 0 : if (tip1->public_key->cipher !=
246 0 : tip2->public_key->cipher)
247 0 : return (tip1->public_key->cipher >
248 0 : tip2->public_key->cipher) ? 1 : -1;
249 0 : return GNUNET_CRYPTO_bsign_pub_cmp (tip1->public_key,
250 0 : tip2->public_key);
251 : }
252 :
253 :
254 : void
255 0 : 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 0 : = GNUNET_CRYPTO_bsign_pub_incref (tip_src->public_key);
261 0 : }
|