Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/ec/curve448/curve448.c
Line
Count
Source
1
/*
2
 * Copyright 2017-2022 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright 2015-2016 Cryptography Research, Inc.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 *
10
 * Originally written by Mike Hamburg
11
 */
12
#include <openssl/crypto.h>
13
#include "word.h"
14
#include "field.h"
15
16
#include "point_448.h"
17
#include "ed448.h"
18
#include "crypto/ecx.h"
19
#include "curve448_local.h"
20
21
794
#define COFACTOR 4
22
23
45
#define C448_WNAF_FIXED_TABLE_BITS 5
24
45
#define C448_WNAF_VAR_TABLE_BITS 3
25
26
18.0k
#define EDWARDS_D (-39081)
27
28
static const curve448_scalar_t precomputed_scalarmul_adjustment = {
29
    { { SC_LIMB(0xc873d6d54a7bb0cfULL), SC_LIMB(0xe933d8d723a70aadULL),
30
        SC_LIMB(0xbb124b65129c96fdULL), SC_LIMB(0x00000008335dc163ULL) } }
31
};
32
33
511
#define TWISTED_D (EDWARDS_D - 1)
34
35
450k
#define WBITS C448_WORD_BITS /* NB this may be different from ARCH_WORD_BITS */
36
37
/* Inverse. */
38
static void gf_invert(gf y, const gf x, int assert_nonzero)
39
544
{
40
544
    mask_t ret;
41
544
    gf t1, t2;
42
43
544
    gf_sqr(t1, x); /* o^2 */
44
544
    ret = gf_isr(t2, t1); /* +-1/sqrt(o^2) = +-1/o */
45
544
    (void)ret;
46
544
    if (assert_nonzero)
47
544
        assert(ret);
48
544
    gf_sqr(t1, t2);
49
544
    gf_mul(t2, t1, x); /* not direct to y in case of alias. */
50
544
    gf_copy(y, t2);
51
544
}
52
53
/** identity = (0,1) */
54
const curve448_point_t ossl_curve448_point_identity = { { { { { 0 } } }, { { { 1 } } }, { { { 1 } } }, { { { 0 } } } } };
55
56
static void point_double_internal(curve448_point_t p, const curve448_point_t q,
57
    int before_double)
58
28.4k
{
59
28.4k
    gf a, b, c, d;
60
61
28.4k
    gf_sqr(c, q->x);
62
28.4k
    gf_sqr(a, q->y);
63
28.4k
    gf_add_nr(d, c, a); /* 2+e */
64
28.4k
    gf_add_nr(p->t, q->y, q->x); /* 2+e */
65
28.4k
    gf_sqr(b, p->t);
66
28.4k
    gf_subx_nr(b, b, d, 3); /* 4+e */
67
28.4k
    gf_sub_nr(p->t, a, c); /* 3+e */
68
28.4k
    gf_sqr(p->x, q->z);
69
28.4k
    gf_add_nr(p->z, p->x, p->x); /* 2+e */
70
28.4k
    gf_subx_nr(a, p->z, p->t, 4); /* 6+e */
71
28.4k
    if (GF_HEADROOM == 5)
72
0
        gf_weak_reduce(a); /* or 1+e */
73
28.4k
    gf_mul(p->x, a, b);
74
28.4k
    gf_mul(p->z, p->t, a);
75
28.4k
    gf_mul(p->y, p->t, d);
76
28.4k
    if (!before_double)
77
13.2k
        gf_mul(p->t, b, d);
78
28.4k
}
79
80
void ossl_curve448_point_double(curve448_point_t p, const curve448_point_t q)
81
45
{
82
45
    point_double_internal(p, q, 0);
83
45
}
84
85
/* Operations on [p]niels */
86
static ossl_inline void cond_neg_niels(niels_t n, mask_t neg)
87
45.4k
{
88
45.4k
    gf_cond_swap(n->a, n->b, neg);
89
45.4k
    gf_cond_neg(n->c, neg);
90
45.4k
}
91
92
static void pt_to_pniels(pniels_t b, const curve448_point_t a)
93
405
{
94
405
    gf_sub(b->n->a, a->y, a->x);
95
405
    gf_add(b->n->b, a->x, a->y);
96
405
    gf_mulw(b->n->c, a->t, 2 * TWISTED_D);
97
405
    gf_add(b->z, a->z, a->z);
98
405
}
99
100
static void pniels_to_pt(curve448_point_t e, const pniels_t d)
101
34
{
102
34
    gf eu;
103
104
34
    gf_add(eu, d->n->b, d->n->a);
105
34
    gf_sub(e->y, d->n->b, d->n->a);
106
34
    gf_mul(e->t, e->y, eu);
107
34
    gf_mul(e->x, d->z, e->y);
108
34
    gf_mul(e->y, d->z, eu);
109
34
    gf_sqr(e->z, d->z);
110
34
}
111
112
static void niels_to_pt(curve448_point_t e, const niels_t n)
113
516
{
114
516
    gf_add(e->y, n->b, n->a);
115
516
    gf_sub(e->x, n->b, n->a);
116
516
    gf_mul(e->t, e->y, e->x);
117
516
    gf_copy(e->z, ONE);
118
516
}
119
120
static void add_niels_to_pt(curve448_point_t d, const niels_t e,
121
    int before_double)
