Import Curve 448 support
[openssl.git] / crypto / ec / ec_key.c
1 /*
2  * Copyright 2002-2017 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 "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->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 int ec_key_simple_generate_key(EC_KEY *eckey)
199 {
200     int ok = 0;
201     BN_CTX *ctx = NULL;
202     BIGNUM *priv_key = NULL;
203     const BIGNUM *order = NULL;
204     EC_POINT *pub_key = NULL;
205
206     if ((ctx = BN_CTX_new()) == NULL)
207         goto err;
208
209     if (eckey->priv_key == NULL) {
210         priv_key = BN_new();
211         if (priv_key == NULL)
212             goto err;
213     } else
214         priv_key = eckey->priv_key;
215
216     order = EC_GROUP_get0_order(eckey->group);
217     if (order == NULL)
218         goto err;
219
220     do
221         if (!BN_priv_rand_range(priv_key, order))
222             goto err;
223     while (BN_is_zero(priv_key)) ;
224
225     if (eckey->pub_key == NULL) {
226         pub_key = EC_POINT_new(eckey->group);
227         if (pub_key == NULL)
228             goto err;
229     } else
230         pub_key = eckey->pub_key;
231
232     if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
233         goto err;
234
235     eckey->priv_key = priv_key;
236     eckey->pub_key = pub_key;
237
238     ok = 1;
239
240  err:
241     if (eckey->pub_key == NULL)
242         EC_POINT_free(pub_key);
243     if (eckey->priv_key != priv_key)
244         BN_free(priv_key);
245     BN_CTX_free(ctx);
246     return ok;
247 }
248
249 int ec_key_simple_generate_public_key(EC_KEY *eckey)
250 {
251     return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
252                         NULL, NULL);
253 }
254
255 int EC_KEY_check_key(const EC_KEY *eckey)
256 {
257     if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
258         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
259         return 0;
260     }
261
262     if (eckey->group->meth->keycheck == NULL) {
263         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
264         return 0;
265     }
266
267     return eckey->group->meth->keycheck(eckey);
268 }
269
270 int ec_key_simple_check_key(const EC_KEY *eckey)
271 {
272     int ok = 0;
273     BN_CTX *ctx = NULL;
274     const BIGNUM *order = NULL;
275     EC_POINT *point = NULL;
276
277     if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
278         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
279         return 0;
280     }
281
282     if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
283         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
284         goto err;
285     }
286
287     if ((ctx = BN_CTX_new()) == NULL)
288         goto err;
289     if ((point = EC_POINT_new(eckey->group)) == NULL)
290         goto err;
291
292     /* testing whether the pub_key is on the elliptic curve */
293     if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
294         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
295         goto err;
296     }
297     /* testing whether pub_key * order is the point at infinity */
298     order = eckey->group->order;
299     if (BN_is_zero(order)) {
300         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
301         goto err;
302     }
303     if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
304         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
305         goto err;
306     }
307     if (!EC_POINT_is_at_infinity(eckey->group, point)) {
308         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
309         goto err;
310     }
311     /*
312      * in case the priv_key is present : check if generator * priv_key ==
313      * pub_key
314      */
315     if (eckey->priv_key != NULL) {
316         if (BN_cmp(eckey->priv_key, order) >= 0) {
317             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
318             goto err;
319         }
320         if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
321                           NULL, NULL, ctx)) {
322             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
323             goto err;
324         }
325         if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
326             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
327             goto err;
328         }
329     }
330     ok = 1;
331  err:
332     BN_CTX_free(ctx);
333     EC_POINT_free(point);
334     return ok;
335 }
336
337 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
338                                              BIGNUM *y)
339 {
340     BN_CTX *ctx = NULL;
341     BIGNUM *tx, *ty;
342     EC_POINT *point = NULL;
343     int ok = 0;
344 #ifndef OPENSSL_NO_EC2M
345     int tmp_nid, is_char_two = 0;
346 #endif
347
348     if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
349         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
350               ERR_R_PASSED_NULL_PARAMETER);
351         return 0;
352     }
353     ctx = BN_CTX_new();
354     if (ctx == NULL)
355         return 0;
356
357     BN_CTX_start(ctx);
358     point = EC_POINT_new(key->group);
359
360     if (point == NULL)
361         goto err;
362
363     tx = BN_CTX_get(ctx);
364     ty = BN_CTX_get(ctx);
365     if (ty == NULL)
366         goto err;
367
368 #ifndef OPENSSL_NO_EC2M
369     tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group));
370
371     if (tmp_nid == NID_X9_62_characteristic_two_field)
372         is_char_two = 1;
373
374     if (is_char_two) {
375         if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point,
376                                                   x, y, ctx))
377             goto err;
378         if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point,
379                                                   tx, ty, ctx))
380             goto err;
381     } else
382 #endif
383     {
384         if (!EC_POINT_set_affine_coordinates_GFp(key->group, point,
385                                                  x, y, ctx))
386             goto err;
387         if (!EC_POINT_get_affine_coordinates_GFp(key->group, point,
388                                                  tx, ty, ctx))
389             goto err;
390     }
391     /*
392      * Check if retrieved coordinates match originals and are less than field
393      * order: if not values are out of range.
394      */
395     if (BN_cmp(x, tx) || BN_cmp(y, ty)
396         || (BN_cmp(x, key->group->field) >= 0)
397         || (BN_cmp(y, key->group->field) >= 0)) {
398         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
399               EC_R_COORDINATES_OUT_OF_RANGE);
400         goto err;
401     }
402
403     if (!EC_KEY_set_public_key(key, point))
404         goto err;
405
406     if (EC_KEY_check_key(key) == 0)
407         goto err;
408
409     ok = 1;
410
411  err:
412     BN_CTX_end(ctx);
413     BN_CTX_free(ctx);
414     EC_POINT_free(point);
415     return ok;
416
417 }
418
419 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
420 {
421     return key->group;
422 }
423
424 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)
425 {
426     if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)
427         return 0;
428     EC_GROUP_free(key->group);
429     key->group = EC_GROUP_dup(group);
430     return (key->group == NULL) ? 0 : 1;
431 }
432
433 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key)
434 {
435     return key->priv_key;
436 }
437
438 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *priv_key)
439 {
440     if (key->group == NULL || key->group->meth == NULL)
441         return 0;
442     if (key->group->meth->set_private != NULL
443         && key->group->meth->set_private(key, priv_key) == 0)
444         return 0;
445     if (key->meth->set_private != NULL
446         && key->meth->set_private(key, priv_key) == 0)
447         return 0;
448     BN_clear_free(key->priv_key);
449     key->priv_key = BN_dup(priv_key);
450     return (key->priv_key == NULL) ? 0 : 1;
451 }
452
453 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key)
454 {
455     return key->pub_key;
456 }
457
458 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub_key)
459 {
460     if (key->meth->set_public != NULL
461         && key->meth->set_public(key, pub_key) == 0)
462         return 0;
463     EC_POINT_free(key->pub_key);
464     key->pub_key = EC_POINT_dup(pub_key, key->group);
465     return (key->pub_key == NULL) ? 0 : 1;
466 }
467
468 unsigned int EC_KEY_get_enc_flags(const EC_KEY *key)
469 {
470     return key->enc_flag;
471 }
472
473 void EC_KEY_set_enc_flags(EC_KEY *key, unsigned int flags)
474 {
475     key->enc_flag = flags;
476 }
477
478 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key)
479 {
480     return key->conv_form;
481 }
482
483 void EC_KEY_set_conv_form(EC_KEY *key, point_conversion_form_t cform)
484 {
485     key->conv_form = cform;
486     if (key->group != NULL)
487         EC_GROUP_set_point_conversion_form(key->group, cform);
488 }
489
490 void EC_KEY_set_asn1_flag(EC_KEY *key, int flag)
491 {
492     if (key->group != NULL)
493         EC_GROUP_set_asn1_flag(key->group, flag);
494 }
495
496 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
497 {
498     if (key->group == NULL)
499         return 0;
500     return EC_GROUP_precompute_mult(key->group, ctx);
501 }
502
503 int EC_KEY_get_flags(const EC_KEY *key)
504 {
505     return key->flags;
506 }
507
508 void EC_KEY_set_flags(EC_KEY *key, int flags)
509 {
510     key->flags |= flags;
511 }
512
513 void EC_KEY_clear_flags(EC_KEY *key, int flags)
514 {
515     key->flags &= ~flags;
516 }
517
518 size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
519                         unsigned char **pbuf, BN_CTX *ctx)
520 {
521     if (key == NULL || key->pub_key == NULL || key->group == NULL)
522         return 0;
523     return EC_POINT_point2buf(key->group, key->pub_key, form, pbuf, ctx);
524 }
525
526 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
527                    BN_CTX *ctx)
528 {
529     if (key == NULL || key->group == NULL)
530         return 0;
531     if (key->pub_key == NULL)
532         key->pub_key = EC_POINT_new(key->group);
533     if (key->pub_key == NULL)
534         return 0;
535     if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
536         return 0;
537     /*
538      * Save the point conversion form.
539      * For non-custom curves the first octet of the buffer (excluding
540      * the last significant bit) contains the point conversion form.
541      * EC_POINT_oct2point() has already performed sanity checking of
542      * the buffer so we know it is valid.
543      */
544     if ((key->group->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0)
545         key->conv_form = (point_conversion_form_t)(buf[0] & ~0x01);
546     return 1;
547 }
548
549 size_t EC_KEY_priv2oct(const EC_KEY *eckey,
550                        unsigned char *buf, size_t len)
551 {
552     if (eckey->group == NULL || eckey->group->meth == NULL)
553         return 0;
554     if (eckey->group->meth->priv2oct == NULL) {
555         ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
556         return 0;
557     }
558
559     return eckey->group->meth->priv2oct(eckey, buf, len);
560 }
561
562 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
563                               unsigned char *buf, size_t len)
564 {
565     size_t buf_len;
566
567     buf_len = (EC_GROUP_order_bits(eckey->group) + 7) / 8;
568     if (eckey->priv_key == NULL)
569         return 0;
570     if (buf == NULL)
571         return buf_len;
572     else if (len < buf_len)
573         return 0;
574
575     /* Octetstring may need leading zeros if BN is to short */
576
577     if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
578         ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
579         return 0;
580     }
581
582     return buf_len;
583 }
584
585 int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
586 {
587     if (eckey->group == NULL || eckey->group->meth == NULL)
588         return 0;
589     if (eckey->group->meth->oct2priv == NULL) {
590         ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
591         return 0;
592     }
593     return eckey->group->meth->oct2priv(eckey, buf, len);
594 }
595
596 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
597 {
598     if (eckey->priv_key == NULL)
599         eckey->priv_key = BN_secure_new();
600     if (eckey->priv_key == NULL) {
601         ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
602         return 0;
603     }
604     eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
605     if (eckey->priv_key == NULL) {
606         ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
607         return 0;
608     }
609     return 1;
610 }
611
612 size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf)
613 {
614     size_t len;
615     unsigned char *buf;
616     len = EC_KEY_priv2oct(eckey, NULL, 0);
617     if (len == 0)
618         return 0;
619     buf = OPENSSL_malloc(len);
620     if (buf == NULL)
621         return 0;
622     len = EC_KEY_priv2oct(eckey, buf, len);
623     if (len == 0) {
624         OPENSSL_free(buf);
625         return 0;
626     }
627     *pbuf = buf;
628     return len;
629 }
630
631 int EC_KEY_can_sign(const EC_KEY *eckey)
632 {
633     if (eckey->group == NULL || eckey->group->meth == NULL
634         || (eckey->group->meth->flags & EC_FLAGS_NO_SIGN))
635         return 0;
636     return 1;
637 }