Coverage Report

Created: 2025-06-13 06:58

/src/openssl31/crypto/ec/ec2_oct.c
Line
Count
Source (jump to first uncovered line)
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
97.3k
{
44
97.3k
    BIGNUM *tmp, *x, *y, *z;
45
97.3k
    int ret = 0, z0;
46
97.3k
#ifndef FIPS_MODULE
47
97.3k
    BN_CTX *new_ctx = NULL;
48
49
97.3k
    if (ctx == NULL) {
50
0
        ctx = new_ctx = BN_CTX_new();
51
0
        if (ctx == NULL)
52
0
            return 0;
53
0
    }
54
97.3k
#endif
55
56
97.3k
    y_bit = (y_bit != 0) ? 1 : 0;
57
58
97.3k
    BN_CTX_start(ctx);
59
97.3k
    tmp = BN_CTX_get(ctx);
60
97.3k
    x = BN_CTX_get(ctx);
61
97.3k
    y = BN_CTX_get(ctx);
62
97.3k
    z = BN_CTX_get(ctx);
63
97.3k
    if (z == NULL)
64
0
        goto err;
65
66
97.3k
    if (!BN_GF2m_mod_arr(x, x_, group->poly))
67
0
        goto err;
68
97.3k
    if (BN_is_zero(x)) {
69
13.8k
        if (!BN_GF2m_mod_sqrt_arr(y, group->b, group->poly, ctx))
70
0
            goto err;
71
83.5k
    } else {
72
83.5k
        if (!group->meth->field_sqr(group, tmp, x, ctx))
73
0
            goto err;
74
83.5k
        if (!group->meth->field_div(group, tmp, group->b, tmp, ctx))
75
0
            goto err;
76
83.5k
        if (!BN_GF2m_add(tmp, group->a, tmp))
77
0
            goto err;
78
83.5k
        if (!BN_GF2m_add(tmp, x, tmp))
79
0
            goto err;
80
83.5k
        ERR_set_mark();
81
83.5k
        if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) {
82
31.1k
#ifndef FIPS_MODULE
83
31.1k
            unsigned long err = ERR_peek_last_error();
84
85
31.1k
            if (ERR_GET_LIB(err) == ERR_LIB_BN
86
31.1k
                && ERR_GET_REASON(err) == BN_R_NO_SOLUTION) {
87
26.9k
                ERR_pop_to_mark();
88
26.9k
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT);
89
26.9k
            } else
90
4.17k
#endif
91
4.17k
            {
92
4.17k
                ERR_clear_last_mark();
93
4.17k
                ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
94
4.17k
            }
95
31.1k
            goto err;
96
31.1k
        }
97
52.4k
        ERR_clear_last_mark();
98
52.4k
        z0 = (BN_is_odd(z)) ? 1 : 0;
99
52.4k
        if (!group->meth->field_mul(group, y, x, z, ctx))
100
0
            goto err;
101
52.4k
        if (z0 != y_bit) {
102
30.1k
            if (!BN_GF2m_add(y, y, x))
103
0
                goto err;
104
30.1k
        }
105
52.4k
    }
106
107
66.2k
    if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
108
0
        goto err;
109
110
66.2k
    ret = 1;
111
112
97.3k
 err:
113
97.3k
    BN_CTX_end(ctx);
114
97.3k
#ifndef FIPS_MODULE
115
97.3k
    BN_CTX_free(new_ctx);
116
97.3k
#endif
117
97.3k
    return ret;
