Coverage Report

Created: 2026-09-27 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open5gs/lib/crypt/curve25519-donna.c
Line
Count
Source
1
/* Copyright 2008, Google Inc.
2
 * All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions are
6
 * met:
7
 *
8
 *     * Redistributions of source code must retain the above copyright
9
 * notice, this list of conditions and the following disclaimer.
10
 *     * Redistributions in binary form must reproduce the above
11
 * copyright notice, this list of conditions and the following disclaimer
12
 * in the documentation and/or other materials provided with the
13
 * distribution.
14
 *     * Neither the name of Google Inc. nor the names of its
15
 * contributors may be used to endorse or promote products derived from
16
 * this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * curve25519-donna: Curve25519 elliptic curve, public key function
31
 *
32
 * http://code.google.com/p/curve25519-donna/
33
 *
34
 * Adam Langley <agl@imperialviolet.org>
35
 *
36
 * Derived from public domain C code by Daniel J. Bernstein <djb@cr.yp.to>
37
 *
38
 * More information about curve25519 can be found here
39
 *   http://cr.yp.to/ecdh.html
40
 *
41
 * djb's sample implementation of curve25519 is written in a special assembly
42
 * language called qhasm and uses the floating point registers.
43
 *
44
 * This is, almost, a clean room reimplementation from the curve25519 paper. It
45
 * uses many of the tricks described therein. Only the crecip function is taken
46
 * from the sample implementation. */
47
48
#if 0 /* modified by acetcom */
49
#include <string.h>
50
#include <stdint.h>
51
52
#ifdef _MSC_VER
53
#define inline __inline
54
#endif
55
#else
56
#include "ogs-crypt.h"
57
#endif
58
59
typedef uint8_t u8;
60
typedef int32_t s32;
61
typedef int64_t limb;
62
63
/* Field element representation:
64
 *
65
 * Field elements are written as an array of signed, 64-bit limbs, least
66
 * significant first. The value of the field element is:
67
 *   x[0] + 2^26·x[1] + x^51·x[2] + 2^102·x[3] + ...
68
 *
69
 * i.e. the limbs are 26, 25, 26, 25, ... bits wide. */
70
71
/* Sum two numbers: output += in */
72
0
static void fsum(limb *output, const limb *in) {
73
0
  unsigned i;
74
0
  for (i = 0; i < 10; i += 2) {
75
0
    output[0+i] = output[0+i] + in[0+i];
76
0
    output[1+i] = output[1+i] + in[1+i];
77
0
  }
78
0
}
79
80
/* Find the difference of two numbers: output = in - output
81
 * (note the order of the arguments!). */
82
0
static void fdifference(limb *output, const limb *in) {
83
0
  unsigned i;
84
0
  for (i = 0; i < 10; ++i) {
85
0
    output[i] = in[i] - output[i];
86
0
  }
87
0
}
88
89
/* Multiply a number by a scalar: output = in * scalar */
90
0
static void fscalar_product(limb *output, const limb *in, const limb scalar) {
91
0
  unsigned i;
92
0
  for (i = 0; i < 10; ++i) {
93
0
    output[i] = in[i] * scalar;
94
0
  }
95
0
}
96
97
/* Multiply two numbers: output = in2 * in
98
 *
99
 * output must be distinct to both inputs. The inputs are reduced coefficient
100
 * form, the output is not.
101
 *
102
 * output[x] <= 14 * the largest product of the input limbs. */
