Coverage Report

Created: 2025-08-28 07:07

/src/openssl33/crypto/ec/curve448/f_generic.c
Line
Count
Source (jump to first uncovered line)
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 "field.h"
13
14
static const gf MODULUS = {
15
    FIELD_LITERAL(0xffffffffffffffULL, 0xffffffffffffffULL, 0xffffffffffffffULL,
16
                  0xffffffffffffffULL, 0xfffffffffffffeULL, 0xffffffffffffffULL,
17
                  0xffffffffffffffULL, 0xffffffffffffffULL)
18
};
19
20
/* Serialize to wire format. */
21
void gf_serialize(uint8_t serial[SER_BYTES], const gf x, int with_hibit)
22
508
{
23
508
    unsigned int j = 0, fill = 0;
24
508
    dword_t buffer = 0;
25
508
    int i;
26
508
    gf red;
27
28
508
    gf_copy(red, x);
29
508
    gf_strong_reduce(red);
30
508
    if (!with_hibit)
31
0
        assert(gf_hibit(red) == 0);
32
33
28.9k
    for (i = 0; i < (with_hibit ? X_SER_BYTES : SER_BYTES); i++) {
34
28.4k
        if (fill < 8 && j < NLIMBS) {
35
4.06k
            buffer |= ((dword_t) red->limb[LIMBPERM(j)]) << fill;
36
4.06k
            fill += LIMB_PLACE_VALUE(LIMBPERM(j));
37
4.06k
            j++;
38
4.06k
        }
39
28.4k
        serial[i] = (uint8_t)buffer;
40
28.4k
        fill -= 8;
41
28.4k
        buffer >>= 8;
42
28.4k
    }
43
508
}
44
45
/* Return high bit of x = low bit of 2x mod p */
46
mask_t gf_hibit(const gf x)
47
0
{
48
0
    gf y;
49
50
0
    gf_add(y, x, x);
51
0
    gf_strong_reduce(y);
52
0
    return 0 - (y->limb[0] & 1);
53
0
}
54
55
/* Return high bit of x = low bit of 2x mod p */
56
mask_t gf_lobit(const gf x)
57
219
{
58
219
    gf y;
59
60
219
    gf_copy(y, x);
61
219
    gf_strong_reduce(y);
62
219
    return 0 - (y->limb[0] & 1);
63
219
}
64
65
/* Deserialize from wire format; return -1 on success and 0 on failure. */
66
mask_t gf_deserialize(gf x, const uint8_t serial[SER_BYTES], int with_hibit,
67
                      uint8_t hi_nmask)
