Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/ec/curve448/curve448.c
Line
Count
Source
1
/*
2
 * Copyright 2017-2023 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
828
#define COFACTOR 4
22
23
36
#define C448_WNAF_FIXED_TABLE_BITS 5
24
36
#define C448_WNAF_VAR_TABLE_BITS 3
25
26
15.2k
#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
402
#define TWISTED_D (EDWARDS_D - 1)
34
35
524k
#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
621
{
40
621
    mask_t ret;
41
621
    gf t1, t2;
42
43
621
    ossl_gf_sqr(t1, x); /* o^2 */
44
621
    ret = gf_isr(t2, t1); /* +-1/sqrt(o^2) = +-1/o */
45
621
    (void)ret;
46
621
    if (assert_nonzero)
47
621
        assert(ret);
48
621
    ossl_gf_sqr(t1, t2);
49
621
    ossl_gf_mul(t2, t1, x); /* not direct to y in case of alias. */
50
621
    gf_copy(y, t2);
51
621
}
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
25.8k
{
59
25.8k
    gf a, b, c, d;
60
61
25.8k
    ossl_gf_sqr(c, q->x);
62
25.8k
    ossl_gf_sqr(a, q->y);
63
25.8k
    gf_add_nr(d, c, a); /* 2+e */
64
25.8k
    gf_add_nr(p->t, q->y, q->x); /* 2+e */
65
25.8k
    ossl_gf_sqr(b, p->t);
66
25.8k
    gf_subx_nr(b, b, d, 3); /* 4+e */
67
25.8k
    gf_sub_nr(p->t, a, c); /* 3+e */
68
25.8k
    ossl_gf_sqr(p->x, q->z);
69
25.8k
    gf_add_nr(p->z, p->x, p->x); /* 2+e */
70
25.8k
    gf_subx_nr(a, p->z, p->t, 4); /* 6+e */
71
25.8k
    if (GF_HEADROOM == 5)
72
0
        gf_weak_reduce(a); /* or 1+e */
73
25.8k
    ossl_gf_mul(p->x, a, b);
74
25.8k
    ossl_gf_mul(p->z, p->t, a);
75
25.8k
    ossl_gf_mul(p->y, p->t, d);
76
25.8k
    if (!before_double)
77
13.6k
        ossl_gf_mul(p->t, b, d);
78
25.8k
}
79
80
void ossl_curve448_point_double(curve448_point_t p, const curve448_point_t q)
81
36
{
82
36
    point_double_internal(p, q, 0);
83
36
}
84
85
/* Operations on [p]niels */
86
static ossl_inline void cond_neg_niels(niels_t n, mask_t neg)
87
52.9k
{
88
52.9k
    gf_cond_swap(n->a, n->b, neg);
89
52.9k
    gf_cond_neg(n->c, neg);
90
52.9k
}
91
92
static void pt_to_pniels(pniels_t b, const curve448_point_t a)
93
324
{
94
324
    gf_sub(b->n->a, a->y, a->x);
95
324
    gf_add(b->n->b, a->x, a->y);
96
324
    gf_mulw(b->n->c, a->t, 2 * TWISTED_D);
97
324
    gf_add(b->z, a->z, a->z);
98
324
}
99
100
static void pniels_to_pt(curve448_point_t e, const pniels_t d)
101
29
{
102
29
    gf eu;
103
104
29
    gf_add(eu, d->n->b, d->n->a);
105
29
    gf_sub(e->y, d->n->b, d->n->a);
106
29
    ossl_gf_mul(e->t, e->y, eu);
107
29
    ossl_gf_mul(e->x, d->z, e->y);
108
29
    ossl_gf_mul(e->y, d->z, eu);
109
29
    ossl_gf_sqr(e->z, d->z);
110
29
}
111
112
static void niels_to_pt(curve448_point_t e, const niels_t n)
113
595
{
114
595
    gf_add(e->y, n->b, n->a);
115
595
    gf_sub(e->x, n->b, n->a);
116
595
    ossl_gf_mul(e->t, e->y, e->x);
117
595
    gf_copy(e->z, ONE);
118
595
}
119
120
static void add_niels_to_pt(curve448_point_t d, const niels_t e,
121
    int before_double)
