1174199Srwatson/*
2224859Srwatson * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
3174199Srwatson * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4174199Srwatson *
5174199Srwatson * Licensed under the OpenSSL license (the "License").  You may not use
6174199Srwatson * this file except in compliance with the License.  You can obtain a copy
7174199Srwatson * in the file LICENSE in the source distribution or at
8174199Srwatson * https://www.openssl.org/source/license.html
9174199Srwatson */
10174199Srwatson
11174199Srwatson#include <openssl/err.h>
12174199Srwatson
13174199Srwatson#include "ec_local.h"
14174199Srwatson
15174199Srwatson#ifndef OPENSSL_NO_EC2M
16174199Srwatson
17174199Srwatson/*-
18174199Srwatson * Calculates and sets the affine coordinates of an EC_POINT from the given
19174199Srwatson * compressed coordinates.  Uses algorithm 2.3.4 of SEC 1.
20174199Srwatson * Note that the simple implementation only uses affine coordinates.
21174199Srwatson *
22174199Srwatson * The method is from the following publication:
23174199Srwatson *
24174199Srwatson *     Harper, Menezes, Vanstone:
25174199Srwatson *     "Public-Key Cryptosystems with Very Small Key Lengths",
26174199Srwatson *     EUROCRYPT '92, Springer-Verlag LNCS 658,
27174199Srwatson *     published February 1993
28174199Srwatson *
29186567Srwatson * US Patents 6,141,420 and 6,618,483 (Vanstone, Mullin, Agnew) describe
30174199Srwatson * the same method, but claim no priority date earlier than July 29, 1994
31174199Srwatson * (and additionally fail to cite the EUROCRYPT '92 publication as prior art).
32174199Srwatson */
33174199Srwatsonint ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group,
34221807Sstas                                              EC_POINT *point,
35195853Sbrooks                                              const BIGNUM *x_, int y_bit,
36174199Srwatson                                              BN_CTX *ctx)
37195853Sbrooks{
38174199Srwatson    BN_CTX *new_ctx = NULL;
39174199Srwatson    BIGNUM *tmp, *x, *y, *z;
40174199Srwatson    int ret = 0, z0;
41249673Strociny
42249673Strociny    /* clear error queue */
43232182Strociny    ERR_clear_error();
44174199Srwatson
45249671Strociny    if (ctx == NULL) {
46174199Srwatson        ctx = new_ctx = BN_CTX_new();
47249671Strociny        if (ctx == NULL)
48249671Strociny            return 0;
49174199Srwatson    }
50174199Srwatson
51232182Strociny    y_bit = (y_bit != 0) ? 1 : 0;
52232182Strociny
53232182Strociny    BN_CTX_start(ctx);
54174199Srwatson    tmp = BN_CTX_get(ctx);
55221807Sstas    x = BN_CTX_get(ctx);
56174530Srwatson    y = BN_CTX_get(ctx);
57174199Srwatson    z = BN_CTX_get(ctx);
58174199Srwatson    if (z == NULL)
59174199Srwatson        goto err;
60174199Srwatson
61174199Srwatson    if (!BN_GF2m_mod_arr(x, x_, group->poly))
62174199Srwatson        goto err;
63249673Strociny    if (BN_is_zero(x)) {
64224859Srwatson        if (!BN_GF2m_mod_sqrt_arr(y, group->b, group->poly, ctx))
65224859Srwatson            goto err;
66195853Sbrooks    } else {
67249671Strociny        if (!group->meth->field_sqr(group, tmp, x, ctx))
68195853Sbrooks            goto err;
69195853Sbrooks        if (!group->meth->field_div(group, tmp, group->b, tmp, ctx))
70249671Strociny            goto err;
71249671Strociny        if (!BN_GF2m_add(tmp, group->a, tmp))
72195853Sbrooks            goto err;
73249671Strociny        if (!BN_GF2m_add(tmp, x, tmp))
74249671Strociny            goto err;
75195853Sbrooks        if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) {
76195853Sbrooks            unsigned long err = ERR_peek_last_error();
77195853Sbrooks
78195853Sbrooks            if (ERR_GET_LIB(err) == ERR_LIB_BN
79195853Sbrooks                && ERR_GET_REASON(err) == BN_R_NO_SOLUTION) {
80195853Sbrooks                ERR_clear_error();
81195853Sbrooks                ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
82249671Strociny                      EC_R_INVALID_COMPRESSED_POINT);
83195853Sbrooks            } else
84174199Srwatson                ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
85174199Srwatson                      ERR_R_BN_LIB);
86232182Strociny            goto err;
87232182Strociny        }
88249673Strociny        z0 = (BN_is_odd(z)) ? 1 : 0;
89232182Strociny        if (!group->meth->field_mul(group, y, x, z, ctx))
90232182Strociny            goto err;
91232182Strociny        if (z0 != y_bit) {
92232182Strociny            if (!BN_GF2m_add(y, y, x))
93249673Strociny                goto err;
94232182Strociny        }
95232182Strociny    }
96232182Strociny
97232182Strociny    if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
98232182Strociny        goto err;
99232182Strociny
100    ret = 1;
101
102 err:
103    BN_CTX_end(ctx);
104    BN_CTX_free(new_ctx);
105    return ret;
106}
107
108/*
109 * Converts an EC_POINT to an octet string. If buf is NULL, the encoded
110 * length will be returned. If the length len of buf is smaller than required
111 * an error will be returned.
112 */
113size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
114                                point_conversion_form_t form,
115                                unsigned char *buf, size_t len, BN_CTX *ctx)
116{
117    size_t ret;
118    BN_CTX *new_ctx = NULL;
119    int used_ctx = 0;
120    BIGNUM *x, *y, *yxi;
121    size_t field_len, i, skip;
122
123    if ((form != POINT_CONVERSION_COMPRESSED)
124        && (form != POINT_CONVERSION_UNCOMPRESSED)
125        && (form != POINT_CONVERSION_HYBRID)) {
126        ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
127        goto err;
128    }
129
130    if (EC_POINT_is_at_infinity(group, point)) {
131        /* encodes to a single 0 octet */
132        if (buf != NULL) {
133            if (len < 1) {
134                ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
135                return 0;
136            }
137            buf[0] = 0;
138        }
139        return 1;
140    }
141
142    /* ret := required output buffer length */
143    field_len = (EC_GROUP_get_degree(group) + 7) / 8;
144    ret =
145        (form ==
146         POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
147
148    /* if 'buf' is NULL, just return required length */
149    if (buf != NULL) {
150        if (len < ret) {
151            ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
152            goto err;
153        }
154
155        if (ctx == NULL) {
156            ctx = new_ctx = BN_CTX_new();
157            if (ctx == NULL)
158                return 0;
159        }
160
161        BN_CTX_start(ctx);
162        used_ctx = 1;
163        x = BN_CTX_get(ctx);
164        y = BN_CTX_get(ctx);
165        yxi = BN_CTX_get(ctx);
166        if (yxi == NULL)
167            goto err;
168
169        if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
170            goto err;
171
172        buf[0] = form;
173        if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) {
174            if (!group->meth->field_div(group, yxi, y, x, ctx))
175                goto err;
176            if (BN_is_odd(yxi))
177                buf[0]++;
178        }
179
180        i = 1;
181
182        skip = field_len - BN_num_bytes(x);
183        if (skip > field_len) {
184            ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
185            goto err;
186        }
187        while (skip > 0) {
188            buf[i++] = 0;
189            skip--;
190        }
191        skip = BN_bn2bin(x, buf + i);
192        i += skip;
193        if (i != 1 + field_len) {
194            ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
195            goto err;
196        }
197
198        if (form == POINT_CONVERSION_UNCOMPRESSED
199            || form == POINT_CONVERSION_HYBRID) {
200            skip = field_len - BN_num_bytes(y);
201            if (skip > field_len) {
202                ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
203                goto err;
204            }
205            while (skip > 0) {
206                buf[i++] = 0;
207                skip--;
208            }
209            skip = BN_bn2bin(y, buf + i);
210            i += skip;
211        }
212
213        if (i != ret) {
214            ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
215            goto err;
216        }
217    }
218
219    if (used_ctx)
220        BN_CTX_end(ctx);
221    BN_CTX_free(new_ctx);
222    return ret;
223
224 err:
225    if (used_ctx)
226        BN_CTX_end(ctx);
227    BN_CTX_free(new_ctx);
228    return 0;
229}
230
231/*
232 * Converts an octet string representation to an EC_POINT. Note that the
233 * simple implementation only uses affine coordinates.
234 */
235int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
236                             const unsigned char *buf, size_t len,
237                             BN_CTX *ctx)
238{
239    point_conversion_form_t form;
240    int y_bit, m;
241    BN_CTX *new_ctx = NULL;
242    BIGNUM *x, *y, *yxi;
243    size_t field_len, enc_len;
244    int ret = 0;
245
246    if (len == 0) {
247        ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
248        return 0;
249    }
250
251    /*
252     * The first octet is the point converison octet PC, see X9.62, page 4
253     * and section 4.4.2.  It must be:
254     *     0x00          for the point at infinity
255     *     0x02 or 0x03  for compressed form
256     *     0x04          for uncompressed form
257     *     0x06 or 0x07  for hybrid form.
258     * For compressed or hybrid forms, we store the last bit of buf[0] as
259     * y_bit and clear it from buf[0] so as to obtain a POINT_CONVERSION_*.
260     * We error if buf[0] contains any but the above values.
261     */
262    y_bit = buf[0] & 1;
263    form = buf[0] & ~1U;
264
265    if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
266        && (form != POINT_CONVERSION_UNCOMPRESSED)
267        && (form != POINT_CONVERSION_HYBRID)) {
268        ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
269        return 0;
270    }
271    if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
272        ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
273        return 0;
274    }
275
276    /* The point at infinity is represented by a single zero octet. */
277    if (form == 0) {
278        if (len != 1) {
279            ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
280            return 0;
281        }
282
283        return EC_POINT_set_to_infinity(group, point);
284    }
285
286    m = EC_GROUP_get_degree(group);
287    field_len = (m + 7) / 8;
288    enc_len =
289        (form ==
290         POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
291
292    if (len != enc_len) {
293        ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
294        return 0;
295    }
296
297    if (ctx == NULL) {
298        ctx = new_ctx = BN_CTX_new();
299        if (ctx == NULL)
300            return 0;
301    }
302
303    BN_CTX_start(ctx);
304    x = BN_CTX_get(ctx);
305    y = BN_CTX_get(ctx);
306    yxi = BN_CTX_get(ctx);
307    if (yxi == NULL)
308        goto err;
309
310    if (!BN_bin2bn(buf + 1, field_len, x))
311        goto err;
312    if (BN_num_bits(x) > m) {
313        ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
314        goto err;
315    }
316
317    if (form == POINT_CONVERSION_COMPRESSED) {
318        if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
319            goto err;
320    } else {
321        if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
322            goto err;
323        if (BN_num_bits(y) > m) {
324            ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
325            goto err;
326        }
327        if (form == POINT_CONVERSION_HYBRID) {
328            /*
329             * Check that the form in the encoding was set correctly
330             * according to X9.62 4.4.2.a, 4(c), see also first paragraph
331             * of X9.62, 4.4.1.b.
332             */
333            if (BN_is_zero(x)) {
334                if (y_bit != 0) {
335                    ECerr(ERR_LIB_EC, EC_R_INVALID_ENCODING);
336                    goto err;
337                }
338            } else {
339                if (!group->meth->field_div(group, yxi, y, x, ctx))
340                    goto err;
341                if (y_bit != BN_is_odd(yxi)) {
342                    ECerr(ERR_LIB_EC, EC_R_INVALID_ENCODING);
343                    goto err;
344                }
345            }
346        }
347
348        /*
349         * EC_POINT_set_affine_coordinates is responsible for checking that
350         * the point is on the curve.
351         */
352        if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
353            goto err;
354    }
355
356    ret = 1;
357
358 err:
359    BN_CTX_end(ctx);
360    BN_CTX_free(new_ctx);
361    return ret;
362}
363#endif
364