Coverage Report

Created: 2025-08-11 07:04

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