Coverage Report

Created: 2026-11-15 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nss/lib/freebl/ecl/ecp_secp521r1.c
Line
Count
Source
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#ifdef FREEBL_NO_DEPEND
6
#include "../stubs.h"
7
#endif
8
9
#include "ecl-priv.h"
10
#include "secitem.h"
11
#include "secerr.h"
12
#include "secmpi.h"
13
#include "../verified/Hacl_P521.h"
14
15
/*
16
 * Point Validation for P-521.
17
 */
18
19
SECStatus
20
ec_secp521r1_pt_validate(const SECItem *pt)
21
3.38k
{
22
3.38k
    SECStatus res = SECSuccess;
23
3.38k
    if (!pt || !pt->data) {
24
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
25
0
        res = SECFailure;
26
0
        return res;
27
0
    }
28
29
3.38k
    if (pt->len != 133) {
30
200
        PORT_SetError(SEC_ERROR_BAD_KEY);
31
200
        res = SECFailure;
32
200
        return res;
33
200
    }
34
35
3.18k
    if (pt->data[0] != EC_POINT_FORM_UNCOMPRESSED) {
36
68
        PORT_SetError(SEC_ERROR_UNSUPPORTED_EC_POINT_FORM);
37
68
        res = SECFailure;
38
68
        return res;
39
68
    }
40
41
#ifndef UNSAFE_FUZZER_MODE
42
1.56k
    bool b = Hacl_P521_validate_public_key(pt->data + 1);
43
#else
44
1.56k
    bool b = PR_TRUE;
45
#endif
46
47
3.12k
    if (!b) {
48
1.08k
        PORT_SetError(SEC_ERROR_BAD_KEY);
49
1.08k
        res = SECFailure;
50
1.08k
    }
51
1.56k
    return res;
52
3.18k
}
ec_secp521r1_pt_validate
Line
Count
Source
21
1.69k
{
22
1.69k
    SECStatus res = SECSuccess;
23
1.69k
    if (!pt || !pt->data) {
24
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
25
0
        res = SECFailure;
26
0
        return res;
27
0
    }
28
29
1.69k
    if (pt->len != 133) {
30
100
        PORT_SetError(SEC_ERROR_BAD_KEY);
31
100
        res = SECFailure;
32
100
        return res;
33
100
    }
34
35
1.59k
    if (pt->data[0] != EC_POINT_FORM_UNCOMPRESSED) {
36
34
        PORT_SetError(SEC_ERROR_UNSUPPORTED_EC_POINT_FORM);
37
34
        res = SECFailure;
38
34
        return res;
39
34
    }
40
41
1.56k
#ifndef UNSAFE_FUZZER_MODE
42
1.59k
    bool b = Hacl_P521_validate_public_key(pt->data + 1);
43
#else
44
    bool b = PR_TRUE;
45
#endif
46
47
1.56k
    if (!b) {
48
541
        PORT_SetError(SEC_ERROR_BAD_KEY);
49
541
        res = SECFailure;
50
541
    }
51
1.56k
    return res;
52
1.59k
}
ec_secp521r1_pt_validate
Line
Count
Source
21
1.69k
{
22
1.69k
    SECStatus res = SECSuccess;
23
1.69k
    if (!pt || !pt->data) {
24
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
25
0
        res = SECFailure;
26
0
        return res;
27
0
    }
28
29
1.69k
    if (pt->len != 133) {
30
100
        PORT_SetError(SEC_ERROR_BAD_KEY);
31
100
        res = SECFailure;
32
100
        return res;
33
100
    }
34
35
1.59k
    if (pt->data[0] != EC_POINT_FORM_UNCOMPRESSED) {
36
34
        PORT_SetError(SEC_ERROR_UNSUPPORTED_EC_POINT_FORM);
37
34
        res = SECFailure;
38
34
        return res;
39
34
    }
40
41
#ifndef UNSAFE_FUZZER_MODE
42
    bool b = Hacl_P521_validate_public_key(pt->data + 1);
43
#else
44
1.59k
    bool b = PR_TRUE;
45
1.56k
#endif
46
47
1.56k
    if (!b) {
48
541
        PORT_SetError(SEC_ERROR_BAD_KEY);
49
541
        res = SECFailure;
50
541
    }
51
1.56k
    return res;
52
1.59k
}
53
54
/*
55
 * Scalar Validation for P-521.
56
 */
