ed25519_tor.c 9.26 KB
Newer Older
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
/*
  Public domain by Andrew M. <liquidsun@gmail.com>

  Ed25519 reference implementation using Ed25519-donna
*/

/*
  Tor specific notes:

  This file is used by Tor instead of `ed25519.c` as the number of
  changes/additions is non-trivial.

  Tor modifications to `ed25519.c`:
   * 'Tab` -> '  '.
   * Include `ed25519_donna_tor.h` instead of `ed25519.h`.

   * The external interface has been reworked to match that provided
     by Tor's copy of the SUPERCOP `ref10` code.

   * The secret (aka private) key is now stored/used in expanded form.

   * The internal math tests from `test-internals.c` have been wrapped
     in a function and the entire file is included to allow for
     runtime validation.
 */


/* define ED25519_SUFFIX to have it appended to the end of each public function */
#if !defined(ED25519_SUFFIX)
#define ED25519_SUFFIX
#endif

#define ED25519_FN3(fn,suffix) fn##suffix
#define ED25519_FN2(fn,suffix) ED25519_FN3(fn,suffix)
#define ED25519_FN(fn)         ED25519_FN2(fn,ED25519_SUFFIX)

37
#include "orconfig.h"
38
39
40

#include "lib/cc/compat_compiler.h"

41
42
43
44
45
#include "ed25519-donna.h"
#include "ed25519_donna_tor.h"
#include "ed25519-randombytes.h"
#include "ed25519-hash.h"

46
#include "lib/crypt_ops/crypto_rand.h"
47
#include "lib/crypt_ops/crypto_util.h"
48

49
50
51
52
typedef unsigned char ed25519_signature[64];
typedef unsigned char ed25519_public_key[32];
typedef unsigned char ed25519_secret_key[32];

53
54
static void ed25519_donna_gettweak(unsigned char *out,
                                   const unsigned char *param);
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
static int ED25519_FN(ed25519_sign_open) (const unsigned char *m, size_t mlen,
  const ed25519_public_key pk, const ed25519_signature RS);


/*
  Generates a (extsk[0..31]) and aExt (extsk[32..63])
*/

DONNA_INLINE static void
ed25519_extsk(hash_512bits extsk, const ed25519_secret_key sk) {
  ed25519_hash(extsk, sk, 32);
  extsk[0] &= 248;
  extsk[31] &= 127;
  extsk[31] |= 64;
}

static void
ed25519_hram(hash_512bits hram, const ed25519_signature RS, const ed25519_public_key pk, const unsigned char *m, size_t mlen) {
  ed25519_hash_context ctx;
  ed25519_hash_init(&ctx);
  ed25519_hash_update(&ctx, RS, 32);
  ed25519_hash_update(&ctx, pk, 32);
  ed25519_hash_update(&ctx, m, mlen);
  ed25519_hash_final(&ctx, hram);
}

static int
ED25519_FN(ed25519_sign_open) (const unsigned char *m, size_t mlen, const ed25519_public_key pk, const ed25519_signature RS) {
  ge25519 ALIGN(16) R, A;
  hash_512bits hash;
  bignum256modm hram, S;
  unsigned char checkR[32];

  if ((RS[63] & 224) || !ge25519_unpack_negative_vartime(&A, pk))
    return -1;

  /* hram = H(R,A,m) */
  ed25519_hram(hash, RS, pk, m, mlen);
  expand256_modm(hram, hash, 64);

  /* S */
  expand256_modm(S, RS + 32, 32);

  /* SB - H(R,A,m)A */
  ge25519_double_scalarmult_vartime(&R, &A, hram, S);
  ge25519_pack(checkR, &R);

  /* check that R = SB - H(R,A,m)A */
  return ed25519_verify(RS, checkR, 32) ? 0 : -1;
}

#include "ed25519-donna-batchverify.h"

/*
  Fast Curve25519 basepoint scalar multiplication
*/