122
47.8k
{
123
47.8k
    gf a, b, c;
124
125
47.8k
    gf_sub_nr(b, d->y, d->x); /* 3+e */
126
47.8k
    gf_mul(a, e->a, b);
127
47.8k
    gf_add_nr(b, d->x, d->y); /* 2+e */
128
47.8k
    gf_mul(d->y, e->b, b);
129
47.8k
    gf_mul(d->x, e->c, d->t);
130
47.8k
    gf_add_nr(c, a, d->y); /* 2+e */
131
47.8k
    gf_sub_nr(b, d->y, a); /* 3+e */
132
47.8k
    gf_sub_nr(d->y, d->z, d->x); /* 3+e */
133
47.8k
    gf_add_nr(a, d->x, d->z); /* 2+e */
134
47.8k
    gf_mul(d->z, a, d->y);
135
47.8k
    gf_mul(d->x, d->y, b);
136
47.8k
    gf_mul(d->y, a, c);
137
47.8k
    if (!before_double)
138
36.7k
        gf_mul(d->t, b, c);
139
47.8k
}
140
141
static void sub_niels_from_pt(curve448_point_t d, const niels_t e,
142
    int before_double)
143
2.26k
{
144
2.26k
    gf a, b, c;
145
146
2.26k
    gf_sub_nr(b, d->y, d->x); /* 3+e */
147
2.26k
    gf_mul(a, e->b, b);
148
2.26k
    gf_add_nr(b, d->x, d->y); /* 2+e */
149
2.26k
    gf_mul(d->y, e->a, b);
150
2.26k
    gf_mul(d->x, e->c, d->t);
151
2.26k
    gf_add_nr(c, a, d->y); /* 2+e */
152
2.26k
    gf_sub_nr(b, d->y, a); /* 3+e */
153
2.26k
    gf_add_nr(d->y, d->z, d->x); /* 2+e */
154
2.26k
    gf_sub_nr(a, d->z, d->x); /* 3+e */
155
2.26k
    gf_mul(d->z, a, d->y);
156
2.26k
    gf_mul(d->x, d->y, b);
157
2.26k
    gf_mul(d->y, a, c);
158
2.26k
    if (!before_double)
159
152
        gf_mul(d->t, b, c);
160
2.26k
}
161
162
static void add_pniels_to_pt(curve448_point_t p, const pniels_t pn,
163
    int before_double)
164
1.96k
{
165
1.96k
    gf L0;
166
167
1.96k
    gf_mul(L0, p->z, pn->z);
168
1.96k
    gf_copy(p->z, L0);
169
1.96k
    add_niels_to_pt(p, pn->n, before_double);
170
1.96k
}
171
172
static void sub_pniels_from_pt(curve448_point_t p, const pniels_t pn,
173
    int before_double)
174
1.65k
{
175
1.65k
    gf L0;
176
177
1.65k
    gf_mul(L0, p->z, pn->z);
178
1.65k
    gf_copy(p->z, L0);
179
1.65k
    sub_niels_from_pt(p, pn->n, before_double);
180
1.65k
}
181
182
c448_bool_t
183
ossl_curve448_point_eq(const curve448_point_t p,
184
    const curve448_point_t q)
