Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/ec/ec2_oct.c
Line
Count
Source
1
/*
2
 * Copyright 2011-2022 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
19
#include "ec_local.h"
20
21
#ifndef OPENSSL_NO_EC2M
22
23
/*-
24
 * Calculates and sets the affine coordinates of an EC_POINT from the given
25
 * compressed coordinates.  Uses algorithm 2.3.4 of SEC 1.
26
 * Note that the simple implementation only uses affine coordinates.
27
 *
28
 * The method is from the following publication:
29
 *
30
 *     Harper, Menezes, Vanstone:
31
 *     "Public-Key Cryptosystems with Very Small Key Lengths",
32
 *     EUROCRYPT '92, Springer-Verlag LNCS 658,
33
 *     published February 1993
34
 *
35
 * US Patents 6,141,420 and 6,618,483 (Vanstone, Mullin, Agnew) describe
36
 * the same method, but claim no priority date earlier than July 29, 1994
37
 * (and additionally fail to cite the EUROCRYPT '92 publication as prior art).
38
 */
39
int ossl_ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group,
40
    EC_POINT *point,
41
    const BIGNUM *x_, int y_bit,
42
    BN_CTX *ctx)
43
204k
{
44
204k
    BIGNUM *tmp, *x, *y, *z;
45
204k
    int ret = 0, z0;
46
204k
#ifndef FIPS_MODULE
47
204k
    BN_CTX *new_ctx = NULL;
48
49
204k
    if (ctx == NULL) {
50
0
        ctx = new_ctx = BN_CTX_new();
51
0
        if (ctx == NULL)
52
0
            return 0;
53
0
    }
54
204k
#endif
55
56
204k
    y_bit = (y_bit != 0) ? 1 : 0;
57
58
204k
    BN_CTX_start(ctx);
59
204k
    tmp = BN_CTX_get(ctx);
60
204k
    x = BN_CTX_get(ctx);
61
204k
    y = BN_CTX_get(ctx);
62
204k
    z = BN_CTX_get(ctx);
63
204k
    if (z == NULL)
64
0
        goto err;
65
66
204k
    if (!BN_GF2m_mod_arr(x, x_, group->poly))
67
0
        goto err;
68
204k
    if (BN_is_zero(x)) {
69
30.6k
        if (!BN_GF2m_mod_sqrt_arr(y, group->b, group->poly, ctx))
70
0
            goto err;
71
173k
    } else {
72
173k
        if (!group->meth->field_sqr(group, tmp, x, ctx))
73
0
            goto err;
74
173k
        if (!group->meth->field_div(group, tmp, group->b, tmp, ctx))
75
0
            goto err;
76
173k
        if (!BN_GF2m_add(tmp, group->a, tmp))
77
0
            goto err;
78
173k
        if (!BN_GF2m_add(tmp, x, tmp))
79
0
            goto err;
80
173k
        ERR_set_mark();
81
173k
        if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) {
82
66.9k
#ifndef FIPS_MODULE
83
66.9k
            unsigned long err = ERR_peek_last_error();
84
85
66.9k
            if (ERR_GET_LIB(err) == ERR_LIB_BN
86
66.9k
                && ERR_GET_REASON(err) == BN_R_NO_SOLUTION) {
87
63.2k
                ERR_pop_to_mark();
88
63.2k
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT);
89
63.2k
            } else
90
3.75k
#endif
91
3.75k
            {
92
3.75k
                ERR_clear_last_mark();
93
3.75k
                ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
94
3.75k
            }
95
66.9k
            goto err;
96
66.9k
        }
97
106k
        ERR_clear_last_mark();
98
106k
        z0 = (BN_is_odd(z)) ? 1 : 0;
99
106k
        if (!group->meth->field_mul(group, y, x, z, ctx))
100
0
            goto err;
101
106k
        if (z0 != y_bit) {
102
52.9k
            if (!BN_GF2m_add(y, y, x))
103
0
                goto err;
104
52.9k
        }
105
106k
    }
106
107
137k
    if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
108
0
        goto err;
109
110
137k
    ret = 1;
111
112
204k
err:
113
204k
    BN_CTX_end(ctx);
114
204k
#ifndef FIPS_MODULE
115
204k
    BN_CTX_free(new_ctx);
116
204k
#endif
117
204k
    return ret;
118
137k
}
119
120
/*
121
 * Converts an EC_POINT to an octet string. If buf is NULL, the encoded
122
 * length will be returned. If the length len of buf is smaller than required
123
 * an error will be returned.
124
 */
125
size_t ossl_ec_GF2m_simple_point2oct(const EC_GROUP *group,
126
    const EC_POINT *point,
127
    point_conversion_form_t form,
128
    unsigned char *buf, size_t len, BN_CTX *ctx)
