Coverage Report

Created: 2025-04-22 06:15

/src/nss/lib/freebl/ecl/ecp_secp521r1.c
Line
Count
Source (jump to first uncovered line)
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
117
{
22
117
    SECStatus res = SECSuccess;
23
117
    if (!pt || !pt->data) {
24
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
25
0
        res = SECFailure;
26
0
        return res;
27
0
    }
28
29
117
    if (pt->len != 133) {
30
10
        PORT_SetError(SEC_ERROR_BAD_KEY);
31
10
        res = SECFailure;
32
10
        return res;
33
10
    }
34
35
107
    if (pt->data[0] != EC_POINT_FORM_UNCOMPRESSED) {
36
0
        PORT_SetError(SEC_ERROR_UNSUPPORTED_EC_POINT_FORM);
37
0
        res = SECFailure;
38
0
        return res;
39
0
    }
40
41
107
    bool b = Hacl_P521_validate_public_key(pt->data + 1);
42
43
107
    if (!b) {
44
44
        PORT_SetError(SEC_ERROR_BAD_KEY);
45
44
        res = SECFailure;
46
44
    }
47
107
    return res;
48
107
}
49
50
/*
51
 * Scalar Validation for P-521.
52
 */
53
54
SECStatus
55
ec_secp521r1_scalar_validate(const SECItem *scalar)
56
56
{
57
56
    SECStatus res = SECSuccess;
58
56
    if (!scalar || !scalar->data) {
59
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
60
0
        res = SECFailure;
61
0
        return res;
62
0
    }
63
64
56
    if (scalar->len != 66) {
65
0
        PORT_SetError(SEC_ERROR_BAD_KEY);
66
0
        res = SECFailure;
67
0
        return res;
68
0
    }
69
70
56
    bool b = Hacl_P521_validate_private_key(scalar->data);
71
72
56
    if (!b) {
73
0
        PORT_SetError(SEC_ERROR_BAD_KEY);
74
0
        res = SECFailure;
75
0
    }
76
56
    return res;
77
56
}
78
79
/*
80
 * Scalar multiplication for P-521.
81
 * If P == NULL, the base point is used.
82
 * Returns X = k*P
83
 */
84
85
SECStatus
86
ec_secp521r1_pt_mul(SECItem *X, SECItem *k, SECItem *P)
87
58
{
88
58
    SECStatus res = SECSuccess;
89
58
    if (!P) {
90
56
        uint8_t derived[132] = { 0 };
91
92
56
        if (!X || !k || !X->data || !k->data ||
93
56
            X->len < 133 || k->len != 66) {
94
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
95
0
            res = SECFailure;
96
0
            return res;
97
0
        }
98
99
56
        bool b = Hacl_P521_dh_initiator(derived, k->data);
100
101
56
        if (!b) {
102
0
            PORT_SetError(SEC_ERROR_BAD_KEY);
103
0
            res = SECFailure;
104
0
            return res;
105
0
        }
106
107
56
        X->len = 133;
108
56
        X->data[0] = EC_POINT_FORM_UNCOMPRESSED;
109
56
        memcpy(X->data + 1, derived, 132);
110
111
56
    } else {
112
2
        uint8_t full_key[66] = { 0 };
113
2
        uint8_t *key;
114
2
        uint8_t derived[132] = { 0 };
115
116
2
        if (!X || !k || !P || !X->data || !k->data || !P->data ||
117
2
            X->len < 66 || P->len != 133 ||
118
2
            P->data[0] != EC_POINT_FORM_UNCOMPRESSED) {
119
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
120
0
            res = SECFailure;
121
0
            return res;
122
0
        }
123
124
        /* We consider keys of up to size 66, or of size 67 with a single leading 0 */
125
2
        if (k->len < 66) {
126
0
            memcpy(full_key + 66 - k->len, k->data, k->len);
127
0
            key = full_key;
128
2
        } else if (k->len == 66) {
129
2
            key = k->data;
130
2
        } else if (k->len == 67 && k->data[0] == 0) {
131
0
            key = k->data + 1;
132
0
        } else {
133
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
134
0
            res = SECFailure;
135
0
            return res;
136
0
        }
137
138
2
        bool b = Hacl_P521_dh_responder(derived, P->data + 1, key);
139
140
2
        if (!b) {
141
0
            PORT_SetError(SEC_ERROR_BAD_KEY);
142
0
            res = SECFailure;
143
0
            return res;
144
0
        }
145
146
2
        X->len = 66;
147
2
        memcpy(X->data, derived, 66);
148
2
    }
149
150
58
    return res;
151
58
}
152
153
/*
154
 * ECDSA Signature for P-521
155
 */
156
157
SECStatus
158
ec_secp521r1_sign_digest(ECPrivateKey *ecPrivKey, SECItem *signature,
159
                         const SECItem *digest, const unsigned char *kb,
160
                         const unsigned int kblen)