185
45
{
186
45
    mask_t succ;
187
45
    gf a, b;
188
189
    /* equality mod 2-torsion compares x/y */
190
45
    gf_mul(a, p->y, q->x);
191
45
    gf_mul(b, q->y, p->x);
192
45
    succ = gf_eq(a, b);
193
194
45
    return mask_to_bool(succ);
195
45
}
196
197
c448_bool_t
198
ossl_curve448_point_valid(const curve448_point_t p)
199
106
{
200
106
    mask_t out;
201
106
    gf a, b, c;
202
203
106
    gf_mul(a, p->x, p->y);
204
106
    gf_mul(b, p->z, p->t);
205
106
    out = gf_eq(a, b);
206
106
    gf_sqr(a, p->x);
207
106
    gf_sqr(b, p->y);
208
106
    gf_sub(a, b, a);
209
106
    gf_sqr(b, p->t);
210
106
    gf_mulw(c, b, TWISTED_D);
211
106
    gf_sqr(b, p->z);
212
106
    gf_add(b, b, c);
213
106
    out &= gf_eq(a, b);
214
106
    out &= ~gf_eq(p->z, ZERO);
215
106
    return mask_to_bool(out);
216
106
}
217
218
static ossl_inline void constant_time_lookup_niels(niels_s *RESTRICT ni,
219
    const niels_t *table,
220
    int nelts, int idx)
221
45.4k
{
222
45.4k
    constant_time_lookup(ni, table, sizeof(niels_s), nelts, idx);
223
45.4k
}
224
225
void ossl_curve448_precomputed_scalarmul(curve448_point_t out,
226
    const curve448_precomputed_s *table,
227
    const curve448_scalar_t scalar)
228
505
{
229
505
    unsigned int i, j, k;
230
505
    const unsigned int n = COMBS_N, t = COMBS_T, s = COMBS_S;
231
505
    niels_t ni;
232
505
    curve448_scalar_t scalar1x;
233
234
505
    ossl_curve448_scalar_add(scalar1x, scalar, precomputed_scalarmul_adjustment);
235
505
    ossl_curve448_scalar_halve(scalar1x, scalar1x);
236
237
9.59k
    for (i = s; i > 0; i--) {
238
9.09k
        if (i != s)
239
8.58k
            point_double_internal(out, out, 0);
240
241
54.5k
        for (j = 0; j < n; j++) {
242
45.4k
            int tab = 0;
243
45.4k
            mask_t invert;
244
245
272k
            for (k = 0; k < t; k++) {
246
227k
                unsigned int bit = (i - 1) + s * (k + j * t);
247
248
227k
                if (bit < C448_SCALAR_BITS)
249
225k
                    tab |= (scalar1x->limb[bit / WBITS] >> (bit % WBITS) & 1) << k;
250
227k
            }
251
252
45.4k
            invert = (tab >> (t - 1)) - 1;
253
45.4k
            tab ^= invert;
254
45.4k
            tab &= (1 << (t - 1)) - 1;
255
256
45.4k
            constant_time_lookup_niels(ni, &table->table[j << (t - 1)],
257
45.4k
                1 << (t - 1), tab);
258
259
45.4k
            cond_neg_niels(ni, invert);
260
45.4k
            if ((i != s) || j != 0)
261
44.9k
                add_niels_to_pt(out, ni, j == n - 1 && i != 1);
262
505
            else
263
505
                niels_to_pt(out, ni);
264
45.4k
        }
265
9.09k
    }
266
267
505
    OPENSSL_cleanse(ni, sizeof(ni));
268
505
    OPENSSL_cleanse(scalar1x, sizeof(scalar1x));
269
505
}
270
271
void ossl_curve448_point_mul_by_ratio_and_encode_like_eddsa(
272
    uint8_t enc[EDDSA_448_PUBLIC_BYTES],
273
    const curve448_point_t p)
