Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/ec/ecp_oct.c
Line
Count
Source
1
/*
2
 * Copyright 2011-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
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
11
/*
12
 * ECDSA low level APIs are deprecated for public use, but still ok for
13
 * internal use.
14
 */
15
#include "internal/deprecated.h"
16
17
#include <openssl/err.h>
18
#include <openssl/symhacks.h>
19
20
#include "ec_local.h"
21
22
int ossl_ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
23
    EC_POINT *point,
24
    const BIGNUM *x_, int y_bit,
25
    BN_CTX *ctx)
26
156k
{
27
156k
    BN_CTX *new_ctx = NULL;
28
156k
    BIGNUM *tmp1, *tmp2, *x, *y;
29
156k
    int ret = 0;
30
31
156k
    if (ctx == NULL) {
32
0
        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
33
0
        if (ctx == NULL)
34
0
            return 0;
35
0
    }
36
37
156k
    y_bit = (y_bit != 0);
38
39
156k
    BN_CTX_start(ctx);
40
156k
    tmp1 = BN_CTX_get(ctx);
41
156k
    tmp2 = BN_CTX_get(ctx);
42
156k
    x = BN_CTX_get(ctx);
43
156k
    y = BN_CTX_get(ctx);
44
156k
    if (y == NULL)
45
0
        goto err;
46
47
    /*-
48
     * Recover y.  We have a Weierstrass equation
49
     *     y^2 = x^3 + a*x + b,
50
     * so  y  is one of the square roots of  x^3 + a*x + b.
51
     */
52
53
    /* tmp1 := x^3 */
54
156k
    if (!BN_nnmod(x, x_, group->field, ctx))
55
0
        goto err;
56
156k
    if (group->meth->field_decode == 0) {
57
        /* field_{sqr,mul} work on standard representation */
58
65.1k
        if (!group->meth->field_sqr(group, tmp2, x_, ctx))
59
0
            goto err;
60
65.1k
        if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))
61
0
            goto err;
62
91.3k
    } else {
63
91.3k
        if (!BN_mod_sqr(tmp2, x_, group->field, ctx))
64
0
            goto err;
65
91.3k
        if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))
66
0
            goto err;
67
91.3k
    }
68
69
    /* tmp1 := tmp1 + a*x */
70
156k
    if (group->a_is_minus3) {
71
111k
        if (!BN_mod_lshift1_quick(tmp2, x, group->field))
72
0
            goto err;
73
111k
        if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))
74
0
            goto err;
75
111k
        if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))
76
0
            goto err;
77
111k
    } else {
78
44.5k
        if (group->meth->field_decode) {
79
44.5k
            if (!group->meth->field_decode(group, tmp2, group->a, ctx))
80
0
                goto err;
81
44.5k
            if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))
82
0
                goto err;
83
44.5k
        } else {
84
            /* field_mul works on standard representation */
85
0
            if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))
86
0
                goto err;
87
0
        }
88
89
44.5k
        if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
90
0
            goto err;
91
44.5k
    }
92
93
    /* tmp1 := tmp1 + b */
94
156k
    if (group->meth->field_decode) {
95
91.3k
        if (!group->meth->field_decode(group, tmp2, group->b, ctx))
96
0
            goto err;
97
91.3k
        if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
98
0
            goto err;
99
91.3k
    } else {
100
65.1k
        if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))
101
0
            goto err;
102
65.1k
    }
103
104
156k
    ERR_set_mark();
105
156k
    if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {
106
55.0k
#ifndef FIPS_MODULE
107
55.0k
        unsigned long err = ERR_peek_last_error();
108
109
55.0k
        if (ERR_GET_LIB(err) == ERR_LIB_BN
110
55.0k
            && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
111
54.5k
            ERR_pop_to_mark();
112
54.5k
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT);
113
54.5k
        } else
114
558
#endif
115
558
        {
116
558
            ERR_clear_last_mark();
117
558
            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
118
558
        }
119
55.0k
        goto err;
120
55.0k
    }
121
101k
    ERR_clear_last_mark();