103
0
static void fproduct(limb *output, const limb *in2, const limb *in) {
104
0
  output[0] =       ((limb) ((s32) in2[0])) * ((s32) in[0]);
105
0
  output[1] =       ((limb) ((s32) in2[0])) * ((s32) in[1]) +
106
0
                    ((limb) ((s32) in2[1])) * ((s32) in[0]);
107
0
  output[2] =  2 *  ((limb) ((s32) in2[1])) * ((s32) in[1]) +
108
0
                    ((limb) ((s32) in2[0])) * ((s32) in[2]) +
109
0
                    ((limb) ((s32) in2[2])) * ((s32) in[0]);
110
0
  output[3] =       ((limb) ((s32) in2[1])) * ((s32) in[2]) +
111
0
                    ((limb) ((s32) in2[2])) * ((s32) in[1]) +
112
0
                    ((limb) ((s32) in2[0])) * ((s32) in[3]) +
113
0
                    ((limb) ((s32) in2[3])) * ((s32) in[0]);
114
0
  output[4] =       ((limb) ((s32) in2[2])) * ((s32) in[2]) +
115
0
               2 * (((limb) ((s32) in2[1])) * ((s32) in[3]) +
116
0
                    ((limb) ((s32) in2[3])) * ((s32) in[1])) +
117
0
                    ((limb) ((s32) in2[0])) * ((s32) in[4]) +
118
0
                    ((limb) ((s32) in2[4])) * ((s32) in[0]);
119
0
  output[5] =       ((limb) ((s32) in2[2])) * ((s32) in[3]) +
120
0
                    ((limb) ((s32) in2[3])) * ((s32) in[2]) +
121
0
                    ((limb) ((s32) in2[1])) * ((s32) in[4]) +
122
0
                    ((limb) ((s32) in2[4])) * ((s32) in[1]) +
123
0
                    ((limb) ((s32) in2[0])) * ((s32) in[5]) +
124
0
                    ((limb) ((s32) in2[5])) * ((s32) in[0]);
125
0
  output[6] =  2 * (((limb) ((s32) in2[3])) * ((s32) in[3]) +
126
0
                    ((limb) ((s32) in2[1])) * ((s32) in[5]) +
127
0
                    ((limb) ((s32) in2[5])) * ((s32) in[1])) +
128
0
                    ((limb) ((s32) in2[2])) * ((s32) in[4]) +
129
0
                    ((limb) ((s32) in2[4])) * ((s32) in[2]) +
130
0
                    ((limb) ((s32) in2[0])) * ((s32) in[6]) +
131
0
                    ((limb) ((s32) in2[6])) * ((s32) in[0]);
132
0
  output[7] =       ((limb) ((s32) in2[3])) * ((s32) in[4]) +
133
0
                    ((limb) ((s32) in2[4])) * ((s32) in[3]) +
134
0
                    ((limb) ((s32) in2[2])) * ((s32) in[5]) +
135
0
                    ((limb) ((s32) in2[5])) * ((s32) in[2]) +
136
0
                    ((limb) ((s32) in2[1])) * ((s32) in[6]) +
137
0
                    ((limb) ((s32) in2[6])) * ((s32) in[1]) +
138
0
                    ((limb) ((s32) in2[0])) * ((s32) in[7]) +
139
0
                    ((limb) ((s32) in2[7])) * ((s32) in[0]);
140
0
  output[8] =       ((limb) ((s32) in2[4])) * ((s32) in[4]) +
141
0
               2 * (((limb) ((s32) in2[3])) * ((s32) in[5]) +
142
0
                    ((limb) ((s32) in2[5])) * ((s32) in[3]) +
143
0
                    ((limb) ((s32) in2[1])) * ((s32) in[7]) +
144
0
                    ((limb) ((s32) in2[7])) * ((s32) in[1])) +
145
0
                    ((limb) ((s32) in2[2])) * ((s32) in[6]) +
146
0
                    ((limb) ((s32) in2[6])) * ((s32) in[2]) +
147
0
                    ((limb) ((s32) in2[0])) * ((s32) in[8]) +
148
0
                    ((limb) ((s32) in2[8])) * ((s32) in[0]);
149
0
  output[9] =       ((limb) ((s32) in2[4])) * ((s32) in[5]) +
150
0
                    ((limb) ((s32) in2[5])) * ((s32) in[4]) +
151
0
                    ((limb) ((s32) in2[3])) * ((s32) in[6]) +
152
0
                    ((limb) ((s32) in2[6])) * ((s32) in[3]) +
153
0
                    ((limb) ((s32) in2[2])) * ((s32) in[7]) +
154
0
                    ((limb) ((s32) in2[7])) * ((s32) in[2]) +
155
0
                    ((limb) ((s32) in2[1])) * ((s32) in[8]) +
156
0
                    ((limb) ((s32) in2[8])) * ((s32) in[1]) +
157
0
                    ((limb) ((s32) in2[0])) * ((s32) in[9]) +
158
0
                    ((limb) ((s32) in2[9])) * ((s32) in[0]);
159
0
  output[10] = 2 * (((limb) ((s32) in2[5])) * ((s32) in[5]) +
160
0
                    ((limb) ((s32) in2[3])) * ((s32) in[7]) +
161
0
                    ((limb) ((s32) in2[7])) * ((s32) in[3]) +
162
0
                    ((limb) ((s32) in2[1])) * ((s32) in[9]) +
163
0
                    ((limb) ((s32) in2[9])) * ((s32) in[1])) +
164
0
                    ((limb) ((s32) in2[4])) * ((s32) in[6]) +
165
0
                    ((limb) ((s32) in2[6])) * ((s32) in[4]) +
166
0
                    ((limb) ((s32) in2[2])) * ((s32) in[8]) +
167
0
                    ((limb) ((s32) in2[8])) * ((s32) in[2]);
168
0
  output[11] =      ((limb) ((s32) in2[5])) * ((s32) in[6]) +
169
0
                    ((limb) ((s32) in2[6])) * ((s32) in[5]) +
170
0
                    ((limb) ((s32) in2[4])) * ((s32) in[7]) +
171
0
                    ((limb) ((s32) in2[7])) * ((s32) in[4]) +
172
0
                    ((limb) ((s32) in2[3])) * ((s32) in[8]) +
173
0
                    ((limb) ((s32) in2[8])) * ((s32) in[3]) +
174
0
                    ((limb) ((s32) in2[2])) * ((s32) in[9]) +
175
0
                    ((limb) ((s32) in2[9])) * ((s32) in[2]);
176
0
  output[12] =      ((limb) ((s32) in2[6])) * ((s32) in[6]) +
177
0
               2 * (((limb) ((s32) in2[5])) * ((s32) in[7]) +
178
0
                    ((limb) ((s32) in2[7])) * ((s32) in[5]) +
179
0
                    ((limb) ((s32) in2[3])) * ((s32) in[9]) +
180
0
                    ((limb) ((s32) in2[9])) * ((s32) in[3])) +
181
0
                    ((limb) ((s32) in2[4])) * ((s32) in[8]) +
182
0
                    ((limb) ((s32) in2[8])) * ((s32) in[4]);
183
0
  output[13] =      ((limb) ((s32) in2[6])) * ((s32) in[7]) +
184
0
                    ((limb) ((s32) in2[7])) * ((s32) in[6]) +
185
0
                    ((limb) ((s32) in2[5])) * ((s32) in[8]) +
186
0
                    ((limb) ((s32) in2[8])) * ((s32) in[5]) +
187
0
                    ((limb) ((s32) in2[4])) * ((s32) in[9]) +
188
0
                    ((limb) ((s32) in2[9])) * ((s32) in[4]);
189
0
  output[14] = 2 * (((limb) ((s32) in2[7])) * ((s32) in[7]) +
190
0
                    ((limb) ((s32) in2[5])) * ((s32) in[9]) +
191
0
                    ((limb) ((s32) in2[9])) * ((s32) in[5])) +
192
0
                    ((limb) ((s32) in2[6])) * ((s32) in[8]) +
193
0
                    ((limb) ((s32) in2[8])) * ((s32) in[6]);
194
0
  output[15] =      ((limb) ((s32) in2[7])) * ((s32) in[8]) +
195
0
                    ((limb) ((s32) in2[8])) * ((s32) in[7]) +
196
0
                    ((limb) ((s32) in2[6])) * ((s32) in[9]) +
197
0
                    ((limb) ((s32) in2[9])) * ((s32) in[6]);
198
0
  output[16] =      ((limb) ((s32) in2[8])) * ((s32) in[8]) +
199
0
               2 * (((limb) ((s32) in2[7])) * ((s32) in[9]) +
200
0
                    ((limb) ((s32) in2[9])) * ((s32) in[7]));
201
0
  output[17] =      ((limb) ((s32) in2[8])) * ((s32) in[9]) +
202
0
                    ((limb) ((s32) in2[9])) * ((s32) in[8]);
203
0
  output[18] = 2 *  ((limb) ((s32) in2[9])) * ((s32) in[9]);
204
0
}
205
206
/* Reduce a long form to a short form by taking the input mod 2^255 - 19.
207
 *
208
 * On entry: |output[i]| < 14*2^54
209
 * On exit: |output[0..8]| < 280*2^54 */