274
23
{
275
23
    gf x, y, z, t;
276
23
    curve448_point_t q;
277
278
    /* The point is now on the twisted curve.  Move it to untwisted. */
279
23
    curve448_point_copy(q, p);
280
281
23
    {
282
        /* 4-isogeny: 2xy/(y^+x^2), (y^2-x^2)/(2z^2-y^2+x^2) */
283
23
        gf u;
284
285
23
        gf_sqr(x, q->x);
286
23
        gf_sqr(t, q->y);
287
23
        gf_add(u, x, t);
288
23
        gf_add(z, q->y, q->x);
289
23
        gf_sqr(y, z);
290
23
        gf_sub(y, y, u);
291
23
        gf_sub(z, t, x);
292
23
        gf_sqr(x, q->z);
293
23
        gf_add(t, x, x);
294
23
        gf_sub(t, t, z);
295
23
        gf_mul(x, t, y);
296
23
        gf_mul(y, z, u);
297
23
        gf_mul(z, u, t);
298
23
        OPENSSL_cleanse(u, sizeof(u));
299
23
    }
300
301
    /* Affinize */
302
23
    gf_invert(z, z, 1);
303
23
    gf_mul(t, x, z);
304
23
    gf_mul(x, y, z);
305
306
    /* Encode */
307
23
    enc[EDDSA_448_PRIVATE_BYTES - 1] = 0;
308
23
    gf_serialize(enc, x, 1);
309
23
    enc[EDDSA_448_PRIVATE_BYTES - 1] |= 0x80 & gf_lobit(t);
310
311
23
    OPENSSL_cleanse(x, sizeof(x));
312
23
    OPENSSL_cleanse(y, sizeof(y));
313
23
    OPENSSL_cleanse(z, sizeof(z));
314
23
    OPENSSL_cleanse(t, sizeof(t));
315
23
    ossl_curve448_point_destroy(q);
316
23
}
317
318
c448_error_t
319
ossl_curve448_point_decode_like_eddsa_and_mul_by_ratio(
320
    curve448_point_t p,
321
    const uint8_t enc[EDDSA_448_PUBLIC_BYTES])
322
106
{
323
106
    uint8_t enc2[EDDSA_448_PUBLIC_BYTES];
324
106
    mask_t low;
325
106
    mask_t succ;
326
327
106
    memcpy(enc2, enc, sizeof(enc2));
328
329
106
    low = ~word_is_zero(enc2[EDDSA_448_PRIVATE_BYTES - 1] & 0x80);
330
106
    enc2[EDDSA_448_PRIVATE_BYTES - 1] &= ~0x80;
331
332
106
    succ = gf_deserialize(p->y, enc2, 1, 0);
333
106
    succ &= word_is_zero(enc2[EDDSA_448_PRIVATE_BYTES - 1]);
334
335
106
    gf_sqr(p->x, p->y);
336
106
    gf_sub(p->z, ONE, p->x); /* num = 1-y^2 */
337
106
    gf_mulw(p->t, p->x, EDWARDS_D); /* dy^2 */
338
106
    gf_sub(p->t, ONE, p->t); /* denom = 1-dy^2 or 1-d + dy^2 */
339
340
106
    gf_mul(p->x, p->z, p->t);
341
106
    succ &= gf_isr(p->t, p->x); /* 1/sqrt(num * denom) */
342
343
106
    gf_mul(p->x, p->t, p->z); /* sqrt(num / denom) */
344
106
    gf_cond_neg(p->x, gf_lobit(p->x) ^ low);
345
106
    gf_copy(p->z, ONE);
346
347
106
    {
348
106
        gf a, b, c, d;
349
350
        /* 4-isogeny 2xy/(y^2-ax^2), (y^2+ax^2)/(2-y^2-ax^2) */
351
106
        gf_sqr(c, p->x);
352
106
        gf_sqr(a, p->y);
353
106
        gf_add(d, c, a);
354
106
        gf_add(p->t, p->y, p->x);
355
106
        gf_sqr(b, p->t);
356
106
        gf_sub(b, b, d);
357
106
        gf_sub(p->t, a, c);
358
106
        gf_sqr(p->x, p->z);
359
106
        gf_add(p->z, p->x, p->x);
360
106
        gf_sub(a, p->z, d);
361
106
        gf_mul(p->x, a, b);
362
106
        gf_mul(p->z, p->t, a);
363
106
        gf_mul(p->y, p->t, d);
364
106
        gf_mul(p->t, b, d);
365
106
        OPENSSL_cleanse(a, sizeof(a));
366
106
        OPENSSL_cleanse(b, sizeof(b));
367
106
        OPENSSL_cleanse(c, sizeof(c));
368
106
        OPENSSL_cleanse(d, sizeof(d));
369
106
    }
370
371
106
    OPENSSL_cleanse(enc2, sizeof(enc2));
372
106
    assert(ossl_curve448_point_valid(p) || ~succ);
373
374
106
    return c448_succeed_if(mask_to_bool(succ));
375
106
}
376
377
c448_error_t
378
ossl_x448_int(uint8_t out[X_PUBLIC_BYTES],
379
    const uint8_t base[X_PUBLIC_BYTES],
380
    const uint8_t scalar[X_PRIVATE_BYTES])
