summaryrefslogtreecommitdiff
path: root/src/util/crypto_ecc_dlog.c
blob: 33fc3f0e4f3a99dc2dc260799c241b0fa09fd088 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
     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"


/**
 * Name of the curve we are using.  Note that we have hard-coded
 * structs that use 256 bits, so using a bigger curve will require
 * changes that break stuff badly.  The name of the curve given here
 * must be agreed by all peers and be supported by libgcrypt.
 */
#define CURVE "Ed25519"


/**
 *
 */
static void
extract_pk (gcry_mpi_point_t pt,
            gcry_ctx_t ctx,
            struct GNUNET_PeerIdentity *pid)
{
  gcry_mpi_t q_y;

  GNUNET_assert (0 == gcry_mpi_ec_set_point ("q", pt, ctx));
  q_y = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0);
  GNUNET_assert (q_y);
  GNUNET_CRYPTO_mpi_print_unsigned (pid->public_key.q_y,
                                    sizeof(pid->public_key.q_y),
                                    q_y);
  gcry_mpi_release (q_y);
}


/**
 * 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;
};


/**
 * Do pre-calculation for ECC discrete logarithm for small factors.
 *
 * @param max maximum value the factor can be
 * @param mem memory to use (should be smaller than @a max), must not be zero.
 * @return NULL on error
 */
struct GNUNET_CRYPTO_EccDlogContext *
GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max,
                                unsigned int mem)
{
  struct GNUNET_CRYPTO_EccDlogContext *edc;
  unsigned int K = ((max + (mem - 1)) / mem);
  // gcry_mpi_point_t g;
  struct GNUNET_PeerIdentity key;
  // gcry_mpi_point_t gKi;
  // gcry_mpi_t fact;
  // gcry_mpi_t n;
  unsigned int i;

  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);

  /*GNUNET_assert (0 == gcry_mpi_ec_new (&edc->ctx,
                                       NULL,
                                       CURVE));
  g = gcry_mpi_ec_get_point ("g", edc->ctx, 0);
  GNUNET_assert (NULL != g);
  fact = gcry_mpi_new (0);
  gKi = gcry_mpi_point_new (0);*/
  unsigned char fact[crypto_core_ed25519_NONREDUCEDSCALARBYTES];
  unsigned char Ki[crypto_scalarmult_ed25519_SCALARBYTES];
  unsigned char nKi[crypto_scalarmult_ed25519_SCALARBYTES];
  unsigned int Kle = htonl (K);
  unsigned char Kles[crypto_core_ed25519_NONREDUCEDSCALARBYTES];
  memset (fact, 0, sizeof (fact));
  memset (Ki, 0, sizeof (Ki));
  memset (nKi, 0, sizeof (nKi));
  memset (Kles, 0, sizeof (Kles));
  // memcpy (Kles, &Kle, sizeof (Kle));
  for (i = 0; i < K; i++)
    sodium_increment (Kles, sizeof (Kles));
  for (i = 1; i <= mem; i++)
  {
    sodium_increment (fact, sizeof (fact));
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Calculating #%u\n", i);
    memset (Ki, 0, sizeof (Ki));
    crypto_core_ed25519_scalar_mul (Ki, fact, Kles);
    GNUNET_assert (0 ==
                   crypto_scalarmult_ed25519_base_noclamp ((unsigned
                                                            char*) &key, Ki));
    /*gcry_mpi_set_ui (fact, i * K);
    gcry_mpi_ec_mul (gKi, fact, g, edc->ctx);
    extract_pk (gKi, edc->ctx, &key);*/
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_CONTAINER_multipeermap_put (edc->map,
                                                      &key,
                                                      (void *) (long) i + max,
                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  }
  /* negative values */
  // n = gcry_mpi_ec_get_mpi ("n", edc->ctx, 1);
  memset (fact, 0, sizeof (fact));
  for (i = 1; i < mem; i++)
  {
    sodium_increment (fact, sizeof (fact));
    memset (Ki, 0, sizeof (Ki));
    memset (nKi, 0, sizeof (nKi));
    crypto_core_ed25519_scalar_mul (Ki, fact, Kles);
    crypto_core_ed25519_scalar_negate (nKi, Ki);
    crypto_scalarmult_ed25519_base_noclamp ((unsigned char*) &key, nKi);
    /*gcry_mpi_set_ui (fact, i * K);
    gcry_mpi_sub (fact, n, fact);
    gcry_mpi_ec_mul (gKi, fact, g, edc->ctx);
    extract_pk (gKi, edc->ctx, &key);*/
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_CONTAINER_multipeermap_put (edc->map,
                                                      &key,
                                                      (void *) (long) max - i,
                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  }
  /*gcry_mpi_release (fact);
  gcry_mpi_release (n);
  gcry_mpi_point_release (gKi);
  gcry_mpi_point_release (g);*/
  return edc;
}


/**
 * Calculate ECC discrete logarithm for small factors.
 *
 * @param edc precalculated values, determine range of factors
 * @param input point on the curve to factor
 * @return INT_MAX if dlog failed, otherwise the factor
 */
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);
  // gcry_mpi_point_t g;
  struct GNUNET_PeerIdentity key;
  // gcry_mpi_point_t q;
  unsigned int i;
  int res;
  void *retp;

  unsigned char g[crypto_scalarmult_BYTES];
  unsigned char q[crypto_scalarmult_BYTES];
  unsigned char fact[crypto_scalarmult_BYTES];
  memset (g, 0, crypto_scalarmult_BYTES);
  memset (q, 0, crypto_scalarmult_BYTES);
  memset (fact, 0, crypto_scalarmult_BYTES);
  sodium_increment (fact, sizeof (fact));
  crypto_scalarmult_ed25519_base_noclamp (g, fact);
  /*g = gcry_mpi_ec_get_point ("g", edc->ctx, 0);
  GNUNET_assert (NULL != g);
  q = gcry_mpi_point_new (0);*/

  // unsigned char key[crypto_scalarmult_BYTES];
  memset (&key, 0, crypto_scalarmult_BYTES);
  res = INT_MAX;
  for (i = 0; i <= edc->max / edc->mem; i++)
  {
    /*if (0 == i)
      extract_pk (input, edc->ctx, &key);
    else
      extract_pk (q, edc->ctx, &key);*/
    if (0 == i)
      memcpy (&key, input, crypto_scalarmult_BYTES);
    else
      memcpy (&key, q, crypto_scalarmult_BYTES);
    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)
      gcry_mpi_ec_add (q, input, g, edc->ctx);
    else
      gcry_mpi_ec_add (q, q, g, edc->ctx);*/
    if (0 == i)
      crypto_core_ed25519_add (q, input->v, g);
    else
      crypto_core_ed25519_add (q, q, g);
  }
  // gcry_mpi_point_release (g);
  // gcry_mpi_point_release (q);

  return res;
}