122
123
101k
    if (y_bit != BN_is_odd(y)) {
124
53.9k
        if (BN_is_zero(y)) {
125
123
            int kron;
126
127
123
            kron = BN_kronecker(x, group->field, ctx);
128
123
            if (kron == -2)
129
0
                goto err;
130
131
123
            if (kron == 1)
132
123
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSION_BIT);
133
123
            else
134
                /*
135
                 * BN_mod_sqrt() should have caught this error (not a square)
136
                 */
137
123
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT);
138
123
            goto err;
139
123
        }
140
53.8k
        if (!BN_usub(y, group->field, y))
141
0
            goto err;
142
53.8k
    }
143
101k
    if (y_bit != BN_is_odd(y)) {
144
0
        ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
145
0
        goto err;
146
0
    }
147
148
101k
    if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
149
0
        goto err;
150
151
101k
    ret = 1;
152
153
156k
err:
154
156k
    BN_CTX_end(ctx);
155
156k
    BN_CTX_free(new_ctx);
156
156k
    return ret;
157
101k
}
158
159
size_t ossl_ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
160
    point_conversion_form_t form,
161
    unsigned char *buf, size_t len, BN_CTX *ctx)
162
158k
{
163
158k
    size_t ret;
164
158k
    BN_CTX *new_ctx = NULL;
165
158k
    int used_ctx = 0;
166
158k
    BIGNUM *x, *y;
167
158k
    size_t field_len, i, skip;
168
169
158k
    if ((form != POINT_CONVERSION_COMPRESSED)
170
144k
        && (form != POINT_CONVERSION_UNCOMPRESSED)
171
414
        && (form != POINT_CONVERSION_HYBRID)) {
172
210
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
173
210
        goto err;
174
210
    }
175
176
158k
    if (EC_POINT_is_at_infinity(group, point)) {
177
        /* encodes to a single 0 octet */
178
1.13k
        if (buf != NULL) {
179
569
            if (len < 1) {
180
0
                ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
181
0
                return 0;
182
0
            }
183
569
            buf[0] = 0;
184
569
        }
185
1.13k
        return 1;
186
1.13k
    }
187
188
    /* ret := required output buffer length */
189
157k
    field_len = BN_num_bytes(group->field);
190
157k
    ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
191
192
    /* if 'buf' is NULL, just return required length */
193
157k
    if (buf != NULL) {
194
78.5k
        if (len < ret) {
195
0
            ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
196
0
            goto err;
197
0
        }
198
199
78.5k
        if (ctx == NULL) {
200
10.2k
            ctx = new_ctx = BN_CTX_new_ex(group->libctx);
201
10.2k
            if (ctx == NULL)
202
0
                return 0;
203
10.2k
        }
204
205
78.5k
        BN_CTX_start(ctx);
206
78.5k
        used_ctx = 1;
207
78.5k
        x = BN_CTX_get(ctx);
208
78.5k
        y = BN_CTX_get(ctx);
209
78.5k
        if (y == NULL)
210
0
            goto err;
211
212
78.5k
        if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
213
0
            goto err;
214
215
78.5k
        if ((form == POINT_CONVERSION_COMPRESSED
216
71.6k
                || form == POINT_CONVERSION_HYBRID)
217
7.07k
            && BN_is_odd(y))
218
2.84k
            buf[0] = form + 1;
219
75.7k
        else
220
75.7k
            buf[0] = form;
221
222
78.5k
        i = 1;
223
224
78.5k
        skip = field_len - BN_num_bytes(x);
225
78.5k
        if (skip > field_len) {
226
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
227
0
            goto err;
228
0
        }
229
140k
        while (skip > 0) {
230
61.6k
            buf[i++] = 0;
231
61.6k
            skip--;
232
61.6k
        }
233
78.5k
        skip = BN_bn2bin(x, buf + i);
234
78.5k
        i += skip;
235
78.5k
        if (i != 1 + field_len) {
236
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
237
0
            goto err;
238
0
        }
239
240
78.5k
        if (form == POINT_CONVERSION_UNCOMPRESSED
241
71.6k
            || form == POINT_CONVERSION_HYBRID) {
242
71.6k
            skip = field_len - BN_num_bytes(y);
243
71.6k
            if (skip > field_len) {
244
0
                ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
245
0
                goto err;
246
0
            }
247
79.9k
            while (skip > 0) {
248
8.37k
                buf[i++] = 0;
249
8.37k
                skip--;
250
8.37k
            }
251
71.6k
            skip = BN_bn2bin(y, buf + i);
252
71.6k
            i += skip;
253
71.6k
        }
254
255
78.5k
        if (i != ret) {
256
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
257
0
            goto err;
258
0
        }
259
78.5k
    }
260
261
157k
    if (used_ctx)
262
78.5k
        BN_CTX_end(ctx);
263
157k
    BN_CTX_free(new_ctx);
264
157k
    return ret;
265
266
210
err:
267
210
    if (used_ctx)
268
0
        BN_CTX_end(ctx);
269
210
    BN_CTX_free(new_ctx);
270
210
    return 0;
271
157k
}
272
273
int ossl_ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
274
    const unsigned char *buf, size_t len,
