blob: 4e8272da654044b1fcaafcc6d56758b33774ee19 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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>
David Benjamin4969cc92016-04-22 15:02:23 -040071#include <openssl/bytestring.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080072#include <openssl/err.h>
73
74#include "internal.h"
75
76
77static size_t ec_GFp_simple_point2oct(const EC_GROUP *group,
78 const EC_POINT *point,
79 point_conversion_form_t form,
80 uint8_t *buf, size_t len, BN_CTX *ctx) {
81 size_t ret;
82 BN_CTX *new_ctx = NULL;
83 int used_ctx = 0;
84 BIGNUM *x, *y;
85 size_t field_len, i;
86
87 if ((form != POINT_CONVERSION_COMPRESSED) &&
88 (form != POINT_CONVERSION_UNCOMPRESSED)) {
Kenny Rootb8494592015-09-25 02:29:14 +000089 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM);
Adam Langleyd9e397b2015-01-22 14:27:53 -080090 goto err;
91 }
92
93 if (EC_POINT_is_at_infinity(group, point)) {
Adam Langley4139edb2016-01-13 15:00:54 -080094 OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
95 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -080096 }
97
Adam Langleyd9e397b2015-01-22 14:27:53 -080098 /* ret := required output buffer length */
99 field_len = BN_num_bytes(&group->field);
100 ret =
101 (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
102
103 /* if 'buf' is NULL, just return required length */
104 if (buf != NULL) {
105 if (len < ret) {
Kenny Rootb8494592015-09-25 02:29:14 +0000106 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800107 goto err;
108 }
109
110 if (ctx == NULL) {
111 ctx = new_ctx = BN_CTX_new();
112 if (ctx == NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800113 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800114 }
115 }
116
117 BN_CTX_start(ctx);
118 used_ctx = 1;
119 x = BN_CTX_get(ctx);
120 y = BN_CTX_get(ctx);
121 if (y == NULL) {
122 goto err;
123 }
124
125 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) {
126 goto err;
127 }
128
129 if ((form == POINT_CONVERSION_COMPRESSED) &&
130 BN_is_odd(y)) {
131 buf[0] = form + 1;
132 } else {
133 buf[0] = form;
134 }
135 i = 1;
136
137 if (!BN_bn2bin_padded(buf + i, field_len, x)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000138 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800139 goto err;
140 }
141 i += field_len;
142
143 if (form == POINT_CONVERSION_UNCOMPRESSED) {
144 if (!BN_bn2bin_padded(buf + i, field_len, y)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000145 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800146 goto err;
147 }
148 i += field_len;
149 }
150
151 if (i != ret) {
Kenny Rootb8494592015-09-25 02:29:14 +0000152 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800153 goto err;
154 }
155 }
156
157 if (used_ctx) {
158 BN_CTX_end(ctx);
159 }
Adam Langleye9ada862015-05-11 17:20:37 -0700160 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800161 return ret;
162
163err:
164 if (used_ctx) {
165 BN_CTX_end(ctx);
166 }
Adam Langleye9ada862015-05-11 17:20:37 -0700167 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800168 return 0;
169}
170
171
172static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
173 const uint8_t *buf, size_t len,
174 BN_CTX *ctx) {
175 point_conversion_form_t form;
176 int y_bit;
177 BN_CTX *new_ctx = NULL;
178 BIGNUM *x, *y;
179 size_t field_len, enc_len;
180 int ret = 0;
181
182 if (len == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000183 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800184 return 0;
185 }
186 form = buf[0];
187 y_bit = form & 1;
188 form = form & ~1U;
Adam Langley4139edb2016-01-13 15:00:54 -0800189 if ((form != POINT_CONVERSION_COMPRESSED &&
190 form != POINT_CONVERSION_UNCOMPRESSED) ||
191 (form == POINT_CONVERSION_UNCOMPRESSED && y_bit)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000192 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800193 return 0;
194 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800195
196 field_len = BN_num_bytes(&group->field);
197 enc_len =
198 (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
199
200 if (len != enc_len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000201 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800202 return 0;
203 }
204
205 if (ctx == NULL) {
206 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700207 if (ctx == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800208 return 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700209 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800210 }
211
212 BN_CTX_start(ctx);
213 x = BN_CTX_get(ctx);
214 y = BN_CTX_get(ctx);
Kenny Rootb8494592015-09-25 02:29:14 +0000215 if (x == NULL || y == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800216 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700217 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800218
Adam Langleye9ada862015-05-11 17:20:37 -0700219 if (!BN_bin2bn(buf + 1, field_len, x)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800220 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700221 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800222 if (BN_ucmp(x, &group->field) >= 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000223 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800224 goto err;
225 }
226
227 if (form == POINT_CONVERSION_COMPRESSED) {
Adam Langleye9ada862015-05-11 17:20:37 -0700228 if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800229 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700230 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800231 } else {
Adam Langleye9ada862015-05-11 17:20:37 -0700232 if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800233 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700234 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800235 if (BN_ucmp(y, &group->field) >= 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000236 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800237 goto err;
238 }
239
Adam Langleye9ada862015-05-11 17:20:37 -0700240 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800241 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700242 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800243 }
244
Adam Langleyd9e397b2015-01-22 14:27:53 -0800245 ret = 1;
246
247err:
248 BN_CTX_end(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700249 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800250 return ret;
251}
252
253int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
254 const uint8_t *buf, size_t len, BN_CTX *ctx) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800255 if (group->meth != point->meth) {
Kenny Rootb8494592015-09-25 02:29:14 +0000256 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800257 return 0;
258 }
Kenny Roote99801b2015-11-06 15:31:15 -0800259 return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800260}
261
262size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
263 point_conversion_form_t form, uint8_t *buf,
264 size_t len, BN_CTX *ctx) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800265 if (group->meth != point->meth) {
Kenny Rootb8494592015-09-25 02:29:14 +0000266 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800267 return 0;
268 }
Kenny Roote99801b2015-11-06 15:31:15 -0800269 return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800270}
271
David Benjamin4969cc92016-04-22 15:02:23 -0400272int EC_POINT_point2cbb(CBB *out, const EC_GROUP *group, const EC_POINT *point,
273 point_conversion_form_t form, BN_CTX *ctx) {
274 size_t len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx);
275 if (len == 0) {
276 return 0;
277 }
278 uint8_t *p;
279 return CBB_add_space(out, &p, len) &&
280 EC_POINT_point2oct(group, point, form, p, len, ctx) == len;
281}
282
Adam Langleyd9e397b2015-01-22 14:27:53 -0800283int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
David Benjamin4969cc92016-04-22 15:02:23 -0400284 EC_POINT *point, const BIGNUM *x,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800285 int y_bit, BN_CTX *ctx) {
David Benjamin4969cc92016-04-22 15:02:23 -0400286 if (BN_is_negative(x) || BN_cmp(x, &group->field) >= 0) {
David Benjamin1b249672016-12-06 18:25:50 -0500287 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
David Benjamin4969cc92016-04-22 15:02:23 -0400288 return 0;
289 }
290
Adam Langleyd9e397b2015-01-22 14:27:53 -0800291 BN_CTX *new_ctx = NULL;
David Benjamin4969cc92016-04-22 15:02:23 -0400292 BIGNUM *tmp1, *tmp2, *y;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800293 int ret = 0;
294
295 ERR_clear_error();
296
297 if (ctx == NULL) {
298 ctx = new_ctx = BN_CTX_new();
299 if (ctx == NULL) {
300 return 0;
301 }
302 }
303
304 y_bit = (y_bit != 0);
305
306 BN_CTX_start(ctx);
307 tmp1 = BN_CTX_get(ctx);
308 tmp2 = BN_CTX_get(ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800309 y = BN_CTX_get(ctx);
310 if (y == NULL) {
311 goto err;
312 }
313
314 /* Recover y. We have a Weierstrass equation
315 * y^2 = x^3 + a*x + b,
316 * so y is one of the square roots of x^3 + a*x + b. */
317
318 /* tmp1 := x^3 */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800319 if (group->meth->field_decode == 0) {
320 /* field_{sqr,mul} work on standard representation */
David Benjamin4969cc92016-04-22 15:02:23 -0400321 if (!group->meth->field_sqr(group, tmp2, x, ctx) ||
322 !group->meth->field_mul(group, tmp1, tmp2, x, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800323 goto err;
324 }
325 } else {
David Benjamin4969cc92016-04-22 15:02:23 -0400326 if (!BN_mod_sqr(tmp2, x, &group->field, ctx) ||
327 !BN_mod_mul(tmp1, tmp2, x, &group->field, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800328 goto err;
329 }
330 }
331
332 /* tmp1 := tmp1 + a*x */
333 if (group->a_is_minus3) {
334 if (!BN_mod_lshift1_quick(tmp2, x, &group->field) ||
335 !BN_mod_add_quick(tmp2, tmp2, x, &group->field) ||
336 !BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) {
337 goto err;
338 }
339 } else {
340 if (group->meth->field_decode) {
341 if (!group->meth->field_decode(group, tmp2, &group->a, ctx) ||
342 !BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) {
343 goto err;
344 }
345 } else {
346 /* field_mul works on standard representation */
347 if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) {
348 goto err;
349 }
350 }
351
352 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) {
353 goto err;
354 }
355 }
356
357 /* tmp1 := tmp1 + b */
358 if (group->meth->field_decode) {
359 if (!group->meth->field_decode(group, tmp2, &group->b, ctx) ||
360 !BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) {
361 goto err;
362 }
363 } else {
364 if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) {
365 goto err;
366 }
367 }
368
369 if (!BN_mod_sqrt(y, tmp1, &group->field, ctx)) {
370 unsigned long err = ERR_peek_last_error();
371
372 if (ERR_GET_LIB(err) == ERR_LIB_BN &&
373 ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
374 ERR_clear_error();
Kenny Rootb8494592015-09-25 02:29:14 +0000375 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800376 } else {
Kenny Rootb8494592015-09-25 02:29:14 +0000377 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800378 }
379 goto err;
380 }
381
382 if (y_bit != BN_is_odd(y)) {
383 if (BN_is_zero(y)) {
David Benjamin1b249672016-12-06 18:25:50 -0500384 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800385 goto err;
386 }
387 if (!BN_usub(y, &group->field, y)) {
388 goto err;
389 }
390 }
391 if (y_bit != BN_is_odd(y)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000392 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800393 goto err;
394 }
395
Adam Langleye9ada862015-05-11 17:20:37 -0700396 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800397 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700398 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800399
400 ret = 1;
401
402err:
403 BN_CTX_end(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700404 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800405 return ret;
406}
407
408int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
409 EC_POINT *point, const BIGNUM *x,
410 int y_bit, BN_CTX *ctx) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800411 if (group->meth != point->meth) {
Kenny Rootb8494592015-09-25 02:29:14 +0000412 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800413 return 0;
414 }
Kenny Roote99801b2015-11-06 15:31:15 -0800415 return ec_GFp_simple_set_compressed_coordinates(group, point, x, y_bit, ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800416}