/**
 * Generate a random value mod n.
 *
 * @param edc ECC context
 * @return random value mod n.
 */
struct GNUNET_CRYPTO_EccScalar*
GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc)
{
  /*gcry_mpi_t n;
  unsigned int highbit;
  gcry_mpi_t r;*/
  struct GNUNET_CRYPTO_EccScalar *res;

  res = GNUNET_new (struct GNUNET_CRYPTO_EccScalar);
  crypto_core_ed25519_scalar_random (res->v);
  return res;
  // n = gcry_mpi_ec_get_mpi ("n", edc->ctx, 1);

  /* check public key for number of bits, bail out if key is all zeros */
  // highbit = 256; /* Curve25519 */
  // while ((! gcry_mpi_test_bit (n, highbit)) &&
  //       (0 != highbit))
  //  highbit--;
  // GNUNET_assert (0 != highbit);
  /* generate fact < n (without bias) */
  // GNUNET_assert (NULL != (r = gcry_mpi_new (0)));
  /*do
  {
    gcry_mpi_randomize (r,
                        highbit + 1,
                        GCRY_STRONG_RANDOM);
  }
  while (gcry_mpi_cmp (r, n) >= 0);
  gcry_mpi_release (n);
  return r;*/
}


/**
 * Release precalculated values.
 *
 * @param edc dlog context
 */
void
GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *edc)
{
  // gcry_ctx_release (edc->ctx);
  GNUNET_CONTAINER_multipeermap_destroy (edc->map);
  GNUNET_free (edc);
}


/**
 * Multiply the generator g of the elliptic curve by @a val
 * to obtain the point on the curve representing @a val.
 * Afterwards, point addition will correspond to integer
 * addition.  #GNUNET_CRYPTO_ecc_dlog() can be used to
 * convert a point back to an integer (as long as the
 * integer is smaller than the MAX of the @a edc context).
 *
 * @param edc calculation context for ECC operations
 * @param val value to encode into a point
 * @return representation of the value as an ECC point,
 *         must be freed using #GNUNET_CRYPTO_ecc_free()
 */
