Coverage Report

Created: 2026-11-12 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wolfssl-heapmath/wolfcrypt/src/fe_x25519_128.h
Line
Count
Source
1
/* fe_x25519_128.h
2
 *
3
 * Copyright (C) 2006-2026 wolfSSL Inc.
4
 *
5
 * This file is part of wolfSSL.
6
 *
7
 * wolfSSL is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * wolfSSL is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20
 */
21
22
/* Generated using (from wolfssl):
23
 *   cd ../scripts
24
 *   ruby ./x25519/fe_x25519_128_gen.rb > ../wolfssl/wolfcrypt/src/fe_x25519_128.h
25
 */
26
27
void fe_init(void)
28
18.8k
{
29
18.8k
}
30
31
/* Convert a number represented as an array of bytes to an array of words with
32
 * 51-bits of data in each word.
33
 *
34
 * in   An array of bytes.
35
 * out  An array of words.
36
 */
37
void fe_frombytes(fe out, const unsigned char *in)
38
18.3k
{
39
18.3k
    out[0] = (sword64)(
40
18.3k
             (((word64)((in[ 0]      )       ))      )
41
18.3k
           | (((word64)((in[ 1]      )       )) <<  8)
42
18.3k
           | (((word64)((in[ 2]      )       )) << 16)
43
18.3k
           | (((word64)((in[ 3]      )       )) << 24)
44
18.3k
           | (((word64)((in[ 4]      )       )) << 32)
45
18.3k
           | (((word64)((in[ 5]      )       )) << 40)
46
18.3k
           | (((word64)((in[ 6]      ) & 0x07)) << 48));
47
18.3k
    out[1] = (sword64)(
48
18.3k
             (((word64)((in[ 6] >>  3) & 0x1f))      )
49
18.3k
           | (((word64)((in[ 7]      )       )) <<  5)
50
18.3k
           | (((word64)((in[ 8]      )       )) << 13)
51
18.3k
           | (((word64)((in[ 9]      )       )) << 21)
52
18.3k
           | (((word64)((in[10]      )       )) << 29)
53
18.3k
           | (((word64)((in[11]      )       )) << 37)
54
18.3k
           | (((word64)((in[12]      ) & 0x3f)) << 45));
55
18.3k
    out[2] = (sword64)(
56
18.3k
             (((word64)((in[12] >>  6) & 0x03))      )
57
18.3k
           | (((word64)((in[13]      )       )) <<  2)
58
18.3k
           | (((word64)((in[14]      )       )) << 10)
59
18.3k
           | (((word64)((in[15]      )       )) << 18)
60
18.3k
           | (((word64)((in[16]      )       )) << 26)
61
18.3k
           | (((word64)((in[17]      )       )) << 34)
62
18.3k
           | (((word64)((in[18]      )       )) << 42)
63
18.3k
           | (((word64)((in[19]      ) & 0x01)) << 50));
64
18.3k
    out[3] = (sword64)(
65
18.3k
             (((word64)((in[19] >>  1) & 0x7f))      )
66
18.3k
           | (((word64)((in[20]      )       )) <<  7)
67
18.3k
           | (((word64)((in[21]      )       )) << 15)
68
18.3k
           | (((word64)((in[22]      )       )) << 23)
69
18.3k
           | (((word64)((in[23]      )       )) << 31)
70
18.3k
           | (((word64)((in[24]      )       )) << 39)
71
18.3k
           | (((word64)((in[25]      ) & 0x0f)) << 47));
72
18.3k
    out[4] = (sword64)(
73
18.3k
             (((word64)((in[25] >>  4) & 0x0f))      )
74
18.3k
           | (((word64)((in[26]      )       )) <<  4)
75
18.3k
           | (((word64)((in[27]      )       )) << 12)
76
18.3k
           | (((word64)((in[28]      )       )) << 20)
77
18.3k
           | (((word64)((in[29]      )       )) << 28)
78
18.3k
           | (((word64)((in[30]      )       )) << 36)
79
18.3k
           | (((word64)((in[31]      ) & 0x7f)) << 44));
80
18.3k
}
81
82
/* Convert a number represented as an array of words to an array of bytes.
83
 * The array of words is normalized to an array of 51-bit data words and if
84
 * greater than the mod, modulo reduced by the prime 2^255 - 1.
85
 *
86
 * n    An array of words.
87
 * out  An array of bytes.
88
 */
