xref: /openbsd-src/lib/libcrypto/ec/ec_key.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /* $OpenBSD: ec_key.c,v 1.22 2018/11/09 23:39:45 tb Exp $ */
2 /*
3  * Written by Nils Larsch for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    openssl-core@openssl.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 /* ====================================================================
59  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60  * Portions originally developed by SUN MICROSYSTEMS, INC., and
61  * contributed to the OpenSSL project.
62  */
63 
64 #include <string.h>
65 
66 #include <openssl/opensslconf.h>
67 
68 #include "bn_lcl.h"
69 #include "ec_lcl.h"
70 #include <openssl/err.h>
71 
72 EC_KEY *
73 EC_KEY_new(void)
74 {
75 	EC_KEY *ret;
76 
77 	ret = malloc(sizeof(EC_KEY));
78 	if (ret == NULL) {
79 		ECerror(ERR_R_MALLOC_FAILURE);
80 		return (NULL);
81 	}
82 	ret->version = 1;
83 	ret->flags = 0;
84 	ret->group = NULL;
85 	ret->pub_key = NULL;
86 	ret->priv_key = NULL;
87 	ret->enc_flag = 0;
88 	ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
89 	ret->references = 1;
90 	ret->method_data = NULL;
91 	return (ret);
92 }
93 
94 EC_KEY *
95 EC_KEY_new_by_curve_name(int nid)
96 {
97 	EC_KEY *ret = EC_KEY_new();
98 	if (ret == NULL)
99 		return NULL;
100 	ret->group = EC_GROUP_new_by_curve_name(nid);
101 	if (ret->group == NULL) {
102 		EC_KEY_free(ret);
103 		return NULL;
104 	}
105 	return ret;
106 }
107 
108 void
109 EC_KEY_free(EC_KEY * r)
110 {
111 	int i;
112 
113 	if (r == NULL)
114 		return;
115 
116 	i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_EC);
117 	if (i > 0)
118 		return;
119 
120 	EC_GROUP_free(r->group);
121 	EC_POINT_free(r->pub_key);
122 	BN_clear_free(r->priv_key);
123 
124 	EC_EX_DATA_free_all_data(&r->method_data);
125 
126 	freezero(r, sizeof(EC_KEY));
127 }
128 
129 EC_KEY *
130 EC_KEY_copy(EC_KEY * dest, const EC_KEY * src)
131 {
132 	EC_EXTRA_DATA *d;
133 
134 	if (dest == NULL || src == NULL) {
135 		ECerror(ERR_R_PASSED_NULL_PARAMETER);
136 		return NULL;
137 	}
138 	/* copy the parameters */
139 	if (src->group) {
140 		const EC_METHOD *meth = EC_GROUP_method_of(src->group);
141 		/* clear the old group */
142 		EC_GROUP_free(dest->group);
143 		dest->group = EC_GROUP_new(meth);
144 		if (dest->group == NULL)
145 			return NULL;
146 		if (!EC_GROUP_copy(dest->group, src->group))
147 			return NULL;
148 	}
149 	/* copy the public key */
150 	if (src->pub_key && src->group) {
151 		EC_POINT_free(dest->pub_key);
152 		dest->pub_key = EC_POINT_new(src->group);
153 		if (dest->pub_key == NULL)
154 			return NULL;
155 		if (!EC_POINT_copy(dest->pub_key, src->pub_key))
156 			return NULL;
157 	}
158 	/* copy the private key */
159 	if (src->priv_key) {
160 		if (dest->priv_key == NULL) {
161 			dest->priv_key = BN_new();
162 			if (dest->priv_key == NULL)
163 				return NULL;
164 		}
165 		if (!BN_copy(dest->priv_key, src->priv_key))
166 			return NULL;
167 	}
168 	/* copy method/extra data */
169 	EC_EX_DATA_free_all_data(&dest->method_data);
170 
171 	for (d = src->method_data; d != NULL; d = d->next) {
172 		void *t = d->dup_func(d->data);
173 
174 		if (t == NULL)
175 			return 0;
176 		if (!EC_EX_DATA_set_data(&dest->method_data, t, d->dup_func,
177 		    d->free_func, d->clear_free_func))
178 			return 0;
179 	}
180 
181 	/* copy the rest */
182 	dest->enc_flag = src->enc_flag;
183 	dest->conv_form = src->conv_form;
184 	dest->version = src->version;
185 	dest->flags = src->flags;
186 
187 	return dest;
188 }
189 
190 EC_KEY *
191 EC_KEY_dup(const EC_KEY * ec_key)
192 {
193 	EC_KEY *ret = EC_KEY_new();
194 	if (ret == NULL)
195 		return NULL;
196 	if (EC_KEY_copy(ret, ec_key) == NULL) {
197 		EC_KEY_free(ret);
198 		return NULL;
199 	}
200 	return ret;
201 }
202 
203 int
204 EC_KEY_up_ref(EC_KEY * r)
205 {
206 	int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_EC);
207 	return ((i > 1) ? 1 : 0);
208 }
209 
210 int
211 EC_KEY_generate_key(EC_KEY *eckey)
212 {
213 	int ok = 0;
214 	BN_CTX *ctx = NULL;
215 	BIGNUM *priv_key = NULL, *order = NULL;
216 	EC_POINT *pub_key = NULL;
217 
218 	if (!eckey || !eckey->group) {
219 		ECerror(ERR_R_PASSED_NULL_PARAMETER);
220 		return 0;
221 	}
222 
223 	if ((order = BN_new()) == NULL)
224 		goto err;
225 	if ((ctx = BN_CTX_new()) == NULL)
226 		goto err;
227 
228 	if ((priv_key = eckey->priv_key) == NULL) {
229 		if ((priv_key = BN_new()) == NULL)
230 			goto err;
231 	}
232 
233 	if (!EC_GROUP_get_order(eckey->group, order, ctx))
234 		goto err;
235 
236 	if (!bn_rand_interval(priv_key, BN_value_one(), order))
237 		goto err;
238 
239 	if ((pub_key = eckey->pub_key) == NULL) {
240 		if ((pub_key = EC_POINT_new(eckey->group)) == NULL)
241 			goto err;
242 	}
243 
244 	if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
245 		goto err;
246 
247 	eckey->priv_key = priv_key;
248 	eckey->pub_key = pub_key;
249 
250 	ok = 1;
251 
252  err:
253 	BN_free(order);
254 	if (eckey->pub_key == NULL)
255 		EC_POINT_free(pub_key);
256 	if (eckey->priv_key == NULL)
257 		BN_free(priv_key);
258 	BN_CTX_free(ctx);
259 	return (ok);
260 }
261 
262 int
263 EC_KEY_check_key(const EC_KEY * eckey)
264 {
265 	int ok = 0;
266 	BN_CTX *ctx = NULL;
267 	const BIGNUM *order = NULL;
268 	EC_POINT *point = NULL;
269 
270 	if (!eckey || !eckey->group || !eckey->pub_key) {
271 		ECerror(ERR_R_PASSED_NULL_PARAMETER);
272 		return 0;
273 	}
274 	if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key) > 0) {
275 		ECerror(EC_R_POINT_AT_INFINITY);
276 		goto err;
277 	}
278 	if ((ctx = BN_CTX_new()) == NULL)
279 		goto err;
280 	if ((point = EC_POINT_new(eckey->group)) == NULL)
281 		goto err;
282 
283 	/* testing whether the pub_key is on the elliptic curve */
284 	if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
285 		ECerror(EC_R_POINT_IS_NOT_ON_CURVE);
286 		goto err;
287 	}
288 	/* testing whether pub_key * order is the point at infinity */
289 	order = &eckey->group->order;
290 	if (BN_is_zero(order)) {
291 		ECerror(EC_R_INVALID_GROUP_ORDER);
292 		goto err;
293 	}
294 	if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
295 		ECerror(ERR_R_EC_LIB);
296 		goto err;
297 	}
298 	if (EC_POINT_is_at_infinity(eckey->group, point) <= 0) {
299 		ECerror(EC_R_WRONG_ORDER);
300 		goto err;
301 	}
302 	/*
303 	 * in case the priv_key is present : check if generator * priv_key ==
304 	 * pub_key
305 	 */
306 	if (eckey->priv_key) {
307 		if (BN_cmp(eckey->priv_key, order) >= 0) {
308 			ECerror(EC_R_WRONG_ORDER);
309 			goto err;
310 		}
311 		if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
312 			NULL, NULL, ctx)) {
313 			ECerror(ERR_R_EC_LIB);
314 			goto err;
315 		}
316 		if (EC_POINT_cmp(eckey->group, point, eckey->pub_key,
317 			ctx) != 0) {
318 			ECerror(EC_R_INVALID_PRIVATE_KEY);
319 			goto err;
320 		}
321 	}
322 	ok = 1;
323  err:
324 	BN_CTX_free(ctx);
325 	EC_POINT_free(point);
326 	return (ok);
327 }
328 
329 int
330 EC_KEY_set_public_key_affine_coordinates(EC_KEY * key, BIGNUM * x, BIGNUM * y)
331 {
332 	BN_CTX *ctx = NULL;
333 	BIGNUM *tx, *ty;
334 	EC_POINT *point = NULL;
335 	int ok = 0, tmp_nid, is_char_two = 0;
336 
337 	if (!key || !key->group || !x || !y) {
338 		ECerror(ERR_R_PASSED_NULL_PARAMETER);
339 		return 0;
340 	}
341 	ctx = BN_CTX_new();
342 	if (!ctx)
343 		goto err;
344 
345 	point = EC_POINT_new(key->group);
346 
347 	if (!point)
348 		goto err;
349 
350 	tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group));
351 
352 	if (tmp_nid == NID_X9_62_characteristic_two_field)
353 		is_char_two = 1;
354 
355 	if ((tx = BN_CTX_get(ctx)) == NULL)
356 		goto err;
357 	if ((ty = BN_CTX_get(ctx)) == NULL)
358 		goto err;
359 
360 #ifndef OPENSSL_NO_EC2M
361 	if (is_char_two) {
362 		if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point,
363 			x, y, ctx))
364 			goto err;
365 		if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point,
366 			tx, ty, ctx))
367 			goto err;
368 	} else
369 #endif
370 	{
371 		if (!EC_POINT_set_affine_coordinates_GFp(key->group, point,
372 			x, y, ctx))
373 			goto err;
374 		if (!EC_POINT_get_affine_coordinates_GFp(key->group, point,
375 			tx, ty, ctx))
376 			goto err;
377 	}
378 	/*
379 	 * Check if retrieved coordinates match originals: if not values are
380 	 * out of range.
381 	 */
382 	if (BN_cmp(x, tx) || BN_cmp(y, ty)) {
383 		ECerror(EC_R_COORDINATES_OUT_OF_RANGE);
384 		goto err;
385 	}
386 	if (!EC_KEY_set_public_key(key, point))
387 		goto err;
388 
389 	if (EC_KEY_check_key(key) == 0)
390 		goto err;
391 
392 	ok = 1;
393 
394  err:
395 	BN_CTX_free(ctx);
396 	EC_POINT_free(point);
397 	return ok;
398 
399 }
400 
401 const EC_GROUP *
402 EC_KEY_get0_group(const EC_KEY * key)
403 {
404 	return key->group;
405 }
406 
407 int
408 EC_KEY_set_group(EC_KEY * key, const EC_GROUP * group)
409 {
410 	EC_GROUP_free(key->group);
411 	key->group = EC_GROUP_dup(group);
412 	return (key->group == NULL) ? 0 : 1;
413 }
414 
415 const BIGNUM *
416 EC_KEY_get0_private_key(const EC_KEY * key)
417 {
418 	return key->priv_key;
419 }
420 
421 int
422 EC_KEY_set_private_key(EC_KEY * key, const BIGNUM * priv_key)
423 {
424 	BN_clear_free(key->priv_key);
425 	key->priv_key = BN_dup(priv_key);
426 	return (key->priv_key == NULL) ? 0 : 1;
427 }
428 
429 const EC_POINT *
430 EC_KEY_get0_public_key(const EC_KEY * key)
431 {
432 	return key->pub_key;
433 }
434 
435 int
436 EC_KEY_set_public_key(EC_KEY * key, const EC_POINT * pub_key)
437 {
438 	EC_POINT_free(key->pub_key);
439 	key->pub_key = EC_POINT_dup(pub_key, key->group);
440 	return (key->pub_key == NULL) ? 0 : 1;
441 }
442 
443 unsigned int
444 EC_KEY_get_enc_flags(const EC_KEY * key)
445 {
446 	return key->enc_flag;
447 }
448 
449 void
450 EC_KEY_set_enc_flags(EC_KEY * key, unsigned int flags)
451 {
452 	key->enc_flag = flags;
453 }
454 
455 point_conversion_form_t
456 EC_KEY_get_conv_form(const EC_KEY * key)
457 {
458 	return key->conv_form;
459 }
460 
461 void
462 EC_KEY_set_conv_form(EC_KEY * key, point_conversion_form_t cform)
463 {
464 	key->conv_form = cform;
465 	if (key->group != NULL)
466 		EC_GROUP_set_point_conversion_form(key->group, cform);
467 }
468 
469 void *
470 EC_KEY_get_key_method_data(EC_KEY *key,
471     void *(*dup_func) (void *),
472     void (*free_func) (void *),
473     void (*clear_free_func) (void *))
474 {
475 	void *ret;
476 
477 	CRYPTO_r_lock(CRYPTO_LOCK_EC);
478 	ret = EC_EX_DATA_get_data(key->method_data, dup_func, free_func, clear_free_func);
479 	CRYPTO_r_unlock(CRYPTO_LOCK_EC);
480 
481 	return ret;
482 }
483 
484 void *
485 EC_KEY_insert_key_method_data(EC_KEY * key, void *data,
486     void *(*dup_func) (void *),
487     void (*free_func) (void *),
488     void (*clear_free_func) (void *))
489 {
490 	EC_EXTRA_DATA *ex_data;
491 
492 	CRYPTO_w_lock(CRYPTO_LOCK_EC);
493 	ex_data = EC_EX_DATA_get_data(key->method_data, dup_func, free_func, clear_free_func);
494 	if (ex_data == NULL)
495 		EC_EX_DATA_set_data(&key->method_data, data, dup_func, free_func, clear_free_func);
496 	CRYPTO_w_unlock(CRYPTO_LOCK_EC);
497 
498 	return ex_data;
499 }
500 
501 void
502 EC_KEY_set_asn1_flag(EC_KEY * key, int flag)
503 {
504 	if (key->group != NULL)
505 		EC_GROUP_set_asn1_flag(key->group, flag);
506 }
507 
508 int
509 EC_KEY_precompute_mult(EC_KEY * key, BN_CTX * ctx)
510 {
511 	if (key->group == NULL)
512 		return 0;
513 	return EC_GROUP_precompute_mult(key->group, ctx);
514 }
515 
516 int
517 EC_KEY_get_flags(const EC_KEY * key)
518 {
519 	return key->flags;
520 }
521 
522 void
523 EC_KEY_set_flags(EC_KEY * key, int flags)
524 {
525 	key->flags |= flags;
526 }
527 
528 void
529 EC_KEY_clear_flags(EC_KEY * key, int flags)
530 {
531 	key->flags &= ~flags;
532 }
533