Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/ec/ecp_oct.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2011-2018 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
#include <openssl/symhacks.h>
13
14
#include "ec_local.h"
15
16
int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
17
                                             EC_POINT *point,
18
                                             const BIGNUM *x_, int y_bit,
19
                                             BN_CTX *ctx)
20
6.31k
{
21
6.31k
    BN_CTX *new_ctx = NULL;
22
6.31k
    BIGNUM *tmp1, *tmp2, *x, *y;
23
6.31k
    int ret = 0;
24
25
    /* clear error queue */
26
6.31k
    ERR_clear_error();
27
28
6.31k
    if (ctx == NULL) {
29
0
        ctx = new_ctx = BN_CTX_new();
30
0
        if (ctx == NULL)
31
0
            return 0;
32
0
    }
33
34
6.31k
    y_bit = (y_bit != 0);
35
36
6.31k
    BN_CTX_start(ctx);
37
6.31k
    tmp1 = BN_CTX_get(ctx);
38
6.31k
    tmp2 = BN_CTX_get(ctx);
39
6.31k
    x = BN_CTX_get(ctx);
40
6.31k
    y = BN_CTX_get(ctx);
41
6.31k
    if (y == NULL)
42
0
        goto err;
43
44
    /*-
45
     * Recover y.  We have a Weierstrass equation
46
     *     y^2 = x^3 + a*x + b,
47
     * so  y  is one of the square roots of  x^3 + a*x + b.
48
     */
49
50
    /* tmp1 := x^3 */
51
6.31k
    if (!BN_nnmod(x, x_, group->field, ctx))
52
0
        goto err;
53
6.31k
    if (group->meth->field_decode == 0) {
54
        /* field_{sqr,mul} work on standard representation */
55
3.02k
        if (!group->meth->field_sqr(group, tmp2, x_, ctx))
56
0
            goto err;
57
3.02k
        if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))
58
0
            goto err;
59
3.28k
    } else {
60
3.28k
        if (!BN_mod_sqr(tmp2, x_, group->field, ctx))
61
0
            goto err;
62
3.28k
        if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))
63
0
            goto err;
64
3.28k
    }
65
66
    /* tmp1 := tmp1 + a*x */
67
6.31k
    if (group->a_is_minus3) {
68
4.00k
        if (!BN_mod_lshift1_quick(tmp2, x, group->field))
69
0
            goto err;
70
4.00k
        if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))
71
0
            goto err;
72
4.00k
        if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))
73
0
            goto err;
74
4.00k
    } else {
75
2.31k
        if (group->meth->field_decode) {
76
2.31k
            if (!group->meth->field_decode(group, tmp2, group->a, ctx))
77
0
                goto err;
78
2.31k
            if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))
79
0
                goto err;
80
2.31k
        } else {
81
            /* field_mul works on standard representation */
82
0
            if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))
83
0
                goto err;
84
0
        }
85
86
2.31k
        if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
87
0
            goto err;
88
2.31k
    }
89
90
    /* tmp1 := tmp1 + b */
91
6.31k
    if (group->meth->field_decode) {
92
3.28k
        if (!group->meth->field_decode(group, tmp2, group->b, ctx))
93
0
            goto err;
94
3.28k
        if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
95
0
            goto err;
96
3.28k
    } else {
97
3.02k
        if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))
98
0
            goto err;
99
3.02k
    }
100
101
6.31k
    if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {
102
2.34k
        unsigned long err = ERR_peek_last_error();
103
104
2.34k
        if (ERR_GET_LIB(err) == ERR_LIB_BN
105
2.34k
            && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
106
2.32k
            ERR_clear_error();
107
2.32k
            ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
108
2.32k
                  EC_R_INVALID_COMPRESSED_POINT);
109
2.32k
        } else
110
2.34k
            ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
111
2.34k
                  ERR_R_BN_LIB);
112
2.34k
        goto err;
113
2.34k
    }
