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 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
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;
26 /* clear error queue */
31 ctx = new_ctx = BN_CTX_new_ex(group->libctx);
39 tmp1 = BN_CTX_get(ctx);
40 tmp2 = BN_CTX_get(ctx);
47 * Recover y. We have a Weierstrass equation
48 * y^2 = x^3 + a*x + b,
49 * so y is one of the square roots of x^3 + a*x + b.
53 if (!BN_nnmod(x, x_, group->field, ctx))
55 if (group->meth->field_decode == 0) {
56 /* field_{sqr,mul} work on standard representation */
57 if (!group->meth->field_sqr(group, tmp2, x_, ctx))
59 if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))
62 if (!BN_mod_sqr(tmp2, x_, group->field, ctx))
64 if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))
68 /* tmp1 := tmp1 + a*x */
69 if (group->a_is_minus3) {
70 if (!BN_mod_lshift1_quick(tmp2, x, group->field))
72 if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))
74 if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))
77 if (group->meth->field_decode) {
78 if (!group->meth->field_decode(group, tmp2, group->a, ctx))
80 if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))
83 /* field_mul works on standard representation */
84 if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))
88 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
92 /* tmp1 := tmp1 + b */
93 if (group->meth->field_decode) {
94 if (!group->meth->field_decode(group, tmp2, group->b, ctx))
96 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
99 if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))
103 if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {
105 unsigned long err = ERR_peek_last_error();
107 if (ERR_GET_LIB(err) == ERR_LIB_BN
108 && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
110 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
111 EC_R_INVALID_COMPRESSED_POINT);
115 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
121 if (y_bit != BN_is_odd(y)) {
125 kron = BN_kronecker(x, group->field, ctx);
130 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
131 EC_R_INVALID_COMPRESSION_BIT);
134 * BN_mod_sqrt() should have caught this error (not a square)
136 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
137 EC_R_INVALID_COMPRESSED_POINT);
140 if (!BN_usub(y, group->field, y))
143 if (y_bit != BN_is_odd(y)) {
144 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
145 ERR_R_INTERNAL_ERROR);
149 if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
156 BN_CTX_free(new_ctx);
160 size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
161 point_conversion_form_t form,
162 unsigned char *buf, size_t len, BN_CTX *ctx)
165 BN_CTX *new_ctx = NULL;
168 size_t field_len, i, skip;
170 if ((form != POINT_CONVERSION_COMPRESSED)
171 && (form != POINT_CONVERSION_UNCOMPRESSED)
172 && (form != POINT_CONVERSION_HYBRID)) {
173 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
177 if (EC_POINT_is_at_infinity(group, point)) {
178 /* encodes to a single 0 octet */
181 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
189 /* ret := required output buffer length */
190 field_len = BN_num_bytes(group->field);
193 POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
195 /* if 'buf' is NULL, just return required length */
198 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
203 ctx = new_ctx = BN_CTX_new_ex(group->libctx);
215 if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
218 if ((form == POINT_CONVERSION_COMPRESSED
219 || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
226 skip = field_len - BN_num_bytes(x);
227 if (skip > field_len) {
228 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
235 skip = BN_bn2bin(x, buf + i);
237 if (i != 1 + field_len) {
238 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
242 if (form == POINT_CONVERSION_UNCOMPRESSED
243 || form == POINT_CONVERSION_HYBRID) {
244 skip = field_len - BN_num_bytes(y);
245 if (skip > field_len) {
246 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
253 skip = BN_bn2bin(y, buf + i);
258 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
265 BN_CTX_free(new_ctx);
271 BN_CTX_free(new_ctx);
275 int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
276 const unsigned char *buf, size_t len, BN_CTX *ctx)
278 point_conversion_form_t form;
280 BN_CTX *new_ctx = NULL;
282 size_t field_len, enc_len;
286 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
292 if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
293 && (form != POINT_CONVERSION_UNCOMPRESSED)
294 && (form != POINT_CONVERSION_HYBRID)) {
295 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
298 if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
299 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
305 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
309 return EC_POINT_set_to_infinity(group, point);
312 field_len = BN_num_bytes(group->field);
315 POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
317 if (len != enc_len) {
318 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
323 ctx = new_ctx = BN_CTX_new_ex(group->libctx);
334 if (!BN_bin2bn(buf + 1, field_len, x))
336 if (BN_ucmp(x, group->field) >= 0) {
337 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
341 if (form == POINT_CONVERSION_COMPRESSED) {
342 if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
345 if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
347 if (BN_ucmp(y, group->field) >= 0) {
348 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
351 if (form == POINT_CONVERSION_HYBRID) {
352 if (y_bit != BN_is_odd(y)) {
353 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
359 * EC_POINT_set_affine_coordinates is responsible for checking that
360 * the point is on the curve.
362 if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
370 BN_CTX_free(new_ctx);