89
void fe_tobytes(unsigned char *out, const fe n)
90
32.6k
{
91
32.6k
    fe      in;
92
32.6k
    sword64 c;
93
94
32.6k
    in[0] = n[0];
95
32.6k
    in[1] = n[1];
96
32.6k
    in[2] = n[2];
97
32.6k
    in[3] = n[3];
98
32.6k
    in[4] = n[4];
99
100
    /* Normalize to 51-bits of data per word. */
101
32.6k
    in[0] += (in[4] >> 51) * 19; in[4] &= 0x7ffffffffffff;
102
103
32.6k
    in[1] += in[0] >> 51; in[0] &= 0x7ffffffffffff;
104
32.6k
    in[2] += in[1] >> 51; in[1] &= 0x7ffffffffffff;
105
32.6k
    in[3] += in[2] >> 51; in[2] &= 0x7ffffffffffff;
106
32.6k
    in[4] += in[3] >> 51; in[3] &= 0x7ffffffffffff;
107
32.6k
    in[0] += (in[4] >> 51) * 19;
108
32.6k
    in[4] &= 0x7ffffffffffff;
109
110
32.6k
    c = (in[0] + 19) >> 51;
111
32.6k
    c = (in[1] + c) >> 51;
112
32.6k
    c = (in[2] + c) >> 51;
113
32.6k
    c = (in[3] + c) >> 51;
114
32.6k
    c = (in[4] + c) >> 51;
115
32.6k
    in[0] += c * 19;
116
32.6k
    in[1] += in[0] >> 51; in[0] &= 0x7ffffffffffff;
117
32.6k
    in[2] += in[1] >> 51; in[1] &= 0x7ffffffffffff;
118
32.6k
    in[3] += in[2] >> 51; in[2] &= 0x7ffffffffffff;
119
32.6k
    in[4] += in[3] >> 51; in[3] &= 0x7ffffffffffff;
120
32.6k
    in[4] &= 0x7ffffffffffff;
121
122
32.6k
    out[ 0] = (((byte)((in[0]      )       ))      );
123
32.6k
    out[ 1] = (((byte)((in[0] >>  8)       ))      );
124
32.6k
    out[ 2] = (((byte)((in[0] >> 16)       ))      );
125
32.6k
    out[ 3] = (((byte)((in[0] >> 24)       ))      );
126
32.6k
    out[ 4] = (((byte)((in[0] >> 32)       ))      );
127
32.6k
    out[ 5] = (((byte)((in[0] >> 40)       ))      );
128
32.6k
    out[ 6] = (byte)((((byte)((in[0] >> 48) & 0x07))      )
129
32.6k
                   | (((byte)((in[1]      ) & 0x1f)) <<  3));
130
32.6k
    out[ 7] = (((byte)((in[1] >>  5)       ))      );
131
32.6k
    out[ 8] = (((byte)((in[1] >> 13)       ))      );
132
32.6k
    out[ 9] = (((byte)((in[1] >> 21)       ))      );
133
32.6k
    out[10] = (((byte)((in[1] >> 29)       ))      );
134
32.6k
    out[11] = (((byte)((in[1] >> 37)       ))      );
135
32.6k
    out[12] = (byte)((((byte)((in[1] >> 45) & 0x3f))      )
136
32.6k
                   | (((byte)((in[2]      ) & 0x03)) <<  6));
137
32.6k
    out[13] = (((byte)((in[2] >>  2)       ))      );
138
32.6k
    out[14] = (((byte)((in[2] >> 10)       ))      );
139
32.6k
    out[15] = (((byte)((in[2] >> 18)       ))      );
140
32.6k
    out[16] = (((byte)((in[2] >> 26)       ))      );
141
32.6k
    out[17] = (((byte)((in[2] >> 34)       ))      );
142
32.6k
    out[18] = (((byte)((in[2] >> 42)       ))      );
143
32.6k
    out[19] = (byte)((((byte)((in[2] >> 50) & 0x01))      )
144
32.6k
                   | (((byte)((in[3]      ) & 0x7f)) <<  1));
145
32.6k
    out[20] = (((byte)((in[3] >>  7)       ))      );
146
32.6k
    out[21] = (((byte)((in[3] >> 15)       ))      );
147
32.6k
    out[22] = (((byte)((in[3] >> 23)       ))      );
148
32.6k
    out[23] = (((byte)((in[3] >> 31)       ))      );
149
32.6k
    out[24] = (((byte)((in[3] >> 39)       ))      );
150
32.6k
    out[25] = (byte)((((byte)((in[3] >> 47) & 0x0f))      )
151
32.6k
                   | (((byte)((in[4]      ) & 0x0f)) <<  4));
152
32.6k
    out[26] = (((byte)((in[4] >>  4)       ))      );
153
32.6k
    out[27] = (((byte)((in[4] >> 12)       ))      );
154
32.6k
    out[28] = (((byte)((in[4] >> 20)       ))      );
155
32.6k
    out[29] = (((byte)((in[4] >> 28)       ))      );
156
32.6k
    out[30] = (((byte)((in[4] >> 36)       ))      );
157
32.6k
    out[31] = (((byte)((in[4] >> 44) & 0x7f))      );
158
32.6k
}
159
160
/* Set the field element to 1.
161
 *
162
 * n  The field element number.
163
 */