381
39
{
382
39
    gf x1, x2, z2, x3, z3, t1, t2;
383
39
    int t;
384
39
    mask_t swap = 0;
385
39
    mask_t nz;
386
387
39
    (void)gf_deserialize(x1, base, 1, 0);
388
39
    gf_copy(x2, ONE);
389
39
    gf_copy(z2, ZERO);
390
39
    gf_copy(x3, x1);
391
39
    gf_copy(z3, ONE);
392
393
17.5k
    for (t = X_PRIVATE_BITS - 1; t >= 0; t--) {
394
17.4k
        uint8_t sb = scalar[t / 8];
395
17.4k
        mask_t k_t;
396
397
        /* Scalar conditioning */
398
17.4k
        if (t / 8 == 0)
399
312
            sb &= -(uint8_t)COFACTOR;
400
17.1k
        else if (t == X_PRIVATE_BITS - 1)
401
39
            sb = -1;
402
403
17.4k
        k_t = (sb >> (t % 8)) & 1;
404
17.4k
        k_t = 0 - k_t; /* set to all 0s or all 1s */
405
406
17.4k
        swap ^= k_t;
407
17.4k
        gf_cond_swap(x2, x3, swap);
408
17.4k
        gf_cond_swap(z2, z3, swap);
409
17.4k
        swap = k_t;
410
411
        /*
412
         * The "_nr" below skips coefficient reduction. In the following
413
         * comments, "2+e" is saying that the coefficients are at most 2+epsilon
414
         * times the reduction limit.
415
         */
416
17.4k
        gf_add_nr(t1, x2, z2); /* A = x2 + z2 */ /* 2+e */
417
17.4k
        gf_sub_nr(t2, x2, z2); /* B = x2 - z2 */ /* 3+e */
418
17.4k
        gf_sub_nr(z2, x3, z3); /* D = x3 - z3 */ /* 3+e */
419
17.4k
        gf_mul(x2, t1, z2); /* DA */
420
17.4k
        gf_add_nr(z2, z3, x3); /* C = x3 + z3 */ /* 2+e */
421
17.4k
        gf_mul(x3, t2, z2); /* CB */
422
17.4k
        gf_sub_nr(z3, x2, x3); /* DA-CB */ /* 3+e */
423
17.4k
        gf_sqr(z2, z3); /* (DA-CB)^2 */
424
17.4k
        gf_mul(z3, x1, z2); /* z3 = x1(DA-CB)^2 */
425
17.4k
        gf_add_nr(z2, x2, x3); /* (DA+CB) */ /* 2+e */
426
17.4k
        gf_sqr(x3, z2); /* x3 = (DA+CB)^2 */
427
428
17.4k
        gf_sqr(z2, t1); /* AA = A^2 */
429
17.4k
        gf_sqr(t1, t2); /* BB = B^2 */
430
17.4k
        gf_mul(x2, z2, t1); /* x2 = AA*BB */
431
17.4k
        gf_sub_nr(t2, z2, t1); /* E = AA-BB */ /* 3+e */
432
433
17.4k
        gf_mulw(t1, t2, -EDWARDS_D); /* E*-d = a24*E */
434
17.4k
        gf_add_nr(t1, t1, z2); /* AA + a24*E */ /* 2+e */
435
17.4k
        gf_mul(z2, t2, t1); /* z2 = E(AA+a24*E) */
436
17.4k
    }
437
438
    /* Finish */
439
39
    gf_cond_swap(x2, x3, swap);
440
39
    gf_cond_swap(z2, z3, swap);
441
39
    gf_invert(z2, z2, 0);
442
39
    gf_mul(x1, x2, z2);
443
39
    gf_serialize(out, x1, 1);
444
39
    nz = ~gf_eq(x1, ZERO);
445
446
39
    OPENSSL_cleanse(x1, sizeof(x1));
447
39
    OPENSSL_cleanse(x2, sizeof(x2));
448
39
    OPENSSL_cleanse(z2, sizeof(z2));
449
39
    OPENSSL_cleanse(x3, sizeof(x3));
450
39
    OPENSSL_cleanse(z3, sizeof(z3));
451
39
    OPENSSL_cleanse(t1, sizeof(t1));
452
39
    OPENSSL_cleanse(t2, sizeof(t2));
453
454
39
    return c448_succeed_if(mask_to_bool(nz));
455
39
}
456
457
void ossl_curve448_point_mul_by_ratio_and_encode_like_x448(uint8_t
458
                                                               out[X_PUBLIC_BYTES],
459
    const curve448_point_t p)