122
54.7k
{
123
54.7k
    gf a, b, c;
124
125
54.7k
    gf_sub_nr(b, d->y, d->x); /* 3+e */
126
54.7k
    ossl_gf_mul(a, e->a, b);
127
54.7k
    gf_add_nr(b, d->x, d->y); /* 2+e */
128
54.7k
    ossl_gf_mul(d->y, e->b, b);
129
54.7k
    ossl_gf_mul(d->x, e->c, d->t);
130
54.7k
    gf_add_nr(c, a, d->y); /* 2+e */
131
54.7k
    gf_sub_nr(b, d->y, a); /* 3+e */
132
54.7k
    gf_sub_nr(d->y, d->z, d->x); /* 3+e */
133
54.7k
    gf_add_nr(a, d->x, d->z); /* 2+e */
134
54.7k
    ossl_gf_mul(d->z, a, d->y);
135
54.7k
    ossl_gf_mul(d->x, d->y, b);
136
54.7k
    ossl_gf_mul(d->y, a, c);
137
54.7k
    if (!before_double)
138
42.6k
        ossl_gf_mul(d->t, b, c);
139
54.7k
}
140
141
static void sub_niels_from_pt(curve448_point_t d, const niels_t e,
142
    int before_double)
143
1.67k
{
144
1.67k
    gf a, b, c;
145
146
1.67k
    gf_sub_nr(b, d->y, d->x); /* 3+e */
147
1.67k
    ossl_gf_mul(a, e->b, b);
148
1.67k
    gf_add_nr(b, d->x, d->y); /* 2+e */
149
1.67k
    ossl_gf_mul(d->y, e->a, b);
150
1.67k
    ossl_gf_mul(d->x, e->c, d->t);
151
1.67k
    gf_add_nr(c, a, d->y); /* 2+e */
152
1.67k
    gf_sub_nr(b, d->y, a); /* 3+e */
153
1.67k
    gf_add_nr(d->y, d->z, d->x); /* 2+e */
154
1.67k
    gf_sub_nr(a, d->z, d->x); /* 3+e */
155
1.67k
    ossl_gf_mul(d->z, a, d->y);
156
1.67k
    ossl_gf_mul(d->x, d->y, b);
157
1.67k
    ossl_gf_mul(d->y, a, c);
158
1.67k
    if (!before_double)
159
110
        ossl_gf_mul(d->t, b, c);
160
1.67k
}
161
162
static void add_pniels_to_pt(curve448_point_t p, const pniels_t pn,
163
    int before_double)
164
1.62k
{
165
1.62k
    gf L0;
166
167
1.62k
    ossl_gf_mul(L0, p->z, pn->z);
168
1.62k
    gf_copy(p->z, L0);
169
1.62k
    add_niels_to_pt(p, pn->n, before_double);
170
1.62k
}
171
172
static void sub_pniels_from_pt(curve448_point_t p, const pniels_t pn,
173
    int before_double)
174
1.28k
{
175
1.28k
    gf L0;
176
177
1.28k
    ossl_gf_mul(L0, p->z, pn->z);
178
1.28k
    gf_copy(p->z, L0);
179
1.28k
    sub_niels_from_pt(p, pn->n, before_double);
180
1.28k
}
181
182
c448_bool_t
183
ossl_curve448_point_eq(const curve448_point_t p,
184
    const curve448_point_t q)
185
36
{
186
36
    mask_t succ;
187
36
    gf a, b;
188
189
    /* equality mod 2-torsion compares x/y */
190
36
    ossl_gf_mul(a, p->y, q->x);
191
36
    ossl_gf_mul(b, q->y, p->x);
192
36
    succ = gf_eq(a, b);
193
194
36
    return mask_to_bool(succ);
195
36
}
196
197
c448_bool_t
198
ossl_curve448_point_valid(const curve448_point_t p)
199
78
{
200
78
    mask_t out;
201
78
    gf a, b, c;
202
203
78
    ossl_gf_mul(a, p->x, p->y);
204
78
    ossl_gf_mul(b, p->z, p->t);
205
78
    out = gf_eq(a, b);
206
78
    ossl_gf_sqr(a, p->x);
207
78
    ossl_gf_sqr(b, p->y);
208
78
    gf_sub(a, b, a);
209
78
    ossl_gf_sqr(b, p->t);
210
78
    gf_mulw(c, b, TWISTED_D);
211
78
    ossl_gf_sqr(b, p->z);
212
78
    gf_add(b, b, c);
213
78
    out &= gf_eq(a, b);
214
78
    out &= ~gf_eq(p->z, ZERO);
215
78
    return mask_to_bool(out);
216
78
}
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
52.9k
{
222
52.9k
    constant_time_lookup(ni, table, sizeof(niels_s), nelts, idx);
223
52.9k
}
224
225
void ossl_curve448_precomputed_scalarmul(curve448_point_t out,
226
    const curve448_precomputed_s *table,
227
    const curve448_scalar_t scalar)
