summaryrefslogtreecommitdiff
path: root/src/mintdb/perf_taler_mintdb_init.c
blob: 3ddf8abc24dc1b83d8bc66670f3b64e3a48b368e (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
   This file is part of TALER
   Copyright (C) 2014, 2015 Christian Grothoff (and other contributing authors)

   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, If not, see <http://www.gnu.org/licenses/>
   */
/**
 * @file mintdb/perf_taler_mintdb_init.c
 * @brief Interpreter library for mint database performance analysis
 * @author Nicolas Fournier
 */
#include "platform.h"
#include "perf_taler_mintdb_init.h"
#include <gnunet/gnunet_signatures.h>
#include "taler_signatures.h"
#include "taler_amount_lib.h"


#define CURRENCY "EUR"


/**
 * @return a randomly generated CollectableBlindcoin
 */
struct TALER_MINTDB_CollectableBlindcoin *
collectable_blindcoin_init ()
{
  uint32_t random_int;
  struct GNUNET_CRYPTO_rsa_PrivateKey  *denomination_key;
  struct GNUNET_CRYPTO_EddsaPrivateKey *reserve_sig_key;
  struct {
    struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
    uint32_t data;
  } unsigned_data;
  struct TALER_MINTDB_CollectableBlindcoin *coin;


  coin = GNUNET_new (struct TALER_MINTDB_CollectableBlindcoin);
  GNUNET_assert (NULL !=
                 (reserve_sig_key = GNUNET_CRYPTO_eddsa_key_create ()));
  GNUNET_assert (NULL !=
                 (denomination_key = GNUNET_CRYPTO_rsa_private_key_create (512)));
  GNUNET_assert (NULL ==
                 (coin->denom_pub.rsa_public_key =
                  GNUNET_CRYPTO_rsa_private_key_get_public (denomination_key)));
  GNUNET_CRYPTO_eddsa_key_get_public (reserve_sig_key,
                                      &coin->reserve_pub.eddsa_pub);
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount (CURRENCY ":1.1",
                                         &coin->amount_with_fee));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount (CURRENCY ":1.1",
                                         &coin->withdraw_fee));
  random_int =
    GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
  GNUNET_assert (NULL !=
                 (coin->sig.rsa_signature =
                  GNUNET_CRYPTO_rsa_sign (denomination_key,
                                          &random_int,
                                          sizeof (random_int))));
  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
                                    &coin->h_coin_envelope);
  unsigned_data.purpose.size = htonl (sizeof (unsigned_data));
  unsigned_data.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
  unsigned_data.data = htonl (random_int);
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CRYPTO_eddsa_sign (reserve_sig_key,
                                           (struct GNUNET_CRYPTO_EccSignaturePurpose *) &unsigned_data,
                                           &coin->reserve_sig.eddsa_signature));
  GNUNET_free (reserve_sig_key);
  GNUNET_CRYPTO_rsa_private_key_free (denomination_key);
  return coin;
}

/**
 * Liberate memory of @a coin
 */
int
collectable_blindcoin_free (struct TALER_MINTDB_CollectableBlindcoin *coin)
{
  if (NULL == coin)
    return GNUNET_OK;

  GNUNET_CRYPTO_rsa_signature_free (coin->sig.rsa_signature);
  GNUNET_CRYPTO_rsa_public_key_free (coin->denom_pub.rsa_public_key);
  GNUNET_free (coin);
  return GNUNET_OK;
}


/**
 * @return a randomly generated reserve
 */
struct TALER_MINTDB_Reserve *
reserve_init ()
{
  struct TALER_MINTDB_Reserve *reserve;
  struct GNUNET_CRYPTO_EddsaPrivateKey *reserve_priv;

  GNUNET_assert (NULL !=
                 (reserve = GNUNET_new (struct TALER_MINTDB_Reserve)));
  GNUNET_assert (NULL !=
                 (reserve_priv = GNUNET_CRYPTO_eddsa_key_create ()));
  GNUNET_CRYPTO_eddsa_key_get_public (reserve_priv ,
                                      &reserve->pub.eddsa_pub);
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount (CURRENCY ":1.1", &reserve->balance));
  reserve->expiry = GNUNET_TIME_absolute_get_forever_ ();
  GNUNET_free (reserve_priv);
  return reserve;
}