57
58
SECStatus
59
ec_secp521r1_scalar_validate(const SECItem *scalar)
60
1.07k
{
61
1.07k
    SECStatus res = SECSuccess;
62
1.07k
    if (!scalar || !scalar->data) {
63
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
64
0
        res = SECFailure;
65
0
        return res;
66
0
    }
67
68
1.07k
    if (scalar->len != 66) {
69
0
        PORT_SetError(SEC_ERROR_BAD_KEY);
70
0
        res = SECFailure;
71
0
        return res;
72
0
    }
73
74
#ifndef UNSAFE_FUZZER_MODE
75
535
    bool b = Hacl_P521_validate_private_key(scalar->data);
76
#else
77
535
    bool b = PR_TRUE;
78
#endif
79
80
1.07k
    if (!b) {
81
0
        PORT_SetError(SEC_ERROR_BAD_KEY);
82
0
        res = SECFailure;
83
0
    }
84
535
    return res;
85
1.07k
}
ec_secp521r1_scalar_validate
Line
Count
Source
60
535
{
61
535
    SECStatus res = SECSuccess;
62
535
    if (!scalar || !scalar->data) {
63
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
64
0
        res = SECFailure;
65
0
        return res;
66
0
    }
67
68
535
    if (scalar->len != 66) {
69
0
        PORT_SetError(SEC_ERROR_BAD_KEY);
70
0
        res = SECFailure;
71
0
        return res;
72
0
    }
73
74
535
#ifndef UNSAFE_FUZZER_MODE
75
535
    bool b = Hacl_P521_validate_private_key(scalar->data);
76
#else
77
    bool b = PR_TRUE;
78
#endif
79
80
535
    if (!b) {
81
0
        PORT_SetError(SEC_ERROR_BAD_KEY);
82
0
        res = SECFailure;
83
0
    }
84
535
    return res;
85
535
}
ec_secp521r1_scalar_validate
Line
Count
Source
60
535
{
61
535
    SECStatus res = SECSuccess;
62
535
    if (!scalar || !scalar->data) {
63
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
64
0
        res = SECFailure;
65
0
        return res;
66
0
    }
67
68
535
    if (scalar->len != 66) {
69
0
        PORT_SetError(SEC_ERROR_BAD_KEY);
70
0
        res = SECFailure;
71
0
        return res;
72
0
    }
73
74
#ifndef UNSAFE_FUZZER_MODE
75
    bool b = Hacl_P521_validate_private_key(scalar->data);
76
#else
77
535
    bool b = PR_TRUE;
78
535
#endif
79
80
535
    if (!b) {
81
0
        PORT_SetError(SEC_ERROR_BAD_KEY);
82
0
        res = SECFailure;
83
0
    }
84
535
    return res;
85
535
}
86
87
/*
88
 * Scalar multiplication for P-521.
89
 * If P == NULL, the base point is used.
90
 * Returns X = k*P
91
 */