164
void fe_1(fe n)
165
697k
{
166
697k
    n[0] = 0x0000000000001;
167
697k
    n[1] = 0x0000000000000;
168
697k
    n[2] = 0x0000000000000;
169
697k
    n[3] = 0x0000000000000;
170
697k
    n[4] = 0x0000000000000;
171
697k
}
172
173
/* Set the field element to 0.
174
 *
175
 * n  The field element number.
176
 */
177
void fe_0(fe n)
178
355k
{
179
355k
    n[0] = 0x0000000000000;
180
355k
    n[1] = 0x0000000000000;
181
355k
    n[2] = 0x0000000000000;
182
355k
    n[3] = 0x0000000000000;
183
355k
    n[4] = 0x0000000000000;
184
355k
}
185
186
/* Copy field element a into field element r.
187
 *
188
 * r  Field element to copy into.
189
 * a  Field element to copy.
190
 */
191
void fe_copy(fe r, const fe a)
192
43.7k
{
193
43.7k
    r[0] = a[0];
194
43.7k
    r[1] = a[1];
195
43.7k
    r[2] = a[2];
196
43.7k
    r[3] = a[3];
197
43.7k
    r[4] = a[4];
198
43.7k
}
199
200
/* Constant time, conditional swap of field elements a and b.
201
 *
202
 * f  A field element.
203
 * g  A field element.
204
 * b  If 1 then swap and if 0 then don't swap.
205
 */
206
void fe_cswap(fe f, fe g, int b)
207
7.27M
{
208
7.27M
    sword64 m = b;
209
7.27M
    sword64 t0, t1, t2, t3, t4;
210
211
    /* Convert conditional into mask. */
212
7.27M
    m = -m;
213
7.27M
    t0 = m & (f[0] ^ g[0]);
214
7.27M
    t1 = m & (f[1] ^ g[1]);
215
7.27M
    t2 = m & (f[2] ^ g[2]);
216
7.27M
    t3 = m & (f[3] ^ g[3]);
217
7.27M
    t4 = m & (f[4] ^ g[4]);
218
219
7.27M
    f[0] ^= t0;
220
7.27M
    f[1] ^= t1;
221
7.27M
    f[2] ^= t2;
222
7.27M
    f[3] ^= t3;
223
7.27M
    f[4] ^= t4;
224
225
7.27M
    g[0] ^= t0;
226
7.27M
    g[1] ^= t1;
227
7.27M
    g[2] ^= t2;
228
7.27M
    g[3] ^= t3;
229
7.27M
    g[4] ^= t4;
230
7.27M
}
231
232
/* Subtract b from a into r. (r = a - b)
233
 *
234
 * r  A field element.
235
 * a  A field element.
236
 * b  A field element.
237
 */
238
void fe_sub(fe r, const fe a, const fe b)
239
9.92M
{
240
9.92M
    r[0] = a[0] - b[0];
241
9.92M
    r[1] = a[1] - b[1];
242
9.92M
    r[2] = a[2] - b[2];
243
9.92M
    r[3] = a[3] - b[3];
244
9.92M
    r[4] = a[4] - b[4];
245
9.92M
}
246
247
/* Add b to a into r. (r = a + b)
248
 *
249
 * r  A field element.
250
 * a  A field element.
251
 * b  A field element.
252
 */
253
void fe_add(fe r, const fe a, const fe b)
254
9.92M
{
255
9.92M
    r[0] = a[0] + b[0];
256
9.92M
    r[1] = a[1] + b[1];
257
9.92M
    r[2] = a[2] + b[2];
258
9.92M
    r[3] = a[3] + b[3];
259
9.92M
    r[4] = a[4] + b[4];
260
9.92M
}
261
262
/* Multiply a and b into r. (r = a * b)
263
 *
264
 * r  A field element.
265
 * a  A field element.
266
 * b  A field element.
267
 */
