Coverage Report

Created: 2023-09-25 06:42

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