summaryrefslogtreecommitdiff
path: root/src/util/crypto_ecc_dlog.c
blob: 916acd9dd183008bc814fa07af421fd806e0a12b (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
/*
     This file is part of GNUnet.
     Copyright (C) 2012, 2013, 2015 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet 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
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file util/crypto_ecc_dlog.c
 * @brief ECC addition and discreate logarithm for small values.
 *        Allows us to use ECC for computations as long as the
 *        result is relativey small.
 * @author Christian Grothoff
 */
#include "platform.h"
#include <gcrypt.h>
#include "gnunet_crypto_lib.h"
#include "gnunet_container_lib.h"


/**
 * Internal structure used to cache pre-calculated values for DLOG calculation.
 */
struct GNUNET_CRYPTO_EccDlogContext
{
  /**
   * Maximum absolute value the calculation supports.
   */
  unsigned int max;

  /**
   * How much memory should we use (relates to the number of entries in the map).
   */
  unsigned int mem;

  /**
   * Map mapping points (here "interpreted" as EdDSA public keys) to
   * a "void * = long" which corresponds to the numeric value of the
   * point.  As NULL is used to represent "unknown", the actual value
   * represented by the entry in the map is the "long" minus @e max.
   */
  struct GNUNET_CONTAINER_MultiPeerMap *map;

  /**
   * Context to use for operations on the elliptic curve.
   */
  gcry_ctx_t ctx;
};


struct GNUNET_CRYPTO_EccDlogContext *
GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max,
                                unsigned int mem)
{
  struct GNUNET_CRYPTO_EccDlogContext *edc;
  int K = ((max + (mem - 1)) / mem);

  GNUNET_assert (max < INT32_MAX);
  edc = GNUNET_new (struct GNUNET_CRYPTO_EccDlogContext);
  edc->max = max;
  edc->mem = mem;
  edc->map = GNUNET_CONTAINER_multipeermap_create (mem * 2,
                                                   GNUNET_NO);
  for (int i = -(int) mem; i <= (int) mem; i++)
  {
    struct GNUNET_CRYPTO_EccScalar Ki;
    struct GNUNET_PeerIdentity key;

    GNUNET_CRYPTO_ecc_scalar_from_int (K * i,
                                       &Ki);
    if (0 == i) /* libsodium does not like to multiply with zero */
      GNUNET_assert (
        0 ==
        crypto_core_ed25519_sub ((unsigned char *) &key,
                                 (unsigned char *) &key,
                                 (unsigned char *) &key));
    else
      GNUNET_assert (
        0 ==
        crypto_scalarmult_ed25519_base_noclamp ((unsigned char*) &key,
                                                Ki.v));
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "K*i: %d (mem=%u, i=%d) => %s\n",
                K * i,
                mem,
                i,
                GNUNET_i2s (&key));
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_CONTAINER_multipeermap_put (edc->map,
                                                      &key,
                                                      (void *) (long) i + max,
                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  }
  return edc;
}


int
GNUNET_CRYPTO_ecc_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
                        const struct GNUNET_CRYPTO_EccPoint *input)
{
  unsigned int K = ((edc->max + (edc->mem - 1)) / edc->mem);
  int res;
  struct GNUNET_CRYPTO_EccPoint g;
  struct GNUNET_CRYPTO_EccPoint q;
  struct GNUNET_CRYPTO_EccPoint nq;

  {
    struct GNUNET_CRYPTO_EccScalar fact;

    memset (&fact,
            0,
            sizeof (fact));
    sodium_increment (fact.v,
                      sizeof (fact.v));
    GNUNET_assert (0 ==
                   crypto_scalarmult_ed25519_base_noclamp (g.v,
                                                           fact.v));
  }
  /* make compiler happy: initialize q and nq, technically not needed! */
  memset (&q,
          0,
          sizeof (q));
  memset (&nq,
          0,
          sizeof (nq));
  res = INT_MAX;
  for (unsigned int i = 0; i <= edc->max / edc->mem; i++)
  {
    struct GNUNET_PeerIdentity key;
    void *retp;

    GNUNET_assert (sizeof (key) == crypto_scalarmult_BYTES);
    if (0 == i)
    {
      memcpy (&key,
              input,
              sizeof (key));
    }
    else
    {
      memcpy (&key,
              &q,
              sizeof (key));
    }
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Trying offset i=%u): %s\n",
                i,
                GNUNET_i2s (&key));
    retp = GNUNET_CONTAINER_multipeermap_get (edc->map,
                                              &key);
    if (NULL != retp)
    {
      res = (((long) retp) - edc->max) * K - i;
      /* we continue the loop here to make the implementation
         "constant-time". If we do not care about this, we could just
         'break' here and do fewer operations... */
    }
    if (i == edc->max / edc->mem)
      break;
    /* q = q + g */
    if (0 == i)
    {
      GNUNET_assert (0 ==
                     crypto_core_ed25519_add (q.v,
                                              input->v,
                                              g.v));
    }
    else
    {
      GNUNET_assert (0 ==
                     crypto_core_ed25519_add (q.v,
                                              q.v,
                                              g.v));
    }
  }
  return res;
}


