Coverage Report

Created: 2025-12-31 06:58

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
621
{
23
621
    unsigned int j = 0, fill = 0;
24
621
    dword_t buffer = 0;
25
621
    int i;
26
621
    gf red;
27
28
621
    gf_copy(red, x);
29
621
    gf_strong_reduce(red);
30
621
    if (!with_hibit)
31
621
        assert(gf_hibit(red) == 0);
32
33
35.3k
    for (i = 0; i < (with_hibit ? X_SER_BYTES : SER_BYTES); i++) {
34
34.7k
        if (fill < 8 && j < NLIMBS) {
35
4.96k
            buffer |= ((dword_t)red->limb[LIMBPERM(j)]) << fill;
36
4.96k
            fill += LIMB_PLACE_VALUE(LIMBPERM(j));
37
4.96k
            j++;
38
4.96k
        }
39
34.7k
        serial[i] = (uint8_t)buffer;
40
34.7k
        fill -= 8;
41
34.7k
        buffer >>= 8;
42
34.7k
    }
43
621
}
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
102
{
58
102
    gf y;
59
60
102
    gf_copy(y, x);
61
102
    gf_strong_reduce(y);
62
102
    return 0 - (y->limb[0] & 1);
63
102
}
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
111
{
69
111
    unsigned int j = 0, fill = 0;
70
111
    dword_t buffer = 0;
71
111
    dsword_t scarry = 0;
72
111
    const unsigned nbytes = with_hibit ? X_SER_BYTES : SER_BYTES;
73
111
    unsigned int i;
74
111
    mask_t succ;
75
76
999
    for (i = 0; i < NLIMBS; i++) {
77
7.10k
        while (fill < LIMB_PLACE_VALUE(LIMBPERM(i)) && j < nbytes) {
78
6.21k
            uint8_t sj;
79
80
6.21k
            sj = serial[j];
81
6.21k
            if (j == nbytes - 1)
82
111
                sj &= ~hi_nmask;
83
6.21k
            buffer |= ((dword_t)sj) << fill;
84
6.21k
            fill += 8;
85
6.21k
            j++;
86
6.21k
        }
87
888
        x->limb[LIMBPERM(i)] = (word_t)((i < NLIMBS - 1) ? buffer & LIMB_MASK(LIMBPERM(i)) : buffer);
88
888
        fill -= LIMB_PLACE_VALUE(LIMBPERM(i));
89
888
        buffer >>= LIMB_PLACE_VALUE(LIMBPERM(i));
90
888
        scarry = (scarry + x->limb[LIMBPERM(i)] - MODULUS->limb[LIMBPERM(i)]) >> (8 * sizeof(word_t));
91
888
    }
92
111
    succ = with_hibit ? 0 - (mask_t)1 : ~gf_hibit(x);
93
111
    return succ & word_is_zero((word_t)buffer) & ~word_is_zero((word_t)scarry);
94
111
}
95
96
/* Reduce to canonical form. */
97
void gf_strong_reduce(gf a)
98
1.72k
{
99
1.72k
    dsword_t scarry;
100
1.72k
    word_t scarry_0;
101
1.72k
    dword_t carry = 0;
102
1.72k
    unsigned int i;
103
104
    /* first, clear high */
105
1.72k
    gf_weak_reduce(a); /* Determined to have negligible perf impact. */
106
107
    /* now the total is less than 2p */
108
109
    /* compute total_value - p.  No need to reduce mod p. */
110
1.72k
    scarry = 0;
111
15.5k
    for (i = 0; i < NLIMBS; i++) {
112
13.8k
        scarry = scarry + a->limb[LIMBPERM(i)] - MODULUS->limb[LIMBPERM(i)];
113
13.8k
        a->limb[LIMBPERM(i)] = scarry & LIMB_MASK(LIMBPERM(i));
114
13.8k
        scarry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
115
13.8k
    }
116
117
    /*
118
     * uncommon case: it was >= p, so now scarry = 0 and this = x common case:
119
     * it was < p, so now scarry = -1 and this = x - p + 2^255 so let's add
120
     * back in p.  will carry back off the top for 2^255.
121
     */
122
1.72k
    assert(scarry == 0 || scarry == -1);
123
124
1.72k
    scarry_0 = (word_t)scarry;
125
126
    /* add it back */
127
15.5k
    for (i = 0; i < NLIMBS; i++) {
128
13.8k
        carry = carry + a->limb[LIMBPERM(i)] + (scarry_0 & MODULUS->limb[LIMBPERM(i)]);
129
13.8k
        a->limb[LIMBPERM(i)] = carry & LIMB_MASK(LIMBPERM(i));
130
13.8k
        carry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
131
13.8k
    }
132
133
1.72k
    assert(carry < 2 && ((word_t)carry + scarry_0) == 0);
134
1.72k
}
135
136
/* Subtract two gf elements d=a-b */
137
void gf_sub(gf d, const gf a, const gf b)
138
55.9k
{
139
55.9k
    gf_sub_RAW(d, a, b);
140
55.9k
    gf_bias(d, 2);
141
55.9k
    gf_weak_reduce(d);
142
55.9k
}
143
144
/* Add two field elements d = a+b */
145
void gf_add(gf d, const gf a, const gf b)
146
1.65k
{
147
1.65k
    gf_add_RAW(d, a, b);
148
1.65k
    gf_weak_reduce(d);
149
1.65k
}
150
151
/* Compare a==b */
152
mask_t gf_eq(const gf a, const gf b)
153
1.00k
{
154
1.00k
    gf c;
155
1.00k
    mask_t ret = 0;
156
1.00k
    unsigned int i;
157
158
1.00k
    gf_sub(c, a, b);
159
1.00k
    gf_strong_reduce(c);
160
161
9.01k
    for (i = 0; i < NLIMBS; i++)
162
8.01k
        ret |= c->limb[LIMBPERM(i)];
163
164
1.00k
    return word_is_zero(ret);
165
1.00k
}
166
167
mask_t gf_isr(gf a, const gf x)
168
699
{
169
699
    gf L0, L1, L2;
170
171
699
    ossl_gf_sqr(L1, x);
172
699
    ossl_gf_mul(L2, x, L1);
173
699
    ossl_gf_sqr(L1, L2);
174
699
    ossl_gf_mul(L2, x, L1);
175
699
    gf_sqrn(L1, L2, 3);
176
699
    ossl_gf_mul(L0, L2, L1);
177
699
    gf_sqrn(L1, L0, 3);
178
699
    ossl_gf_mul(L0, L2, L1);
179
699
    gf_sqrn(L2, L0, 9);
180
699
    ossl_gf_mul(L1, L0, L2);
181
699
    ossl_gf_sqr(L0, L1);
182
699
    ossl_gf_mul(L2, x, L0);
183
699
    gf_sqrn(L0, L2, 18);
184
699
    ossl_gf_mul(L2, L1, L0);
185
699
    gf_sqrn(L0, L2, 37);
186
699
    ossl_gf_mul(L1, L2, L0);
187
699
    gf_sqrn(L0, L1, 37);
188
699
    ossl_gf_mul(L1, L2, L0);
189
699
    gf_sqrn(L0, L1, 111);
190
699
    ossl_gf_mul(L2, L1, L0);
191
699
    ossl_gf_sqr(L0, L2);
192
699
    ossl_gf_mul(L1, x, L0);
193
699
    gf_sqrn(L0, L1, 223);
194
699
    ossl_gf_mul(L1, L2, L0);
195
699
    ossl_gf_sqr(L2, L1);
196
699
    ossl_gf_mul(L0, L2, x);
197
699
    gf_copy(a, L1);
198
699
    return gf_eq(L0, ONE);
199
699
}