210
0
static void freduce_degree(limb *output) {
211
  /* Each of these shifts and adds ends up multiplying the value by 19.
212
   *
213
   * For output[0..8], the absolute entry value is < 14*2^54 and we add, at
214
   * most, 19*14*2^54 thus, on exit, |output[0..8]| < 280*2^54. */
215
0
  output[8] += output[18] << 4;
216
0
  output[8] += output[18] << 1;
217
0
  output[8] += output[18];
218
0
  output[7] += output[17] << 4;
219
0
  output[7] += output[17] << 1;
220
0
  output[7] += output[17];
221
0
  output[6] += output[16] << 4;
222
0
  output[6] += output[16] << 1;
223
0
  output[6] += output[16];
224
0
  output[5] += output[15] << 4;
225
0
  output[5] += output[15] << 1;
226
0
  output[5] += output[15];
227
0
  output[4] += output[14] << 4;
228
0
  output[4] += output[14] << 1;
229
0
  output[4] += output[14];
230
0
  output[3] += output[13] << 4;
231
0
  output[3] += output[13] << 1;
232
0
  output[3] += output[13];
233
0
  output[2] += output[12] << 4;
234
0
  output[2] += output[12] << 1;
235
0
  output[2] += output[12];
236
0
  output[1] += output[11] << 4;
237
0
  output[1] += output[11] << 1;
238
0
  output[1] += output[11];
239
0
  output[0] += output[10] << 4;
240
0
  output[0] += output[10] << 1;
241
0
  output[0] += output[10];
242
0
}
243
244
#if (-1 & 3) != 3
245
#error "This code only works on a two's complement system"
246
#endif
247
248
/* return v / 2^26, using only shifts and adds.
249
 *
250
 * On entry: v can take any value. */
251
static inline limb
252
div_by_2_26(const limb v)
253
0
{
254
  /* High word of v; no shift needed. */
255
0
  const uint32_t highword = (uint32_t) (((uint64_t) v) >> 32);
256
  /* Set to all 1s if v was negative; else set to 0s. */
257
0
  const int32_t sign = ((int32_t) highword) >> 31;
258
  /* Set to 0x3ffffff if v was negative; else set to 0. */
259
0
  const int32_t roundoff = ((uint32_t) sign) >> 6;
260
  /* Should return v / (1<<26) */
261
0
  return (v + roundoff) >> 26;
262
0
}
263
264
/* return v / (2^25), using only shifts and adds.
265
 *
266
 * On entry: v can take any value. */
267
static inline limb
268
div_by_2_25(const limb v)
269
0
{
270
  /* High word of v; no shift needed*/
271
0
  const uint32_t highword = (uint32_t) (((uint64_t) v) >> 32);
272
  /* Set to all 1s if v was negative; else set to 0s. */
273
0
  const int32_t sign = ((int32_t) highword) >> 31;
274
  /* Set to 0x1ffffff if v was negative; else set to 0. */
275
0
  const int32_t roundoff = ((uint32_t) sign) >> 7;
276
  /* Should return v / (1<<25) */
277
0
  return (v + roundoff) >> 25;
278
0
}
279
280
/* Reduce all coefficients of the short form input so that |x| < 2^26.
281
 *
282
 * On entry: |output[i]| < 280*2^54 */
283
0
static void freduce_coefficients(limb *output) {
284
0
  unsigned i;
285
286
0
  output[10] = 0;
287
288
0
  for (i = 0; i < 10; i += 2) {
289
0
    limb over = div_by_2_26(output[i]);
290
    /* The entry condition (that |output[i]| < 280*2^54) means that over is, at
291
     * most, 280*2^28 in the first iteration of this loop. This is added to the
292
     * next limb and we can approximate the resulting bound of that limb by
293
     * 281*2^54. */
294
0
    output[i] -= over << 26;
295
0
    output[i+1] += over;
296
297
    /* For the first iteration, |output[i+1]| < 281*2^54, thus |over| <
298
     * 281*2^29. When this is added to the next limb, the resulting bound can
299
     * be approximated as 281*2^54.
300
     *
301
     * For subsequent iterations of the loop, 281*2^54 remains a conservative
302
     * bound and no overflow occurs. */
303
0
    over = div_by_2_25(output[i+1]);
304
0
    output[i+1] -= over << 25;
305
0
    output[i+2] += over;
306
0
  }
307
  /* Now |output[10]| < 281*2^29 and all other coefficients are reduced. */
308
0
  output[0] += output[10] << 4;
309
0
  output[0] += output[10] << 1;
310
0
  output[0] += output[10];
311
312
0
  output[10] = 0;
313
314
  /* Now output[1..9] are reduced, and |output[0]| < 2^26 + 19*281*2^29
315
   * So |over| will be no more than 2^16. */
316
0
  {
317
0
    limb over = div_by_2_26(output[0]);
318
0
    output[0] -= over << 26;
319
0
    output[1] += over;
320
0
  }
321
322
  /* Now output[0,2..9] are reduced, and |output[1]| < 2^25 + 2^16 < 2^26. The
323
   * bound on |output[1]| is sufficient to meet our needs. */
324
0
}
325
326
/* A helpful wrapper around fproduct: output = in * in2.
327
 *
328
 * On entry: |in[i]| < 2^27 and |in2[i]| < 2^27.
329
 *
330
 * output must be distinct to both inputs. The output is reduced degree
331
 * (indeed, one need only provide storage for 10 limbs) and |output[i]| < 2^26. */
