Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/ec/curve448/f_generic.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 "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
429
{
23
429
    unsigned int j = 0, fill = 0;
24
429
    dword_t buffer = 0;
25
429
    int i;
26
429
    gf red;
27
28
429
    gf_copy(red, x);
29
429
    gf_strong_reduce(red);
30
429
    if (!with_hibit)
31
429
        assert(gf_hibit(red) == 0);
32
33
24.4k
    for (i = 0; i < (with_hibit ? X_SER_BYTES : SER_BYTES); i++) {
34
24.0k
        if (fill < 8 && j < NLIMBS) {
35
3.43k
            buffer |= ((dword_t) red->limb[LIMBPERM(j)]) << fill;
36
3.43k
            fill += LIMB_PLACE_VALUE(LIMBPERM(j));
37
3.43k
            j++;
38
3.43k
        }
39
24.0k
        serial[i] = (uint8_t)buffer;
40
24.0k
        fill -= 8;
41
24.0k
        buffer >>= 8;
42
24.0k
    }
43
429
}
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
205
{
58
205
    gf y;
59
60
205
    gf_copy(y, x);
61
205
    gf_strong_reduce(y);
62
205
    return 0 - (y->limb[0] & 1);
63
205
}
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
203
{
69
203
    unsigned int j = 0, fill = 0;
70
203
    dword_t buffer = 0;
71
203
    dsword_t scarry = 0;
72
203
    const unsigned nbytes = with_hibit ? X_SER_BYTES : SER_BYTES;
73
203
    unsigned int i;
74
203
    mask_t succ;
75
76
1.82k
    for (i = 0; i < NLIMBS; i++) {
77
12.9k
        while (fill < LIMB_PLACE_VALUE(LIMBPERM(i)) && j < nbytes) {
78
11.3k
            uint8_t sj;
79
80
11.3k
            sj = serial[j];
81
11.3k
            if (j == nbytes - 1)
82
203
                sj &= ~hi_nmask;
83
11.3k
            buffer |= ((dword_t) sj) << fill;
84
11.3k
            fill += 8;
85
11.3k
            j++;
86
11.3k
        }
87
1.62k
        x->limb[LIMBPERM(i)] = (word_t)
88
1.62k
            ((i < NLIMBS - 1) ? buffer & LIMB_MASK(LIMBPERM(i)) : buffer);
89
1.62k
        fill -= LIMB_PLACE_VALUE(LIMBPERM(i));
90
1.62k
        buffer >>= LIMB_PLACE_VALUE(LIMBPERM(i));
91
1.62k
        scarry =
92
1.62k
            (scarry + x->limb[LIMBPERM(i)] -
93
1.62k
             MODULUS->limb[LIMBPERM(i)]) >> (8 * sizeof(word_t));
94
1.62k
    }
95
203
    succ = with_hibit ? 0 - (mask_t) 1 : ~gf_hibit(x);
96
203
    return succ & word_is_zero((word_t)buffer) & ~word_is_zero((word_t)scarry);
97
203
}
98
99
/* Reduce to canonical form. */
100
void gf_strong_reduce(gf a)
101
1.85k
{
102
1.85k
    dsword_t scarry;
103
1.85k
    word_t scarry_0;
104
1.85k
    dword_t carry = 0;
105
1.85k
    unsigned int i;
106
107
    /* first, clear high */
108
1.85k
    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
1.85k
    scarry = 0;
114
16.6k
    for (i = 0; i < NLIMBS; i++) {
115
14.8k
        scarry = scarry + a->limb[LIMBPERM(i)] - MODULUS->limb[LIMBPERM(i)];
116
14.8k
        a->limb[LIMBPERM(i)] = scarry & LIMB_MASK(LIMBPERM(i));
117
14.8k
        scarry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
118
14.8k
    }
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
1.85k
    assert(scarry == 0 || scarry == -1);
126
127
1.85k
    scarry_0 = (word_t)scarry;
128
129
    /* add it back */
130
16.6k
    for (i = 0; i < NLIMBS; i++) {
131
14.8k
        carry =
132
14.8k
            carry + a->limb[LIMBPERM(i)] +
133
14.8k
            (scarry_0 & MODULUS->limb[LIMBPERM(i)]);
134
14.8k
        a->limb[LIMBPERM(i)] = carry & LIMB_MASK(LIMBPERM(i));
135
14.8k
        carry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
136
14.8k
    }
137
138
1.85k
    assert(carry < 2 && ((word_t)carry + scarry_0) == 0);
139
1.85k
}
140
141
/* Subtract two gf elements d=a-b */
142
void gf_sub(gf d, const gf a, const gf b)
143
40.3k
{
144
40.3k
    gf_sub_RAW(d, a, b);
145
40.3k
    gf_bias(d, 2);
146
40.3k
    gf_weak_reduce(d);
147
40.3k
}
148
149
/* Add two field elements d = a+b */
150
void gf_add(gf d, const gf a, const gf b)
151
2.66k
{
152
2.66k
    gf_add_RAW(d, a, b);
153
2.66k
    gf_weak_reduce(d);
154
2.66k
}
155
156
/* Compare a==b */
157
mask_t gf_eq(const gf a, const gf b)
158
1.22k
{
159
1.22k
    gf c;
160
1.22k
    mask_t ret = 0;
161
1.22k
    unsigned int i;
162
163
1.22k
    gf_sub(c, a, b);
164
1.22k
    gf_strong_reduce(c);
165
166
10.9k
    for (i = 0; i < NLIMBS; i++)
167
9.76k
        ret |= c->limb[LIMBPERM(i)];
168
169
1.22k
    return word_is_zero(ret);
170
1.22k
}
171
172
mask_t gf_isr(gf a, const gf x)
173
599
{
174
599
    gf L0, L1, L2;
175
176
599
    ossl_gf_sqr(L1, x);
177
599
    ossl_gf_mul(L2, x, L1);
178
599
    ossl_gf_sqr(L1, L2);
179
599
    ossl_gf_mul(L2, x, L1);
180
599
    gf_sqrn(L1, L2, 3);
181
599
    ossl_gf_mul(L0, L2, L1);
182
599
    gf_sqrn(L1, L0, 3);
183
599
    ossl_gf_mul(L0, L2, L1);
184
599
    gf_sqrn(L2, L0, 9);
185
599
    ossl_gf_mul(L1, L0, L2);
186
599
    ossl_gf_sqr(L0, L1);
187
599
    ossl_gf_mul(L2, x, L0);
188
599
    gf_sqrn(L0, L2, 18);
189
599
    ossl_gf_mul(L2, L1, L0);
190
599
    gf_sqrn(L0, L2, 37);
191
599
    ossl_gf_mul(L1, L2, L0);
192
599
    gf_sqrn(L0, L1, 37);
193
599
    ossl_gf_mul(L1, L2, L0);
194
599
    gf_sqrn(L0, L1, 111);
195
599
    ossl_gf_mul(L2, L1, L0);
196
599
    ossl_gf_sqr(L0, L2);
197
599
    ossl_gf_mul(L1, x, L0);
198
599
    gf_sqrn(L0, L1, 223);
199
599
    ossl_gf_mul(L1, L2, L0);
200
599
    ossl_gf_sqr(L2, L1);
201
599
    ossl_gf_mul(L0, L2, x);
202
599
    gf_copy(a, L1);
203
599
    return gf_eq(L0, ONE);
204
599
}