460
482
{
461
482
    curve448_point_t q;
462
463
482
    curve448_point_copy(q, p);
464
482
    gf_invert(q->t, q->x, 0); /* 1/x */
465
482
    gf_mul(q->z, q->t, q->y); /* y/x */
466
482
    gf_sqr(q->y, q->z); /* (y/x)^2 */
467
482
    gf_serialize(out, q->y, 1);
468
482
    ossl_curve448_point_destroy(q);
469
482
}
470
471
void ossl_x448_derive_public_key(uint8_t out[X_PUBLIC_BYTES],
472
    const uint8_t scalar[X_PRIVATE_BYTES])
473
482
{
474
    /* Scalar conditioning */
475
482
    uint8_t scalar2[X_PRIVATE_BYTES];
476
482
    curve448_scalar_t the_scalar;
477
482
    curve448_point_t p;
478
482
    unsigned int i;
479
480
482
    memcpy(scalar2, scalar, sizeof(scalar2));
481
482
    scalar2[0] &= -(uint8_t)COFACTOR;
482
483
482
    scalar2[X_PRIVATE_BYTES - 1] &= ~((0u - 1u) << ((X_PRIVATE_BITS + 7) % 8));
484
482
    scalar2[X_PRIVATE_BYTES - 1] |= 1 << ((X_PRIVATE_BITS + 7) % 8);
485
486
482
    ossl_curve448_scalar_decode_long(the_scalar, scalar2, sizeof(scalar2));
487
488
    /* Compensate for the encoding ratio */
489
964
    for (i = 1; i < X448_ENCODE_RATIO; i <<= 1)
490
482
        ossl_curve448_scalar_halve(the_scalar, the_scalar);
491
492
482
    ossl_curve448_precomputed_scalarmul(p, ossl_curve448_precomputed_base,
493
482
        the_scalar);
494
482
    ossl_curve448_point_mul_by_ratio_and_encode_like_x448(out, p);
495
482
    ossl_curve448_point_destroy(p);
496
482
}
497
498
/* Control for variable-time scalar multiply algorithms. */
499
struct smvt_control {
500
    int power, addend;
501
};
502
503
#if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3))
504
4.88k
#define NUMTRAILINGZEROS __builtin_ctz
505
#else
506
#define NUMTRAILINGZEROS numtrailingzeros
507
static uint32_t numtrailingzeros(uint32_t i)
508
{
509
    uint32_t tmp;
510
    uint32_t num = 31;
511
512
    if (i == 0)
513
        return 32;
514
515
    tmp = i << 16;
516
    if (tmp != 0) {
517
        i = tmp;
518
        num -= 16;
519
    }
520
    tmp = i << 8;
521
    if (tmp != 0) {
522
        i = tmp;
523
        num -= 8;
524
    }
525
    tmp = i << 4;
526
    if (tmp != 0) {
527
        i = tmp;
528
        num -= 4;
529
    }
530
    tmp = i << 2;
531
    if (tmp != 0) {
532
        i = tmp;
533
        num -= 2;
534
    }
535
    tmp = i << 1;
536
    if (tmp != 0)
537
        num--;
538
539
    return num;
540
}
541
#endif
542
543
static int recode_wnaf(struct smvt_control *control,
544
    /* [nbits/(table_bits + 1) + 3] */
545
    const curve448_scalar_t scalar,
546
    unsigned int table_bits)
