Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/ec/ecp_oct.c
Line
Count
Source
1
/*
2
 * Copyright 2011-2021 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
172k
{
27
172k
    BN_CTX *new_ctx = NULL;
28
172k
    BIGNUM *tmp1, *tmp2, *x, *y;
29
172k
    int ret = 0;
30
31
172k
    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
172k
    y_bit = (y_bit != 0);
38
39
172k
    BN_CTX_start(ctx);
40
172k
    tmp1 = BN_CTX_get(ctx);
41
172k
    tmp2 = BN_CTX_get(ctx);
42
172k
    x = BN_CTX_get(ctx);
43
172k
    y = BN_CTX_get(ctx);
44
172k
    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
172k
    if (!BN_nnmod(x, x_, group->field, ctx))
55
0
        goto err;
56
172k
    if (group->meth->field_decode == 0) {
57
        /* field_{sqr,mul} work on standard representation */
58
62.9k
        if (!group->meth->field_sqr(group, tmp2, x_, ctx))
59
0
            goto err;
60
62.9k
        if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))
61
0
            goto err;
62
109k
    } else {
63
109k
        if (!BN_mod_sqr(tmp2, x_, group->field, ctx))
64
0
            goto err;
65
109k
        if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))
66
0
            goto err;
67
109k
    }
68
69
    /* tmp1 := tmp1 + a*x */
70
172k
    if (group->a_is_minus3) {
71
132k
        if (!BN_mod_lshift1_quick(tmp2, x, group->field))
72
0
            goto err;
73
132k
        if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))
74
0
            goto err;
75
132k
        if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))
76
0
            goto err;
77
132k
    } else {
78
40.4k
        if (group->meth->field_decode) {
79
40.4k
            if (!group->meth->field_decode(group, tmp2, group->a, ctx))
80
0
                goto err;
81
40.4k
            if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))
82
0
                goto err;
83
40.4k
        } 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
40.4k
        if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
90
0
            goto err;
91
40.4k
    }
92
93
    /* tmp1 := tmp1 + b */
94
172k
    if (group->meth->field_decode) {
95
109k
        if (!group->meth->field_decode(group, tmp2, group->b, ctx))
96
0
            goto err;
97
109k
        if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
98
0
            goto err;
99
109k
    } else {
100
62.9k
        if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))
101
0
            goto err;
102
62.9k
    }
103
104
172k
    ERR_set_mark();
105
172k
    if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {
106
52.4k
#ifndef FIPS_MODULE
107
52.4k
        unsigned long err = ERR_peek_last_error();
108
109
52.4k
        if (ERR_GET_LIB(err) == ERR_LIB_BN
110
52.4k
            && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
111
51.6k
            ERR_pop_to_mark();
112
51.6k
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT);
113
51.6k
        } else
114
748
#endif
115
748
        {
116
748
            ERR_clear_last_mark();
117
748
            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
118
748
        }
119
52.4k
        goto err;
120
52.4k
    }
121
120k
    ERR_clear_last_mark();
122
123
120k
    if (y_bit != BN_is_odd(y)) {
124
58.1k
        if (BN_is_zero(y)) {
125
161
            int kron;
126
127
161
            kron = BN_kronecker(x, group->field, ctx);
128
161
            if (kron == -2)
129
0
                goto err;
130
131
161
            if (kron == 1)
132
161
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSION_BIT);
133
161
            else
134
                /*
135
                 * BN_mod_sqrt() should have caught this error (not a square)
136
                 */
137
161
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT);
138
161
            goto err;
139
161
        }
140
58.0k
        if (!BN_usub(y, group->field, y))
141
0
            goto err;
142
58.0k
    }
143
119k
    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
119k
    if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
149
0
        goto err;
150
151
119k
    ret = 1;
152
153
172k
 err:
154
172k
    BN_CTX_end(ctx);
155
172k
    BN_CTX_free(new_ctx);
156
172k
    return ret;