void
ED25519_FN(curved25519_scalarmult_basepoint) (curved25519_key pk, const curved25519_key e) {
  curved25519_key ec;
  bignum256modm s;
  bignum25519 ALIGN(16) yplusz, zminusy;
  ge25519 ALIGN(16) p;
  size_t i;

  /* clamp */
  for (i = 0; i < 32; i++) ec[i] = e[i];
  ec[0] &= 248;
  ec[31] &= 127;
  ec[31] |= 64;

  expand_raw256_modm(s, ec);

  /* scalar * basepoint */
  ge25519_scalarmult_base_niels(&p, ge25519_niels_base_multiples, s);

  /* u = (y + z) / (z - y) */
  curve25519_add(yplusz, p.y, p.z);
  curve25519_sub(zminusy, p.z, p.y);
  curve25519_recip(zminusy, zminusy);
  curve25519_mul(yplusz, yplusz, zminusy);
  curve25519_contract(pk, yplusz);
}

/*
141
   Tor has a specific idea of how an Ed25519 implementation should behave.
142
143
144
145
146
   Implement such a beast using the ed25519-donna primitives/internals.

    * Private key generation using Tor's CSPRNG.

    * Routines that deal with the private key now use the expanded form.
147
148

    * Support for multiplicative key blinding has been added.
149
150

    * Support for converting a Curve25519 key to an Ed25519 key has been added.
151
152
153
154
155
156
157
 */

int
ed25519_donna_seckey(unsigned char *sk)
{
  ed25519_secret_key seed;

158
  crypto_strongest_rand(seed, 32);
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

  ed25519_extsk(sk, seed);

  memwipe(seed, 0, sizeof(seed));

  return 0;
}

int
ed25519_donna_seckey_expand(unsigned char *sk, const unsigned char *skseed)
{
  ed25519_extsk(sk, skseed);

  return 0;
}

int
ed25519_donna_pubkey(unsigned char *pk, const unsigned char *sk)
{
178
179
  bignum256modm a = {0};
  ge25519 ALIGN(16) A = {{0}, {0}, {0}, {0}};
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

  /* A = aB */
  expand256_modm(a, sk, 32);
  ge25519_scalarmult_base_niels(&A, ge25519_niels_base_multiples, a);
  ge25519_pack(pk, &A);

  return 0;
}

int
ed25519_donna_keygen(unsigned char *pk, unsigned char *sk)
{
  int ok;
  ok = ed25519_donna_seckey(sk);
  ed25519_donna_pubkey(pk, sk);

  return ok;
}

int
ed25519_donna_open(const unsigned char *signature, const unsigned char *m,
  size_t mlen, const unsigned char *pk)
{
  /* Wrap the ed25519-donna routine, since it is also used by the batch
   * verification code.
   */
  return ED25519_FN(ed25519_sign_open)(m, mlen, pk, signature);
}

int
ed25519_donna_sign(unsigned char *sig, const unsigned char *m, size_t mlen,
  const unsigned char *sk, const unsigned char *pk)
{
  ed25519_hash_context ctx;
214
215
  bignum256modm r = {0}, S, a;
  ge25519 ALIGN(16) R = {{0}, {0}, {0}, {0}};
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
  hash_512bits hashr, hram;

  /* This is equivalent to the removed `ED25519_FN(ed25519_sign)` routine,
   * except that the key expansion step is omitted as sk already is in expanded
   * form.
   */

  /* r = H(aExt[32..64], m) */
  ed25519_hash_init(&ctx);
  ed25519_hash_update(&ctx, sk + 32, 32);
  ed25519_hash_update(&ctx, m, mlen);
  ed25519_hash_final(&ctx, hashr);
  expand256_modm(r, hashr, 64);

  /* R = rB */
  ge25519_scalarmult_base_niels(&R, ge25519_niels_base_multiples, r);
  ge25519_pack(sig, &R);

  /* S = H(R,A,m).. */
  ed25519_hram(hram, sig, pk, m, mlen);
  expand256_modm(S, hram, 64);

  /* S = H(R,A,m)a */
  expand256_modm(a, sk, 32);
  mul256_modm(S, S, a);

  /* S = (r + H(R,A,m)a) */
  add256_modm(S, S, r);

  /* S = (r + H(R,A,m)a) mod L */
  contract256_modm(sig + 32, S);

  return 0;
}

251
static void
252
ed25519_donna_gettweak(unsigned char *out, const unsigned char *param)
253
{
254
  memcpy(out, param, 32);
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269

  out[0] &= 248;  /* Is this necessary ? */
  out[31] &= 63;
  out[31] |= 64;
}

