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
ec_point_byte_len(const EC_GROUP * group,point_conversion_form_t form)76 size_t ec_point_byte_len(const EC_GROUP *group, point_conversion_form_t form) {
77 if (form != POINT_CONVERSION_COMPRESSED &&
78 form != POINT_CONVERSION_UNCOMPRESSED) {
79 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM);
80 return 0;
81 }
82
83 const size_t field_len = BN_num_bytes(&group->field);
84 size_t output_len = 1 /* type byte */ + field_len;
85 if (form == POINT_CONVERSION_UNCOMPRESSED) {
86 // Uncompressed points have a second coordinate.
87 output_len += field_len;
88 }
89 return output_len;
90 }
91
ec_point_to_bytes(const EC_GROUP * group,const EC_AFFINE * point,point_conversion_form_t form,uint8_t * buf,size_t max_out)92 size_t ec_point_to_bytes(const EC_GROUP *group, const EC_AFFINE *point,
93 point_conversion_form_t form, uint8_t *buf,
94 size_t max_out) {
95 size_t output_len = ec_point_byte_len(group, form);
96 if (max_out < output_len) {
97 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
98 return 0;
99 }
100
101 size_t field_len;
102 ec_felem_to_bytes(group, buf + 1, &field_len, &point->X);
103 assert(field_len == BN_num_bytes(&group->field));
104
105 if (form == POINT_CONVERSION_UNCOMPRESSED) {
106 ec_felem_to_bytes(group, buf + 1 + field_len, &field_len, &point->Y);
107 assert(field_len == BN_num_bytes(&group->field));
108 buf[0] = form;
109 } else {
110 uint8_t y_buf[EC_MAX_BYTES];
111 ec_felem_to_bytes(group, y_buf, &field_len, &point->Y);
112 buf[0] = form + (y_buf[field_len - 1] & 1);
113 }
114
115 return output_len;
116 }
117
ec_point_from_uncompressed(const EC_GROUP * group,EC_AFFINE * out,const uint8_t * in,size_t len)118 int ec_point_from_uncompressed(const EC_GROUP *group, EC_AFFINE *out,
119 const uint8_t *in, size_t len) {
120 const size_t field_len = BN_num_bytes(&group->field);
121 if (len != 1 + 2 * field_len || in[0] != POINT_CONVERSION_UNCOMPRESSED) {
122 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
123 return 0;
124 }
125
126 EC_FELEM x, y;
127 if (!ec_felem_from_bytes(group, &x, in + 1, field_len) ||
128 !ec_felem_from_bytes(group, &y, in + 1 + field_len, field_len) ||
129 !ec_point_set_affine_coordinates(group, out, &x, &y)) {
130 return 0;
131 }
132
133 return 1;
134 }
135
ec_GFp_simple_oct2point(const EC_GROUP * group,EC_POINT * point,const uint8_t * buf,size_t len,BN_CTX * ctx)136 static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
137 const uint8_t *buf, size_t len,
138 BN_CTX *ctx) {
139 if (len == 0) {
140 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
141 return 0;
142 }
143
144 point_conversion_form_t form = buf[0];
145 if (form == POINT_CONVERSION_UNCOMPRESSED) {
146 EC_AFFINE affine;
147 if (!ec_point_from_uncompressed(group, &affine, buf, len)) {
148 // In the event of an error, defend against the caller not checking the
149 // return value by setting a known safe value.
150 ec_set_to_safe_point(group, &point->raw);
151 return 0;
152 }
153 ec_affine_to_jacobian(group, &point->raw, &affine);
154 return 1;
155 }
156
157 const int y_bit = form & 1;
158 const size_t field_len = BN_num_bytes(&group->field);
159 form = form & ~1u;
160 if (form != POINT_CONVERSION_COMPRESSED ||
161 len != 1 /* type byte */ + field_len) {
162 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
163 return 0;
164 }
165
166 // TODO(davidben): Integrate compressed coordinates with the lower-level EC
167 // abstractions. This requires a way to compute square roots, which is tricky
168 // for primes which are not 3 (mod 4), namely P-224 and custom curves. P-224's
169 // prime is particularly inconvenient for compressed coordinates. See
170 // https://cr.yp.to/papers/sqroot.pdf
171 BN_CTX *new_ctx = NULL;
172 if (ctx == NULL) {
173 ctx = new_ctx = BN_CTX_new();
174 if (ctx == NULL) {
175 return 0;
176 }
177 }
178
179 int ret = 0;
180 BN_CTX_start(ctx);
181 BIGNUM *x = BN_CTX_get(ctx);
182 if (x == NULL || !BN_bin2bn(buf + 1, field_len, x)) {
183 goto err;
184 }
185 if (BN_ucmp(x, &group->field) >= 0) {
186 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
187 goto err;
188 }
189
190 if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) {
191 goto err;
192 }
193
194 ret = 1;
195
196 err:
197 BN_CTX_end(ctx);
198 BN_CTX_free(new_ctx);
199 return ret;
200 }
201
EC_POINT_oct2point(const EC_GROUP * group,EC_POINT * point,const uint8_t * buf,size_t len,BN_CTX * ctx)202 int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
203 const uint8_t *buf, size_t len, BN_CTX *ctx) {
204 if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
205 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
206 return 0;
207 }
208 return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
209 }
210
EC_POINT_point2oct(const EC_GROUP * group,const EC_POINT * point,point_conversion_form_t form,uint8_t * buf,size_t max_out,BN_CTX * ctx)211 size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
212 point_conversion_form_t form, uint8_t *buf,
213 size_t max_out, BN_CTX *ctx) {
214 if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
215 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
216 return 0;
217 }
218 if (buf == NULL) {
219 // When |buf| is NULL, just return the number of bytes that would be
220 // written, without doing an expensive Jacobian-to-affine conversion.
221 if (ec_GFp_simple_is_at_infinity(group, &point->raw)) {
222 OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
223 return 0;
224 }
225 return ec_point_byte_len(group, form);
226 }
227 EC_AFFINE affine;
228 if (!ec_jacobian_to_affine(group, &affine, &point->raw)) {
229 return 0;
230 }
231 return ec_point_to_bytes(group, &affine, form, buf, max_out);
232 }
233
EC_POINT_point2buf(const EC_GROUP * group,const EC_POINT * point,point_conversion_form_t form,uint8_t ** out_buf,BN_CTX * ctx)234 size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
235 point_conversion_form_t form, uint8_t **out_buf,
236 BN_CTX *ctx) {
237 *out_buf = NULL;
238 size_t len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx);
239 if (len == 0) {
240 return 0;
241 }
242 uint8_t *buf = OPENSSL_malloc(len);
243 if (buf == NULL) {
244 return 0;
245 }
246 len = EC_POINT_point2oct(group, point, form, buf, len, ctx);
247 if (len == 0) {
248 OPENSSL_free(buf);
249 return 0;
250 }
251 *out_buf = buf;
252 return len;
253 }
254
EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,int y_bit,BN_CTX * ctx)255 int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
256 EC_POINT *point, const BIGNUM *x,
257 int y_bit, BN_CTX *ctx) {
258 if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
259 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
260 return 0;
261 }
262
263 if (BN_is_negative(x) || BN_cmp(x, &group->field) >= 0) {
264 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
265 return 0;
266 }
267
268 BN_CTX *new_ctx = NULL;
269 int ret = 0;
270
271 ERR_clear_error();
272
273 if (ctx == NULL) {
274 ctx = new_ctx = BN_CTX_new();
275 if (ctx == NULL) {
276 return 0;
277 }
278 }
279
280 y_bit = (y_bit != 0);
281
282 BN_CTX_start(ctx);
283 BIGNUM *tmp1 = BN_CTX_get(ctx);
284 BIGNUM *tmp2 = BN_CTX_get(ctx);
285 BIGNUM *a = BN_CTX_get(ctx);
286 BIGNUM *b = BN_CTX_get(ctx);
287 BIGNUM *y = BN_CTX_get(ctx);
288 if (y == NULL ||
289 !EC_GROUP_get_curve_GFp(group, NULL, a, b, ctx)) {
290 goto err;
291 }
292
293 // Recover y. We have a Weierstrass equation
294 // y^2 = x^3 + a*x + b,
295 // so y is one of the square roots of x^3 + a*x + b.
296
297 // tmp1 := x^3
298 if (!BN_mod_sqr(tmp2, x, &group->field, ctx) ||
299 !BN_mod_mul(tmp1, tmp2, x, &group->field, ctx)) {
300 goto err;
301 }
302
303 // tmp1 := tmp1 + a*x
304 if (group->a_is_minus3) {
305 if (!bn_mod_lshift1_consttime(tmp2, x, &group->field, ctx) ||
306 !bn_mod_add_consttime(tmp2, tmp2, x, &group->field, ctx) ||
307 !bn_mod_sub_consttime(tmp1, tmp1, tmp2, &group->field, ctx)) {
308 goto err;
309 }
310 } else {
311 if (!BN_mod_mul(tmp2, a, x, &group->field, ctx) ||
312 !bn_mod_add_consttime(tmp1, tmp1, tmp2, &group->field, ctx)) {
313 goto err;
314 }
315 }
316
317 // tmp1 := tmp1 + b
318 if (!bn_mod_add_consttime(tmp1, tmp1, b, &group->field, ctx)) {
319 goto err;
320 }
321
322 if (!BN_mod_sqrt(y, tmp1, &group->field, ctx)) {
323 uint32_t err = ERR_peek_last_error();
324 if (ERR_GET_LIB(err) == ERR_LIB_BN &&
325 ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
326 ERR_clear_error();
327 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
328 } else {
329 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
330 }
331 goto err;
332 }
333
334 if (y_bit != BN_is_odd(y)) {
335 if (BN_is_zero(y)) {
336 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT);
337 goto err;
338 }
339 if (!BN_usub(y, &group->field, y)) {
340 goto err;
341 }
342 }
343 if (y_bit != BN_is_odd(y)) {
344 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
345 goto err;
346 }
347
348 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
349 goto err;
350 }
351
352 ret = 1;
353
354 err:
355 BN_CTX_end(ctx);
356 BN_CTX_free(new_ctx);
357 return ret;
358 }
359