275
    BN_CTX *ctx)
276
377k
{
277
377k
    point_conversion_form_t form;
278
377k
    int y_bit;
279
377k
    BN_CTX *new_ctx = NULL;
280
377k
    BIGNUM *x, *y;
281
377k
    int field_len, enc_len;
282
377k
    int ret = 0;
283
284
377k
    if (len == 0) {
285
20.8k
        ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
286
20.8k
        return 0;
287
20.8k
    }
288
356k
    form = buf[0];
289
356k
    y_bit = form & 1;
290
356k
    form = form & ~1U;
291
356k
    if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
292
173k
        && (form != POINT_CONVERSION_UNCOMPRESSED)
293
17.4k
        && (form != POINT_CONVERSION_HYBRID)) {
294
6.06k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
295
6.06k
        return 0;
296
6.06k
    }
297
350k
    if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
298
3.25k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
299
3.25k
        return 0;
300
3.25k
    }
301
302
347k
    if (form == 0) {
303
16.0k
        if (len != 1) {
304
7.26k
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
305
7.26k
            return 0;
306
7.26k
        }
307
308
8.74k
        return EC_POINT_set_to_infinity(group, point);
309
16.0k
    }
310
311
331k
    field_len = BN_num_bytes(group->field);
312
331k
    enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
313
314
331k
    if (len != (size_t)enc_len) {
315
8.75k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
316
8.75k
        return 0;
317
8.75k
    }
318
319
322k
    if (ctx == NULL) {
320
257k
        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
321
257k
        if (ctx == NULL)
322
0
            return 0;
323
257k
    }
324
325
322k
    BN_CTX_start(ctx);
326
322k
    x = BN_CTX_get(ctx);
327
322k
    y = BN_CTX_get(ctx);
328
322k
    if (y == NULL)
329
0
        goto err;
330
331
322k
    if (!BN_bin2bn(buf + 1, field_len, x))
332
0
        goto err;
333
322k
    if (BN_ucmp(x, group->field) >= 0) {
334
4.69k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
335
4.69k
        goto err;
336
4.69k
    }
337
338
318k
    if (form == POINT_CONVERSION_COMPRESSED) {
339
156k
        if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
340
55.1k
            goto err;
341
161k
    } else {
342
161k
        if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
343
0
            goto err;
344
161k
        if (BN_ucmp(y, group->field) >= 0) {
345
419
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
346
419
            goto err;
347
419
        }
348
161k
        if (form == POINT_CONVERSION_HYBRID) {
349
7.07k
            if (y_bit != BN_is_odd(y)) {
350
984
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
351
984
                goto err;
352
984
            }
353
7.07k
        }
354
355
        /*
356
         * EC_POINT_set_affine_coordinates is responsible for checking that
357
         * the point is on the curve.
358
         */
359
160k
        if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
360
14.6k
            goto err;
361
160k
    }
362
363
246k
    ret = 1;
364
365
322k
err:
366
322k
    BN_CTX_end(ctx);
367
322k
    BN_CTX_free(new_ctx);
368
322k
    return ret;
369
246k
}