228
588
{
229
588
    unsigned int i, j, k;
230
588
    const unsigned int n = COMBS_N, t = COMBS_T, s = COMBS_S;
231
588
    niels_t ni;
232
588
    curve448_scalar_t scalar1x;
233
234
588
    ossl_curve448_scalar_add(scalar1x, scalar, precomputed_scalarmul_adjustment);
235
588
    ossl_curve448_scalar_halve(scalar1x, scalar1x);
236
237
11.1k
    for (i = s; i > 0; i--) {
238
10.5k
        if (i != s)
239
9.99k
            point_double_internal(out, out, 0);
240
241
63.5k
        for (j = 0; j < n; j++) {
242
52.9k
            int tab = 0;
243
52.9k
            mask_t invert;
244
245
317k
            for (k = 0; k < t; k++) {
246
264k
                unsigned int bit = (i - 1) + s * (k + j * t);
247
248
264k
                if (bit < C448_SCALAR_BITS)
249
262k
                    tab |= (scalar1x->limb[bit / WBITS] >> (bit % WBITS) & 1) << k;
250
264k
            }
251
252
52.9k
            invert = (tab >> (t - 1)) - 1;
253
52.9k
            tab ^= invert;
254
52.9k
            tab &= (1 << (t - 1)) - 1;
255
256
52.9k
            constant_time_lookup_niels(ni, &table->table[j << (t - 1)],
257
52.9k
                1 << (t - 1), tab);
258
259
52.9k
            cond_neg_niels(ni, invert);
260
52.9k
            if ((i != s) || j != 0)
261
52.3k
                add_niels_to_pt(out, ni, j == n - 1 && i != 1);
262
588
            else
263
588
                niels_to_pt(out, ni);
264
52.9k
        }
265
10.5k
    }
266
267
588
    OPENSSL_cleanse(ni, sizeof(ni));
268
588
    OPENSSL_cleanse(scalar1x, sizeof(scalar1x));
269
588
}
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
24
{
275
24
    gf x, y, z, t;
276
24
    curve448_point_t q;
277
278
    /* The point is now on the twisted curve.  Move it to untwisted. */
279
24
    curve448_point_copy(q, p);
280
281
24
    {
282
        /* 4-isogeny: 2xy/(y^+x^2), (y^2-x^2)/(2z^2-y^2+x^2) */
283
24
        gf u;
284
285
24
        ossl_gf_sqr(x, q->x);
286
24
        ossl_gf_sqr(t, q->y);
287
24
        gf_add(u, x, t);
288
24
        gf_add(z, q->y, q->x);
289
24
        ossl_gf_sqr(y, z);
290
24
        gf_sub(y, y, u);
291
24
        gf_sub(z, t, x);
292
24
        ossl_gf_sqr(x, q->z);
293
24
        gf_add(t, x, x);
294
24
        gf_sub(t, t, z);
295
24
        ossl_gf_mul(x, t, y);
296
24
        ossl_gf_mul(y, z, u);
297
24
        ossl_gf_mul(z, u, t);
298
24
        OPENSSL_cleanse(u, sizeof(u));
299
24
    }
300
301
    /* Affinize */
302
24
    gf_invert(z, z, 1);
303
24
    ossl_gf_mul(t, x, z);
304
24
    ossl_gf_mul(x, y, z);
305
306
    /* Encode */
307
24
    enc[EDDSA_448_PRIVATE_BYTES - 1] = 0;
308
24
    gf_serialize(enc, x, 1);
309
24
    enc[EDDSA_448_PRIVATE_BYTES - 1] |= 0x80 & gf_lobit(t);
310
311
24
    OPENSSL_cleanse(x, sizeof(x));
312
24
    OPENSSL_cleanse(y, sizeof(y));
313
24
    OPENSSL_cleanse(z, sizeof(z));
314
24
    OPENSSL_cleanse(t, sizeof(t));
315
24
    ossl_curve448_point_destroy(q);
316
24
}
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
78
{
323
78
    uint8_t enc2[EDDSA_448_PUBLIC_BYTES];
324
78
    mask_t low;
325
78
    mask_t succ;
326
327
78
    memcpy(enc2, enc, sizeof(enc2));
328
329
78
    low = ~word_is_zero(enc2[EDDSA_448_PRIVATE_BYTES - 1] & 0x80);
330
78
    enc2[EDDSA_448_PRIVATE_BYTES - 1] &= ~0x80;
331
332
78
    succ = gf_deserialize(p->y, enc2, 1, 0);
333
78
    succ &= word_is_zero(enc2[EDDSA_448_PRIVATE_BYTES - 1]);
334
335
78
    ossl_gf_sqr(p->x, p->y);
336
78
    gf_sub(p->z, ONE, p->x); /* num = 1-y^2 */
337
78
    gf_mulw(p->t, p->x, EDWARDS_D); /* dy^2 */
338
78
    gf_sub(p->t, ONE, p->t); /* denom = 1-dy^2 or 1-d + dy^2 */
339
340
78
    ossl_gf_mul(p->x, p->z, p->t);
341
78
    succ &= gf_isr(p->t, p->x); /* 1/sqrt(num * denom) */
342
343
78
    ossl_gf_mul(p->x, p->t, p->z); /* sqrt(num / denom) */
344
78
    gf_cond_neg(p->x, gf_lobit(p->x) ^ low);
345
78
    gf_copy(p->z, ONE);
346
347
78
    {
348
78
        gf a, b, c, d;
349
350
        /* 4-isogeny 2xy/(y^2-ax^2), (y^2+ax^2)/(2-y^2-ax^2) */
351
78
        ossl_gf_sqr(c, p->x);
352
78
        ossl_gf_sqr(a, p->y);
353
78
        gf_add(d, c, a);
354
78
        gf_add(p->t, p->y, p->x);
355
78
        ossl_gf_sqr(b, p->t);
356
78
        gf_sub(b, b, d);
357
78
        gf_sub(p->t, a, c);
358
78
        ossl_gf_sqr(p->x, p->z);
359
78
        gf_add(p->z, p->x, p->x);
360
78
        gf_sub(a, p->z, d);
361
78
        ossl_gf_mul(p->x, a, b);
362
78
        ossl_gf_mul(p->z, p->t, a);
363
78
        ossl_gf_mul(p->y, p->t, d);
364
78
        ossl_gf_mul(p->t, b, d);
365
78
        OPENSSL_cleanse(a, sizeof(a));
366
78
        OPENSSL_cleanse(b, sizeof(b));
367
78
        OPENSSL_cleanse(c, sizeof(c));
368
78
        OPENSSL_cleanse(d, sizeof(d));
369
78
    }
370
371
78
    OPENSSL_cleanse(enc2, sizeof(enc2));
372
78
    assert(ossl_curve448_point_valid(p) || ~succ);
373
374
78
    return c448_succeed_if(mask_to_bool(succ));
375
78
}
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
33
{
382
33
    gf x1, x2, z2, x3, z3, t1, t2;
383
33
    int t;
384
33
    mask_t swap = 0;
385
33
    mask_t nz;
386
387
33
    (void)gf_deserialize(x1, base, 1, 0);
388
33
    gf_copy(x2, ONE);
389
33
    gf_copy(z2, ZERO);
390
33
    gf_copy(x3, x1);
391
33
    gf_copy(z3, ONE);
392
393
14.8k
    for (t = X_PRIVATE_BITS - 1; t >= 0; t--) {
394
14.7k
        uint8_t sb = scalar[t / 8];
395
14.7k
        mask_t k_t;
396
397
        /* Scalar conditioning */
398
14.7k
        if (t / 8 == 0)
399
264
            sb &= -(uint8_t)COFACTOR;
400
14.5k
        else if (t == X_PRIVATE_BITS - 1)
401
33
            sb = -1;
402
403
14.7k
        k_t = (sb >> (t % 8)) & 1;
404
14.7k
        k_t = 0 - k_t; /* set to all 0s or all 1s */
405
406
14.7k
        swap ^= k_t;
407
14.7k
        gf_cond_swap(x2, x3, swap);
408
14.7k
        gf_cond_swap(z2, z3, swap);
409
14.7k
        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
14.7k
        gf_add_nr(t1, x2, z2); /* A = x2 + z2 */ /* 2+e */
417
14.7k
        gf_sub_nr(t2, x2, z2); /* B = x2 - z2 */ /* 3+e */
418
14.7k
        gf_sub_nr(z2, x3, z3); /* D = x3 - z3 */ /* 3+e */
419
14.7k
        ossl_gf_mul(x2, t1, z2); /* DA */
420
14.7k
        gf_add_nr(z2, z3, x3); /* C = x3 + z3 */ /* 2+e */
421
14.7k
        ossl_gf_mul(x3, t2, z2); /* CB */
422
14.7k
        gf_sub_nr(z3, x2, x3); /* DA-CB */ /* 3+e */
423
14.7k
        ossl_gf_sqr(z2, z3); /* (DA-CB)^2 */
424
14.7k
        ossl_gf_mul(z3, x1, z2); /* z3 = x1(DA-CB)^2 */
425
14.7k
        gf_add_nr(z2, x2, x3); /* (DA+CB) */ /* 2+e */
426
14.7k
        ossl_gf_sqr(x3, z2); /* x3 = (DA+CB)^2 */
427
428
14.7k
        ossl_gf_sqr(z2, t1); /* AA = A^2 */
429
14.7k
        ossl_gf_sqr(t1, t2); /* BB = B^2 */
430
14.7k
        ossl_gf_mul(x2, z2, t1); /* x2 = AA*BB */
431
14.7k
        gf_sub_nr(t2, z2, t1); /* E = AA-BB */ /* 3+e */
432
433
14.7k
        gf_mulw(t1, t2, -EDWARDS_D); /* E*-d = a24*E */
434
14.7k
        gf_add_nr(t1, t1, z2); /* AA + a24*E */ /* 2+e */
435
14.7k
        ossl_gf_mul(z2, t2, t1); /* z2 = E(AA+a24*E) */
436
14.7k
    }
437
438
    /* Finish */
439
33
    gf_cond_swap(x2, x3, swap);
440
33
    gf_cond_swap(z2, z3, swap);
441
33
    gf_invert(z2, z2, 0);
442
33
    ossl_gf_mul(x1, x2, z2);
443
33
    gf_serialize(out, x1, 1);
444
33
    nz = ~gf_eq(x1, ZERO);
445
446
33
    OPENSSL_cleanse(x1, sizeof(x1));
447
33
    OPENSSL_cleanse(x2, sizeof(x2));
448
33
    OPENSSL_cleanse(z2, sizeof(z2));
449
33
    OPENSSL_cleanse(x3, sizeof(x3));
450
33
    OPENSSL_cleanse(z3, sizeof(z3));
451
33
    OPENSSL_cleanse(t1, sizeof(t1));
452
33
    OPENSSL_cleanse(t2, sizeof(t2));
453
454
33
    return c448_succeed_if(mask_to_bool(nz));
455
33
}
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
564
{
461
564
    curve448_point_t q;
462
463
564
    curve448_point_copy(q, p);
464
564
    gf_invert(q->t, q->x, 0); /* 1/x */
465
564
    ossl_gf_mul(q->z, q->t, q->y); /* y/x */
466
564
    ossl_gf_sqr(q->y, q->z); /* (y/x)^2 */
467
564
    gf_serialize(out, q->y, 1);
468
564
    ossl_curve448_point_destroy(q);
469
564
}
470
471
void ossl_x448_derive_public_key(uint8_t out[X_PUBLIC_BYTES],
472
    const uint8_t scalar[X_PRIVATE_BYTES])
