Coverage Report

Created: 2023-09-25 06:45

/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
19.3k
{
21
19.3k
    BN_CTX *new_ctx = NULL;
22
19.3k
    BIGNUM *tmp1, *tmp2, *x, *y;
23
19.3k
    int ret = 0;
24
25
    /* clear error queue */
26
19.3k
    ERR_clear_error();
27
28
19.3k
    if (ctx == NULL) {
29
0
        ctx = new_ctx = BN_CTX_new();
30
0
        if (ctx == NULL)
31
0
            return 0;
32
0
    }
33
34
19.3k
    y_bit = (y_bit != 0);
35
36
19.3k
    BN_CTX_start(ctx);
37
19.3k
    tmp1 = BN_CTX_get(ctx);
38
19.3k
    tmp2 = BN_CTX_get(ctx);
39
19.3k
    x = BN_CTX_get(ctx);
40
19.3k
    y = BN_CTX_get(ctx);
41
19.3k
    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
19.3k
    if (!BN_nnmod(x, x_, group->field, ctx))
52
0
        goto err;
53
19.3k
    if (group->meth->field_decode == 0) {
54
        /* field_{sqr,mul} work on standard representation */
55
9.92k
        if (!group->meth->field_sqr(group, tmp2, x_, ctx))
56
0
            goto err;
57
9.92k
        if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))
58
0
            goto err;
59
9.92k
    } else {
60
9.39k
        if (!BN_mod_sqr(tmp2, x_, group->field, ctx))
61
0
            goto err;
62
9.39k
        if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))
63
0
            goto err;
64
9.39k
    }
65
66
    /* tmp1 := tmp1 + a*x */
67
19.3k
    if (group->a_is_minus3) {
68
13.4k
        if (!BN_mod_lshift1_quick(tmp2, x, group->field))
69
0
            goto err;
70
13.4k
        if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))
71
0
            goto err;
72
13.4k
        if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))
73
0
            goto err;
74
13.4k
    } else {
75
5.91k
        if (group->meth->field_decode) {
76
5.91k
            if (!group->meth->field_decode(group, tmp2, group->a, ctx))
77
0
                goto err;
78
5.91k
            if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))
79
0
                goto err;
80
5.91k
        } 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
5.91k
        if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
87
0
            goto err;
88
5.91k
    }
89
90
    /* tmp1 := tmp1 + b */
91
19.3k
    if (group->meth->field_decode) {
92
9.39k
        if (!group->meth->field_decode(group, tmp2, group->b, ctx))
93
0
            goto err;
94
9.39k
        if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
95
0
            goto err;
96
9.92k
    } else {
97
9.92k
        if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))
98
0
            goto err;
99
9.92k
    }
100
101
19.3k
    if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {
102
6.48k
        unsigned long err = ERR_peek_last_error();
103
104
6.48k
        if (ERR_GET_LIB(err) == ERR_LIB_BN
105
6.48k
            && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
106
6.45k
            ERR_clear_error();
107
6.45k
            ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
108
6.45k
                  EC_R_INVALID_COMPRESSED_POINT);
109
6.45k
        } else
110
6.48k
            ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
111
6.48k
                  ERR_R_BN_LIB);
112
6.48k
        goto err;
113
6.48k
    }
114
115
12.8k
    if (y_bit != BN_is_odd(y)) {
116
7.23k
        if (BN_is_zero(y)) {
117
2
            int kron;
118
119
2
            kron = BN_kronecker(x, group->field, ctx);
120
2
            if (kron == -2)
121
0
                goto err;
122
123
2
            if (kron == 1)
124
2
                ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
125
2
                      EC_R_INVALID_COMPRESSION_BIT);
126
2
            else
127
                /*
128
                 * BN_mod_sqrt() should have caught this error (not a square)
129
                 */
130
2
                ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
131
2
                      EC_R_INVALID_COMPRESSED_POINT);
132
2
            goto err;
133
2
        }
134
7.23k
        if (!BN_usub(y, group->field, y))
135
0
            goto err;
136
7.23k
    }
137
12.8k
    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
12.8k
    if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
144
0
        goto err;
145
146
12.8k
    ret = 1;
147
148
19.3k
 err:
149
19.3k
    BN_CTX_end(ctx);
150
19.3k
    BN_CTX_free(new_ctx);
151
19.3k
    return ret;