268
void fe_mul(fe r, const fe a, const fe b)
269
13.9M
{
270
13.9M
    const __int128_t k19 = 19;
271
13.9M
    __int128_t t0 = ((__int128_t)a[0]) * b[0];
272
13.9M
    __int128_t t1 = ((__int128_t)a[0]) * b[1]
273
13.9M
                  + ((__int128_t)a[1]) * b[0];
274
13.9M
    __int128_t t2 = ((__int128_t)a[0]) * b[2]
275
13.9M
                  + ((__int128_t)a[1]) * b[1]
276
13.9M
                  + ((__int128_t)a[2]) * b[0];
277
13.9M
    __int128_t t3 = ((__int128_t)a[0]) * b[3]
278
13.9M
                  + ((__int128_t)a[1]) * b[2]
279
13.9M
                  + ((__int128_t)a[2]) * b[1]
280
13.9M
                  + ((__int128_t)a[3]) * b[0];
281
13.9M
    __int128_t t4 = ((__int128_t)a[0]) * b[4]
282
13.9M
                  + ((__int128_t)a[1]) * b[3]
283
13.9M
                  + ((__int128_t)a[2]) * b[2]
284
13.9M
                  + ((__int128_t)a[3]) * b[1]
285
13.9M
                  + ((__int128_t)a[4]) * b[0];
286
13.9M
    __int128_t t5 = ((__int128_t)a[1]) * b[4]
287
13.9M
                  + ((__int128_t)a[2]) * b[3]
288
13.9M
                  + ((__int128_t)a[3]) * b[2]
289
13.9M
                  + ((__int128_t)a[4]) * b[1];
290
13.9M
    __int128_t t6 = ((__int128_t)a[2]) * b[4]
291
13.9M
                  + ((__int128_t)a[3]) * b[3]
292
13.9M
                  + ((__int128_t)a[4]) * b[2];
293
13.9M
    __int128_t t7 = ((__int128_t)a[3]) * b[4]
294
13.9M
                  + ((__int128_t)a[4]) * b[3];
295
13.9M
    __int128_t t8 = ((__int128_t)a[4]) * b[4];
296
297
    /* Modulo reduce double long word. */
298
13.9M
    t0 += t5 * k19;
299
13.9M
    t1 += t6 * k19;
300
13.9M
    t2 += t7 * k19;
301
13.9M
    t3 += t8 * k19;
302
303
    /* Normalize to 51-bits of data per word. */
304
13.9M
    t0 += (t4 >> 51) * k19; t4 &= 0x7ffffffffffff;
305
306
13.9M
    t1 += t0 >> 51; r[0] = t0 & 0x7ffffffffffff;
307
13.9M
    t2 += t1 >> 51; r[1] = t1 & 0x7ffffffffffff;
308
13.9M
    t3 += t2 >> 51; r[2] = t2 & 0x7ffffffffffff;
309
13.9M
    t4 += t3 >> 51; r[3] = t3 & 0x7ffffffffffff;
310
13.9M
    r[0] += (sword64)((t4 >> 51) * k19);
311
13.9M
    r[4] = t4 & 0x7ffffffffffff;
312
13.9M
}
313
314
/* Square a and put result in r. (r = a * a)
315
 *
316
 * r  A field element.
317
 * a  A field element.
318
 * b  A field element.
319
 */
320
void fe_sq(fe r, const fe a)
321
13.1M
{
322
13.1M
    const __int128_t k19 = 19;
323
13.1M
    const __int128_t k2 = 2;
324
13.1M
    __int128_t t0 = ((__int128_t)a[0]) * a[0];
325
13.1M
    __int128_t t1 = ((__int128_t)a[0]) * a[1] * k2;
326
13.1M
    __int128_t t2 = ((__int128_t)a[0]) * a[2] * k2
327
13.1M
                  + ((__int128_t)a[1]) * a[1];
328
13.1M
    __int128_t t3 = ((__int128_t)a[0]) * a[3] * k2
329
13.1M
                  + ((__int128_t)a[1]) * a[2] * k2;
330
13.1M
    __int128_t t4 = ((__int128_t)a[0]) * a[4] * k2
331
13.1M
                  + ((__int128_t)a[1]) * a[3] * k2
332
13.1M
                  + ((__int128_t)a[2]) * a[2];
333
13.1M
    __int128_t t5 = ((__int128_t)a[1]) * a[4] * k2
334
13.1M
                  + ((__int128_t)a[2]) * a[3] * k2;
335
13.1M
    __int128_t t6 = ((__int128_t)a[2]) * a[4] * k2
336
13.1M
                  + ((__int128_t)a[3]) * a[3];
337
13.1M
    __int128_t t7 = ((__int128_t)a[3]) * a[4] * k2;
338
13.1M
    __int128_t t8 = ((__int128_t)a[4]) * a[4];
339
340
    /* Modulo reduce double long word. */
341
13.1M
    t0 += t5 * k19;
342
13.1M
    t1 += t6 * k19;
343
13.1M
    t2 += t7 * k19;
344
13.1M
    t3 += t8 * k19;
345
346
    /* Normalize to 51-bits of data per word. */
347
13.1M
    t0 += (t4 >> 51) * k19; t4 &= 0x7ffffffffffff;
348
349
13.1M
    t1 += t0 >> 51; r[0] = t0 & 0x7ffffffffffff;
350
13.1M
    t2 += t1 >> 51; r[1] = t1 & 0x7ffffffffffff;
351
13.1M
    t3 += t2 >> 51; r[2] = t2 & 0x7ffffffffffff;
352
13.1M
    t4 += t3 >> 51; r[3] = t3 & 0x7ffffffffffff;
353
13.1M
    r[0] += (sword64)((t4 >> 51) * k19);
354
13.1M
    r[4] = t4 & 0x7ffffffffffff;
355
13.1M
}
356
357
/* Multiply a by 121666 and put result in r. (r = 121666 * a)
358
 *
359
 * r  A field element.
360
 * a  A field element.
361
 * b  A field element.
362
 */