void
GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccScalar *r)
{
  crypto_core_ed25519_scalar_random (r->v);
}


void
GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *edc)
{
  GNUNET_CONTAINER_multipeermap_destroy (edc->map);
  GNUNET_free (edc);
}


void
GNUNET_CRYPTO_ecc_dexp (int val,
                        struct GNUNET_CRYPTO_EccPoint *r)
{
  struct GNUNET_CRYPTO_EccScalar fact;

  GNUNET_CRYPTO_ecc_scalar_from_int (val,
                                     &fact);
  crypto_scalarmult_ed25519_base_noclamp (r->v,
                                          fact.v);
}


enum GNUNET_GenericReturnValue
GNUNET_CRYPTO_ecc_dexp_mpi (const struct GNUNET_CRYPTO_EccScalar *val,
                            struct GNUNET_CRYPTO_EccPoint *r)
{
  if (0 ==
      crypto_scalarmult_ed25519_base_noclamp (r->v,
                                              val->v))
    return GNUNET_OK;
  return GNUNET_SYSERR;
}


enum GNUNET_GenericReturnValue
GNUNET_CRYPTO_ecc_add (const struct GNUNET_CRYPTO_EccPoint *a,
                       const struct GNUNET_CRYPTO_EccPoint *b,
                       struct GNUNET_CRYPTO_EccPoint *r)
{
  if (0 ==
      crypto_core_ed25519_add (r->v,
                               a->v,
                               b->v))
    return GNUNET_OK;
  return GNUNET_SYSERR;
}


enum GNUNET_GenericReturnValue
GNUNET_CRYPTO_ecc_pmul_mpi (const struct GNUNET_CRYPTO_EccPoint *p,
                            const struct GNUNET_CRYPTO_EccScalar *val,
                            struct GNUNET_CRYPTO_EccPoint *r)
{
  if (0 ==
      crypto_scalarmult_ed25519_noclamp (r->v,
                                         val->v,
                                         p->v))
    return GNUNET_OK;
  return GNUNET_SYSERR;
}


enum GNUNET_GenericReturnValue
GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccPoint *r,
                       struct GNUNET_CRYPTO_EccPoint *r_inv)
{
  struct GNUNET_CRYPTO_EccScalar s;
  unsigned char inv_s[crypto_scalarmult_ed25519_SCALARBYTES];

  GNUNET_CRYPTO_ecc_random_mod_n (&s);
  if (0 !=
      crypto_scalarmult_ed25519_base_noclamp (r->v,
                                              s.v))
    return GNUNET_SYSERR;
  crypto_core_ed25519_scalar_negate (inv_s,
                                     s.v);
  if (0 !=
      crypto_scalarmult_ed25519_base_noclamp (r_inv->v,
                                              inv_s))
    return GNUNET_SYSERR;
  return GNUNET_OK;
}


void
GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccScalar *r,
                           struct GNUNET_CRYPTO_EccScalar *r_neg)
{
  GNUNET_CRYPTO_ecc_random_mod_n (r);
  crypto_core_ed25519_scalar_negate (r_neg->v,
                                     r->v);
}


void
GNUNET_CRYPTO_ecc_scalar_from_int (int64_t val,
                                   struct GNUNET_CRYPTO_EccScalar *r)
{
  unsigned char fact[crypto_scalarmult_ed25519_SCALARBYTES];
  uint64_t valBe;

  GNUNET_assert (sizeof (*r) == sizeof (fact));
  if (val < 0)
  {
    if (INT64_MIN == val)
      valBe = GNUNET_htonll ((uint64_t) INT64_MAX);
    else
      valBe = GNUNET_htonll ((uint64_t) (-val));
  }
  else
  {
    valBe = GNUNET_htonll ((uint64_t) val);
  }
  memset (fact,
          0,
          sizeof (fact));
  for (unsigned int i = 0; i < sizeof (val); i++)
    fact[i] = ((unsigned char*) &valBe)[sizeof (val) - 1 - i];
  if (val < 0)
  {
    if (INT64_MIN == val)
      /* See above: fact is one too small, increment now that we can */
      sodium_increment (fact,
                        sizeof (fact));
    crypto_core_ed25519_scalar_negate (r->v,
                                       fact);
  }
  else
  {
    memcpy (r,
            fact,
            sizeof (fact));
  }
}


/* end of crypto_ecc_dlog.c */