/**
 * Free memory of a reserve
 */
int
reserve_free (struct TALER_MINTDB_Reserve *reserve)
{
  GNUNET_free (reserve);
  return GNUNET_OK;
}


/**
 * @return a randomly generated refresh session
 */
struct TALER_MINTDB_RefreshSession *
refresh_session_init ()
{
  struct TALER_MINTDB_RefreshSession *refresh_session;

  GNUNET_assert (NULL !=
                 (refresh_session = GNUNET_new (struct TALER_MINTDB_RefreshSession)));
  refresh_session->noreveal_index = 1;
  refresh_session->num_oldcoins = 1;
  refresh_session->num_newcoins = 1;

  return refresh_session;
}


/**
 * Free a refresh session
 */
int
refresh_session_free (struct TALER_MINTDB_RefreshSession *refresh_session)
{
  GNUNET_free (refresh_session);
  return GNUNET_OK;
}


/**
 * Create a randomly generated deposit
 */
struct TALER_MINTDB_Deposit *
deposit_init ()
{
  static int transaction_id = 0;
  struct TALER_MINTDB_Deposit *deposit;

  GNUNET_assert (NULL !=
                 (deposit = GNUNET_new (struct TALER_MINTDB_Deposit)));
  deposit-> transaction_id = transaction_id;
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount (CURRENCY ":1.1", 
                                         &deposit->amount_with_fee));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount (CURRENCY ":0.1", 
                                         &deposit->deposit_fee));
  deposit->timestamp = GNUNET_TIME_absolute_get ();
  deposit->refund_deadline = GNUNET_TIME_absolute_get ();
  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
                                    &deposit->h_contract);
  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
                                    &deposit->h_wire);
  // Coin Spend Signature
  {
    struct u32_presign
    {
      struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
      uint32_t data;
    } unsigned_data;
    struct GNUNET_CRYPTO_EddsaPrivateKey *eddsa_prvt;
    uint32_t random_int;

    GNUNET_assert (NULL !=
                   (eddsa_prvt = GNUNET_CRYPTO_eddsa_key_create ()));

    random_int = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 
                                           UINT32_MAX);
    unsigned_data.purpose.size = htonl (sizeof (struct u32_presign));
    unsigned_data.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
    unsigned_data.data = random_int;
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_CRYPTO_eddsa_sign (eddsa_prvt,
                                             &unsigned_data.purpose,
                                             &deposit->csig.eddsa_signature));
    // No need to clear as this is a dummy key
    GNUNET_free (eddsa_prvt);
  }
  // Merchant Key
  {
    struct GNUNET_CRYPTO_EddsaPrivateKey *eddsa_prv;

    GNUNET_assert(NULL !=
                  (eddsa_prv = GNUNET_CRYPTO_eddsa_key_create ()));
    GNUNET_CRYPTO_eddsa_key_get_public (
                                        eddsa_prv,
                                        &deposit->merchant_pub.eddsa_pub);
    GNUNET_free (eddsa_prv);
  }
  // Coin
  {
    {
      struct GNUNET_CRYPTO_EddsaPrivateKey *eddsa_prvt;

      GNUNET_assert (NULL !=
                     (eddsa_prvt = GNUNET_CRYPTO_eddsa_key_create ()));
      GNUNET_CRYPTO_eddsa_key_get_public (
                                          eddsa_prvt,
                                          &deposit->coin.coin_pub.eddsa_pub);
      GNUNET_free (eddsa_prvt);
    }

    {
      struct GNUNET_CRYPTO_rsa_PrivateKey *rsa_prv;

      GNUNET_assert (NULL !=
                     (rsa_prv = GNUNET_CRYPTO_rsa_private_key_create (128)));
      GNUNET_assert (NULL !=
                     (deposit->coin.denom_pub.rsa_public_key =
                      GNUNET_CRYPTO_rsa_private_key_get_public (rsa_prv)));
      GNUNET_assert (NULL !=
                     (deposit->coin.denom_sig.rsa_signature =
                      GNUNET_CRYPTO_rsa_sign (rsa_prv,
                                              &deposit->coin.coin_pub.eddsa_pub,
                                              sizeof (struct GNUNET_CRYPTO_EddsaPublicKey))));
      GNUNET_CRYPTO_rsa_private_key_free (rsa_prv);
    }
  }
  return deposit;
}