363
void fe_mul121666(fe r, fe a)
364
1.72M
{
365
1.72M
    const __int128_t k19 = 19;
366
1.72M
    const __int128_t k121666 = 121666;
367
1.72M
    __int128_t t0 = ((__int128_t)a[0]) * k121666;
368
1.72M
    __int128_t t1 = ((__int128_t)a[1]) * k121666;
369
1.72M
    __int128_t t2 = ((__int128_t)a[2]) * k121666;
370
1.72M
    __int128_t t3 = ((__int128_t)a[3]) * k121666;
371
1.72M
    __int128_t t4 = ((__int128_t)a[4]) * k121666;
372
373
    /* Normalize to 51-bits of data per word. */
374
1.72M
    t0 += (t4 >> 51) * k19; t4 &= 0x7ffffffffffff;
375
376
1.72M
    t1 += t0 >> 51; r[0] = t0 & 0x7ffffffffffff;
377
1.72M
    t2 += t1 >> 51; r[1] = t1 & 0x7ffffffffffff;
378
1.72M
    t3 += t2 >> 51; r[2] = t2 & 0x7ffffffffffff;
379
1.72M
    t4 += t3 >> 51; r[3] = t3 & 0x7ffffffffffff;
380
1.72M
    r[0] += (sword64)((t4 >> 51) * k19);
381
1.72M
    r[4] = t4 & 0x7ffffffffffff;
382
1.72M
}
383
384
/* Find the inverse of a modulo 2^255 - 1 and put result in r.
385
 * (r * a) mod (2^255 - 1) = 1
386
 * Implementation is constant time.
387
 *
388
 * r  A field element.
389
 * a  A field element.
390
 */
391
void fe_invert(fe r, const fe a)
392
14.0k
{
393
14.0k
    fe  t0, t1, t2, t3;
394
14.0k
    int i;
395
396
    /* a ^ (2^255 - 21) */
397
14.0k
    fe_sq(t0,  a); for (i = 1; i <   1; ++i) fe_sq(t0, t0);
398
28.0k
    fe_sq(t1, t0); for (i = 1; i <   2; ++i) fe_sq(t1, t1); fe_mul(t1,  a, t1);
399
14.0k
    fe_mul(t0, t0, t1);
400
14.0k
    fe_sq(t2, t0); for (i = 1; i <   1; ++i) fe_sq(t2, t2); fe_mul(t1, t1, t2);
401
70.1k
    fe_sq(t2, t1); for (i = 1; i <   5; ++i) fe_sq(t2, t2); fe_mul(t1, t2, t1);
402
140k
    fe_sq(t2, t1); for (i = 1; i <  10; ++i) fe_sq(t2, t2); fe_mul(t2, t2, t1);
403
280k
    fe_sq(t3, t2); for (i = 1; i <  20; ++i) fe_sq(t3, t3); fe_mul(t2, t3, t2);
404
140k
    fe_sq(t2, t2); for (i = 1; i <  10; ++i) fe_sq(t2, t2); fe_mul(t1, t2, t1);
405
701k
    fe_sq(t2, t1); for (i = 1; i <  50; ++i) fe_sq(t2, t2); fe_mul(t2, t2, t1);
406
1.40M
    fe_sq(t3, t2); for (i = 1; i < 100; ++i) fe_sq(t3, t3); fe_mul(t2, t3, t2);
407
701k
    fe_sq(t2, t2); for (i = 1; i <  50; ++i) fe_sq(t2, t2); fe_mul(t1, t2, t1);
408
70.1k
    fe_sq(t1, t1); for (i = 1; i <   5; ++i) fe_sq(t1, t1); fe_mul( r, t1, t0);
409
14.0k
}
410
411
#ifndef CURVE25519_SMALL
412
#ifndef WOLFSSL_CURVE25519_BLINDING
413
/* Scalar multiply the field element a by n using Montgomery Ladder and places
414
 * result in r.
415
 *
416
 * r  A field element as an array of bytes.
417
 * n  The scalar as an array of bytes.
418
 * a  A field element as an array of bytes.
419
 */