118
66.2k
}
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
5.97k
{
130
5.97k
    size_t ret;
131
5.97k
    int used_ctx = 0;
132
5.97k
    BIGNUM *x, *y, *yxi;
133
5.97k
    size_t field_len, i, skip;
134
5.97k
#ifndef FIPS_MODULE
135
5.97k
    BN_CTX *new_ctx = NULL;
136
5.97k
#endif
137
138
5.97k
    if ((form != POINT_CONVERSION_COMPRESSED)
139
5.97k
        && (form != POINT_CONVERSION_UNCOMPRESSED)
140
5.97k
        && (form != POINT_CONVERSION_HYBRID)) {
141
345
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
142
345
        goto err;
143
345
    }
144
145
5.63k
    if (EC_POINT_is_at_infinity(group, point)) {
146
        /* encodes to a single 0 octet */
147
592
        if (buf != NULL) {
148
296
            if (len < 1) {
149
0
                ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
150
0
                return 0;
151
0
            }
152
296
            buf[0] = 0;
153
296
        }
154
592
        return 1;
155
592
    }
156
157
    /* ret := required output buffer length */
158
5.03k
    field_len = (EC_GROUP_get_degree(group) + 7) / 8;
159
5.03k
    ret =
160
5.03k
        (form ==
161
5.03k
         POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
162
163
    /* if 'buf' is NULL, just return required length */
164
5.03k
    if (buf != NULL) {
165
2.51k
        if (len < ret) {
166
0
            ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
167
0
            goto err;
168
0
        }
169
170
2.51k
#ifndef FIPS_MODULE
171
2.51k
        if (ctx == NULL) {
172
2.51k
            ctx = new_ctx = BN_CTX_new();
173
2.51k
            if (ctx == NULL)
174
0
                return 0;
175
2.51k
        }
176
2.51k
#endif
177
178
2.51k
        BN_CTX_start(ctx);
179
2.51k
        used_ctx = 1;
180
2.51k
        x = BN_CTX_get(ctx);
181
2.51k
        y = BN_CTX_get(ctx);
182
2.51k
        yxi = BN_CTX_get(ctx);
183
2.51k
        if (yxi == NULL)
184
0
            goto err;
185
186
2.51k
        if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
187
0
            goto err;
188
189
2.51k
        buf[0] = form;
190
2.51k
        if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) {
191
552
            if (!group->meth->field_div(group, yxi, y, x, ctx))
192
0
                goto err;
193
552
            if (BN_is_odd(yxi))
194
277
                buf[0]++;
195
552
        }
196
197
2.51k
        i = 1;
198
199
2.51k
        skip = field_len - BN_num_bytes(x);
200
2.51k
        if (skip > field_len) {
201
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
202
0
            goto err;
203
0
        }
204
7.35k
        while (skip > 0) {
205
4.83k
            buf[i++] = 0;
206
4.83k
            skip--;
207
4.83k
        }
208
2.51k
        skip = BN_bn2bin(x, buf + i);
209
2.51k
        i += skip;
210
2.51k
        if (i != 1 + field_len) {
211
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
212
0
            goto err;
213
0
        }
214
215
2.51k
        if (form == POINT_CONVERSION_UNCOMPRESSED
216
2.51k
            || form == POINT_CONVERSION_HYBRID) {
217
1.93k
            skip = field_len - BN_num_bytes(y);
218
1.93k
            if (skip > field_len) {
219
0
                ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
220
0
                goto err;
221
0
            }
222
3.08k
            while (skip > 0) {
223
1.14k
                buf[i++] = 0;
224
1.14k
                skip--;
225
1.14k
            }
226
1.93k
            skip = BN_bn2bin(y, buf + i);
227
1.93k
            i += skip;
228
1.93k
        }
229
230
2.51k
        if (i != ret) {
231
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
232
0
            goto err;
233
0
        }
234
2.51k
    }
235
236
5.03k
    if (used_ctx)
237
2.51k
        BN_CTX_end(ctx);
238
5.03k
#ifndef FIPS_MODULE
239
5.03k
    BN_CTX_free(new_ctx);
240
5.03k
#endif
241
5.03k
    return ret;
242
243
345
 err:
244
345
    if (used_ctx)
245
0
        BN_CTX_end(ctx);
246
345
#ifndef FIPS_MODULE
247
345
    BN_CTX_free(new_ctx);
248
345
#endif
249
345
    return 0;
250
5.03k
}
251
252
/*
253
 * Converts an octet string representation to an EC_POINT. Note that the
254
 * simple implementation only uses affine coordinates.
255
 */
256
int ossl_ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
257
                                  const unsigned char *buf, size_t len,
258
                                  BN_CTX *ctx)