/**
 * Free memory of a deposit
 */
int
deposit_free (struct TALER_MINTDB_Deposit *deposit)
{
  if ( NULL == deposit)
    return GNUNET_OK;
  GNUNET_CRYPTO_rsa_public_key_free (deposit->coin.denom_pub.rsa_public_key);
  GNUNET_CRYPTO_rsa_signature_free (deposit->coin.denom_sig.rsa_signature);
  GNUNET_free (deposit);

  return GNUNET_OK;
}


/**
 * Generate a randomly generate DenominationKeyInformation
 */
struct TALER_MINTDB_DenominationKeyIssueInformation *
denomination_init ()
{
  struct TALER_MINTDB_DenominationKeyIssueInformation *dki;
  struct GNUNET_CRYPTO_EddsaPrivateKey *master_prvt;
  struct GNUNET_TIME_Absolute anchor;
  struct TALER_Amount amount;

  GNUNET_assert (NULL !=
                 (dki = GNUNET_new (struct TALER_MINTDB_DenominationKeyIssueInformation)));
  GNUNET_assert (NULL !=
                 (dki->denom_priv.rsa_private_key
                  = GNUNET_CRYPTO_rsa_private_key_create (128)));
  GNUNET_assert (NULL !=
                 (dki->denom_pub.rsa_public_key =
                  GNUNET_CRYPTO_rsa_private_key_get_public (dki->denom_priv.rsa_private_key)));
  GNUNET_CRYPTO_rsa_public_key_hash (dki->denom_pub.rsa_public_key,
                                     &dki->issue.denom_hash);
  GNUNET_assert (NULL !=
                 (master_prvt = GNUNET_CRYPTO_eddsa_key_create ()));
  GNUNET_CRYPTO_eddsa_key_get_public (master_prvt,
                                      &dki->issue.master.eddsa_pub);
  anchor = GNUNET_TIME_absolute_get ();
  dki->issue.start = GNUNET_TIME_absolute_hton (anchor);
  dki->issue.expire_withdraw =
    GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_add (anchor,
                                                         GNUNET_TIME_relative_get_hour_ ()));
  dki->issue.expire_spend =
    GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_add (anchor,
                                                         GNUNET_TIME_relative_get_hour_ ()));
  dki->issue.expire_legal =
    GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_add (anchor,
                                                         GNUNET_TIME_relative_get_hour_ ()));
  GNUNET_assert (GNUNET_OK ==
                 TALER_string_to_amount (CURRENCY ":1.1", &amount));
  TALER_amount_hton (&dki->issue.value, &amount);
  TALER_amount_hton (&dki->issue.fee_withdraw, &amount);
  TALER_amount_hton (&dki->issue.fee_deposit, &amount);
  TALER_amount_hton (&dki->issue.fee_refresh, &amount);
  dki->issue.purpose.purpose = htonl (TALER_SIGNATURE_MASTER_DENOMINATION_KEY_VALIDITY);
  dki->issue.purpose.size =
    htonl (sizeof (struct TALER_MINTDB_DenominationKeyIssueInformation) -
           offsetof (struct TALER_MINTDB_DenominationKeyIssueInformation,
                     issue.purpose));
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CRYPTO_eddsa_sign (master_prvt,
                                           &dki->issue.purpose,
                                           &dki->issue.signature.eddsa_signature));
  GNUNET_free (master_prvt);

  return dki;
}


/**
 * Free memory for a DenominationKeyIssueInformation
 */
int
denomination_free (struct TALER_MINTDB_DenominationKeyIssueInformation *dki)
{
  if (NULL ==dki)
    return GNUNET_OK;
  GNUNET_CRYPTO_rsa_private_key_free (dki->denom_priv.rsa_private_key);
  GNUNET_CRYPTO_rsa_public_key_free (dki->denom_pub.rsa_public_key);
  GNUNET_free (dki);

  return GNUNET_OK;
}