473
564
{
474
    /* Scalar conditioning */
475
564
    uint8_t scalar2[X_PRIVATE_BYTES];
476
564
    curve448_scalar_t the_scalar;
477
564
    curve448_point_t p;
478
564
    unsigned int i;
479
480
564
    memcpy(scalar2, scalar, sizeof(scalar2));
481
564
    scalar2[0] &= -(uint8_t)COFACTOR;
482
483
564
    scalar2[X_PRIVATE_BYTES - 1] &= ~((0u - 1u) << ((X_PRIVATE_BITS + 7) % 8));
484
564
    scalar2[X_PRIVATE_BYTES - 1] |= 1 << ((X_PRIVATE_BITS + 7) % 8);
485
486
564
    ossl_curve448_scalar_decode_long(the_scalar, scalar2, sizeof(scalar2));
487
488
    /* Compensate for the encoding ratio */
489
1.12k
    for (i = 1; i < X448_ENCODE_RATIO; i <<= 1)
490
564
        ossl_curve448_scalar_halve(the_scalar, the_scalar);
491
492
564
    ossl_curve448_precomputed_scalarmul(p, ossl_curve448_precomputed_base,
493
564
        the_scalar);
494
564
    ossl_curve448_point_mul_by_ratio_and_encode_like_x448(out, p);
495
564
    ossl_curve448_point_destroy(p);
496
564
}
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
0
#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
0
{
548
0
    unsigned int table_size = C448_SCALAR_BITS / (table_bits + 1) + 3;
549
0
    int position = table_size - 1; /* at the end */
550
0
    uint64_t current = scalar->limb[0] & 0xFFFF;
551
0
    uint32_t mask = (1 << (table_bits + 1)) - 1;
552
0
    unsigned int w;
553
0
    const unsigned int B_OVER_16 = sizeof(scalar->limb[0]) / 2;
554
0
    unsigned int n, i;
555
556
    /* place the end marker */
557
0
    control[position].power = -1;
558
0
    control[position].addend = 0;
559
0
    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
0
    for (w = 1; w < (C448_SCALAR_BITS - 1) / 16 + 3; w++) {
568
0
        if (w < (C448_SCALAR_BITS - 1) / 16 + 1) {
569
            /* Refill the 16 high bits of current */
570
0
            current += (uint32_t)((scalar->limb[w / B_OVER_16]
571
0
                                      >> (16 * (w % B_OVER_16)))
572
0
                << 16);
573
0
        }
574
575
0
        while (current & 0xFFFF) {
576
0
            uint32_t pos = NUMTRAILINGZEROS((uint32_t)current);
577
0
            uint32_t odd = (uint32_t)current >> pos;
578
0
            int32_t delta = odd & mask;
579
580
0
            assert(position >= 0);
581
0
            if (odd & (1 << (table_bits + 1)))
582
0
                delta -= (1 << (table_bits + 1));
583
            /*
584
             * Coverity gets confused by the value of pos, thinking it might be
585
             * 32.  This would require current & 0xFFFF to be zero which isn't
586
             * possible.  Suppress this false positive, since adding a check
587
             * isn't desirable.
588
             */
589
            /* coverity[overflow_before_widen] */
590
0
            current -= delta * (1 << pos);
591
0
            control[position].power = pos + 16 * (w - 1);
592
0
            control[position].addend = delta;
593
0
            position--;
594
0
        }
595
0
        current >>= 16;
596
0
    }
597
0
    assert(current == 0);
598
599
0
    position++;
600
0
    n = table_size - position;
601
0
    for (i = 0; i < n; i++)
602
0
        control[i] = control[i + position];
603
604
0
    return n - 1;
605
0
}
606
607
static void prepare_wnaf_table(pniels_t *output,
608
    const curve448_point_t working,
609
    unsigned int tbits)
