aboutsummaryrefslogtreecommitdiff
path: root/src/util/donau_crypto.c
blob: 6d864665d55c6a9a3667ea17c15e62059671ff43 (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
/*
  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 util/donau_crypto.c
 * @brief Cryptographic utility functions
 * @author Lukas Matyja
 */
#include <taler/platform.h>
#include <taler/taler_util.h>
#include "donau_util.h"
#include <gcrypt.h>

GNUNET_NETWORK_STRUCT_BEGIN
/**
 * Structure we hash to compute the group key for
 * a donation unit group.
 */
struct DonationUnitGroupP
{
  /**
   * Value of coins in this donation unit group.
   */
  struct TALER_AmountNBO value;

  /**
   * Cipher used for the donation unit, in NBO.
   */
  uint32_t cipher GNUNET_PACKED;
};
GNUNET_NETWORK_STRUCT_END


void
DONAU_donation_unit_group_get_key (
  const struct DONAU_DonationUnitGroup *dg,
  struct GNUNET_HashCode *key)
{
  struct DonationUnitGroupP dgp = {
    .cipher = htonl (dg->cipher)
  };

  TALER_amount_hton (&dgp.value,
                     &dg->value);
  GNUNET_CRYPTO_hash (&dgp,
                      sizeof (dgp),
                      key);
}


int
DONAU_donation_unit_pub_cmp (
  const struct DONAU_DonationUnitPublicKey *donation_unit1,
  const struct DONAU_DonationUnitPublicKey *donation_unit2)
{
  if (donation_unit1->bsign_pub_key->cipher !=
      donation_unit2->bsign_pub_key->cipher)
    return (donation_unit1->bsign_pub_key->cipher >
            donation_unit2->bsign_pub_key->cipher) ? 1 : -1;
  return GNUNET_CRYPTO_bsign_pub_cmp (donation_unit1->bsign_pub_key,
                                      donation_unit2->bsign_pub_key);
}


void
DONAU_donation_unit_pub_deep_copy (
  struct DONAU_DonationUnitPublicKey *donation_unit_dst,
  const struct DONAU_DonationUnitPublicKey *donation_unit_src)
{
  donation_unit_dst->bsign_pub_key
    = GNUNET_CRYPTO_bsign_pub_incref (donation_unit_src->bsign_pub_key);
}


void
DONAU_donation_unit_pub_free (
  struct DONAU_DonationUnitPublicKey *donation_unit_pub)
{
  if (NULL != donation_unit_pub->bsign_pub_key)
  {
    GNUNET_CRYPTO_blind_sign_pub_decref (donation_unit_pub->bsign_pub_key);
    donation_unit_pub->bsign_pub_key = NULL;
  }
}


void
DONAU_blinded_donation_unit_sig_free (
  struct DONAU_BlindedDonationUnitSignature *du_sig)
{
  if (NULL != du_sig->blinded_sig)
  {
    GNUNET_CRYPTO_blinded_sig_decref (du_sig->blinded_sig);
    du_sig->blinded_sig = NULL;
  }
}


void
DONAU_donation_unit_pub_hash (
  const struct DONAU_DonationUnitPublicKey *donation_unit_pub,
  struct DONAU_DonationUnitHashP *donation_unit_hash
  )
{
  struct GNUNET_CRYPTO_BlindSignPublicKey *bsp
    = donation_unit_pub->bsign_pub_key;

  switch (bsp->cipher)
  {
  case GNUNET_CRYPTO_BSA_RSA:
    /* Important: this MUST match the way the RSA-secmod does the
       hashing of the public keys (see donau-httpd_keys.c) */
    GNUNET_CRYPTO_rsa_public_key_hash (bsp->details.rsa_public_key,
                                       &donation_unit_hash->hash);
    break;
  case GNUNET_CRYPTO_BSA_CS:
    /* Important: this MUST match the way the CS-secmod does the
       hashing of the public keys (see donau-httpd_keys.c) */
    GNUNET_CRYPTO_hash (&bsp->details.cs_public_key,
                        sizeof(bsp->details.cs_public_key),
                        &donation_unit_hash->hash);
    break;
  default:
    GNUNET_assert (0);
  }
}


void
DONAU_unique_donor_id_hash (const struct DONAU_HashDonorTaxId *h_donor_tax_id,
                            const struct DONAU_UniqueDonorIdentifierNonce *nonce
                            ,
                            struct DONAU_UniqueDonorIdentifierHashP *h_udi)
{
  struct GNUNET_HashContext *hash_context;
  hash_context = GNUNET_CRYPTO_hash_context_start ();

  GNUNET_CRYPTO_hash_context_read (
    hash_context,
    h_donor_tax_id,
    sizeof(struct DONAU_HashDonorTaxId));
  GNUNET_CRYPTO_hash_context_read (
    hash_context,
    nonce,
    sizeof(struct DONAU_UniqueDonorIdentifierNonce));
  GNUNET_CRYPTO_hash_context_finish (
    hash_context,
    &h_udi->hash);
}


enum GNUNET_GenericReturnValue
DONAU_donation_receipt_verify (
  const struct DONAU_DonationUnitPublicKey *donation_unit_pub,
  const struct DONAU_UniqueDonorIdentifierHashP *h_udi,
  const struct DONAU_DonationUnitSignature *donation_unit_sig)
{
  return GNUNET_CRYPTO_blind_sig_verify (donation_unit_pub->bsign_pub_key,
                                         donation_unit_sig->unblinded_sig,
                                         h_udi,
                                         sizeof (*h_udi));
}