332
static void
333
0
fmul(limb *output, const limb *in, const limb *in2) {
334
0
  limb t[19];
335
0
  fproduct(t, in, in2);
336
  /* |t[i]| < 14*2^54 */
337
0
  freduce_degree(t);
338
0
  freduce_coefficients(t);
339
  /* |t[i]| < 2^26 */
340
0
  memcpy(output, t, sizeof(limb) * 10);
341
0
}
342
343
/* Square a number: output = in**2
344
 *
345
 * output must be distinct from the input. The inputs are reduced coefficient
346
 * form, the output is not.
347
 *
348
 * output[x] <= 14 * the largest product of the input limbs. */
349
0
static void fsquare_inner(limb *output, const limb *in) {
350
0
  output[0] =       ((limb) ((s32) in[0])) * ((s32) in[0]);
351
0
  output[1] =  2 *  ((limb) ((s32) in[0])) * ((s32) in[1]);
352
0
  output[2] =  2 * (((limb) ((s32) in[1])) * ((s32) in[1]) +
353
0
                    ((limb) ((s32) in[0])) * ((s32) in[2]));
354
0
  output[3] =  2 * (((limb) ((s32) in[1])) * ((s32) in[2]) +
355
0
                    ((limb) ((s32) in[0])) * ((s32) in[3]));
356
0
  output[4] =       ((limb) ((s32) in[2])) * ((s32) in[2]) +
357
0
               4 *  ((limb) ((s32) in[1])) * ((s32) in[3]) +
358
0
               2 *  ((limb) ((s32) in[0])) * ((s32) in[4]);
359
0
  output[5] =  2 * (((limb) ((s32) in[2])) * ((s32) in[3]) +
360
0
                    ((limb) ((s32) in[1])) * ((s32) in[4]) +
361
0
                    ((limb) ((s32) in[0])) * ((s32) in[5]));
362
0
  output[6] =  2 * (((limb) ((s32) in[3])) * ((s32) in[3]) +
363
0
                    ((limb) ((s32) in[2])) * ((s32) in[4]) +
364
0
                    ((limb) ((s32) in[0])) * ((s32) in[6]) +
365
0
               2 *  ((limb) ((s32) in[1])) * ((s32) in[5]));
366
0
  output[7] =  2 * (((limb) ((s32) in[3])) * ((s32) in[4]) +
367
0
                    ((limb) ((s32) in[2])) * ((s32) in[5]) +
368
0
                    ((limb) ((s32) in[1])) * ((s32) in[6]) +
369
0
                    ((limb) ((s32) in[0])) * ((s32) in[7]));
370
0
  output[8] =       ((limb) ((s32) in[4])) * ((s32) in[4]) +
371
0
               2 * (((limb) ((s32) in[2])) * ((s32) in[6]) +
372
0
                    ((limb) ((s32) in[0])) * ((s32) in[8]) +
373
0
               2 * (((limb) ((s32) in[1])) * ((s32) in[7]) +
374
0
                    ((limb) ((s32) in[3])) * ((s32) in[5])));
375
0
  output[9] =  2 * (((limb) ((s32) in[4])) * ((s32) in[5]) +
376
0
                    ((limb) ((s32) in[3])) * ((s32) in[6]) +
377
0
                    ((limb) ((s32) in[2])) * ((s32) in[7]) +
378
0
                    ((limb) ((s32) in[1])) * ((s32) in[8]) +
379
0
                    ((limb) ((s32) in[0])) * ((s32) in[9]));
380
0
  output[10] = 2 * (((limb) ((s32) in[5])) * ((s32) in[5]) +
381
0
                    ((limb) ((s32) in[4])) * ((s32) in[6]) +
382
0
                    ((limb) ((s32) in[2])) * ((s32) in[8]) +
383
0
               2 * (((limb) ((s32) in[3])) * ((s32) in[7]) +
384
0
                    ((limb) ((s32) in[1])) * ((s32) in[9])));
385
0
  output[11] = 2 * (((limb) ((s32) in[5])) * ((s32) in[6]) +
386
0
                    ((limb) ((s32) in[4])) * ((s32) in[7]) +
387
0
                    ((limb) ((s32) in[3])) * ((s32) in[8]) +
388
0
                    ((limb) ((s32) in[2])) * ((s32) in[9]));
389
0
  output[12] =      ((limb) ((s32) in[6])) * ((s32) in[6]) +
390
0
               2 * (((limb) ((s32) in[4])) * ((s32) in[8]) +
391
0
               2 * (((limb) ((s32) in[5])) * ((s32) in[7]) +
392
0
                    ((limb) ((s32) in[3])) * ((s32) in[9])));
393
0
  output[13] = 2 * (((limb) ((s32) in[6])) * ((s32) in[7]) +
394
0
                    ((limb) ((s32) in[5])) * ((s32) in[8]) +
395
0
                    ((limb) ((s32) in[4])) * ((s32) in[9]));
396
0
  output[14] = 2 * (((limb) ((s32) in[7])) * ((s32) in[7]) +
397
0
                    ((limb) ((s32) in[6])) * ((s32) in[8]) +
398
0
               2 *  ((limb) ((s32) in[5])) * ((s32) in[9]));
399
0
  output[15] = 2 * (((limb) ((s32) in[7])) * ((s32) in[8]) +
400
0
                    ((limb) ((s32) in[6])) * ((s32) in[9]));
401
0
  output[16] =      ((limb) ((s32) in[8])) * ((s32) in[8]) +
402
0
               4 *  ((limb) ((s32) in[7])) * ((s32) in[9]);
403
0
  output[17] = 2 *  ((limb) ((s32) in[8])) * ((s32) in[9]);
404
0
  output[18] = 2 *  ((limb) ((s32) in[9])) * ((s32) in[9]);
405
0
}
406
407
/* fsquare sets output = in^2.
408
 *
409
 * On entry: The |in| argument is in reduced coefficients form and |in[i]| <
410
 * 2^27.
411
 *
412
 * On exit: The |output| argument is in reduced coefficients form (indeed, one
413
 * need only provide storage for 10 limbs) and |out[i]| < 2^26. */