420
int curve25519(byte* r, const byte* n, const byte* a)
421
{
422
    fe           x1, x2, z2, x3, z3;
423
    fe           t0, t1;
424
    int          pos;
425
    unsigned int swap;
426
    unsigned int b;
427
428
    fe_frombytes(x1, a);
429
    fe_1(x2);
430
    fe_0(z2);
431
    fe_copy(x3, x1);
432
    fe_1(z3);
433
434
    swap = 0;
435
    for (pos = 254;pos >= 0;--pos) {
436
        b = (unsigned int)(n[pos / 8] >> (pos & 7));
437
        b &= 1;
438
        swap ^= b;
439
        fe_cswap(x2, x3, (int)swap);
440
        fe_cswap(z2, z3, (int)swap);
441
        swap = b;
442
443
        fe_sub(t0, x3, z3);
444
        fe_sub(t1, x2, z2);
445
        fe_add(x2, x2, z2);
446
        fe_add(z2, x3, z3);
447
        fe_mul(z3, t0, x2);
448
        fe_mul(z2, z2, t1);
449
        fe_sq(t0, t1);
450
        fe_sq(t1, x2);
451
        fe_add(x3, z3, z2);
452
        fe_sub(z2, z3, z2);
453
        fe_mul(x2, t1, t0);
454
        fe_sub(t1, t1, t0);
455
        fe_sq(z2, z2);
456
        fe_mul121666(z3, t1);
457
        fe_sq(x3, x3);
458
        fe_add(t0, t0, z3);
459
        fe_mul(z3, x1, z2);
460
        fe_mul(z2, t1, t0);
461
    }
462
    fe_cswap(x2, x3, (int)swap);
463
    fe_cswap(z2, z3, (int)swap);
464
465
    fe_invert(z2, z2);
466
    fe_mul(x2, x2, z2);
467
    fe_tobytes(r, x2);
468
469
    return 0;
470
}
471
#else
472
int curve25519_blind(byte* r, const byte* n, const byte* mask, const byte* a,
473
    const byte* rz)
474
6.77k
{
475
6.77k
    fe           x1, x2, z2, x3, z3;
476
6.77k
    fe           t0, t1;
477
6.77k
    int          pos;
478
6.77k
    unsigned int b;
479
480
6.77k
    fe_frombytes(x1, a);
481
6.77k
    fe_1(x2);
482
6.77k
    fe_0(z2);
483
6.77k
    fe_copy(x3, x1);
484
6.77k
    fe_frombytes(z3, rz);
485
6.77k
    fe_mul(x3, x3, z3);
486
487
    /* mask_bits[252] */
488
6.77k
    b = (unsigned int)(mask[31] >> 7);
489
6.77k
    b &= 1;
490
6.77k
    fe_cswap(x2,x3,(int)b);
491
6.77k
    fe_cswap(z2,z3,(int)b);
492
1.73M
    for (pos = 255;pos >= 1;--pos) {
493
1.72M
        b = (unsigned int)(n[pos / 8] >> (pos & 7));
494
1.72M
        b &= 1;
495
1.72M
        fe_cswap(x2, x3, (int)b);
496
1.72M
        fe_cswap(z2, z3, (int)b);
497
498
        /* montgomery */
499
1.72M
        fe_sub(t0, x3, z3);
500
1.72M
        fe_sub(t1, x2, z2);
501
1.72M
        fe_add(x2, x2, z2);
502
1.72M
        fe_add(z2, x3, z3);
503
1.72M
        fe_mul(z3, t0, x2);
504
1.72M
        fe_mul(z2, z2, t1);
505
1.72M
        fe_sq(t0, t1);
506
1.72M
        fe_sq(t1, x2);
507
1.72M
        fe_add(x3, z3, z2);
508
1.72M
        fe_sub(z2, z3, z2);
509
1.72M
        fe_mul(x2, t1, t0);
510
1.72M
        fe_sub(t1, t1, t0);
511
1.72M
        fe_sq(z2, z2);
512
1.72M
        fe_mul121666(z3, t1);
513
1.72M
        fe_sq(x3, x3);
514
1.72M
        fe_add(t0, t0, z3);
515
1.72M
        fe_mul(z3, x1, z2);
516
1.72M
        fe_mul(z2, t1, t0);
517
518
1.72M
        b = (unsigned int)(mask[(pos - 1) / 8] >> ((pos - 1) & 7));
519
1.72M
        b &= 1;
520
1.72M
        fe_cswap(x2, x3, (int)b);
521
1.72M
        fe_cswap(z2, z3, (int)b);
522
1.72M
    }
523
6.77k
    b = (unsigned int)(n[0] & 1);
524
6.77k
    fe_cswap(x2, x3, (int)b);
525
6.77k
    fe_cswap(z2, z3, (int)b);
526
527
6.77k
    fe_invert(z2, z2);
528
6.77k
    fe_mul(x2, x2, z2);
529
6.77k
    fe_tobytes(r, x2);
530
531
6.77k
    return 0;
532
6.77k
}
533
#endif /* WOLFSSL_CURVE25519_BLINDING */
534
#endif /* !CURVE25519_SMALL */
535
536
/* The field element value 0 as an array of bytes. */
537
static const unsigned char zero[32] = {0};
538
539
/* Constant time check as to whether a is not 0.
540
 *
541
 * a  A field element.
542
 */
