summaryrefslogtreecommitdiff
path: root/src/util/donau_crypto.c
blob: 5eedc5273644761d513dc434fce493f791290ae5 (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
/*
  This file is part of TALER
  Copyright (C) 2014-2022 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_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);
  }
}