414
static void
415
0
fsquare(limb *output, const limb *in) {
416
0
  limb t[19];
417
0
  fsquare_inner(t, in);
418
  /* |t[i]| < 14*2^54 because the largest product of two limbs will be <
419
   * 2^(27+27) and fsquare_inner adds together, at most, 14 of those
420
   * products. */
421
0
  freduce_degree(t);
422
0
  freduce_coefficients(t);
423
  /* |t[i]| < 2^26 */
424
0
  memcpy(output, t, sizeof(limb) * 10);
425
0
}
426
427
/* Take a little-endian, 32-byte number and expand it into polynomial form */
428
static void
429
0
fexpand(limb *output, const u8 *input) {
430
0
#define F(n,start,shift,mask) \
431
0
  output[n] = ((((limb) input[start + 0]) | \
432
0
                ((limb) input[start + 1]) << 8 | \
433
0
                ((limb) input[start + 2]) << 16 | \
434
0
                ((limb) input[start + 3]) << 24) >> shift) & mask;
435
0
  F(0, 0, 0, 0x3ffffff);
436
0
  F(1, 3, 2, 0x1ffffff);
437
0
  F(2, 6, 3, 0x3ffffff);
438
0
  F(3, 9, 5, 0x1ffffff);
439
0
  F(4, 12, 6, 0x3ffffff);
440
0
  F(5, 16, 0, 0x1ffffff);
441
0
  F(6, 19, 1, 0x3ffffff);
442
0
  F(7, 22, 3, 0x1ffffff);
443
0
  F(8, 25, 4, 0x3ffffff);
444
0
  F(9, 28, 6, 0x1ffffff);
445
0
#undef F
446
0
}
447
448
#if (-32 >> 1) != -16
449
#error "This code only works when >> does sign-extension on negative numbers"
450
#endif
451
452
/* s32_eq returns 0xffffffff iff a == b and zero otherwise. */
453
0
static s32 s32_eq(s32 a, s32 b) {
454
0
  a = ~(a ^ b);
455
0
  a &= a << 16;
456
0
  a &= a << 8;
457
0
  a &= a << 4;
458
0
  a &= a << 2;
459
0
  a &= a << 1;
460
0
  return a >> 31;
461
0
}
462
463
/* s32_gte returns 0xffffffff if a >= b and zero otherwise, where a and b are
464
 * both non-negative. */
465
0
static s32 s32_gte(s32 a, s32 b) {
466
0
  a -= b;
467
  /* a >= 0 iff a >= b. */
468
0
  return ~(a >> 31);
469
0
}
470
471
/* Take a fully reduced polynomial form number and contract it into a
472
 * little-endian, 32-byte array.
473
 *
474
 * On entry: |input_limbs[i]| < 2^26 */
