Coverage Report

Created: 2018-08-29 13:53

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