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>
18 #include <openssl/symhacks.h>
22 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
24 const BIGNUM *x_, int y_bit,
27 BN_CTX *new_ctx = NULL;
28 BIGNUM *tmp1, *tmp2, *x, *y;
32 /* clear error queue */
37 ctx = new_ctx = BN_CTX_new_ex(group->libctx);
45 tmp1 = BN_CTX_get(ctx);
46 tmp2 = BN_CTX_get(ctx);
53 * Recover y. We have a Weierstrass equation
54 * y^2 = x^3 + a*x + b,
55 * so y is one of the square roots of x^3 + a*x + b.
59 if (!BN_nnmod(x, x_, group->field, ctx))
61 if (group->meth->field_decode == 0) {
62 /* field_{sqr,mul} work on standard representation */
63 if (!group->meth->field_sqr(group, tmp2, x_, ctx))
65 if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))
68 if (!BN_mod_sqr(tmp2, x_, group->field, ctx))
70 if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))
74 /* tmp1 := tmp1 + a*x */
75 if (group->a_is_minus3) {
76 if (!BN_mod_lshift1_quick(tmp2, x, group->field))
78 if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))
80 if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))
83 if (group->meth->field_decode) {
84 if (!group->meth->field_decode(group, tmp2, group->a, ctx))
86 if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))
89 /* field_mul works on standard representation */
90 if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))
94 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
98 /* tmp1 := tmp1 + b */
99 if (group->meth->field_decode) {
100 if (!group->meth->field_decode(group, tmp2, group->b, ctx))
102 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
105 if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))
109 if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {
111 unsigned long err = ERR_peek_last_error();
113 if (ERR_GET_LIB(err) == ERR_LIB_BN
114 && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
116 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
117 EC_R_INVALID_COMPRESSED_POINT);
121 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
127 if (y_bit != BN_is_odd(y)) {
131 kron = BN_kronecker(x, group->field, ctx);
136 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
137 EC_R_INVALID_COMPRESSION_BIT);
140 * BN_mod_sqrt() should have caught this error (not a square)
142 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
143 EC_R_INVALID_COMPRESSED_POINT);
146 if (!BN_usub(y, group->field, y))
149 if (y_bit != BN_is_odd(y)) {
150 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
151 ERR_R_INTERNAL_ERROR);
155 if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
162 BN_CTX_free(new_ctx);
166 size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
167 point_conversion_form_t form,
168 unsigned char *buf, size_t len, BN_CTX *ctx)
171 BN_CTX *new_ctx = NULL;
174 size_t field_len, i, skip;
176 if ((form != POINT_CONVERSION_COMPRESSED)
177 && (form != POINT_CONVERSION_UNCOMPRESSED)
178 && (form != POINT_CONVERSION_HYBRID)) {
179 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
183 if (EC_POINT_is_at_infinity(group, point)) {
184 /* encodes to a single 0 octet */
187 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
195 /* ret := required output buffer length */
196 field_len = BN_num_bytes(group->field);
199 POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
201 /* if 'buf' is NULL, just return required length */
204 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
209 ctx = new_ctx = BN_CTX_new_ex(group->libctx);
221 if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx))
224 if ((form == POINT_CONVERSION_COMPRESSED
225 || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
232 skip = field_len - BN_num_bytes(x);
233 if (skip > field_len) {
234 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
241 skip = BN_bn2bin(x, buf + i);
243 if (i != 1 + field_len) {
244 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
248 if (form == POINT_CONVERSION_UNCOMPRESSED
249 || form == POINT_CONVERSION_HYBRID) {
250 skip = field_len - BN_num_bytes(y);
251 if (skip > field_len) {
252 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
259 skip = BN_bn2bin(y, buf + i);
264 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
271 BN_CTX_free(new_ctx);
277 BN_CTX_free(new_ctx);
281 int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
282 const unsigned char *buf, size_t len, BN_CTX *ctx)
284 point_conversion_form_t form;
286 BN_CTX *new_ctx = NULL;
288 size_t field_len, enc_len;
292 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
298 if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
299 && (form != POINT_CONVERSION_UNCOMPRESSED)
300 && (form != POINT_CONVERSION_HYBRID)) {
301 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
304 if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
305 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
311 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
315 return EC_POINT_set_to_infinity(group, point);
318 field_len = BN_num_bytes(group->field);
321 POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
323 if (len != enc_len) {
324 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
329 ctx = new_ctx = BN_CTX_new_ex(group->libctx);
340 if (!BN_bin2bn(buf + 1, field_len, x))
342 if (BN_ucmp(x, group->field) >= 0) {
343 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
347 if (form == POINT_CONVERSION_COMPRESSED) {
348 if (!EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx))
351 if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
353 if (BN_ucmp(y, group->field) >= 0) {
354 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
357 if (form == POINT_CONVERSION_HYBRID) {
358 if (y_bit != BN_is_odd(y)) {
359 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
365 * EC_POINT_set_affine_coordinates is responsible for checking that
366 * the point is on the curve.
368 if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))
376 BN_CTX_free(new_ctx);