475
static void
476
0
fcontract(u8 *output, limb *input_limbs) {
477
0
  int i;
478
0
  int j;
479
0
  s32 input[10];
480
0
  s32 mask;
481
482
  /* |input_limbs[i]| < 2^26, so it's valid to convert to an s32. */
483
0
  for (i = 0; i < 10; i++) {
484
0
    input[i] = input_limbs[i];
485
0
  }
486
487
0
  for (j = 0; j < 2; ++j) {
488
0
    for (i = 0; i < 9; ++i) {
489
0
      if ((i & 1) == 1) {
490
        /* This calculation is a time-invariant way to make input[i]
491
         * non-negative by borrowing from the next-larger limb. */
492
0
        const s32 mask = input[i] >> 31;
493
0
        const s32 carry = -((input[i] & mask) >> 25);
494
0
        input[i] = input[i] + (carry << 25);
495
0
        input[i+1] = input[i+1] - carry;
496
0
      } else {
497
0
        const s32 mask = input[i] >> 31;
498
0
        const s32 carry = -((input[i] & mask) >> 26);
499
0
        input[i] = input[i] + (carry << 26);
500
0
        input[i+1] = input[i+1] - carry;
501
0
      }
502
0
    }
503
504
    /* There's no greater limb for input[9] to borrow from, but we can multiply
505
     * by 19 and borrow from input[0], which is valid mod 2^255-19. */
506
0
    {
507
0
      const s32 mask = input[9] >> 31;
508
0
      const s32 carry = -((input[9] & mask) >> 25);
509
0
      input[9] = input[9] + (carry << 25);
510
0
      input[0] = input[0] - (carry * 19);
511
0
    }
512
513
    /* After the first iteration, input[1..9] are non-negative and fit within
514
     * 25 or 26 bits, depending on position. However, input[0] may be
515
     * negative. */
516
0
  }
517
518
  /* The first borrow-propagation pass above ended with every limb
519
     except (possibly) input[0] non-negative.
520
521
     If input[0] was negative after the first pass, then it was because of a
522
     carry from input[9]. On entry, input[9] < 2^26 so the carry was, at most,
523
     one, since (2**26-1) >> 25 = 1. Thus input[0] >= -19.
524
525
     In the second pass, each limb is decreased by at most one. Thus the second
526
     borrow-propagation pass could only have wrapped around to decrease
527
     input[0] again if the first pass left input[0] negative *and* input[1]
528
     through input[9] were all zero.  In that case, input[1] is now 2^25 - 1,
529
     and this last borrow-propagation step will leave input[1] non-negative. */
530
0
  {
531
0
    const s32 mask = input[0] >> 31;
532
0
    const s32 carry = -((input[0] & mask) >> 26);
533
0
    input[0] = input[0] + (carry << 26);
534
0
    input[1] = input[1] - carry;
535
0
  }
536
537
  /* All input[i] are now non-negative. However, there might be values between
538
   * 2^25 and 2^26 in a limb which is, nominally, 25 bits wide. */
539
0
  for (j = 0; j < 2; j++) {
540
0
    for (i = 0; i < 9; i++) {
541
0
      if ((i & 1) == 1) {
542
0
        const s32 carry = input[i] >> 25;
543
0
        input[i] &= 0x1ffffff;
544
0
        input[i+1] += carry;
545
0
      } else {
546
0
        const s32 carry = input[i] >> 26;
547
0
        input[i] &= 0x3ffffff;
548
0
        input[i+1] += carry;
549
0
      }
550
0
    }
551
552
0
    {
553
0
      const s32 carry = input[9] >> 25;
554
0
      input[9] &= 0x1ffffff;
555
0
      input[0] += 19*carry;
556
0
    }
557
0
  }
558
559
  /* If the first carry-chain pass, just above, ended up with a carry from
560
   * input[9], and that caused input[0] to be out-of-bounds, then input[0] was
561
   * < 2^26 + 2*19, because the carry was, at most, two.
562
   *
563
   * If the second pass carried from input[9] again then input[0] is < 2*19 and
564
   * the input[9] -> input[0] carry didn't push input[0] out of bounds. */
565
566
  /* It still remains the case that input might be between 2^255-19 and 2^255.
567
   * In this case, input[1..9] must take their maximum value and input[0] must
568
   * be >= (2^255-19) & 0x3ffffff, which is 0x3ffffed. */
569
0
  mask = s32_gte(input[0], 0x3ffffed);
570
0
  for (i = 1; i < 10; i++) {
571
0
    if ((i & 1) == 1) {
572
0
      mask &= s32_eq(input[i], 0x1ffffff);
573
0
    } else {
574
0
      mask &= s32_eq(input[i], 0x3ffffff);
575
0
    }
576
0
  }
577
578
  /* mask is either 0xffffffff (if input >= 2^255-19) and zero otherwise. Thus
579
   * this conditionally subtracts 2^255-19. */
580
0
  input[0] -= mask & 0x3ffffed;
581
582
0
  for (i = 1; i < 10; i++) {
583
0
    if ((i & 1) == 1) {
584
0
      input[i] -= mask & 0x1ffffff;
585
0
    } else {
586
0
      input[i] -= mask & 0x3ffffff;
587
0
    }
588
0
  }
589
590
0
  input[1] <<= 2;
591
0
  input[2] <<= 3;
592
0
  input[3] <<= 5;
593
0
  input[4] <<= 6;
594
0
  input[6] <<= 1;
595
0
  input[7] <<= 3;
596
0
  input[8] <<= 4;
597
0
  input[9] <<= 6;
598
0
#define F(i, s) \
599
0
  output[s+0] |=  input[i] & 0xff; \
600
0
  output[s+1]  = (input[i] >> 8) & 0xff; \
601
0
  output[s+2]  = (input[i] >> 16) & 0xff; \
602
0
  output[s+3]  = (input[i] >> 24) & 0xff;
603
0
  output[0] = 0;
604
0
  output[16] = 0;
605
0
  F(0,0);
606
0
  F(1,3);
607
0
  F(2,6);
608
0
  F(3,9);
609
0
  F(4,12);
610
0
  F(5,16);
611
0
  F(6,19);
612
0
  F(7,22);
613
0
  F(8,25);
614
0
  F(9,28);
615
0
#undef F
616
0
}
617
618
/* Input: Q, Q', Q-Q'
619
 * Output: 2Q, Q+Q'
620
 *
621
 *   x2 z3: long form
622
 *   x3 z3: long form
623
 *   x z: short form, destroyed
624
 *   xprime zprime: short form, destroyed
625
 *   qmqp: short form, preserved
626
 *
627
 * On entry and exit, the absolute value of the limbs of all inputs and outputs
628
 * are < 2^26. */
