2 * Copyright 2011-2021 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 ossl_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 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_COMPRESSED_POINT
);
120 ERR_raise(ERR_LIB_EC
, ERR_R_BN_LIB
);
125 if (y_bit
!= BN_is_odd(y
)) {
129 kron
= BN_kronecker(x
, group
->field
, ctx
);
134 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_COMPRESSION_BIT
);
137 * BN_mod_sqrt() should have caught this error (not a square)
139 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_COMPRESSED_POINT
);
142 if (!BN_usub(y
, group
->field
, y
))
145 if (y_bit
!= BN_is_odd(y
)) {
146 ERR_raise(ERR_LIB_EC
, ERR_R_INTERNAL_ERROR
);
150 if (!EC_POINT_set_affine_coordinates(group
, point
, x
, y
, ctx
))
157 BN_CTX_free(new_ctx
);
161 size_t ossl_ec_GFp_simple_point2oct(const EC_GROUP
*group
, const EC_POINT
*point
,
162 point_conversion_form_t form
,
163 unsigned char *buf
, size_t len
, BN_CTX
*ctx
)
166 BN_CTX
*new_ctx
= NULL
;
169 size_t field_len
, i
, skip
;
171 if ((form
!= POINT_CONVERSION_COMPRESSED
)
172 && (form
!= POINT_CONVERSION_UNCOMPRESSED
)
173 && (form
!= POINT_CONVERSION_HYBRID
)) {
174 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_FORM
);
178 if (EC_POINT_is_at_infinity(group
, point
)) {
179 /* encodes to a single 0 octet */
182 ERR_raise(ERR_LIB_EC
, EC_R_BUFFER_TOO_SMALL
);
190 /* ret := required output buffer length */
191 field_len
= BN_num_bytes(group
->field
);
194 POINT_CONVERSION_COMPRESSED
) ? 1 + field_len
: 1 + 2 * field_len
;
196 /* if 'buf' is NULL, just return required length */
199 ERR_raise(ERR_LIB_EC
, EC_R_BUFFER_TOO_SMALL
);
204 ctx
= new_ctx
= BN_CTX_new_ex(group
->libctx
);
216 if (!EC_POINT_get_affine_coordinates(group
, point
, x
, y
, ctx
))
219 if ((form
== POINT_CONVERSION_COMPRESSED
220 || form
== POINT_CONVERSION_HYBRID
) && BN_is_odd(y
))
227 skip
= field_len
- BN_num_bytes(x
);
228 if (skip
> field_len
) {
229 ERR_raise(ERR_LIB_EC
, ERR_R_INTERNAL_ERROR
);
236 skip
= BN_bn2bin(x
, buf
+ i
);
238 if (i
!= 1 + field_len
) {
239 ERR_raise(ERR_LIB_EC
, ERR_R_INTERNAL_ERROR
);
243 if (form
== POINT_CONVERSION_UNCOMPRESSED
244 || form
== POINT_CONVERSION_HYBRID
) {
245 skip
= field_len
- BN_num_bytes(y
);
246 if (skip
> field_len
) {
247 ERR_raise(ERR_LIB_EC
, ERR_R_INTERNAL_ERROR
);
254 skip
= BN_bn2bin(y
, buf
+ i
);
259 ERR_raise(ERR_LIB_EC
, ERR_R_INTERNAL_ERROR
);
266 BN_CTX_free(new_ctx
);
272 BN_CTX_free(new_ctx
);
276 int ossl_ec_GFp_simple_oct2point(const EC_GROUP
*group
, EC_POINT
*point
,
277 const unsigned char *buf
, size_t len
,
280 point_conversion_form_t form
;
282 BN_CTX
*new_ctx
= NULL
;
284 size_t field_len
, enc_len
;
288 ERR_raise(ERR_LIB_EC
, EC_R_BUFFER_TOO_SMALL
);
294 if ((form
!= 0) && (form
!= POINT_CONVERSION_COMPRESSED
)
295 && (form
!= POINT_CONVERSION_UNCOMPRESSED
)
296 && (form
!= POINT_CONVERSION_HYBRID
)) {
297 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
300 if ((form
== 0 || form
== POINT_CONVERSION_UNCOMPRESSED
) && y_bit
) {
301 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
307 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
311 return EC_POINT_set_to_infinity(group
, point
);
314 field_len
= BN_num_bytes(group
->field
);
317 POINT_CONVERSION_COMPRESSED
) ? 1 + field_len
: 1 + 2 * field_len
;
319 if (len
!= enc_len
) {
320 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
325 ctx
= new_ctx
= BN_CTX_new_ex(group
->libctx
);
336 if (!BN_bin2bn(buf
+ 1, field_len
, x
))
338 if (BN_ucmp(x
, group
->field
) >= 0) {
339 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
343 if (form
== POINT_CONVERSION_COMPRESSED
) {
344 if (!EC_POINT_set_compressed_coordinates(group
, point
, x
, y_bit
, ctx
))
347 if (!BN_bin2bn(buf
+ 1 + field_len
, field_len
, y
))
349 if (BN_ucmp(y
, group
->field
) >= 0) {
350 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
353 if (form
== POINT_CONVERSION_HYBRID
) {
354 if (y_bit
!= BN_is_odd(y
)) {
355 ERR_raise(ERR_LIB_EC
, EC_R_INVALID_ENCODING
);
361 * EC_POINT_set_affine_coordinates is responsible for checking that
362 * the point is on the curve.
364 if (!EC_POINT_set_affine_coordinates(group
, point
, x
, y
, ctx
))
372 BN_CTX_free(new_ctx
);