152
12.8k
}
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
3.30k
{
158
3.30k
    size_t ret;
159
3.30k
    BN_CTX *new_ctx = NULL;
160
3.30k
    int used_ctx = 0;
161
3.30k
    BIGNUM *x, *y;
162
3.30k
    size_t field_len, i, skip;
163
164
3.30k
    if ((form != POINT_CONVERSION_COMPRESSED)
165
3.30k
        && (form != POINT_CONVERSION_UNCOMPRESSED)
166
3.30k
        && (form != POINT_CONVERSION_HYBRID)) {
167
7
        ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
168
7
        goto err;
169
7
    }
170
171
3.29k
    if (EC_POINT_is_at_infinity(group, point)) {
172
        /* encodes to a single 0 octet */
173
38
        if (buf != NULL) {
174
19
            if (len < 1) {
175
0
                ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
176
0
                return 0;
177
0
            }
178
19
            buf[0] = 0;
179
19
        }
180
38
        return 1;
181
38
    }
182
183
    /* ret := required output buffer length */
184
3.25k
    field_len = BN_num_bytes(group->field);
185
3.25k
    ret =
186
3.25k
        (form ==
187
3.25k
         POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
188
189
    /* if 'buf' is NULL, just return required length */
190
3.25k
    if (buf != NULL) {
191
1.62k
        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
1.62k
        if (ctx == NULL) {
197
1.49k
            ctx = new_ctx = BN_CTX_new();
198
1.49k
            if (ctx == NULL)
199
0
                return 0;
200
1.49k
        }
201
202
1.62k
        BN_CTX_start(ctx);
203
1.62k
        used_ctx = 1;
204
1.62k
        x = BN_CTX_get(ctx);
205
1.62k
        y = BN_CTX_get(ctx);
206
1.62k
        if (y == NULL)
207
0
            goto err;
208
209
1.62k
        if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
210
0
            goto err;
211
212
1.62k
        if ((form == POINT_CONVERSION_COMPRESSED
213
1.62k
             || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
214
105
            buf[0] = form + 1;
215
1.52k
        else
216
1.52k
            buf[0] = form;
217
218
1.62k
        i = 1;
219
220
1.62k
        skip = field_len - BN_num_bytes(x);
221
1.62k
        if (skip > field_len) {
222
0
            ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
223
0
            goto err;
224
0
        }
225
3.38k
        while (skip > 0) {
226
1.75k
            buf[i++] = 0;
227
1.75k
            skip--;
228
1.75k
        }
229
1.62k
        skip = BN_bn2bin(x, buf + i);
230
1.62k
        i += skip;
231
1.62k
        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
1.62k
        if (form == POINT_CONVERSION_UNCOMPRESSED
237
1.62k
            || form == POINT_CONVERSION_HYBRID) {
238
1.16k
            skip = field_len - BN_num_bytes(y);
239
1.16k
            if (skip > field_len) {
240
0
                ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
241
0
                goto err;
242
0
            }
243
1.35k
            while (skip > 0) {
244
186
                buf[i++] = 0;
245
186
                skip--;
246
186
            }
247
1.16k
            skip = BN_bn2bin(y, buf + i);
248
1.16k
            i += skip;
249
1.16k
        }
250
251
1.62k
        if (i != ret) {
252
0
            ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
253
0
            goto err;
254
0
        }
255
1.62k
    }
256
257
3.25k
    if (used_ctx)
258
1.62k
        BN_CTX_end(ctx);
259
3.25k
    BN_CTX_free(new_ctx);
260
3.25k
    return ret;
261
262
7
 err:
263
7
    if (used_ctx)
264
0
        BN_CTX_end(ctx);
265
7
    BN_CTX_free(new_ctx);
266
7
    return 0;
267
3.25k
}
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
38.7k
{
272
38.7k
    point_conversion_form_t form;
273
38.7k
    int y_bit;
274
38.7k
    BN_CTX *new_ctx = NULL;
275
38.7k
    BIGNUM *x, *y;
276
38.7k
    size_t field_len, enc_len;
277
38.7k
    int ret = 0;
278
279
38.7k
    if (len == 0) {
280
621
        ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
281
621
        return 0;
282
621
    }
283
38.1k
    form = buf[0];
284
38.1k
    y_bit = form & 1;
285
38.1k
    form = form & ~1U;
286
38.1k
    if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
287
38.1k
        && (form != POINT_CONVERSION_UNCOMPRESSED)
288
38.1k
        && (form != POINT_CONVERSION_HYBRID)) {
289
503
        ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
290
503
        return 0;
291
503
    }
292
37.6k
    if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
293
168
        ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
294
168
        return 0;
295
168
    }
296
297
37.4k
    if (form == 0) {
298
712
        if (len != 1) {
299
239
            ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
300
239
            return 0;
301
239
        }
302
303
473
        return EC_POINT_set_to_infinity(group, point);
304
712
    }
305
306
36.7k
    field_len = BN_num_bytes(group->field);
307
36.7k
    enc_len =
308
36.7k
        (form ==
309
36.7k
         POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
310
311
36.7k
    if (len != enc_len) {
312
650
        ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
313
650
        return 0;
314
650
    }
315
316
36.0k
    if (ctx == NULL) {
317
36.0k
        ctx = new_ctx = BN_CTX_new();
318
36.0k
        if (ctx == NULL)
319
0
            return 0;
320
36.0k
    }
321
322
36.0k
    BN_CTX_start(ctx);
323
36.0k
    x = BN_CTX_get(ctx);
324
36.0k
    y = BN_CTX_get(ctx);
325
36.0k
    if (y == NULL)
326
0
        goto err;
327
328
36.0k
    if (!BN_bin2bn(buf + 1, field_len, x))
329
0
        goto err;
330
36.0k
    if (BN_ucmp(x, group->field) >= 0) {
331
133
        ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
332
133
        goto err;
333
133
    }
334
335
35.9k
    if (form == POINT_CONVERSION_COMPRESSED) {
336
19.3k
        if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
337
6.49k
            goto err;
338
19.3k
    } else {
339
16.6k
        if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
340
0
            goto err;
341
16.6k
        if (BN_ucmp(y, group->field) >= 0) {
342
96
            ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
343
96
            goto err;
344
96
        }
345
16.5k
        if (form == POINT_CONVERSION_HYBRID) {
346
302
            if (y_bit != BN_is_odd(y)) {
347
117
                ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
348
117
                goto err;
349
117
            }
350
302
        }
351
352
        /*
353
         * EC_POINT_set_affine_coordinates is responsible for checking that
354
         * the point is on the curve.
355
         */
356
16.4k
        if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
357
2.28k
            goto err;
358
16.4k
    }
359
360
26.9k
    ret = 1;
361
362
36.0k
 err:
363
36.0k
    BN_CTX_end(ctx);
364
36.0k
    BN_CTX_free(new_ctx);
365
36.0k
    return ret;
366
26.9k
}