629
static void fmonty(limb *x2, limb *z2,  /* output 2Q */
630
                   limb *x3, limb *z3,  /* output Q + Q' */
631
                   limb *x, limb *z,    /* input Q */
632
                   limb *xprime, limb *zprime,  /* input Q' */
633
0
                   const limb *qmqp /* input Q - Q' */) {
634
0
  limb origx[10], origxprime[10], zzz[19], xx[19], zz[19], xxprime[19],
635
0
        zzprime[19], zzzprime[19], xxxprime[19];
636
637
0
  memcpy(origx, x, 10 * sizeof(limb));
638
0
  fsum(x, z);
639
  /* |x[i]| < 2^27 */
640
0
  fdifference(z, origx);  /* does x - z */
641
  /* |z[i]| < 2^27 */
642
643
0
  memcpy(origxprime, xprime, sizeof(limb) * 10);
644
0
  fsum(xprime, zprime);
645
  /* |xprime[i]| < 2^27 */
646
0
  fdifference(zprime, origxprime);
647
  /* |zprime[i]| < 2^27 */
648
0
  fproduct(xxprime, xprime, z);
649
  /* |xxprime[i]| < 14*2^54: the largest product of two limbs will be <
650
   * 2^(27+27) and fproduct adds together, at most, 14 of those products.
651
   * (Approximating that to 2^58 doesn't work out.) */
652
0
  fproduct(zzprime, x, zprime);
653
  /* |zzprime[i]| < 14*2^54 */
654
0
  freduce_degree(xxprime);
655
0
  freduce_coefficients(xxprime);
656
  /* |xxprime[i]| < 2^26 */
657
0
  freduce_degree(zzprime);
658
0
  freduce_coefficients(zzprime);
659
  /* |zzprime[i]| < 2^26 */
660
0
  memcpy(origxprime, xxprime, sizeof(limb) * 10);
661
0
  fsum(xxprime, zzprime);
662
  /* |xxprime[i]| < 2^27 */
663
0
  fdifference(zzprime, origxprime);
664
  /* |zzprime[i]| < 2^27 */
665
0
  fsquare(xxxprime, xxprime);
666
  /* |xxxprime[i]| < 2^26 */
667
0
  fsquare(zzzprime, zzprime);
668
  /* |zzzprime[i]| < 2^26 */
669
0
  fproduct(zzprime, zzzprime, qmqp);
670
  /* |zzprime[i]| < 14*2^52 */
671
0
  freduce_degree(zzprime);
672
0
  freduce_coefficients(zzprime);
673
  /* |zzprime[i]| < 2^26 */
674
0
  memcpy(x3, xxxprime, sizeof(limb) * 10);
675
0
  memcpy(z3, zzprime, sizeof(limb) * 10);
676
677
0
  fsquare(xx, x);
678
  /* |xx[i]| < 2^26 */
679
0
  fsquare(zz, z);
680
  /* |zz[i]| < 2^26 */
681
0
  fproduct(x2, xx, zz);
682
  /* |x2[i]| < 14*2^52 */
683
0
  freduce_degree(x2);
684
0
  freduce_coefficients(x2);
685
  /* |x2[i]| < 2^26 */
686
0
  fdifference(zz, xx);  // does zz = xx - zz
687
  /* |zz[i]| < 2^27 */
688
0
  memset(zzz + 10, 0, sizeof(limb) * 9);
689
0
  fscalar_product(zzz, zz, 121665);
690
  /* |zzz[i]| < 2^(27+17) */
691
  /* No need to call freduce_degree here:
692
     fscalar_product doesn't increase the degree of its input. */
693
0
  freduce_coefficients(zzz);
694
  /* |zzz[i]| < 2^26 */
695
0
  fsum(zzz, xx);
696
  /* |zzz[i]| < 2^27 */
697
0
  fproduct(z2, zz, zzz);
698
  /* |z2[i]| < 14*2^(26+27) */
699
0
  freduce_degree(z2);
700
0
  freduce_coefficients(z2);
701
  /* |z2|i| < 2^26 */
702
0
}
703
704
/* Conditionally swap two reduced-form limb arrays if 'iswap' is 1, but leave
705
 * them unchanged if 'iswap' is 0.  Runs in data-invariant time to avoid
706
 * side-channel attacks.
707
 *
708
 * NOTE that this function requires that 'iswap' be 1 or 0; other values give
709
 * wrong results.  Also, the two limb arrays must be in reduced-coefficient,
710
 * reduced-degree form: the values in a[10..19] or b[10..19] aren't swapped,
711
 * and all all values in a[0..9],b[0..9] must have magnitude less than
712
 * INT32_MAX. */
713
static void
714
0
swap_conditional(limb a[19], limb b[19], limb iswap) {
715
0
  unsigned i;
716
0
  const s32 swap = (s32) -iswap;
717
718
0
  for (i = 0; i < 10; ++i) {
719
0
    const s32 x = swap & ( ((s32)a[i]) ^ ((s32)b[i]) );
720
0
    a[i] = ((s32)a[i]) ^ x;
721
0
    b[i] = ((s32)b[i]) ^ x;
722
0
  }
723
0
}
724
725
/* Calculates nQ where Q is the x-coordinate of a point on the curve
726
 *
727
 *   resultx/resultz: the x coordinate of the resulting curve point (short form)
728
 *   n: a little endian, 32-byte number
729
 *   q: a point of the curve (short form) */