92
93
SECStatus
94
ec_secp521r1_pt_mul(SECItem *X, SECItem *k, SECItem *P)
95
974
{
96
974
    SECStatus res = SECSuccess;
97
974
    if (!P) {
98
755
        uint8_t derived[132] = { 0 };
99
100
755
        if (!X || !k || !X->data || !k->data ||
101
755
            X->len < 133 || k->len != 66) {
102
4
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
103
4
            res = SECFailure;
104
4
            return res;
105
4
        }
106
107
#ifndef UNSAFE_FUZZER_MODE
108
479
        bool b = Hacl_P521_dh_initiator(derived, k->data);
109
#else
110
272
        bool b = PR_TRUE;
111
#endif
112
113
751
        if (!b) {
114
9
            PORT_SetError(SEC_ERROR_BAD_KEY);
115
9
            res = SECFailure;
116
9
            return res;
117
9
        }
118
119
742
        X->len = 133;
120
742
        X->data[0] = EC_POINT_FORM_UNCOMPRESSED;
121
742
        memcpy(X->data + 1, derived, 132);
122
123
742
    } else {
124
219
        uint8_t full_key[66] = { 0 };
125
219
        uint8_t *key;
126
219
        uint8_t derived[132] = { 0 };
127
128
219
        if (!X || !k || !P || !X->data || !k->data || !P->data ||
129
219
            X->len < 66 || P->len != 133 ||
130
219
            P->data[0] != EC_POINT_FORM_UNCOMPRESSED) {
131
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
132
0
            res = SECFailure;
133
0
            return res;
134
0
        }
135
136
        /* We consider keys of up to size 66, or of size 67 with a single leading 0 */
137
219
        if (k->len < 66) {
138
0
            memcpy(full_key + 66 - k->len, k->data, k->len);
139
0
            key = full_key;
140
219
        } else if (k->len == 66) {
141
219
            key = k->data;
142
219
        } else if (k->len == 67 && k->data[0] == 0) {
143
0
            key = k->data + 1;
144
0
        } else {
145
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
146
0
            res = SECFailure;
147
0
            return res;
148
0
        }
149
150
#ifndef UNSAFE_FUZZER_MODE
151
17
        bool b = Hacl_P521_dh_responder(derived, P->data + 1, key);
152
#else
153
202
        bool b = key != NULL; /* Avoiding unused variable warnings */
154
202
#endif
155
156
219
        if (!b) {
157
0
            PORT_SetError(SEC_ERROR_BAD_KEY);
158
0
            res = SECFailure;
159
0
            return res;
160
0
        }
161
162
219
        X->len = 66;
163
219
        memcpy(X->data, derived, 66);
164
219
    }
165
166
961
    return res;
167
974
}
ec_secp521r1_pt_mul
Line
Count
Source
95
500
{
96
500
    SECStatus res = SECSuccess;
97
500
    if (!P) {
98
483
        uint8_t derived[132] = { 0 };
99
100
483
        if (!X || !k || !X->data || !k->data ||
101
483
            X->len < 133 || k->len != 66) {
102
4
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
103
4
            res = SECFailure;
104
4
            return res;
105
4
        }
106
107
479
#ifndef UNSAFE_FUZZER_MODE
108
483
        bool b = Hacl_P521_dh_initiator(derived, k->data);
109
#else
110
        bool b = PR_TRUE;
111
#endif
112
113
479
        if (!b) {
114
9
            PORT_SetError(SEC_ERROR_BAD_KEY);
115
9
            res = SECFailure;
116
9
            return res;
117
9
        }
118
119
470
        X->len = 133;
120
470
        X->data[0] = EC_POINT_FORM_UNCOMPRESSED;
121
470
        memcpy(X->data + 1, derived, 132);
122
123
470
    } else {
124
17
        uint8_t full_key[66] = { 0 };
125
17
        uint8_t *key;
126
17
        uint8_t derived[132] = { 0 };
127
128
17
        if (!X || !k || !P || !X->data || !k->data || !P->data ||
129
17
            X->len < 66 || P->len != 133 ||
130
17
            P->data[0] != EC_POINT_FORM_UNCOMPRESSED) {
131
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
132
0
            res = SECFailure;
133
0
            return res;
134
0
        }
135
136
        /* We consider keys of up to size 66, or of size 67 with a single leading 0 */
137
17
        if (k->len < 66) {
138
0
            memcpy(full_key + 66 - k->len, k->data, k->len);
139
0
            key = full_key;
140
17
        } else if (k->len == 66) {
141
17
            key = k->data;
142
17
        } else if (k->len == 67 && k->data[0] == 0) {
143
0
            key = k->data + 1;
144
0
        } else {
145
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
146
0
            res = SECFailure;
147
0
            return res;
148
0
        }
149
150
17
#ifndef UNSAFE_FUZZER_MODE
151
17
        bool b = Hacl_P521_dh_responder(derived, P->data + 1, key);
152
#else
153
        bool b = key != NULL; /* Avoiding unused variable warnings */
154
#endif
155
156
17
        if (!b) {
157
0
            PORT_SetError(SEC_ERROR_BAD_KEY);
158
0
            res = SECFailure;
159
0
            return res;
160
0
        }
161
162
17
        X->len = 66;
163
17
        memcpy(X->data, derived, 66);
164
17
    }
165
166
487
    return res;
167
500
}
ec_secp521r1_pt_mul
Line
Count
Source
95
474
{
96
474
    SECStatus res = SECSuccess;
97
474
    if (!P) {
98
272
        uint8_t derived[132] = { 0 };
99
100
272
        if (!X || !k || !X->data || !k->data ||
101
272
            X->len < 133 || k->len != 66) {
102
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
103
0
            res = SECFailure;
104
0
            return res;
105
0
        }
106
107
#ifndef UNSAFE_FUZZER_MODE
108
        bool b = Hacl_P521_dh_initiator(derived, k->data);
109
#else
110
272
        bool b = PR_TRUE;
111
272
#endif
112
113
272
        if (!b) {
114
0
            PORT_SetError(SEC_ERROR_BAD_KEY);
115
0
            res = SECFailure;
116
0
            return res;
117
0
        }
118
119
272
        X->len = 133;
120
272
        X->data[0] = EC_POINT_FORM_UNCOMPRESSED;
121
272
        memcpy(X->data + 1, derived, 132);
122
123
272
    } else {
124
202
        uint8_t full_key[66] = { 0 };
125
202
        uint8_t *key;
126
202
        uint8_t derived[132] = { 0 };
127
128
202
        if (!X || !k || !P || !X->data || !k->data || !P->data ||
129
202
            X->len < 66 || P->len != 133 ||
130
202
            P->data[0] != EC_POINT_FORM_UNCOMPRESSED) {
131
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
132
0
            res = SECFailure;
133
0
            return res;
134
0
        }
135
136
        /* We consider keys of up to size 66, or of size 67 with a single leading 0 */
137
202
        if (k->len < 66) {
138
0
            memcpy(full_key + 66 - k->len, k->data, k->len);
139
0
            key = full_key;
140
202
        } else if (k->len == 66) {
141
202
            key = k->data;
142
202
        } else if (k->len == 67 && k->data[0] == 0) {
143
0
            key = k->data + 1;
144
0
        } else {
145
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
146
0
            res = SECFailure;
147
0
            return res;
148
0
        }
149
150
#ifndef UNSAFE_FUZZER_MODE
151
        bool b = Hacl_P521_dh_responder(derived, P->data + 1, key);
152
#else
153
202
        bool b = key != NULL; /* Avoiding unused variable warnings */
154
202
#endif
155
156
202
        if (!b) {
157
0
            PORT_SetError(SEC_ERROR_BAD_KEY);
158
0
            res = SECFailure;
159
0
            return res;
160
0
        }
161
162
202
        X->len = 66;
163
202
        memcpy(X->data, derived, 66);
164
202
    }
165
166
474
    return res;
167
474
}
168
169
/*
170
 * ECDSA Signature for P-521
171
 */