68
193
{
69
193
    unsigned int j = 0, fill = 0;
70
193
    dword_t buffer = 0;
71
193
    dsword_t scarry = 0;
72
193
    const unsigned nbytes = with_hibit ? X_SER_BYTES : SER_BYTES;
73
193
    unsigned int i;
74
193
    mask_t succ;
75
76
1.73k
    for (i = 0; i < NLIMBS; i++) {
77
12.3k
        while (fill < LIMB_PLACE_VALUE(LIMBPERM(i)) && j < nbytes) {
78
10.8k
            uint8_t sj;
79
80
10.8k
            sj = serial[j];
81
10.8k
            if (j == nbytes - 1)
82
193
                sj &= ~hi_nmask;
83
10.8k
            buffer |= ((dword_t) sj) << fill;
84
10.8k
            fill += 8;
85
10.8k
            j++;
86
10.8k
        }
87
1.54k
        x->limb[LIMBPERM(i)] = (word_t)
88
1.54k
            ((i < NLIMBS - 1) ? buffer & LIMB_MASK(LIMBPERM(i)) : buffer);
89
1.54k
        fill -= LIMB_PLACE_VALUE(LIMBPERM(i));
90
1.54k
        buffer >>= LIMB_PLACE_VALUE(LIMBPERM(i));
91
1.54k
        scarry =
92
1.54k
            (scarry + x->limb[LIMBPERM(i)] -
93
1.54k
             MODULUS->limb[LIMBPERM(i)]) >> (8 * sizeof(word_t));
94
1.54k
    }
95
193
    succ = with_hibit ? 0 - (mask_t) 1 : ~gf_hibit(x);
96
193
    return succ & word_is_zero((word_t)buffer) & ~word_is_zero((word_t)scarry);
97
193
}
98
99
/* Reduce to canonical form. */
100
void gf_strong_reduce(gf a)
101
2.03k
{
102
2.03k
    dsword_t scarry;
103
2.03k
    word_t scarry_0;
104
2.03k
    dword_t carry = 0;
105
2.03k
    unsigned int i;
106
107
    /* first, clear high */
108
2.03k
    gf_weak_reduce(a);          /* Determined to have negligible perf impact. */
109
110
    /* now the total is less than 2p */
111
112
    /* compute total_value - p.  No need to reduce mod p. */
113
2.03k
    scarry = 0;
114
18.3k
    for (i = 0; i < NLIMBS; i++) {
115
16.3k
        scarry = scarry + a->limb[LIMBPERM(i)] - MODULUS->limb[LIMBPERM(i)];
116
16.3k
        a->limb[LIMBPERM(i)] = scarry & LIMB_MASK(LIMBPERM(i));
117
16.3k
        scarry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
118
16.3k
    }
119
120
    /*
121
     * uncommon case: it was >= p, so now scarry = 0 and this = x common case:
122
     * it was < p, so now scarry = -1 and this = x - p + 2^255 so let's add
123
     * back in p.  will carry back off the top for 2^255.
124
     */
125
2.03k
    assert(scarry == 0 || scarry == -1);
126
127
2.03k
    scarry_0 = (word_t)scarry;
128
129
    /* add it back */
130
18.3k
    for (i = 0; i < NLIMBS; i++) {
131
16.3k
        carry =
132
16.3k
            carry + a->limb[LIMBPERM(i)] +
133
16.3k
            (scarry_0 & MODULUS->limb[LIMBPERM(i)]);
134
16.3k
        a->limb[LIMBPERM(i)] = carry & LIMB_MASK(LIMBPERM(i));
135
16.3k
        carry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
136
16.3k
    }
137
138
2.03k
    assert(carry < 2 && ((word_t)carry + scarry_0) == 0);
139
2.03k
}
140
141
/* Subtract two gf elements d=a-b */
142
void gf_sub(gf d, const gf a, const gf b)
143
49.3k
{
144
49.3k
    gf_sub_RAW(d, a, b);
145
49.3k
    gf_bias(d, 2);
146
49.3k
    gf_weak_reduce(d);
147
49.3k
}
148
149
/* Add two field elements d = a+b */
150
void gf_add(gf d, const gf a, const gf b)
151
2.84k
{
152
2.84k
    gf_add_RAW(d, a, b);
153
2.84k
    gf_weak_reduce(d);
154
2.84k
}
155
156
/* Compare a==b */
157
mask_t gf_eq(const gf a, const gf b)
158
1.31k
{
159
1.31k
    gf c;
160
1.31k
    mask_t ret = 0;
161
1.31k
    unsigned int i;
162
163
1.31k
    gf_sub(c, a, b);
164
1.31k
    gf_strong_reduce(c);
165
166
11.8k
    for (i = 0; i < NLIMBS; i++)
167
10.4k
        ret |= c->limb[LIMBPERM(i)];
168
169
1.31k
    return word_is_zero(ret);
170
1.31k
}
171
172
mask_t gf_isr(gf a, const gf x)
173
685
{
174
685
    gf L0, L1, L2;
175
176
685
    ossl_gf_sqr(L1, x);
177
685
    ossl_gf_mul(L2, x, L1);
178
685
    ossl_gf_sqr(L1, L2);
179
685
    ossl_gf_mul(L2, x, L1);
180
685
    gf_sqrn(L1, L2, 3);
181
685
    ossl_gf_mul(L0, L2, L1);
182
685
    gf_sqrn(L1, L0, 3);
183
685
    ossl_gf_mul(L0, L2, L1);
184
685
    gf_sqrn(L2, L0, 9);
185
685
    ossl_gf_mul(L1, L0, L2);
186
685
    ossl_gf_sqr(L0, L1);
187
685
    ossl_gf_mul(L2, x, L0);
188
685
    gf_sqrn(L0, L2, 18);
189
685
    ossl_gf_mul(L2, L1, L0);
190
685
    gf_sqrn(L0, L2, 37);
191
685
    ossl_gf_mul(L1, L2, L0);
192
685
    gf_sqrn(L0, L1, 37);
193
685
    ossl_gf_mul(L1, L2, L0);
194
685
    gf_sqrn(L0, L1, 111);
195
685
    ossl_gf_mul(L2, L1, L0);
196
685
    ossl_gf_sqr(L0, L2);
197
685
    ossl_gf_mul(L1, x, L0);
198
685
    gf_sqrn(L0, L1, 223);
199
685
    ossl_gf_mul(L1, L2, L0);
200
685
    ossl_gf_sqr(L2, L1);
201
685
    ossl_gf_mul(L0, L2, x);
202
685
    gf_copy(a, L1);
203
685
    return gf_eq(L0, ONE);
204
685
}