Coverage Report

Created: 2023-09-25 06:45

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