416c0e08fc5b9648ae59226725cdfcaccf3348c8
[openssl.git] / crypto / ec / ec_key.c
1 /*
2  * Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4  *
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
9  */
10
11 #include "internal/cryptlib.h"
12 #include <string.h>
13 #include "ec_lcl.h"
14 #include "internal/refcount.h"
15 #include <openssl/err.h>
16 #include <openssl/engine.h>
17
18 EC_KEY *EC_KEY_new(void)
19 {
20     return EC_KEY_new_method(NULL);
21 }
22
23 EC_KEY *EC_KEY_new_by_curve_name(int nid)
24 {
25     EC_KEY *ret = EC_KEY_new();
26     if (ret == NULL)
27         return NULL;
28     ret->group = EC_GROUP_new_by_curve_name(nid);
29     if (ret->group == NULL) {
30         EC_KEY_free(ret);
31         return NULL;
32     }
33     if (ret->meth->set_group != NULL
34         && ret->meth->set_group(ret, ret->group) == 0) {
35         EC_KEY_free(ret);
36         return NULL;
37     }
38     return ret;
39 }
40
41 void EC_KEY_free(EC_KEY *r)
42 {
43     int i;
44
45     if (r == NULL)
46         return;
47
48     CRYPTO_DOWN_REF(&r->references, &i, r->lock);
49     REF_PRINT_COUNT("EC_KEY", r);
50     if (i > 0)
51         return;
52     REF_ASSERT_ISNT(i < 0);
53
54     if (r->meth != NULL && r->meth->finish != NULL)
55         r->meth->finish(r);
56
57 #ifndef OPENSSL_NO_ENGINE
58     ENGINE_finish(r->engine);
59 #endif
60
61     if (r->group && r->group->meth->keyfinish)
62         r->group->meth->keyfinish(r);
63
64     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
65     CRYPTO_THREAD_lock_free(r->lock);
66     EC_GROUP_free(r->group);
67     EC_POINT_free(r->pub_key);
68     BN_clear_free(r->priv_key);
69
70     OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
71 }
72
73 EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src)
74 {
75     if (dest == NULL || src == NULL) {
76         ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
77         return NULL;
78     }
79     if (src->meth != dest->meth) {
80         if (dest->meth->finish != NULL)
81             dest->meth->finish(dest);
82         if (dest->group && dest->group->meth->keyfinish)
83             dest->group->meth->keyfinish(dest);
84 #ifndef OPENSSL_NO_ENGINE
85         if (ENGINE_finish(dest->engine) == 0)
86             return 0;
87         dest->engine = NULL;
88 #endif
89     }
90     /* copy the parameters */
91     if (src->group != NULL) {
92         const EC_METHOD *meth = EC_GROUP_method_of(src->group);
93         /* clear the old group */
94         EC_GROUP_free(dest->group);
95         dest->group = EC_GROUP_new(meth);
96         if (dest->group == NULL)
97             return NULL;
98         if (!EC_GROUP_copy(dest->group, src->group))
99             return NULL;
100
101         /*  copy the public key */
102         if (src->pub_key != NULL) {
103             EC_POINT_free(dest->pub_key);
104             dest->pub_key = EC_POINT_new(src->group);
105             if (dest->pub_key == NULL)
106                 return NULL;
107             if (!EC_POINT_copy(dest->pub_key, src->pub_key))
108                 return NULL;
109         }
110         /* copy the private key */
111         if (src->priv_key != NULL) {
112             if (dest->priv_key == NULL) {
113                 dest->priv_key = BN_new();
114                 if (dest->priv_key == NULL)
115                     return NULL;
116             }
117             if (!BN_copy(dest->priv_key, src->priv_key))
118                 return NULL;
119             if (src->group->meth->keycopy
120                 && src->group->meth->keycopy(dest, src) == 0)
121                 return NULL;
122         }
123     }
124
125
126     /* copy the rest */
127     dest->enc_flag = src->enc_flag;
128     dest->conv_form = src->conv_form;
129     dest->version = src->version;
130     dest->flags = src->flags;
131     if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
132                             &dest->ex_data, &src->ex_data))
133         return NULL;
134
135     if (src->meth != dest->meth) {
136 #ifndef OPENSSL_NO_ENGINE
137         if (src->engine != NULL && ENGINE_init(src->engine) == 0)
138             return NULL;
139         dest->engine = src->engine;
140 #endif
141         dest->meth = src->meth;
142     }
143
144     if (src->meth->copy != NULL && src->meth->copy(dest, src) == 0)
145         return NULL;
146
147     return dest;
148 }
149
150 EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
151 {
152     EC_KEY *ret = EC_KEY_new_method(ec_key->engine);
153
154     if (ret == NULL)
155         return NULL;
156
157     if (EC_KEY_copy(ret, ec_key) == NULL) {
158         EC_KEY_free(ret);
159         return NULL;
160     }
161     return ret;
162 }
163
164 int EC_KEY_up_ref(EC_KEY *r)
165 {
166     int i;
167
168     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
169         return 0;
170
171     REF_PRINT_COUNT("EC_KEY", r);
172     REF_ASSERT_ISNT(i < 2);
173     return ((i > 1) ? 1 : 0);
174 }
175
176 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey)
177 {
178     return eckey->engine;
179 }
180
181 int EC_KEY_generate_key(EC_KEY *eckey)
182 {
183     if (eckey == NULL || eckey->group == NULL) {
184         ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
185         return 0;
186     }
187     if (eckey->meth->keygen != NULL)
188         return eckey->meth->keygen(eckey);
189     ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
190     return 0;
191 }
192
193 int ossl_ec_key_gen(EC_KEY *eckey)
194 {
195     return eckey->group->meth->keygen(eckey);
196 }
197
198 /*
199  * ECC Key generation.
200  * See SP800-56AR3 5.6.1.2.2 "Key Pair Generation by Testing Candidates"
201  *
202  * Params:
203  *     eckey An EC key object that contains domain params. The generated keypair
204  *           is stored in this object.
205  * Returns 1 if the keypair was generated or 0 otherwise.
206  */
207 int ec_key_simple_generate_key(EC_KEY *eckey)
208 {
209     int ok = 0;
210     BIGNUM *priv_key = NULL;
211     const BIGNUM *order = NULL;
212     EC_POINT *pub_key = NULL;
213     const EC_GROUP *group = eckey->group;
214
215     if (eckey->priv_key == NULL) {
216         priv_key = BN_secure_new();
217         if (priv_key == NULL)
218             goto err;
219     } else
220         priv_key = eckey->priv_key;
221
222     /*
223      * Steps (1-2): Check domain parameters and security strength.
224      * These steps must be done by the user. This would need to be
225      * stated in the security policy.
226      */
227
228     order = EC_GROUP_get0_order(group);
229     if (order == NULL)
230         goto err;
231
232     /*
233      * Steps (3-7): priv_key = DRBG_RAND(order_n_bits) (range [1, n-1]).
234      * Although this is slightly different from the standard, it is effectively
235      * equivalent as it gives an unbiased result ranging from 1..n-1. It is also
236      * faster as the standard needs to retry more often. Also doing
237      * 1 + rand[0..n-2] would effect the way that tests feed dummy entropy into
238      * rand so the simpler backward compatible method has been used here.
239      */
240     do
241         if (!BN_priv_rand_range(priv_key, order))
242             goto err;
243     while (BN_is_zero(priv_key)) ;
244
245     if (eckey->pub_key == NULL) {
246         pub_key = EC_POINT_new(group);
247         if (pub_key == NULL)
248             goto err;
249     } else
250         pub_key = eckey->pub_key;
251
252     /* Step (8) : pub_key = priv_key * G (where G is a point on the curve) */
253     if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL))
254         goto err;
255
256     eckey->priv_key = priv_key;
257     eckey->pub_key = pub_key;
258     priv_key = NULL;
259     pub_key = NULL;
260
261     ok = 1;
262
263 err:
264     /* Step (9): If there is an error return an invalid keypair. */
265     if (!ok) {
266         BN_clear(eckey->priv_key);
267         if (eckey->pub_key != NULL)
268             EC_POINT_set_to_infinity(group, eckey->pub_key);
269     }
270
271     EC_POINT_free(pub_key);
272     BN_clear_free(priv_key);
273     return ok;
274 }
275
276 int ec_key_simple_generate_public_key(EC_KEY *eckey)
277 {
278     /*
279      * See SP800-56AR3 5.6.1.2.2: Step (8)
280      * pub_key = priv_key * G (where G is a point on the curve)
281      */
282     return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
283                         NULL, NULL);
284 }
285
286 int EC_KEY_check_key(const EC_KEY *eckey)
287 {
288     if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
289         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
290         return 0;
291     }
292
293     if (eckey->group->meth->keycheck == NULL) {
294         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
295         return 0;
296     }
297
298     return eckey->group->meth->keycheck(eckey);
299 }
300
301 int ec_key_simple_check_key(const EC_KEY *eckey)
302 {
303     int ok = 0;
304     BN_CTX *ctx = NULL;
305     const BIGNUM *order = NULL;
306     EC_POINT *point = NULL;
307
308     if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
309         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
310         return 0;
311     }
312
313     if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
314         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
315         goto err;
316     }
317
318     if ((ctx = BN_CTX_new()) == NULL)
319         goto err;
320     if ((point = EC_POINT_new(eckey->group)) == NULL)
321         goto err;
322
323     /* testing whether the pub_key is on the elliptic curve */
324     if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
325         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
326         goto err;
327     }
328     /* testing whether pub_key * order is the point at infinity */
329     order = eckey->group->order;
330     if (BN_is_zero(order)) {
331         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
332         goto err;
333     }
334     if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
335         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
336         goto err;
337     }
338     if (!EC_POINT_is_at_infinity(eckey->group, point)) {
339         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
340         goto err;
341     }
342     /*
343      * in case the priv_key is present : check if generator * priv_key ==
344      * pub_key
345      */
346     if (eckey->priv_key != NULL) {
347         if (BN_cmp(eckey->priv_key, order) >= 0) {
348             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
349             goto err;
350         }
351         if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
352                           NULL, NULL, ctx)) {
353             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
354             goto err;
355         }
356         if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
357             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
358             goto err;
359         }
360     }
361     ok = 1;
362  err:
363     BN_CTX_free(ctx);
364     EC_POINT_free(point);
365     return ok;
366 }
367
368 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
369                                              BIGNUM *y)
370 {
371     BN_CTX *ctx = NULL;
372     BIGNUM *tx, *ty;
373     EC_POINT *point = NULL;
374     int ok = 0;
375
376     if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
377         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
378               ERR_R_PASSED_NULL_PARAMETER);
379         return 0;
380     }
381     ctx = BN_CTX_new();
382     if (ctx == NULL)
383         return 0;
384
385     BN_CTX_start(ctx);
386     point = EC_POINT_new(key->group);
387
388     if (point == NULL)
389         goto err;
390
391     tx = BN_CTX_get(ctx);
392     ty = BN_CTX_get(ctx);
393     if (ty == NULL)
394         goto err;
395
396     if (!EC_POINT_set_affine_coordinates(key->group, point, x, y, ctx))
397         goto err;
398     if (!EC_POINT_get_affine_coordinates(key->group, point, tx, ty, ctx))
399         goto err;
400
401     /*
402      * Check if retrieved coordinates match originals and are less than field
403      * order: if not values are out of range.
404      */
405     if (BN_cmp(x, tx) || BN_cmp(y, ty)
406         || (BN_cmp(x, key->group->field) >= 0)
407         || (BN_cmp(y, key->group->field) >= 0)) {
408         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
409               EC_R_COORDINATES_OUT_OF_RANGE);
410         goto err;
411     }
412
413     if (!EC_KEY_set_public_key(key, point))
414         goto err;
415
416     if (EC_KEY_check_key(key) == 0)
417         goto err;
418
419     ok = 1;
420
421  err:
422     BN_CTX_end(ctx);
423     BN_CTX_free(ctx);
424     EC_POINT_free(point);
425     return ok;
426
427 }
428
429 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
430 {
431     return key->group;
432 }
433
434 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
435 {
436     if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
437         return 0;
438     EC_GROUP_free(key->group);
439     key->group = EC_GROUP_dup(group);
440     return (key->group == NULL) ? 0 : 1;
441 }
442
443 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
444 {
445     return key->priv_key;
446 }
447
448 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
449 {
450     if (key->group == NULL || key->group->meth == NULL)
451         return 0;
452     if (key->group->meth->set_private != NULL
453         && key->group->meth->set_private(key, priv_key) == 0)
454         return 0;
455     if (key->meth->set_private != NULL
456         && key->meth->set_private(key, priv_key) == 0)
457         return 0;
458     BN_clear_free(key->priv_key);
459     key->priv_key = BN_dup(priv_key);
460     return (key->priv_key == NULL) ? 0 : 1;
461 }
462
463 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
464 {
465     return key->pub_key;
466 }
467
468 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
469 {
470     if (key->meth->set_public != NULL
471         && key->meth->set_public(key, pub_key) == 0)
472         return 0;
473     EC_POINT_free(key->pub_key);
474     key->pub_key = EC_POINT_dup(pub_key, key->group);
475     return (key->pub_key == NULL) ? 0 : 1;
476 }
477
478 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
479 {
480     return key->enc_flag;
481 }
482
483 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
484 {
485     key->enc_flag = flags;
486 }
487
488 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
489 {
490     return key->conv_form;
491 }
492
493 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
494 {
495     key->conv_form = cform;
496     if (key->group != NULL)
497         EC_GROUP_set_point_conversion_form(key->group, cform);
498 }
499
500 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
501 {
502     if (key->group != NULL)
503         EC_GROUP_set_asn1_flag(key->group, flag);
504 }
505
506 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
507 {
508     if (key->group == NULL)
509         return 0;
510     return EC_GROUP_precompute_mult(key->group, ctx);
511 }
512
513 int EC_KEY_get_flags(const EC_KEY *key)
514 {
515     return key->flags;
516 }
517
518 void EC_KEY_set_flags(EC_KEY *key, int flags)
519 {
520     key->flags |= flags;
521 }
522
523 void EC_KEY_clear_flags(EC_KEY *key, int flags)
524 {
525     key->flags &= ~flags;
526 }
527
528 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
529                         unsigned char **pbuf, BN_CTX *ctx)
530 {
531     if (key == NULL || key->pub_key == NULL || key->group == NULL)
532         return 0;
533     return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
534 }
535
536 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
537                    BN_CTX *ctx)
538 {
539     if (key == NULL || key->group == NULL)
540         return 0;
541     if (key->pub_key == NULL)
542         key->pub_key = EC_POINT_new(key->group);
543     if (key->pub_key == NULL)
544         return 0;
545     if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
546         return 0;
547     /*
548      * Save the point conversion form.
549      * For non-custom curves the first octet of the buffer (excluding
550      * the last significant bit) contains the point conversion form.
551      * EC_POINT_oct2point() has already performed sanity checking of
552      * the buffer so we know it is valid.
553      */
554     if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
555         key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
556     return 1;
557 }
558
559 size_t EC_KEY_priv2oct(const EC_KEY *eckey,
560                        unsigned char *buf, size_t len)
561 {
562     if (eckey->group == NULL || eckey->group->meth == NULL)
563         return 0;
564     if (eckey->group->meth->priv2oct == NULL) {
565         ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
566         return 0;
567     }
568
569     return eckey->group->meth->priv2oct(eckey, buf, len);
570 }
571
572 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
573                               unsigned char *buf, size_t len)
574 {
575     size_t buf_len;
576
577     buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
578     if (eckey->priv_key == NULL)
579         return 0;
580     if (buf == NULL)
581         return buf_len;
582     else if (len < buf_len)
583         return 0;
584
585     /* Octetstring may need leading zeros if BN is to short */
586
587     if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
588         ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
589         return 0;
590     }
591
592     return buf_len;
593 }
594
595 int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
596 {
597     if (eckey->group == NULL || eckey->group->meth == NULL)
598         return 0;
599     if (eckey->group->meth->oct2priv == NULL) {
600         ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
601         return 0;
602     }
603     return eckey->group->meth->oct2priv(eckey, buf, len);
604 }
605
606 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
607 {
608     if (eckey->priv_key == NULL)
609         eckey->priv_key = BN_secure_new();
610     if (eckey->priv_key == NULL) {
611         ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
612         return 0;
613     }
614     eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
615     if (eckey->priv_key == NULL) {
616         ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
617         return 0;
618     }
619     return 1;
620 }
621
622 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
623 {
624     size_t len;
625     unsigned char *buf;
626
627     len = EC_KEY_priv2oct(eckey, NULL, 0);
628     if (len == 0)
629         return 0;
630     if ((buf = OPENSSL_malloc(len)) == NULL) {
631         ECerr(EC_F_EC_KEY_PRIV2BUF, ERR_R_MALLOC_FAILURE);
632         return 0;
633     }
634     len = EC_KEY_priv2oct(eckey, buf, len);
635     if (len == 0) {
636         OPENSSL_free(buf);
637         return 0;
638     }
639     *pbuf = buf;
640     return len;
641 }
642
643 int EC_KEY_can_sign(const EC_KEY *eckey)
644 {
645     if (eckey->group == NULL || eckey->group->meth == NULL
646         || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))
647         return 0;
648     return 1;
649 }