2 * Copyright 2011-2016 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>
12 #include <openssl/symhacks.h>
16 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
18 const BIGNUM *x_, int y_bit,
21 BN_CTX *new_ctx = NULL;
22 BIGNUM *tmp1, *tmp2, *x, *y;
25 /* clear error queue */
29 ctx = new_ctx = BN_CTX_new();
37 tmp1 = BN_CTX_get(ctx);
38 tmp2 = BN_CTX_get(ctx);
45 * Recover y. We have a Weierstrass equation
46 * y^2 = x^3 + a*x + b,
47 * so y is one of the square roots of x^3 + a*x + b.
51 if (!BN_nnmod(x, x_, group->field, ctx))
53 if (group->meth->field_decode == 0) {
54 /* field_{sqr,mul} work on standard representation */
55 if (!group->meth->field_sqr(group, tmp2, x_, ctx))
57 if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))
60 if (!BN_mod_sqr(tmp2, x_, group->field, ctx))
62 if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))
66 /* tmp1 := tmp1 + a*x */
67 if (group->a_is_minus3) {
68 if (!BN_mod_lshift1_quick(tmp2, x, group->field))
70 if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))
72 if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))
75 if (group->meth->field_decode) {
76 if (!group->meth->field_decode(group, tmp2, group->a, ctx))
78 if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))
81 /* field_mul works on standard representation */
82 if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))
86 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
90 /* tmp1 := tmp1 + b */
91 if (group->meth->field_decode) {
92 if (!group->meth->field_decode(group, tmp2, group->b, ctx))
94 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
97 if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))
101 if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {
102 unsigned long err = ERR_peek_last_error();
104 if (ERR_GET_LIB(err) == ERR_LIB_BN
105 && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
107 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
108 EC_R_INVALID_COMPRESSED_POINT);
110 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
115 if (y_bit != BN_is_odd(y)) {
119 kron = BN_kronecker(x, group->field, ctx);
124 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
125 EC_R_INVALID_COMPRESSION_BIT);
128 * BN_mod_sqrt() should have cought this error (not a square)
130 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
131 EC_R_INVALID_COMPRESSED_POINT);
134 if (!BN_usub(y, group->field, y))
137 if (y_bit != BN_is_odd(y)) {
138 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
139 ERR_R_INTERNAL_ERROR);
143 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
150 BN_CTX_free(new_ctx);
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)
159 BN_CTX *new_ctx = NULL;
162 size_t field_len, i, skip;
164 if ((form != POINT_CONVERSION_COMPRESSED)
165 && (form != POINT_CONVERSION_UNCOMPRESSED)
166 && (form != POINT_CONVERSION_HYBRID)) {
167 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
171 if (EC_POINT_is_at_infinity(group, point)) {
172 /* encodes to a single 0 octet */
175 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
183 /* ret := required output buffer length */
184 field_len = BN_num_bytes(group->field);
187 POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
189 /* if 'buf' is NULL, just return required length */
192 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
197 ctx = new_ctx = BN_CTX_new();
209 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx))
212 if ((form == POINT_CONVERSION_COMPRESSED
213 || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
220 skip = field_len - BN_num_bytes(x);
221 if (skip > field_len) {
222 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
229 skip = BN_bn2bin(x, buf + i);
231 if (i != 1 + field_len) {
232 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
236 if (form == POINT_CONVERSION_UNCOMPRESSED
237 || form == POINT_CONVERSION_HYBRID) {
238 skip = field_len - BN_num_bytes(y);
239 if (skip > field_len) {
240 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
247 skip = BN_bn2bin(y, buf + i);
252 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
259 BN_CTX_free(new_ctx);
265 BN_CTX_free(new_ctx);
269 int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
270 const unsigned char *buf, size_t len, BN_CTX *ctx)
272 point_conversion_form_t form;
274 BN_CTX *new_ctx = NULL;
276 size_t field_len, enc_len;
280 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
286 if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
287 && (form != POINT_CONVERSION_UNCOMPRESSED)
288 && (form != POINT_CONVERSION_HYBRID)) {
289 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
292 if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
293 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
299 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
303 return EC_POINT_set_to_infinity(group, point);
306 field_len = BN_num_bytes(group->field);
309 POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
311 if (len != enc_len) {
312 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
317 ctx = new_ctx = BN_CTX_new();
328 if (!BN_bin2bn(buf + 1, field_len, x))
330 if (BN_ucmp(x, group->field) >= 0) {
331 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
335 if (form == POINT_CONVERSION_COMPRESSED) {
336 if (!EC_POINT_set_compressed_coordinates_GFp
337 (group, point, x, y_bit, ctx))
340 if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
342 if (BN_ucmp(y, group->field) >= 0) {
343 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
346 if (form == POINT_CONVERSION_HYBRID) {
347 if (y_bit != BN_is_odd(y)) {
348 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
354 * EC_POINT_set_affine_coordinates_GFp is responsible for checking that
355 * the point is on the curve.
357 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
365 BN_CTX_free(new_ctx);