129
10.3k
{
130
10.3k
    size_t ret;
131
10.3k
    int used_ctx = 0;
132
10.3k
    BIGNUM *x, *y, *yxi;
133
10.3k
    size_t field_len, i, skip;
134
10.3k
#ifndef FIPS_MODULE
135
10.3k
    BN_CTX *new_ctx = NULL;
136
10.3k
#endif
137
138
10.3k
    if ((form != POINT_CONVERSION_COMPRESSED)
139
8.01k
        && (form != POINT_CONVERSION_UNCOMPRESSED)
140
303
        && (form != POINT_CONVERSION_HYBRID)) {
141
269
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
142
269
        goto err;
143
269
    }
144
145
10.0k
    if (EC_POINT_is_at_infinity(group, point)) {
146
        /* encodes to a single 0 octet */
147
1.06k
        if (buf != NULL) {
148
530
            if (len < 1) {
149
0
                ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
150
0
                return 0;
151
0
            }
152
530
            buf[0] = 0;
153
530
        }
154
1.06k
        return 1;
155
1.06k
    }
156
157
    /* ret := required output buffer length */
158
9.00k
    field_len = (EC_GROUP_get_degree(group) + 7) / 8;
159
9.00k
    ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
160
161
    /* if 'buf' is NULL, just return required length */
162
9.00k
    if (buf != NULL) {
163
4.50k
        if (len < ret) {
164
0
            ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
165
0
            goto err;
166
0
        }
167
168
4.50k
#ifndef FIPS_MODULE
169
4.50k
        if (ctx == NULL) {
170
4.50k
            ctx = new_ctx = BN_CTX_new();
171
4.50k
            if (ctx == NULL)
172
0
                return 0;
173
4.50k
        }
174
4.50k
#endif
175
176
4.50k
        BN_CTX_start(ctx);
177
4.50k
        used_ctx = 1;
178
4.50k
        x = BN_CTX_get(ctx);
179
4.50k
        y = BN_CTX_get(ctx);
180
4.50k
        yxi = BN_CTX_get(ctx);
181
4.50k
        if (yxi == NULL)
182
0
            goto err;
183
184
4.50k
        if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
185
0
            goto err;
186
187
4.50k
        buf[0] = form;
188
4.50k
        if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) {
189
1.08k
            if (!group->meth->field_div(group, yxi, y, x, ctx))
190
0
                goto err;
191
1.08k
            if (BN_is_odd(yxi))
192
580
                buf[0]++;
193
1.08k
        }
194
195
4.50k
        i = 1;
196
197
4.50k
        skip = field_len - BN_num_bytes(x);
198
4.50k
        if (skip > field_len) {
199
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
200
0
            goto err;
201
0
        }
202
14.2k
        while (skip > 0) {
203
9.71k
            buf[i++] = 0;
204
9.71k
            skip--;
205
9.71k
        }
206
4.50k
        skip = BN_bn2bin(x, buf + i);
207
4.50k
        i += skip;
208
4.50k
        if (i != 1 + field_len) {
209
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
210
0
            goto err;
211
0
        }
212
213
4.50k
        if (form == POINT_CONVERSION_UNCOMPRESSED
214
3.34k
            || form == POINT_CONVERSION_HYBRID) {
215
3.34k
            skip = field_len - BN_num_bytes(y);
216
3.34k
            if (skip > field_len) {
217
0
                ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
218
0
                goto err;
219
0
            }
220
5.13k
            while (skip > 0) {
221
1.79k
                buf[i++] = 0;
222
1.79k
                skip--;
223
1.79k
            }
224
3.34k
            skip = BN_bn2bin(y, buf + i);
225
3.34k
            i += skip;
226
3.34k
        }
227
228
4.50k
        if (i != ret) {
229
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
230
0
            goto err;
231
0
        }
232
4.50k
    }
233
234
9.00k
    if (used_ctx)
235
4.50k
        BN_CTX_end(ctx);
236
9.00k
#ifndef FIPS_MODULE
237
9.00k
    BN_CTX_free(new_ctx);
238
9.00k
#endif
239
9.00k
    return ret;
240
241
269
err:
242
269
    if (used_ctx)
243
0
        BN_CTX_end(ctx);
244
269
#ifndef FIPS_MODULE
245
269
    BN_CTX_free(new_ctx);
246
269
#endif
247
269
    return 0;
248
9.00k
}
249
250
/*
251
 * Converts an octet string representation to an EC_POINT. Note that the
252
 * simple implementation only uses affine coordinates.
253
 */
254
int ossl_ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
255
    const unsigned char *buf, size_t len,
256
    BN_CTX *ctx)