730
static void
731
0
cmult(limb *resultx, limb *resultz, const u8 *n, const limb *q) {
732
0
  limb a[19] = {0}, b[19] = {1}, c[19] = {1}, d[19] = {0};
733
0
  limb *nqpqx = a, *nqpqz = b, *nqx = c, *nqz = d, *t;
734
0
  limb e[19] = {0}, f[19] = {1}, g[19] = {0}, h[19] = {1};
735
0
  limb *nqpqx2 = e, *nqpqz2 = f, *nqx2 = g, *nqz2 = h;
736
737
0
  unsigned i, j;
738
739
0
  memcpy(nqpqx, q, sizeof(limb) * 10);
740
741
0
  for (i = 0; i < 32; ++i) {
742
0
    u8 byte = n[31 - i];
743
0
    for (j = 0; j < 8; ++j) {
744
0
      const limb bit = byte >> 7;
745
746
0
      swap_conditional(nqx, nqpqx, bit);
747
0
      swap_conditional(nqz, nqpqz, bit);
748
0
      fmonty(nqx2, nqz2,
749
0
             nqpqx2, nqpqz2,
750
0
             nqx, nqz,
751
0
             nqpqx, nqpqz,
752
0
             q);
753
0
      swap_conditional(nqx2, nqpqx2, bit);
754
0
      swap_conditional(nqz2, nqpqz2, bit);
755
756
0
      t = nqx;
757
0
      nqx = nqx2;
758
0
      nqx2 = t;
759
0
      t = nqz;
760
0
      nqz = nqz2;
761
0
      nqz2 = t;
762
0
      t = nqpqx;
763
0
      nqpqx = nqpqx2;
764
0
      nqpqx2 = t;
765
0
      t = nqpqz;
766
0
      nqpqz = nqpqz2;
767
0
      nqpqz2 = t;
768
769
0
      byte <<= 1;
770
0
    }
771
0
  }
772
773
0
  memcpy(resultx, nqx, sizeof(limb) * 10);
774
0
  memcpy(resultz, nqz, sizeof(limb) * 10);
775
0
}
776
777
// -----------------------------------------------------------------------------
778
// Shamelessly copied from djb's code
779
// -----------------------------------------------------------------------------
780
static void
781
0
crecip(limb *out, const limb *z) {
782
0
  limb z2[10];
783
0
  limb z9[10];
784
0
  limb z11[10];
785
0
  limb z2_5_0[10];
786
0
  limb z2_10_0[10];
787
0
  limb z2_20_0[10];
788
0
  limb z2_50_0[10];
789
0
  limb z2_100_0[10];
790
0
  limb t0[10];
791
0
  limb t1[10];
792
0
  int i;
793
794
  /* 2 */ fsquare(z2,z);
795
0
  /* 4 */ fsquare(t1,z2);
796
0
  /* 8 */ fsquare(t0,t1);
797
0
  /* 9 */ fmul(z9,t0,z);
798
0
  /* 11 */ fmul(z11,z9,z2);
799
0
  /* 22 */ fsquare(t0,z11);
800
0
  /* 2^5 - 2^0 = 31 */ fmul(z2_5_0,t0,z9);
801
802
  /* 2^6 - 2^1 */ fsquare(t0,z2_5_0);
803
0
  /* 2^7 - 2^2 */ fsquare(t1,t0);
804
0
  /* 2^8 - 2^3 */ fsquare(t0,t1);
805
0
  /* 2^9 - 2^4 */ fsquare(t1,t0);
806
0
  /* 2^10 - 2^5 */ fsquare(t0,t1);
807
0
  /* 2^10 - 2^0 */ fmul(z2_10_0,t0,z2_5_0);
808
809
  /* 2^11 - 2^1 */ fsquare(t0,z2_10_0);
810
0
  /* 2^12 - 2^2 */ fsquare(t1,t0);
811
0
  /* 2^20 - 2^10 */ for (i = 2;i < 10;i += 2) { fsquare(t0,t1); fsquare(t1,t0); }
812
0
  /* 2^20 - 2^0 */ fmul(z2_20_0,t1,z2_10_0);
813
814
  /* 2^21 - 2^1 */ fsquare(t0,z2_20_0);
815
0
  /* 2^22 - 2^2 */ fsquare(t1,t0);
816
0
  /* 2^40 - 2^20 */ for (i = 2;i < 20;i += 2) { fsquare(t0,t1); fsquare(t1,t0); }
817
0
  /* 2^40 - 2^0 */ fmul(t0,t1,z2_20_0);
818
819
  /* 2^41 - 2^1 */ fsquare(t1,t0);
820
0
  /* 2^42 - 2^2 */ fsquare(t0,t1);
821
0
  /* 2^50 - 2^10 */ for (i = 2;i < 10;i += 2) { fsquare(t1,t0); fsquare(t0,t1); }
822
0
  /* 2^50 - 2^0 */ fmul(z2_50_0,t0,z2_10_0);
823
824
  /* 2^51 - 2^1 */ fsquare(t0,z2_50_0);
825
0
  /* 2^52 - 2^2 */ fsquare(t1,t0);
826
0
  /* 2^100 - 2^50 */ for (i = 2;i < 50;i += 2) { fsquare(t0,t1); fsquare(t1,t0); }
827
0
  /* 2^100 - 2^0 */ fmul(z2_100_0,t1,z2_50_0);
828
829
  /* 2^101 - 2^1 */ fsquare(t1,z2_100_0);
830
0
  /* 2^102 - 2^2 */ fsquare(t0,t1);
831
0
  /* 2^200 - 2^100 */ for (i = 2;i < 100;i += 2) { fsquare(t1,t0); fsquare(t0,t1); }
832
0
  /* 2^200 - 2^0 */ fmul(t1,t0,z2_100_0);
833
834
  /* 2^201 - 2^1 */ fsquare(t0,t1);
835
0
  /* 2^202 - 2^2 */ fsquare(t1,t0);
836
0
  /* 2^250 - 2^50 */ for (i = 2;i < 50;i += 2) { fsquare(t0,t1); fsquare(t1,t0); }
837
0
  /* 2^250 - 2^0 */ fmul(t0,t1,z2_50_0);
838
839
  /* 2^251 - 2^1 */ fsquare(t1,t0);
840
0
  /* 2^252 - 2^2 */ fsquare(t0,t1);
841
0
  /* 2^253 - 2^3 */ fsquare(t1,t0);
842
0
  /* 2^254 - 2^4 */ fsquare(t0,t1);
843
0
  /* 2^255 - 2^5 */ fsquare(t1,t0);
844
0
  /* 2^255 - 21 */ fmul(out,t1,z11);
845
0
}
846
847
int
848
0
curve25519_donna(u8 *mypublic, const u8 *secret, const u8 *basepoint) {
849
0
  limb bp[10], x[10], z[11], zmone[10];
850
0
  uint8_t e[32];
851
0
  int i;
852
853
0
  for (i = 0; i < 32; ++i) e[i] = secret[i];
854
0
  e[0] &= 248;
855
0
  e[31] &= 127;
856
0
  e[31] |= 64;
857
858
0
  fexpand(bp, basepoint);
859
0
  cmult(x, z, e, bp);
860
0
  crecip(zmone, z);
861
0
  fmul(z, x, zmone);
862
0
  fcontract(mypublic, z);
863
0
  return 0;
864
0
}