114
115
3.97k
    if (y_bit != BN_is_odd(y)) {
116
2.18k
        if (BN_is_zero(y)) {
117
0
            int kron;
118
119
0
            kron = BN_kronecker(x, group->field, ctx);
120
0
            if (kron == -2)
121
0
                goto err;
122
123
0
            if (kron == 1)
124
0
                ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
125
0
                      EC_R_INVALID_COMPRESSION_BIT);
126
0
            else
127
                /*
128
                 * BN_mod_sqrt() should have caught this error (not a square)
129
                 */
130
0
                ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
131
0
                      EC_R_INVALID_COMPRESSED_POINT);
132
0
            goto err;
133
0
        }
134
2.18k
        if (!BN_usub(y, group->field, y))
135
0
            goto err;
136
2.18k
    }
137
3.97k
    if (y_bit != BN_is_odd(y)) {
138
0
        ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
139
0
              ERR_R_INTERNAL_ERROR);
140
0
        goto err;
141
0
    }
142
143
3.97k
    if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
144
0
        goto err;
145
146
3.97k
    ret = 1;
147
148
6.31k
 err:
149
6.31k
    BN_CTX_end(ctx);
150
6.31k
    BN_CTX_free(new_ctx);
151
6.31k
    return ret;
152
3.97k
}
153
154
size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
155
                               point_conversion_form_t form,
156
                               unsigned char *buf, size_t len, BN_CTX *ctx)