int
ed25519_donna_blind_secret_key(unsigned char *out, const unsigned char *inp,
  const unsigned char *param)
{
  static const char str[] = "Derive temporary signing key hash input";
  unsigned char tweak[64];
  ed25519_hash_context ctx;
  bignum256modm ALIGN(16) sk, t;

270
  ed25519_donna_gettweak(tweak, param);
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
  expand256_modm(t, tweak, 32);

  expand256_modm(sk, inp, 32);
  mul256_modm(sk, sk, t);
  contract256_modm(out, sk);

  ed25519_hash_init(&ctx);
  ed25519_hash_update(&ctx, (const unsigned char*)str, strlen(str));
  ed25519_hash_update(&ctx, inp + 32, 32);
  ed25519_hash_final(&ctx, tweak);

  memcpy(out + 32, tweak, 32);

  memwipe(sk, 0, sizeof(sk));
  memwipe(t, 0, sizeof(t));
  memwipe(tweak, 0, sizeof(tweak));

  return 0;
}

int
ed25519_donna_blind_public_key(unsigned char *out, const unsigned char *inp,
  const unsigned char *param)
{
  static const bignum256modm zero = { 0 };
  unsigned char tweak[64];
  unsigned char pkcopy[32];
  ge25519 ALIGN(16) A, Aprime;
  bignum256modm ALIGN(16) t;

301
  ed25519_donna_gettweak(tweak, param);
302
303
304
305
306
  expand256_modm(t, tweak, 32);

  /* No "ge25519_unpack", negate the public key. */
  memcpy(pkcopy, inp, 32);
  pkcopy[31] ^= (1<<7);
307
308
309
  if (!ge25519_unpack_negative_vartime(&A, pkcopy)) {
    return -1;
  }
310
311
312
313
314
315
316
317
318
319
320
321
322
323

  /* A' = [tweak] * A + [0] * basepoint. */
  ge25519_double_scalarmult_vartime(&Aprime, &A, t, zero);
  ge25519_pack(out, &Aprime);

  memwipe(tweak, 0, sizeof(tweak));
  memwipe(pkcopy, 0, sizeof(pkcopy));
  memwipe(&A, 0, sizeof(A));
  memwipe(&Aprime, 0, sizeof(Aprime));
  memwipe(t, 0, sizeof(t));

  return 0;
}

324
325
326
327
int
ed25519_donna_pubkey_from_curve25519_pubkey(unsigned char *out,
  const unsigned char *inp, int signbit)
{
328
  static const bignum25519 ALIGN(16) one = { 1 };
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  bignum25519 ALIGN(16) u, uminus1, uplus1, inv_uplus1, y;

  /* Prop228: y = (u-1)/(u+1) */
  curve25519_expand(u, inp);
  curve25519_sub(uminus1, u, one);
  curve25519_add(uplus1, u, one);
  curve25519_recip(inv_uplus1, uplus1);
  curve25519_mul(y, uminus1, inv_uplus1);
  curve25519_contract(out, y);

  /* Propagate sign. */
  out[31] |= (!!signbit) << 7;

  return 0;
}

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
/* Do the scalar multiplication of <b>pubkey</b> with the group order
 * <b>modm_m</b>.  Place the result in <b>out</b> which must be at least 32
 * bytes long. */
int
ed25519_donna_scalarmult_with_group_order(unsigned char *out,
                                          const unsigned char *pubkey)
{
  static const bignum256modm ALIGN(16) zero = { 0 };
  unsigned char pkcopy[32];
  ge25519 ALIGN(16) Point, Result;

  /* No "ge25519_unpack", negate the public key and unpack it back.
   * See ed25519_donna_blind_public_key() */
  memcpy(pkcopy, pubkey, 32);
  pkcopy[31] ^= (1<<7);
  if (!ge25519_unpack_negative_vartime(&Point, pkcopy)) {
    return -1; /* error: bail out */
  }

  /* There is no regular scalarmult function so we have to do:
   * Result = l*P + 0*B */
  ge25519_double_scalarmult_vartime(&Result, &Point, modm_m, zero);
  ge25519_pack(out, &Result);

  return 0;
}

372
#include "test-internals.c"