543
int fe_isnonzero(const fe a)
544
6.72k
{
545
6.72k
    unsigned char s[32];
546
6.72k
    fe_tobytes(s, a);
547
6.72k
    return ConstantCompare(s, zero, 32);
548
6.72k
}
549
550
/* Checks whether a is negative.
551
 *
552
 * a  A field element.
553
 */
554
int fe_isnegative(const fe a)
555
11.8k
{
556
11.8k
    unsigned char s[32];
557
11.8k
    fe_tobytes(s, a);
558
11.8k
    return s[0] & 1;
559
11.8k
}
560
561
/* Negates field element a and stores the result in r.
562
 *
563
 * r  A field element.
564
 * a  A field element.
565
 */
566
void fe_neg(fe r, const fe a)
567
343k
{
568
343k
    r[0] = -a[0];
569
343k
    r[1] = -a[1];
570
343k
    r[2] = -a[2];
571
343k
    r[3] = -a[3];
572
343k
    r[4] = -a[4];
573
343k
}
574
575
/* Constant time, conditional move of b into a.
576
 * a is not changed if the condition is 0.
577
 *
578
 * f  A field element.
579
 * g  A field element.
580
 * b  If 1 then copy and if 0 then don't copy.
581
 */
582
void fe_cmov(fe f, const fe g, int b)
583
8.53M
{
584
8.53M
    sword64 m = b;
585
8.53M
    sword64 t0, t1, t2, t3, t4;
586
587
    /* Convert conditional into mask. */
588
8.53M
    m = -m;
589
8.53M
    t0 = m & (f[0] ^ g[0]);
590
8.53M
    t1 = m & (f[1] ^ g[1]);
591
8.53M
    t2 = m & (f[2] ^ g[2]);
592
8.53M
    t3 = m & (f[3] ^ g[3]);
593
8.53M
    t4 = m & (f[4] ^ g[4]);
594
595
8.53M
    f[0] ^= t0;
596
8.53M
    f[1] ^= t1;
597
8.53M
    f[2] ^= t2;
598
8.53M
    f[3] ^= t3;
599
8.53M
    f[4] ^= t4;
600
8.53M
}
601
602
void fe_pow22523(fe r, const fe a)
603
4.78k
{
604
4.78k
    fe t0, t1, t2;
605
4.78k
    int i;
606
607
    /* a ^ (2^255 - 23) */
608
4.78k
    fe_sq(t0,  a); for (i = 1; i <   1; ++i) fe_sq(t0, t0);
609
9.56k
    fe_sq(t1, t0); for (i = 1; i <   2; ++i) fe_sq(t1, t1); fe_mul(t1,  a, t1);
610
4.78k
    fe_mul(t0, t0, t1);
611
4.78k
    fe_sq(t0, t0); for (i = 1; i <   1; ++i) fe_sq(t0, t0); fe_mul(t0, t1, t0);
612
23.9k
    fe_sq(t1, t0); for (i = 1; i <   5; ++i) fe_sq(t1, t1); fe_mul(t0, t1, t0);
613
47.8k
    fe_sq(t1, t0); for (i = 1; i <  10; ++i) fe_sq(t1, t1); fe_mul(t1, t1, t0);
614
95.6k
    fe_sq(t2, t1); for (i = 1; i <  20; ++i) fe_sq(t2, t2); fe_mul(t1, t2, t1);
615
47.8k
    fe_sq(t1, t1); for (i = 1; i <  10; ++i) fe_sq(t1, t1); fe_mul(t0, t1, t0);
616
239k
    fe_sq(t1, t0); for (i = 1; i <  50; ++i) fe_sq(t1, t1); fe_mul(t1, t1, t0);
617
478k
    fe_sq(t2, t1); for (i = 1; i < 100; ++i) fe_sq(t2, t2); fe_mul(t1, t2, t1);
618
239k
    fe_sq(t1, t1); for (i = 1; i <  50; ++i) fe_sq(t1, t1); fe_mul(t0, t1, t0);
619
9.56k
    fe_sq(t0, t0); for (i = 1; i <   2; ++i) fe_sq(t0, t0); fe_mul( r, t0, a);
620
621
4.78k
    return;
622
4.78k
}
623
624
/* Double the square of a and put result in r. (r = 2 * a * a)
625
 *
626
 * r  A field element.
627
 * a  A field element.
628
 * b  A field element.
629
 */