161
0
{
162
0
    SECStatus res = SECSuccess;
163
164
0
    if (!ecPrivKey || !signature || !digest || !kb ||
165
0
        !ecPrivKey->privateValue.data ||
166
0
        !signature->data || !digest->data ||
167
0
        ecPrivKey->ecParams.name != ECCurve_NIST_P521) {
168
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
169
0
        return SECFailure;
170
0
    }
171
172
0
    if (kblen == 0 || digest->len == 0 || signature->len < 132) {
173
0
        PORT_SetError(SEC_ERROR_INPUT_LEN);
174
0
        return SECFailure;
175
0
    }
176
177
    // Private keys should be 66 bytes, but some software trims leading zeros,
178
    // and some software produces 67 byte keys with a leading zero. We'll
179
    // accept these variants.
180
0
    uint8_t padded_key_data[66] = { 0 };
181
0
    uint8_t *key;
182
0
    SECItem *privKey = &ecPrivKey->privateValue;
183
0
    if (privKey->len == 66) {
184
0
        key = privKey->data;
185
0
    } else if (privKey->len == 67 && privKey->data[0] == 0) {
186
0
        key = privKey->data + 1;
187
0
    } else if (privKey->len < 66) {
188
0
        memcpy(padded_key_data + 66 - privKey->len, privKey->data, privKey->len);
189
0
        key = padded_key_data;
190
0
    } else {
191
0
        PORT_SetError(SEC_ERROR_INPUT_LEN);
192
0
        return SECFailure;
193
0
    }
194
195
0
    uint8_t hash[66] = { 0 };
196
0
    if (digest->len < 66) {
197
0
        memcpy(hash + 66 - digest->len, digest->data, digest->len);
198
0
    } else {
199
        // SEC 1 takes the most significant ceil(log(n)) bits of hash output when the hash output is longer than log(n).
200
0
        hash[0] = digest->data[0] >> 7;
201
0
        for (size_t i = 1; i < 66; i++) {
202
0
            hash[i] = (digest->data[i - 1] << 1) | (digest->data[i] >> 7);
203
0
        }
204
0
    }
205
206
0
    uint8_t nonce[66] = { 0 };
207
0
    if (kblen < 66) {
208
0
        memcpy(nonce + 66 - kblen, kb, kblen);
209
0
    } else {
210
0
        memcpy(nonce, kb, 66);
211
0
    }
212
213
0
    bool b = Hacl_P521_ecdsa_sign_p521_without_hash(
214
0
        signature->data, 66, hash, key, nonce);
215
0
    if (!b) {
216
0
        PORT_SetError(SEC_ERROR_BAD_KEY);
217
0
        res = SECFailure;
218
0
        return res;
219
0
    }
220
221
0
    signature->len = 132;
222
0
    return res;
223
0
}
224
225
/*
226
 * ECDSA Signature Verification for P-521
227
 */
228
229
SECStatus
230
ec_secp521r1_verify_digest(ECPublicKey *key, const SECItem *signature,
231
                           const SECItem *digest)
232
5
{
233
5
    SECStatus res = SECSuccess;
234
235
5
    if (!key || !signature || !digest ||
236
5
        !key->publicValue.data ||
237
5
        !signature->data || !digest->data ||
238
5
        key->ecParams.name != ECCurve_NIST_P521) {
239
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
240
0
        res = SECFailure;
241
0
        return res;
242
0
    }
243
244
5
    if (signature->len == 0 || signature->len % 2 != 0 ||
245
5
        signature->len > 132 || digest->len == 0 ||
246
5
        key->publicValue.len != 133) {
247
0
        PORT_SetError(SEC_ERROR_INPUT_LEN);
248
0
        res = SECFailure;
249
0
        return res;
250
0
    }
251
252
5
    if (key->publicValue.data[0] != EC_POINT_FORM_UNCOMPRESSED) {
253
0
        PORT_SetError(SEC_ERROR_UNSUPPORTED_EC_POINT_FORM);
254
0
        res = SECFailure;
255
0
        return res;
256
0
    }
257
258
    // Signatures should be 132 bytes, but some software produces short signatures.
259
    // Pad components with zeros if necessary.
260
5
    uint8_t paddedSigData[132] = { 0 };
261
5
    uint8_t *sig;
262
5
    if (signature->len != 132) {
263
0
        size_t split = signature->len / 2;
264
265
0
        memcpy(paddedSigData + 66 - split, signature->data, split);
266
0
        memcpy(paddedSigData + 132 - split, signature->data + split, split);
267
268
0
        sig = paddedSigData;
269
5
    } else {
270
5
        sig = signature->data;
271
5
    }
272
273
5
    uint8_t hash[66] = { 0 };
274
5
    if (digest->len < 66) {
275
5
        memcpy(hash + 66 - digest->len, digest->data, digest->len);
276
5
    } else {
277
        // SEC 1 takes the most significant ceil(log(n)) bits of hash output when the hash output is longer than log(n).
278
0
        hash[0] = digest->data[0] >> 7;
279
0
        for (size_t i = 1; i < 66; i++) {
280
0
            hash[i] = (digest->data[i - 1] << 1) | (digest->data[i] >> 7);
281
0
        }
282
0
    }
283
284
5
    bool b = Hacl_P521_ecdsa_verif_without_hash(
285
5
        66, hash, key->publicValue.data + 1, sig, sig + 66);
286
5
    if (!b) {
287
5
        PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
288
5
        res = SECFailure;
289
5
        return res;
290
5
    }
291
292
0
    return res;
293
5
}