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