struct GNUNET_CRYPTO_EccPoint*
GNUNET_CRYPTO_ecc_dexp (struct GNUNET_CRYPTO_EccDlogContext *edc,
                        int val)
{
  /*gcry_mpi_t fact;
  gcry_mpi_t n;
  gcry_mpi_point_t g;
  gcry_mpi_point_t r;

  g = gcry_mpi_ec_get_point ("g", edc->ctx, 0);
  GNUNET_assert (NULL != g);
  fact = gcry_mpi_new (0);*/
  struct GNUNET_CRYPTO_EccPoint *r;
  r = GNUNET_new (struct GNUNET_CRYPTO_EccPoint);
  unsigned char fact[crypto_scalarmult_ed25519_SCALARBYTES];
  unsigned char nFact[crypto_scalarmult_ed25519_SCALARBYTES];
  unsigned int valLe = htonl (val); // little-endian
  memset (fact, 0, sizeof (fact));
  memset (nFact, 0, sizeof (fact));
  // memcpy (fact, &valLe, sizeof (valLe));
  if (val < 0)
  {
    for (int i = 0; i < -val; i++)
      sodium_increment (fact, sizeof (fact));
    crypto_core_ed25519_scalar_negate (nFact, fact);
    crypto_scalarmult_ed25519_base_noclamp (r->v, nFact);

    /* = gcry_mpi_ec_get_mpi ("n", edc->ctx, 1);
    gcry_mpi_set_ui (fact, -val);
    gcry_mpi_sub (fact, n, fact);
    gcry_mpi_release (n);*/
  }
  else
  {
    for (int i = 0; i < val; i++)
      sodium_increment (fact, sizeof (fact));
    crypto_scalarmult_ed25519_base_noclamp (r->v, fact);
    /*gcry_mpi_set_ui (fact, val);*/
  }
  return r;
  /*r = gcry_mpi_point_new (0);
  gcry_mpi_ec_mul (r, fact, g, edc->ctx);
  gcry_mpi_release (fact);
  gcry_mpi_point_release (g);
  return r;*/
}


/**
 * Multiply the generator g of the elliptic curve by @a val
 * to obtain the point on the curve representing @a val.
 *
 * @param edc calculation context for ECC operations
 * @param val (positive) value to encode into a point
 * @return representation of the value as an ECC point,
 *         must be freed using #GNUNET_CRYPTO_ecc_free()
 */
struct GNUNET_CRYPTO_EccPoint*
GNUNET_CRYPTO_ecc_dexp_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
                            const struct GNUNET_CRYPTO_EccScalar *val)
{
  // gcry_mpi_point_t g;
  // gcry_mpi_point_t r;
  struct GNUNET_CRYPTO_EccPoint *r = GNUNET_new (struct GNUNET_CRYPTO_EccPoint);
  GNUNET_assert (0 == crypto_scalarmult_ed25519_base_noclamp (r->v, val->v));
  return r;

  /*g = gcry_mpi_ec_get_point ("g", edc->ctx, 0);
  GNUNET_assert (NULL != g);
  r = gcry_mpi_point_new (0);
  gcry_mpi_ec_mul (r, val, g, edc->ctx);
  gcry_mpi_point_release (g);
  return r;*/
}


/**
 * Add two points on the elliptic curve.
 *
 * @param edc calculation context for ECC operations
 * @param a some value
 * @param b some value
 * @return @a a + @a b, must be freed using #GNUNET_CRYPTO_ecc_free()
 */
struct GNUNET_CRYPTO_EccPoint*
GNUNET_CRYPTO_ecc_add (struct GNUNET_CRYPTO_EccDlogContext *edc,
                       const struct GNUNET_CRYPTO_EccPoint *a,
                       const struct GNUNET_CRYPTO_EccPoint *b)
{
  // gcry_mpi_point_t r;
  struct GNUNET_CRYPTO_EccPoint *r = GNUNET_new (struct GNUNET_CRYPTO_EccPoint);
  crypto_core_ed25519_add (r->v, a->v, b->v);
  return r;
  /*r = gcry_mpi_point_new (0);
  gcry_mpi_ec_add (r, a, b, edc->ctx);
  return r;*/
}


/**
 * Multiply the point @a p on the elliptic curve by @a val.
 *
 * @param edc calculation context for ECC operations
 * @param p point to multiply
 * @param val (positive) value to encode into a point
 * @return representation of the value as an ECC point,
 *         must be freed using #GNUNET_CRYPTO_ecc_free()
 */