157
1.08k
{
158
1.08k
    size_t ret;
159
1.08k
    BN_CTX *new_ctx = NULL;
160
1.08k
    int used_ctx = 0;
161
1.08k
    BIGNUM *x, *y;
162
1.08k
    size_t field_len, i, skip;
163
164
1.08k
    if ((form != POINT_CONVERSION_COMPRESSED)
165
1.08k
        && (form != POINT_CONVERSION_UNCOMPRESSED)
166
1.08k
        && (form != POINT_CONVERSION_HYBRID)) {
167
0
        ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
168
0
        goto err;
169
0
    }
170
171
1.08k
    if (EC_POINT_is_at_infinity(group, point)) {
172
        /* encodes to a single 0 octet */
173
40
        if (buf != NULL) {
174
20
            if (len < 1) {
175
0
                ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
176
0
                return 0;
177
0
            }
178
20
            buf[0] = 0;
179
20
        }
180
40
        return 1;
181
40
    }
182
183
    /* ret := required output buffer length */
184
1.04k
    field_len = BN_num_bytes(group->field);
185
1.04k
    ret =
186
1.04k
        (form ==
187
1.04k
         POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
188
189
    /* if 'buf' is NULL, just return required length */
190
1.04k
    if (buf != NULL) {
191
521
        if (len < ret) {
192
0
            ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
193
0
            goto err;
194
0
        }
195
196
521
        if (ctx == NULL) {
197
418
            ctx = new_ctx = BN_CTX_new();
198
418
            if (ctx == NULL)
199
0
                return 0;
200
418
        }
201
202
521
        BN_CTX_start(ctx);
203
521
        used_ctx = 1;
204
521
        x = BN_CTX_get(ctx);
205
521
        y = BN_CTX_get(ctx);
206
521
        if (y == NULL)
207
0
            goto err;
208
209
521
        if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
210
0
            goto err;
211
212
521
        if ((form == POINT_CONVERSION_COMPRESSED
213
521
             || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
214
36
            buf[0] = form + 1;
215
485
        else
216
485
            buf[0] = form;
217
218
521
        i = 1;
219
220
521
        skip = field_len - BN_num_bytes(x);
221
521
        if (skip > field_len) {
222
0
            ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
223
0
            goto err;
224
0
        }
225
876
        while (skip > 0) {
226
355
            buf[i++] = 0;
227
355
            skip--;
228
355
        }
229
521
        skip = BN_bn2bin(x, buf + i);
230
521
        i += skip;
231
521
        if (i != 1 + field_len) {
232
0
            ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
233
0
            goto err;
234
0
        }
235
236
521
        if (form == POINT_CONVERSION_UNCOMPRESSED
237
521
            || form == POINT_CONVERSION_HYBRID) {
238
323
            skip = field_len - BN_num_bytes(y);
239
323
            if (skip > field_len) {
240
0
                ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
241
0
                goto err;
242
0
            }
243
370
            while (skip > 0) {
244
47
                buf[i++] = 0;
245
47
                skip--;
246
47
            }
247
323
            skip = BN_bn2bin(y, buf + i);
248
323
            i += skip;
249
323
        }
250
251
521
        if (i != ret) {
252
0
            ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
253
0
            goto err;
254
0
        }
255
521
    }
256
257
1.04k
    if (used_ctx)
258
521
        BN_CTX_end(ctx);
259
1.04k
    BN_CTX_free(new_ctx);
260
1.04k
    return ret;
261
262
0
 err:
263
0
    if (used_ctx)
264
0
        BN_CTX_end(ctx);
265
0
    BN_CTX_free(new_ctx);
266
0
    return 0;
267
1.04k
}
268
269
int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
270
                            const unsigned char *buf, size_t len, BN_CTX *ctx)
271
12.2k
{
272
12.2k
    point_conversion_form_t form;
273
12.2k
    int y_bit;
274
12.2k
    BN_CTX *new_ctx = NULL;
275
12.2k
    BIGNUM *x, *y;
276
12.2k
    size_t field_len, enc_len;
277
12.2k
    int ret = 0;
278
279
12.2k
    if (len == 0) {
280
325
        ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
281
325
        return 0;
282
325
    }
283
11.8k
    form = buf[0];
284
11.8k
    y_bit = form & 1;
285
11.8k
    form = form & ~1U;
286
11.8k
    if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
287
11.8k
        && (form != POINT_CONVERSION_UNCOMPRESSED)
288
11.8k
        && (form != POINT_CONVERSION_HYBRID)) {
289
261
        ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
290
261
        return 0;
291
261
    }
292
11.6k
    if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
293
84
        ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
294
84
        return 0;
295
84
    }
296
297
11.5k
    if (form == 0) {
298
385
        if (len != 1) {
299
91
            ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
300
91
            return 0;
301
91
        }
302
303
294
        return EC_POINT_set_to_infinity(group, point);
304
385
    }
305
306
11.1k
    field_len = BN_num_bytes(group->field);
307
11.1k
    enc_len =
308
11.1k
        (form ==
309
11.1k
         POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
310
311
11.1k
    if (len != enc_len) {
312
393
        ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
313
393
        return 0;
314
393
    }
315
316
10.7k
    if (ctx == NULL) {
317
10.7k
        ctx = new_ctx = BN_CTX_new();
318
10.7k
        if (ctx == NULL)
319
0
            return 0;
320
10.7k
    }
321
322
10.7k
    BN_CTX_start(ctx);
323
10.7k
    x = BN_CTX_get(ctx);
324
10.7k
    y = BN_CTX_get(ctx);
325
10.7k
    if (y == NULL)
326
0
        goto err;
327
328
10.7k
    if (!BN_bin2bn(buf + 1, field_len, x))
329
0
        goto err;
330
10.7k
    if (BN_ucmp(x, group->field) >= 0) {
331
77
        ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
332
77
        goto err;
333
77
    }
334
335
10.6k
    if (form == POINT_CONVERSION_COMPRESSED) {
336
6.31k
        if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
337
2.34k
            goto err;
338
6.31k
    } else {
339
4.36k
        if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
340
0
            goto err;
341
4.36k
        if (BN_ucmp(y, group->field) >= 0) {
342
67
            ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
343
67
            goto err;
344
67
        }
345
4.30k
        if (form == POINT_CONVERSION_HYBRID) {
346
378
            if (y_bit != BN_is_odd(y)) {
347
304
                ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
348
304
                goto err;
349
304
            }
350
378
        }
351
352
        /*
353
         * EC_POINT_set_affine_coordinates is responsible for checking that
354
         * the point is on the curve.
355
         */
356
3.99k
        if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
357
2.08k
            goto err;
358
3.99k
    }
359
360
5.88k
    ret = 1;
361
362
10.7k
 err:
363
10.7k
    BN_CTX_end(ctx);
364
10.7k
    BN_CTX_free(new_ctx);
365
10.7k
    return ret;
366
5.88k
}