257
327k
{
258
327k
    point_conversion_form_t form;
259
327k
    int y_bit, m;
260
327k
    BIGNUM *x, *y, *yxi;
261
327k
    size_t field_len, enc_len;
262
327k
    int ret = 0;
263
327k
#ifndef FIPS_MODULE
264
327k
    BN_CTX *new_ctx = NULL;
265
327k
#endif
266
267
327k
    if (len == 0) {
268
5.12k
        ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
269
5.12k
        return 0;
270
5.12k
    }
271
272
    /*
273
     * The first octet is the point conversion octet PC, see X9.62, page 4
274
     * and section 4.4.2.  It must be:
275
     *     0x00          for the point at infinity
276
     *     0x02 or 0x03  for compressed form
277
     *     0x04          for uncompressed form
278
     *     0x06 or 0x07  for hybrid form.
279
     * For compressed or hybrid forms, we store the last bit of buf[0] as
280
     * y_bit and clear it from buf[0] so as to obtain a POINT_CONVERSION_*.
281
     * We error if buf[0] contains any but the above values.
282
     */
283
322k
    y_bit = buf[0] & 1;
284
322k
    form = buf[0] & ~1U;
285
286
322k
    if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
287
53.8k
        && (form != POINT_CONVERSION_UNCOMPRESSED)
288
51.4k
        && (form != POINT_CONVERSION_HYBRID)) {
289
7.98k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
290
7.98k
        return 0;
291
7.98k
    }
292
314k
    if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
293
2.45k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
294
2.45k
        return 0;
295
2.45k
    }
296
297
    /* The point at infinity is represented by a single zero octet. */
298
311k
    if (form == 0) {
299
36.4k
        if (len != 1) {
300
3.79k
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
301
3.79k
            return 0;
302
3.79k
        }
303
304
32.6k
        return EC_POINT_set_to_infinity(group, point);
305
36.4k
    }
306
307
275k
    m = EC_GROUP_get_degree(group);
308
275k
    field_len = (m + 7) / 8;
309
275k
    enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
310
311
275k
    if (len != enc_len) {
312
21.1k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
313
21.1k
        return 0;
314
21.1k
    }
315
316
254k
#ifndef FIPS_MODULE
317
254k
    if (ctx == NULL) {
318
254k
        ctx = new_ctx = BN_CTX_new();
319
254k
        if (ctx == NULL)
320
0
            return 0;
321
254k
    }
322
254k
#endif
323
324
254k
    BN_CTX_start(ctx);
325
254k
    x = BN_CTX_get(ctx);
326
254k
    y = BN_CTX_get(ctx);
327
254k
    yxi = BN_CTX_get(ctx);
328
254k
    if (yxi == NULL)
329
0
        goto err;
330
331
254k
    if (!BN_bin2bn(buf + 1, field_len, x))
332
0
        goto err;
333
254k
    if (BN_num_bits(x) > m) {
334
15.5k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
335
15.5k
        goto err;
336
15.5k
    }
337
338
238k
    if (form == POINT_CONVERSION_COMPRESSED) {
339
204k
        if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
340
66.9k
            goto err;
341
204k
    } else {
342
34.3k
        if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
343
0
            goto err;
344
34.3k
        if (BN_num_bits(y) > m) {
345
1.13k
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
346
1.13k
            goto err;
347
1.13k
        }
348
33.2k
        if (form == POINT_CONVERSION_HYBRID) {
349
            /*
350
             * Check that the form in the encoding was set correctly
351
             * according to X9.62 4.4.2.a, 4(c), see also first paragraph
352
             * of X9.62, 4.4.1.b.
353
             */
354
32.4k
            if (BN_is_zero(x)) {
355
1.14k
                if (y_bit != 0) {
356
291
                    ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
357
291
                    goto err;
358
291
                }
359
31.3k
            } else {
360
31.3k
                if (!group->meth->field_div(group, yxi, y, x, ctx))
361
0
                    goto err;
362
31.3k
                if (y_bit != BN_is_odd(yxi)) {
363
14.6k
                    ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
364
14.6k
                    goto err;
365
14.6k
                }
366
31.3k
            }
367
32.4k
        }
368
369
        /*
370
         * EC_POINT_set_affine_coordinates is responsible for checking that
371
         * the point is on the curve.
372
         */
373
18.2k
        if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
374
18.1k
            goto err;
375
18.2k
    }
376
377
137k
    ret = 1;
378
379
254k
err:
380
254k
    BN_CTX_end(ctx);
381
254k
#ifndef FIPS_MODULE
382
254k
    BN_CTX_free(new_ctx);
383
254k
#endif
384
254k
    return ret;
385
137k
}
386
#endif