630
void fe_sq2(fe r, const fe a)
631
500k
{
632
500k
    const __int128_t k2 = 2;
633
500k
    const __int128_t k19 = 19;
634
500k
    __int128_t t0 = k2 * (((__int128_t)a[0]) * a[0]);
635
500k
    __int128_t t1 = k2 * (((__int128_t)a[0]) * a[1] * k2);
636
500k
    __int128_t t2 = k2 * (((__int128_t)a[0]) * a[2] * k2
637
500k
                  + ((__int128_t)a[1]) * a[1]);
638
500k
    __int128_t t3 = k2 * (((__int128_t)a[0]) * a[3] * k2
639
500k
                  + ((__int128_t)a[1]) * a[2] * k2);
640
500k
    __int128_t t4 = k2 * (((__int128_t)a[0]) * a[4] * k2
641
500k
                  + ((__int128_t)a[1]) * a[3] * k2
642
500k
                  + ((__int128_t)a[2]) * a[2]);
643
500k
    __int128_t t5 = k2 * (((__int128_t)a[1]) * a[4] * k2
644
500k
                  + ((__int128_t)a[2]) * a[3] * k2);
645
500k
    __int128_t t6 = k2 * (((__int128_t)a[2]) * a[4] * k2
646
500k
                  + ((__int128_t)a[3]) * a[3]);
647
500k
    __int128_t t7 = k2 * (((__int128_t)a[3]) * a[4] * k2);
648
500k
    __int128_t t8 = k2 * (((__int128_t)a[4]) * a[4]);
649
650
    /* Modulo reduce double long word. */
651
500k
    t0 += t5 * k19;
652
500k
    t1 += t6 * k19;
653
500k
    t2 += t7 * k19;
654
500k
    t3 += t8 * k19;
655
656
    /* Normalize to 51-bits of data per word. */
657
500k
    t0 += (t4 >> 51) * k19; t4 &= 0x7ffffffffffff;
658
659
500k
    t1 += t0 >> 51; r[0] = t0 & 0x7ffffffffffff;
660
500k
    t2 += t1 >> 51; r[1] = t1 & 0x7ffffffffffff;
661
500k
    t3 += t2 >> 51; r[2] = t2 & 0x7ffffffffffff;
662
500k
    t4 += t3 >> 51; r[3] = t3 & 0x7ffffffffffff;
663
500k
    r[0] += (sword64)((t4 >> 51) * k19);
664
500k
    r[4] = t4 & 0x7ffffffffffff;
665
500k
}
666
667
/* Load 3 little endian bytes into a 64-bit word.
668
 *
669
 * in  An array of bytes.
670
 * returns a 64-bit word.
671
 */
672
sword64 load_3(const unsigned char *in)
673
0
{
674
0
    word64 result;
675
676
0
    result = ((((word64)in[0])      ) |
677
0
              (((word64)in[1]) <<  8) |
678
0
              (((word64)in[2]) << 16));
679
680
0
    return (sword64)result;
681
0
}
682
683
/* Load 4 little endian bytes into a 64-bit word.
684
 *
685
 * in  An array of bytes.
686
 * returns a 64-bit word.
687
 */
688
sword64 load_4(const unsigned char *in)
689
0
{
690
0
    word64 result;
691
692
0
    result = ((((word64)in[0])      ) |
693
0
              (((word64)in[1]) <<  8) |
694
0
              (((word64)in[2]) << 16) |
695
0
              (((word64)in[3]) << 24));
696
697
0
    return (sword64)result;
698
0
}
699