struct GNUNET_CRYPTO_EccPoint*
GNUNET_CRYPTO_ecc_pmul_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
                            const struct GNUNET_CRYPTO_EccPoint *p,
                            const struct GNUNET_CRYPTO_EccScalar *val)
{
  struct GNUNET_CRYPTO_EccPoint *r = GNUNET_new (struct GNUNET_CRYPTO_EccPoint);
  GNUNET_assert (0 ==
                 crypto_scalarmult_ed25519_noclamp (r->v, val->v, p->v));
  return r;
  /*gcry_mpi_point_t r;

  r = gcry_mpi_point_new (0);
  gcry_mpi_ec_mul (r, val, p, edc->ctx);
  return r;*/
}


/**
 * Obtain a random point on the curve and its
 * additive inverse. Both returned values
 * must be freed using #GNUNET_CRYPTO_ecc_free().
 *
 * @param edc calculation context for ECC operations
 * @param[out] r set to a random point on the curve
 * @param[out] r_inv set to the additive inverse of @a r
 */
void
GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccDlogContext *edc,
                       struct GNUNET_CRYPTO_EccPoint **r,
                       struct GNUNET_CRYPTO_EccPoint **r_inv)
{
  /*gcry_mpi_t fact;
  gcry_mpi_t n;
  gcry_mpi_point_t g;*/

  struct GNUNET_CRYPTO_EccScalar *s = GNUNET_CRYPTO_ecc_random_mod_n (edc);
  *r = GNUNET_new (struct GNUNET_CRYPTO_EccPoint);
  GNUNET_assert (0 ==
                 crypto_scalarmult_ed25519_base_noclamp ((*r)->v, s->v));
  *r_inv = GNUNET_new (struct GNUNET_CRYPTO_EccPoint);
  unsigned char inv_s[crypto_scalarmult_ed25519_SCALARBYTES];
  crypto_core_ed25519_scalar_negate (inv_s, s->v);
  GNUNET_assert (0 ==
                 crypto_scalarmult_ed25519_base_noclamp ((*r_inv)->v, inv_s));
  GNUNET_free (s);

  /* calculate 'r' */
  /*g = gcry_mpi_ec_get_point ("g", edc->ctx, 0);
  GNUNET_assert (NULL != g);
  *r = gcry_mpi_point_new (0);
  gcry_mpi_ec_mul (*r, fact, g, edc->ctx);
*/
  /* calculate 'r_inv' */
  // n = gcry_mpi_ec_get_mpi ("n", edc->ctx, 1);
  // gcry_mpi_sub (fact, n, fact);  /* fact = n - fact = - fact */
  // *r_inv = gcry_mpi_point_new (0);
  // gcry_mpi_ec_mul (*r_inv, fact, g, edc->ctx);

  // gcry_mpi_release (n);
  // gcry_mpi_release (fact);
  // gcry_mpi_point_release (g);
}


/**
 * Obtain a random scalar for point multiplication on the curve and
 * its multiplicative inverse.
 *
 * @param edc calculation context for ECC operations
 * @param[out] r set to a random scalar on the curve
 * @param[out] r_inv set to the multiplicative inverse of @a r
 */
void
GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccDlogContext *edc,
                           struct GNUNET_CRYPTO_EccScalar **r,
                           struct GNUNET_CRYPTO_EccScalar **r_inv)
{

  *r = GNUNET_CRYPTO_ecc_random_mod_n (edc);
  *r_inv = GNUNET_CRYPTO_ecc_random_mod_n (edc);
  crypto_core_ed25519_scalar_invert ((*r_inv)->v, (*r)->v);
}


/**
 * Free a point value returned by the API.
 *
 * @param p point to free
 */
void
GNUNET_CRYPTO_ecc_free (struct GNUNET_CRYPTO_EccPoint *p)
{
  GNUNET_free (p);
}

struct GNUNET_CRYPTO_EccScalar*
GNUNET_CRYPTO_ecc_scalar_from_int (int val)
{
  struct GNUNET_CRYPTO_EccScalar *ret;

  ret = GNUNET_new (struct GNUNET_CRYPTO_EccScalar);
  unsigned char fact[crypto_scalarmult_ed25519_SCALARBYTES];
  memset (fact, 0, sizeof (fact));

  if (val < 0)
  {
    for (int i = 0; i < -val; i++)
      sodium_increment (fact, sizeof (fact));
    crypto_core_ed25519_scalar_negate (ret->v, fact);
  }
  else
  {
    for (int i = 0; i < val; i++)
      sodium_increment (fact, sizeof (fact));
    crypto_scalarmult_ed25519_base_noclamp (ret->v, fact);
  }
  return ret;
}


/* end of crypto_ecc_dlog.c */