172
173
SECStatus
174
ec_secp521r1_sign_digest(ECPrivateKey *ecPrivKey, SECItem *signature,
175
                         const SECItem *digest, const unsigned char *kb,
176
                         const unsigned int kblen)
177
76
{
178
76
    SECStatus res = SECSuccess;
179
180
76
    if (!ecPrivKey || !signature || !digest || !kb ||
181
76
        !ecPrivKey->privateValue.data ||
182
76
        !signature->data || !digest->data ||
183
76
        ecPrivKey->ecParams.name != ECCurve_NIST_P521) {
184
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
185
0
        return SECFailure;
186
0
    }
187
188
76
    if (kblen == 0 || digest->len == 0 || signature->len < 132) {
189
0
        PORT_SetError(SEC_ERROR_INPUT_LEN);
190
0
        return SECFailure;
191
0
    }
192
193
    // Private keys should be 66 bytes, but some software trims leading zeros,
194
    // and some software produces 67 byte keys with a leading zero. We'll
195
    // accept these variants.
196
76
    uint8_t padded_key_data[66] = { 0 };
197
76
    uint8_t *key;
198
76
    SECItem *privKey = &ecPrivKey->privateValue;
199
76
    if (privKey->len == 66) {
200
76
        key = privKey->data;
201
76
    } else if (privKey->len == 67 && privKey->data[0] == 0) {
202
0
        key = privKey->data + 1;
203
0
    } else if (privKey->len < 66) {
204
0
        memcpy(padded_key_data + 66 - privKey->len, privKey->data, privKey->len);
205
0
        key = padded_key_data;
206
0
    } else {
207
0
        PORT_SetError(SEC_ERROR_INPUT_LEN);
208
0
        return SECFailure;
209
0
    }
210
211
76
    uint8_t hash[66] = { 0 };
212
76
    if (digest->len < 66) {
213
70
        memcpy(hash + 66 - digest->len, digest->data, digest->len);
214
70
    } else {
215
        // SEC 1 takes the most significant ceil(log(n)) bits of hash output when the hash output is longer than log(n).
216
6
        hash[0] = digest->data[0] >> 7;
217
396
        for (size_t i = 1; i < 66; i++) {
218
390
            hash[i] = (digest->data[i - 1] << 1) | (digest->data[i] >> 7);
219
390
        }
220
6
    }
221
222
76
    uint8_t nonce[66] = { 0 };
223
76
    if (kblen < 66) {
224
46
        memcpy(nonce + 66 - kblen, kb, kblen);
225
46
    } else {
226
30
        memcpy(nonce, kb, 66);
227
30
    }
228
229
76
#ifndef UNSAFE_FUZZER_MODE
230
76
    bool b = Hacl_P521_ecdsa_sign_p521_without_hash(
231
76
        signature->data, 66, hash, key, nonce);
232
#else
233
    bool b = key != NULL; /* Avoiding unused variable warnings */
234
#endif
235
236
76
    if (!b) {
237
6
        PORT_SetError(SEC_ERROR_BAD_KEY);
238
6
        res = SECFailure;
239
6
        return res;
240
6
    }
241
242
70
    signature->len = 132;
243
70
    return res;
244
76
}
245
246
/*
247
 * ECDSA Signature Verification for P-521
248
 */
