Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/ec/ecp_oct.c
Line
Count
Source
1
/*
2
 * Copyright 2011-2025 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
0
{
27
0
    BN_CTX *new_ctx = NULL;
28
0
    BIGNUM *tmp1, *tmp2, *x, *y;
29
0
    int ret = 0;
30
31
0
    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
0
    y_bit = (y_bit != 0);
38
39
0
    BN_CTX_start(ctx);
40
0
    tmp1 = BN_CTX_get(ctx);
41
0
    tmp2 = BN_CTX_get(ctx);
42
0
    x = BN_CTX_get(ctx);
43
0
    y = BN_CTX_get(ctx);
44
0
    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
0
    if (!BN_nnmod(x, x_, group->field, ctx))
55
0
        goto err;
56
0
    if (group->meth->field_decode == 0) {
57
        /* field_{sqr,mul} work on standard representation */
58
0
        if (!group->meth->field_sqr(group, tmp2, x_, ctx))
59
0
            goto err;
60
0
        if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))
61
0
            goto err;
62
0
    } else {
63
0
        if (!BN_mod_sqr(tmp2, x_, group->field, ctx))
64
0
            goto err;
65
0
        if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))
66
0
            goto err;
67
0
    }
68
69
    /* tmp1 := tmp1 + a*x */
70
0
    if (group->a_is_minus3) {
71
0
        if (!BN_mod_lshift1_quick(tmp2, x, group->field))
72
0
            goto err;
73
0
        if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))
74
0
            goto err;
75
0
        if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))
76
0
            goto err;
77
0
    } else {
78
0
        if (group->meth->field_decode) {
79
0
            if (!group->meth->field_decode(group, tmp2, group->a, ctx))
80
0
                goto err;
81
0
            if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))
82
0
                goto err;
83
0
        } 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
0
        if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
90
0
            goto err;
91
0
    }
92
93
    /* tmp1 := tmp1 + b */
94
0
    if (group->meth->field_decode) {
95
0
        if (!group->meth->field_decode(group, tmp2, group->b, ctx))
96
0
            goto err;
97
0
        if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
98
0
            goto err;
99
0
    } else {
100
0
        if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))
101
0
            goto err;
102
0
    }
103
104
0
    ERR_set_mark();
105
0
    if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {
106
0
#ifndef FIPS_MODULE
107
0
        unsigned long err = ERR_peek_last_error();
108
109
0
        if (ERR_GET_LIB(err) == ERR_LIB_BN
110
0
            && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
111
0
            ERR_pop_to_mark();
112
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT);
113
0
        } else
114
0
#endif
115
0
        {
116
0
            ERR_clear_last_mark();
117
0
            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
118
0
        }
119
0
        goto err;
120
0
    }
121
0
    ERR_clear_last_mark();
122
123
0
    if (y_bit != BN_is_odd(y)) {
124
0
        if (BN_is_zero(y)) {
125
0
            int kron;
126
127
0
            kron = BN_kronecker(x, group->field, ctx);
128
0
            if (kron == -2)
129
0
                goto err;
130
131
0
            if (kron == 1)
132
0
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSION_BIT);
133
0
            else
134
                /*
135
                 * BN_mod_sqrt() should have caught this error (not a square)
136
                 */
137
0
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT);
138
0
            goto err;
139
0
        }
140
0
        if (!BN_usub(y, group->field, y))
141
0
            goto err;
142
0
    }
143
0
    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
0
    if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
149
0
        goto err;
150
151
0
    ret = 1;
152
153
0
err:
154
0
    BN_CTX_end(ctx);
155
0
    BN_CTX_free(new_ctx);
156
0
    return ret;
