1/* Originally written by Bodo Moeller for the OpenSSL project.
2 * ====================================================================
3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55/* ====================================================================
56 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57 *
58 * Portions of the attached software ("Contribution") are developed by
59 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60 *
61 * The Contribution is licensed pursuant to the OpenSSL open source
62 * license provided above.
63 *
64 * The elliptic curve binary polynomial software is originally written by
65 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
66 * Laboratories. */
67
68#include <openssl/ec.h>
69
70#include <openssl/bn.h>
71#include <openssl/err.h>
72
73#include "internal.h"
74
75
76static size_t ec_GFp_simple_point2oct(const EC_GROUP *group,
77 const EC_RAW_POINT *point,
78 point_conversion_form_t form,
79 uint8_t *buf, size_t len) {
80 if (form != POINT_CONVERSION_COMPRESSED &&
81 form != POINT_CONVERSION_UNCOMPRESSED) {
82 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM);
83 return 0;
84 }
85
86 if (ec_GFp_simple_is_at_infinity(group, point)) {
87 OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
88 return 0;
89 }
90
91 const size_t field_len = BN_num_bytes(&group->field);
92 size_t output_len = 1 /* type byte */ + field_len;
93 if (form == POINT_CONVERSION_UNCOMPRESSED) {
94 // Uncompressed points have a second coordinate.
95 output_len += field_len;
96 }
97
98 // if 'buf' is NULL, just return required length
99 if (buf != NULL) {
100 if (len < output_len) {
101 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
102 return 0;
103 }
104
105 uint8_t y_buf[EC_MAX_BYTES];
106 size_t field_len_out;
107 if (!ec_point_get_affine_coordinate_bytes(
108 group, buf + 1 /* x */,
109 form == POINT_CONVERSION_COMPRESSED ? y_buf : buf + 1 + field_len,
110 &field_len_out, field_len, point)) {
111 return 0;
112 }
113
114 if (field_len_out != field_len) {
115 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
116 return 0;
117 }
118
119 if (form == POINT_CONVERSION_COMPRESSED) {
120 buf[0] = form + (y_buf[field_len - 1] & 1);
121 } else {
122 buf[0] = form;
123 }
124 }
125
126 return output_len;
127}
128
129static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
130 const uint8_t *buf, size_t len,
131 BN_CTX *ctx) {
132 BN_CTX *new_ctx = NULL;
133 int ret = 0, used_ctx = 0;
134
135 if (len == 0) {
136 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
137 goto err;
138 }
139
140 point_conversion_form_t form = buf[0];
141 const int y_bit = form & 1;
142 form = form & ~1U;
143 if ((form != POINT_CONVERSION_COMPRESSED &&
144 form != POINT_CONVERSION_UNCOMPRESSED) ||
145 (form == POINT_CONVERSION_UNCOMPRESSED && y_bit)) {
146 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
147 goto err;
148 }
149
150 const size_t field_len = BN_num_bytes(&group->field);
151 size_t enc_len = 1 /* type byte */ + field_len;
152 if (form == POINT_CONVERSION_UNCOMPRESSED) {
153 // Uncompressed points have a second coordinate.
154 enc_len += field_len;
155 }
156
157 if (len != enc_len) {
158 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
159 goto err;
160 }
161
162 if (ctx == NULL) {
163 ctx = new_ctx = BN_CTX_new();
164 if (ctx == NULL) {
165 goto err;
166 }
167 }
168
169 BN_CTX_start(ctx);
170 used_ctx = 1;
171 BIGNUM *x = BN_CTX_get(ctx);
172 BIGNUM *y = BN_CTX_get(ctx);
173 if (x == NULL || y == NULL) {
174 goto err;
175 }
176
177 if (!BN_bin2bn(buf + 1, field_len, x)) {
178 goto err;
179 }
180 if (BN_ucmp(x, &group->field) >= 0) {
181 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
182 goto err;
183 }
184
185 if (form == POINT_CONVERSION_COMPRESSED) {
186 if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) {
187 goto err;
188 }
189 } else {
190 if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) {
191 goto err;
192 }
193 if (BN_ucmp(y, &group->field) >= 0) {
194 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
195 goto err;
196 }
197
198 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
199 goto err;
200 }
201 }
202
203 ret = 1;
204
205err:
206 if (used_ctx) {
207 BN_CTX_end(ctx);
208 }
209 BN_CTX_free(new_ctx);
210 return ret;
211}
212
213int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
214 const uint8_t *buf, size_t len, BN_CTX *ctx) {
215 if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
216 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
217 return 0;
218 }
219 return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
220}
221
222size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
223 point_conversion_form_t form, uint8_t *buf,
224 size_t len, BN_CTX *ctx) {
225 if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
226 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
227 return 0;
228 }
229 return ec_GFp_simple_point2oct(group, &point->raw, form, buf, len);
230}
231
232int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
233 EC_POINT *point, const BIGNUM *x,
234 int y_bit, BN_CTX *ctx) {
235 if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
236 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
237 return 0;
238 }
239
240 if (BN_is_negative(x) || BN_cmp(x, &group->field) >= 0) {
241 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
242 return 0;
243 }
244
245 BN_CTX *new_ctx = NULL;
246 int ret = 0;
247
248 ERR_clear_error();
249
250 if (ctx == NULL) {
251 ctx = new_ctx = BN_CTX_new();
252 if (ctx == NULL) {
253 return 0;
254 }
255 }
256
257 y_bit = (y_bit != 0);
258
259 BN_CTX_start(ctx);
260 BIGNUM *tmp1 = BN_CTX_get(ctx);
261 BIGNUM *tmp2 = BN_CTX_get(ctx);
262 BIGNUM *a = BN_CTX_get(ctx);
263 BIGNUM *b = BN_CTX_get(ctx);
264 BIGNUM *y = BN_CTX_get(ctx);
265 if (y == NULL ||
266 !EC_GROUP_get_curve_GFp(group, NULL, a, b, ctx)) {
267 goto err;
268 }
269
270 // Recover y. We have a Weierstrass equation
271 // y^2 = x^3 + a*x + b,
272 // so y is one of the square roots of x^3 + a*x + b.
273
274 // tmp1 := x^3
275 if (!BN_mod_sqr(tmp2, x, &group->field, ctx) ||
276 !BN_mod_mul(tmp1, tmp2, x, &group->field, ctx)) {
277 goto err;
278 }
279
280 // tmp1 := tmp1 + a*x
281 if (group->a_is_minus3) {
282 if (!bn_mod_lshift1_consttime(tmp2, x, &group->field, ctx) ||
283 !bn_mod_add_consttime(tmp2, tmp2, x, &group->field, ctx) ||
284 !bn_mod_sub_consttime(tmp1, tmp1, tmp2, &group->field, ctx)) {
285 goto err;
286 }
287 } else {
288 if (!BN_mod_mul(tmp2, a, x, &group->field, ctx) ||
289 !bn_mod_add_consttime(tmp1, tmp1, tmp2, &group->field, ctx)) {
290 goto err;
291 }
292 }
293
294 // tmp1 := tmp1 + b
295 if (!bn_mod_add_consttime(tmp1, tmp1, b, &group->field, ctx)) {
296 goto err;
297 }
298
299 if (!BN_mod_sqrt(y, tmp1, &group->field, ctx)) {
300 unsigned long err = ERR_peek_last_error();
301
302 if (ERR_GET_LIB(err) == ERR_LIB_BN &&
303 ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
304 ERR_clear_error();
305 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
306 } else {
307 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
308 }
309 goto err;
310 }
311
312 if (y_bit != BN_is_odd(y)) {
313 if (BN_is_zero(y)) {
314 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT);
315 goto err;
316 }
317 if (!BN_usub(y, &group->field, y)) {
318 goto err;
319 }
320 }
321 if (y_bit != BN_is_odd(y)) {
322 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
323 goto err;
324 }
325
326 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
327 goto err;
328 }
329
330 ret = 1;
331
332err:
333 BN_CTX_end(ctx);
334 BN_CTX_free(new_ctx);
335 return ret;
336}
337