249
250
SECStatus
251
ec_secp521r1_verify_digest(ECPublicKey *key, const SECItem *signature,
252
                           const SECItem *digest)
253
273
{
254
273
    SECStatus res = SECSuccess;
255
256
273
    if (!key || !signature || !digest ||
257
273
        !key->publicValue.data ||
258
273
        !signature->data || !digest->data ||
259
273
        key->ecParams.name != ECCurve_NIST_P521) {
260
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
261
0
        res = SECFailure;
262
0
        return res;
263
0
    }
264
265
273
    if (signature->len == 0 || signature->len % 2 != 0 ||
266
273
        signature->len > 132 || digest->len == 0 ||
267
273
        key->publicValue.len != 133) {
268
0
        PORT_SetError(SEC_ERROR_INPUT_LEN);
269
0
        res = SECFailure;
270
0
        return res;
271
0
    }
272
273
273
    if (key->publicValue.data[0] != EC_POINT_FORM_UNCOMPRESSED) {
274
0
        PORT_SetError(SEC_ERROR_UNSUPPORTED_EC_POINT_FORM);
275
0
        res = SECFailure;
276
0
        return res;
277
0
    }
278
279
    // Signatures should be 132 bytes, but some software produces short signatures.
280
    // Pad components with zeros if necessary.
281
273
    uint8_t paddedSigData[132] = { 0 };
282
273
    uint8_t *sig;
283
273
    if (signature->len != 132) {
284
0
        size_t split = signature->len / 2;
285
286
0
        memcpy(paddedSigData + 66 - split, signature->data, split);
287
0
        memcpy(paddedSigData + 132 - split, signature->data + split, split);
288
289
0
        sig = paddedSigData;
290
273
    } else {
291
273
        sig = signature->data;
292
273
    }
293
294
273
    uint8_t hash[66] = { 0 };
295
273
    if (digest->len < 66) {
296
267
        memcpy(hash + 66 - digest->len, digest->data, digest->len);
297
267
    } else {
298
        // SEC 1 takes the most significant ceil(log(n)) bits of hash output when the hash output is longer than log(n).
299
6
        hash[0] = digest->data[0] >> 7;
300
396
        for (size_t i = 1; i < 66; i++) {
301
390
            hash[i] = (digest->data[i - 1] << 1) | (digest->data[i] >> 7);
302
390
        }
303
6
    }
304
305
273
#ifndef UNSAFE_FUZZER_MODE
306
273
    bool b = Hacl_P521_ecdsa_verif_without_hash(
307
273
        66, hash, key->publicValue.data + 1, sig, sig + 66);
308
#else
309
    bool b = sig != NULL; /* Avoiding unused variable warnings */
310
#endif
311
312
273
    if (!b) {
313
193
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
314
193
        res = SECFailure;
315
193
        return res;
316
193
    }
317
318
80
    return res;
319
273
}
320
321
/*
322
    Point decompression for P-521.
323
324
    publicCompressed must be 67 bytes (1 byte for a sign and 66 bytes for the x coordinate.
325
    publicUncompressed must be 132 bytes (66 * 2).
326
    The function returns SECSuccess if the decompression was success and the decompresse
327
    point is a valid P-521 curve point.
328
*/
329
330
SECStatus
331
ec_secp521r1_decompress(const SECItem *publicCompressed, SECItem *publicUncompressed)
332
149
{
333
149
    if (!publicCompressed || !publicCompressed->data) {
334
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
335
0
        return SECFailure;
336
0
    }
337
338
149
    if (publicCompressed->len != 67) {
339
149
        PORT_SetError(SEC_ERROR_BAD_KEY);
340
149
        return SECFailure;
341
149
    }
342
343
0
    if (!publicUncompressed || !publicUncompressed->data) {
344
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
345
0
        return SECFailure;
346
0
    }
347
348
0
    if (publicUncompressed->len != 133) {
349
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
350
0
        return SECFailure;
351
0
    }
352
353
0
    if (publicCompressed->data[0] != EC_POINT_FORM_COMPRESSED_Y0 &&
354
0
        publicCompressed->data[0] != EC_POINT_FORM_COMPRESSED_Y1) {
355
0
        PORT_SetError(SEC_ERROR_UNSUPPORTED_EC_POINT_FORM);
356
0
        return SECFailure;
357
0
    }
358
359
0
    bool b = Hacl_P521_compressed_to_raw(publicCompressed->data, publicUncompressed->data + 1);
360
361
0
    if (!b) {
362
0
        PORT_SetError(SEC_ERROR_BAD_KEY);
363
0
        return SECFailure;
364
0
    }
365
366
0
    publicUncompressed->data[0] = EC_POINT_FORM_UNCOMPRESSED;
367
0
    return SECSuccess;
368
0
}