157
0
}
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
0
{
163
0
    size_t ret;
164
0
    BN_CTX *new_ctx = NULL;
165
0
    int used_ctx = 0;
166
0
    BIGNUM *x, *y;
167
0
    size_t field_len, i, skip;
168
169
0
    if ((form != POINT_CONVERSION_COMPRESSED)
170
0
        && (form != POINT_CONVERSION_UNCOMPRESSED)
171
0
        && (form != POINT_CONVERSION_HYBRID)) {
172
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
173
0
        goto err;
174
0
    }
175
176
0
    if (EC_POINT_is_at_infinity(group, point)) {
177
        /* encodes to a single 0 octet */
178
0
        if (buf != NULL) {
179
0
            if (len < 1) {
180
0
                ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
181
0
                return 0;
182
0
            }
183
0
            buf[0] = 0;
184
0
        }
185
0
        return 1;
186
0
    }
187
188
    /* ret := required output buffer length */
189
0
    field_len = BN_num_bytes(group->field);
190
0
    ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
191
192
    /* if 'buf' is NULL, just return required length */
193
0
    if (buf != NULL) {
194
0
        if (len < ret) {
195
0
            ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
196
0
            goto err;
197
0
        }
198
199
0
        if (ctx == NULL) {
200
0
            ctx = new_ctx = BN_CTX_new_ex(group->libctx);
201
0
            if (ctx == NULL)
202
0
                return 0;
203
0
        }
204
205
0
        BN_CTX_start(ctx);
206
0
        used_ctx = 1;
207
0
        x = BN_CTX_get(ctx);
208
0
        y = BN_CTX_get(ctx);
209
0
        if (y == NULL)
210
0
            goto err;
211
212
0
        if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
213
0
            goto err;
214
215
0
        if ((form == POINT_CONVERSION_COMPRESSED
216
0
                || form == POINT_CONVERSION_HYBRID)
217
0
            && BN_is_odd(y))
218
0
            buf[0] = form + 1;
219
0
        else
220
0
            buf[0] = form;
221
222
0
        i = 1;
223
224
0
        skip = field_len - BN_num_bytes(x);
225
0
        if (skip > field_len) {
226
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
227
0
            goto err;
228
0
        }
229
0
        while (skip > 0) {
230
0
            buf[i++] = 0;
231
0
            skip--;
232
0
        }
233
0
        skip = BN_bn2bin(x, buf + i);
234
0
        i += skip;
235
0
        if (i != 1 + field_len) {
236
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
237
0
            goto err;
238
0
        }
239
240
0
        if (form == POINT_CONVERSION_UNCOMPRESSED
241
0
            || form == POINT_CONVERSION_HYBRID) {
242
0
            skip = field_len - BN_num_bytes(y);
243
0
            if (skip > field_len) {
244
0
                ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
245
0
                goto err;
246
0
            }
247
0
            while (skip > 0) {
248
0
                buf[i++] = 0;
249
0
                skip--;
250
0
            }
251
0
            skip = BN_bn2bin(y, buf + i);
252
0
            i += skip;
253
0
        }
254
255
0
        if (i != ret) {
256
0
            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
257
0
            goto err;
258
0
        }
259
0
    }
260
261
0
    if (used_ctx)
262
0
        BN_CTX_end(ctx);
263
0
    BN_CTX_free(new_ctx);
264
0
    return ret;
265
266
0
err:
267
0
    if (used_ctx)
268
0
        BN_CTX_end(ctx);
269
0
    BN_CTX_free(new_ctx);
270
0
    return 0;
271
0
}
272
273
int ossl_ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
274
    const unsigned char *buf, size_t len,
275
    BN_CTX *ctx)
276
0
{
277
0
    point_conversion_form_t form;
278
0
    int y_bit;
279
0
    BN_CTX *new_ctx = NULL;
280
0
    BIGNUM *x, *y;
281
0
    int field_len, enc_len;
282
0
    int ret = 0;
283
284
0
    if (len == 0) {
285
0
        ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
286
0
        return 0;
287
0
    }
288
0
    form = buf[0];
289
0
    y_bit = form & 1;
290
0
    form = form & ~1U;
291
0
    if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
292
0
        && (form != POINT_CONVERSION_UNCOMPRESSED)
293
0
        && (form != POINT_CONVERSION_HYBRID)) {
294
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
295
0
        return 0;
296
0
    }
297
0
    if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
298
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
299
0
        return 0;
300
0
    }
301
302
0
    if (form == 0) {
303
0
        if (len != 1) {
304
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
305
0
            return 0;
306
0
        }
307
308
0
        return EC_POINT_set_to_infinity(group, point);
309
0
    }
310
311
0
    field_len = BN_num_bytes(group->field);
312
0
    enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
313
314
0
    if (len != (size_t)enc_len) {
315
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
316
0
        return 0;
317
0
    }
318
319
0
    if (ctx == NULL) {
320
0
        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
321
0
        if (ctx == NULL)
322
0
            return 0;
323
0
    }
324
325
0
    BN_CTX_start(ctx);
326
0
    x = BN_CTX_get(ctx);
327
0
    y = BN_CTX_get(ctx);
328
0
    if (y == NULL)
329
0
        goto err;
330
331
0
    if (!BN_bin2bn(buf + 1, field_len, x))
332
0
        goto err;
333
0
    if (BN_ucmp(x, group->field) >= 0) {
334
0
        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
335
0
        goto err;
336
0
    }
337
338
0
    if (form == POINT_CONVERSION_COMPRESSED) {
339
0
        if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
340
0
            goto err;
341
0
    } else {
342
0
        if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
343
0
            goto err;
344
0
        if (BN_ucmp(y, group->field) >= 0) {
345
0
            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
346
0
            goto err;
347
0
        }
348
0
        if (form == POINT_CONVERSION_HYBRID) {
349
0
            if (y_bit != BN_is_odd(y)) {
350
0
                ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
351
0
                goto err;
352
0
            }
353
0
        }
354
355
        /*
356
         * EC_POINT_set_affine_coordinates is responsible for checking that
357
         * the point is on the curve.
358
         */
359
0
        if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
360
0
            goto err;
361
0
    }
362
363
0
    ret = 1;
364
365
0
err:
366
0
    BN_CTX_end(ctx);
367
0
    BN_CTX_free(new_ctx);
368
0
    return ret;
369
0
}