610
36
{
611
36
    curve448_point_t tmp;
612
36
    int i;
613
36
    pniels_t twop;
614
615
36
    pt_to_pniels(output[0], working);
616
617
36
    if (tbits == 0)
618
0
        return;
619
620
36
    ossl_curve448_point_double(tmp, working);
621
36
    pt_to_pniels(twop, tmp);
622
623
36
    add_pniels_to_pt(tmp, output[0], 0);
624
36
    pt_to_pniels(output[1], tmp);
625
626
252
    for (i = 2; i < 1 << tbits; i++) {
627
216
        add_pniels_to_pt(tmp, twop, 0);
628
216
        pt_to_pniels(output[i], tmp);
629
216
    }
630
631
36
    ossl_curve448_point_destroy(tmp);
632
36
    OPENSSL_cleanse(twop, sizeof(twop));
633
36
}
634
635
void ossl_curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
636
    const curve448_scalar_t scalar1,
637
    const curve448_point_t base2,
638
    const curve448_scalar_t scalar2)
639
36
{
640
36
    const int table_bits_var = C448_WNAF_VAR_TABLE_BITS;
641
36
    const int table_bits_pre = C448_WNAF_FIXED_TABLE_BITS;
642
36
    struct smvt_control control_var[C448_SCALAR_BITS / (C448_WNAF_VAR_TABLE_BITS + 1) + 3];
643
36
    struct smvt_control control_pre[C448_SCALAR_BITS / (C448_WNAF_FIXED_TABLE_BITS + 1) + 3];
644
36
    int ncb_pre = recode_wnaf(control_pre, scalar1, table_bits_pre);
645
36
    int ncb_var = recode_wnaf(control_var, scalar2, table_bits_var);
646
36
    pniels_t precmp_var[1 << C448_WNAF_VAR_TABLE_BITS];
647
36
    int contp = 0, contv = 0, i;
648
649
36
    prepare_wnaf_table(precmp_var, base2, table_bits_var);
650
36
    i = control_var[0].power;
651
652
36
    if (i < 0) {
653
0
        curve448_point_copy(combo, ossl_curve448_point_identity);
654
0
        return;
655
0
    }
656
36
    if (i > control_pre[0].power) {
657
25
        pniels_to_pt(combo, precmp_var[control_var[0].addend >> 1]);
658
25
        contv++;
659
25
    } else if (i == control_pre[0].power && i >= 0) {
660
4
        pniels_to_pt(combo, precmp_var[control_var[0].addend >> 1]);
661
4
        add_niels_to_pt(combo,
662
4
            ossl_curve448_wnaf_base[control_pre[0].addend >> 1],
663
4
            i);
664
4
        contv++;
665
4
        contp++;
666
7
    } else {
667
7
        i = control_pre[0].power;
668
7
        niels_to_pt(combo, ossl_curve448_wnaf_base[control_pre[0].addend >> 1]);
669
7
        contp++;
670
7
    }
671
672
15.8k
    for (i--; i >= 0; i--) {
673
15.8k
        int cv = (i == control_var[contv].power);
674
15.8k
        int cp = (i == control_pre[contp].power);
675
676
15.8k
        point_double_internal(combo, combo, i && !(cv || cp));
677
678
15.8k
        if (cv) {
679
2.65k
            assert(control_var[contv].addend);
680
681
2.65k
            if (control_var[contv].addend > 0)
682
1.36k
                add_pniels_to_pt(combo,
683
1.36k
                    precmp_var[control_var[contv].addend >> 1],
684
1.36k
                    i && !cp);
685
1.28k
            else
686
1.28k
                sub_pniels_from_pt(combo,
687
1.28k
                    precmp_var[(-control_var[contv].addend)
688
1.28k
                        >> 1],
689
1.28k
                    i && !cp);
690
2.65k
            contv++;
691
2.65k
        }
692
693
15.8k
        if (cp) {
694
1.16k
            assert(control_pre[contp].addend);
695
696
1.16k
            if (control_pre[contp].addend > 0)
697
776
                add_niels_to_pt(combo,
698
776
                    ossl_curve448_wnaf_base[control_pre[contp].addend
699
776
                        >> 1],
700
776
                    i);
701
389
            else
702
389
                sub_niels_from_pt(combo,
703
389
                    ossl_curve448_wnaf_base[(-control_pre
704
389
                                                    [contp]
705
389
                                                        .addend)
706
389
                        >> 1],
707
389
                    i);
708
1.16k
            contp++;
709
1.16k
        }
710
15.8k
    }
711
712
    /* This function is non-secret, but whatever this is cheap. */
713
36
    OPENSSL_cleanse(control_var, sizeof(control_var));
714
36
    OPENSSL_cleanse(control_pre, sizeof(control_pre));
715
36
    OPENSSL_cleanse(precmp_var, sizeof(precmp_var));
716
717
36
    assert(contv == ncb_var);
718
36
    (void)ncb_var;
719
36
    assert(contp == ncb_pre);
720
36
    (void)ncb_pre;
721
36
}
722
723
void ossl_curve448_point_destroy(curve448_point_t point)
724
1.21k
{
725
1.21k
    OPENSSL_cleanse(point, sizeof(curve448_point_t));
726
1.21k
}
727
728
int ossl_x448(uint8_t out_shared_key[56], const uint8_t private_key[56],
729
    const uint8_t peer_public_value[56])
730
33
{
731
33
    return ossl_x448_int(out_shared_key, peer_public_value, private_key)
732
33
        == C448_SUCCESS;
733
33
}
734
735
void ossl_x448_public_from_private(uint8_t out_public_value[56],
736
    const uint8_t private_key[56])
737
564
{
738
564
    ossl_x448_derive_public_key(out_public_value, private_key);
739
564
}