2 * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
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
11 #include <openssl/err.h>
15 #ifndef OPENSSL_NO_EC2M
18 * Calculates and sets the affine coordinates of an EC_POINT from the given
19 * compressed coordinates. Uses algorithm 2.3.4 of SEC 1.
20 * Note that the simple implementation only uses affine coordinates.
22 * The method is from the following publication:
24 * Harper, Menezes, Vanstone:
25 * "Public-Key Cryptosystems with Very Small Key Lengths",
26 * EUROCRYPT '92, Springer-Verlag LNCS 658,
27 * published February 1993
29 * US Patents 6,141,420 and 6,618,483 (Vanstone, Mullin, Agnew) describe
30 * the same method, but claim no priority date earlier than July 29, 1994
31 * (and additionally fail to cite the EUROCRYPT '92 publication as prior art).
33 int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group,
35 const BIGNUM *x_, int y_bit,
38 BN_CTX *new_ctx = NULL;
39 BIGNUM *tmp, *x, *y, *z;
42 /* clear error queue */
46 ctx = new_ctx = BN_CTX_new();
51 y_bit = (y_bit != 0) ? 1 : 0;
54 tmp = BN_CTX_get(ctx);
61 if (!BN_GF2m_mod_arr(x, x_, group->poly))
64 if (!BN_GF2m_mod_sqrt_arr(y, group->b, group->poly, ctx))
67 if (!group->meth->field_sqr(group, tmp, x, ctx))
69 if (!group->meth->field_div(group, tmp, group->b, tmp, ctx))
71 if (!BN_GF2m_add(tmp, group->a, tmp))
73 if (!BN_GF2m_add(tmp, x, tmp))
75 if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) {
76 unsigned long err = ERR_peek_last_error();
78 if (ERR_GET_LIB(err) == ERR_LIB_BN
79 && ERR_GET_REASON(err) == BN_R_NO_SOLUTION) {
81 ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
82 EC_R_INVALID_COMPRESSED_POINT);
84 ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
88 z0 = (BN_is_odd(z)) ? 1 : 0;
89 if (!group->meth->field_mul(group, y, x, z, ctx))
92 if (!BN_GF2m_add(y, y, x))
97 if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
104 BN_CTX_free(new_ctx);
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.
113 size_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)
118 BN_CTX *new_ctx = NULL;
121 size_t field_len, i, skip;
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);
130 if (EC_POINT_is_at_infinity(group, point)) {
131 /* encodes to a single 0 octet */
134 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
142 /* ret := required output buffer length */
143 field_len = (EC_GROUP_get_degree(group) + 7) / 8;
146 POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
148 /* if 'buf' is NULL, just return required length */
151 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
156 ctx = new_ctx = BN_CTX_new();
165 yxi = BN_CTX_get(ctx);
169 if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
173 if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) {
174 if (!group->meth->field_div(group, yxi, y, x, ctx))
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);
191 skip = BN_bn2bin(x, buf + i);
193 if (i != 1 + field_len) {
194 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
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);
209 skip = BN_bn2bin(y, buf + i);
214 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
221 BN_CTX_free(new_ctx);
227 BN_CTX_free(new_ctx);
232 * Converts an octet string representation to an EC_POINT. Note that the
233 * simple implementation only uses affine coordinates.
235 int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
236 const unsigned char *buf, size_t len,
239 point_conversion_form_t form;
241 BN_CTX *new_ctx = NULL;
243 size_t field_len, enc_len;
247 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
253 if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
254 && (form != POINT_CONVERSION_UNCOMPRESSED)
255 && (form != POINT_CONVERSION_HYBRID)) {
256 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
259 if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
260 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
266 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
270 return EC_POINT_set_to_infinity(group, point);
273 field_len = (EC_GROUP_get_degree(group) + 7) / 8;
276 POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
278 if (len != enc_len) {
279 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
284 ctx = new_ctx = BN_CTX_new();
292 yxi = BN_CTX_get(ctx);
296 if (!BN_bin2bn(buf + 1, field_len, x))
298 if (BN_ucmp(x, group->field) >= 0) {
299 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
303 if (form == POINT_CONVERSION_COMPRESSED) {
304 if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
307 if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
309 if (BN_ucmp(y, group->field) >= 0) {
310 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
313 if (form == POINT_CONVERSION_HYBRID) {
314 if (!group->meth->field_div(group, yxi, y, x, ctx))
316 if (y_bit != BN_is_odd(yxi)) {
317 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
323 * EC_POINT_set_affine_coordinates is responsible for checking that
324 * the point is on the curve.
326 if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
334 BN_CTX_free(new_ctx);