157
119k
}
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
235k
{
163
235k
    size_t ret;
164
235k
    BN_CTX *new_ctx = NULL;
165
235k
    int used_ctx = 0;
166
235k
    BIGNUM *x, *y;
167
235k
    size_t field_len, i, skip;
168
169
235k
    if ((form != POINT_CONVERSION_COMPRESSED)
170
215k
        && (form != POINT_CONVERSION_UNCOMPRESSED)
171
722
        && (form != POINT_CONVERSION_HYBRID)) {
172
334
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
173
334
        goto err;
174
334
    }
175
176
234k
    if (EC_POINT_is_at_infinity(group, point)) {
177
        /* encodes to a single 0 octet */
178
1.48k
        if (buf != NULL) {
179
743
            if (len < 1) {
180
0
                ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
181
0
                return 0;
182
0
            }
183
743
            buf[0] = 0;
184
743
        }
185
1.48k
        return 1;
186
1.48k
    }
187
188
    /* ret := required output buffer length */
189
233k
    field_len = BN_num_bytes(group->field);
190
233k
    ret =
191
233k
        (form ==
192
233k
         POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
193
194
    /* if 'buf' is NULL, just return required length */
195
233k
    if (buf != NULL) {
196
116k
        if (len < ret) {
197
0
            ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
198
0
            goto err;
199
0
        }
200
201
116k
        if (ctx == NULL) {
202
14.4k
            ctx = new_ctx = BN_CTX_new_ex(group->libctx);
203
14.4k
            if (ctx == NULL)
204
0
                return 0;
205
14.4k
        }
206
207
116k
        BN_CTX_start(ctx);
208
116k
        used_ctx = 1;
209
116k
        x = BN_CTX_get(ctx);
210
116k
        y = BN_CTX_get(ctx);
211
116k
        if (y == NULL)
212
0
            goto err;
213
214
116k
        if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
215
0
            goto err;
216
217
116k
        if ((form == POINT_CONVERSION_COMPRESSED
218
106k
             || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
219
3.70k
            buf[0] = form + 1;
220
112k
        else
221
112k
            buf[0] = form;
222
223
116k
        i = 1;
224
225
116k
        skip = field_len - BN_num_bytes(x);
226
116k
        if (skip > field_len) {
227
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
228
0
            goto err;
229
0
        }
230
187k
        while (skip > 0) {
231
70.5k
            buf[i++] = 0;
232
70.5k
            skip--;
233
70.5k
        }
234
116k
        skip = BN_bn2bin(x, buf + i);
235
116k
        i += skip;
236
116k
        if (i != 1 + field_len) {
237
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
238
0
            goto err;
239
0
        }
240
241
116k
        if (form == POINT_CONVERSION_UNCOMPRESSED
242
106k
            || form == POINT_CONVERSION_HYBRID) {
243
106k
            skip = field_len - BN_num_bytes(y);
244
106k
            if (skip > field_len) {
245
0
                ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
246
0
                goto err;
247
0
            }
248
117k
            while (skip > 0) {
249
10.5k
                buf[i++] = 0;
250
10.5k
                skip--;
251
10.5k
            }
252
106k
            skip = BN_bn2bin(y, buf + i);
253
106k
            i += skip;
254
106k
        }
255
256
116k
        if (i != ret) {
257
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
258
0
            goto err;
259
0
        }
260
116k
    }
261
262
233k
    if (used_ctx)
263
116k
        BN_CTX_end(ctx);
264
233k
    BN_CTX_free(new_ctx);
265
233k
    return ret;
266
267
334
 err:
268
334
    if (used_ctx)
269
0
        BN_CTX_end(ctx);
270
334
    BN_CTX_free(new_ctx);
271
334
    return 0;
272
233k
}
273
274
int ossl_ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
275
                                 const unsigned char *buf, size_t len,
276
                                 BN_CTX *ctx)
277
442k
{
278
442k
    point_conversion_form_t form;
279
442k
    int y_bit;
280
442k
    BN_CTX *new_ctx = NULL;
281
442k
    BIGNUM *x, *y;
282
442k
    size_t field_len, enc_len;
283
442k
    int ret = 0;
284
285
442k
    if (len == 0) {
286
10.8k
        ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
287
10.8k
        return 0;
288
10.8k
    }
289
431k
    form = buf[0];
290
431k
    y_bit = form & 1;
291
431k
    form = form & ~1U;
292
431k
    if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
293
227k
        && (form != POINT_CONVERSION_UNCOMPRESSED)
294
18.4k
        && (form != POINT_CONVERSION_HYBRID)) {
295
6.10k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
296
6.10k
        return 0;
297
6.10k
    }
298
425k
    if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
299
3.47k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
300
3.47k
        return 0;
301
3.47k
    }
302
303
421k
    if (form == 0) {
304
22.7k
        if (len != 1) {
305
4.90k
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
306
4.90k
            return 0;
307
4.90k
        }
308
309
17.8k
        return EC_POINT_set_to_infinity(group, point);
310
22.7k
    }
311
312
399k
    field_len = BN_num_bytes(group->field);
313
399k
    enc_len =
314
399k
        (form ==
315
399k
         POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
316
317
399k
    if (len != enc_len) {
318
8.29k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
319
8.29k
        return 0;
320
8.29k
    }
321
322
390k
    if (ctx == NULL) {
323
292k
        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
324
292k
        if (ctx == NULL)
325
0
            return 0;
326
292k
    }
327
328
390k
    BN_CTX_start(ctx);
329
390k
    x = BN_CTX_get(ctx);
330
390k
    y = BN_CTX_get(ctx);
331
390k
    if (y == NULL)
332
0
        goto err;
333
334
390k
    if (!BN_bin2bn(buf + 1, field_len, x))
335
0
        goto err;
336
390k
    if (BN_ucmp(x, group->field) >= 0) {
337
2.13k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
338
2.13k
        goto err;
339
2.13k
    }
340
341
388k
    if (form == POINT_CONVERSION_COMPRESSED) {
342
172k
        if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
343
52.6k
            goto err;
344
216k
    } else {
345
216k
        if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
346
0
            goto err;
347
216k
        if (BN_ucmp(y, group->field) >= 0) {
348
620
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
349
620
            goto err;
350
620
        }
351
215k
        if (form == POINT_CONVERSION_HYBRID) {
352
8.46k
            if (y_bit != BN_is_odd(y)) {
353
2.34k
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
354
2.34k
                goto err;
355
2.34k
            }
356
8.46k
        }
357
358
        /*
359
         * EC_POINT_set_affine_coordinates is responsible for checking that
360
         * the point is on the curve.
361
         */
362
213k
        if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
363
17.5k
            goto err;
364
213k
    }
365
366
315k
    ret = 1;
367
368
390k
 err:
369
390k
    BN_CTX_end(ctx);
370
390k
    BN_CTX_free(new_ctx);
371
390k
    return ret;
372
315k
}