259
194k
{
260
194k
    point_conversion_form_t form;
261
194k
    int y_bit, m;
262
194k
    BIGNUM *x, *y, *yxi;
263
194k
    size_t field_len, enc_len;
264
194k
    int ret = 0;
265
194k
#ifndef FIPS_MODULE
266
194k
    BN_CTX *new_ctx = NULL;
267
194k
#endif
268
269
194k
    if (len == 0) {
270
5.62k
        ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
271
5.62k
        return 0;
272
5.62k
    }
273
274
    /*
275
     * The first octet is the point conversion octet PC, see X9.62, page 4
276
     * and section 4.4.2.  It must be:
277
     *     0x00          for the point at infinity
278
     *     0x02 or 0x03  for compressed form
279
     *     0x04          for uncompressed form
280
     *     0x06 or 0x07  for hybrid form.
281
     * For compressed or hybrid forms, we store the last bit of buf[0] as
282
     * y_bit and clear it from buf[0] so as to obtain a POINT_CONVERSION_*.
283
     * We error if buf[0] contains any but the above values.
284
     */
285
188k
    y_bit = buf[0] & 1;
286
188k
    form = buf[0] & ~1U;
287
288
188k
    if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
289
188k
        && (form != POINT_CONVERSION_UNCOMPRESSED)
290
188k
        && (form != POINT_CONVERSION_HYBRID)) {
291
4.44k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
292
4.44k
        return 0;
293
4.44k
    }
294
183k
    if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
295
1.45k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
296
1.45k
        return 0;
297
1.45k
    }
298
299
    /* The point at infinity is represented by a single zero octet. */
300
182k
    if (form == 0) {
301
42.8k
        if (len != 1) {
302
3.88k
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
303
3.88k
            return 0;
304
3.88k
        }
305
306
38.9k
        return EC_POINT_set_to_infinity(group, point);
307
42.8k
    }
308
309
139k
    m = EC_GROUP_get_degree(group);
310
139k
    field_len = (m + 7) / 8;
311
139k
    enc_len =
312
139k
        (form ==
313
139k
         POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
314
315
139k
    if (len != enc_len) {
316
8.03k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
317
8.03k
        return 0;
318
8.03k
    }
319
320
131k
#ifndef FIPS_MODULE
321
131k
    if (ctx == NULL) {
322
131k
        ctx = new_ctx = BN_CTX_new();
323
131k
        if (ctx == NULL)
324
0
            return 0;
325
131k
    }
326
131k
#endif
327
328
131k
    BN_CTX_start(ctx);
329
131k
    x = BN_CTX_get(ctx);
330
131k
    y = BN_CTX_get(ctx);
331
131k
    yxi = BN_CTX_get(ctx);
332
131k
    if (yxi == NULL)
333
0
        goto err;
334
335
131k
    if (!BN_bin2bn(buf + 1, field_len, x))
336
0
        goto err;
337
131k
    if (BN_num_bits(x) > m) {
338
5.84k
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
339
5.84k
        goto err;
340
5.84k
    }
341
342
125k
    if (form == POINT_CONVERSION_COMPRESSED) {
343
97.3k
        if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
344
31.1k
            goto err;
345
97.3k
    } else {
346
28.3k
        if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
347
0
            goto err;
348
28.3k
        if (BN_num_bits(y) > m) {
349
1.44k
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
350
1.44k
            goto err;
351
1.44k
        }
352
26.9k
        if (form == POINT_CONVERSION_HYBRID) {
353
            /*
354
             * Check that the form in the encoding was set correctly
355
             * according to X9.62 4.4.2.a, 4(c), see also first paragraph
356
             * of X9.62, 4.4.1.b.
357
             */
358
25.3k
            if (BN_is_zero(x)) {
359
1.79k
                if (y_bit != 0) {
360
312
                    ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
361
312
                    goto err;
362
312
                }
363
23.5k
            } else {
364
23.5k
                if (!group->meth->field_div(group, yxi, y, x, ctx))
365
0
                    goto err;
366
23.5k
                if (y_bit != BN_is_odd(yxi)) {
367
8.98k
                    ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
368
8.98k
                    goto err;
369
8.98k
                }
370
23.5k
            }
371
25.3k
        }
372
373
        /*
374
         * EC_POINT_set_affine_coordinates is responsible for checking that
375
         * the point is on the curve.
376
         */
377
17.6k
        if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
378
17.6k
            goto err;
379
17.6k
    }
380
381
66.2k
    ret = 1;
382
383
131k
 err:
384
131k
    BN_CTX_end(ctx);
385
131k
#ifndef FIPS_MODULE
386
131k
    BN_CTX_free(new_ctx);
387
131k
#endif
388
131k
    return ret;
389
66.2k
}
390
#endif