2 * Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
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
12 * ECDSA low level APIs are deprecated for public use, but still ok for
15 #include "internal/deprecated.h"
17 #include <openssl/err.h>
21 #ifndef OPENSSL_NO_EC2M
24 * Calculates and sets the affine coordinates of an EC_POINT from the given
25 * compressed coordinates. Uses algorithm 2.3.4 of SEC 1.
26 * Note that the simple implementation only uses affine coordinates.
28 * The method is from the following publication:
30 * Harper, Menezes, Vanstone:
31 * "Public-Key Cryptosystems with Very Small Key Lengths",
32 * EUROCRYPT '92, Springer-Verlag LNCS 658,
33 * published February 1993
35 * US Patents 6,141,420 and 6,618,483 (Vanstone, Mullin, Agnew) describe
36 * the same method, but claim no priority date earlier than July 29, 1994
37 * (and additionally fail to cite the EUROCRYPT '92 publication as prior art).
39 int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group,
41 const BIGNUM *x_, int y_bit,
44 BIGNUM *tmp, *x, *y, *z;
47 BN_CTX *new_ctx = NULL;
49 /* clear error queue */
53 ctx = new_ctx = BN_CTX_new();
59 y_bit = (y_bit != 0) ? 1 : 0;
62 tmp = BN_CTX_get(ctx);
69 if (!BN_GF2m_mod_arr(x, x_, group->poly))
72 if (!BN_GF2m_mod_sqrt_arr(y, group->b, group->poly, ctx))
75 if (!group->meth->field_sqr(group, tmp, x, ctx))
77 if (!group->meth->field_div(group, tmp, group->b, tmp, ctx))
79 if (!BN_GF2m_add(tmp, group->a, tmp))
81 if (!BN_GF2m_add(tmp, x, tmp))
83 if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) {
85 unsigned long err = ERR_peek_last_error();
87 if (ERR_GET_LIB(err) == ERR_LIB_BN
88 && ERR_GET_REASON(err) == BN_R_NO_SOLUTION) {
90 ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
91 EC_R_INVALID_COMPRESSED_POINT);
95 ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
100 z0 = (BN_is_odd(z)) ? 1 : 0;
101 if (!group->meth->field_mul(group, y, x, z, ctx))
104 if (!BN_GF2m_add(y, y, x))
109 if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
117 BN_CTX_free(new_ctx);
123 * Converts an EC_POINT to an octet string. If buf is NULL, the encoded
124 * length will be returned. If the length len of buf is smaller than required
125 * an error will be returned.
127 size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
128 point_conversion_form_t form,
129 unsigned char *buf, size_t len, BN_CTX *ctx)
134 size_t field_len, i, skip;
136 BN_CTX *new_ctx = NULL;
139 if ((form != POINT_CONVERSION_COMPRESSED)
140 && (form != POINT_CONVERSION_UNCOMPRESSED)
141 && (form != POINT_CONVERSION_HYBRID)) {
142 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
146 if (EC_POINT_is_at_infinity(group, point)) {
147 /* encodes to a single 0 octet */
150 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
158 /* ret := required output buffer length */
159 field_len = (EC_GROUP_get_degree(group) + 7) / 8;
162 POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
164 /* if 'buf' is NULL, just return required length */
167 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
173 ctx = new_ctx = BN_CTX_new();
183 yxi = BN_CTX_get(ctx);
187 if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
191 if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) {
192 if (!group->meth->field_div(group, yxi, y, x, ctx))
200 skip = field_len - BN_num_bytes(x);
201 if (skip > field_len) {
202 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
209 skip = BN_bn2bin(x, buf + i);
211 if (i != 1 + field_len) {
212 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
216 if (form == POINT_CONVERSION_UNCOMPRESSED
217 || form == POINT_CONVERSION_HYBRID) {
218 skip = field_len - BN_num_bytes(y);
219 if (skip > field_len) {
220 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
227 skip = BN_bn2bin(y, buf + i);
232 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
240 BN_CTX_free(new_ctx);
248 BN_CTX_free(new_ctx);
254 * Converts an octet string representation to an EC_POINT. Note that the
255 * simple implementation only uses affine coordinates.
257 int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
258 const unsigned char *buf, size_t len,
261 point_conversion_form_t form;
264 size_t field_len, enc_len;
267 BN_CTX *new_ctx = NULL;
271 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
277 if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
278 && (form != POINT_CONVERSION_UNCOMPRESSED)
279 && (form != POINT_CONVERSION_HYBRID)) {
280 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
283 if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
284 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
290 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
294 return EC_POINT_set_to_infinity(group, point);
297 m = EC_GROUP_get_degree(group);
298 field_len = (m + 7) / 8;
301 POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
303 if (len != enc_len) {
304 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
310 ctx = new_ctx = BN_CTX_new();
319 yxi = BN_CTX_get(ctx);
323 if (!BN_bin2bn(buf + 1, field_len, x))
325 if (BN_num_bits(x) > m) {
326 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
330 if (form == POINT_CONVERSION_COMPRESSED) {
331 if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
334 if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
336 if (BN_num_bits(y) > m) {
337 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
340 if (form == POINT_CONVERSION_HYBRID) {
341 if (!group->meth->field_div(group, yxi, y, x, ctx))
343 if (y_bit != BN_is_odd(yxi)) {
344 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
350 * EC_POINT_set_affine_coordinates is responsible for checking that
351 * the point is on the curve.
353 if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
362 BN_CTX_free(new_ctx);