547
90
{
548
90
    unsigned int table_size = C448_SCALAR_BITS / (table_bits + 1) + 3;
549
90
    int position = table_size - 1; /* at the end */
550
90
    uint64_t current = scalar->limb[0] & 0xFFFF;
551
90
    uint32_t mask = (1 << (table_bits + 1)) - 1;
552
90
    unsigned int w;
553
90
    const unsigned int B_OVER_16 = sizeof(scalar->limb[0]) / 2;
554
90
    unsigned int n, i;
555
556
    /* place the end marker */
557
90
    control[position].power = -1;
558
90
    control[position].addend = 0;
559
90
    position--;
560
561
    /*
562
     * PERF: Could negate scalar if it's large.  But then would need more cases
563
     * in the actual code that uses it, all for an expected reduction of like
564
     * 1/5 op. Probably not worth it.
565
     */
566
567
2.70k
    for (w = 1; w < (C448_SCALAR_BITS - 1) / 16 + 3; w++) {
568
2.61k
        if (w < (C448_SCALAR_BITS - 1) / 16 + 1) {
569
            /* Refill the 16 high bits of current */
570
2.43k
            current += (uint32_t)((scalar->limb[w / B_OVER_16]
571
2.43k
                                      >> (16 * (w % B_OVER_16)))
572
2.43k
                << 16);
573
2.43k
        }
574
575
7.49k
        while (current & 0xFFFF) {
576
4.88k
            uint32_t pos = NUMTRAILINGZEROS((uint32_t)current);
577
4.88k
            uint32_t odd = (uint32_t)current >> pos;
578
4.88k
            int32_t delta = odd & mask;
579
580
4.88k
            assert(position >= 0);
581
4.88k
            assert(pos < 32); /* can't fail since current & 0xFFFF != 0 */
582
4.88k
            if (odd & (1 << (table_bits + 1)))
583
2.26k
                delta -= (1 << (table_bits + 1));
584
4.88k
            current -= delta * (1 << pos);
585
4.88k
            control[position].power = pos + 16 * (w - 1);
586
4.88k
            control[position].addend = delta;
587
4.88k
            position--;
588
4.88k
        }
589
2.61k
        current >>= 16;
590
2.61k
    }
591
90
    assert(current == 0);
592
593
90
    position++;
594
90
    n = table_size - position;
595
5.06k
    for (i = 0; i < n; i++)
596
4.97k
        control[i] = control[i + position];
597
598
90
    return n - 1;
599
90
}
600
601
static void prepare_wnaf_table(pniels_t *output,
602
    const curve448_point_t working,
603
    unsigned int tbits)
604
45
{
605
45
    curve448_point_t tmp;
606
45
    int i;
607
45
    pniels_t twop;
608
609
45
    pt_to_pniels(output[0], working);
610
611
45
    if (tbits == 0)
612
0
        return;
613
614
45
    ossl_curve448_point_double(tmp, working);
615
45
    pt_to_pniels(twop, tmp);
616
617
45
    add_pniels_to_pt(tmp, output[0], 0);
618
45
    pt_to_pniels(output[1], tmp);
619
620
315
    for (i = 2; i < 1 << tbits; i++) {
621
270
        add_pniels_to_pt(tmp, twop, 0);
622
270
        pt_to_pniels(output[i], tmp);
623
270
    }
624
625
45
    ossl_curve448_point_destroy(tmp);
626
45
    OPENSSL_cleanse(twop, sizeof(twop));
627
45
}
628
629
void ossl_curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
630
    const curve448_scalar_t scalar1,
631
    const curve448_point_t base2,
632
    const curve448_scalar_t scalar2)
633
45
{
634
45
    const int table_bits_var = C448_WNAF_VAR_TABLE_BITS;
635
45
    const int table_bits_pre = C448_WNAF_FIXED_TABLE_BITS;
636
45
    struct smvt_control control_var[C448_SCALAR_BITS / (C448_WNAF_VAR_TABLE_BITS + 1) + 3];
637
45
    struct smvt_control control_pre[C448_SCALAR_BITS / (C448_WNAF_FIXED_TABLE_BITS + 1) + 3];
638
45
    int ncb_pre = recode_wnaf(control_pre, scalar1, table_bits_pre);
639
45
    int ncb_var = recode_wnaf(control_var, scalar2, table_bits_var);
640
45
    pniels_t precmp_var[1 << C448_WNAF_VAR_TABLE_BITS];
641
45
    int contp = 0, contv = 0, i;
642
643
45
    prepare_wnaf_table(precmp_var, base2, table_bits_var);
644
45
    i = control_var[0].power;
645
646
45
    if (i < 0) {
647
0
        curve448_point_copy(combo, ossl_curve448_point_identity);
648
0
        return;
649
0
    }
650
45
    if (i > control_pre[0].power) {
651
30
        pniels_to_pt(combo, precmp_var[control_var[0].addend >> 1]);
652
30
        contv++;
653
30
    } else if (i == control_pre[0].power && i >= 0) {
654
4
        pniels_to_pt(combo, precmp_var[control_var[0].addend >> 1]);
655
4
        add_niels_to_pt(combo,
656
4
            ossl_curve448_wnaf_base[control_pre[0].addend >> 1],
657
4
            i);
658
4
        contv++;
659
4
        contp++;
660
11
    } else {
661
11
        i = control_pre[0].power;
662
11
        niels_to_pt(combo, ossl_curve448_wnaf_base[control_pre[0].addend >> 1]);
663
11
        contp++;
664
11
    }
665
666
19.9k
    for (i--; i >= 0; i--) {
667
19.8k
        int cv = (i == control_var[contv].power);
668
19.8k
        int cp = (i == control_pre[contp].power);
669
670
19.8k
        point_double_internal(combo, combo, i && !(cv || cp));
671
672
19.8k
        if (cv) {
673
3.30k
            assert(control_var[contv].addend);
674
675
3.30k
            if (control_var[contv].addend > 0)
676
1.65k
                add_pniels_to_pt(combo,
677
1.65k
                    precmp_var[control_var[contv].addend >> 1],
678
1.65k
                    i && !cp);
679
1.65k
            else
680
1.65k
                sub_pniels_from_pt(combo,
681
1.65k
                    precmp_var[(-control_var[contv].addend)
682
1.65k
                        >> 1],
683
1.65k
                    i && !cp);
684
3.30k
            contv++;
685
3.30k
        }
686
687
19.8k
        if (cp) {
688
1.53k
            assert(control_pre[contp].addend);
689
690
1.53k
            if (control_pre[contp].addend > 0)
691
926
                add_niels_to_pt(combo,
692
926
                    ossl_curve448_wnaf_base[control_pre[contp].addend
693
926
                        >> 1],
694
926
                    i);
695
610
            else
696
610
                sub_niels_from_pt(combo,
697
610
                    ossl_curve448_wnaf_base[(-control_pre
698
610
                                                    [contp]
699
610
                                                        .addend)
700
610
                        >> 1],
701
610
                    i);
702
1.53k
            contp++;
703
1.53k
        }
704
19.8k
    }
705
706
    /* This function is non-secret, but whatever this is cheap. */
707
45
    OPENSSL_cleanse(control_var, sizeof(control_var));
708
45
    OPENSSL_cleanse(control_pre, sizeof(control_pre));
709
45
    OPENSSL_cleanse(precmp_var, sizeof(precmp_var));
710
711
45
    assert(contv == ncb_var);
712
45
    (void)ncb_var;
713
45
    assert(contp == ncb_pre);
714
45
    (void)ncb_pre;
715
45
}
716
717
void ossl_curve448_point_destroy(curve448_point_t point)
718
1.05k
{
719
1.05k
    OPENSSL_cleanse(point, sizeof(curve448_point_t));
720
1.05k
}
721
722
int ossl_x448(uint8_t out_shared_key[56], const uint8_t private_key[56],
723
    const uint8_t peer_public_value[56])
724
39
{
725
39
    return ossl_x448_int(out_shared_key, peer_public_value, private_key)
726
39
        == C448_SUCCESS;
727
39
}
728
729
void ossl_x448_public_from_private(uint8_t out_public_value[56],
730
    const uint8_t private_key[56])
731
482
{
732
482
    ossl_x448_derive_public_key(out_public_value, private_key);
733
482
}